blob: 3791eb07f1cffec4c7254ef670cd883dda847195 [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);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100232
Andrei Popescu01068702009-08-03 16:03:24 +0100233 // HTML5 API flags
234 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100235 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100236 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100237 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100238 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100239
Andrei Popescu01068702009-08-03 16:03:24 +0100240 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100241 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100242 s.setAppCachePath(b.appCachePath);
243 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100244 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100245
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800246 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800247 }
248 }
249
250 /**
251 * Load settings from the browser app's database.
252 * NOTE: Strings used for the preferences must match those specified
253 * in the browser_preferences.xml
254 * @param ctx A Context object used to query the browser's settings
255 * database. If the database exists, the saved settings will be
256 * stored in this BrowserSettings object. This will update all
257 * observers of this object.
258 */
Bjorn Bringertd2670652010-09-13 14:06:41 +0100259 public void loadFromDb(final Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800260 SharedPreferences p =
261 PreferenceManager.getDefaultSharedPreferences(ctx);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100262 // Set the default value for the Application Caches path.
263 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100264 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100265 webStorageSizeManager = new WebStorageSizeManager(
266 ctx,
267 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
268 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100269 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100270 // Set the default value for the Database path.
271 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100272 // Set the default value for the Geolocation database path.
273 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800274
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700275 if (p.getString(PREF_HOMEPAGE, "") == "") {
276 // No home page preferences is set, set it to default.
277 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
278 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700279
Grace Kloba9804c432009-12-02 11:07:40 -0800280 // the cost of one cached page is ~3M (measured using nytimes.com). For
281 // low end devices, we only cache one page. For high end devices, we try
282 // to cache more pages, currently choose 5.
283 ActivityManager am = (ActivityManager) ctx
284 .getSystemService(Context.ACTIVITY_SERVICE);
285 if (am.getMemoryClass() > 16) {
Andrei Popescuba676ef2010-03-29 12:12:55 +0100286 pageCacheCapacity = 5;
Grace Kloba9804c432009-12-02 11:07:40 -0800287 } else {
288 pageCacheCapacity = 1;
289 }
290
Bjorn Bringertd2670652010-09-13 14:06:41 +0100291 // Load the defaults from the xml
The Android Open Source Project0c908882009-03-03 19:32:16 -0800292 // This call is TOO SLOW, need to manually keep the defaults
293 // in sync
294 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
Bjorn Bringertd2670652010-09-13 14:06:41 +0100295 syncSharedPreferences(ctx, p);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800296 }
297
Bjorn Bringertd2670652010-09-13 14:06:41 +0100298 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700299
The Android Open Source Project0c908882009-03-03 19:32:16 -0800300 homeUrl =
301 p.getString(PREF_HOMEPAGE, homeUrl);
Leon Scroggins III31306602010-09-17 11:10:29 -0400302 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
303 SearchEngine.GOOGLE);
Bjorn Bringertd2670652010-09-13 14:06:41 +0100304 if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
305 if (searchEngine != null) {
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400306 if (searchEngine.supportsVoiceSearch()) {
307 // One or more tabs could have been in voice search mode.
308 // Clear it, since the new SearchEngine may not support
309 // it, or may handle it differently.
310 for (int i = 0; i < mTabControl.getTabCount(); i++) {
311 mTabControl.getTab(i).revertVoiceSearchMode();
312 }
313 }
Bjorn Bringertd2670652010-09-13 14:06:41 +0100314 searchEngine.close();
315 }
316 searchEngine = SearchEngines.get(ctx, searchEngineName);
317 }
318 Log.i(TAG, "Selected search engine: " + searchEngine);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700319
The Android Open Source Project0c908882009-03-03 19:32:16 -0800320 loadsImagesAutomatically = p.getBoolean("load_images",
321 loadsImagesAutomatically);
322 javaScriptEnabled = p.getBoolean("enable_javascript",
323 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400324 pluginState = WebSettings.PluginState.valueOf(
325 p.getString("plugin_state", pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800326 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
327 "block_popup_windows",
328 !javaScriptCanOpenWindowsAutomatically);
329 showSecurityWarnings = p.getBoolean("show_security_warnings",
330 showSecurityWarnings);
331 rememberPasswords = p.getBoolean("remember_passwords",
332 rememberPasswords);
333 saveFormData = p.getBoolean("save_formdata",
334 saveFormData);
335 boolean accept_cookies = p.getBoolean("accept_cookies",
336 CookieManager.getInstance().acceptCookie());
337 CookieManager.getInstance().setAcceptCookie(accept_cookies);
338 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800339 textSize = WebSettings.TextSize.valueOf(
340 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700341 zoomDensity = WebSettings.ZoomDensity.valueOf(
342 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800343 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700344 loadsPageInOverviewMode = p.getBoolean("load_page",
345 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700346 boolean landscapeOnlyTemp =
347 p.getBoolean("landscape_only", landscapeOnly);
348 if (landscapeOnlyTemp != landscapeOnly) {
349 landscapeOnly = landscapeOnlyTemp;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700350 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800351 useWideViewPort = true; // use wide view port for either setting
352 if (autoFitPage) {
353 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
354 } else {
355 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
356 }
357 defaultTextEncodingName =
358 p.getString(PREF_DEFAULT_TEXT_ENCODING,
359 defaultTextEncodingName);
360
361 showDebugSettings =
362 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
363 // Debug menu items have precidence if the menu is visible
364 if (showDebugSettings) {
365 boolean small_screen = p.getBoolean("small_screen",
366 layoutAlgorithm ==
367 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
368 if (small_screen) {
369 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
370 } else {
371 boolean normal_layout = p.getBoolean("normal_layout",
372 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
373 if (normal_layout) {
374 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
375 } else {
376 layoutAlgorithm =
377 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
378 }
379 }
380 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
381 tracing = p.getBoolean("enable_tracing", tracing);
382 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
383 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800384 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800385 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700386 // JS flags is loaded from DB even if showDebugSettings is false,
387 // so that it can be set once and be effective all the time.
388 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100389
390 // Read the setting for showing/hiding the JS Console always so that should the
391 // user enable debug settings, we already know if we should show the console.
392 // The user will never see the console unless they navigate to about:debug,
393 // regardless of the setting we read here. This setting is only used after debug
394 // is enabled.
395 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700396
Andrei Popescu01068702009-08-03 16:03:24 +0100397 // HTML5 API flags
398 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
399 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
400 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100401 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100402 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100403
Grace Klobad9ee1392009-07-20 11:53:33 -0700404 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800405 }
406
The Android Open Source Project0c908882009-03-03 19:32:16 -0800407 public String getHomePage() {
408 return homeUrl;
409 }
410
Bjorn Bringertd2670652010-09-13 14:06:41 +0100411 public SearchEngine getSearchEngine() {
412 return searchEngine;
413 }
414
Feng Qianb3c02da2009-06-29 15:58:08 -0700415 public String getJsFlags() {
416 return jsFlags;
417 }
418
Andrei Popescu79e82b72009-07-27 12:01:59 +0100419 public WebStorageSizeManager getWebStorageSizeManager() {
420 return webStorageSizeManager;
421 }
422
The Android Open Source Project0c908882009-03-03 19:32:16 -0800423 public void setHomePage(Context context, String url) {
424 Editor ed = PreferenceManager.
425 getDefaultSharedPreferences(context).edit();
426 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700427 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800428 homeUrl = url;
429 }
430
The Android Open Source Project0c908882009-03-03 19:32:16 -0800431 public WebSettings.TextSize getTextSize() {
432 return textSize;
433 }
434
Grace Kloba2f830682009-06-25 11:08:53 -0700435 public WebSettings.ZoomDensity getDefaultZoom() {
436 return zoomDensity;
437 }
438
The Android Open Source Project0c908882009-03-03 19:32:16 -0800439 public boolean openInBackground() {
440 return openInBackground;
441 }
442
443 public boolean showSecurityWarnings() {
444 return showSecurityWarnings;
445 }
446
447 public boolean isTracing() {
448 return tracing;
449 }
450
451 public boolean isLightTouch() {
452 return lightTouch;
453 }
454
455 public boolean isNavDump() {
456 return navDump;
457 }
458
The Android Open Source Project0c908882009-03-03 19:32:16 -0800459 public boolean showDebugSettings() {
460 return showDebugSettings;
461 }
462
463 public void toggleDebugSettings() {
464 showDebugSettings = !showDebugSettings;
465 navDump = showDebugSettings;
466 update();
467 }
468
469 /**
470 * Add a WebSettings object to the list of observers that will be updated
471 * when update() is called.
472 *
473 * @param s A WebSettings object that is strictly tied to the life of a
474 * WebView.
475 */
476 public Observer addObserver(WebSettings s) {
477 Observer old = mWebSettingsToObservers.get(s);
478 if (old != null) {
479 super.deleteObserver(old);
480 }
481 Observer o = new Observer(s);
482 mWebSettingsToObservers.put(s, o);
483 super.addObserver(o);
484 return o;
485 }
486
487 /**
488 * Delete the given WebSettings observer from the list of observers.
489 * @param s The WebSettings object to be deleted.
490 */
491 public void deleteObserver(WebSettings s) {
492 Observer o = mWebSettingsToObservers.get(s);
493 if (o != null) {
494 mWebSettingsToObservers.remove(s);
495 super.deleteObserver(o);
496 }
497 }
498
499 /*
500 * Package level method for obtaining a single app instance of the
501 * BrowserSettings.
502 */
503 /*package*/ static BrowserSettings getInstance() {
504 if (sSingleton == null ) {
505 sSingleton = new BrowserSettings();
506 }
507 return sSingleton;
508 }
509
510 /*
511 * Package level method for associating the BrowserSettings with TabControl
512 */
513 /* package */void setTabControl(TabControl tabControl) {
514 mTabControl = tabControl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800515 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800516 }
517
518 /*
519 * Update all the observers of the object.
520 */
521 /*package*/ void update() {
522 setChanged();
523 notifyObservers();
524 }
525
526 /*package*/ void clearCache(Context context) {
527 WebIconDatabase.getInstance().removeAllIcons();
528 if (mTabControl != null) {
529 WebView current = mTabControl.getCurrentWebView();
530 if (current != null) {
531 current.clearCache(true);
532 }
533 }
534 }
535
536 /*package*/ void clearCookies(Context context) {
537 CookieManager.getInstance().removeAllCookie();
538 }
539
540 /* package */void clearHistory(Context context) {
541 ContentResolver resolver = context.getContentResolver();
542 Browser.clearHistory(resolver);
543 Browser.clearSearches(resolver);
544 }
545
546 /* package */ void clearFormData(Context context) {
547 WebViewDatabase.getInstance(context).clearFormData();
548 if (mTabControl != null) {
Henrik Baard794dc722010-02-19 16:28:06 +0100549 WebView currentTopView = mTabControl.getCurrentTopWebView();
550 if (currentTopView != null) {
551 currentTopView.clearFormData();
552 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800553 }
554 }
555
556 /*package*/ void clearPasswords(Context context) {
557 WebViewDatabase db = WebViewDatabase.getInstance(context);
558 db.clearUsernamePassword();
559 db.clearHttpAuthUsernamePassword();
560 }
561
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800562 private void updateTabControlSettings() {
563 // Enable/disable the error console.
564 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
565 showDebugSettings && showConsole);
566 mTabControl.getBrowserActivity().setRequestedOrientation(
567 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
568 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
569 }
570
Steve Blockf344d032009-07-30 10:50:45 +0100571 private void maybeDisableWebsiteSettings(Context context) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100572 PreferenceActivity activity = (PreferenceActivity) context;
573 final PreferenceScreen screen = (PreferenceScreen)
574 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
575 screen.setEnabled(false);
576 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
577 public void onReceiveValue(Map webStorageOrigins) {
578 if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) {
579 screen.setEnabled(true);
580 }
581 }
582 });
583
Steve Block2a6a0f42009-11-19 15:55:42 +0000584 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
585 public void onReceiveValue(Set<String> geolocationOrigins) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100586 if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) {
587 screen.setEnabled(true);
588 }
589 }
590 });
Steve Blockf344d032009-07-30 10:50:45 +0100591 }
592
Nicolas Roard78a98e42009-05-11 13:34:17 +0100593 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100594 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100595 maybeDisableWebsiteSettings(context);
596 }
597
598 /*package*/ void clearLocationAccess(Context context) {
599 GeolocationPermissions.getInstance().clearAll();
600 maybeDisableWebsiteSettings(context);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100601 }
602
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400603 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800604 reset();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800605 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400606 PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700607 p.edit().clear().apply();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400608 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800609 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700610 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700611 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100612 // reset appcache max size
613 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700614 }
615
616 private String getFactoryResetHomeUrl(Context context) {
617 String url = context.getResources().getString(R.string.homepage_base);
618 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500619 url = url.replace("{CID}",
620 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700621 }
622 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800623 }
624
The Android Open Source Project0c908882009-03-03 19:32:16 -0800625 // Private constructor that does nothing.
626 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800627 reset();
628 }
629
630 private void reset() {
631 // Private variables for settings
632 // NOTE: these defaults need to be kept in sync with the XML
633 // until the performance of PreferenceManager.setDefaultValues()
634 // is improved.
635 loadsImagesAutomatically = true;
636 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400637 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800638 javaScriptCanOpenWindowsAutomatically = false;
639 showSecurityWarnings = true;
640 rememberPasswords = true;
641 saveFormData = true;
642 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800643 autoFitPage = true;
644 landscapeOnly = false;
645 loadsPageInOverviewMode = true;
646 showDebugSettings = false;
647 // HTML5 API flags
648 appCacheEnabled = true;
649 databaseEnabled = true;
650 domStorageEnabled = true;
651 geolocationEnabled = true;
652 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800653 }
654}