blob: c80c0e8ecbfc2c6cb8982a894bb1e407667ce80f [file] [log] [blame]
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001/*
2 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30
31package com.android.browser;
32
33import org.codeaurora.swe.GeolocationPermissions;
34import org.codeaurora.swe.GeolocationPermissions.OnGeolocationPolicyModifiedListener;
35import org.json.JSONArray;
36
37import android.app.AlertDialog;
38import android.content.Context;
39import android.content.DialogInterface;
40import android.net.Uri;
41import android.util.AttributeSet;
42import android.view.View;
43import android.webkit.ValueCallback;
44import android.widget.ImageButton;
45
46public class LocationButton extends ImageButton
47 implements OnGeolocationPolicyModifiedListener {
48 private GeolocationPermissions mGeolocationPermissions;
49 private long mCurrentTabId;
50 private String mCurrentOrigin;
51 private boolean mCurrentIncognito;
52
53 private static final long MILLIS_PER_DAY = 86400000;
54
55 protected long geolocationPolicyExpiration;
56 protected boolean geolocationPolicyOriginAllowed;
57
58 public LocationButton(Context context) {
59 super(context);
60 }
61
62 public LocationButton(Context context, AttributeSet attrs) {
63 super(context, attrs);
64
65 }
66
67 public LocationButton(Context context, AttributeSet attrs, int defStyle) {
68 super(context, attrs, defStyle);
69 }
70
71 @Override
72 protected void onFinishInflate() {
73 super.onFinishInflate();
Tarun Nainani8eb00912014-07-17 12:28:32 -070074 init();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070075 }
76
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -070077 private void updateGeolocationPermissions() {
78 mGeolocationPermissions = mCurrentIncognito ?
79 GeolocationPermissions.getIncognitoInstance() :
80 GeolocationPermissions.getInstance();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070081 mGeolocationPermissions.registerOnGeolocationPolicyModifiedListener(this);
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -070082 }
83
84 // TODO: Perform this initilalization only after the engine initialization is complete.
85 private void init() {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070086 mCurrentTabId = -1;
87 mCurrentOrigin = null;
88 mCurrentIncognito = false;
89
90 setOnClickListener(new View.OnClickListener() {
91 @Override
92 public void onClick(View v) {
93 if (!mCurrentOrigin.isEmpty()) {
94 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -070095 updateGeolocationPermissions();
96 final GeolocationPermissions geolocationPermissions = mGeolocationPermissions;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070097 DialogInterface.OnClickListener alertDialogListener =
98 new AlertDialog.OnClickListener() {
99 public void onClick(DialogInterface dlg, int which) {
100 String origin = mCurrentOrigin;
101 int selectedPosition = ((AlertDialog)dlg)
102 .getListView().getCheckedItemPosition();
103 switch (selectedPosition) {
104 case 0: // Deny forever
105 geolocationPermissions.deny(origin);
106 break;
107 case 1: // Extend for 24 hours
108 // encode the expiration time and origin as a JSON string
109 JSONArray jsonArray = new JSONArray();
110 jsonArray.put(System.currentTimeMillis() + MILLIS_PER_DAY);
111 jsonArray.put(origin);
112 geolocationPermissions.allow(jsonArray.toString());
113 break;
114 case 2: // Allow forever
115 geolocationPermissions.allow(origin);
116 break;
117 case 3: // Always ask
118 geolocationPermissions.clear(origin);
119 break;
120 default:
121 break;
122 }
123 }};
124
125 builder.setTitle(String.format(getResources()
126 .getString(R.string.geolocation_settings_page_dialog_title),
127 "http".equals(Uri.parse(mCurrentOrigin).getScheme()) ?
128 mCurrentOrigin.substring(7) : mCurrentOrigin))
129 .setPositiveButton(R.string.geolocation_settings_page_dialog_ok_button,
130 alertDialogListener)
131 .setNegativeButton(R.string.geolocation_settings_page_dialog_cancel_button, null);
132
133 final ValueCallback<Long> getExpirationCallback = new ValueCallback<Long>() {
134 public void onReceiveValue(Long expirationTime) {
135 if (expirationTime != null) {
136 geolocationPolicyExpiration = expirationTime.longValue();
137 // Set radio button and location icon
138 if (!geolocationPolicyOriginAllowed) {
139 // 0: Deny forever
140 builder.setSingleChoiceItems(R.array.geolocation_settings_choices, 0, null);
141 } else {
142 if (geolocationPolicyExpiration
143 != GeolocationPermissions.DO_NOT_EXPIRE) {
144 // 1: Allow for 24 hours
145 builder.setSingleChoiceItems(R.array.geolocation_settings_choices, 1, null);
146 } else {
147 // 2: Allow forever
148 builder.setSingleChoiceItems(R.array.geolocation_settings_choices, 2, null);
149 }
150 }
151 }
152 builder.show();
153 }};
154
155 final ValueCallback<Boolean> getAllowedCallback = new ValueCallback<Boolean>() {
156 public void onReceiveValue(Boolean allowed) {
157 if (allowed != null) {
158 geolocationPolicyOriginAllowed = allowed.booleanValue();
159 //Get the policy expiration time
160 geolocationPermissions
161 .getExpirationTime(mCurrentOrigin, getExpirationCallback);
162 }
163 }};
164
165 geolocationPermissions.hasOrigin(mCurrentOrigin,
166 new ValueCallback<Boolean>() {
167 public void onReceiveValue(Boolean hasOrigin) {
168 if (hasOrigin != null && hasOrigin.booleanValue()) {
169 //Get whether origin is allowed or denied
170 geolocationPermissions.getAllowed(mCurrentOrigin,
171 getAllowedCallback);
172 }
173 }
174 });
175 }
176 }
177 });
178 }
179
180 public void onTabDataChanged(Tab tab) {
181 long tabId = tab.getId();
182 String origin = GeolocationPermissions.getOriginFromUrl(tab.getUrl());
183 boolean incognito = tab.isPrivateBrowsingEnabled();
184
185 if (mCurrentTabId != tabId) {
186 mCurrentTabId = tabId;
187 mCurrentOrigin = origin;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700188 update();
189 }
190 // Update icon if we are in the same tab and origin has changed
191 else if (!((mCurrentOrigin == null && origin == null) ||
192 (mCurrentOrigin != null && origin != null
193 && mCurrentOrigin.equals(origin)))) {
194 mCurrentOrigin = origin;
195 update();
196 }
197 }
198
199 public void update() {
200 if (mCurrentOrigin != null) {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700201 updateGeolocationPermissions();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700202 mGeolocationPermissions.hasOrigin(mCurrentOrigin,
203 new ValueCallback<Boolean>() {
204 public void onReceiveValue(Boolean hasOrigin) {
205 if (hasOrigin != null && hasOrigin.booleanValue()) {
206 mGeolocationPermissions.getAllowed(mCurrentOrigin,
207 new ValueCallback<Boolean>() {
208 public void onReceiveValue(Boolean allowed) {
209 if (allowed != null) {
210 if (allowed.booleanValue()) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800211 LocationButton.this.setImageResource(R.drawable.ic_action_gps_on);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700212 LocationButton.this.setVisibility(VISIBLE);
213 } else {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800214 LocationButton.this.setImageResource(R.drawable.ic_action_gps_off);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700215 LocationButton.this.setVisibility(VISIBLE);
216 }
217 }
218 }
219 });
220 } else {
221 LocationButton.this.setVisibility(GONE);
222 }
223 }
224 });
225 } else {
226 this.setVisibility(GONE);
227 }
228 }
229
230 @Override
231 public void onGeolocationPolicyAdded(String origin, boolean allow) {
232 if (mCurrentOrigin != null && mCurrentOrigin.equals(origin)) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800233 this.setImageResource(allow ? R.drawable.ic_action_gps_on :
234 R.drawable.ic_action_gps_off);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700235 this.setVisibility(VISIBLE);
236 }
237 }
238
239 @Override
240 public void onGeolocationPolicyCleared(String origin) {
241 if (mCurrentOrigin != null && mCurrentOrigin.equals(origin)) {
242 this.setVisibility(GONE);
243 }
244 }
245
246 @Override
247 public void onGeolocationPolicyClearedAll() {
248 this.setVisibility(GONE);
249 }
250
251}