WebsiteSettingsFragment.java revision 99b3ae1a384981f96fca5432f3d20bf4e8d13667
1/* 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; 32import android.view.Menu; 33import android.view.MenuInflater; 34import android.view.MenuItem; 35import android.view.View; 36import android.view.ViewGroup; 37import android.webkit.GeolocationPermissions; 38import android.webkit.ValueCallback; 39import 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; 48import java.util.HashSet; 49import java.util.Iterator; 50import java.util.Map; 51import java.util.Set; 52import java.util.Vector; 53 54/** 55 * Manage the settings for an origin. 56 * We use it to keep track of the 'HTML5' settings, i.e. database (webstorage) 57 * and Geolocation. 58 */ 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; 69 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; 76 private final static int FEATURE_GEOLOCATION = 1; 77 // The number of features available. 78 private final static int FEATURE_COUNT = 2; 79 80 public Site(String origin) { 81 mOrigin = origin; 82 mTitle = null; 83 mIcon = null; 84 mFeatures = 0; 85 } 86 87 public void addFeature(int feature) { 88 mFeatures |= (1 << feature); 89 } 90 91 public boolean hasFeature(int feature) { 92 return (mFeatures & (1 << feature)) != 0; 93 } 94 95 /** 96 * Gets the number of features supported by this site. 97 */ 98 public int getFeatureCount() { 99 int count = 0; 100 for (int i = 0; i < FEATURE_COUNT; ++i) { 101 count += hasFeature(i) ? 1 : 0; 102 } 103 return count; 104 } 105 106 /** 107 * Gets the ID of the nth (zero-based) feature supported by this site. 108 * The return value is a feature ID - one of the FEATURE_XXX values. 109 * This is required to determine which feature is displayed at a given 110 * position in the list of features for this site. This is used both 111 * when populating the view and when responding to clicks on the list. 112 */ 113 public int getFeatureByIndex(int n) { 114 int j = -1; 115 for (int i = 0; i < FEATURE_COUNT; ++i) { 116 j += hasFeature(i) ? 1 : 0; 117 if (j == n) { 118 return i; 119 } 120 } 121 return -1; 122 } 123 124 public String getOrigin() { 125 return mOrigin; 126 } 127 128 public void setTitle(String title) { 129 mTitle = title; 130 } 131 132 public void setIcon(Bitmap icon) { 133 mIcon = icon; 134 } 135 136 public Bitmap getIcon() { 137 return mIcon; 138 } 139 140 public String getPrettyOrigin() { 141 return mTitle == null ? null : hideHttp(mOrigin); 142 } 143 144 public String getPrettyTitle() { 145 return mTitle == null ? hideHttp(mOrigin) : mTitle; 146 } 147 148 private String hideHttp(String str) { 149 Uri uri = Uri.parse(str); 150 return "http".equals(uri.getScheme()) ? str.substring(7) : str; 151 } 152 } 153 154 class SiteAdapter extends ArrayAdapter<Site> 155 implements AdapterView.OnItemClickListener { 156 private int mResource; 157 private LayoutInflater mInflater; 158 private Bitmap mDefaultIcon; 159 private Bitmap mUsageEmptyIcon; 160 private Bitmap mUsageLowIcon; 161 private Bitmap mUsageMediumIcon; 162 private Bitmap mUsageHighIcon; 163 private Bitmap mLocationIcon; 164 private Site mCurrentSite; 165 166 public SiteAdapter(Context context, int rsc) { 167 super(context, rsc); 168 mResource = rsc; 169 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 170 mDefaultIcon = BitmapFactory.decodeResource(getResources(), 171 R.drawable.ic_launcher_shortcut_browser_bookmark); 172 mUsageEmptyIcon = BitmapFactory.decodeResource(getResources(), 173 R.drawable.usage_empty); 174 mUsageLowIcon = BitmapFactory.decodeResource(getResources(), 175 R.drawable.usage_low); 176 mUsageMediumIcon = BitmapFactory.decodeResource(getResources(), 177 R.drawable.usage_medium); 178 mUsageHighIcon = BitmapFactory.decodeResource(getResources(), 179 R.drawable.usage_high); 180 mLocationIcon = BitmapFactory.decodeResource(getResources(), 181 R.drawable.location); 182 askForOrigins(); 183 } 184 185 /** 186 * Adds the specified feature to the site corresponding to supplied 187 * origin in the map. Creates the site if it does not already exist. 188 */ 189 private void addFeatureToSite(Map sites, String origin, int feature) { 190 Site site = null; 191 if (sites.containsKey(origin)) { 192 site = (Site) sites.get(origin); 193 } else { 194 site = new Site(origin); 195 sites.put(origin, site); 196 } 197 site.addFeature(feature); 198 } 199 200 public void askForOrigins() { 201 // Get the list of origins we want to display. 202 // All 'HTML 5 modules' (Database, Geolocation etc) form these 203 // origin strings using WebCore::SecurityOrigin::toString(), so it's 204 // safe to group origins here. Note that WebCore::SecurityOrigin 205 // uses 0 (which is not printed) for the port if the port is the 206 // default for the protocol. Eg http://www.google.com and 207 // http://www.google.com:80 both record a port of 0 and hence 208 // toString() == 'http://www.google.com' for both. 209 210 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() { 211 public void onReceiveValue(Map origins) { 212 Map sites = new HashMap<String, Site>(); 213 if (origins != null) { 214 Iterator<String> iter = origins.keySet().iterator(); 215 while (iter.hasNext()) { 216 addFeatureToSite(sites, iter.next(), Site.FEATURE_WEB_STORAGE); 217 } 218 } 219 askForGeolocation(sites); 220 } 221 }); 222 } 223 224 public void askForGeolocation(final Map sites) { 225 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set>() { 226 public void onReceiveValue(Set origins) { 227 if (origins != null) { 228 Iterator<String> iter = origins.iterator(); 229 while (iter.hasNext()) { 230 addFeatureToSite(sites, iter.next(), Site.FEATURE_GEOLOCATION); 231 } 232 } 233 populateIcons(sites); 234 populateOrigins(sites); 235 } 236 }); 237 } 238 239 public void populateIcons(Map sites) { 240 // Create a map from host to origin. This is used to add metadata 241 // (title, icon) for this origin from the bookmarks DB. 242 HashMap hosts = new HashMap<String, Set<Site> >(); 243 Set keys = sites.keySet(); 244 Iterator<String> originIter = keys.iterator(); 245 while (originIter.hasNext()) { 246 String origin = originIter.next(); 247 Site site = (Site) sites.get(origin); 248 String host = Uri.parse(origin).getHost(); 249 Set hostSites = null; 250 if (hosts.containsKey(host)) { 251 hostSites = (Set) hosts.get(host); 252 } else { 253 hostSites = new HashSet<Site>(); 254 hosts.put(host, hostSites); 255 } 256 hostSites.add(site); 257 } 258 259 // Check the bookmark DB. If we have data for a host used by any of 260 // our origins, use it to set their title and favicon 261 Cursor c = getContext().getContentResolver().query(Browser.BOOKMARKS_URI, 262 new String[] { Browser.BookmarkColumns.URL, Browser.BookmarkColumns.TITLE, 263 Browser.BookmarkColumns.FAVICON }, "bookmark = 1", null, null); 264 265 if ((c != null) && c.moveToFirst()) { 266 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL); 267 int titleIndex = c.getColumnIndex(Browser.BookmarkColumns.TITLE); 268 int faviconIndex = c.getColumnIndex(Browser.BookmarkColumns.FAVICON); 269 do { 270 String url = c.getString(urlIndex); 271 String host = Uri.parse(url).getHost(); 272 if (hosts.containsKey(host)) { 273 String title = c.getString(titleIndex); 274 Bitmap bmp = null; 275 byte[] data = c.getBlob(faviconIndex); 276 if (data != null) { 277 bmp = BitmapFactory.decodeByteArray(data, 0, data.length); 278 } 279 Set matchingSites = (Set) hosts.get(host); 280 Iterator<Site> sitesIter = matchingSites.iterator(); 281 while (sitesIter.hasNext()) { 282 Site site = sitesIter.next(); 283 site.setTitle(title); 284 if (bmp != null) { 285 site.setIcon(bmp); 286 } 287 } 288 } 289 } while (c.moveToNext()); 290 } 291 292 c.close(); 293 } 294 295 296 public void populateOrigins(Map sites) { 297 clear(); 298 299 // We can now simply populate our array with Site instances 300 Set keys = sites.keySet(); 301 Iterator<String> originIter = keys.iterator(); 302 while (originIter.hasNext()) { 303 String origin = originIter.next(); 304 Site site = (Site) sites.get(origin); 305 add(site); 306 } 307 308 notifyDataSetChanged(); 309 310 if (getCount() == 0) { 311 finish(); // we close the screen 312 } 313 } 314 315 public int getCount() { 316 if (mCurrentSite == null) { 317 return super.getCount(); 318 } 319 return mCurrentSite.getFeatureCount(); 320 } 321 322 public String sizeValueToString(long bytes) { 323 // We display the size in MB, to 1dp, rounding up to the next 0.1MB. 324 // bytes should always be greater than zero. 325 if (bytes <= 0) { 326 Log.e(LOGTAG, "sizeValueToString called with non-positive value"); 327 return "0"; 328 } 329 float megabytes = (float) bytes / (1024.0F * 1024.0F); 330 int truncated = (int) Math.ceil(megabytes * 10.0F); 331 float result = (float) (truncated / 10.0F); 332 return String.valueOf(result); 333 } 334 335 /* 336 * If we receive the back event and are displaying 337 * site's settings, we want to go back to the main 338 * list view. If not, we just do nothing (see 339 * dispatchKeyEvent() below). 340 */ 341 public boolean backKeyPressed() { 342 if (mCurrentSite != null) { 343 mCurrentSite = null; 344 askForOrigins(); 345 return true; 346 } 347 return false; 348 } 349 350 /** 351 * @hide 352 * Utility function 353 * Set the icon according to the usage 354 */ 355 public void setIconForUsage(ImageView usageIcon, long usageInBytes) { 356 float usageInMegabytes = (float) usageInBytes / (1024.0F * 1024.0F); 357 usageIcon.setVisibility(View.VISIBLE); 358 359 // We set the correct icon: 360 // 0 < empty < 0.1MB 361 // 0.1MB < low < 3MB 362 // 3MB < medium < 6MB 363 // 6MB < high 364 if (usageInMegabytes <= 0.1) { 365 usageIcon.setImageBitmap(mUsageEmptyIcon); 366 } else if (usageInMegabytes > 0.1 && usageInMegabytes <= 3) { 367 usageIcon.setImageBitmap(mUsageLowIcon); 368 } else if (usageInMegabytes > 3 && usageInMegabytes <= 6) { 369 usageIcon.setImageBitmap(mUsageMediumIcon); 370 } else if (usageInMegabytes > 6) { 371 usageIcon.setImageBitmap(mUsageHighIcon); 372 } 373 } 374 375 public View getView(int position, View convertView, ViewGroup parent) { 376 View view; 377 final TextView title; 378 final TextView subtitle; 379 ImageView icon; 380 final ImageView usageIcon; 381 ImageView locationIcon; 382 383 if (convertView == null) { 384 view = mInflater.inflate(mResource, parent, false); 385 } else { 386 view = convertView; 387 } 388 389 title = (TextView) view.findViewById(R.id.title); 390 subtitle = (TextView) view.findViewById(R.id.subtitle); 391 icon = (ImageView) view.findViewById(R.id.icon); 392 usageIcon = (ImageView) view.findViewById(R.id.usage_icon); 393 locationIcon = (ImageView) view.findViewById(R.id.location_icon); 394 usageIcon.setVisibility(View.GONE); 395 locationIcon.setVisibility(View.GONE); 396 397 if (mCurrentSite == null) { 398 setTitle(getString(R.string.pref_extras_website_settings)); 399 400 Site site = getItem(position); 401 title.setText(site.getPrettyTitle()); 402 subtitle.setText(site.getPrettyOrigin()); 403 icon.setVisibility(View.VISIBLE); 404 usageIcon.setVisibility(View.INVISIBLE); 405 locationIcon.setVisibility(View.INVISIBLE); 406 Bitmap bmp = site.getIcon(); 407 if (bmp == null) { 408 bmp = mDefaultIcon; 409 } 410 icon.setImageBitmap(bmp); 411 // We set the site as the view's tag, 412 // so that we can get it in onItemClick() 413 view.setTag(site); 414 415 if (site.hasFeature(Site.FEATURE_WEB_STORAGE)) { 416 String origin = site.getOrigin(); 417 WebStorage.getInstance().getUsageForOrigin(origin, new ValueCallback<Long>() { 418 public void onReceiveValue(Long value) { 419 if (value != null) { 420 setIconForUsage(usageIcon, value.longValue()); 421 } 422 } 423 }); 424 } 425 426 if (site.hasFeature(Site.FEATURE_GEOLOCATION)) { 427 locationIcon.setVisibility(View.VISIBLE); 428 locationIcon.setImageBitmap(mLocationIcon); 429 } 430 } else { 431 setTitle(mCurrentSite.getPrettyTitle()); 432 icon.setVisibility(View.GONE); 433 String origin = mCurrentSite.getOrigin(); 434 switch (mCurrentSite.getFeatureByIndex(position)) { 435 case Site.FEATURE_WEB_STORAGE: 436 WebStorage.getInstance().getUsageForOrigin(origin, new ValueCallback<Long>() { 437 public void onReceiveValue(Long value) { 438 if (value != null) { 439 String usage = sizeValueToString(value.longValue()) + " " + sMBStored; 440 title.setText(R.string.webstorage_clear_data_title); 441 subtitle.setText(usage); 442 } 443 } 444 }); 445 break; 446 case Site.FEATURE_GEOLOCATION: 447 title.setText(R.string.geolocation_settings_page_title); 448 GeolocationPermissions.getInstance().getAllowed(origin, new ValueCallback<Boolean>() { 449 public void onReceiveValue(Boolean allowed) { 450 if (allowed != null) { 451 if (allowed.booleanValue()) { 452 subtitle.setText(R.string.geolocation_settings_page_summary_allowed); 453 } else { 454 subtitle.setText(R.string.geolocation_settings_page_summary_not_allowed); 455 } 456 } 457 } 458 }); 459 break; 460 } 461 } 462 463 return view; 464 } 465 466 public void onItemClick(AdapterView<?> parent, 467 View view, 468 int position, 469 long id) { 470 if (mCurrentSite != null) { 471 switch (mCurrentSite.getFeatureByIndex(position)) { 472 case Site.FEATURE_WEB_STORAGE: 473 new AlertDialog.Builder(getContext()) 474 .setTitle(R.string.webstorage_clear_data_dialog_title) 475 .setMessage(R.string.webstorage_clear_data_dialog_message) 476 .setPositiveButton(R.string.webstorage_clear_data_dialog_ok_button, 477 new AlertDialog.OnClickListener() { 478 public void onClick(DialogInterface dlg, int which) { 479 WebStorage.getInstance().deleteOrigin(mCurrentSite.getOrigin()); 480 mCurrentSite = null; 481 askForOrigins(); 482 }}) 483 .setNegativeButton(R.string.webstorage_clear_data_dialog_cancel_button, null) 484 .setIcon(android.R.drawable.ic_dialog_alert) 485 .show(); 486 break; 487 case Site.FEATURE_GEOLOCATION: 488 new AlertDialog.Builder(getContext()) 489 .setTitle(R.string.geolocation_settings_page_dialog_title) 490 .setMessage(R.string.geolocation_settings_page_dialog_message) 491 .setPositiveButton(R.string.geolocation_settings_page_dialog_ok_button, 492 new AlertDialog.OnClickListener() { 493 public void onClick(DialogInterface dlg, int which) { 494 GeolocationPermissions.getInstance().clear(mCurrentSite.getOrigin()); 495 mCurrentSite = null; 496 askForOrigins(); 497 }}) 498 .setNegativeButton(R.string.geolocation_settings_page_dialog_cancel_button, null) 499 .setIcon(android.R.drawable.ic_dialog_alert) 500 .show(); 501 break; 502 } 503 } else { 504 mCurrentSite = (Site) view.getTag(); 505 notifyDataSetChanged(); 506 } 507 } 508 } 509 510 /** 511 * Intercepts the back key to immediately notify 512 * NativeDialog that we are done. 513 */ 514 public boolean dispatchKeyEvent(KeyEvent event) { 515 if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK) 516 && (event.getAction() == KeyEvent.ACTION_DOWN)) { 517 if ((mAdapter != null) && (mAdapter.backKeyPressed())){ 518 return true; // event consumed 519 } 520 } 521 return super.dispatchKeyEvent(event); 522 } 523 524 @Override 525 protected void onCreate(Bundle icicle) { 526 super.onCreate(icicle); 527 if (sMBStored == null) { 528 sMBStored = getString(R.string.webstorage_origin_summary_mb_stored); 529 } 530 mAdapter = new SiteAdapter(this, R.layout.website_settings_row); 531 setListAdapter(mAdapter); 532 getListView().setOnItemClickListener(mAdapter); 533 } 534 535 @Override 536 public boolean onCreateOptionsMenu(Menu menu) { 537 MenuInflater inflater = getMenuInflater(); 538 inflater.inflate(R.menu.websitesettings, menu); 539 return true; 540 } 541 542 @Override 543 public boolean onPrepareOptionsMenu(Menu menu) { 544 // If we aren't listing any sites hide the clear all button (and hence the menu). 545 return mAdapter.getCount() > 0; 546 } 547 548 @Override 549 public boolean onOptionsItemSelected(MenuItem item) { 550 switch (item.getItemId()) { 551 case R.id.website_settings_menu_clear_all: 552 // Show the prompt to clear all origins of their data and geolocation permissions. 553 new AlertDialog.Builder(this) 554 .setTitle(R.string.website_settings_clear_all_dialog_title) 555 .setMessage(R.string.website_settings_clear_all_dialog_message) 556 .setPositiveButton(R.string.website_settings_clear_all_dialog_ok_button, 557 new AlertDialog.OnClickListener() { 558 public void onClick(DialogInterface dlg, int which) { 559 WebStorage.getInstance().deleteAllData(); 560 GeolocationPermissions.getInstance().clearAll(); 561 mAdapter.askForOrigins(); 562 finish(); 563 }}) 564 .setNegativeButton(R.string.website_settings_clear_all_dialog_cancel_button, null) 565 .setIcon(android.R.drawable.ic_dialog_alert) 566 .show(); 567 return true; 568 } 569 return false; 570 } 571} 572