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