GeolocationPermissionsPrompt.java revision 97557cf41b5f26d4d653a89e6551d8155fda18ed
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
19import android.content.Context;
20import android.graphics.drawable.Drawable;
21import android.net.Uri;
22import android.util.AttributeSet;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.webkit.WebView;
26import android.webkit.GeolocationPermissions;
27import android.widget.Button;
28import android.widget.CheckBox;
29import android.widget.LinearLayout;
30import android.widget.TextView;
31
32public class GeolocationPermissionsPrompt extends LinearLayout {
33    private LinearLayout mInner;
34    private TextView mMessage;
35    private Button mShareButton;
36    private Button mDontShareButton;
37    private CheckBox mRemember;
38    private GeolocationPermissions.Callback mCallback;
39    private String mOrigin;
40
41    public GeolocationPermissionsPrompt(Context context) {
42        this(context, null);
43    }
44
45    public GeolocationPermissionsPrompt(Context context, AttributeSet attrs) {
46        super(context, attrs);
47        LayoutInflater factory = LayoutInflater.from(context);
48        factory.inflate(R.layout.geolocation_permissions_prompt, this);
49
50        mInner = (LinearLayout) findViewById(R.id.inner);
51        mMessage = (TextView) findViewById(R.id.message);
52        mShareButton = (Button) findViewById(R.id.share_button);
53        mDontShareButton = (Button) findViewById(R.id.dont_share_button);
54        mRemember = (CheckBox) findViewById(R.id.remember);
55        setButtonClickListeners();
56    }
57
58    /**
59     * Shows the prompt for the given origin. When the user clicks on one of
60     * the buttons, the supplied callback is be called.
61     */
62    public void show(String origin, GeolocationPermissions.Callback callback) {
63        mOrigin = origin;
64        mCallback = callback;
65        Uri uri = Uri.parse(mOrigin);
66        setMessage("http".equals(uri.getScheme()) ?  mOrigin.substring(7) : mOrigin);
67        // The checkbox should always be intially checked.
68        mRemember.setChecked(true);
69        showDialog(true);
70    }
71
72    /**
73     * Hides the prompt.
74     */
75    public void hide() {
76        showDialog(false);
77    }
78
79    /**
80     * Sets the on click listeners for the buttons.
81     */
82    private void setButtonClickListeners() {
83        final GeolocationPermissionsPrompt me = this;
84        mShareButton.setOnClickListener(new View.OnClickListener() {
85            public void onClick(View v) {
86                me.handleButtonClick(true);
87            }
88        });
89        mDontShareButton.setOnClickListener(new View.OnClickListener() {
90            public void onClick(View v) {
91                me.handleButtonClick(false);
92            }
93        });
94    }
95
96    /**
97     * Handles a click on one the buttons by invoking the callback.
98     */
99    private void handleButtonClick(boolean allow) {
100        boolean remember = mRemember.isChecked();
101        showDialog(false);
102        mCallback.invoke(mOrigin, allow, remember);
103    }
104
105    /**
106     * Sets the prompt's message.
107     */
108    private void setMessage(CharSequence origin) {
109        mMessage.setText(String.format(
110            getResources().getString(R.string.geolocation_permissions_prompt_message),
111            origin));
112    }
113
114    /**
115     * Shows or hides the prompt.
116     */
117    private void showDialog(boolean shown) {
118        mInner.setVisibility(shown ? View.VISIBLE : View.GONE);
119    }
120}
121