blob: 95f8fb02c2ac0dadcab0b358a93639f1b67b1b80 [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;
Ben Murdocha7c9a552010-11-25 15:42:19 +000027import android.os.AsyncTask;
Nicolas Roarde46990e2009-06-19 16:27:49 +010028import android.os.Bundle;
Ben Murdochb4b87c62010-11-25 15:27:20 +000029import android.provider.BrowserContract.Bookmarks;
Nicolas Roarde46990e2009-06-19 16:27:49 +010030import android.util.Log;
31import android.view.KeyEvent;
32import android.view.LayoutInflater;
Ben Murdochb9daacb2009-09-14 10:48:24 +010033import android.view.Menu;
34import android.view.MenuInflater;
35import android.view.MenuItem;
Nicolas Roarde46990e2009-06-19 16:27:49 +010036import android.view.View;
37import android.view.ViewGroup;
Steve Blockf344d032009-07-30 10:50:45 +010038import android.webkit.GeolocationPermissions;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010039import android.webkit.ValueCallback;
Nicolas Roarde46990e2009-06-19 16:27:49 +010040import android.webkit.WebIconDatabase;
41import android.webkit.WebStorage;
42import android.widget.ArrayAdapter;
43import android.widget.AdapterView;
44import android.widget.AdapterView.OnItemClickListener;
45import android.widget.ImageView;
46import android.widget.TextView;
47
48import java.util.HashMap;
Steve Blockee0d6392009-07-28 13:49:15 +010049import java.util.HashSet;
Nicolas Roarde46990e2009-06-19 16:27:49 +010050import java.util.Iterator;
Steve Block089ce3a2009-07-29 17:16:01 +010051import java.util.Map;
Nicolas Roarde46990e2009-06-19 16:27:49 +010052import java.util.Set;
53import java.util.Vector;
54
55/**
56 * Manage the settings for an origin.
Steve Block089ce3a2009-07-29 17:16:01 +010057 * We use it to keep track of the 'HTML5' settings, i.e. database (webstorage)
58 * and Geolocation.
Nicolas Roarde46990e2009-06-19 16:27:49 +010059 */
60public class WebsiteSettingsActivity extends ListActivity {
61
62 private String LOGTAG = "WebsiteSettingsActivity";
63 private static String sMBStored = null;
64 private SiteAdapter mAdapter = null;
65
Henrik Baard98f42de2010-04-27 08:26:06 +020066 static class Site {
Nicolas Roarde46990e2009-06-19 16:27:49 +010067 private String mOrigin;
68 private String mTitle;
69 private Bitmap mIcon;
Steve Block089ce3a2009-07-29 17:16:01 +010070 private int mFeatures;
71
72 // These constants provide the set of features that a site may support
73 // They must be consecutive. To add a new feature, add a new FEATURE_XXX
74 // variable with value equal to the current value of FEATURE_COUNT, then
75 // increment FEATURE_COUNT.
76 private final static int FEATURE_WEB_STORAGE = 0;
Steve Blockf344d032009-07-30 10:50:45 +010077 private final static int FEATURE_GEOLOCATION = 1;
Steve Block089ce3a2009-07-29 17:16:01 +010078 // The number of features available.
Steve Blockf344d032009-07-30 10:50:45 +010079 private final static int FEATURE_COUNT = 2;
Nicolas Roarde46990e2009-06-19 16:27:49 +010080
Steve Block1ad98cf2009-07-28 11:07:10 +010081 public Site(String origin) {
Nicolas Roarde46990e2009-06-19 16:27:49 +010082 mOrigin = origin;
Steve Block1ad98cf2009-07-28 11:07:10 +010083 mTitle = null;
84 mIcon = null;
Steve Block089ce3a2009-07-29 17:16:01 +010085 mFeatures = 0;
86 }
87
88 public void addFeature(int feature) {
89 mFeatures |= (1 << feature);
90 }
91
Ben Murdochbe9560d2009-11-09 09:52:21 -080092 public void removeFeature(int feature) {
93 mFeatures &= ~(1 << feature);
94 }
95
Steve Block089ce3a2009-07-29 17:16:01 +010096 public boolean hasFeature(int feature) {
97 return (mFeatures & (1 << feature)) != 0;
98 }
99
100 /**
101 * Gets the number of features supported by this site.
102 */
103 public int getFeatureCount() {
104 int count = 0;
105 for (int i = 0; i < FEATURE_COUNT; ++i) {
106 count += hasFeature(i) ? 1 : 0;
107 }
108 return count;
109 }
110
111 /**
112 * Gets the ID of the nth (zero-based) feature supported by this site.
113 * The return value is a feature ID - one of the FEATURE_XXX values.
114 * This is required to determine which feature is displayed at a given
115 * position in the list of features for this site. This is used both
Steve Blockf344d032009-07-30 10:50:45 +0100116 * when populating the view and when responding to clicks on the list.
Steve Block089ce3a2009-07-29 17:16:01 +0100117 */
118 public int getFeatureByIndex(int n) {
119 int j = -1;
120 for (int i = 0; i < FEATURE_COUNT; ++i) {
121 j += hasFeature(i) ? 1 : 0;
122 if (j == n) {
123 return i;
124 }
125 }
126 return -1;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100127 }
128
129 public String getOrigin() {
130 return mOrigin;
131 }
132
133 public void setTitle(String title) {
134 mTitle = title;
135 }
136
Nicolas Roarde46990e2009-06-19 16:27:49 +0100137 public void setIcon(Bitmap icon) {
138 mIcon = icon;
139 }
140
141 public Bitmap getIcon() {
142 return mIcon;
143 }
Steve Block1ad98cf2009-07-28 11:07:10 +0100144
145 public String getPrettyOrigin() {
146 return mTitle == null ? null : hideHttp(mOrigin);
147 }
148
149 public String getPrettyTitle() {
150 return mTitle == null ? hideHttp(mOrigin) : mTitle;
151 }
152
153 private String hideHttp(String str) {
154 Uri uri = Uri.parse(str);
155 return "http".equals(uri.getScheme()) ? str.substring(7) : str;
156 }
Nicolas Roarde46990e2009-06-19 16:27:49 +0100157 }
158
159 class SiteAdapter extends ArrayAdapter<Site>
160 implements AdapterView.OnItemClickListener {
161 private int mResource;
162 private LayoutInflater mInflater;
163 private Bitmap mDefaultIcon;
Nicolas Roardc5702322009-09-14 20:39:08 +0100164 private Bitmap mUsageEmptyIcon;
165 private Bitmap mUsageLowIcon;
Nicolas Roardc5702322009-09-14 20:39:08 +0100166 private Bitmap mUsageHighIcon;
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100167 private Bitmap mLocationAllowedIcon;
168 private Bitmap mLocationDisallowedIcon;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100169 private Site mCurrentSite;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100170
171 public SiteAdapter(Context context, int rsc) {
172 super(context, rsc);
173 mResource = rsc;
174 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
175 mDefaultIcon = BitmapFactory.decodeResource(getResources(),
Ben Murdochbe9560d2009-11-09 09:52:21 -0800176 R.drawable.app_web_browser_sm);
Nicolas Roardc5702322009-09-14 20:39:08 +0100177 mUsageEmptyIcon = BitmapFactory.decodeResource(getResources(),
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100178 R.drawable.ic_list_data_off);
Nicolas Roardc5702322009-09-14 20:39:08 +0100179 mUsageLowIcon = BitmapFactory.decodeResource(getResources(),
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100180 R.drawable.ic_list_data_small);
Nicolas Roardc5702322009-09-14 20:39:08 +0100181 mUsageHighIcon = BitmapFactory.decodeResource(getResources(),
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100182 R.drawable.ic_list_data_large);
183 mLocationAllowedIcon = BitmapFactory.decodeResource(getResources(),
184 R.drawable.ic_list_gps_on);
185 mLocationDisallowedIcon = BitmapFactory.decodeResource(getResources(),
Nicolas Roard10f9e832009-09-30 20:00:15 +0100186 R.drawable.ic_list_gps_denied);
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100187 askForOrigins();
Nicolas Roarde46990e2009-06-19 16:27:49 +0100188 }
189
Steve Block089ce3a2009-07-29 17:16:01 +0100190 /**
191 * Adds the specified feature to the site corresponding to supplied
192 * origin in the map. Creates the site if it does not already exist.
193 */
Henrik Baard98f42de2010-04-27 08:26:06 +0200194 private void addFeatureToSite(Map<String, Site> sites, String origin, int feature) {
Steve Block089ce3a2009-07-29 17:16:01 +0100195 Site site = null;
196 if (sites.containsKey(origin)) {
197 site = (Site) sites.get(origin);
198 } else {
199 site = new Site(origin);
200 sites.put(origin, site);
201 }
202 site.addFeature(feature);
203 }
204
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100205 public void askForOrigins() {
Steve Block089ce3a2009-07-29 17:16:01 +0100206 // Get the list of origins we want to display.
207 // All 'HTML 5 modules' (Database, Geolocation etc) form these
208 // origin strings using WebCore::SecurityOrigin::toString(), so it's
209 // safe to group origins here. Note that WebCore::SecurityOrigin
210 // uses 0 (which is not printed) for the port if the port is the
211 // default for the protocol. Eg http://www.google.com and
212 // http://www.google.com:80 both record a port of 0 and hence
213 // toString() == 'http://www.google.com' for both.
Nicolas Roarde46990e2009-06-19 16:27:49 +0100214
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100215 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
216 public void onReceiveValue(Map origins) {
Henrik Baard98f42de2010-04-27 08:26:06 +0200217 Map<String, Site> sites = new HashMap<String, Site>();
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100218 if (origins != null) {
219 Iterator<String> iter = origins.keySet().iterator();
220 while (iter.hasNext()) {
221 addFeatureToSite(sites, iter.next(), Site.FEATURE_WEB_STORAGE);
222 }
223 }
224 askForGeolocation(sites);
225 }
226 });
227 }
228
Henrik Baard98f42de2010-04-27 08:26:06 +0200229 public void askForGeolocation(final Map<String, Site> sites) {
Steve Block2a6a0f42009-11-19 15:55:42 +0000230 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
231 public void onReceiveValue(Set<String> origins) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100232 if (origins != null) {
233 Iterator<String> iter = origins.iterator();
234 while (iter.hasNext()) {
235 addFeatureToSite(sites, iter.next(), Site.FEATURE_GEOLOCATION);
236 }
237 }
238 populateIcons(sites);
239 populateOrigins(sites);
240 }
241 });
242 }
243
Henrik Baard98f42de2010-04-27 08:26:06 +0200244 public void populateIcons(Map<String, Site> sites) {
Steve Blockee0d6392009-07-28 13:49:15 +0100245 // Create a map from host to origin. This is used to add metadata
Ben Murdocha7c9a552010-11-25 15:42:19 +0000246 // (title, icon) for this origin from the bookmarks DB. We must do
247 // the DB access on a background thread.
248 new UpdateFromBookmarksDbTask(this.getContext(), sites).execute();
249 }
250
251 private class UpdateFromBookmarksDbTask extends AsyncTask<Void, Void, Void> {
252
253 private Context mContext;
254 private boolean mDataSetChanged;
255 private Map<String, Site> mSites;
256
257 public UpdateFromBookmarksDbTask(Context ctx, Map<String, Site> sites) {
258 mContext = ctx;
259 mSites = sites;
Steve Blockee0d6392009-07-28 13:49:15 +0100260 }
261
Ben Murdocha7c9a552010-11-25 15:42:19 +0000262 protected Void doInBackground(Void... unused) {
263 HashMap<String, Set<Site>> hosts = new HashMap<String, Set<Site>>();
264 Set<Map.Entry<String, Site>> elements = mSites.entrySet();
265 Iterator<Map.Entry<String, Site>> originIter = elements.iterator();
266 while (originIter.hasNext()) {
267 Map.Entry<String, Site> entry = originIter.next();
268 Site site = entry.getValue();
269 String host = Uri.parse(entry.getKey()).getHost();
270 Set<Site> hostSites = null;
271 if (hosts.containsKey(host)) {
272 hostSites = (Set<Site>)hosts.get(host);
273 } else {
274 hostSites = new HashSet<Site>();
275 hosts.put(host, hostSites);
276 }
277 hostSites.add(site);
Henrik Baard98f42de2010-04-27 08:26:06 +0200278 }
Ben Murdocha7c9a552010-11-25 15:42:19 +0000279
280 // Check the bookmark DB. If we have data for a host used by any of
281 // our origins, use it to set their title and favicon
282 Cursor c = mContext.getContentResolver().query(Bookmarks.CONTENT_URI,
283 new String[] { Bookmarks.URL, Bookmarks.TITLE, Bookmarks.FAVICON },
284 Bookmarks.IS_FOLDER + " == 0", null, null);
285
286 if (c != null) {
287 if (c.moveToFirst()) {
288 int urlIndex = c.getColumnIndex(Bookmarks.URL);
289 int titleIndex = c.getColumnIndex(Bookmarks.TITLE);
290 int faviconIndex = c.getColumnIndex(Bookmarks.FAVICON);
291 do {
292 String url = c.getString(urlIndex);
293 String host = Uri.parse(url).getHost();
294 if (hosts.containsKey(host)) {
295 String title = c.getString(titleIndex);
296 Bitmap bmp = null;
297 byte[] data = c.getBlob(faviconIndex);
298 if (data != null) {
299 bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
300 }
301 Set matchingSites = (Set) hosts.get(host);
302 Iterator<Site> sitesIter = matchingSites.iterator();
303 while (sitesIter.hasNext()) {
304 Site site = sitesIter.next();
305 // We should only set the title if the bookmark is for the root
306 // (i.e. www.google.com), as website settings act on the origin
307 // as a whole rather than a single page under that origin. If the
308 // user has bookmarked a page under the root but *not* the root,
309 // then we risk displaying the title of that page which may or
310 // may not have any relevance to the origin.
311 if (url.equals(site.getOrigin()) ||
312 (new String(site.getOrigin()+"/")).equals(url)) {
313 mDataSetChanged = true;
314 site.setTitle(title);
315 }
316
317 if (bmp != null) {
318 mDataSetChanged = true;
319 site.setIcon(bmp);
320 }
321 }
322 }
323 } while (c.moveToNext());
324 }
325 c.close();
326 }
327 return null;
328 }
329
330 protected void onPostExecute(Void unused) {
331 if (mDataSetChanged) {
332 notifyDataSetChanged();
333 }
Nicolas Roarde46990e2009-06-19 16:27:49 +0100334 }
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100335 }
336
337
Henrik Baard98f42de2010-04-27 08:26:06 +0200338 public void populateOrigins(Map<String, Site> sites) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100339 clear();
Nicolas Roardd95fb212009-09-17 16:56:27 +0100340
Nicolas Roarde46990e2009-06-19 16:27:49 +0100341 // We can now simply populate our array with Site instances
Henrik Baard98f42de2010-04-27 08:26:06 +0200342 Set<Map.Entry<String, Site>> elements = sites.entrySet();
343 Iterator<Map.Entry<String, Site>> entryIterator = elements.iterator();
344 while (entryIterator.hasNext()) {
345 Map.Entry<String, Site> entry = entryIterator.next();
346 Site site = entry.getValue();
Nicolas Roarde46990e2009-06-19 16:27:49 +0100347 add(site);
348 }
349
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100350 notifyDataSetChanged();
351
Nicolas Roarde46990e2009-06-19 16:27:49 +0100352 if (getCount() == 0) {
353 finish(); // we close the screen
354 }
355 }
356
357 public int getCount() {
358 if (mCurrentSite == null) {
359 return super.getCount();
360 }
Steve Block089ce3a2009-07-29 17:16:01 +0100361 return mCurrentSite.getFeatureCount();
Nicolas Roarde46990e2009-06-19 16:27:49 +0100362 }
363
Steve Block764f0c92009-09-10 15:01:37 +0100364 public String sizeValueToString(long bytes) {
365 // We display the size in MB, to 1dp, rounding up to the next 0.1MB.
366 // bytes should always be greater than zero.
367 if (bytes <= 0) {
Ben Murdochbe9560d2009-11-09 09:52:21 -0800368 Log.e(LOGTAG, "sizeValueToString called with non-positive value: " + bytes);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100369 return "0";
370 }
Steve Block764f0c92009-09-10 15:01:37 +0100371 float megabytes = (float) bytes / (1024.0F * 1024.0F);
372 int truncated = (int) Math.ceil(megabytes * 10.0F);
373 float result = (float) (truncated / 10.0F);
374 return String.valueOf(result);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100375 }
376
377 /*
378 * If we receive the back event and are displaying
379 * site's settings, we want to go back to the main
380 * list view. If not, we just do nothing (see
381 * dispatchKeyEvent() below).
382 */
383 public boolean backKeyPressed() {
384 if (mCurrentSite != null) {
385 mCurrentSite = null;
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100386 askForOrigins();
Nicolas Roarde46990e2009-06-19 16:27:49 +0100387 return true;
388 }
389 return false;
390 }
391
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100392 /**
393 * @hide
394 * Utility function
395 * Set the icon according to the usage
396 */
397 public void setIconForUsage(ImageView usageIcon, long usageInBytes) {
398 float usageInMegabytes = (float) usageInBytes / (1024.0F * 1024.0F);
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100399 // We set the correct icon:
400 // 0 < empty < 0.1MB
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100401 // 0.1MB < low < 5MB
402 // 5MB < high
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100403 if (usageInMegabytes <= 0.1) {
404 usageIcon.setImageBitmap(mUsageEmptyIcon);
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100405 } else if (usageInMegabytes > 0.1 && usageInMegabytes <= 5) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100406 usageIcon.setImageBitmap(mUsageLowIcon);
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100407 } else if (usageInMegabytes > 5) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100408 usageIcon.setImageBitmap(mUsageHighIcon);
409 }
410 }
411
Nicolas Roarde46990e2009-06-19 16:27:49 +0100412 public View getView(int position, View convertView, ViewGroup parent) {
413 View view;
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100414 final TextView title;
415 final TextView subtitle;
Ben Murdochbe9560d2009-11-09 09:52:21 -0800416 final ImageView icon;
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100417 final ImageView usageIcon;
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100418 final ImageView locationIcon;
Ben Murdochbe9560d2009-11-09 09:52:21 -0800419 final ImageView featureIcon;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100420
421 if (convertView == null) {
422 view = mInflater.inflate(mResource, parent, false);
423 } else {
424 view = convertView;
425 }
426
427 title = (TextView) view.findViewById(R.id.title);
428 subtitle = (TextView) view.findViewById(R.id.subtitle);
429 icon = (ImageView) view.findViewById(R.id.icon);
Ben Murdochbe9560d2009-11-09 09:52:21 -0800430 featureIcon = (ImageView) view.findViewById(R.id.feature_icon);
Nicolas Roardc5702322009-09-14 20:39:08 +0100431 usageIcon = (ImageView) view.findViewById(R.id.usage_icon);
432 locationIcon = (ImageView) view.findViewById(R.id.location_icon);
433 usageIcon.setVisibility(View.GONE);
434 locationIcon.setVisibility(View.GONE);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100435
436 if (mCurrentSite == null) {
Steve Block4d055a52009-07-31 08:38:58 +0100437 setTitle(getString(R.string.pref_extras_website_settings));
438
Nicolas Roarde46990e2009-06-19 16:27:49 +0100439 Site site = getItem(position);
Steve Block1ad98cf2009-07-28 11:07:10 +0100440 title.setText(site.getPrettyTitle());
Ben Murdochbe9560d2009-11-09 09:52:21 -0800441 String subtitleText = site.getPrettyOrigin();
442 if (subtitleText != null) {
443 title.setMaxLines(1);
444 title.setSingleLine(true);
445 subtitle.setVisibility(View.VISIBLE);
446 subtitle.setText(subtitleText);
447 } else {
448 subtitle.setVisibility(View.GONE);
449 title.setMaxLines(2);
450 title.setSingleLine(false);
451 }
452
Nicolas Roarde46990e2009-06-19 16:27:49 +0100453 icon.setVisibility(View.VISIBLE);
Nicolas Roardc5702322009-09-14 20:39:08 +0100454 usageIcon.setVisibility(View.INVISIBLE);
455 locationIcon.setVisibility(View.INVISIBLE);
Ben Murdochbe9560d2009-11-09 09:52:21 -0800456 featureIcon.setVisibility(View.GONE);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100457 Bitmap bmp = site.getIcon();
458 if (bmp == null) {
459 bmp = mDefaultIcon;
460 }
461 icon.setImageBitmap(bmp);
462 // We set the site as the view's tag,
463 // so that we can get it in onItemClick()
464 view.setTag(site);
Nicolas Roardc5702322009-09-14 20:39:08 +0100465
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100466 String origin = site.getOrigin();
Nicolas Roardc5702322009-09-14 20:39:08 +0100467 if (site.hasFeature(Site.FEATURE_WEB_STORAGE)) {
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100468 WebStorage.getInstance().getUsageForOrigin(origin, new ValueCallback<Long>() {
469 public void onReceiveValue(Long value) {
470 if (value != null) {
471 setIconForUsage(usageIcon, value.longValue());
Ben Murdochbe9560d2009-11-09 09:52:21 -0800472 usageIcon.setVisibility(View.VISIBLE);
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100473 }
474 }
475 });
Nicolas Roardc5702322009-09-14 20:39:08 +0100476 }
477
478 if (site.hasFeature(Site.FEATURE_GEOLOCATION)) {
Nicolas Roard9cdaba52009-09-30 16:54:34 +0100479 locationIcon.setVisibility(View.VISIBLE);
480 GeolocationPermissions.getInstance().getAllowed(origin, new ValueCallback<Boolean>() {
481 public void onReceiveValue(Boolean allowed) {
482 if (allowed != null) {
483 if (allowed.booleanValue()) {
484 locationIcon.setImageBitmap(mLocationAllowedIcon);
485 } else {
486 locationIcon.setImageBitmap(mLocationDisallowedIcon);
487 }
488 }
489 }
490 });
Nicolas Roardc5702322009-09-14 20:39:08 +0100491 }
Nicolas Roarde46990e2009-06-19 16:27:49 +0100492 } else {
493 icon.setVisibility(View.GONE);
Ben Murdochbe9560d2009-11-09 09:52:21 -0800494 locationIcon.setVisibility(View.GONE);
495 usageIcon.setVisibility(View.GONE);
496 featureIcon.setVisibility(View.VISIBLE);
497 setTitle(mCurrentSite.getPrettyTitle());
Steve Block089ce3a2009-07-29 17:16:01 +0100498 String origin = mCurrentSite.getOrigin();
499 switch (mCurrentSite.getFeatureByIndex(position)) {
500 case Site.FEATURE_WEB_STORAGE:
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100501 WebStorage.getInstance().getUsageForOrigin(origin, new ValueCallback<Long>() {
502 public void onReceiveValue(Long value) {
503 if (value != null) {
504 String usage = sizeValueToString(value.longValue()) + " " + sMBStored;
505 title.setText(R.string.webstorage_clear_data_title);
506 subtitle.setText(usage);
Ben Murdochbe9560d2009-11-09 09:52:21 -0800507 subtitle.setVisibility(View.VISIBLE);
508 setIconForUsage(featureIcon, value.longValue());
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100509 }
510 }
511 });
Steve Block089ce3a2009-07-29 17:16:01 +0100512 break;
Steve Blockf344d032009-07-30 10:50:45 +0100513 case Site.FEATURE_GEOLOCATION:
514 title.setText(R.string.geolocation_settings_page_title);
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100515 GeolocationPermissions.getInstance().getAllowed(origin, new ValueCallback<Boolean>() {
516 public void onReceiveValue(Boolean allowed) {
517 if (allowed != null) {
518 if (allowed.booleanValue()) {
519 subtitle.setText(R.string.geolocation_settings_page_summary_allowed);
Ben Murdochbe9560d2009-11-09 09:52:21 -0800520 featureIcon.setImageBitmap(mLocationAllowedIcon);
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100521 } else {
522 subtitle.setText(R.string.geolocation_settings_page_summary_not_allowed);
Ben Murdochbe9560d2009-11-09 09:52:21 -0800523 featureIcon.setImageBitmap(mLocationDisallowedIcon);
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100524 }
Ben Murdochbe9560d2009-11-09 09:52:21 -0800525 subtitle.setVisibility(View.VISIBLE);
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100526 }
527 }
528 });
Steve Blockf344d032009-07-30 10:50:45 +0100529 break;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100530 }
531 }
532
533 return view;
534 }
535
536 public void onItemClick(AdapterView<?> parent,
537 View view,
538 int position,
539 long id) {
540 if (mCurrentSite != null) {
Steve Block089ce3a2009-07-29 17:16:01 +0100541 switch (mCurrentSite.getFeatureByIndex(position)) {
542 case Site.FEATURE_WEB_STORAGE:
543 new AlertDialog.Builder(getContext())
544 .setTitle(R.string.webstorage_clear_data_dialog_title)
545 .setMessage(R.string.webstorage_clear_data_dialog_message)
546 .setPositiveButton(R.string.webstorage_clear_data_dialog_ok_button,
547 new AlertDialog.OnClickListener() {
548 public void onClick(DialogInterface dlg, int which) {
549 WebStorage.getInstance().deleteOrigin(mCurrentSite.getOrigin());
Ben Murdochbe9560d2009-11-09 09:52:21 -0800550 // If this site has no more features, then go back to the
551 // origins list.
552 mCurrentSite.removeFeature(Site.FEATURE_WEB_STORAGE);
553 if (mCurrentSite.getFeatureCount() == 0) {
554 mCurrentSite = null;
555 }
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100556 askForOrigins();
Ben Murdochbe9560d2009-11-09 09:52:21 -0800557 notifyDataSetChanged();
Steve Block089ce3a2009-07-29 17:16:01 +0100558 }})
559 .setNegativeButton(R.string.webstorage_clear_data_dialog_cancel_button, null)
560 .setIcon(android.R.drawable.ic_dialog_alert)
561 .show();
562 break;
Steve Blockf344d032009-07-30 10:50:45 +0100563 case Site.FEATURE_GEOLOCATION:
564 new AlertDialog.Builder(getContext())
565 .setTitle(R.string.geolocation_settings_page_dialog_title)
566 .setMessage(R.string.geolocation_settings_page_dialog_message)
567 .setPositiveButton(R.string.geolocation_settings_page_dialog_ok_button,
568 new AlertDialog.OnClickListener() {
569 public void onClick(DialogInterface dlg, int which) {
570 GeolocationPermissions.getInstance().clear(mCurrentSite.getOrigin());
Ben Murdochbe9560d2009-11-09 09:52:21 -0800571 mCurrentSite.removeFeature(Site.FEATURE_GEOLOCATION);
572 if (mCurrentSite.getFeatureCount() == 0) {
573 mCurrentSite = null;
574 }
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100575 askForOrigins();
Ben Murdochbe9560d2009-11-09 09:52:21 -0800576 notifyDataSetChanged();
Steve Blockf344d032009-07-30 10:50:45 +0100577 }})
578 .setNegativeButton(R.string.geolocation_settings_page_dialog_cancel_button, null)
579 .setIcon(android.R.drawable.ic_dialog_alert)
580 .show();
581 break;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100582 }
583 } else {
584 mCurrentSite = (Site) view.getTag();
585 notifyDataSetChanged();
586 }
587 }
Ben Murdoch9e19e442009-10-26 18:03:44 +0000588
589 public Site currentSite() {
590 return mCurrentSite;
591 }
Nicolas Roarde46990e2009-06-19 16:27:49 +0100592 }
593
594 /**
595 * Intercepts the back key to immediately notify
596 * NativeDialog that we are done.
597 */
598 public boolean dispatchKeyEvent(KeyEvent event) {
599 if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
600 && (event.getAction() == KeyEvent.ACTION_DOWN)) {
601 if ((mAdapter != null) && (mAdapter.backKeyPressed())){
602 return true; // event consumed
603 }
604 }
605 return super.dispatchKeyEvent(event);
606 }
607
608 @Override
609 protected void onCreate(Bundle icicle) {
610 super.onCreate(icicle);
611 if (sMBStored == null) {
612 sMBStored = getString(R.string.webstorage_origin_summary_mb_stored);
613 }
Nicolas Roardc5702322009-09-14 20:39:08 +0100614 mAdapter = new SiteAdapter(this, R.layout.website_settings_row);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100615 setListAdapter(mAdapter);
616 getListView().setOnItemClickListener(mAdapter);
617 }
Ben Murdochb9daacb2009-09-14 10:48:24 +0100618
619 @Override
620 public boolean onCreateOptionsMenu(Menu menu) {
621 MenuInflater inflater = getMenuInflater();
622 inflater.inflate(R.menu.websitesettings, menu);
623 return true;
624 }
625
626 @Override
627 public boolean onPrepareOptionsMenu(Menu menu) {
Ben Murdoch9e19e442009-10-26 18:03:44 +0000628 // If we are not on the sites list (rather on the page for a specific site) or
629 // we aren't listing any sites hide the clear all button (and hence the menu).
630 return mAdapter.currentSite() == null && mAdapter.getCount() > 0;
Ben Murdochb9daacb2009-09-14 10:48:24 +0100631 }
632
633 @Override
634 public boolean onOptionsItemSelected(MenuItem item) {
635 switch (item.getItemId()) {
636 case R.id.website_settings_menu_clear_all:
637 // Show the prompt to clear all origins of their data and geolocation permissions.
638 new AlertDialog.Builder(this)
639 .setTitle(R.string.website_settings_clear_all_dialog_title)
640 .setMessage(R.string.website_settings_clear_all_dialog_message)
641 .setPositiveButton(R.string.website_settings_clear_all_dialog_ok_button,
642 new AlertDialog.OnClickListener() {
643 public void onClick(DialogInterface dlg, int which) {
644 WebStorage.getInstance().deleteAllData();
645 GeolocationPermissions.getInstance().clearAll();
Nicolas Roardeb9032c2009-09-28 17:03:36 +0100646 WebStorageSizeManager.resetLastOutOfSpaceNotificationTime();
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100647 mAdapter.askForOrigins();
648 finish();
Ben Murdochb9daacb2009-09-14 10:48:24 +0100649 }})
650 .setNegativeButton(R.string.website_settings_clear_all_dialog_cancel_button, null)
651 .setIcon(android.R.drawable.ic_dialog_alert)
652 .show();
653 return true;
654 }
655 return false;
656 }
Nicolas Roarde46990e2009-06-19 16:27:49 +0100657}