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