blob: fc72b62b1dbf1ee214a20ba7f0fa6a8df1da1ae3 [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 Roarde46990e2009-06-19 16:27:49 +010038import android.webkit.WebIconDatabase;
39import android.webkit.WebStorage;
40import android.widget.ArrayAdapter;
41import android.widget.AdapterView;
42import android.widget.AdapterView.OnItemClickListener;
43import android.widget.ImageView;
44import android.widget.TextView;
45
46import java.util.HashMap;
Steve Blockee0d6392009-07-28 13:49:15 +010047import java.util.HashSet;
Nicolas Roarde46990e2009-06-19 16:27:49 +010048import java.util.Iterator;
Steve Block089ce3a2009-07-29 17:16:01 +010049import java.util.Map;
Nicolas Roarde46990e2009-06-19 16:27:49 +010050import java.util.Set;
51import java.util.Vector;
52
53/**
54 * Manage the settings for an origin.
Steve Block089ce3a2009-07-29 17:16:01 +010055 * We use it to keep track of the 'HTML5' settings, i.e. database (webstorage)
56 * and Geolocation.
Nicolas Roarde46990e2009-06-19 16:27:49 +010057 */
58public class WebsiteSettingsActivity extends ListActivity {
59
60 private String LOGTAG = "WebsiteSettingsActivity";
61 private static String sMBStored = null;
62 private SiteAdapter mAdapter = null;
63
64 class Site {
65 private String mOrigin;
66 private String mTitle;
67 private Bitmap mIcon;
Steve Block089ce3a2009-07-29 17:16:01 +010068 private int mFeatures;
69
70 // These constants provide the set of features that a site may support
71 // They must be consecutive. To add a new feature, add a new FEATURE_XXX
72 // variable with value equal to the current value of FEATURE_COUNT, then
73 // increment FEATURE_COUNT.
74 private final static int FEATURE_WEB_STORAGE = 0;
Steve Blockf344d032009-07-30 10:50:45 +010075 private final static int FEATURE_GEOLOCATION = 1;
Steve Block089ce3a2009-07-29 17:16:01 +010076 // The number of features available.
Steve Blockf344d032009-07-30 10:50:45 +010077 private final static int FEATURE_COUNT = 2;
Nicolas Roarde46990e2009-06-19 16:27:49 +010078
Steve Block1ad98cf2009-07-28 11:07:10 +010079 public Site(String origin) {
Nicolas Roarde46990e2009-06-19 16:27:49 +010080 mOrigin = origin;
Steve Block1ad98cf2009-07-28 11:07:10 +010081 mTitle = null;
82 mIcon = null;
Steve Block089ce3a2009-07-29 17:16:01 +010083 mFeatures = 0;
84 }
85
86 public void addFeature(int feature) {
87 mFeatures |= (1 << feature);
88 }
89
90 public boolean hasFeature(int feature) {
91 return (mFeatures & (1 << feature)) != 0;
92 }
93
94 /**
95 * Gets the number of features supported by this site.
96 */
97 public int getFeatureCount() {
98 int count = 0;
99 for (int i = 0; i < FEATURE_COUNT; ++i) {
100 count += hasFeature(i) ? 1 : 0;
101 }
102 return count;
103 }
104
105 /**
106 * Gets the ID of the nth (zero-based) feature supported by this site.
107 * The return value is a feature ID - one of the FEATURE_XXX values.
108 * This is required to determine which feature is displayed at a given
109 * position in the list of features for this site. This is used both
Steve Blockf344d032009-07-30 10:50:45 +0100110 * when populating the view and when responding to clicks on the list.
Steve Block089ce3a2009-07-29 17:16:01 +0100111 */
112 public int getFeatureByIndex(int n) {
113 int j = -1;
114 for (int i = 0; i < FEATURE_COUNT; ++i) {
115 j += hasFeature(i) ? 1 : 0;
116 if (j == n) {
117 return i;
118 }
119 }
120 return -1;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100121 }
122
123 public String getOrigin() {
124 return mOrigin;
125 }
126
127 public void setTitle(String title) {
128 mTitle = title;
129 }
130
Nicolas Roarde46990e2009-06-19 16:27:49 +0100131 public void setIcon(Bitmap icon) {
132 mIcon = icon;
133 }
134
135 public Bitmap getIcon() {
136 return mIcon;
137 }
Steve Block1ad98cf2009-07-28 11:07:10 +0100138
139 public String getPrettyOrigin() {
140 return mTitle == null ? null : hideHttp(mOrigin);
141 }
142
143 public String getPrettyTitle() {
144 return mTitle == null ? hideHttp(mOrigin) : mTitle;
145 }
146
147 private String hideHttp(String str) {
148 Uri uri = Uri.parse(str);
149 return "http".equals(uri.getScheme()) ? str.substring(7) : str;
150 }
Nicolas Roarde46990e2009-06-19 16:27:49 +0100151 }
152
153 class SiteAdapter extends ArrayAdapter<Site>
154 implements AdapterView.OnItemClickListener {
155 private int mResource;
156 private LayoutInflater mInflater;
157 private Bitmap mDefaultIcon;
158 private Site mCurrentSite;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100159
160 public SiteAdapter(Context context, int rsc) {
161 super(context, rsc);
162 mResource = rsc;
163 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
164 mDefaultIcon = BitmapFactory.decodeResource(getResources(),
165 R.drawable.ic_launcher_shortcut_browser_bookmark);
166 populateOrigins();
167 }
168
Steve Block089ce3a2009-07-29 17:16:01 +0100169 /**
170 * Adds the specified feature to the site corresponding to supplied
171 * origin in the map. Creates the site if it does not already exist.
172 */
173 private void addFeatureToSite(Map sites, String origin, int feature) {
174 Site site = null;
175 if (sites.containsKey(origin)) {
176 site = (Site) sites.get(origin);
177 } else {
178 site = new Site(origin);
179 sites.put(origin, site);
180 }
181 site.addFeature(feature);
182 }
183
Nicolas Roarde46990e2009-06-19 16:27:49 +0100184 public void populateOrigins() {
185 clear();
186
Steve Block089ce3a2009-07-29 17:16:01 +0100187 // Get the list of origins we want to display.
188 // All 'HTML 5 modules' (Database, Geolocation etc) form these
189 // origin strings using WebCore::SecurityOrigin::toString(), so it's
190 // safe to group origins here. Note that WebCore::SecurityOrigin
191 // uses 0 (which is not printed) for the port if the port is the
192 // default for the protocol. Eg http://www.google.com and
193 // http://www.google.com:80 both record a port of 0 and hence
194 // toString() == 'http://www.google.com' for both.
Andrei Popescu824faeb2009-07-21 18:24:06 +0100195 Set origins = WebStorage.getInstance().getOrigins();
Steve Block089ce3a2009-07-29 17:16:01 +0100196 Map sites = new HashMap<String, Site>();
Nicolas Roarde46990e2009-06-19 16:27:49 +0100197 if (origins != null) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100198 Iterator<String> iter = origins.iterator();
199 while (iter.hasNext()) {
Steve Block089ce3a2009-07-29 17:16:01 +0100200 addFeatureToSite(sites, iter.next(), Site.FEATURE_WEB_STORAGE);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100201 }
202 }
Steve Blockf344d032009-07-30 10:50:45 +0100203 origins = GeolocationPermissions.getInstance().getOrigins();
204 if (origins != null) {
205 Iterator<String> iter = origins.iterator();
206 while (iter.hasNext()) {
207 addFeatureToSite(sites, iter.next(), Site.FEATURE_GEOLOCATION);
208 }
209 }
Nicolas Roarde46990e2009-06-19 16:27:49 +0100210
Steve Blockee0d6392009-07-28 13:49:15 +0100211 // Create a map from host to origin. This is used to add metadata
212 // (title, icon) for this origin from the bookmarks DB.
213 HashMap hosts = new HashMap<String, Set<Site> >();
Steve Block089ce3a2009-07-29 17:16:01 +0100214 Set keys = sites.keySet();
215 Iterator<String> originIter = keys.iterator();
216 while (originIter.hasNext()) {
217 String origin = originIter.next();
218 Site site = (Site) sites.get(origin);
219 String host = Uri.parse(origin).getHost();
Steve Blockee0d6392009-07-28 13:49:15 +0100220 Set hostSites = null;
221 if (hosts.containsKey(host)) {
222 hostSites = (Set) hosts.get(host);
223 } else {
224 hostSites = new HashSet<Site>();
225 hosts.put(host, hostSites);
226 }
227 hostSites.add(site);
228 }
229
230 // Check the bookmark DB. If we have data for a host used by any of
231 // our origins, use it to set their title and favicon
Nicolas Roarde46990e2009-06-19 16:27:49 +0100232 Cursor c = getContext().getContentResolver().query(Browser.BOOKMARKS_URI,
233 new String[] { Browser.BookmarkColumns.URL, Browser.BookmarkColumns.TITLE,
234 Browser.BookmarkColumns.FAVICON }, "bookmark = 1", null, null);
235
236 if ((c != null) && c.moveToFirst()) {
237 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
238 int titleIndex = c.getColumnIndex(Browser.BookmarkColumns.TITLE);
239 int faviconIndex = c.getColumnIndex(Browser.BookmarkColumns.FAVICON);
240 do {
241 String url = c.getString(urlIndex);
242 String host = Uri.parse(url).getHost();
Steve Blockee0d6392009-07-28 13:49:15 +0100243 if (hosts.containsKey(host)) {
Nicolas Roarde46990e2009-06-19 16:27:49 +0100244 String title = c.getString(titleIndex);
Steve Blockee0d6392009-07-28 13:49:15 +0100245 Bitmap bmp = null;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100246 byte[] data = c.getBlob(faviconIndex);
247 if (data != null) {
Steve Blockee0d6392009-07-28 13:49:15 +0100248 bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
249 }
250 Set matchingSites = (Set) hosts.get(host);
Steve Block089ce3a2009-07-29 17:16:01 +0100251 Iterator<Site> sitesIter = matchingSites.iterator();
Steve Blockee0d6392009-07-28 13:49:15 +0100252 while (sitesIter.hasNext()) {
253 Site site = sitesIter.next();
254 site.setTitle(title);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100255 if (bmp != null) {
256 site.setIcon(bmp);
257 }
258 }
259 }
260 } while (c.moveToNext());
261 }
262
263 // We can now simply populate our array with Site instances
Steve Block089ce3a2009-07-29 17:16:01 +0100264 keys = sites.keySet();
265 originIter = keys.iterator();
266 while (originIter.hasNext()) {
267 String origin = originIter.next();
268 Site site = (Site) sites.get(origin);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100269 add(site);
270 }
271
272 if (getCount() == 0) {
273 finish(); // we close the screen
274 }
275 }
276
277 public int getCount() {
278 if (mCurrentSite == null) {
279 return super.getCount();
280 }
Steve Block089ce3a2009-07-29 17:16:01 +0100281 return mCurrentSite.getFeatureCount();
Nicolas Roarde46990e2009-06-19 16:27:49 +0100282 }
283
Steve Block764f0c92009-09-10 15:01:37 +0100284 public String sizeValueToString(long bytes) {
285 // We display the size in MB, to 1dp, rounding up to the next 0.1MB.
286 // bytes should always be greater than zero.
287 if (bytes <= 0) {
288 Log.e(LOGTAG, "sizeValueToString called with non-positive value");
Nicolas Roarde46990e2009-06-19 16:27:49 +0100289 return "0";
290 }
Steve Block764f0c92009-09-10 15:01:37 +0100291 float megabytes = (float) bytes / (1024.0F * 1024.0F);
292 int truncated = (int) Math.ceil(megabytes * 10.0F);
293 float result = (float) (truncated / 10.0F);
294 return String.valueOf(result);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100295 }
296
297 /*
298 * If we receive the back event and are displaying
299 * site's settings, we want to go back to the main
300 * list view. If not, we just do nothing (see
301 * dispatchKeyEvent() below).
302 */
303 public boolean backKeyPressed() {
304 if (mCurrentSite != null) {
305 mCurrentSite = null;
306 populateOrigins();
307 notifyDataSetChanged();
308 return true;
309 }
310 return false;
311 }
312
313 public View getView(int position, View convertView, ViewGroup parent) {
314 View view;
315 TextView title;
316 TextView subtitle;
317 ImageView icon;
318
319 if (convertView == null) {
320 view = mInflater.inflate(mResource, parent, false);
321 } else {
322 view = convertView;
323 }
324
325 title = (TextView) view.findViewById(R.id.title);
326 subtitle = (TextView) view.findViewById(R.id.subtitle);
327 icon = (ImageView) view.findViewById(R.id.icon);
328
329 if (mCurrentSite == null) {
Steve Block4d055a52009-07-31 08:38:58 +0100330 setTitle(getString(R.string.pref_extras_website_settings));
331
Nicolas Roarde46990e2009-06-19 16:27:49 +0100332 Site site = getItem(position);
Steve Block1ad98cf2009-07-28 11:07:10 +0100333 title.setText(site.getPrettyTitle());
334 subtitle.setText(site.getPrettyOrigin());
Nicolas Roarde46990e2009-06-19 16:27:49 +0100335 icon.setVisibility(View.VISIBLE);
336 Bitmap bmp = site.getIcon();
337 if (bmp == null) {
338 bmp = mDefaultIcon;
339 }
340 icon.setImageBitmap(bmp);
341 // We set the site as the view's tag,
342 // so that we can get it in onItemClick()
343 view.setTag(site);
344 } else {
Steve Block4d055a52009-07-31 08:38:58 +0100345 setTitle(mCurrentSite.getPrettyTitle());
Nicolas Roarde46990e2009-06-19 16:27:49 +0100346 icon.setVisibility(View.GONE);
Steve Block089ce3a2009-07-29 17:16:01 +0100347 String origin = mCurrentSite.getOrigin();
348 switch (mCurrentSite.getFeatureByIndex(position)) {
349 case Site.FEATURE_WEB_STORAGE:
350 long usageValue = WebStorage.getInstance().getUsageForOrigin(origin);
351 String usage = sizeValueToString(usageValue) + " " + sMBStored;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100352
Steve Block089ce3a2009-07-29 17:16:01 +0100353 title.setText(R.string.webstorage_clear_data_title);
354 subtitle.setText(usage);
355 break;
Steve Blockf344d032009-07-30 10:50:45 +0100356 case Site.FEATURE_GEOLOCATION:
357 title.setText(R.string.geolocation_settings_page_title);
358 boolean allowed = GeolocationPermissions.getInstance().getAllowed(origin);
359 subtitle.setText(allowed ?
360 R.string.geolocation_settings_page_summary_allowed :
361 R.string.geolocation_settings_page_summary_not_allowed);
362 break;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100363 }
364 }
365
366 return view;
367 }
368
369 public void onItemClick(AdapterView<?> parent,
370 View view,
371 int position,
372 long id) {
373 if (mCurrentSite != null) {
Steve Block089ce3a2009-07-29 17:16:01 +0100374 switch (mCurrentSite.getFeatureByIndex(position)) {
375 case Site.FEATURE_WEB_STORAGE:
376 new AlertDialog.Builder(getContext())
377 .setTitle(R.string.webstorage_clear_data_dialog_title)
378 .setMessage(R.string.webstorage_clear_data_dialog_message)
379 .setPositiveButton(R.string.webstorage_clear_data_dialog_ok_button,
380 new AlertDialog.OnClickListener() {
381 public void onClick(DialogInterface dlg, int which) {
382 WebStorage.getInstance().deleteOrigin(mCurrentSite.getOrigin());
383 mCurrentSite = null;
384 populateOrigins();
385 notifyDataSetChanged();
386 }})
387 .setNegativeButton(R.string.webstorage_clear_data_dialog_cancel_button, null)
388 .setIcon(android.R.drawable.ic_dialog_alert)
389 .show();
390 break;
Steve Blockf344d032009-07-30 10:50:45 +0100391 case Site.FEATURE_GEOLOCATION:
392 new AlertDialog.Builder(getContext())
393 .setTitle(R.string.geolocation_settings_page_dialog_title)
394 .setMessage(R.string.geolocation_settings_page_dialog_message)
395 .setPositiveButton(R.string.geolocation_settings_page_dialog_ok_button,
396 new AlertDialog.OnClickListener() {
397 public void onClick(DialogInterface dlg, int which) {
398 GeolocationPermissions.getInstance().clear(mCurrentSite.getOrigin());
399 mCurrentSite = null;
400 populateOrigins();
401 notifyDataSetChanged();
402 }})
403 .setNegativeButton(R.string.geolocation_settings_page_dialog_cancel_button, null)
404 .setIcon(android.R.drawable.ic_dialog_alert)
405 .show();
406 break;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100407 }
408 } else {
409 mCurrentSite = (Site) view.getTag();
410 notifyDataSetChanged();
411 }
412 }
413 }
414
415 /**
416 * Intercepts the back key to immediately notify
417 * NativeDialog that we are done.
418 */
419 public boolean dispatchKeyEvent(KeyEvent event) {
420 if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
421 && (event.getAction() == KeyEvent.ACTION_DOWN)) {
422 if ((mAdapter != null) && (mAdapter.backKeyPressed())){
423 return true; // event consumed
424 }
425 }
426 return super.dispatchKeyEvent(event);
427 }
428
429 @Override
430 protected void onCreate(Bundle icicle) {
431 super.onCreate(icicle);
432 if (sMBStored == null) {
433 sMBStored = getString(R.string.webstorage_origin_summary_mb_stored);
434 }
435 mAdapter = new SiteAdapter(this, R.layout.application);
436 setListAdapter(mAdapter);
437 getListView().setOnItemClickListener(mAdapter);
438 }
Ben Murdochb9daacb2009-09-14 10:48:24 +0100439
440 @Override
441 public boolean onCreateOptionsMenu(Menu menu) {
442 MenuInflater inflater = getMenuInflater();
443 inflater.inflate(R.menu.websitesettings, menu);
444 return true;
445 }
446
447 @Override
448 public boolean onPrepareOptionsMenu(Menu menu) {
449 // If we aren't listing any sites hide the clear all button (and hence the menu).
450 return mAdapter.getCount() > 0;
451 }
452
453 @Override
454 public boolean onOptionsItemSelected(MenuItem item) {
455 switch (item.getItemId()) {
456 case R.id.website_settings_menu_clear_all:
457 // Show the prompt to clear all origins of their data and geolocation permissions.
458 new AlertDialog.Builder(this)
459 .setTitle(R.string.website_settings_clear_all_dialog_title)
460 .setMessage(R.string.website_settings_clear_all_dialog_message)
461 .setPositiveButton(R.string.website_settings_clear_all_dialog_ok_button,
462 new AlertDialog.OnClickListener() {
463 public void onClick(DialogInterface dlg, int which) {
464 WebStorage.getInstance().deleteAllData();
465 GeolocationPermissions.getInstance().clearAll();
466 mAdapter.populateOrigins();
467 mAdapter.notifyDataSetChanged();
468 }})
469 .setNegativeButton(R.string.website_settings_clear_all_dialog_cancel_button, null)
470 .setIcon(android.R.drawable.ic_dialog_alert)
471 .show();
472 return true;
473 }
474 return false;
475 }
Nicolas Roarde46990e2009-06-19 16:27:49 +0100476}