blob: 15371ef112f7e0a1d2fbaa72c6131e95f082f49e [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 Nainaniea28dde2014-08-27 17:25:09 -070074 //SWE-FIXME : Enable once SWE geolocation api works.
75 //init();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070076 }
77
78 private void init() {
79 mGeolocationPermissions = GeolocationPermissions.getInstance();
80 mGeolocationPermissions.registerOnGeolocationPolicyModifiedListener(this);
81 mCurrentTabId = -1;
82 mCurrentOrigin = null;
83 mCurrentIncognito = false;
84
85 setOnClickListener(new View.OnClickListener() {
86 @Override
87 public void onClick(View v) {
88 if (!mCurrentOrigin.isEmpty()) {
89 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
90 final GeolocationPermissions geolocationPermissions =
91 (mCurrentIncognito ?
92 GeolocationPermissions.getIncognitoInstance() :
93 GeolocationPermissions.getInstance());
94
95 DialogInterface.OnClickListener alertDialogListener =
96 new AlertDialog.OnClickListener() {
97 public void onClick(DialogInterface dlg, int which) {
98 String origin = mCurrentOrigin;
99 int selectedPosition = ((AlertDialog)dlg)
100 .getListView().getCheckedItemPosition();
101 switch (selectedPosition) {
102 case 0: // Deny forever
103 geolocationPermissions.deny(origin);
104 break;
105 case 1: // Extend for 24 hours
106 // encode the expiration time and origin as a JSON string
107 JSONArray jsonArray = new JSONArray();
108 jsonArray.put(System.currentTimeMillis() + MILLIS_PER_DAY);
109 jsonArray.put(origin);
110 geolocationPermissions.allow(jsonArray.toString());
111 break;
112 case 2: // Allow forever
113 geolocationPermissions.allow(origin);
114 break;
115 case 3: // Always ask
116 geolocationPermissions.clear(origin);
117 break;
118 default:
119 break;
120 }
121 }};
122
123 builder.setTitle(String.format(getResources()
124 .getString(R.string.geolocation_settings_page_dialog_title),
125 "http".equals(Uri.parse(mCurrentOrigin).getScheme()) ?
126 mCurrentOrigin.substring(7) : mCurrentOrigin))
127 .setPositiveButton(R.string.geolocation_settings_page_dialog_ok_button,
128 alertDialogListener)
129 .setNegativeButton(R.string.geolocation_settings_page_dialog_cancel_button, null);
130
131 final ValueCallback<Long> getExpirationCallback = new ValueCallback<Long>() {
132 public void onReceiveValue(Long expirationTime) {
133 if (expirationTime != null) {
134 geolocationPolicyExpiration = expirationTime.longValue();
135 // Set radio button and location icon
136 if (!geolocationPolicyOriginAllowed) {
137 // 0: Deny forever
138 builder.setSingleChoiceItems(R.array.geolocation_settings_choices, 0, null);
139 } else {
140 if (geolocationPolicyExpiration
141 != GeolocationPermissions.DO_NOT_EXPIRE) {
142 // 1: Allow for 24 hours
143 builder.setSingleChoiceItems(R.array.geolocation_settings_choices, 1, null);
144 } else {
145 // 2: Allow forever
146 builder.setSingleChoiceItems(R.array.geolocation_settings_choices, 2, null);
147 }
148 }
149 }
150 builder.show();
151 }};
152
153 final ValueCallback<Boolean> getAllowedCallback = new ValueCallback<Boolean>() {
154 public void onReceiveValue(Boolean allowed) {
155 if (allowed != null) {
156 geolocationPolicyOriginAllowed = allowed.booleanValue();
157 //Get the policy expiration time
158 geolocationPermissions
159 .getExpirationTime(mCurrentOrigin, getExpirationCallback);
160 }
161 }};
162
163 geolocationPermissions.hasOrigin(mCurrentOrigin,
164 new ValueCallback<Boolean>() {
165 public void onReceiveValue(Boolean hasOrigin) {
166 if (hasOrigin != null && hasOrigin.booleanValue()) {
167 //Get whether origin is allowed or denied
168 geolocationPermissions.getAllowed(mCurrentOrigin,
169 getAllowedCallback);
170 }
171 }
172 });
173 }
174 }
175 });
176 }
177
178 public void onTabDataChanged(Tab tab) {
179 long tabId = tab.getId();
180 String origin = GeolocationPermissions.getOriginFromUrl(tab.getUrl());
181 boolean incognito = tab.isPrivateBrowsingEnabled();
182
183 if (mCurrentTabId != tabId) {
184 mCurrentTabId = tabId;
185 mCurrentOrigin = origin;
186
187 // Switch GeolocationPermissions if we went from a regular to an
188 // incognito tab or vice versa
189 if (mCurrentIncognito != incognito) {
190 mCurrentIncognito = incognito;
191 mGeolocationPermissions = mCurrentIncognito ?
192 GeolocationPermissions.getIncognitoInstance() :
193 GeolocationPermissions.getInstance();
194 mGeolocationPermissions.registerOnGeolocationPolicyModifiedListener(this);
195 }
196 update();
197 }
198 // Update icon if we are in the same tab and origin has changed
199 else if (!((mCurrentOrigin == null && origin == null) ||
200 (mCurrentOrigin != null && origin != null
201 && mCurrentOrigin.equals(origin)))) {
202 mCurrentOrigin = origin;
203 update();
204 }
205 }
206
207 public void update() {
208 if (mCurrentOrigin != null) {
209 mGeolocationPermissions.hasOrigin(mCurrentOrigin,
210 new ValueCallback<Boolean>() {
211 public void onReceiveValue(Boolean hasOrigin) {
212 if (hasOrigin != null && hasOrigin.booleanValue()) {
213 mGeolocationPermissions.getAllowed(mCurrentOrigin,
214 new ValueCallback<Boolean>() {
215 public void onReceiveValue(Boolean allowed) {
216 if (allowed != null) {
217 if (allowed.booleanValue()) {
218 LocationButton.this.setImageResource(R.drawable.ic_gps_on_holo_dark);
219 LocationButton.this.setVisibility(VISIBLE);
220 } else {
221 LocationButton.this.setImageResource(R.drawable.ic_gps_denied_holo_dark);
222 LocationButton.this.setVisibility(VISIBLE);
223 }
224 }
225 }
226 });
227 } else {
228 LocationButton.this.setVisibility(GONE);
229 }
230 }
231 });
232 } else {
233 this.setVisibility(GONE);
234 }
235 }
236
237 @Override
238 public void onGeolocationPolicyAdded(String origin, boolean allow) {
239 if (mCurrentOrigin != null && mCurrentOrigin.equals(origin)) {
240 this.setImageResource(allow ? R.drawable.ic_gps_on_holo_dark :
241 R.drawable.ic_gps_denied_holo_dark);
242 this.setVisibility(VISIBLE);
243 }
244 }
245
246 @Override
247 public void onGeolocationPolicyCleared(String origin) {
248 if (mCurrentOrigin != null && mCurrentOrigin.equals(origin)) {
249 this.setVisibility(GONE);
250 }
251 }
252
253 @Override
254 public void onGeolocationPolicyClearedAll() {
255 this.setVisibility(GONE);
256 }
257
258}