blob: cb4918b5022f263f71d321504fbfdceccba97ee0 [file] [log] [blame]
Cary Clarkf2407c62009-09-04 12:25:10 -04001
The Android Open Source Project0c908882009-03-03 19:32:16 -08002/*
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.android.browser;
19
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010020import com.android.browser.search.SearchEngine;
21import com.android.browser.search.SearchEngines;
22
Grace Kloba9804c432009-12-02 11:07:40 -080023import android.app.ActivityManager;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010024import android.content.ComponentName;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.content.ContentResolver;
26import android.content.Context;
27import android.content.pm.ActivityInfo;
28import android.content.SharedPreferences;
29import android.content.SharedPreferences.Editor;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010030import android.database.ContentObserver;
Jeff Davidson43610292010-07-16 16:03:58 -070031import android.net.Uri;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010032import android.os.Handler;
Nicolas Roard78a98e42009-05-11 13:34:17 +010033import android.preference.PreferenceActivity;
34import android.preference.PreferenceScreen;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010035import android.provider.Settings;
36import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010038import android.webkit.GeolocationPermissions;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010039import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080040import android.webkit.WebView;
41import android.webkit.WebViewDatabase;
42import android.webkit.WebIconDatabase;
43import android.webkit.WebSettings;
Nicolas Roard78a98e42009-05-11 13:34:17 +010044import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.preference.PreferenceManager;
46import android.provider.Browser;
47
48import java.util.HashMap;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010049import java.util.Map;
50import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051import java.util.Observable;
52
53/*
54 * Package level class for storing various WebView and Browser settings. To use
55 * this class:
56 * BrowserSettings s = BrowserSettings.getInstance();
57 * s.addObserver(webView.getSettings());
58 * s.loadFromDb(context); // Only needed on app startup
59 * s.javaScriptEnabled = true;
60 * ... // set any other settings
61 * s.update(); // this will update all the observers
62 *
63 * To remove an observer:
64 * s.deleteObserver(webView.getSettings());
65 */
66class BrowserSettings extends Observable {
67
Leon Scroggins9ac587d2009-04-20 12:53:46 -040068 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080069 // NOTE: these defaults need to be kept in sync with the XML
70 // until the performance of PreferenceManager.setDefaultValues()
71 // is improved.
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080072 // Note: boolean variables are set inside reset function.
73 private boolean loadsImagesAutomatically;
74 private boolean javaScriptEnabled;
Patrick Scotte536b332010-03-22 10:19:32 -040075 private WebSettings.PluginState pluginState;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080076 private boolean javaScriptCanOpenWindowsAutomatically;
77 private boolean showSecurityWarnings;
78 private boolean rememberPasswords;
79 private boolean saveFormData;
80 private boolean openInBackground;
The Android Open Source Project0c908882009-03-03 19:32:16 -080081 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070082 private String homeUrl = "";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010083 private SearchEngine searchEngine;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080084 private boolean autoFitPage;
85 private boolean landscapeOnly;
86 private boolean loadsPageInOverviewMode;
87 private boolean showDebugSettings;
Andrei Popescu01068702009-08-03 16:03:24 +010088 // HTML5 API flags
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080089 private boolean appCacheEnabled;
90 private boolean databaseEnabled;
91 private boolean domStorageEnabled;
92 private boolean geolocationEnabled;
93 private boolean workersEnabled; // only affects V8. JSC does not have a similar setting
Andrei Popescu01068702009-08-03 16:03:24 +010094 // HTML5 API configuration params
95 private long appCacheMaxSize = Long.MAX_VALUE;
96 private String appCachePath; // default value set in loadFromDb().
97 private String databasePath; // default value set in loadFromDb()
Steve Block5029cf02009-08-21 13:16:58 +010098 private String geolocationDatabasePath; // default value set in loadFromDb()
Andrei Popescu01068702009-08-03 16:03:24 +010099 private WebStorageSizeManager webStorageSizeManager;
100
101 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800102
Nicolas Roard78a98e42009-05-11 13:34:17 +0100103 private final static String TAG = "BrowserSettings";
104
The Android Open Source Project0c908882009-03-03 19:32:16 -0800105 // Development settings
106 public WebSettings.LayoutAlgorithm layoutAlgorithm =
107 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
108 private boolean useWideViewPort = true;
109 private int userAgent = 0;
110 private boolean tracing = false;
111 private boolean lightTouch = false;
112 private boolean navDump = false;
Feng Qianb3c02da2009-06-29 15:58:08 -0700113
Ben Murdochbff2d602009-07-01 20:19:05 +0100114 // By default the error console is shown once the user navigates to about:debug.
115 // The setting can be then toggled from the settings menu.
116 private boolean showConsole = true;
117
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118 // Private preconfigured values
Shimeng (Simon) Wangb4fb25c2010-07-13 15:18:10 -0700119 private static int minimumFontSize = 1;
120 private static int minimumLogicalFontSize = 1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121 private static int defaultFontSize = 16;
122 private static int defaultFixedFontSize = 13;
123 private static WebSettings.TextSize textSize =
124 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700125 private static WebSettings.ZoomDensity zoomDensity =
126 WebSettings.ZoomDensity.MEDIUM;
Grace Kloba9804c432009-12-02 11:07:40 -0800127 private static int pageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800128
129 // Preference keys that are used outside this class
130 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
131 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
132 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
133 public final static String PREF_HOMEPAGE = "homepage";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100134 public final static String PREF_SEARCH_ENGINE = "search_engine";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800135 public final static String PREF_CLEAR_FORM_DATA =
136 "privacy_clear_form_data";
137 public final static String PREF_CLEAR_PASSWORDS =
138 "privacy_clear_passwords";
139 public final static String PREF_EXTRAS_RESET_DEFAULTS =
140 "reset_default_preferences";
141 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100142 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800143 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700144 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800145 public final static String PREF_DEFAULT_TEXT_ENCODING =
146 "default_text_encoding";
Steve Blockc6f577f2009-08-20 18:11:08 +0100147 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
148 "privacy_clear_geolocation_access";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800149
150 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700151 "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
152 "like Gecko) Version/5.0 Safari/533.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800153
154 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700155 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
156 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
157
158 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
159 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
160 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
161
162 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
163 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
164 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800165
166 // Value to truncate strings when adding them to a TextView within
167 // a ListView
168 public final static int MAX_TEXTVIEW_LEN = 80;
169
Jeff Davidson43610292010-07-16 16:03:58 -0700170 public static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider";
171
172 public static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/");
173
The Android Open Source Project0c908882009-03-03 19:32:16 -0800174 private TabControl mTabControl;
175
176 // Single instance of the BrowserSettings for use in the Browser app.
177 private static BrowserSettings sSingleton;
178
179 // Private map of WebSettings to Observer objects used when deleting an
180 // observer.
181 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
182 new HashMap<WebSettings,Observer>();
183
184 /*
185 * An observer wrapper for updating a WebSettings object with the new
186 * settings after a call to BrowserSettings.update().
187 */
188 static class Observer implements java.util.Observer {
189 // Private WebSettings object that will be updated.
190 private WebSettings mSettings;
191
192 Observer(WebSettings w) {
193 mSettings = w;
194 }
195
196 public void update(Observable o, Object arg) {
197 BrowserSettings b = (BrowserSettings)o;
198 WebSettings s = mSettings;
199
200 s.setLayoutAlgorithm(b.layoutAlgorithm);
201 if (b.userAgent == 0) {
202 // use the default ua string
203 s.setUserAgentString(null);
204 } else if (b.userAgent == 1) {
205 s.setUserAgentString(DESKTOP_USERAGENT);
206 } else if (b.userAgent == 2) {
207 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700208 } else if (b.userAgent == 3) {
209 s.setUserAgentString(IPAD_USERAGENT);
210 } else if (b.userAgent == 4) {
211 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800212 }
213 s.setUseWideViewPort(b.useWideViewPort);
214 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
215 s.setJavaScriptEnabled(b.javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400216 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800217 s.setJavaScriptCanOpenWindowsAutomatically(
218 b.javaScriptCanOpenWindowsAutomatically);
219 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
220 s.setMinimumFontSize(b.minimumFontSize);
221 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
222 s.setDefaultFontSize(b.defaultFontSize);
223 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
224 s.setNavDump(b.navDump);
225 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700226 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800227 s.setLightTouchEnabled(b.lightTouch);
228 s.setSaveFormData(b.saveFormData);
229 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700230 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800231 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800232
233 // WebView inside Browser doesn't want initial focus to be set.
234 s.setNeedInitialFocus(false);
235 // Browser supports multiple windows
236 s.setSupportMultipleWindows(true);
Grace Kloba61fc77c2010-06-22 09:57:33 -0700237 // enable smooth transition for better performance during panning or
238 // zooming
239 s.setEnableSmoothTransition(true);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100240
Andrei Popescu01068702009-08-03 16:03:24 +0100241 // HTML5 API flags
242 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100243 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100244 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100245 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100246 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100247
Andrei Popescu01068702009-08-03 16:03:24 +0100248 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100249 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100250 s.setAppCachePath(b.appCachePath);
251 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100252 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100253
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800254 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800255 }
256 }
257
258 /**
259 * Load settings from the browser app's database.
260 * NOTE: Strings used for the preferences must match those specified
261 * in the browser_preferences.xml
262 * @param ctx A Context object used to query the browser's settings
263 * database. If the database exists, the saved settings will be
264 * stored in this BrowserSettings object. This will update all
265 * observers of this object.
266 */
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100267 public void loadFromDb(final Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800268 SharedPreferences p =
269 PreferenceManager.getDefaultSharedPreferences(ctx);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100270 // Set the default value for the Application Caches path.
271 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100272 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100273 webStorageSizeManager = new WebStorageSizeManager(
274 ctx,
275 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
276 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100277 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100278 // Set the default value for the Database path.
279 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100280 // Set the default value for the Geolocation database path.
281 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800282
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700283 if (p.getString(PREF_HOMEPAGE, "") == "") {
284 // No home page preferences is set, set it to default.
285 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
286 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700287
Grace Kloba9804c432009-12-02 11:07:40 -0800288 // the cost of one cached page is ~3M (measured using nytimes.com). For
289 // low end devices, we only cache one page. For high end devices, we try
290 // to cache more pages, currently choose 5.
291 ActivityManager am = (ActivityManager) ctx
292 .getSystemService(Context.ACTIVITY_SERVICE);
293 if (am.getMemoryClass() > 16) {
Andrei Popescuba676ef2010-03-29 12:12:55 +0100294 pageCacheCapacity = 5;
Grace Kloba9804c432009-12-02 11:07:40 -0800295 } else {
296 pageCacheCapacity = 1;
297 }
298
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100299 // Load the defaults from the xml
The Android Open Source Project0c908882009-03-03 19:32:16 -0800300 // This call is TOO SLOW, need to manually keep the defaults
301 // in sync
302 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100303 syncSharedPreferences(ctx, p);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800304 }
305
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100306 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700307
The Android Open Source Project0c908882009-03-03 19:32:16 -0800308 homeUrl =
309 p.getString(PREF_HOMEPAGE, homeUrl);
Leon Scroggins III31306602010-09-17 11:10:29 -0400310 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
311 SearchEngine.GOOGLE);
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100312 if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
313 if (searchEngine != null) {
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400314 if (searchEngine.supportsVoiceSearch()) {
315 // One or more tabs could have been in voice search mode.
316 // Clear it, since the new SearchEngine may not support
317 // it, or may handle it differently.
318 for (int i = 0; i < mTabControl.getTabCount(); i++) {
319 mTabControl.getTab(i).revertVoiceSearchMode();
320 }
321 }
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100322 searchEngine.close();
323 }
324 searchEngine = SearchEngines.get(ctx, searchEngineName);
325 }
326 Log.i(TAG, "Selected search engine: " + searchEngine);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700327
The Android Open Source Project0c908882009-03-03 19:32:16 -0800328 loadsImagesAutomatically = p.getBoolean("load_images",
329 loadsImagesAutomatically);
330 javaScriptEnabled = p.getBoolean("enable_javascript",
331 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400332 pluginState = WebSettings.PluginState.valueOf(
333 p.getString("plugin_state", pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800334 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
335 "block_popup_windows",
336 !javaScriptCanOpenWindowsAutomatically);
337 showSecurityWarnings = p.getBoolean("show_security_warnings",
338 showSecurityWarnings);
339 rememberPasswords = p.getBoolean("remember_passwords",
340 rememberPasswords);
341 saveFormData = p.getBoolean("save_formdata",
342 saveFormData);
343 boolean accept_cookies = p.getBoolean("accept_cookies",
344 CookieManager.getInstance().acceptCookie());
345 CookieManager.getInstance().setAcceptCookie(accept_cookies);
346 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800347 textSize = WebSettings.TextSize.valueOf(
348 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700349 zoomDensity = WebSettings.ZoomDensity.valueOf(
350 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800351 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700352 loadsPageInOverviewMode = p.getBoolean("load_page",
353 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700354 boolean landscapeOnlyTemp =
355 p.getBoolean("landscape_only", landscapeOnly);
356 if (landscapeOnlyTemp != landscapeOnly) {
357 landscapeOnly = landscapeOnlyTemp;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700358 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800359 useWideViewPort = true; // use wide view port for either setting
360 if (autoFitPage) {
361 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
362 } else {
363 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
364 }
365 defaultTextEncodingName =
366 p.getString(PREF_DEFAULT_TEXT_ENCODING,
367 defaultTextEncodingName);
368
369 showDebugSettings =
370 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
371 // Debug menu items have precidence if the menu is visible
372 if (showDebugSettings) {
373 boolean small_screen = p.getBoolean("small_screen",
374 layoutAlgorithm ==
375 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
376 if (small_screen) {
377 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
378 } else {
379 boolean normal_layout = p.getBoolean("normal_layout",
380 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
381 if (normal_layout) {
382 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
383 } else {
384 layoutAlgorithm =
385 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
386 }
387 }
388 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
389 tracing = p.getBoolean("enable_tracing", tracing);
390 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
391 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800392 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800393 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700394 // JS flags is loaded from DB even if showDebugSettings is false,
395 // so that it can be set once and be effective all the time.
396 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100397
398 // Read the setting for showing/hiding the JS Console always so that should the
399 // user enable debug settings, we already know if we should show the console.
400 // The user will never see the console unless they navigate to about:debug,
401 // regardless of the setting we read here. This setting is only used after debug
402 // is enabled.
403 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700404
Andrei Popescu01068702009-08-03 16:03:24 +0100405 // HTML5 API flags
406 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
407 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
408 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100409 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100410 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100411
Grace Klobad9ee1392009-07-20 11:53:33 -0700412 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800413 }
414
The Android Open Source Project0c908882009-03-03 19:32:16 -0800415 public String getHomePage() {
416 return homeUrl;
417 }
418
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100419 public SearchEngine getSearchEngine() {
420 return searchEngine;
421 }
422
Feng Qianb3c02da2009-06-29 15:58:08 -0700423 public String getJsFlags() {
424 return jsFlags;
425 }
426
Andrei Popescu79e82b72009-07-27 12:01:59 +0100427 public WebStorageSizeManager getWebStorageSizeManager() {
428 return webStorageSizeManager;
429 }
430
The Android Open Source Project0c908882009-03-03 19:32:16 -0800431 public void setHomePage(Context context, String url) {
432 Editor ed = PreferenceManager.
433 getDefaultSharedPreferences(context).edit();
434 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700435 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800436 homeUrl = url;
437 }
438
The Android Open Source Project0c908882009-03-03 19:32:16 -0800439 public WebSettings.TextSize getTextSize() {
440 return textSize;
441 }
442
Grace Kloba2f830682009-06-25 11:08:53 -0700443 public WebSettings.ZoomDensity getDefaultZoom() {
444 return zoomDensity;
445 }
446
The Android Open Source Project0c908882009-03-03 19:32:16 -0800447 public boolean openInBackground() {
448 return openInBackground;
449 }
450
451 public boolean showSecurityWarnings() {
452 return showSecurityWarnings;
453 }
454
455 public boolean isTracing() {
456 return tracing;
457 }
458
459 public boolean isLightTouch() {
460 return lightTouch;
461 }
462
463 public boolean isNavDump() {
464 return navDump;
465 }
466
The Android Open Source Project0c908882009-03-03 19:32:16 -0800467 public boolean showDebugSettings() {
468 return showDebugSettings;
469 }
470
471 public void toggleDebugSettings() {
472 showDebugSettings = !showDebugSettings;
473 navDump = showDebugSettings;
474 update();
475 }
476
477 /**
478 * Add a WebSettings object to the list of observers that will be updated
479 * when update() is called.
480 *
481 * @param s A WebSettings object that is strictly tied to the life of a
482 * WebView.
483 */
484 public Observer addObserver(WebSettings s) {
485 Observer old = mWebSettingsToObservers.get(s);
486 if (old != null) {
487 super.deleteObserver(old);
488 }
489 Observer o = new Observer(s);
490 mWebSettingsToObservers.put(s, o);
491 super.addObserver(o);
492 return o;
493 }
494
495 /**
496 * Delete the given WebSettings observer from the list of observers.
497 * @param s The WebSettings object to be deleted.
498 */
499 public void deleteObserver(WebSettings s) {
500 Observer o = mWebSettingsToObservers.get(s);
501 if (o != null) {
502 mWebSettingsToObservers.remove(s);
503 super.deleteObserver(o);
504 }
505 }
506
507 /*
508 * Package level method for obtaining a single app instance of the
509 * BrowserSettings.
510 */
511 /*package*/ static BrowserSettings getInstance() {
512 if (sSingleton == null ) {
513 sSingleton = new BrowserSettings();
514 }
515 return sSingleton;
516 }
517
518 /*
519 * Package level method for associating the BrowserSettings with TabControl
520 */
521 /* package */void setTabControl(TabControl tabControl) {
522 mTabControl = tabControl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800523 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800524 }
525
526 /*
527 * Update all the observers of the object.
528 */
529 /*package*/ void update() {
530 setChanged();
531 notifyObservers();
532 }
533
534 /*package*/ void clearCache(Context context) {
535 WebIconDatabase.getInstance().removeAllIcons();
536 if (mTabControl != null) {
537 WebView current = mTabControl.getCurrentWebView();
538 if (current != null) {
539 current.clearCache(true);
540 }
541 }
542 }
543
544 /*package*/ void clearCookies(Context context) {
545 CookieManager.getInstance().removeAllCookie();
546 }
547
548 /* package */void clearHistory(Context context) {
549 ContentResolver resolver = context.getContentResolver();
550 Browser.clearHistory(resolver);
551 Browser.clearSearches(resolver);
552 }
553
554 /* package */ void clearFormData(Context context) {
555 WebViewDatabase.getInstance(context).clearFormData();
556 if (mTabControl != null) {
Henrik Baard794dc722010-02-19 16:28:06 +0100557 WebView currentTopView = mTabControl.getCurrentTopWebView();
558 if (currentTopView != null) {
559 currentTopView.clearFormData();
560 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800561 }
562 }
563
564 /*package*/ void clearPasswords(Context context) {
565 WebViewDatabase db = WebViewDatabase.getInstance(context);
566 db.clearUsernamePassword();
567 db.clearHttpAuthUsernamePassword();
568 }
569
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800570 private void updateTabControlSettings() {
571 // Enable/disable the error console.
572 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
573 showDebugSettings && showConsole);
574 mTabControl.getBrowserActivity().setRequestedOrientation(
575 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
576 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
577 }
578
Steve Blockf344d032009-07-30 10:50:45 +0100579 private void maybeDisableWebsiteSettings(Context context) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100580 PreferenceActivity activity = (PreferenceActivity) context;
581 final PreferenceScreen screen = (PreferenceScreen)
582 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
583 screen.setEnabled(false);
584 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
585 public void onReceiveValue(Map webStorageOrigins) {
586 if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) {
587 screen.setEnabled(true);
588 }
589 }
590 });
591
Steve Block2a6a0f42009-11-19 15:55:42 +0000592 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
593 public void onReceiveValue(Set<String> geolocationOrigins) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100594 if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) {
595 screen.setEnabled(true);
596 }
597 }
598 });
Steve Blockf344d032009-07-30 10:50:45 +0100599 }
600
Nicolas Roard78a98e42009-05-11 13:34:17 +0100601 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100602 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100603 maybeDisableWebsiteSettings(context);
604 }
605
606 /*package*/ void clearLocationAccess(Context context) {
607 GeolocationPermissions.getInstance().clearAll();
608 maybeDisableWebsiteSettings(context);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100609 }
610
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400611 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800612 reset();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800613 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400614 PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700615 p.edit().clear().apply();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400616 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800617 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700618 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700619 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100620 // reset appcache max size
621 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700622 }
623
624 private String getFactoryResetHomeUrl(Context context) {
625 String url = context.getResources().getString(R.string.homepage_base);
626 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500627 url = url.replace("{CID}",
628 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700629 }
630 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800631 }
632
The Android Open Source Project0c908882009-03-03 19:32:16 -0800633 // Private constructor that does nothing.
634 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800635 reset();
636 }
637
638 private void reset() {
639 // Private variables for settings
640 // NOTE: these defaults need to be kept in sync with the XML
641 // until the performance of PreferenceManager.setDefaultValues()
642 // is improved.
643 loadsImagesAutomatically = true;
644 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400645 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800646 javaScriptCanOpenWindowsAutomatically = false;
647 showSecurityWarnings = true;
648 rememberPasswords = true;
649 saveFormData = true;
650 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800651 autoFitPage = true;
652 landscapeOnly = false;
653 loadsPageInOverviewMode = true;
654 showDebugSettings = false;
655 // HTML5 API flags
656 appCacheEnabled = true;
657 databaseEnabled = true;
658 domStorageEnabled = true;
659 geolocationEnabled = true;
660 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800661 }
662}