3 * Copyright (C) 2007 The Android Open Source Project
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package com.android.browser;
20 import android.app.ActivityManager;
21 import android.content.ContentResolver;
22 import android.content.Context;
23 import android.content.pm.ActivityInfo;
24 import android.content.SharedPreferences;
25 import android.content.SharedPreferences.Editor;
26 import android.preference.PreferenceActivity;
27 import android.preference.PreferenceScreen;
28 import android.webkit.CookieManager;
29 import android.webkit.GeolocationPermissions;
30 import android.webkit.ValueCallback;
31 import android.webkit.WebView;
32 import android.webkit.WebViewDatabase;
33 import android.webkit.WebIconDatabase;
34 import android.webkit.WebSettings;
35 import android.webkit.WebStorage;
36 import android.preference.PreferenceManager;
37 import android.provider.Browser;
39 import java.util.HashMap;
42 import java.util.Observable;
45 * Package level class for storing various WebView and Browser settings. To use
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
54 * To remove an observer:
55 * s.deleteObserver(webView.getSettings());
57 class BrowserSettings extends Observable {
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()
63 // Note: boolean variables are set inside reset function.
64 private boolean loadsImagesAutomatically;
65 private boolean javaScriptEnabled;
66 private boolean pluginsEnabled;
67 private boolean javaScriptCanOpenWindowsAutomatically;
68 private boolean showSecurityWarnings;
69 private boolean rememberPasswords;
70 private boolean saveFormData;
71 private boolean openInBackground;
72 private String defaultTextEncodingName;
73 private String homeUrl = "";
74 private boolean autoFitPage;
75 private boolean landscapeOnly;
76 private boolean loadsPageInOverviewMode;
77 private boolean showDebugSettings;
79 private boolean appCacheEnabled;
80 private boolean databaseEnabled;
81 private boolean domStorageEnabled;
82 private boolean geolocationEnabled;
83 private boolean workersEnabled; // 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;
91 private String jsFlags = "";
93 private final static String TAG = "BrowserSettings";
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;
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;
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;
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";
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";
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";
147 // Value to truncate strings when adding them to a TextView within
149 public final static int MAX_TEXTVIEW_LEN = 80;
151 private TabControl mTabControl;
153 // Single instance of the BrowserSettings for use in the Browser app.
154 private static BrowserSettings sSingleton;
156 // Private map of WebSettings to Observer objects used when deleting an
158 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
159 new HashMap<WebSettings,Observer>();
162 * An observer wrapper for updating a WebSettings object with the new
163 * settings after a call to BrowserSettings.update().
165 static class Observer implements java.util.Observer {
166 // Private WebSettings object that will be updated.
167 private WebSettings mSettings;
169 Observer(WebSettings w) {
173 public void update(Observable o, Object arg) {
174 BrowserSettings b = (BrowserSettings)o;
175 WebSettings s = mSettings;
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);
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);
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 internal pattern for over scroll background
211 s.setUseWebViewBackgroundForOverscrollBackground(false);
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);
220 // HTML5 configuration parameters.
221 s.setAppCacheMaxSize(b.appCacheMaxSize);
222 s.setAppCachePath(b.appCachePath);
223 s.setDatabasePath(b.databasePath);
224 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
226 b.updateTabControlSettings();
231 * Load settings from the browser app's database.
232 * NOTE: Strings used for the preferences must match those specified
233 * in the browser_preferences.xml
234 * @param ctx A Context object used to query the browser's settings
235 * database. If the database exists, the saved settings will be
236 * stored in this BrowserSettings object. This will update all
237 * observers of this object.
239 public void loadFromDb(Context ctx) {
240 SharedPreferences p =
241 PreferenceManager.getDefaultSharedPreferences(ctx);
242 // Set the default value for the Application Caches path.
243 appCachePath = ctx.getDir("appcache", 0).getPath();
244 // Determine the maximum size of the application cache.
245 webStorageSizeManager = new WebStorageSizeManager(
247 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
248 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
249 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
250 // Set the default value for the Database path.
251 databasePath = ctx.getDir("databases", 0).getPath();
252 // Set the default value for the Geolocation database path.
253 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
255 homeUrl = getFactoryResetHomeUrl(ctx);
257 // the cost of one cached page is ~3M (measured using nytimes.com). For
258 // low end devices, we only cache one page. For high end devices, we try
259 // to cache more pages, currently choose 5.
261 // Note: the page cache is disabled on high-end devices due to an
262 // incompatibility with V8. TODO: re-enable the page cache once the
263 // problem with V8 is solved.
264 ActivityManager am = (ActivityManager) ctx
265 .getSystemService(Context.ACTIVITY_SERVICE);
266 if (am.getMemoryClass() > 16) {
267 pageCacheCapacity = 0;
269 pageCacheCapacity = 1;
272 // Load the defaults from the xml
273 // This call is TOO SLOW, need to manually keep the defaults
275 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
276 syncSharedPreferences(p);
279 /* package */ void syncSharedPreferences(SharedPreferences p) {
282 p.getString(PREF_HOMEPAGE, homeUrl);
284 loadsImagesAutomatically = p.getBoolean("load_images",
285 loadsImagesAutomatically);
286 javaScriptEnabled = p.getBoolean("enable_javascript",
288 pluginsEnabled = p.getBoolean("enable_plugins",
290 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
291 "block_popup_windows",
292 !javaScriptCanOpenWindowsAutomatically);
293 showSecurityWarnings = p.getBoolean("show_security_warnings",
294 showSecurityWarnings);
295 rememberPasswords = p.getBoolean("remember_passwords",
297 saveFormData = p.getBoolean("save_formdata",
299 boolean accept_cookies = p.getBoolean("accept_cookies",
300 CookieManager.getInstance().acceptCookie());
301 CookieManager.getInstance().setAcceptCookie(accept_cookies);
302 openInBackground = p.getBoolean("open_in_background", openInBackground);
303 textSize = WebSettings.TextSize.valueOf(
304 p.getString(PREF_TEXT_SIZE, textSize.name()));
305 zoomDensity = WebSettings.ZoomDensity.valueOf(
306 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
307 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
308 loadsPageInOverviewMode = p.getBoolean("load_page",
309 loadsPageInOverviewMode);
310 boolean landscapeOnlyTemp =
311 p.getBoolean("landscape_only", landscapeOnly);
312 if (landscapeOnlyTemp != landscapeOnly) {
313 landscapeOnly = landscapeOnlyTemp;
315 useWideViewPort = true; // use wide view port for either setting
317 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
319 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
321 defaultTextEncodingName =
322 p.getString(PREF_DEFAULT_TEXT_ENCODING,
323 defaultTextEncodingName);
326 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
327 // Debug menu items have precidence if the menu is visible
328 if (showDebugSettings) {
329 boolean small_screen = p.getBoolean("small_screen",
331 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
333 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
335 boolean normal_layout = p.getBoolean("normal_layout",
336 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
338 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
341 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
344 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
345 tracing = p.getBoolean("enable_tracing", tracing);
346 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
347 navDump = p.getBoolean("enable_nav_dump", navDump);
348 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
350 // JS flags is loaded from DB even if showDebugSettings is false,
351 // so that it can be set once and be effective all the time.
352 jsFlags = p.getString("js_engine_flags", "");
354 // Read the setting for showing/hiding the JS Console always so that should the
355 // user enable debug settings, we already know if we should show the console.
356 // The user will never see the console unless they navigate to about:debug,
357 // regardless of the setting we read here. This setting is only used after debug
359 showConsole = p.getBoolean("javascript_console", showConsole);
362 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
363 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
364 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
365 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
366 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
371 public String getHomePage() {
375 public String getJsFlags() {
379 public WebStorageSizeManager getWebStorageSizeManager() {
380 return webStorageSizeManager;
383 public void setHomePage(Context context, String url) {
384 Editor ed = PreferenceManager.
385 getDefaultSharedPreferences(context).edit();
386 ed.putString(PREF_HOMEPAGE, url);
391 public WebSettings.TextSize getTextSize() {
395 public WebSettings.ZoomDensity getDefaultZoom() {
399 public boolean openInBackground() {
400 return openInBackground;
403 public boolean showSecurityWarnings() {
404 return showSecurityWarnings;
407 public boolean isTracing() {
411 public boolean isLightTouch() {
415 public boolean isNavDump() {
419 public boolean showDebugSettings() {
420 return showDebugSettings;
423 public void toggleDebugSettings() {
424 showDebugSettings = !showDebugSettings;
425 navDump = showDebugSettings;
430 * Add a WebSettings object to the list of observers that will be updated
431 * when update() is called.
433 * @param s A WebSettings object that is strictly tied to the life of a
436 public Observer addObserver(WebSettings s) {
437 Observer old = mWebSettingsToObservers.get(s);
439 super.deleteObserver(old);
441 Observer o = new Observer(s);
442 mWebSettingsToObservers.put(s, o);
443 super.addObserver(o);
448 * Delete the given WebSettings observer from the list of observers.
449 * @param s The WebSettings object to be deleted.
451 public void deleteObserver(WebSettings s) {
452 Observer o = mWebSettingsToObservers.get(s);
454 mWebSettingsToObservers.remove(s);
455 super.deleteObserver(o);
460 * Package level method for obtaining a single app instance of the
463 /*package*/ static BrowserSettings getInstance() {
464 if (sSingleton == null ) {
465 sSingleton = new BrowserSettings();
471 * Package level method for associating the BrowserSettings with TabControl
473 /* package */void setTabControl(TabControl tabControl) {
474 mTabControl = tabControl;
475 updateTabControlSettings();
479 * Update all the observers of the object.
481 /*package*/ void update() {
486 /*package*/ void clearCache(Context context) {
487 WebIconDatabase.getInstance().removeAllIcons();
488 if (mTabControl != null) {
489 WebView current = mTabControl.getCurrentWebView();
490 if (current != null) {
491 current.clearCache(true);
496 /*package*/ void clearCookies(Context context) {
497 CookieManager.getInstance().removeAllCookie();
500 /* package */void clearHistory(Context context) {
501 ContentResolver resolver = context.getContentResolver();
502 Browser.clearHistory(resolver);
503 Browser.clearSearches(resolver);
506 /* package */ void clearFormData(Context context) {
507 WebViewDatabase.getInstance(context).clearFormData();
508 if (mTabControl != null) {
509 mTabControl.getCurrentTopWebView().clearFormData();
513 /*package*/ void clearPasswords(Context context) {
514 WebViewDatabase db = WebViewDatabase.getInstance(context);
515 db.clearUsernamePassword();
516 db.clearHttpAuthUsernamePassword();
519 private void updateTabControlSettings() {
520 // Enable/disable the error console.
521 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
522 showDebugSettings && showConsole);
523 mTabControl.getBrowserActivity().setRequestedOrientation(
524 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
525 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
528 private void maybeDisableWebsiteSettings(Context context) {
529 PreferenceActivity activity = (PreferenceActivity) context;
530 final PreferenceScreen screen = (PreferenceScreen)
531 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
532 screen.setEnabled(false);
533 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
534 public void onReceiveValue(Map webStorageOrigins) {
535 if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) {
536 screen.setEnabled(true);
541 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
542 public void onReceiveValue(Set<String> geolocationOrigins) {
543 if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) {
544 screen.setEnabled(true);
550 /*package*/ void clearDatabases(Context context) {
551 WebStorage.getInstance().deleteAllData();
552 maybeDisableWebsiteSettings(context);
555 /*package*/ void clearLocationAccess(Context context) {
556 GeolocationPermissions.getInstance().clearAll();
557 maybeDisableWebsiteSettings(context);
560 /*package*/ void resetDefaultPreferences(Context ctx) {
562 SharedPreferences p =
563 PreferenceManager.getDefaultSharedPreferences(ctx);
564 p.edit().clear().commit();
565 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
568 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
569 // reset appcache max size
570 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
573 private String getFactoryResetHomeUrl(Context context) {
574 String url = context.getResources().getString(R.string.homepage_base);
575 if (url.indexOf("{CID}") != -1) {
576 url = url.replace("{CID}",
577 BrowserProvider.getClientId(context.getContentResolver()));
582 // Private constructor that does nothing.
583 private BrowserSettings() {
587 private void reset() {
588 // Private variables for settings
589 // NOTE: these defaults need to be kept in sync with the XML
590 // until the performance of PreferenceManager.setDefaultValues()
592 loadsImagesAutomatically = true;
593 javaScriptEnabled = true;
594 pluginsEnabled = true;
595 javaScriptCanOpenWindowsAutomatically = false;
596 showSecurityWarnings = true;
597 rememberPasswords = true;
599 openInBackground = false;
601 landscapeOnly = false;
602 loadsPageInOverviewMode = true;
603 showDebugSettings = false;
605 appCacheEnabled = true;
606 databaseEnabled = true;
607 domStorageEnabled = true;
608 geolocationEnabled = true;
609 workersEnabled = true; // only affects V8. JSC does not have a similar setting