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