blob: 430286f6cda55406d7ea7ec4777550d2ae532e10 [file] [log] [blame]
Nicolas Roarde46990e2009-06-19 16:27:49 +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.app.AlertDialog;
20import android.app.ListActivity;
21import android.content.Context;
22import android.content.DialogInterface;
23import android.database.Cursor;
24import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
26import android.net.Uri;
27import android.os.Bundle;
28import android.provider.Browser;
29import android.util.Log;
30import android.view.KeyEvent;
31import android.view.LayoutInflater;
Ben Murdochb9daacb2009-09-14 10:48:24 +010032import android.view.Menu;
33import android.view.MenuInflater;
34import android.view.MenuItem;
Nicolas Roarde46990e2009-06-19 16:27:49 +010035import android.view.View;
36import android.view.ViewGroup;
Steve Blockf344d032009-07-30 10:50:45 +010037import android.webkit.GeolocationPermissions;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010038import android.webkit.ValueCallback;
Nicolas Roarde46990e2009-06-19 16:27:49 +010039import android.webkit.WebIconDatabase;
40import android.webkit.WebStorage;
41import android.widget.ArrayAdapter;
42import android.widget.AdapterView;
43import android.widget.AdapterView.OnItemClickListener;
44import android.widget.ImageView;
45import android.widget.TextView;
46
47import java.util.HashMap;
Steve Blockee0d6392009-07-28 13:49:15 +010048import java.util.HashSet;
Nicolas Roarde46990e2009-06-19 16:27:49 +010049import java.util.Iterator;
Steve Block089ce3a2009-07-29 17:16:01 +010050import java.util.Map;
Nicolas Roarde46990e2009-06-19 16:27:49 +010051import java.util.Set;
52import java.util.Vector;
53
54/**
55 * Manage the settings for an origin.
Steve Block089ce3a2009-07-29 17:16:01 +010056 * We use it to keep track of the 'HTML5' settings, i.e. database (webstorage)
57 * and Geolocation.
Nicolas Roarde46990e2009-06-19 16:27:49 +010058 */
59public class WebsiteSettingsActivity extends ListActivity {
60
61 private String LOGTAG = "WebsiteSettingsActivity";
62 private static String sMBStored = null;
63 private SiteAdapter mAdapter = null;
64
65 class Site {
66 private String mOrigin;
67 private String mTitle;
68 private Bitmap mIcon;
Steve Block089ce3a2009-07-29 17:16:01 +010069 private int mFeatures;
70
71 // These constants provide the set of features that a site may support
72 // They must be consecutive. To add a new feature, add a new FEATURE_XXX
73 // variable with value equal to the current value of FEATURE_COUNT, then
74 // increment FEATURE_COUNT.
75 private final static int FEATURE_WEB_STORAGE = 0;
Steve Blockf344d032009-07-30 10:50:45 +010076 private final static int FEATURE_GEOLOCATION = 1;
Steve Block089ce3a2009-07-29 17:16:01 +010077 // The number of features available.
Steve Blockf344d032009-07-30 10:50:45 +010078 private final static int FEATURE_COUNT = 2;
Nicolas Roarde46990e2009-06-19 16:27:49 +010079
Steve Block1ad98cf2009-07-28 11:07:10 +010080 public Site(String origin) {
Nicolas Roarde46990e2009-06-19 16:27:49 +010081 mOrigin = origin;
Steve Block1ad98cf2009-07-28 11:07:10 +010082 mTitle = null;
83 mIcon = null;
Steve Block089ce3a2009-07-29 17:16:01 +010084 mFeatures = 0;
85 }
86
87 public void addFeature(int feature) {
88 mFeatures |= (1 << feature);
89 }
90
Ben Murdochbe9560d2009-11-09 09:52:21 -080091 public void removeFeature(int feature) {
92 mFeatures &= ~(1 << feature);
93 }
94
Steve Block089ce3a2009-07-29 17:16:01 +010095 public boolean hasFeature(int feature) {
96 return (mFeatures & (1 << feature)) != 0;
97 }
98
99 /**
100 * Gets the number of features supported by this site.
101 */
102 public int getFeatureCount() {
103 int count = 0;
104 for (int i = 0; i < FEATURE_COUNT; ++i) {
105 count += hasFeature(i) ? 1 : 0;
106 }
107 return count;
108 }
109
110 /**
111 * Gets the ID of the nth (zero-based) feature supported by this site.
112 * The return value is a feature ID - one of the FEATURE_XXX values.
113 * This is required to determine which feature is displayed at a given
114 * position in the list of features for this site. This is used both
Steve Blockf344d032009-07-30 10:50:45 +0100115 * when populating the view and when responding to clicks on the list.
Steve Block089ce3a2009-07-29 17:16:01 +0100116 */
117 public int getFeatureByIndex(int n) {
118 int j = -1;
119 for (int i = 0; i < FEATURE_COUNT; ++i) {
120 j += hasFeature(i) ? 1 : 0;
121 if (j == n) {
122 return i;
123 }
124 }
125 return -1;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100126 }
127
128 public String getOrigin() {
129 return mOrigin;
130 }
131
132 public void setTitle(String title) {
133 mTitle = title;
134 }
135
Nicolas Roarde46990e2009-06-19 16:27:49 +0100136 public void setIcon(Bitmap icon) {
137 mIcon = icon;
138 }
139
140 public Bitmap getIcon() {
141 return mIcon;
142 }
Steve Block1ad98cf2009-07-28 11:07:10 +0100143
144 public String getPrettyOrigin() {
145 return mTitle == null ? null : hideHttp(mOrigin);
146 }
147
148 public String getPrettyTitle() {
149 return mTitle == null ? hideHttp(mOrigin) : mTitle;
150 }
151
152 private String hideHttp(String str) {
153 Uri uri = Uri.parse(str);
154 return "http".equals(uri.getScheme()) ? str.substring(7) : str;
155 }
Nicolas Roarde46990e2009-06-19 16:27:49 +0100156 }
157
158 class SiteAdapter extends ArrayAdapter<Site>
159 implements AdapterView.OnItemClickListener {
160 private int mResource;
161 private LayoutInflater mInflater;
162 private Bitmap mDefaultIcon;
Nicolas Roardc5702322009-09-14 20:39:08 +0100163 private Bitmap mUsageEmptyIcon;
164 private Bitmap mUsageLowIcon;
Nicolas Roardc5702322009-09-14 20:39:08 +0100165 private Bitmap mUsageHighIcon;
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100166 private Bitmap mLocationAllowedIcon;
167 private Bitmap mLocationDisallowedIcon;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100168 private Site mCurrentSite;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100169
170 public SiteAdapter(Context context, int rsc) {
171 super(context, rsc);
172 mResource = rsc;
173 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
174 mDefaultIcon = BitmapFactory.decodeResource(getResources(),
Ben Murdochbe9560d2009-11-09 09:52:21 -0800175 R.drawable.app_web_browser_sm);
Nicolas Roardc5702322009-09-14 20:39:08 +0100176 mUsageEmptyIcon = BitmapFactory.decodeResource(getResources(),
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100177 R.drawable.ic_list_data_off);
Nicolas Roardc5702322009-09-14 20:39:08 +0100178 mUsageLowIcon = BitmapFactory.decodeResource(getResources(),
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100179 R.drawable.ic_list_data_small);
Nicolas Roardc5702322009-09-14 20:39:08 +0100180 mUsageHighIcon = BitmapFactory.decodeResource(getResources(),
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100181 R.drawable.ic_list_data_large);
182 mLocationAllowedIcon = BitmapFactory.decodeResource(getResources(),
183 R.drawable.ic_list_gps_on);
184 mLocationDisallowedIcon = BitmapFactory.decodeResource(getResources(),
Nicolas Roard10f9e832009-09-30 20:00:15 +0100185 R.drawable.ic_list_gps_denied);
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100186 askForOrigins();
Nicolas Roarde46990e2009-06-19 16:27:49 +0100187 }
188
Steve Block089ce3a2009-07-29 17:16:01 +0100189 /**
190 * Adds the specified feature to the site corresponding to supplied
191 * origin in the map. Creates the site if it does not already exist.
192 */
193 private void addFeatureToSite(Map sites, String origin, int feature) {
194 Site site = null;
195 if (sites.containsKey(origin)) {
196 site = (Site) sites.get(origin);
197 } else {
198 site = new Site(origin);
199 sites.put(origin, site);
200 }
201 site.addFeature(feature);
202 }
203
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100204 public void askForOrigins() {
Steve Block089ce3a2009-07-29 17:16:01 +0100205 // Get the list of origins we want to display.
206 // All 'HTML 5 modules' (Database, Geolocation etc) form these
207 // origin strings using WebCore::SecurityOrigin::toString(), so it's
208 // safe to group origins here. Note that WebCore::SecurityOrigin
209 // uses 0 (which is not printed) for the port if the port is the
210 // default for the protocol. Eg http://www.google.com and
211 // http://www.google.com:80 both record a port of 0 and hence
212 // toString() == 'http://www.google.com' for both.
Nicolas Roarde46990e2009-06-19 16:27:49 +0100213
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100214 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
215 public void onReceiveValue(Map origins) {
216 Map sites = new HashMap<String, Site>();
217 if (origins != null) {
218 Iterator<String> iter = origins.keySet().iterator();
219 while (iter.hasNext()) {
220 addFeatureToSite(sites, iter.next(), Site.FEATURE_WEB_STORAGE);
221 }
222 }
223 askForGeolocation(sites);
224 }
225 });
226 }
227
228 public void askForGeolocation(final Map sites) {
Steve Block2a6a0f42009-11-19 15:55:42 +0000229 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
230 public void onReceiveValue(Set<String> origins) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100231 if (origins != null) {
232 Iterator<String> iter = origins.iterator();
233 while (iter.hasNext()) {
234 addFeatureToSite(sites, iter.next(), Site.FEATURE_GEOLOCATION);
235 }
236 }
237 populateIcons(sites);
238 populateOrigins(sites);
239 }
240 });
241 }
242
243 public void populateIcons(Map sites) {
Steve Blockee0d6392009-07-28 13:49:15 +0100244 // Create a map from host to origin. This is used to add metadata
245 // (title, icon) for this origin from the bookmarks DB.
246 HashMap hosts = new HashMap<String, Set<Site> >();
Steve Block089ce3a2009-07-29 17:16:01 +0100247 Set keys = sites.keySet();
248 Iterator<String> originIter = keys.iterator();
249 while (originIter.hasNext()) {
250 String origin = originIter.next();
251 Site site = (Site) sites.get(origin);
252 String host = Uri.parse(origin).getHost();
Steve Blockee0d6392009-07-28 13:49:15 +0100253 Set hostSites = null;
254 if (hosts.containsKey(host)) {
255 hostSites = (Set) hosts.get(host);
256 } else {
257 hostSites = new HashSet<Site>();
258 hosts.put(host, hostSites);
259 }
260 hostSites.add(site);
261 }
262
263 // Check the bookmark DB. If we have data for a host used by any of
264 // our origins, use it to set their title and favicon
Nicolas Roarde46990e2009-06-19 16:27:49 +0100265 Cursor c = getContext().getContentResolver().query(Browser.BOOKMARKS_URI,
266 new String[] { Browser.BookmarkColumns.URL, Browser.BookmarkColumns.TITLE,
267 Browser.BookmarkColumns.FAVICON }, "bookmark = 1", null, null);
268
269 if ((c != null) && c.moveToFirst()) {
270 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
271 int titleIndex = c.getColumnIndex(Browser.BookmarkColumns.TITLE);
272 int faviconIndex = c.getColumnIndex(Browser.BookmarkColumns.FAVICON);
273 do {
274 String url = c.getString(urlIndex);
275 String host = Uri.parse(url).getHost();
Steve Blockee0d6392009-07-28 13:49:15 +0100276 if (hosts.containsKey(host)) {
Nicolas Roarde46990e2009-06-19 16:27:49 +0100277 String title = c.getString(titleIndex);
Steve Blockee0d6392009-07-28 13:49:15 +0100278 Bitmap bmp = null;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100279 byte[] data = c.getBlob(faviconIndex);
280 if (data != null) {
Steve Blockee0d6392009-07-28 13:49:15 +0100281 bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
282 }
283 Set matchingSites = (Set) hosts.get(host);
Steve Block089ce3a2009-07-29 17:16:01 +0100284 Iterator<Site> sitesIter = matchingSites.iterator();
Steve Blockee0d6392009-07-28 13:49:15 +0100285 while (sitesIter.hasNext()) {
286 Site site = sitesIter.next();
Ben Murdochbe9560d2009-11-09 09:52:21 -0800287 // We should only set the title if the bookmark is for the root
288 // (i.e. www.google.com), as website settings act on the origin
289 // as a whole rather than a single page under that origin. If the
290 // user has bookmarked a page under the root but *not* the root,
291 // then we risk displaying the title of that page which may or
292 // may not have any relevance to the origin.
293 if (url.equals(site.getOrigin()) ||
294 (new String(site.getOrigin()+"/")).equals(url)) {
295 site.setTitle(title);
296 }
Nicolas Roarde46990e2009-06-19 16:27:49 +0100297 if (bmp != null) {
298 site.setIcon(bmp);
299 }
300 }
301 }
302 } while (c.moveToNext());
303 }
304
Nicolas Roardd95fb212009-09-17 16:56:27 +0100305 c.close();
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100306 }
307
308
309 public void populateOrigins(Map sites) {
310 clear();
Nicolas Roardd95fb212009-09-17 16:56:27 +0100311
Nicolas Roarde46990e2009-06-19 16:27:49 +0100312 // We can now simply populate our array with Site instances
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100313 Set keys = sites.keySet();
314 Iterator<String> originIter = keys.iterator();
Steve Block089ce3a2009-07-29 17:16:01 +0100315 while (originIter.hasNext()) {
316 String origin = originIter.next();
317 Site site = (Site) sites.get(origin);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100318 add(site);
319 }
320
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100321 notifyDataSetChanged();
322
Nicolas Roarde46990e2009-06-19 16:27:49 +0100323 if (getCount() == 0) {
324 finish(); // we close the screen
325 }
326 }
327
328 public int getCount() {
329 if (mCurrentSite == null) {
330 return super.getCount();
331 }
Steve Block089ce3a2009-07-29 17:16:01 +0100332 return mCurrentSite.getFeatureCount();
Nicolas Roarde46990e2009-06-19 16:27:49 +0100333 }
334
Steve Block764f0c92009-09-10 15:01:37 +0100335 public String sizeValueToString(long bytes) {
336 // We display the size in MB, to 1dp, rounding up to the next 0.1MB.
337 // bytes should always be greater than zero.
338 if (bytes <= 0) {
Ben Murdochbe9560d2009-11-09 09:52:21 -0800339 Log.e(LOGTAG, "sizeValueToString called with non-positive value: " + bytes);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100340 return "0";
341 }
Steve Block764f0c92009-09-10 15:01:37 +0100342 float megabytes = (float) bytes / (1024.0F * 1024.0F);
343 int truncated = (int) Math.ceil(megabytes * 10.0F);
344 float result = (float) (truncated / 10.0F);
345 return String.valueOf(result);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100346 }
347
348 /*
349 * If we receive the back event and are displaying
350 * site's settings, we want to go back to the main
351 * list view. If not, we just do nothing (see
352 * dispatchKeyEvent() below).
353 */
354 public boolean backKeyPressed() {
355 if (mCurrentSite != null) {
356 mCurrentSite = null;
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100357 askForOrigins();
Nicolas Roarde46990e2009-06-19 16:27:49 +0100358 return true;
359 }
360 return false;
361 }
362
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100363 /**
364 * @hide
365 * Utility function
366 * Set the icon according to the usage
367 */
368 public void setIconForUsage(ImageView usageIcon, long usageInBytes) {
369 float usageInMegabytes = (float) usageInBytes / (1024.0F * 1024.0F);
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100370 // We set the correct icon:
371 // 0 < empty < 0.1MB
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100372 // 0.1MB < low < 5MB
373 // 5MB < high
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100374 if (usageInMegabytes <= 0.1) {
375 usageIcon.setImageBitmap(mUsageEmptyIcon);
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100376 } else if (usageInMegabytes > 0.1 && usageInMegabytes <= 5) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100377 usageIcon.setImageBitmap(mUsageLowIcon);
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100378 } else if (usageInMegabytes > 5) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100379 usageIcon.setImageBitmap(mUsageHighIcon);
380 }
381 }
382
Nicolas Roarde46990e2009-06-19 16:27:49 +0100383 public View getView(int position, View convertView, ViewGroup parent) {
384 View view;
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100385 final TextView title;
386 final TextView subtitle;
Ben Murdochbe9560d2009-11-09 09:52:21 -0800387 final ImageView icon;
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100388 final ImageView usageIcon;
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100389 final ImageView locationIcon;
Ben Murdochbe9560d2009-11-09 09:52:21 -0800390 final ImageView featureIcon;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100391
392 if (convertView == null) {
393 view = mInflater.inflate(mResource, parent, false);
394 } else {
395 view = convertView;
396 }
397
398 title = (TextView) view.findViewById(R.id.title);
399 subtitle = (TextView) view.findViewById(R.id.subtitle);
400 icon = (ImageView) view.findViewById(R.id.icon);
Ben Murdochbe9560d2009-11-09 09:52:21 -0800401 featureIcon = (ImageView) view.findViewById(R.id.feature_icon);
Nicolas Roardc5702322009-09-14 20:39:08 +0100402 usageIcon = (ImageView) view.findViewById(R.id.usage_icon);
403 locationIcon = (ImageView) view.findViewById(R.id.location_icon);
404 usageIcon.setVisibility(View.GONE);
405 locationIcon.setVisibility(View.GONE);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100406
407 if (mCurrentSite == null) {
Steve Block4d055a52009-07-31 08:38:58 +0100408 setTitle(getString(R.string.pref_extras_website_settings));
409
Nicolas Roarde46990e2009-06-19 16:27:49 +0100410 Site site = getItem(position);
Steve Block1ad98cf2009-07-28 11:07:10 +0100411 title.setText(site.getPrettyTitle());
Ben Murdochbe9560d2009-11-09 09:52:21 -0800412 String subtitleText = site.getPrettyOrigin();
413 if (subtitleText != null) {
414 title.setMaxLines(1);
415 title.setSingleLine(true);
416 subtitle.setVisibility(View.VISIBLE);
417 subtitle.setText(subtitleText);
418 } else {
419 subtitle.setVisibility(View.GONE);
420 title.setMaxLines(2);
421 title.setSingleLine(false);
422 }
423
Nicolas Roarde46990e2009-06-19 16:27:49 +0100424 icon.setVisibility(View.VISIBLE);
Nicolas Roardc5702322009-09-14 20:39:08 +0100425 usageIcon.setVisibility(View.INVISIBLE);
426 locationIcon.setVisibility(View.INVISIBLE);
Ben Murdochbe9560d2009-11-09 09:52:21 -0800427 featureIcon.setVisibility(View.GONE);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100428 Bitmap bmp = site.getIcon();
429 if (bmp == null) {
430 bmp = mDefaultIcon;
431 }
432 icon.setImageBitmap(bmp);
433 // We set the site as the view's tag,
434 // so that we can get it in onItemClick()
435 view.setTag(site);
Nicolas Roardc5702322009-09-14 20:39:08 +0100436
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100437 String origin = site.getOrigin();
Nicolas Roardc5702322009-09-14 20:39:08 +0100438 if (site.hasFeature(Site.FEATURE_WEB_STORAGE)) {
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100439 WebStorage.getInstance().getUsageForOrigin(origin, new ValueCallback<Long>() {
440 public void onReceiveValue(Long value) {
441 if (value != null) {
442 setIconForUsage(usageIcon, value.longValue());
Ben Murdochbe9560d2009-11-09 09:52:21 -0800443 usageIcon.setVisibility(View.VISIBLE);
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100444 }
445 }
446 });
Nicolas Roardc5702322009-09-14 20:39:08 +0100447 }
448
449 if (site.hasFeature(Site.FEATURE_GEOLOCATION)) {
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100450 locationIcon.setVisibility(View.VISIBLE);
451 GeolocationPermissions.getInstance().getAllowed(origin, new ValueCallback<Boolean>() {
452 public void onReceiveValue(Boolean allowed) {
453 if (allowed != null) {
454 if (allowed.booleanValue()) {
455 locationIcon.setImageBitmap(mLocationAllowedIcon);
456 } else {
457 locationIcon.setImageBitmap(mLocationDisallowedIcon);
458 }
459 }
460 }
461 });
Nicolas Roardc5702322009-09-14 20:39:08 +0100462 }
Nicolas Roarde46990e2009-06-19 16:27:49 +0100463 } else {
464 icon.setVisibility(View.GONE);
Ben Murdochbe9560d2009-11-09 09:52:21 -0800465 locationIcon.setVisibility(View.GONE);
466 usageIcon.setVisibility(View.GONE);
467 featureIcon.setVisibility(View.VISIBLE);
468 setTitle(mCurrentSite.getPrettyTitle());
Steve Block089ce3a2009-07-29 17:16:01 +0100469 String origin = mCurrentSite.getOrigin();
470 switch (mCurrentSite.getFeatureByIndex(position)) {
471 case Site.FEATURE_WEB_STORAGE:
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100472 WebStorage.getInstance().getUsageForOrigin(origin, new ValueCallback<Long>() {
473 public void onReceiveValue(Long value) {
474 if (value != null) {
475 String usage = sizeValueToString(value.longValue()) + " " + sMBStored;
476 title.setText(R.string.webstorage_clear_data_title);
477 subtitle.setText(usage);
Ben Murdochbe9560d2009-11-09 09:52:21 -0800478 subtitle.setVisibility(View.VISIBLE);
479 setIconForUsage(featureIcon, value.longValue());
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100480 }
481 }
482 });
Steve Block089ce3a2009-07-29 17:16:01 +0100483 break;
Steve Blockf344d032009-07-30 10:50:45 +0100484 case Site.FEATURE_GEOLOCATION:
485 title.setText(R.string.geolocation_settings_page_title);
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100486 GeolocationPermissions.getInstance().getAllowed(origin, new ValueCallback<Boolean>() {
487 public void onReceiveValue(Boolean allowed) {
488 if (allowed != null) {
489 if (allowed.booleanValue()) {
490 subtitle.setText(R.string.geolocation_settings_page_summary_allowed);
Ben Murdochbe9560d2009-11-09 09:52:21 -0800491 featureIcon.setImageBitmap(mLocationAllowedIcon);
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100492 } else {
493 subtitle.setText(R.string.geolocation_settings_page_summary_not_allowed);
Ben Murdochbe9560d2009-11-09 09:52:21 -0800494 featureIcon.setImageBitmap(mLocationDisallowedIcon);
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100495 }
Ben Murdochbe9560d2009-11-09 09:52:21 -0800496 subtitle.setVisibility(View.VISIBLE);
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100497 }
498 }
499 });
Steve Blockf344d032009-07-30 10:50:45 +0100500 break;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100501 }
502 }
503
504 return view;
505 }
506
507 public void onItemClick(AdapterView<?> parent,
508 View view,
509 int position,
510 long id) {
511 if (mCurrentSite != null) {
Steve Block089ce3a2009-07-29 17:16:01 +0100512 switch (mCurrentSite.getFeatureByIndex(position)) {
513 case Site.FEATURE_WEB_STORAGE:
514 new AlertDialog.Builder(getContext())
515 .setTitle(R.string.webstorage_clear_data_dialog_title)
516 .setMessage(R.string.webstorage_clear_data_dialog_message)
517 .setPositiveButton(R.string.webstorage_clear_data_dialog_ok_button,
518 new AlertDialog.OnClickListener() {
519 public void onClick(DialogInterface dlg, int which) {
520 WebStorage.getInstance().deleteOrigin(mCurrentSite.getOrigin());
Ben Murdochbe9560d2009-11-09 09:52:21 -0800521 // If this site has no more features, then go back to the
522 // origins list.
523 mCurrentSite.removeFeature(Site.FEATURE_WEB_STORAGE);
524 if (mCurrentSite.getFeatureCount() == 0) {
525 mCurrentSite = null;
526 }
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100527 askForOrigins();
Ben Murdochbe9560d2009-11-09 09:52:21 -0800528 notifyDataSetChanged();
Steve Block089ce3a2009-07-29 17:16:01 +0100529 }})
530 .setNegativeButton(R.string.webstorage_clear_data_dialog_cancel_button, null)
531 .setIcon(android.R.drawable.ic_dialog_alert)
532 .show();
533 break;
Steve Blockf344d032009-07-30 10:50:45 +0100534 case Site.FEATURE_GEOLOCATION:
535 new AlertDialog.Builder(getContext())
536 .setTitle(R.string.geolocation_settings_page_dialog_title)
537 .setMessage(R.string.geolocation_settings_page_dialog_message)
538 .setPositiveButton(R.string.geolocation_settings_page_dialog_ok_button,
539 new AlertDialog.OnClickListener() {
540 public void onClick(DialogInterface dlg, int which) {
541 GeolocationPermissions.getInstance().clear(mCurrentSite.getOrigin());
Ben Murdochbe9560d2009-11-09 09:52:21 -0800542 mCurrentSite.removeFeature(Site.FEATURE_GEOLOCATION);
543 if (mCurrentSite.getFeatureCount() == 0) {
544 mCurrentSite = null;
545 }
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100546 askForOrigins();
Ben Murdochbe9560d2009-11-09 09:52:21 -0800547 notifyDataSetChanged();
Steve Blockf344d032009-07-30 10:50:45 +0100548 }})
549 .setNegativeButton(R.string.geolocation_settings_page_dialog_cancel_button, null)
550 .setIcon(android.R.drawable.ic_dialog_alert)
551 .show();
552 break;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100553 }
554 } else {
555 mCurrentSite = (Site) view.getTag();
556 notifyDataSetChanged();
557 }
558 }
Ben Murdoch9e19e442009-10-26 18:03:44 +0000559
560 public Site currentSite() {
561 return mCurrentSite;
562 }
Nicolas Roarde46990e2009-06-19 16:27:49 +0100563 }
564
565 /**
566 * Intercepts the back key to immediately notify
567 * NativeDialog that we are done.
568 */
569 public boolean dispatchKeyEvent(KeyEvent event) {
570 if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
571 && (event.getAction() == KeyEvent.ACTION_DOWN)) {
572 if ((mAdapter != null) && (mAdapter.backKeyPressed())){
573 return true; // event consumed
574 }
575 }
576 return super.dispatchKeyEvent(event);
577 }
578
579 @Override
580 protected void onCreate(Bundle icicle) {
581 super.onCreate(icicle);
582 if (sMBStored == null) {
583 sMBStored = getString(R.string.webstorage_origin_summary_mb_stored);
584 }
Nicolas Roardc5702322009-09-14 20:39:08 +0100585 mAdapter = new SiteAdapter(this, R.layout.website_settings_row);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100586 setListAdapter(mAdapter);
587 getListView().setOnItemClickListener(mAdapter);
588 }
Ben Murdochb9daacb2009-09-14 10:48:24 +0100589
590 @Override
591 public boolean onCreateOptionsMenu(Menu menu) {
592 MenuInflater inflater = getMenuInflater();
593 inflater.inflate(R.menu.websitesettings, menu);
594 return true;
595 }
596
597 @Override
598 public boolean onPrepareOptionsMenu(Menu menu) {
Ben Murdoch9e19e442009-10-26 18:03:44 +0000599 // If we are not on the sites list (rather on the page for a specific site) or
600 // we aren't listing any sites hide the clear all button (and hence the menu).
601 return mAdapter.currentSite() == null && mAdapter.getCount() > 0;
Ben Murdochb9daacb2009-09-14 10:48:24 +0100602 }
603
604 @Override
605 public boolean onOptionsItemSelected(MenuItem item) {
606 switch (item.getItemId()) {
607 case R.id.website_settings_menu_clear_all:
608 // Show the prompt to clear all origins of their data and geolocation permissions.
609 new AlertDialog.Builder(this)
610 .setTitle(R.string.website_settings_clear_all_dialog_title)
611 .setMessage(R.string.website_settings_clear_all_dialog_message)
612 .setPositiveButton(R.string.website_settings_clear_all_dialog_ok_button,
613 new AlertDialog.OnClickListener() {
614 public void onClick(DialogInterface dlg, int which) {
615 WebStorage.getInstance().deleteAllData();
616 GeolocationPermissions.getInstance().clearAll();
Nicolas Roardeb9032c2009-09-28 17:03:36 +0100617 WebStorageSizeManager.resetLastOutOfSpaceNotificationTime();
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100618 mAdapter.askForOrigins();
619 finish();
Ben Murdochb9daacb2009-09-14 10:48:24 +0100620 }})
621 .setNegativeButton(R.string.website_settings_clear_all_dialog_cancel_button, null)
622 .setIcon(android.R.drawable.ic_dialog_alert)
623 .show();
624 return true;
625 }
626 return false;
627 }
Nicolas Roarde46990e2009-06-19 16:27:49 +0100628}