blob: a429b2d992b025dc783c66a1b0d2253a24bcd54b [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080018
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070019import org.codeaurora.swe.GeolocationPermissions;
Panos Thomasb298aad2014-10-22 12:24:21 -070020import org.codeaurora.swe.GeolocationPermissions.GeolocationPolicyChange;
21import org.codeaurora.swe.GeolocationPermissions.GeolocationPolicyListener;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070022import org.json.JSONArray;
23
Steve Block2bc69912009-07-30 14:45:13 +010024import android.content.Context;
Steve Block8844d192009-09-29 13:14:41 +010025import android.net.Uri;
Steve Block2bc69912009-07-30 14:45:13 +010026import android.util.AttributeSet;
Steve Blockc9f6d7a2009-10-16 18:19:08 +010027import android.view.Gravity;
Steve Block2bc69912009-07-30 14:45:13 +010028import android.view.View;
Steve Block2bc69912009-07-30 14:45:13 +010029import android.widget.Button;
30import android.widget.CheckBox;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070031import android.widget.CompoundButton;
Enrico Ros1f5a0952014-11-18 20:15:48 -080032import android.widget.LinearLayout;
Steve Block2bc69912009-07-30 14:45:13 +010033import android.widget.TextView;
Steve Blockc9f6d7a2009-10-16 18:19:08 +010034import android.widget.Toast;
Steve Block2bc69912009-07-30 14:45:13 +010035
Panos Thomasb298aad2014-10-22 12:24:21 -070036public class GeolocationPermissionsPrompt extends LinearLayout implements
37 GeolocationPolicyListener {
Steve Block2bc69912009-07-30 14:45:13 +010038 private TextView mMessage;
39 private Button mShareButton;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070040 private Button mShareForLimitedTimeButton;
Steve Block2bc69912009-07-30 14:45:13 +010041 private Button mDontShareButton;
42 private CheckBox mRemember;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070043 private android.webkit.GeolocationPermissions.Callback mCallback;
Steve Block2bc69912009-07-30 14:45:13 +010044 private String mOrigin;
Panos Thomasb298aad2014-10-22 12:24:21 -070045 private GeolocationPermissions mGeolocationPermissions;
Steve Block2bc69912009-07-30 14:45:13 +010046
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070047 private static final long MILLIS_PER_DAY = 86400000;
48
Steve Block2bc69912009-07-30 14:45:13 +010049 public GeolocationPermissionsPrompt(Context context) {
50 this(context, null);
51 }
52
53 public GeolocationPermissionsPrompt(Context context, AttributeSet attrs) {
54 super(context, attrs);
Grace Kloba50c241e2010-04-20 11:07:50 -070055 }
Steve Block2bc69912009-07-30 14:45:13 +010056
John Reck7c6e1c92011-06-30 11:55:55 -070057 @Override
58 protected void onFinishInflate() {
59 super.onFinishInflate();
John Reck7c6e1c92011-06-30 11:55:55 -070060 }
61
Panos Thomasb298aad2014-10-22 12:24:21 -070062 public void init(boolean privateBrowsing) {
Steve Block2bc69912009-07-30 14:45:13 +010063 mMessage = (TextView) findViewById(R.id.message);
64 mShareButton = (Button) findViewById(R.id.share_button);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070065 mShareForLimitedTimeButton = (Button)
66 findViewById(R.id.share_for_limited_time_button);
Steve Block2bc69912009-07-30 14:45:13 +010067 mDontShareButton = (Button) findViewById(R.id.dont_share_button);
68 mRemember = (CheckBox) findViewById(R.id.remember);
Grace Kloba50c241e2010-04-20 11:07:50 -070069
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070070 mRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
71 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
72 mShareForLimitedTimeButton.setEnabled(isChecked);
73 }
74 });
75
Grace Kloba50c241e2010-04-20 11:07:50 -070076 mShareButton.setOnClickListener(new View.OnClickListener() {
77 public void onClick(View v) {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070078 handleButtonClick(true, GeolocationPermissions.DO_NOT_EXPIRE);
79 }
80 });
81 mShareForLimitedTimeButton.setOnClickListener(new View.OnClickListener() {
82 public void onClick(View v) {
83 handleButtonClick(true, System.currentTimeMillis() + MILLIS_PER_DAY);
Grace Kloba50c241e2010-04-20 11:07:50 -070084 }
85 });
86 mDontShareButton.setOnClickListener(new View.OnClickListener() {
87 public void onClick(View v) {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070088 handleButtonClick(false, GeolocationPermissions.DO_NOT_EXPIRE);
Grace Kloba50c241e2010-04-20 11:07:50 -070089 }
90 });
Panos Thomasb298aad2014-10-22 12:24:21 -070091
92 mGeolocationPermissions = (privateBrowsing ? GeolocationPermissions.getIncognitoInstance()
93 : GeolocationPermissions.getInstance());
94 mGeolocationPermissions.addListener(this);
Steve Block2bc69912009-07-30 14:45:13 +010095 }
96
97 /**
98 * Shows the prompt for the given origin. When the user clicks on one of
99 * the buttons, the supplied callback is be called.
100 */
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700101 public void show(String origin,
102 android.webkit.GeolocationPermissions.Callback callback) {
Steve Block2bc69912009-07-30 14:45:13 +0100103 mOrigin = origin;
104 mCallback = callback;
Steve Block8844d192009-09-29 13:14:41 +0100105 Uri uri = Uri.parse(mOrigin);
106 setMessage("http".equals(uri.getScheme()) ? mOrigin.substring(7) : mOrigin);
Steve Block97557cf2009-09-30 17:46:24 +0100107 // The checkbox should always be intially checked.
108 mRemember.setChecked(true);
John Reck7c6e1c92011-06-30 11:55:55 -0700109 setVisibility(View.VISIBLE);
Steve Block2bc69912009-07-30 14:45:13 +0100110 }
111
112 /**
Panos Thomasb298aad2014-10-22 12:24:21 -0700113 * This method is called when the user modifies the geolocation policy in a different Tab.
114 */
115 public void onGeolocationPolicyChanged(GeolocationPolicyChange change) {
116 int action = change.getAction();
117
118 if (action == GeolocationPermissions.ALL_POLICIES_REMOVED) {
119 // do not dismiss when policy is removed
120 return;
121 } else if (change.getOrigin() != null && mOrigin.equals(change.getOrigin())) {
122 boolean allow = false;
123 switch (action) {
124 case GeolocationPermissions.ORIGIN_POLICY_REMOVED:
125 // do not dismiss when policy is removed
126 break;
127 case GeolocationPermissions.ORIGIN_ALLOWED:
128 allow = true;
129 case GeolocationPermissions.ORIGIN_DENIED:
130 hide();
131 JSONArray jsonArray = new JSONArray();
132 jsonArray.put(System.currentTimeMillis());
133 jsonArray.put(mOrigin);
134 // no need to retain policy since it has already been saved
135 mCallback.invoke(jsonArray.toString(), allow, false /*retain*/);
136 break;
137 default:
138 return;
139 }
140 }
141 }
142
143 /**
144 * This method is called when the user navigates to a new URL (onPageStarted). In this case,
145 * we respond as if user denied access without retaining the policy.
146 */
147 public void dismiss() {
148 hide();
149 JSONArray jsonArray = new JSONArray();
150 jsonArray.put(System.currentTimeMillis());
151 jsonArray.put(mOrigin);
152 mCallback.invoke(jsonArray.toString(), false /*allow*/, false /*retain*/);
153 }
154 /**
Steve Block2bc69912009-07-30 14:45:13 +0100155 * Hides the prompt.
156 */
157 public void hide() {
John Reck7c6e1c92011-06-30 11:55:55 -0700158 setVisibility(View.GONE);
Panos Thomasb298aad2014-10-22 12:24:21 -0700159 mGeolocationPermissions.removeListener(this);
Steve Block2bc69912009-07-30 14:45:13 +0100160 }
161
162 /**
Steve Block2bc69912009-07-30 14:45:13 +0100163 * Handles a click on one the buttons by invoking the callback.
164 */
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700165 private void handleButtonClick(boolean allow, long expirationTime) {
John Reck7c6e1c92011-06-30 11:55:55 -0700166 hide();
Steve Blockc9f6d7a2009-10-16 18:19:08 +0100167
168 boolean remember = mRemember.isChecked();
169 if (remember) {
170 Toast toast = Toast.makeText(
171 getContext(),
172 allow ? R.string.geolocation_permissions_prompt_toast_allowed :
173 R.string.geolocation_permissions_prompt_toast_disallowed,
174 Toast.LENGTH_LONG);
175 toast.setGravity(Gravity.BOTTOM, 0, 0);
176 toast.show();
177 }
178
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700179 // Encode the expirationTime and origin as a JSON string.
180 JSONArray jsonArray = new JSONArray();
181 jsonArray.put(expirationTime);
182 jsonArray.put(mOrigin);
183 mCallback.invoke(jsonArray.toString(), allow, remember);
Steve Block2bc69912009-07-30 14:45:13 +0100184 }
185
186 /**
187 * Sets the prompt's message.
188 */
189 private void setMessage(CharSequence origin) {
190 mMessage.setText(String.format(
191 getResources().getString(R.string.geolocation_permissions_prompt_message),
192 origin));
193 }
Steve Block2bc69912009-07-30 14:45:13 +0100194}