blob: afbf39f0401515adf091bfc22dd680c67ef3f1eb [file] [log] [blame]
Steve Block2bc69912009-07-30 14:45:13 +01001/*
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;
Steve Block8844d192009-09-29 13:14:41 +010020import android.net.Uri;
Steve Block2bc69912009-07-30 14:45:13 +010021import android.util.AttributeSet;
Steve Blockc9f6d7a2009-10-16 18:19:08 +010022import android.view.Gravity;
Steve Block2bc69912009-07-30 14:45:13 +010023import android.view.View;
Steve Block2bc69912009-07-30 14:45:13 +010024import android.webkit.GeolocationPermissions;
25import android.widget.Button;
26import android.widget.CheckBox;
John Reck7c6e1c92011-06-30 11:55:55 -070027import android.widget.RelativeLayout;
Steve Block2bc69912009-07-30 14:45:13 +010028import android.widget.TextView;
Steve Blockc9f6d7a2009-10-16 18:19:08 +010029import android.widget.Toast;
Steve Block2bc69912009-07-30 14:45:13 +010030
John Reck7c6e1c92011-06-30 11:55:55 -070031public class GeolocationPermissionsPrompt extends RelativeLayout {
Steve Block2bc69912009-07-30 14:45:13 +010032 private TextView mMessage;
33 private Button mShareButton;
34 private Button mDontShareButton;
35 private CheckBox mRemember;
36 private GeolocationPermissions.Callback mCallback;
37 private String mOrigin;
38
39 public GeolocationPermissionsPrompt(Context context) {
40 this(context, null);
41 }
42
43 public GeolocationPermissionsPrompt(Context context, AttributeSet attrs) {
44 super(context, attrs);
Grace Kloba50c241e2010-04-20 11:07:50 -070045 }
Steve Block2bc69912009-07-30 14:45:13 +010046
John Reck7c6e1c92011-06-30 11:55:55 -070047 @Override
48 protected void onFinishInflate() {
49 super.onFinishInflate();
50 init();
51 }
52
53 private void init() {
Steve Block2bc69912009-07-30 14:45:13 +010054 mMessage = (TextView) findViewById(R.id.message);
55 mShareButton = (Button) findViewById(R.id.share_button);
56 mDontShareButton = (Button) findViewById(R.id.dont_share_button);
57 mRemember = (CheckBox) findViewById(R.id.remember);
Grace Kloba50c241e2010-04-20 11:07:50 -070058
Grace Kloba50c241e2010-04-20 11:07:50 -070059 mShareButton.setOnClickListener(new View.OnClickListener() {
60 public void onClick(View v) {
John Reck7c6e1c92011-06-30 11:55:55 -070061 handleButtonClick(true);
Grace Kloba50c241e2010-04-20 11:07:50 -070062 }
63 });
64 mDontShareButton.setOnClickListener(new View.OnClickListener() {
65 public void onClick(View v) {
John Reck7c6e1c92011-06-30 11:55:55 -070066 handleButtonClick(false);
Grace Kloba50c241e2010-04-20 11:07:50 -070067 }
68 });
Steve Block2bc69912009-07-30 14:45:13 +010069 }
70
71 /**
72 * Shows the prompt for the given origin. When the user clicks on one of
73 * the buttons, the supplied callback is be called.
74 */
75 public void show(String origin, GeolocationPermissions.Callback callback) {
76 mOrigin = origin;
77 mCallback = callback;
Steve Block8844d192009-09-29 13:14:41 +010078 Uri uri = Uri.parse(mOrigin);
79 setMessage("http".equals(uri.getScheme()) ? mOrigin.substring(7) : mOrigin);
Steve Block97557cf2009-09-30 17:46:24 +010080 // The checkbox should always be intially checked.
81 mRemember.setChecked(true);
John Reck7c6e1c92011-06-30 11:55:55 -070082 setVisibility(View.VISIBLE);
Steve Block2bc69912009-07-30 14:45:13 +010083 }
84
85 /**
86 * Hides the prompt.
87 */
88 public void hide() {
John Reck7c6e1c92011-06-30 11:55:55 -070089 setVisibility(View.GONE);
Steve Block2bc69912009-07-30 14:45:13 +010090 }
91
92 /**
Steve Block2bc69912009-07-30 14:45:13 +010093 * Handles a click on one the buttons by invoking the callback.
94 */
95 private void handleButtonClick(boolean allow) {
John Reck7c6e1c92011-06-30 11:55:55 -070096 hide();
Steve Blockc9f6d7a2009-10-16 18:19:08 +010097
98 boolean remember = mRemember.isChecked();
99 if (remember) {
100 Toast toast = Toast.makeText(
101 getContext(),
102 allow ? R.string.geolocation_permissions_prompt_toast_allowed :
103 R.string.geolocation_permissions_prompt_toast_disallowed,
104 Toast.LENGTH_LONG);
105 toast.setGravity(Gravity.BOTTOM, 0, 0);
106 toast.show();
107 }
108
Steve Block2bc69912009-07-30 14:45:13 +0100109 mCallback.invoke(mOrigin, allow, remember);
110 }
111
112 /**
113 * Sets the prompt's message.
114 */
115 private void setMessage(CharSequence origin) {
116 mMessage.setText(String.format(
117 getResources().getString(R.string.geolocation_permissions_prompt_message),
118 origin));
119 }
Steve Block2bc69912009-07-30 14:45:13 +0100120}