blob: 56d3b6f65eb94d2198e1911d159ebb5ea54b950c [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 Bringertd2670652010-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 Bringertd2670652010-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 Bringertd2670652010-09-13 14:06:41 +010030import android.database.ContentObserver;
31import android.os.Handler;
Nicolas Roard78a98e42009-05-11 13:34:17 +010032import android.preference.PreferenceActivity;
33import android.preference.PreferenceScreen;
Bjorn Bringertd2670652010-09-13 14:06:41 +010034import android.provider.Settings;
35import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010037import android.webkit.GeolocationPermissions;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010038import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.webkit.WebView;
40import android.webkit.WebViewDatabase;
41import android.webkit.WebIconDatabase;
42import android.webkit.WebSettings;
Nicolas Roard78a98e42009-05-11 13:34:17 +010043import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044import android.preference.PreferenceManager;
45import android.provider.Browser;
46
47import java.util.HashMap;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010048import java.util.Map;
49import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050import java.util.Observable;
51
52/*
53 * Package level class for storing various WebView and Browser settings. To use
54 * this class:
55 * BrowserSettings s = BrowserSettings.getInstance();
56 * s.addObserver(webView.getSettings());
57 * s.loadFromDb(context); // Only needed on app startup
58 * s.javaScriptEnabled = true;
59 * ... // set any other settings
60 * s.update(); // this will update all the observers
61 *
62 * To remove an observer:
63 * s.deleteObserver(webView.getSettings());
64 */
65class BrowserSettings extends Observable {
66
Leon Scroggins9ac587d2009-04-20 12:53:46 -040067 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080068 // NOTE: these defaults need to be kept in sync with the XML
69 // until the performance of PreferenceManager.setDefaultValues()
70 // is improved.
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080071 // Note: boolean variables are set inside reset function.
72 private boolean loadsImagesAutomatically;
73 private boolean javaScriptEnabled;
Patrick Scotte536b332010-03-22 10:19:32 -040074 private WebSettings.PluginState pluginState;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080075 private boolean javaScriptCanOpenWindowsAutomatically;
76 private boolean showSecurityWarnings;
77 private boolean rememberPasswords;
78 private boolean saveFormData;
79 private boolean openInBackground;
The Android Open Source Project0c908882009-03-03 19:32:16 -080080 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070081 private String homeUrl = "";
Bjorn Bringertd2670652010-09-13 14:06:41 +010082 private SearchEngine searchEngine;
83 private boolean showSearchSuggestions;
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
119 private static int minimumFontSize = 8;
120 private static int minimumLogicalFontSize = 8;
121 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 Bringertd2670652010-09-13 14:06:41 +0100134 public final static String PREF_SEARCH_ENGINE = "search_engine";
135 public final static String PREF_SHOW_SEARCH_SUGGESTIONS = "show_search_suggestions";
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
171 private TabControl mTabControl;
172
173 // Single instance of the BrowserSettings for use in the Browser app.
174 private static BrowserSettings sSingleton;
175
176 // Private map of WebSettings to Observer objects used when deleting an
177 // observer.
178 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
179 new HashMap<WebSettings,Observer>();
180
181 /*
182 * An observer wrapper for updating a WebSettings object with the new
183 * settings after a call to BrowserSettings.update().
184 */
185 static class Observer implements java.util.Observer {
186 // Private WebSettings object that will be updated.
187 private WebSettings mSettings;
188
189 Observer(WebSettings w) {
190 mSettings = w;
191 }
192
193 public void update(Observable o, Object arg) {
194 BrowserSettings b = (BrowserSettings)o;
195 WebSettings s = mSettings;
196
197 s.setLayoutAlgorithm(b.layoutAlgorithm);
198 if (b.userAgent == 0) {
199 // use the default ua string
200 s.setUserAgentString(null);
201 } else if (b.userAgent == 1) {
202 s.setUserAgentString(DESKTOP_USERAGENT);
203 } else if (b.userAgent == 2) {
204 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700205 } else if (b.userAgent == 3) {
206 s.setUserAgentString(IPAD_USERAGENT);
207 } else if (b.userAgent == 4) {
208 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800209 }
210 s.setUseWideViewPort(b.useWideViewPort);
211 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
212 s.setJavaScriptEnabled(b.javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400213 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800214 s.setJavaScriptCanOpenWindowsAutomatically(
215 b.javaScriptCanOpenWindowsAutomatically);
216 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
217 s.setMinimumFontSize(b.minimumFontSize);
218 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
219 s.setDefaultFontSize(b.defaultFontSize);
220 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
221 s.setNavDump(b.navDump);
222 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700223 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800224 s.setLightTouchEnabled(b.lightTouch);
225 s.setSaveFormData(b.saveFormData);
226 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700227 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800228 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800229
230 // WebView inside Browser doesn't want initial focus to be set.
231 s.setNeedInitialFocus(false);
232 // Browser supports multiple windows
233 s.setSupportMultipleWindows(true);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100234
Andrei Popescu01068702009-08-03 16:03:24 +0100235 // HTML5 API flags
236 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100237 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100238 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100239 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100240 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100241
Andrei Popescu01068702009-08-03 16:03:24 +0100242 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100243 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100244 s.setAppCachePath(b.appCachePath);
245 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100246 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100247
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800248 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800249 }
250 }
251
252 /**
253 * Load settings from the browser app's database.
254 * NOTE: Strings used for the preferences must match those specified
255 * in the browser_preferences.xml
256 * @param ctx A Context object used to query the browser's settings
257 * database. If the database exists, the saved settings will be
258 * stored in this BrowserSettings object. This will update all
259 * observers of this object.
260 */
Bjorn Bringertd2670652010-09-13 14:06:41 +0100261 public void loadFromDb(final Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800262 SharedPreferences p =
263 PreferenceManager.getDefaultSharedPreferences(ctx);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100264 // Set the default value for the Application Caches path.
265 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100266 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100267 webStorageSizeManager = new WebStorageSizeManager(
268 ctx,
269 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
270 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100271 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100272 // Set the default value for the Database path.
273 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100274 // Set the default value for the Geolocation database path.
275 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800276
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700277 if (p.getString(PREF_HOMEPAGE, "") == "") {
278 // No home page preferences is set, set it to default.
279 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
280 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700281
Grace Kloba9804c432009-12-02 11:07:40 -0800282 // the cost of one cached page is ~3M (measured using nytimes.com). For
283 // low end devices, we only cache one page. For high end devices, we try
284 // to cache more pages, currently choose 5.
285 ActivityManager am = (ActivityManager) ctx
286 .getSystemService(Context.ACTIVITY_SERVICE);
287 if (am.getMemoryClass() > 16) {
Andrei Popescuba676ef2010-03-29 12:12:55 +0100288 pageCacheCapacity = 5;
Grace Kloba9804c432009-12-02 11:07:40 -0800289 } else {
290 pageCacheCapacity = 1;
291 }
292
Bjorn Bringertd2670652010-09-13 14:06:41 +0100293 final ContentResolver cr = ctx.getContentResolver();
294 cr.registerContentObserver(
295 Settings.System.getUriFor(Settings.System.SHOW_WEB_SUGGESTIONS), false,
296 new ContentObserver(new Handler()) {
297 @Override
298 public void onChange(boolean selfChange) {
299 SharedPreferences p =
300 PreferenceManager.getDefaultSharedPreferences(ctx);
301 updateShowWebSuggestions(cr, p);
302 }
303 });
304 updateShowWebSuggestions(cr, p);
305
306 // Load the defaults from the xml
The Android Open Source Project0c908882009-03-03 19:32:16 -0800307 // This call is TOO SLOW, need to manually keep the defaults
308 // in sync
309 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
Bjorn Bringertd2670652010-09-13 14:06:41 +0100310 syncSharedPreferences(ctx, p);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800311 }
312
Bjorn Bringertd2670652010-09-13 14:06:41 +0100313 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700314
The Android Open Source Project0c908882009-03-03 19:32:16 -0800315 homeUrl =
316 p.getString(PREF_HOMEPAGE, homeUrl);
Leon Scroggins III31306602010-09-17 11:10:29 -0400317 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
318 SearchEngine.GOOGLE);
Bjorn Bringertd2670652010-09-13 14:06:41 +0100319 if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
320 if (searchEngine != null) {
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400321 if (searchEngine.supportsVoiceSearch()) {
322 // One or more tabs could have been in voice search mode.
323 // Clear it, since the new SearchEngine may not support
324 // it, or may handle it differently.
325 for (int i = 0; i < mTabControl.getTabCount(); i++) {
326 mTabControl.getTab(i).revertVoiceSearchMode();
327 }
328 }
Bjorn Bringertd2670652010-09-13 14:06:41 +0100329 searchEngine.close();
330 }
331 searchEngine = SearchEngines.get(ctx, searchEngineName);
332 }
333 Log.i(TAG, "Selected search engine: " + searchEngine);
334 showSearchSuggestions = p.getBoolean(PREF_SHOW_SEARCH_SUGGESTIONS, true);
335 // Persist to system settings
336 saveShowWebSuggestions(ctx.getContentResolver());
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700337
The Android Open Source Project0c908882009-03-03 19:32:16 -0800338 loadsImagesAutomatically = p.getBoolean("load_images",
339 loadsImagesAutomatically);
340 javaScriptEnabled = p.getBoolean("enable_javascript",
341 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400342 pluginState = WebSettings.PluginState.valueOf(
343 p.getString("plugin_state", pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800344 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
345 "block_popup_windows",
346 !javaScriptCanOpenWindowsAutomatically);
347 showSecurityWarnings = p.getBoolean("show_security_warnings",
348 showSecurityWarnings);
349 rememberPasswords = p.getBoolean("remember_passwords",
350 rememberPasswords);
351 saveFormData = p.getBoolean("save_formdata",
352 saveFormData);
353 boolean accept_cookies = p.getBoolean("accept_cookies",
354 CookieManager.getInstance().acceptCookie());
355 CookieManager.getInstance().setAcceptCookie(accept_cookies);
356 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800357 textSize = WebSettings.TextSize.valueOf(
358 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700359 zoomDensity = WebSettings.ZoomDensity.valueOf(
360 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800361 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700362 loadsPageInOverviewMode = p.getBoolean("load_page",
363 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700364 boolean landscapeOnlyTemp =
365 p.getBoolean("landscape_only", landscapeOnly);
366 if (landscapeOnlyTemp != landscapeOnly) {
367 landscapeOnly = landscapeOnlyTemp;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700368 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800369 useWideViewPort = true; // use wide view port for either setting
370 if (autoFitPage) {
371 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
372 } else {
373 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
374 }
375 defaultTextEncodingName =
376 p.getString(PREF_DEFAULT_TEXT_ENCODING,
377 defaultTextEncodingName);
378
379 showDebugSettings =
380 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
381 // Debug menu items have precidence if the menu is visible
382 if (showDebugSettings) {
383 boolean small_screen = p.getBoolean("small_screen",
384 layoutAlgorithm ==
385 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
386 if (small_screen) {
387 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
388 } else {
389 boolean normal_layout = p.getBoolean("normal_layout",
390 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
391 if (normal_layout) {
392 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
393 } else {
394 layoutAlgorithm =
395 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
396 }
397 }
398 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
399 tracing = p.getBoolean("enable_tracing", tracing);
400 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
401 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800402 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800403 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700404 // JS flags is loaded from DB even if showDebugSettings is false,
405 // so that it can be set once and be effective all the time.
406 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100407
408 // Read the setting for showing/hiding the JS Console always so that should the
409 // user enable debug settings, we already know if we should show the console.
410 // The user will never see the console unless they navigate to about:debug,
411 // regardless of the setting we read here. This setting is only used after debug
412 // is enabled.
413 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700414
Andrei Popescu01068702009-08-03 16:03:24 +0100415 // HTML5 API flags
416 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
417 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
418 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100419 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100420 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100421
Grace Klobad9ee1392009-07-20 11:53:33 -0700422 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800423 }
424
Bjorn Bringertd2670652010-09-13 14:06:41 +0100425 private void saveShowWebSuggestions(ContentResolver cr) {
426 int value = showSearchSuggestions ? 1 : 0;
427 Settings.System.putInt(cr, Settings.System.SHOW_WEB_SUGGESTIONS, value);
428 }
429
430 private void updateShowWebSuggestions(ContentResolver cr, SharedPreferences p) {
431 showSearchSuggestions =
432 Settings.System.getInt(cr,
433 Settings.System.SHOW_WEB_SUGGESTIONS, 1) == 1;
434 p.edit().putBoolean(PREF_SHOW_SEARCH_SUGGESTIONS, showSearchSuggestions).commit();
435 }
436
The Android Open Source Project0c908882009-03-03 19:32:16 -0800437 public String getHomePage() {
438 return homeUrl;
439 }
440
Bjorn Bringertd2670652010-09-13 14:06:41 +0100441 public SearchEngine getSearchEngine() {
442 return searchEngine;
443 }
444
445 public boolean getShowSearchSuggestions() {
446 return showSearchSuggestions;
447 }
448
Feng Qianb3c02da2009-06-29 15:58:08 -0700449 public String getJsFlags() {
450 return jsFlags;
451 }
452
Andrei Popescu79e82b72009-07-27 12:01:59 +0100453 public WebStorageSizeManager getWebStorageSizeManager() {
454 return webStorageSizeManager;
455 }
456
The Android Open Source Project0c908882009-03-03 19:32:16 -0800457 public void setHomePage(Context context, String url) {
458 Editor ed = PreferenceManager.
459 getDefaultSharedPreferences(context).edit();
460 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700461 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800462 homeUrl = url;
463 }
464
The Android Open Source Project0c908882009-03-03 19:32:16 -0800465 public WebSettings.TextSize getTextSize() {
466 return textSize;
467 }
468
Grace Kloba2f830682009-06-25 11:08:53 -0700469 public WebSettings.ZoomDensity getDefaultZoom() {
470 return zoomDensity;
471 }
472
The Android Open Source Project0c908882009-03-03 19:32:16 -0800473 public boolean openInBackground() {
474 return openInBackground;
475 }
476
477 public boolean showSecurityWarnings() {
478 return showSecurityWarnings;
479 }
480
481 public boolean isTracing() {
482 return tracing;
483 }
484
485 public boolean isLightTouch() {
486 return lightTouch;
487 }
488
489 public boolean isNavDump() {
490 return navDump;
491 }
492
The Android Open Source Project0c908882009-03-03 19:32:16 -0800493 public boolean showDebugSettings() {
494 return showDebugSettings;
495 }
496
497 public void toggleDebugSettings() {
498 showDebugSettings = !showDebugSettings;
499 navDump = showDebugSettings;
500 update();
501 }
502
503 /**
504 * Add a WebSettings object to the list of observers that will be updated
505 * when update() is called.
506 *
507 * @param s A WebSettings object that is strictly tied to the life of a
508 * WebView.
509 */
510 public Observer addObserver(WebSettings s) {
511 Observer old = mWebSettingsToObservers.get(s);
512 if (old != null) {
513 super.deleteObserver(old);
514 }
515 Observer o = new Observer(s);
516 mWebSettingsToObservers.put(s, o);
517 super.addObserver(o);
518 return o;
519 }
520
521 /**
522 * Delete the given WebSettings observer from the list of observers.
523 * @param s The WebSettings object to be deleted.
524 */
525 public void deleteObserver(WebSettings s) {
526 Observer o = mWebSettingsToObservers.get(s);
527 if (o != null) {
528 mWebSettingsToObservers.remove(s);
529 super.deleteObserver(o);
530 }
531 }
532
533 /*
534 * Package level method for obtaining a single app instance of the
535 * BrowserSettings.
536 */
537 /*package*/ static BrowserSettings getInstance() {
538 if (sSingleton == null ) {
539 sSingleton = new BrowserSettings();
540 }
541 return sSingleton;
542 }
543
544 /*
545 * Package level method for associating the BrowserSettings with TabControl
546 */
547 /* package */void setTabControl(TabControl tabControl) {
548 mTabControl = tabControl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800549 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800550 }
551
552 /*
553 * Update all the observers of the object.
554 */
555 /*package*/ void update() {
556 setChanged();
557 notifyObservers();
558 }
559
560 /*package*/ void clearCache(Context context) {
561 WebIconDatabase.getInstance().removeAllIcons();
562 if (mTabControl != null) {
563 WebView current = mTabControl.getCurrentWebView();
564 if (current != null) {
565 current.clearCache(true);
566 }
567 }
568 }
569
570 /*package*/ void clearCookies(Context context) {
571 CookieManager.getInstance().removeAllCookie();
572 }
573
574 /* package */void clearHistory(Context context) {
575 ContentResolver resolver = context.getContentResolver();
576 Browser.clearHistory(resolver);
577 Browser.clearSearches(resolver);
578 }
579
580 /* package */ void clearFormData(Context context) {
581 WebViewDatabase.getInstance(context).clearFormData();
582 if (mTabControl != null) {
Henrik Baard794dc722010-02-19 16:28:06 +0100583 WebView currentTopView = mTabControl.getCurrentTopWebView();
584 if (currentTopView != null) {
585 currentTopView.clearFormData();
586 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800587 }
588 }
589
590 /*package*/ void clearPasswords(Context context) {
591 WebViewDatabase db = WebViewDatabase.getInstance(context);
592 db.clearUsernamePassword();
593 db.clearHttpAuthUsernamePassword();
594 }
595
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800596 private void updateTabControlSettings() {
597 // Enable/disable the error console.
598 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
599 showDebugSettings && showConsole);
600 mTabControl.getBrowserActivity().setRequestedOrientation(
601 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
602 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
603 }
604
Steve Blockf344d032009-07-30 10:50:45 +0100605 private void maybeDisableWebsiteSettings(Context context) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100606 PreferenceActivity activity = (PreferenceActivity) context;
607 final PreferenceScreen screen = (PreferenceScreen)
608 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
609 screen.setEnabled(false);
610 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
611 public void onReceiveValue(Map webStorageOrigins) {
612 if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) {
613 screen.setEnabled(true);
614 }
615 }
616 });
617
Steve Block2a6a0f42009-11-19 15:55:42 +0000618 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
619 public void onReceiveValue(Set<String> geolocationOrigins) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100620 if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) {
621 screen.setEnabled(true);
622 }
623 }
624 });
Steve Blockf344d032009-07-30 10:50:45 +0100625 }
626
Nicolas Roard78a98e42009-05-11 13:34:17 +0100627 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100628 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100629 maybeDisableWebsiteSettings(context);
630 }
631
632 /*package*/ void clearLocationAccess(Context context) {
633 GeolocationPermissions.getInstance().clearAll();
634 maybeDisableWebsiteSettings(context);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100635 }
636
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400637 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800638 reset();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800639 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400640 PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700641 p.edit().clear().apply();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400642 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800643 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700644 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700645 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100646 // reset appcache max size
647 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700648 }
649
650 private String getFactoryResetHomeUrl(Context context) {
651 String url = context.getResources().getString(R.string.homepage_base);
652 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500653 url = url.replace("{CID}",
654 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700655 }
656 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800657 }
658
The Android Open Source Project0c908882009-03-03 19:32:16 -0800659 // Private constructor that does nothing.
660 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800661 reset();
662 }
663
664 private void reset() {
665 // Private variables for settings
666 // NOTE: these defaults need to be kept in sync with the XML
667 // until the performance of PreferenceManager.setDefaultValues()
668 // is improved.
669 loadsImagesAutomatically = true;
670 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400671 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800672 javaScriptCanOpenWindowsAutomatically = false;
673 showSecurityWarnings = true;
674 rememberPasswords = true;
675 saveFormData = true;
676 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800677 autoFitPage = true;
678 landscapeOnly = false;
679 loadsPageInOverviewMode = true;
680 showDebugSettings = false;
681 // HTML5 API flags
682 appCacheEnabled = true;
683 databaseEnabled = true;
684 domStorageEnabled = true;
685 geolocationEnabled = true;
686 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800687 }
688}