blob: dfec48f9313ae2ff8d4d1110bda5aae770f98f36 [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 */
Jeff Hamilton462b8e82010-09-23 14:33:43 -050066public class BrowserSettings extends Observable {
The Android Open Source Project0c908882009-03-03 19:32:16 -080067
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;
Ben Murdochce549fc2010-09-09 10:28:08 +010080 private boolean autoFillEnabled;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080081 private boolean openInBackground;
The Android Open Source Project0c908882009-03-03 19:32:16 -080082 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070083 private String homeUrl = "";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010084 private SearchEngine searchEngine;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080085 private boolean autoFitPage;
86 private boolean landscapeOnly;
87 private boolean loadsPageInOverviewMode;
88 private boolean showDebugSettings;
Andrei Popescu01068702009-08-03 16:03:24 +010089 // HTML5 API flags
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080090 private boolean appCacheEnabled;
91 private boolean databaseEnabled;
92 private boolean domStorageEnabled;
93 private boolean geolocationEnabled;
94 private boolean workersEnabled; // only affects V8. JSC does not have a similar setting
Andrei Popescu01068702009-08-03 16:03:24 +010095 // HTML5 API configuration params
96 private long appCacheMaxSize = Long.MAX_VALUE;
97 private String appCachePath; // default value set in loadFromDb().
98 private String databasePath; // default value set in loadFromDb()
Steve Block5029cf02009-08-21 13:16:58 +010099 private String geolocationDatabasePath; // default value set in loadFromDb()
Andrei Popescu01068702009-08-03 16:03:24 +0100100 private WebStorageSizeManager webStorageSizeManager;
101
102 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800103
Nicolas Roard78a98e42009-05-11 13:34:17 +0100104 private final static String TAG = "BrowserSettings";
105
The Android Open Source Project0c908882009-03-03 19:32:16 -0800106 // Development settings
107 public WebSettings.LayoutAlgorithm layoutAlgorithm =
108 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
109 private boolean useWideViewPort = true;
110 private int userAgent = 0;
111 private boolean tracing = false;
112 private boolean lightTouch = false;
113 private boolean navDump = false;
Feng Qianb3c02da2009-06-29 15:58:08 -0700114
Ben Murdochbff2d602009-07-01 20:19:05 +0100115 // By default the error console is shown once the user navigates to about:debug.
116 // The setting can be then toggled from the settings menu.
117 private boolean showConsole = true;
118
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119 // Private preconfigured values
Shimeng (Simon) Wangb4fb25c2010-07-13 15:18:10 -0700120 private static int minimumFontSize = 1;
121 private static int minimumLogicalFontSize = 1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800122 private static int defaultFontSize = 16;
123 private static int defaultFixedFontSize = 13;
124 private static WebSettings.TextSize textSize =
125 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700126 private static WebSettings.ZoomDensity zoomDensity =
127 WebSettings.ZoomDensity.MEDIUM;
Grace Kloba9804c432009-12-02 11:07:40 -0800128 private static int pageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800129
130 // Preference keys that are used outside this class
131 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
132 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
133 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
134 public final static String PREF_HOMEPAGE = "homepage";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100135 public final static String PREF_SEARCH_ENGINE = "search_engine";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800136 public final static String PREF_CLEAR_FORM_DATA =
137 "privacy_clear_form_data";
138 public final static String PREF_CLEAR_PASSWORDS =
139 "privacy_clear_passwords";
140 public final static String PREF_EXTRAS_RESET_DEFAULTS =
141 "reset_default_preferences";
142 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100143 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800144 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700145 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 public final static String PREF_DEFAULT_TEXT_ENCODING =
147 "default_text_encoding";
Steve Blockc6f577f2009-08-20 18:11:08 +0100148 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
149 "privacy_clear_geolocation_access";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800150
151 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700152 "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
153 "like Gecko) Version/5.0 Safari/533.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800154
155 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700156 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
157 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
158
159 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
160 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
161 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
162
163 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
164 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
165 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800166
167 // Value to truncate strings when adding them to a TextView within
168 // a ListView
169 public final static int MAX_TEXTVIEW_LEN = 80;
170
Jeff Davidson43610292010-07-16 16:03:58 -0700171 public static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider";
172
173 public static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/");
174
The Android Open Source Project0c908882009-03-03 19:32:16 -0800175 private TabControl mTabControl;
176
177 // Single instance of the BrowserSettings for use in the Browser app.
178 private static BrowserSettings sSingleton;
179
180 // Private map of WebSettings to Observer objects used when deleting an
181 // observer.
182 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
183 new HashMap<WebSettings,Observer>();
184
185 /*
186 * An observer wrapper for updating a WebSettings object with the new
187 * settings after a call to BrowserSettings.update().
188 */
189 static class Observer implements java.util.Observer {
190 // Private WebSettings object that will be updated.
191 private WebSettings mSettings;
192
193 Observer(WebSettings w) {
194 mSettings = w;
195 }
196
197 public void update(Observable o, Object arg) {
198 BrowserSettings b = (BrowserSettings)o;
199 WebSettings s = mSettings;
200
201 s.setLayoutAlgorithm(b.layoutAlgorithm);
202 if (b.userAgent == 0) {
203 // use the default ua string
204 s.setUserAgentString(null);
205 } else if (b.userAgent == 1) {
206 s.setUserAgentString(DESKTOP_USERAGENT);
207 } else if (b.userAgent == 2) {
208 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700209 } else if (b.userAgent == 3) {
210 s.setUserAgentString(IPAD_USERAGENT);
211 } else if (b.userAgent == 4) {
212 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800213 }
214 s.setUseWideViewPort(b.useWideViewPort);
215 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
216 s.setJavaScriptEnabled(b.javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400217 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800218 s.setJavaScriptCanOpenWindowsAutomatically(
219 b.javaScriptCanOpenWindowsAutomatically);
220 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
221 s.setMinimumFontSize(b.minimumFontSize);
222 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
223 s.setDefaultFontSize(b.defaultFontSize);
224 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
225 s.setNavDump(b.navDump);
226 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700227 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800228 s.setLightTouchEnabled(b.lightTouch);
229 s.setSaveFormData(b.saveFormData);
Ben Murdochce549fc2010-09-09 10:28:08 +0100230 s.setAutoFillEnabled(b.autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800231 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700232 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800233 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800234
235 // WebView inside Browser doesn't want initial focus to be set.
236 s.setNeedInitialFocus(false);
237 // Browser supports multiple windows
238 s.setSupportMultipleWindows(true);
Grace Kloba61fc77c2010-06-22 09:57:33 -0700239 // enable smooth transition for better performance during panning or
240 // zooming
241 s.setEnableSmoothTransition(true);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100242
Andrei Popescu01068702009-08-03 16:03:24 +0100243 // HTML5 API flags
244 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100245 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100246 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100247 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100248 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100249
Andrei Popescu01068702009-08-03 16:03:24 +0100250 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100251 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100252 s.setAppCachePath(b.appCachePath);
253 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100254 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100255
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800256 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800257 }
258 }
259
260 /**
261 * Load settings from the browser app's database.
262 * NOTE: Strings used for the preferences must match those specified
Jeff Hamilton175ab302010-10-04 12:53:49 -0500263 * in the various preference XML files.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800264 * @param ctx A Context object used to query the browser's settings
265 * database. If the database exists, the saved settings will be
266 * stored in this BrowserSettings object. This will update all
267 * observers of this object.
268 */
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100269 public void loadFromDb(final Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800270 SharedPreferences p =
271 PreferenceManager.getDefaultSharedPreferences(ctx);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100272 // Set the default value for the Application Caches path.
273 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100274 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100275 webStorageSizeManager = new WebStorageSizeManager(
276 ctx,
277 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
278 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100279 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100280 // Set the default value for the Database path.
281 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100282 // Set the default value for the Geolocation database path.
283 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800284
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700285 if (p.getString(PREF_HOMEPAGE, "") == "") {
286 // No home page preferences is set, set it to default.
287 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
288 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700289
Grace Kloba9804c432009-12-02 11:07:40 -0800290 // the cost of one cached page is ~3M (measured using nytimes.com). For
291 // low end devices, we only cache one page. For high end devices, we try
292 // to cache more pages, currently choose 5.
293 ActivityManager am = (ActivityManager) ctx
294 .getSystemService(Context.ACTIVITY_SERVICE);
295 if (am.getMemoryClass() > 16) {
Andrei Popescuba676ef2010-03-29 12:12:55 +0100296 pageCacheCapacity = 5;
Grace Kloba9804c432009-12-02 11:07:40 -0800297 } else {
298 pageCacheCapacity = 1;
299 }
300
Jeff Hamilton175ab302010-10-04 12:53:49 -0500301 // PreferenceManager.setDefaultValues is TOO SLOW, need to manually keep
302 // the defaults in sync
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);
Ben Murdochce549fc2010-09-09 10:28:08 +0100343 autoFillEnabled = p.getBoolean("autoFill_enabled", autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800344 boolean accept_cookies = p.getBoolean("accept_cookies",
345 CookieManager.getInstance().acceptCookie());
346 CookieManager.getInstance().setAcceptCookie(accept_cookies);
347 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800348 textSize = WebSettings.TextSize.valueOf(
349 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700350 zoomDensity = WebSettings.ZoomDensity.valueOf(
351 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800352 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700353 loadsPageInOverviewMode = p.getBoolean("load_page",
354 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700355 boolean landscapeOnlyTemp =
356 p.getBoolean("landscape_only", landscapeOnly);
357 if (landscapeOnlyTemp != landscapeOnly) {
358 landscapeOnly = landscapeOnlyTemp;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700359 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800360 useWideViewPort = true; // use wide view port for either setting
361 if (autoFitPage) {
362 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
363 } else {
364 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
365 }
366 defaultTextEncodingName =
367 p.getString(PREF_DEFAULT_TEXT_ENCODING,
368 defaultTextEncodingName);
369
370 showDebugSettings =
371 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
372 // Debug menu items have precidence if the menu is visible
373 if (showDebugSettings) {
374 boolean small_screen = p.getBoolean("small_screen",
375 layoutAlgorithm ==
376 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
377 if (small_screen) {
378 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
379 } else {
380 boolean normal_layout = p.getBoolean("normal_layout",
381 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
382 if (normal_layout) {
383 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
384 } else {
385 layoutAlgorithm =
386 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
387 }
388 }
389 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
390 tracing = p.getBoolean("enable_tracing", tracing);
391 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
392 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800393 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800394 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700395 // JS flags is loaded from DB even if showDebugSettings is false,
396 // so that it can be set once and be effective all the time.
397 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100398
399 // Read the setting for showing/hiding the JS Console always so that should the
400 // user enable debug settings, we already know if we should show the console.
401 // The user will never see the console unless they navigate to about:debug,
402 // regardless of the setting we read here. This setting is only used after debug
403 // is enabled.
404 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700405
Andrei Popescu01068702009-08-03 16:03:24 +0100406 // HTML5 API flags
407 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
408 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
409 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100410 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100411 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100412
Grace Klobad9ee1392009-07-20 11:53:33 -0700413 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800414 }
415
The Android Open Source Project0c908882009-03-03 19:32:16 -0800416 public String getHomePage() {
417 return homeUrl;
418 }
419
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100420 public SearchEngine getSearchEngine() {
421 return searchEngine;
422 }
423
Feng Qianb3c02da2009-06-29 15:58:08 -0700424 public String getJsFlags() {
425 return jsFlags;
426 }
427
Andrei Popescu79e82b72009-07-27 12:01:59 +0100428 public WebStorageSizeManager getWebStorageSizeManager() {
429 return webStorageSizeManager;
430 }
431
The Android Open Source Project0c908882009-03-03 19:32:16 -0800432 public void setHomePage(Context context, String url) {
433 Editor ed = PreferenceManager.
434 getDefaultSharedPreferences(context).edit();
435 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700436 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800437 homeUrl = url;
438 }
439
The Android Open Source Project0c908882009-03-03 19:32:16 -0800440 public WebSettings.TextSize getTextSize() {
441 return textSize;
442 }
443
Grace Kloba2f830682009-06-25 11:08:53 -0700444 public WebSettings.ZoomDensity getDefaultZoom() {
445 return zoomDensity;
446 }
447
The Android Open Source Project0c908882009-03-03 19:32:16 -0800448 public boolean openInBackground() {
449 return openInBackground;
450 }
451
452 public boolean showSecurityWarnings() {
453 return showSecurityWarnings;
454 }
455
456 public boolean isTracing() {
457 return tracing;
458 }
459
460 public boolean isLightTouch() {
461 return lightTouch;
462 }
463
464 public boolean isNavDump() {
465 return navDump;
466 }
467
The Android Open Source Project0c908882009-03-03 19:32:16 -0800468 public boolean showDebugSettings() {
469 return showDebugSettings;
470 }
471
472 public void toggleDebugSettings() {
473 showDebugSettings = !showDebugSettings;
474 navDump = showDebugSettings;
475 update();
476 }
477
478 /**
479 * Add a WebSettings object to the list of observers that will be updated
480 * when update() is called.
481 *
482 * @param s A WebSettings object that is strictly tied to the life of a
483 * WebView.
484 */
485 public Observer addObserver(WebSettings s) {
486 Observer old = mWebSettingsToObservers.get(s);
487 if (old != null) {
488 super.deleteObserver(old);
489 }
490 Observer o = new Observer(s);
491 mWebSettingsToObservers.put(s, o);
492 super.addObserver(o);
493 return o;
494 }
495
496 /**
497 * Delete the given WebSettings observer from the list of observers.
498 * @param s The WebSettings object to be deleted.
499 */
500 public void deleteObserver(WebSettings s) {
501 Observer o = mWebSettingsToObservers.get(s);
502 if (o != null) {
503 mWebSettingsToObservers.remove(s);
504 super.deleteObserver(o);
505 }
506 }
507
508 /*
509 * Package level method for obtaining a single app instance of the
510 * BrowserSettings.
511 */
512 /*package*/ static BrowserSettings getInstance() {
513 if (sSingleton == null ) {
514 sSingleton = new BrowserSettings();
515 }
516 return sSingleton;
517 }
518
519 /*
520 * Package level method for associating the BrowserSettings with TabControl
521 */
522 /* package */void setTabControl(TabControl tabControl) {
523 mTabControl = tabControl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800524 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800525 }
526
527 /*
528 * Update all the observers of the object.
529 */
530 /*package*/ void update() {
531 setChanged();
532 notifyObservers();
533 }
534
535 /*package*/ void clearCache(Context context) {
536 WebIconDatabase.getInstance().removeAllIcons();
537 if (mTabControl != null) {
538 WebView current = mTabControl.getCurrentWebView();
539 if (current != null) {
540 current.clearCache(true);
541 }
542 }
543 }
544
545 /*package*/ void clearCookies(Context context) {
546 CookieManager.getInstance().removeAllCookie();
547 }
548
549 /* package */void clearHistory(Context context) {
550 ContentResolver resolver = context.getContentResolver();
551 Browser.clearHistory(resolver);
552 Browser.clearSearches(resolver);
553 }
554
555 /* package */ void clearFormData(Context context) {
556 WebViewDatabase.getInstance(context).clearFormData();
557 if (mTabControl != null) {
Henrik Baard794dc722010-02-19 16:28:06 +0100558 WebView currentTopView = mTabControl.getCurrentTopWebView();
559 if (currentTopView != null) {
560 currentTopView.clearFormData();
561 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800562 }
563 }
564
565 /*package*/ void clearPasswords(Context context) {
566 WebViewDatabase db = WebViewDatabase.getInstance(context);
567 db.clearUsernamePassword();
568 db.clearHttpAuthUsernamePassword();
569 }
570
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800571 private void updateTabControlSettings() {
572 // Enable/disable the error console.
573 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
574 showDebugSettings && showConsole);
575 mTabControl.getBrowserActivity().setRequestedOrientation(
576 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
577 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
578 }
579
Nicolas Roard78a98e42009-05-11 13:34:17 +0100580 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100581 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100582 }
583
584 /*package*/ void clearLocationAccess(Context context) {
585 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100586 }
587
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400588 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800589 reset();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500590 SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700591 p.edit().clear().apply();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500592 PreferenceManager.setDefaultValues(ctx, R.xml.page_content_preferences, true);
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500593 PreferenceManager.setDefaultValues(ctx, R.xml.personal_preferences, true);
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500594 PreferenceManager.setDefaultValues(ctx, R.xml.privacy_preferences, true);
595 PreferenceManager.setDefaultValues(ctx, R.xml.security_preferences, true);
596 PreferenceManager.setDefaultValues(ctx, R.xml.advanced_preferences, true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700597 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700598 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100599 // reset appcache max size
600 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700601 }
602
603 private String getFactoryResetHomeUrl(Context context) {
604 String url = context.getResources().getString(R.string.homepage_base);
605 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500606 url = url.replace("{CID}",
607 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700608 }
609 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800610 }
611
The Android Open Source Project0c908882009-03-03 19:32:16 -0800612 // Private constructor that does nothing.
613 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800614 reset();
615 }
616
617 private void reset() {
618 // Private variables for settings
619 // NOTE: these defaults need to be kept in sync with the XML
620 // until the performance of PreferenceManager.setDefaultValues()
621 // is improved.
622 loadsImagesAutomatically = true;
623 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400624 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800625 javaScriptCanOpenWindowsAutomatically = false;
626 showSecurityWarnings = true;
627 rememberPasswords = true;
628 saveFormData = true;
Ben Murdochce549fc2010-09-09 10:28:08 +0100629 autoFillEnabled = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800630 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800631 autoFitPage = true;
632 landscapeOnly = false;
633 loadsPageInOverviewMode = true;
634 showDebugSettings = false;
635 // HTML5 API flags
636 appCacheEnabled = true;
637 databaseEnabled = true;
638 domStorageEnabled = true;
639 geolocationEnabled = true;
640 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800641 }
642}