BrowserSettings.java revision 7205e9df23ee995f9c15c54bab341c251fdeb779
1 2/* 3 * Copyright (C) 2007 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package com.android.browser; 19 20import android.app.ActivityManager; 21import android.content.ContentResolver; 22import android.content.Context; 23import android.content.pm.ActivityInfo; 24import android.content.SharedPreferences; 25import android.content.SharedPreferences.Editor; 26import android.preference.PreferenceActivity; 27import android.preference.PreferenceScreen; 28import android.webkit.CookieManager; 29import android.webkit.GeolocationPermissions; 30import android.webkit.ValueCallback; 31import android.webkit.WebView; 32import android.webkit.WebViewDatabase; 33import android.webkit.WebIconDatabase; 34import android.webkit.WebSettings; 35import android.webkit.WebStorage; 36import android.preference.PreferenceManager; 37import android.provider.Browser; 38 39import java.util.HashMap; 40import java.util.Map; 41import java.util.Set; 42import java.util.Observable; 43 44/* 45 * Package level class for storing various WebView and Browser settings. To use 46 * this class: 47 * BrowserSettings s = BrowserSettings.getInstance(); 48 * s.addObserver(webView.getSettings()); 49 * s.loadFromDb(context); // Only needed on app startup 50 * s.javaScriptEnabled = true; 51 * ... // set any other settings 52 * s.update(); // this will update all the observers 53 * 54 * To remove an observer: 55 * s.deleteObserver(webView.getSettings()); 56 */ 57class BrowserSettings extends Observable { 58 59 // Private variables for settings 60 // NOTE: these defaults need to be kept in sync with the XML 61 // until the performance of PreferenceManager.setDefaultValues() 62 // is improved. 63 private boolean loadsImagesAutomatically = true; 64 private boolean javaScriptEnabled = true; 65 private boolean pluginsEnabled = true; 66 private boolean javaScriptCanOpenWindowsAutomatically = false; 67 private boolean showSecurityWarnings = true; 68 private boolean rememberPasswords = true; 69 private boolean saveFormData = true; 70 private boolean openInBackground = false; 71 private String defaultTextEncodingName; 72 private String homeUrl = ""; 73 private boolean loginInitialized = false; 74 private boolean autoFitPage = true; 75 private boolean landscapeOnly = false; 76 private boolean loadsPageInOverviewMode = true; 77 private boolean showDebugSettings = false; 78 // HTML5 API flags 79 private boolean appCacheEnabled = true; 80 private boolean databaseEnabled = true; 81 private boolean domStorageEnabled = true; 82 private boolean geolocationEnabled = true; 83 private boolean workersEnabled = true; // only affects V8. JSC does not have a similar setting 84 // HTML5 API configuration params 85 private long appCacheMaxSize = Long.MAX_VALUE; 86 private String appCachePath; // default value set in loadFromDb(). 87 private String databasePath; // default value set in loadFromDb() 88 private String geolocationDatabasePath; // default value set in loadFromDb() 89 private WebStorageSizeManager webStorageSizeManager; 90 91 private String jsFlags = ""; 92 93 private final static String TAG = "BrowserSettings"; 94 95 // Development settings 96 public WebSettings.LayoutAlgorithm layoutAlgorithm = 97 WebSettings.LayoutAlgorithm.NARROW_COLUMNS; 98 private boolean useWideViewPort = true; 99 private int userAgent = 0; 100 private boolean tracing = false; 101 private boolean lightTouch = false; 102 private boolean navDump = false; 103 104 // By default the error console is shown once the user navigates to about:debug. 105 // The setting can be then toggled from the settings menu. 106 private boolean showConsole = true; 107 108 // Private preconfigured values 109 private static int minimumFontSize = 8; 110 private static int minimumLogicalFontSize = 8; 111 private static int defaultFontSize = 16; 112 private static int defaultFixedFontSize = 13; 113 private static WebSettings.TextSize textSize = 114 WebSettings.TextSize.NORMAL; 115 private static WebSettings.ZoomDensity zoomDensity = 116 WebSettings.ZoomDensity.MEDIUM; 117 private static int pageCacheCapacity; 118 119 // Preference keys that are used outside this class 120 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache"; 121 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies"; 122 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history"; 123 public final static String PREF_HOMEPAGE = "homepage"; 124 public final static String PREF_CLEAR_FORM_DATA = 125 "privacy_clear_form_data"; 126 public final static String PREF_CLEAR_PASSWORDS = 127 "privacy_clear_passwords"; 128 public final static String PREF_EXTRAS_RESET_DEFAULTS = 129 "reset_default_preferences"; 130 public final static String PREF_DEBUG_SETTINGS = "debug_menu"; 131 public final static String PREF_WEBSITE_SETTINGS = "website_settings"; 132 public final static String PREF_TEXT_SIZE = "text_size"; 133 public final static String PREF_DEFAULT_ZOOM = "default_zoom"; 134 public final static String PREF_DEFAULT_TEXT_ENCODING = 135 "default_text_encoding"; 136 public final static String PREF_CLEAR_GEOLOCATION_ACCESS = 137 "privacy_clear_geolocation_access"; 138 139 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " + 140 "U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, " + 141 "like Gecko) Version/4.0 Safari/530.17"; 142 143 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " + 144 "CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 " + 145 "(KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16"; 146 147 // Value to truncate strings when adding them to a TextView within 148 // a ListView 149 public final static int MAX_TEXTVIEW_LEN = 80; 150 151 private TabControl mTabControl; 152 153 // Single instance of the BrowserSettings for use in the Browser app. 154 private static BrowserSettings sSingleton; 155 156 // Private map of WebSettings to Observer objects used when deleting an 157 // observer. 158 private HashMap<WebSettings,Observer> mWebSettingsToObservers = 159 new HashMap<WebSettings,Observer>(); 160 161 /* 162 * An observer wrapper for updating a WebSettings object with the new 163 * settings after a call to BrowserSettings.update(). 164 */ 165 static class Observer implements java.util.Observer { 166 // Private WebSettings object that will be updated. 167 private WebSettings mSettings; 168 169 Observer(WebSettings w) { 170 mSettings = w; 171 } 172 173 public void update(Observable o, Object arg) { 174 BrowserSettings b = (BrowserSettings)o; 175 WebSettings s = mSettings; 176 177 s.setLayoutAlgorithm(b.layoutAlgorithm); 178 if (b.userAgent == 0) { 179 // use the default ua string 180 s.setUserAgentString(null); 181 } else if (b.userAgent == 1) { 182 s.setUserAgentString(DESKTOP_USERAGENT); 183 } else if (b.userAgent == 2) { 184 s.setUserAgentString(IPHONE_USERAGENT); 185 } 186 s.setUseWideViewPort(b.useWideViewPort); 187 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically); 188 s.setJavaScriptEnabled(b.javaScriptEnabled); 189 s.setPluginsEnabled(b.pluginsEnabled); 190 s.setJavaScriptCanOpenWindowsAutomatically( 191 b.javaScriptCanOpenWindowsAutomatically); 192 s.setDefaultTextEncodingName(b.defaultTextEncodingName); 193 s.setMinimumFontSize(b.minimumFontSize); 194 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize); 195 s.setDefaultFontSize(b.defaultFontSize); 196 s.setDefaultFixedFontSize(b.defaultFixedFontSize); 197 s.setNavDump(b.navDump); 198 s.setTextSize(b.textSize); 199 s.setDefaultZoom(b.zoomDensity); 200 s.setLightTouchEnabled(b.lightTouch); 201 s.setSaveFormData(b.saveFormData); 202 s.setSavePassword(b.rememberPasswords); 203 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode); 204 s.setPageCacheCapacity(pageCacheCapacity); 205 206 // WebView inside Browser doesn't want initial focus to be set. 207 s.setNeedInitialFocus(false); 208 // Browser supports multiple windows 209 s.setSupportMultipleWindows(true); 210 // Use system over scroll background 211 s.setUseSystemOverscrollBackground(true); 212 213 // HTML5 API flags 214 s.setAppCacheEnabled(b.appCacheEnabled); 215 s.setDatabaseEnabled(b.databaseEnabled); 216 s.setDomStorageEnabled(b.domStorageEnabled); 217 s.setWorkersEnabled(b.workersEnabled); // This only affects V8. 218 s.setGeolocationEnabled(b.geolocationEnabled); 219 220 // HTML5 configuration parameters. 221 s.setAppCacheMaxSize(b.appCacheMaxSize); 222 s.setAppCachePath(b.appCachePath); 223 s.setDatabasePath(b.databasePath); 224 s.setGeolocationDatabasePath(b.geolocationDatabasePath); 225 226 // Enable/Disable the error console. 227 b.mTabControl.getBrowserActivity().setShouldShowErrorConsole( 228 b.showDebugSettings && b.showConsole); 229 } 230 } 231 232 /** 233 * Load settings from the browser app's database. 234 * NOTE: Strings used for the preferences must match those specified 235 * in the browser_preferences.xml 236 * @param ctx A Context object used to query the browser's settings 237 * database. If the database exists, the saved settings will be 238 * stored in this BrowserSettings object. This will update all 239 * observers of this object. 240 */ 241 public void loadFromDb(Context ctx) { 242 SharedPreferences p = 243 PreferenceManager.getDefaultSharedPreferences(ctx); 244 // Set the default value for the Application Caches path. 245 appCachePath = ctx.getDir("appcache", 0).getPath(); 246 // Determine the maximum size of the application cache. 247 webStorageSizeManager = new WebStorageSizeManager( 248 ctx, 249 new WebStorageSizeManager.StatFsDiskInfo(appCachePath), 250 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath)); 251 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize(); 252 // Set the default value for the Database path. 253 databasePath = ctx.getDir("databases", 0).getPath(); 254 // Set the default value for the Geolocation database path. 255 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath(); 256 257 homeUrl = getFactoryResetHomeUrl(ctx); 258 259 // the cost of one cached page is ~3M (measured using nytimes.com). For 260 // low end devices, we only cache one page. For high end devices, we try 261 // to cache more pages, currently choose 5. 262 ActivityManager am = (ActivityManager) ctx 263 .getSystemService(Context.ACTIVITY_SERVICE); 264 if (am.getMemoryClass() > 16) { 265 pageCacheCapacity = 5; 266 } else { 267 pageCacheCapacity = 1; 268 } 269 270 // Load the defaults from the xml 271 // This call is TOO SLOW, need to manually keep the defaults 272 // in sync 273 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences); 274 syncSharedPreferences(p); 275 } 276 277 /* package */ void syncSharedPreferences(SharedPreferences p) { 278 279 homeUrl = 280 p.getString(PREF_HOMEPAGE, homeUrl); 281 282 loadsImagesAutomatically = p.getBoolean("load_images", 283 loadsImagesAutomatically); 284 javaScriptEnabled = p.getBoolean("enable_javascript", 285 javaScriptEnabled); 286 pluginsEnabled = p.getBoolean("enable_plugins", 287 pluginsEnabled); 288 javaScriptCanOpenWindowsAutomatically = !p.getBoolean( 289 "block_popup_windows", 290 !javaScriptCanOpenWindowsAutomatically); 291 showSecurityWarnings = p.getBoolean("show_security_warnings", 292 showSecurityWarnings); 293 rememberPasswords = p.getBoolean("remember_passwords", 294 rememberPasswords); 295 saveFormData = p.getBoolean("save_formdata", 296 saveFormData); 297 boolean accept_cookies = p.getBoolean("accept_cookies", 298 CookieManager.getInstance().acceptCookie()); 299 CookieManager.getInstance().setAcceptCookie(accept_cookies); 300 openInBackground = p.getBoolean("open_in_background", openInBackground); 301 loginInitialized = p.getBoolean("login_initialized", loginInitialized); 302 textSize = WebSettings.TextSize.valueOf( 303 p.getString(PREF_TEXT_SIZE, textSize.name())); 304 zoomDensity = WebSettings.ZoomDensity.valueOf( 305 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name())); 306 autoFitPage = p.getBoolean("autofit_pages", autoFitPage); 307 loadsPageInOverviewMode = p.getBoolean("load_page", 308 loadsPageInOverviewMode); 309 boolean landscapeOnlyTemp = 310 p.getBoolean("landscape_only", landscapeOnly); 311 if (landscapeOnlyTemp != landscapeOnly) { 312 landscapeOnly = landscapeOnlyTemp; 313 mTabControl.getBrowserActivity().setRequestedOrientation( 314 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE 315 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 316 } 317 useWideViewPort = true; // use wide view port for either setting 318 if (autoFitPage) { 319 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS; 320 } else { 321 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL; 322 } 323 defaultTextEncodingName = 324 p.getString(PREF_DEFAULT_TEXT_ENCODING, 325 defaultTextEncodingName); 326 327 showDebugSettings = 328 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings); 329 // Debug menu items have precidence if the menu is visible 330 if (showDebugSettings) { 331 boolean small_screen = p.getBoolean("small_screen", 332 layoutAlgorithm == 333 WebSettings.LayoutAlgorithm.SINGLE_COLUMN); 334 if (small_screen) { 335 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN; 336 } else { 337 boolean normal_layout = p.getBoolean("normal_layout", 338 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL); 339 if (normal_layout) { 340 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL; 341 } else { 342 layoutAlgorithm = 343 WebSettings.LayoutAlgorithm.NARROW_COLUMNS; 344 } 345 } 346 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort); 347 tracing = p.getBoolean("enable_tracing", tracing); 348 lightTouch = p.getBoolean("enable_light_touch", lightTouch); 349 navDump = p.getBoolean("enable_nav_dump", navDump); 350 userAgent = Integer.parseInt(p.getString("user_agent", "0")); 351 } 352 // JS flags is loaded from DB even if showDebugSettings is false, 353 // so that it can be set once and be effective all the time. 354 jsFlags = p.getString("js_engine_flags", ""); 355 356 // Read the setting for showing/hiding the JS Console always so that should the 357 // user enable debug settings, we already know if we should show the console. 358 // The user will never see the console unless they navigate to about:debug, 359 // regardless of the setting we read here. This setting is only used after debug 360 // is enabled. 361 showConsole = p.getBoolean("javascript_console", showConsole); 362 mTabControl.getBrowserActivity().setShouldShowErrorConsole( 363 showDebugSettings && showConsole); 364 365 // HTML5 API flags 366 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled); 367 databaseEnabled = p.getBoolean("enable_database", databaseEnabled); 368 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled); 369 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled); 370 workersEnabled = p.getBoolean("enable_workers", workersEnabled); 371 372 update(); 373 } 374 375 public String getHomePage() { 376 return homeUrl; 377 } 378 379 public String getJsFlags() { 380 return jsFlags; 381 } 382 383 public WebStorageSizeManager getWebStorageSizeManager() { 384 return webStorageSizeManager; 385 } 386 387 public void setHomePage(Context context, String url) { 388 Editor ed = PreferenceManager. 389 getDefaultSharedPreferences(context).edit(); 390 ed.putString(PREF_HOMEPAGE, url); 391 ed.commit(); 392 homeUrl = url; 393 } 394 395 public boolean isLoginInitialized() { 396 return loginInitialized; 397 } 398 399 public void setLoginInitialized(Context context) { 400 loginInitialized = true; 401 Editor ed = PreferenceManager. 402 getDefaultSharedPreferences(context).edit(); 403 ed.putBoolean("login_initialized", loginInitialized); 404 ed.commit(); 405 } 406 407 public WebSettings.TextSize getTextSize() { 408 return textSize; 409 } 410 411 public WebSettings.ZoomDensity getDefaultZoom() { 412 return zoomDensity; 413 } 414 415 public boolean openInBackground() { 416 return openInBackground; 417 } 418 419 public boolean showSecurityWarnings() { 420 return showSecurityWarnings; 421 } 422 423 public boolean isTracing() { 424 return tracing; 425 } 426 427 public boolean isLightTouch() { 428 return lightTouch; 429 } 430 431 public boolean isNavDump() { 432 return navDump; 433 } 434 435 public boolean showDebugSettings() { 436 return showDebugSettings; 437 } 438 439 public void toggleDebugSettings() { 440 showDebugSettings = !showDebugSettings; 441 navDump = showDebugSettings; 442 update(); 443 } 444 445 /** 446 * Add a WebSettings object to the list of observers that will be updated 447 * when update() is called. 448 * 449 * @param s A WebSettings object that is strictly tied to the life of a 450 * WebView. 451 */ 452 public Observer addObserver(WebSettings s) { 453 Observer old = mWebSettingsToObservers.get(s); 454 if (old != null) { 455 super.deleteObserver(old); 456 } 457 Observer o = new Observer(s); 458 mWebSettingsToObservers.put(s, o); 459 super.addObserver(o); 460 return o; 461 } 462 463 /** 464 * Delete the given WebSettings observer from the list of observers. 465 * @param s The WebSettings object to be deleted. 466 */ 467 public void deleteObserver(WebSettings s) { 468 Observer o = mWebSettingsToObservers.get(s); 469 if (o != null) { 470 mWebSettingsToObservers.remove(s); 471 super.deleteObserver(o); 472 } 473 } 474 475 /* 476 * Package level method for obtaining a single app instance of the 477 * BrowserSettings. 478 */ 479 /*package*/ static BrowserSettings getInstance() { 480 if (sSingleton == null ) { 481 sSingleton = new BrowserSettings(); 482 } 483 return sSingleton; 484 } 485 486 /* 487 * Package level method for associating the BrowserSettings with TabControl 488 */ 489 /* package */void setTabControl(TabControl tabControl) { 490 mTabControl = tabControl; 491 } 492 493 /* 494 * Update all the observers of the object. 495 */ 496 /*package*/ void update() { 497 setChanged(); 498 notifyObservers(); 499 } 500 501 /*package*/ void clearCache(Context context) { 502 WebIconDatabase.getInstance().removeAllIcons(); 503 if (mTabControl != null) { 504 WebView current = mTabControl.getCurrentWebView(); 505 if (current != null) { 506 current.clearCache(true); 507 } 508 } 509 } 510 511 /*package*/ void clearCookies(Context context) { 512 CookieManager.getInstance().removeAllCookie(); 513 } 514 515 /* package */void clearHistory(Context context) { 516 ContentResolver resolver = context.getContentResolver(); 517 Browser.clearHistory(resolver); 518 Browser.clearSearches(resolver); 519 } 520 521 /* package */ void clearFormData(Context context) { 522 WebViewDatabase.getInstance(context).clearFormData(); 523 if (mTabControl != null) { 524 mTabControl.getCurrentTopWebView().clearFormData(); 525 } 526 } 527 528 /*package*/ void clearPasswords(Context context) { 529 WebViewDatabase db = WebViewDatabase.getInstance(context); 530 db.clearUsernamePassword(); 531 db.clearHttpAuthUsernamePassword(); 532 } 533 534 private void maybeDisableWebsiteSettings(Context context) { 535 PreferenceActivity activity = (PreferenceActivity) context; 536 final PreferenceScreen screen = (PreferenceScreen) 537 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS); 538 screen.setEnabled(false); 539 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() { 540 public void onReceiveValue(Map webStorageOrigins) { 541 if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) { 542 screen.setEnabled(true); 543 } 544 } 545 }); 546 547 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() { 548 public void onReceiveValue(Set<String> geolocationOrigins) { 549 if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) { 550 screen.setEnabled(true); 551 } 552 } 553 }); 554 } 555 556 /*package*/ void clearDatabases(Context context) { 557 WebStorage.getInstance().deleteAllData(); 558 maybeDisableWebsiteSettings(context); 559 } 560 561 /*package*/ void clearLocationAccess(Context context) { 562 GeolocationPermissions.getInstance().clearAll(); 563 maybeDisableWebsiteSettings(context); 564 } 565 566 /*package*/ void resetDefaultPreferences(Context ctx) { 567 SharedPreferences p = 568 PreferenceManager.getDefaultSharedPreferences(ctx); 569 p.edit().clear().commit(); 570 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences, 571 true); 572 // reset homeUrl 573 setHomePage(ctx, getFactoryResetHomeUrl(ctx)); 574 // reset appcache max size 575 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize(); 576 } 577 578 private String getFactoryResetHomeUrl(Context context) { 579 String url = context.getResources().getString(R.string.homepage_base); 580 if (url.indexOf("{CID}") != -1) { 581 url = url.replace("{CID}", 582 BrowserProvider.getClientId(context.getContentResolver())); 583 } 584 return url; 585 } 586 587 // Private constructor that does nothing. 588 private BrowserSettings() { 589 } 590} 591