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