blob: a322b597afd0c73821794d2fb3c1656e0de8426e [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;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080083 private boolean autoFitPage;
84 private boolean landscapeOnly;
85 private boolean loadsPageInOverviewMode;
86 private boolean showDebugSettings;
Andrei Popescu01068702009-08-03 16:03:24 +010087 // HTML5 API flags
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080088 private boolean appCacheEnabled;
89 private boolean databaseEnabled;
90 private boolean domStorageEnabled;
91 private boolean geolocationEnabled;
92 private boolean workersEnabled; // only affects V8. JSC does not have a similar setting
Andrei Popescu01068702009-08-03 16:03:24 +010093 // HTML5 API configuration params
94 private long appCacheMaxSize = Long.MAX_VALUE;
95 private String appCachePath; // default value set in loadFromDb().
96 private String databasePath; // default value set in loadFromDb()
Steve Block5029cf02009-08-21 13:16:58 +010097 private String geolocationDatabasePath; // default value set in loadFromDb()
Andrei Popescu01068702009-08-03 16:03:24 +010098 private WebStorageSizeManager webStorageSizeManager;
99
100 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800101
Nicolas Roard78a98e42009-05-11 13:34:17 +0100102 private final static String TAG = "BrowserSettings";
103
The Android Open Source Project0c908882009-03-03 19:32:16 -0800104 // Development settings
105 public WebSettings.LayoutAlgorithm layoutAlgorithm =
106 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
107 private boolean useWideViewPort = true;
108 private int userAgent = 0;
109 private boolean tracing = false;
110 private boolean lightTouch = false;
111 private boolean navDump = false;
Feng Qianb3c02da2009-06-29 15:58:08 -0700112
Ben Murdochbff2d602009-07-01 20:19:05 +0100113 // By default the error console is shown once the user navigates to about:debug.
114 // The setting can be then toggled from the settings menu.
115 private boolean showConsole = true;
116
The Android Open Source Project0c908882009-03-03 19:32:16 -0800117 // Private preconfigured values
118 private static int minimumFontSize = 8;
119 private static int minimumLogicalFontSize = 8;
120 private static int defaultFontSize = 16;
121 private static int defaultFixedFontSize = 13;
122 private static WebSettings.TextSize textSize =
123 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700124 private static WebSettings.ZoomDensity zoomDensity =
125 WebSettings.ZoomDensity.MEDIUM;
Grace Kloba9804c432009-12-02 11:07:40 -0800126 private static int pageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800127
128 // Preference keys that are used outside this class
129 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
130 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
131 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
132 public final static String PREF_HOMEPAGE = "homepage";
Bjorn Bringertd2670652010-09-13 14:06:41 +0100133 public final static String PREF_SEARCH_ENGINE = "search_engine";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800134 public final static String PREF_CLEAR_FORM_DATA =
135 "privacy_clear_form_data";
136 public final static String PREF_CLEAR_PASSWORDS =
137 "privacy_clear_passwords";
138 public final static String PREF_EXTRAS_RESET_DEFAULTS =
139 "reset_default_preferences";
140 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100141 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800142 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700143 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800144 public final static String PREF_DEFAULT_TEXT_ENCODING =
145 "default_text_encoding";
Steve Blockc6f577f2009-08-20 18:11:08 +0100146 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
147 "privacy_clear_geolocation_access";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800148
149 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700150 "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
151 "like Gecko) Version/5.0 Safari/533.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800152
153 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700154 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
155 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
156
157 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
158 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
159 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
160
161 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
162 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
163 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800164
165 // Value to truncate strings when adding them to a TextView within
166 // a ListView
167 public final static int MAX_TEXTVIEW_LEN = 80;
168
169 private TabControl mTabControl;
170
171 // Single instance of the BrowserSettings for use in the Browser app.
172 private static BrowserSettings sSingleton;
173
174 // Private map of WebSettings to Observer objects used when deleting an
175 // observer.
176 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
177 new HashMap<WebSettings,Observer>();
178
179 /*
180 * An observer wrapper for updating a WebSettings object with the new
181 * settings after a call to BrowserSettings.update().
182 */
183 static class Observer implements java.util.Observer {
184 // Private WebSettings object that will be updated.
185 private WebSettings mSettings;
186
187 Observer(WebSettings w) {
188 mSettings = w;
189 }
190
191 public void update(Observable o, Object arg) {
192 BrowserSettings b = (BrowserSettings)o;
193 WebSettings s = mSettings;
194
195 s.setLayoutAlgorithm(b.layoutAlgorithm);
196 if (b.userAgent == 0) {
197 // use the default ua string
198 s.setUserAgentString(null);
199 } else if (b.userAgent == 1) {
200 s.setUserAgentString(DESKTOP_USERAGENT);
201 } else if (b.userAgent == 2) {
202 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700203 } else if (b.userAgent == 3) {
204 s.setUserAgentString(IPAD_USERAGENT);
205 } else if (b.userAgent == 4) {
206 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800207 }
208 s.setUseWideViewPort(b.useWideViewPort);
209 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
210 s.setJavaScriptEnabled(b.javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400211 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800212 s.setJavaScriptCanOpenWindowsAutomatically(
213 b.javaScriptCanOpenWindowsAutomatically);
214 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
215 s.setMinimumFontSize(b.minimumFontSize);
216 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
217 s.setDefaultFontSize(b.defaultFontSize);
218 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
219 s.setNavDump(b.navDump);
220 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700221 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800222 s.setLightTouchEnabled(b.lightTouch);
223 s.setSaveFormData(b.saveFormData);
224 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700225 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800226 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800227
228 // WebView inside Browser doesn't want initial focus to be set.
229 s.setNeedInitialFocus(false);
230 // Browser supports multiple windows
231 s.setSupportMultipleWindows(true);
Huahui Wu604a5982011-03-22 15:00:11 -0700232 // disable content url access
233 s.setAllowContentAccess(false);
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 // Load the defaults from the xml
The Android Open Source Project0c908882009-03-03 19:32:16 -0800294 // This call is TOO SLOW, need to manually keep the defaults
295 // in sync
296 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
Bjorn Bringertd2670652010-09-13 14:06:41 +0100297 syncSharedPreferences(ctx, p);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800298 }
299
Bjorn Bringertd2670652010-09-13 14:06:41 +0100300 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700301
The Android Open Source Project0c908882009-03-03 19:32:16 -0800302 homeUrl =
303 p.getString(PREF_HOMEPAGE, homeUrl);
Leon Scroggins III31306602010-09-17 11:10:29 -0400304 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
305 SearchEngine.GOOGLE);
Bjorn Bringertd2670652010-09-13 14:06:41 +0100306 if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
307 if (searchEngine != null) {
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400308 if (searchEngine.supportsVoiceSearch()) {
309 // One or more tabs could have been in voice search mode.
310 // Clear it, since the new SearchEngine may not support
311 // it, or may handle it differently.
312 for (int i = 0; i < mTabControl.getTabCount(); i++) {
313 mTabControl.getTab(i).revertVoiceSearchMode();
314 }
315 }
Bjorn Bringertd2670652010-09-13 14:06:41 +0100316 searchEngine.close();
317 }
318 searchEngine = SearchEngines.get(ctx, searchEngineName);
319 }
320 Log.i(TAG, "Selected search engine: " + searchEngine);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700321
The Android Open Source Project0c908882009-03-03 19:32:16 -0800322 loadsImagesAutomatically = p.getBoolean("load_images",
323 loadsImagesAutomatically);
324 javaScriptEnabled = p.getBoolean("enable_javascript",
325 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400326 pluginState = WebSettings.PluginState.valueOf(
327 p.getString("plugin_state", pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800328 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
329 "block_popup_windows",
330 !javaScriptCanOpenWindowsAutomatically);
331 showSecurityWarnings = p.getBoolean("show_security_warnings",
332 showSecurityWarnings);
333 rememberPasswords = p.getBoolean("remember_passwords",
334 rememberPasswords);
335 saveFormData = p.getBoolean("save_formdata",
336 saveFormData);
337 boolean accept_cookies = p.getBoolean("accept_cookies",
338 CookieManager.getInstance().acceptCookie());
339 CookieManager.getInstance().setAcceptCookie(accept_cookies);
340 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800341 textSize = WebSettings.TextSize.valueOf(
342 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700343 zoomDensity = WebSettings.ZoomDensity.valueOf(
344 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800345 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700346 loadsPageInOverviewMode = p.getBoolean("load_page",
347 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700348 boolean landscapeOnlyTemp =
349 p.getBoolean("landscape_only", landscapeOnly);
350 if (landscapeOnlyTemp != landscapeOnly) {
351 landscapeOnly = landscapeOnlyTemp;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700352 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800353 useWideViewPort = true; // use wide view port for either setting
354 if (autoFitPage) {
355 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
356 } else {
357 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
358 }
359 defaultTextEncodingName =
360 p.getString(PREF_DEFAULT_TEXT_ENCODING,
361 defaultTextEncodingName);
362
363 showDebugSettings =
364 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
365 // Debug menu items have precidence if the menu is visible
366 if (showDebugSettings) {
367 boolean small_screen = p.getBoolean("small_screen",
368 layoutAlgorithm ==
369 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
370 if (small_screen) {
371 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
372 } else {
373 boolean normal_layout = p.getBoolean("normal_layout",
374 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
375 if (normal_layout) {
376 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
377 } else {
378 layoutAlgorithm =
379 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
380 }
381 }
382 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
383 tracing = p.getBoolean("enable_tracing", tracing);
384 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
385 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800386 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800387 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700388 // JS flags is loaded from DB even if showDebugSettings is false,
389 // so that it can be set once and be effective all the time.
390 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100391
392 // Read the setting for showing/hiding the JS Console always so that should the
393 // user enable debug settings, we already know if we should show the console.
394 // The user will never see the console unless they navigate to about:debug,
395 // regardless of the setting we read here. This setting is only used after debug
396 // is enabled.
397 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700398
Andrei Popescu01068702009-08-03 16:03:24 +0100399 // HTML5 API flags
400 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
401 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
402 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100403 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100404 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100405
Grace Klobad9ee1392009-07-20 11:53:33 -0700406 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800407 }
408
The Android Open Source Project0c908882009-03-03 19:32:16 -0800409 public String getHomePage() {
410 return homeUrl;
411 }
412
Bjorn Bringertd2670652010-09-13 14:06:41 +0100413 public SearchEngine getSearchEngine() {
414 return searchEngine;
415 }
416
Feng Qianb3c02da2009-06-29 15:58:08 -0700417 public String getJsFlags() {
418 return jsFlags;
419 }
420
Andrei Popescu79e82b72009-07-27 12:01:59 +0100421 public WebStorageSizeManager getWebStorageSizeManager() {
422 return webStorageSizeManager;
423 }
424
The Android Open Source Project0c908882009-03-03 19:32:16 -0800425 public void setHomePage(Context context, String url) {
426 Editor ed = PreferenceManager.
427 getDefaultSharedPreferences(context).edit();
428 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700429 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800430 homeUrl = url;
431 }
432
The Android Open Source Project0c908882009-03-03 19:32:16 -0800433 public WebSettings.TextSize getTextSize() {
434 return textSize;
435 }
436
Grace Kloba2f830682009-06-25 11:08:53 -0700437 public WebSettings.ZoomDensity getDefaultZoom() {
438 return zoomDensity;
439 }
440
The Android Open Source Project0c908882009-03-03 19:32:16 -0800441 public boolean openInBackground() {
442 return openInBackground;
443 }
444
445 public boolean showSecurityWarnings() {
446 return showSecurityWarnings;
447 }
448
449 public boolean isTracing() {
450 return tracing;
451 }
452
453 public boolean isLightTouch() {
454 return lightTouch;
455 }
456
457 public boolean isNavDump() {
458 return navDump;
459 }
460
The Android Open Source Project0c908882009-03-03 19:32:16 -0800461 public boolean showDebugSettings() {
462 return showDebugSettings;
463 }
464
465 public void toggleDebugSettings() {
466 showDebugSettings = !showDebugSettings;
467 navDump = showDebugSettings;
468 update();
469 }
470
471 /**
472 * Add a WebSettings object to the list of observers that will be updated
473 * when update() is called.
474 *
475 * @param s A WebSettings object that is strictly tied to the life of a
476 * WebView.
477 */
478 public Observer addObserver(WebSettings s) {
479 Observer old = mWebSettingsToObservers.get(s);
480 if (old != null) {
481 super.deleteObserver(old);
482 }
483 Observer o = new Observer(s);
484 mWebSettingsToObservers.put(s, o);
485 super.addObserver(o);
486 return o;
487 }
488
489 /**
490 * Delete the given WebSettings observer from the list of observers.
491 * @param s The WebSettings object to be deleted.
492 */
493 public void deleteObserver(WebSettings s) {
494 Observer o = mWebSettingsToObservers.get(s);
495 if (o != null) {
496 mWebSettingsToObservers.remove(s);
497 super.deleteObserver(o);
498 }
499 }
500
501 /*
502 * Package level method for obtaining a single app instance of the
503 * BrowserSettings.
504 */
505 /*package*/ static BrowserSettings getInstance() {
506 if (sSingleton == null ) {
507 sSingleton = new BrowserSettings();
508 }
509 return sSingleton;
510 }
511
512 /*
513 * Package level method for associating the BrowserSettings with TabControl
514 */
515 /* package */void setTabControl(TabControl tabControl) {
516 mTabControl = tabControl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800517 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800518 }
519
520 /*
521 * Update all the observers of the object.
522 */
523 /*package*/ void update() {
524 setChanged();
525 notifyObservers();
526 }
527
528 /*package*/ void clearCache(Context context) {
529 WebIconDatabase.getInstance().removeAllIcons();
530 if (mTabControl != null) {
531 WebView current = mTabControl.getCurrentWebView();
532 if (current != null) {
533 current.clearCache(true);
534 }
535 }
536 }
537
538 /*package*/ void clearCookies(Context context) {
539 CookieManager.getInstance().removeAllCookie();
540 }
541
542 /* package */void clearHistory(Context context) {
543 ContentResolver resolver = context.getContentResolver();
544 Browser.clearHistory(resolver);
545 Browser.clearSearches(resolver);
546 }
547
548 /* package */ void clearFormData(Context context) {
549 WebViewDatabase.getInstance(context).clearFormData();
550 if (mTabControl != null) {
Henrik Baard794dc722010-02-19 16:28:06 +0100551 WebView currentTopView = mTabControl.getCurrentTopWebView();
552 if (currentTopView != null) {
553 currentTopView.clearFormData();
554 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800555 }
556 }
557
558 /*package*/ void clearPasswords(Context context) {
559 WebViewDatabase db = WebViewDatabase.getInstance(context);
560 db.clearUsernamePassword();
561 db.clearHttpAuthUsernamePassword();
562 }
563
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800564 private void updateTabControlSettings() {
565 // Enable/disable the error console.
566 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
567 showDebugSettings && showConsole);
568 mTabControl.getBrowserActivity().setRequestedOrientation(
569 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
570 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
571 }
572
Steve Blockf344d032009-07-30 10:50:45 +0100573 private void maybeDisableWebsiteSettings(Context context) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100574 PreferenceActivity activity = (PreferenceActivity) context;
575 final PreferenceScreen screen = (PreferenceScreen)
576 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
577 screen.setEnabled(false);
578 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
579 public void onReceiveValue(Map webStorageOrigins) {
580 if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) {
581 screen.setEnabled(true);
582 }
583 }
584 });
585
Steve Block2a6a0f42009-11-19 15:55:42 +0000586 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
587 public void onReceiveValue(Set<String> geolocationOrigins) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100588 if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) {
589 screen.setEnabled(true);
590 }
591 }
592 });
Steve Blockf344d032009-07-30 10:50:45 +0100593 }
594
Nicolas Roard78a98e42009-05-11 13:34:17 +0100595 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100596 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100597 maybeDisableWebsiteSettings(context);
598 }
599
600 /*package*/ void clearLocationAccess(Context context) {
601 GeolocationPermissions.getInstance().clearAll();
602 maybeDisableWebsiteSettings(context);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100603 }
604
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400605 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800606 reset();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800607 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400608 PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700609 p.edit().clear().apply();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400610 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800611 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700612 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700613 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100614 // reset appcache max size
615 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700616 }
617
618 private String getFactoryResetHomeUrl(Context context) {
619 String url = context.getResources().getString(R.string.homepage_base);
620 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500621 url = url.replace("{CID}",
622 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700623 }
624 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800625 }
626
The Android Open Source Project0c908882009-03-03 19:32:16 -0800627 // Private constructor that does nothing.
628 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800629 reset();
630 }
631
632 private void reset() {
633 // Private variables for settings
634 // NOTE: these defaults need to be kept in sync with the XML
635 // until the performance of PreferenceManager.setDefaultValues()
636 // is improved.
637 loadsImagesAutomatically = true;
638 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400639 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800640 javaScriptCanOpenWindowsAutomatically = false;
641 showSecurityWarnings = true;
642 rememberPasswords = true;
643 saveFormData = true;
644 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800645 autoFitPage = true;
646 landscapeOnly = false;
647 loadsPageInOverviewMode = true;
648 showDebugSettings = false;
649 // HTML5 API flags
650 appCacheEnabled = true;
651 databaseEnabled = true;
652 domStorageEnabled = true;
653 geolocationEnabled = true;
654 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800655 }
656}