2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.browser;
19 import android.content.Context;
20 import android.graphics.drawable.Drawable;
21 import android.net.Uri;
22 import android.util.AttributeSet;
23 import android.view.Gravity;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.webkit.WebView;
27 import android.webkit.GeolocationPermissions;
28 import android.widget.Button;
29 import android.widget.CheckBox;
30 import android.widget.LinearLayout;
31 import android.widget.TextView;
32 import android.widget.Toast;
34 public class GeolocationPermissionsPrompt extends LinearLayout {
35 private LinearLayout mInner;
36 private TextView mMessage;
37 private Button mShareButton;
38 private Button mDontShareButton;
39 private CheckBox mRemember;
40 private GeolocationPermissions.Callback mCallback;
41 private String mOrigin;
43 public GeolocationPermissionsPrompt(Context context) {
47 public GeolocationPermissionsPrompt(Context context, AttributeSet attrs) {
48 super(context, attrs);
49 LayoutInflater factory = LayoutInflater.from(context);
50 factory.inflate(R.layout.geolocation_permissions_prompt, this);
52 mInner = (LinearLayout) findViewById(R.id.inner);
53 mMessage = (TextView) findViewById(R.id.message);
54 mShareButton = (Button) findViewById(R.id.share_button);
55 mDontShareButton = (Button) findViewById(R.id.dont_share_button);
56 mRemember = (CheckBox) findViewById(R.id.remember);
57 setButtonClickListeners();
61 * Shows the prompt for the given origin. When the user clicks on one of
62 * the buttons, the supplied callback is be called.
64 public void show(String origin, GeolocationPermissions.Callback callback) {
67 Uri uri = Uri.parse(mOrigin);
68 setMessage("http".equals(uri.getScheme()) ? mOrigin.substring(7) : mOrigin);
69 // The checkbox should always be intially checked.
70 mRemember.setChecked(true);
82 * Sets the on click listeners for the buttons.
84 private void setButtonClickListeners() {
85 final GeolocationPermissionsPrompt me = this;
86 mShareButton.setOnClickListener(new View.OnClickListener() {
87 public void onClick(View v) {
88 me.handleButtonClick(true);
91 mDontShareButton.setOnClickListener(new View.OnClickListener() {
92 public void onClick(View v) {
93 me.handleButtonClick(false);
99 * Handles a click on one the buttons by invoking the callback.
101 private void handleButtonClick(boolean allow) {
104 boolean remember = mRemember.isChecked();
106 Toast toast = Toast.makeText(
108 allow ? R.string.geolocation_permissions_prompt_toast_allowed :
109 R.string.geolocation_permissions_prompt_toast_disallowed,
111 toast.setGravity(Gravity.BOTTOM, 0, 0);
115 mCallback.invoke(mOrigin, allow, remember);
119 * Sets the prompt's message.
121 private void setMessage(CharSequence origin) {
122 mMessage.setText(String.format(
123 getResources().getString(R.string.geolocation_permissions_prompt_message),
128 * Shows or hides the prompt.
130 private void showDialog(boolean shown) {
131 mInner.setVisibility(shown ? View.VISIBLE : View.GONE);