GeolocationPermissionsPrompt.java revision 2bc69918d97a767a02132090e58718077381ba1a
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.util.AttributeSet;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.webkit.WebView;
25import android.webkit.GeolocationPermissions;
26import android.widget.Button;
27import android.widget.CheckBox;
28import android.widget.LinearLayout;
29import android.widget.TextView;
30
31public class GeolocationPermissionsPrompt extends LinearLayout {
32    private LinearLayout mInner;
33    private TextView mMessage;
34    private Button mShareButton;
35    private Button mDontShareButton;
36    private CheckBox mRemember;
37    private GeolocationPermissions.Callback mCallback;
38    private String mOrigin;
39
40    public GeolocationPermissionsPrompt(Context context) {
41        this(context, null);
42    }
43
44    public GeolocationPermissionsPrompt(Context context, AttributeSet attrs) {
45        super(context, attrs);
46        LayoutInflater factory = LayoutInflater.from(context);
47        factory.inflate(R.layout.geolocation_permissions_prompt, this);
48
49        mInner = (LinearLayout) findViewById(R.id.inner);
50        mMessage = (TextView) findViewById(R.id.message);
51        mShareButton = (Button) findViewById(R.id.share_button);
52        mDontShareButton = (Button) findViewById(R.id.dont_share_button);
53        mRemember = (CheckBox) findViewById(R.id.remember);
54        setButtonClickListeners();
55    }
56
57    /**
58     * Shows the prompt for the given origin. When the user clicks on one of
59     * the buttons, the supplied callback is be called.
60     */
61    public void show(String origin, GeolocationPermissions.Callback callback) {
62        mOrigin = origin;
63        mCallback = callback;
64        setMessage(mOrigin);
65        showDialog(true);
66    }
67
68    /**
69     * Hides the prompt.
70     */
71    public void hide() {
72        showDialog(false);
73    }
74
75    /**
76     * Sets the on click listeners for the buttons.
77     */
78    private void setButtonClickListeners() {
79        final GeolocationPermissionsPrompt me = this;
80        mShareButton.setOnClickListener(new View.OnClickListener() {
81            public void onClick(View v) {
82                me.handleButtonClick(true);
83            }
84        });
85        mDontShareButton.setOnClickListener(new View.OnClickListener() {
86            public void onClick(View v) {
87                me.handleButtonClick(false);
88            }
89        });
90    }
91
92    /**
93     * Handles a click on one the buttons by invoking the callback.
94     */
95    private void handleButtonClick(boolean allow) {
96        boolean remember = mRemember.isChecked();
97        showDialog(false);
98        mCallback.invoke(mOrigin, allow, remember);
99    }
100
101    /**
102     * Sets the prompt's message.
103     */
104    private void setMessage(CharSequence origin) {
105        mMessage.setText(String.format(
106            getResources().getString(R.string.geolocation_permissions_prompt_message),
107            origin));
108    }
109
110    /**
111     * Shows or hides the prompt.
112     */
113    private void showDialog(boolean shown) {
114        mInner.setVisibility(shown ? View.VISIBLE : View.GONE);
115    }
116}
117