Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 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 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 17 | package com.android.browser; |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 18 | |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 19 | import org.codeaurora.swe.GeolocationPermissions; |
Panos Thomas | b298aad | 2014-10-22 12:24:21 -0700 | [diff] [blame] | 20 | import org.codeaurora.swe.GeolocationPermissions.GeolocationPolicyChange; |
| 21 | import org.codeaurora.swe.GeolocationPermissions.GeolocationPolicyListener; |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 22 | import org.json.JSONArray; |
| 23 | |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 24 | import android.content.Context; |
Steve Block | 8844d19 | 2009-09-29 13:14:41 +0100 | [diff] [blame] | 25 | import android.net.Uri; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 26 | import android.util.AttributeSet; |
Steve Block | c9f6d7a | 2009-10-16 18:19:08 +0100 | [diff] [blame] | 27 | import android.view.Gravity; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 28 | import android.view.View; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 29 | import android.widget.Button; |
| 30 | import android.widget.CheckBox; |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 31 | import android.widget.CompoundButton; |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 32 | import android.widget.LinearLayout; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 33 | import android.widget.TextView; |
Steve Block | c9f6d7a | 2009-10-16 18:19:08 +0100 | [diff] [blame] | 34 | import android.widget.Toast; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 35 | |
Panos Thomas | b298aad | 2014-10-22 12:24:21 -0700 | [diff] [blame] | 36 | public class GeolocationPermissionsPrompt extends LinearLayout implements |
| 37 | GeolocationPolicyListener { |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 38 | private TextView mMessage; |
| 39 | private Button mShareButton; |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 40 | private Button mShareForLimitedTimeButton; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 41 | private Button mDontShareButton; |
| 42 | private CheckBox mRemember; |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 43 | private android.webkit.GeolocationPermissions.Callback mCallback; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 44 | private String mOrigin; |
Panos Thomas | b298aad | 2014-10-22 12:24:21 -0700 | [diff] [blame] | 45 | private GeolocationPermissions mGeolocationPermissions; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 46 | |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 47 | private static final long MILLIS_PER_DAY = 86400000; |
| 48 | |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 49 | public GeolocationPermissionsPrompt(Context context) { |
| 50 | this(context, null); |
| 51 | } |
| 52 | |
| 53 | public GeolocationPermissionsPrompt(Context context, AttributeSet attrs) { |
| 54 | super(context, attrs); |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 55 | } |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 56 | |
John Reck | 7c6e1c9 | 2011-06-30 11:55:55 -0700 | [diff] [blame] | 57 | @Override |
| 58 | protected void onFinishInflate() { |
| 59 | super.onFinishInflate(); |
John Reck | 7c6e1c9 | 2011-06-30 11:55:55 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Panos Thomas | b298aad | 2014-10-22 12:24:21 -0700 | [diff] [blame] | 62 | public void init(boolean privateBrowsing) { |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 63 | mMessage = (TextView) findViewById(R.id.message); |
| 64 | mShareButton = (Button) findViewById(R.id.share_button); |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 65 | mShareForLimitedTimeButton = (Button) |
| 66 | findViewById(R.id.share_for_limited_time_button); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 67 | mDontShareButton = (Button) findViewById(R.id.dont_share_button); |
| 68 | mRemember = (CheckBox) findViewById(R.id.remember); |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 69 | |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 70 | mRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { |
| 71 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 72 | mShareForLimitedTimeButton.setEnabled(isChecked); |
| 73 | } |
| 74 | }); |
| 75 | |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 76 | mShareButton.setOnClickListener(new View.OnClickListener() { |
| 77 | public void onClick(View v) { |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 78 | 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 Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 84 | } |
| 85 | }); |
| 86 | mDontShareButton.setOnClickListener(new View.OnClickListener() { |
| 87 | public void onClick(View v) { |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 88 | handleButtonClick(false, GeolocationPermissions.DO_NOT_EXPIRE); |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 89 | } |
| 90 | }); |
Panos Thomas | b298aad | 2014-10-22 12:24:21 -0700 | [diff] [blame] | 91 | |
| 92 | mGeolocationPermissions = (privateBrowsing ? GeolocationPermissions.getIncognitoInstance() |
| 93 | : GeolocationPermissions.getInstance()); |
| 94 | mGeolocationPermissions.addListener(this); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 95 | } |
| 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. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 101 | public void show(String origin, |
| 102 | android.webkit.GeolocationPermissions.Callback callback) { |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 103 | mOrigin = origin; |
| 104 | mCallback = callback; |
Steve Block | 8844d19 | 2009-09-29 13:14:41 +0100 | [diff] [blame] | 105 | Uri uri = Uri.parse(mOrigin); |
| 106 | setMessage("http".equals(uri.getScheme()) ? mOrigin.substring(7) : mOrigin); |
Steve Block | 97557cf | 2009-09-30 17:46:24 +0100 | [diff] [blame] | 107 | // The checkbox should always be intially checked. |
| 108 | mRemember.setChecked(true); |
John Reck | 7c6e1c9 | 2011-06-30 11:55:55 -0700 | [diff] [blame] | 109 | setVisibility(View.VISIBLE); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
Panos Thomas | b298aad | 2014-10-22 12:24:21 -0700 | [diff] [blame] | 113 | * 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 Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 155 | * Hides the prompt. |
| 156 | */ |
| 157 | public void hide() { |
John Reck | 7c6e1c9 | 2011-06-30 11:55:55 -0700 | [diff] [blame] | 158 | setVisibility(View.GONE); |
Panos Thomas | b298aad | 2014-10-22 12:24:21 -0700 | [diff] [blame] | 159 | mGeolocationPermissions.removeListener(this); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 163 | * Handles a click on one the buttons by invoking the callback. |
| 164 | */ |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 165 | private void handleButtonClick(boolean allow, long expirationTime) { |
John Reck | 7c6e1c9 | 2011-06-30 11:55:55 -0700 | [diff] [blame] | 166 | hide(); |
Steve Block | c9f6d7a | 2009-10-16 18:19:08 +0100 | [diff] [blame] | 167 | |
| 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. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 179 | // 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 Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 184 | } |
| 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 Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 194 | } |