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 com.android.browser.search.SearchEngine;
21 import com.android.browser.search.SearchEngines;
23 import android.app.ActivityManager;
24 import android.content.ComponentName;
25 import android.content.ContentResolver;
26 import android.content.Context;
27 import android.content.pm.ActivityInfo;
28 import android.content.SharedPreferences;
29 import android.content.SharedPreferences.Editor;
30 import android.database.ContentObserver;
31 import android.os.Handler;
32 import android.preference.PreferenceActivity;
33 import android.preference.PreferenceScreen;
34 import android.provider.Settings;
35 import android.util.Log;
36 import android.webkit.CookieManager;
37 import android.webkit.GeolocationPermissions;
38 import android.webkit.ValueCallback;
39 import android.webkit.WebView;
40 import android.webkit.WebViewDatabase;
41 import android.webkit.WebIconDatabase;
42 import android.webkit.WebSettings;
43 import android.webkit.WebStorage;
44 import android.preference.PreferenceManager;
45 import android.provider.Browser;
47 import java.util.HashMap;
50 import java.util.Observable;
53 * Package level class for storing various WebView and Browser settings. To use
55 * BrowserSettings s = BrowserSettings.getInstance();
56 * s.addObserver(webView.getSettings());
57 * s.loadFromDb(context); // Only needed on app startup
58 * s.javaScriptEnabled = true;
59 * ... // set any other settings
60 * s.update(); // this will update all the observers
62 * To remove an observer:
63 * s.deleteObserver(webView.getSettings());
65 class BrowserSettings extends Observable {
67 // Private variables for settings
68 // NOTE: these defaults need to be kept in sync with the XML
69 // until the performance of PreferenceManager.setDefaultValues()
71 // Note: boolean variables are set inside reset function.
72 private boolean loadsImagesAutomatically;
73 private boolean javaScriptEnabled;
74 private WebSettings.PluginState pluginState;
75 private boolean javaScriptCanOpenWindowsAutomatically;
76 private boolean showSecurityWarnings;
77 private boolean rememberPasswords;
78 private boolean saveFormData;
79 private boolean openInBackground;
80 private String defaultTextEncodingName;
81 private String homeUrl = "";
82 private SearchEngine searchEngine;
83 private boolean autoFitPage;
84 private boolean landscapeOnly;
85 private boolean loadsPageInOverviewMode;
86 private boolean showDebugSettings;
88 private boolean appCacheEnabled;
89 private boolean databaseEnabled;
90 private boolean domStorageEnabled;
91 private boolean geolocationEnabled;
92 private boolean workersEnabled; // only affects V8. JSC does not have a similar setting
93 // HTML5 API configuration params
94 private long appCacheMaxSize = Long.MAX_VALUE;
95 private String appCachePath; // default value set in loadFromDb().
96 private String databasePath; // default value set in loadFromDb()
97 private String geolocationDatabasePath; // default value set in loadFromDb()
98 private WebStorageSizeManager webStorageSizeManager;
100 private String jsFlags = "";
102 private final static String TAG = "BrowserSettings";
104 // Development settings
105 public WebSettings.LayoutAlgorithm layoutAlgorithm =
106 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
107 private boolean useWideViewPort = true;
108 private int userAgent = 0;
109 private boolean tracing = false;
110 private boolean lightTouch = false;
111 private boolean navDump = false;
113 // By default the error console is shown once the user navigates to about:debug.
114 // The setting can be then toggled from the settings menu.
115 private boolean showConsole = true;
117 // Private preconfigured values
118 private static int minimumFontSize = 8;
119 private static int minimumLogicalFontSize = 8;
120 private static int defaultFontSize = 16;
121 private static int defaultFixedFontSize = 13;
122 private static WebSettings.TextSize textSize =
123 WebSettings.TextSize.NORMAL;
124 private static WebSettings.ZoomDensity zoomDensity =
125 WebSettings.ZoomDensity.MEDIUM;
126 private static int pageCacheCapacity;
128 // Preference keys that are used outside this class
129 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
130 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
131 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
132 public final static String PREF_HOMEPAGE = "homepage";
133 public final static String PREF_SEARCH_ENGINE = "search_engine";
134 public final static String PREF_CLEAR_FORM_DATA =
135 "privacy_clear_form_data";
136 public final static String PREF_CLEAR_PASSWORDS =
137 "privacy_clear_passwords";
138 public final static String PREF_EXTRAS_RESET_DEFAULTS =
139 "reset_default_preferences";
140 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
141 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
142 public final static String PREF_TEXT_SIZE = "text_size";
143 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
144 public final static String PREF_DEFAULT_TEXT_ENCODING =
145 "default_text_encoding";
146 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
147 "privacy_clear_geolocation_access";
149 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
150 "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
151 "like Gecko) Version/5.0 Safari/533.16";
153 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
154 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
155 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
157 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
158 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
159 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
161 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
162 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
163 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
165 // Value to truncate strings when adding them to a TextView within
167 public final static int MAX_TEXTVIEW_LEN = 80;
169 private TabControl mTabControl;
171 // Single instance of the BrowserSettings for use in the Browser app.
172 private static BrowserSettings sSingleton;
174 // Private map of WebSettings to Observer objects used when deleting an
176 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
177 new HashMap<WebSettings,Observer>();
180 * An observer wrapper for updating a WebSettings object with the new
181 * settings after a call to BrowserSettings.update().
183 static class Observer implements java.util.Observer {
184 // Private WebSettings object that will be updated.
185 private WebSettings mSettings;
187 Observer(WebSettings w) {
191 public void update(Observable o, Object arg) {
192 BrowserSettings b = (BrowserSettings)o;
193 WebSettings s = mSettings;
195 s.setLayoutAlgorithm(b.layoutAlgorithm);
196 if (b.userAgent == 0) {
197 // use the default ua string
198 s.setUserAgentString(null);
199 } else if (b.userAgent == 1) {
200 s.setUserAgentString(DESKTOP_USERAGENT);
201 } else if (b.userAgent == 2) {
202 s.setUserAgentString(IPHONE_USERAGENT);
203 } else if (b.userAgent == 3) {
204 s.setUserAgentString(IPAD_USERAGENT);
205 } else if (b.userAgent == 4) {
206 s.setUserAgentString(FROYO_USERAGENT);
208 s.setUseWideViewPort(b.useWideViewPort);
209 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
210 s.setJavaScriptEnabled(b.javaScriptEnabled);
211 s.setPluginState(b.pluginState);
212 s.setJavaScriptCanOpenWindowsAutomatically(
213 b.javaScriptCanOpenWindowsAutomatically);
214 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
215 s.setMinimumFontSize(b.minimumFontSize);
216 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
217 s.setDefaultFontSize(b.defaultFontSize);
218 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
219 s.setNavDump(b.navDump);
220 s.setTextSize(b.textSize);
221 s.setDefaultZoom(b.zoomDensity);
222 s.setLightTouchEnabled(b.lightTouch);
223 s.setSaveFormData(b.saveFormData);
224 s.setSavePassword(b.rememberPasswords);
225 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
226 s.setPageCacheCapacity(pageCacheCapacity);
228 // WebView inside Browser doesn't want initial focus to be set.
229 s.setNeedInitialFocus(false);
230 // Browser supports multiple windows
231 s.setSupportMultipleWindows(true);
232 // disable content url access
233 s.setAllowContentAccess(false);
236 s.setAppCacheEnabled(b.appCacheEnabled);
237 s.setDatabaseEnabled(b.databaseEnabled);
238 s.setDomStorageEnabled(b.domStorageEnabled);
239 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
240 s.setGeolocationEnabled(b.geolocationEnabled);
242 // HTML5 configuration parameters.
243 s.setAppCacheMaxSize(b.appCacheMaxSize);
244 s.setAppCachePath(b.appCachePath);
245 s.setDatabasePath(b.databasePath);
246 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
248 b.updateTabControlSettings();
253 * Load settings from the browser app's database.
254 * NOTE: Strings used for the preferences must match those specified
255 * in the browser_preferences.xml
256 * @param ctx A Context object used to query the browser's settings
257 * database. If the database exists, the saved settings will be
258 * stored in this BrowserSettings object. This will update all
259 * observers of this object.
261 public void loadFromDb(final Context ctx) {
262 SharedPreferences p =
263 PreferenceManager.getDefaultSharedPreferences(ctx);
264 // Set the default value for the Application Caches path.
265 appCachePath = ctx.getDir("appcache", 0).getPath();
266 // Determine the maximum size of the application cache.
267 webStorageSizeManager = new WebStorageSizeManager(
269 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
270 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
271 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
272 // Set the default value for the Database path.
273 databasePath = ctx.getDir("databases", 0).getPath();
274 // Set the default value for the Geolocation database path.
275 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
277 if (p.getString(PREF_HOMEPAGE, "") == "") {
278 // No home page preferences is set, set it to default.
279 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
282 // the cost of one cached page is ~3M (measured using nytimes.com). For
283 // low end devices, we only cache one page. For high end devices, we try
284 // to cache more pages, currently choose 5.
285 ActivityManager am = (ActivityManager) ctx
286 .getSystemService(Context.ACTIVITY_SERVICE);
287 if (am.getMemoryClass() > 16) {
288 pageCacheCapacity = 5;
290 pageCacheCapacity = 1;
293 // Load the defaults from the xml
294 // This call is TOO SLOW, need to manually keep the defaults
296 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
297 syncSharedPreferences(ctx, p);
300 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
303 p.getString(PREF_HOMEPAGE, homeUrl);
304 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
305 SearchEngine.GOOGLE);
306 if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
307 if (searchEngine != null) {
308 if (searchEngine.supportsVoiceSearch()) {
309 // One or more tabs could have been in voice search mode.
310 // Clear it, since the new SearchEngine may not support
311 // it, or may handle it differently.
312 for (int i = 0; i < mTabControl.getTabCount(); i++) {
313 mTabControl.getTab(i).revertVoiceSearchMode();
316 searchEngine.close();
318 searchEngine = SearchEngines.get(ctx, searchEngineName);
320 Log.i(TAG, "Selected search engine: " + searchEngine);
322 loadsImagesAutomatically = p.getBoolean("load_images",
323 loadsImagesAutomatically);
324 javaScriptEnabled = p.getBoolean("enable_javascript",
326 pluginState = WebSettings.PluginState.valueOf(
327 p.getString("plugin_state", pluginState.name()));
328 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
329 "block_popup_windows",
330 !javaScriptCanOpenWindowsAutomatically);
331 showSecurityWarnings = p.getBoolean("show_security_warnings",
332 showSecurityWarnings);
333 rememberPasswords = p.getBoolean("remember_passwords",
335 saveFormData = p.getBoolean("save_formdata",
337 boolean accept_cookies = p.getBoolean("accept_cookies",
338 CookieManager.getInstance().acceptCookie());
339 CookieManager.getInstance().setAcceptCookie(accept_cookies);
340 openInBackground = p.getBoolean("open_in_background", openInBackground);
341 textSize = WebSettings.TextSize.valueOf(
342 p.getString(PREF_TEXT_SIZE, textSize.name()));
343 zoomDensity = WebSettings.ZoomDensity.valueOf(
344 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
345 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
346 loadsPageInOverviewMode = p.getBoolean("load_page",
347 loadsPageInOverviewMode);
348 boolean landscapeOnlyTemp =
349 p.getBoolean("landscape_only", landscapeOnly);
350 if (landscapeOnlyTemp != landscapeOnly) {
351 landscapeOnly = landscapeOnlyTemp;
353 useWideViewPort = true; // use wide view port for either setting
355 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
357 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
359 defaultTextEncodingName =
360 p.getString(PREF_DEFAULT_TEXT_ENCODING,
361 defaultTextEncodingName);
364 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
365 // Debug menu items have precidence if the menu is visible
366 if (showDebugSettings) {
367 boolean small_screen = p.getBoolean("small_screen",
369 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
371 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
373 boolean normal_layout = p.getBoolean("normal_layout",
374 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
376 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
379 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
382 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
383 tracing = p.getBoolean("enable_tracing", tracing);
384 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
385 navDump = p.getBoolean("enable_nav_dump", navDump);
386 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
388 // JS flags is loaded from DB even if showDebugSettings is false,
389 // so that it can be set once and be effective all the time.
390 jsFlags = p.getString("js_engine_flags", "");
392 // Read the setting for showing/hiding the JS Console always so that should the
393 // user enable debug settings, we already know if we should show the console.
394 // The user will never see the console unless they navigate to about:debug,
395 // regardless of the setting we read here. This setting is only used after debug
397 showConsole = p.getBoolean("javascript_console", showConsole);
400 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
401 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
402 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
403 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
404 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
409 public String getHomePage() {
413 public SearchEngine getSearchEngine() {
417 public String getJsFlags() {
421 public WebStorageSizeManager getWebStorageSizeManager() {
422 return webStorageSizeManager;
425 public void setHomePage(Context context, String url) {
426 Editor ed = PreferenceManager.
427 getDefaultSharedPreferences(context).edit();
428 ed.putString(PREF_HOMEPAGE, url);
433 public WebSettings.TextSize getTextSize() {
437 public WebSettings.ZoomDensity getDefaultZoom() {
441 public boolean openInBackground() {
442 return openInBackground;
445 public boolean showSecurityWarnings() {
446 return showSecurityWarnings;
449 public boolean isTracing() {
453 public boolean isLightTouch() {
457 public boolean isNavDump() {
461 public boolean showDebugSettings() {
462 return showDebugSettings;
465 public void toggleDebugSettings() {
466 showDebugSettings = !showDebugSettings;
467 navDump = showDebugSettings;
472 * Add a WebSettings object to the list of observers that will be updated
473 * when update() is called.
475 * @param s A WebSettings object that is strictly tied to the life of a
478 public Observer addObserver(WebSettings s) {
479 Observer old = mWebSettingsToObservers.get(s);
481 super.deleteObserver(old);
483 Observer o = new Observer(s);
484 mWebSettingsToObservers.put(s, o);
485 super.addObserver(o);
490 * Delete the given WebSettings observer from the list of observers.
491 * @param s The WebSettings object to be deleted.
493 public void deleteObserver(WebSettings s) {
494 Observer o = mWebSettingsToObservers.get(s);
496 mWebSettingsToObservers.remove(s);
497 super.deleteObserver(o);
502 * Package level method for obtaining a single app instance of the
505 /*package*/ static BrowserSettings getInstance() {
506 if (sSingleton == null ) {
507 sSingleton = new BrowserSettings();
513 * Package level method for associating the BrowserSettings with TabControl
515 /* package */void setTabControl(TabControl tabControl) {
516 mTabControl = tabControl;
517 updateTabControlSettings();
521 * Update all the observers of the object.
523 /*package*/ void update() {
528 /*package*/ void clearCache(Context context) {
529 WebIconDatabase.getInstance().removeAllIcons();
530 if (mTabControl != null) {
531 WebView current = mTabControl.getCurrentWebView();
532 if (current != null) {
533 current.clearCache(true);
538 /*package*/ void clearCookies(Context context) {
539 CookieManager.getInstance().removeAllCookie();
542 /* package */void clearHistory(Context context) {
543 ContentResolver resolver = context.getContentResolver();
544 Browser.clearHistory(resolver);
545 Browser.clearSearches(resolver);
548 /* package */ void clearFormData(Context context) {
549 WebViewDatabase.getInstance(context).clearFormData();
550 if (mTabControl != null) {
551 WebView currentTopView = mTabControl.getCurrentTopWebView();
552 if (currentTopView != null) {
553 currentTopView.clearFormData();
558 /*package*/ void clearPasswords(Context context) {
559 WebViewDatabase db = WebViewDatabase.getInstance(context);
560 db.clearUsernamePassword();
561 db.clearHttpAuthUsernamePassword();
564 private void updateTabControlSettings() {
565 // Enable/disable the error console.
566 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
567 showDebugSettings && showConsole);
568 mTabControl.getBrowserActivity().setRequestedOrientation(
569 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
570 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
573 private void maybeDisableWebsiteSettings(Context context) {
574 PreferenceActivity activity = (PreferenceActivity) context;
575 final PreferenceScreen screen = (PreferenceScreen)
576 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
577 screen.setEnabled(false);
578 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
579 public void onReceiveValue(Map webStorageOrigins) {
580 if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) {
581 screen.setEnabled(true);
586 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
587 public void onReceiveValue(Set<String> geolocationOrigins) {
588 if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) {
589 screen.setEnabled(true);
595 /*package*/ void clearDatabases(Context context) {
596 WebStorage.getInstance().deleteAllData();
597 maybeDisableWebsiteSettings(context);
600 /*package*/ void clearLocationAccess(Context context) {
601 GeolocationPermissions.getInstance().clearAll();
602 maybeDisableWebsiteSettings(context);
605 /*package*/ void resetDefaultPreferences(Context ctx) {
607 SharedPreferences p =
608 PreferenceManager.getDefaultSharedPreferences(ctx);
609 p.edit().clear().apply();
610 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
613 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
614 // reset appcache max size
615 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
618 private String getFactoryResetHomeUrl(Context context) {
619 String url = context.getResources().getString(R.string.homepage_base);
620 if (url.indexOf("{CID}") != -1) {
621 url = url.replace("{CID}",
622 BrowserProvider.getClientId(context.getContentResolver()));
627 // Private constructor that does nothing.
628 private BrowserSettings() {
632 private void reset() {
633 // Private variables for settings
634 // NOTE: these defaults need to be kept in sync with the XML
635 // until the performance of PreferenceManager.setDefaultValues()
637 loadsImagesAutomatically = true;
638 javaScriptEnabled = true;
639 pluginState = WebSettings.PluginState.ON;
640 javaScriptCanOpenWindowsAutomatically = false;
641 showSecurityWarnings = true;
642 rememberPasswords = true;
644 openInBackground = false;
646 landscapeOnly = false;
647 loadsPageInOverviewMode = true;
648 showDebugSettings = false;
650 appCacheEnabled = true;
651 databaseEnabled = true;
652 domStorageEnabled = true;
653 geolocationEnabled = true;
654 workersEnabled = true; // only affects V8. JSC does not have a similar setting