blob: 4c798a06dd505ba6e234566d30f4304a1478cd09 [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";
Ben Murdochaf554522010-09-10 22:09:30 +0100150 public final static String PREF_AUTOFILL_ENABLED = "autofill_enabled";
151 public final static String PREF_AUTOFILL_PROFILE = "autofill_profile";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800152
153 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700154 "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
155 "like Gecko) Version/5.0 Safari/533.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800156
157 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700158 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
159 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
160
161 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
162 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
163 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
164
165 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
166 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
167 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800168
169 // Value to truncate strings when adding them to a TextView within
170 // a ListView
171 public final static int MAX_TEXTVIEW_LEN = 80;
172
Jeff Davidson43610292010-07-16 16:03:58 -0700173 public static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider";
174
175 public static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/");
176
The Android Open Source Project0c908882009-03-03 19:32:16 -0800177 private TabControl mTabControl;
178
179 // Single instance of the BrowserSettings for use in the Browser app.
180 private static BrowserSettings sSingleton;
181
182 // Private map of WebSettings to Observer objects used when deleting an
183 // observer.
184 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
185 new HashMap<WebSettings,Observer>();
186
187 /*
188 * An observer wrapper for updating a WebSettings object with the new
189 * settings after a call to BrowserSettings.update().
190 */
191 static class Observer implements java.util.Observer {
192 // Private WebSettings object that will be updated.
193 private WebSettings mSettings;
194
195 Observer(WebSettings w) {
196 mSettings = w;
197 }
198
199 public void update(Observable o, Object arg) {
200 BrowserSettings b = (BrowserSettings)o;
201 WebSettings s = mSettings;
202
203 s.setLayoutAlgorithm(b.layoutAlgorithm);
204 if (b.userAgent == 0) {
205 // use the default ua string
206 s.setUserAgentString(null);
207 } else if (b.userAgent == 1) {
208 s.setUserAgentString(DESKTOP_USERAGENT);
209 } else if (b.userAgent == 2) {
210 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700211 } else if (b.userAgent == 3) {
212 s.setUserAgentString(IPAD_USERAGENT);
213 } else if (b.userAgent == 4) {
214 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800215 }
216 s.setUseWideViewPort(b.useWideViewPort);
217 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
218 s.setJavaScriptEnabled(b.javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400219 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800220 s.setJavaScriptCanOpenWindowsAutomatically(
221 b.javaScriptCanOpenWindowsAutomatically);
222 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
223 s.setMinimumFontSize(b.minimumFontSize);
224 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
225 s.setDefaultFontSize(b.defaultFontSize);
226 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
227 s.setNavDump(b.navDump);
228 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700229 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800230 s.setLightTouchEnabled(b.lightTouch);
231 s.setSaveFormData(b.saveFormData);
Ben Murdochce549fc2010-09-09 10:28:08 +0100232 s.setAutoFillEnabled(b.autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800233 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700234 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800235 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800236
237 // WebView inside Browser doesn't want initial focus to be set.
238 s.setNeedInitialFocus(false);
239 // Browser supports multiple windows
240 s.setSupportMultipleWindows(true);
Grace Kloba61fc77c2010-06-22 09:57:33 -0700241 // enable smooth transition for better performance during panning or
242 // zooming
243 s.setEnableSmoothTransition(true);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100244
Andrei Popescu01068702009-08-03 16:03:24 +0100245 // HTML5 API flags
246 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100247 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100248 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100249 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100250 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100251
Andrei Popescu01068702009-08-03 16:03:24 +0100252 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100253 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100254 s.setAppCachePath(b.appCachePath);
255 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100256 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100257
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800258 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800259 }
260 }
261
262 /**
263 * Load settings from the browser app's database.
264 * NOTE: Strings used for the preferences must match those specified
Jeff Hamilton175ab302010-10-04 12:53:49 -0500265 * in the various preference XML files.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800266 * @param ctx A Context object used to query the browser's settings
267 * database. If the database exists, the saved settings will be
268 * stored in this BrowserSettings object. This will update all
269 * observers of this object.
270 */
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100271 public void loadFromDb(final Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800272 SharedPreferences p =
273 PreferenceManager.getDefaultSharedPreferences(ctx);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100274 // Set the default value for the Application Caches path.
275 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100276 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100277 webStorageSizeManager = new WebStorageSizeManager(
278 ctx,
279 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
280 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100281 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100282 // Set the default value for the Database path.
283 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100284 // Set the default value for the Geolocation database path.
285 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800286
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700287 if (p.getString(PREF_HOMEPAGE, "") == "") {
288 // No home page preferences is set, set it to default.
289 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
290 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700291
Grace Kloba9804c432009-12-02 11:07:40 -0800292 // the cost of one cached page is ~3M (measured using nytimes.com). For
293 // low end devices, we only cache one page. For high end devices, we try
294 // to cache more pages, currently choose 5.
295 ActivityManager am = (ActivityManager) ctx
296 .getSystemService(Context.ACTIVITY_SERVICE);
297 if (am.getMemoryClass() > 16) {
Andrei Popescuba676ef2010-03-29 12:12:55 +0100298 pageCacheCapacity = 5;
Grace Kloba9804c432009-12-02 11:07:40 -0800299 } else {
300 pageCacheCapacity = 1;
301 }
302
Jeff Hamilton175ab302010-10-04 12:53:49 -0500303 // PreferenceManager.setDefaultValues is TOO SLOW, need to manually keep
304 // the defaults in sync
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100305 syncSharedPreferences(ctx, p);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800306 }
307
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100308 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700309
The Android Open Source Project0c908882009-03-03 19:32:16 -0800310 homeUrl =
311 p.getString(PREF_HOMEPAGE, homeUrl);
Leon Scroggins III31306602010-09-17 11:10:29 -0400312 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
313 SearchEngine.GOOGLE);
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100314 if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
315 if (searchEngine != null) {
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400316 if (searchEngine.supportsVoiceSearch()) {
317 // One or more tabs could have been in voice search mode.
318 // Clear it, since the new SearchEngine may not support
319 // it, or may handle it differently.
320 for (int i = 0; i < mTabControl.getTabCount(); i++) {
321 mTabControl.getTab(i).revertVoiceSearchMode();
322 }
323 }
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100324 searchEngine.close();
325 }
326 searchEngine = SearchEngines.get(ctx, searchEngineName);
327 }
328 Log.i(TAG, "Selected search engine: " + searchEngine);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700329
The Android Open Source Project0c908882009-03-03 19:32:16 -0800330 loadsImagesAutomatically = p.getBoolean("load_images",
331 loadsImagesAutomatically);
332 javaScriptEnabled = p.getBoolean("enable_javascript",
333 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400334 pluginState = WebSettings.PluginState.valueOf(
335 p.getString("plugin_state", pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800336 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
337 "block_popup_windows",
338 !javaScriptCanOpenWindowsAutomatically);
339 showSecurityWarnings = p.getBoolean("show_security_warnings",
340 showSecurityWarnings);
341 rememberPasswords = p.getBoolean("remember_passwords",
342 rememberPasswords);
343 saveFormData = p.getBoolean("save_formdata",
344 saveFormData);
Ben Murdochaf554522010-09-10 22:09:30 +0100345 autoFillEnabled = p.getBoolean("autofill_enabled", autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800346 boolean accept_cookies = p.getBoolean("accept_cookies",
347 CookieManager.getInstance().acceptCookie());
348 CookieManager.getInstance().setAcceptCookie(accept_cookies);
349 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800350 textSize = WebSettings.TextSize.valueOf(
351 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700352 zoomDensity = WebSettings.ZoomDensity.valueOf(
353 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800354 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700355 loadsPageInOverviewMode = p.getBoolean("load_page",
356 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700357 boolean landscapeOnlyTemp =
358 p.getBoolean("landscape_only", landscapeOnly);
359 if (landscapeOnlyTemp != landscapeOnly) {
360 landscapeOnly = landscapeOnlyTemp;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700361 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800362 useWideViewPort = true; // use wide view port for either setting
363 if (autoFitPage) {
364 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
365 } else {
366 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
367 }
368 defaultTextEncodingName =
369 p.getString(PREF_DEFAULT_TEXT_ENCODING,
370 defaultTextEncodingName);
371
372 showDebugSettings =
373 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
374 // Debug menu items have precidence if the menu is visible
375 if (showDebugSettings) {
376 boolean small_screen = p.getBoolean("small_screen",
377 layoutAlgorithm ==
378 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
379 if (small_screen) {
380 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
381 } else {
382 boolean normal_layout = p.getBoolean("normal_layout",
383 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
384 if (normal_layout) {
385 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
386 } else {
387 layoutAlgorithm =
388 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
389 }
390 }
391 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
392 tracing = p.getBoolean("enable_tracing", tracing);
393 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
394 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800395 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800396 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700397 // JS flags is loaded from DB even if showDebugSettings is false,
398 // so that it can be set once and be effective all the time.
399 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100400
401 // Read the setting for showing/hiding the JS Console always so that should the
402 // user enable debug settings, we already know if we should show the console.
403 // The user will never see the console unless they navigate to about:debug,
404 // regardless of the setting we read here. This setting is only used after debug
405 // is enabled.
406 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700407
Andrei Popescu01068702009-08-03 16:03:24 +0100408 // HTML5 API flags
409 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
410 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
411 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100412 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100413 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100414
Grace Klobad9ee1392009-07-20 11:53:33 -0700415 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800416 }
417
The Android Open Source Project0c908882009-03-03 19:32:16 -0800418 public String getHomePage() {
419 return homeUrl;
420 }
421
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100422 public SearchEngine getSearchEngine() {
423 return searchEngine;
424 }
425
Feng Qianb3c02da2009-06-29 15:58:08 -0700426 public String getJsFlags() {
427 return jsFlags;
428 }
429
Andrei Popescu79e82b72009-07-27 12:01:59 +0100430 public WebStorageSizeManager getWebStorageSizeManager() {
431 return webStorageSizeManager;
432 }
433
The Android Open Source Project0c908882009-03-03 19:32:16 -0800434 public void setHomePage(Context context, String url) {
435 Editor ed = PreferenceManager.
436 getDefaultSharedPreferences(context).edit();
437 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700438 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800439 homeUrl = url;
440 }
441
The Android Open Source Project0c908882009-03-03 19:32:16 -0800442 public WebSettings.TextSize getTextSize() {
443 return textSize;
444 }
445
Grace Kloba2f830682009-06-25 11:08:53 -0700446 public WebSettings.ZoomDensity getDefaultZoom() {
447 return zoomDensity;
448 }
449
The Android Open Source Project0c908882009-03-03 19:32:16 -0800450 public boolean openInBackground() {
451 return openInBackground;
452 }
453
454 public boolean showSecurityWarnings() {
455 return showSecurityWarnings;
456 }
457
458 public boolean isTracing() {
459 return tracing;
460 }
461
462 public boolean isLightTouch() {
463 return lightTouch;
464 }
465
466 public boolean isNavDump() {
467 return navDump;
468 }
469
The Android Open Source Project0c908882009-03-03 19:32:16 -0800470 public boolean showDebugSettings() {
471 return showDebugSettings;
472 }
473
474 public void toggleDebugSettings() {
475 showDebugSettings = !showDebugSettings;
476 navDump = showDebugSettings;
477 update();
478 }
479
480 /**
481 * Add a WebSettings object to the list of observers that will be updated
482 * when update() is called.
483 *
484 * @param s A WebSettings object that is strictly tied to the life of a
485 * WebView.
486 */
487 public Observer addObserver(WebSettings s) {
488 Observer old = mWebSettingsToObservers.get(s);
489 if (old != null) {
490 super.deleteObserver(old);
491 }
492 Observer o = new Observer(s);
493 mWebSettingsToObservers.put(s, o);
494 super.addObserver(o);
495 return o;
496 }
497
498 /**
499 * Delete the given WebSettings observer from the list of observers.
500 * @param s The WebSettings object to be deleted.
501 */
502 public void deleteObserver(WebSettings s) {
503 Observer o = mWebSettingsToObservers.get(s);
504 if (o != null) {
505 mWebSettingsToObservers.remove(s);
506 super.deleteObserver(o);
507 }
508 }
509
510 /*
511 * Package level method for obtaining a single app instance of the
512 * BrowserSettings.
513 */
514 /*package*/ static BrowserSettings getInstance() {
515 if (sSingleton == null ) {
516 sSingleton = new BrowserSettings();
517 }
518 return sSingleton;
519 }
520
521 /*
522 * Package level method for associating the BrowserSettings with TabControl
523 */
524 /* package */void setTabControl(TabControl tabControl) {
525 mTabControl = tabControl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800526 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800527 }
528
529 /*
530 * Update all the observers of the object.
531 */
532 /*package*/ void update() {
533 setChanged();
534 notifyObservers();
535 }
536
537 /*package*/ void clearCache(Context context) {
538 WebIconDatabase.getInstance().removeAllIcons();
539 if (mTabControl != null) {
540 WebView current = mTabControl.getCurrentWebView();
541 if (current != null) {
542 current.clearCache(true);
543 }
544 }
545 }
546
547 /*package*/ void clearCookies(Context context) {
548 CookieManager.getInstance().removeAllCookie();
549 }
550
551 /* package */void clearHistory(Context context) {
552 ContentResolver resolver = context.getContentResolver();
553 Browser.clearHistory(resolver);
554 Browser.clearSearches(resolver);
555 }
556
557 /* package */ void clearFormData(Context context) {
558 WebViewDatabase.getInstance(context).clearFormData();
559 if (mTabControl != null) {
Henrik Baard794dc722010-02-19 16:28:06 +0100560 WebView currentTopView = mTabControl.getCurrentTopWebView();
561 if (currentTopView != null) {
562 currentTopView.clearFormData();
563 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800564 }
565 }
566
567 /*package*/ void clearPasswords(Context context) {
568 WebViewDatabase db = WebViewDatabase.getInstance(context);
569 db.clearUsernamePassword();
570 db.clearHttpAuthUsernamePassword();
571 }
572
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800573 private void updateTabControlSettings() {
574 // Enable/disable the error console.
575 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
576 showDebugSettings && showConsole);
577 mTabControl.getBrowserActivity().setRequestedOrientation(
578 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
579 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
580 }
581
Nicolas Roard78a98e42009-05-11 13:34:17 +0100582 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100583 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100584 }
585
586 /*package*/ void clearLocationAccess(Context context) {
587 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100588 }
589
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400590 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800591 reset();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500592 SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700593 p.edit().clear().apply();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500594 PreferenceManager.setDefaultValues(ctx, R.xml.page_content_preferences, true);
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500595 PreferenceManager.setDefaultValues(ctx, R.xml.personal_preferences, true);
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500596 PreferenceManager.setDefaultValues(ctx, R.xml.privacy_preferences, true);
597 PreferenceManager.setDefaultValues(ctx, R.xml.security_preferences, true);
598 PreferenceManager.setDefaultValues(ctx, R.xml.advanced_preferences, true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700599 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700600 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100601 // reset appcache max size
602 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700603 }
604
605 private String getFactoryResetHomeUrl(Context context) {
606 String url = context.getResources().getString(R.string.homepage_base);
607 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500608 url = url.replace("{CID}",
609 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700610 }
611 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800612 }
613
The Android Open Source Project0c908882009-03-03 19:32:16 -0800614 // Private constructor that does nothing.
615 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800616 reset();
617 }
618
619 private void reset() {
620 // Private variables for settings
621 // NOTE: these defaults need to be kept in sync with the XML
622 // until the performance of PreferenceManager.setDefaultValues()
623 // is improved.
624 loadsImagesAutomatically = true;
625 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400626 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800627 javaScriptCanOpenWindowsAutomatically = false;
628 showSecurityWarnings = true;
629 rememberPasswords = true;
630 saveFormData = true;
Ben Murdochce549fc2010-09-09 10:28:08 +0100631 autoFillEnabled = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800632 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800633 autoFitPage = true;
634 landscapeOnly = false;
635 loadsPageInOverviewMode = true;
636 showDebugSettings = false;
637 // HTML5 API flags
638 appCacheEnabled = true;
639 databaseEnabled = true;
640 domStorageEnabled = true;
641 geolocationEnabled = true;
642 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800643 }
644}