blob: 5a7dd0db8336c6f64b57f935595839dc047ed088 [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
Grace Kloba9804c432009-12-02 11:07:40 -080020import android.app.ActivityManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.content.ContentResolver;
22import android.content.Context;
23import android.content.pm.ActivityInfo;
24import android.content.SharedPreferences;
25import android.content.SharedPreferences.Editor;
Jeff Davidson43610292010-07-16 16:03:58 -070026import android.net.Uri;
Nicolas Roard78a98e42009-05-11 13:34:17 +010027import android.preference.PreferenceActivity;
28import android.preference.PreferenceScreen;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010030import android.webkit.GeolocationPermissions;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010031import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.webkit.WebView;
33import android.webkit.WebViewDatabase;
34import android.webkit.WebIconDatabase;
35import android.webkit.WebSettings;
Nicolas Roard78a98e42009-05-11 13:34:17 +010036import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.preference.PreferenceManager;
38import android.provider.Browser;
39
40import java.util.HashMap;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010041import java.util.Map;
42import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import java.util.Observable;
44
45/*
46 * Package level class for storing various WebView and Browser settings. To use
47 * this class:
48 * BrowserSettings s = BrowserSettings.getInstance();
49 * s.addObserver(webView.getSettings());
50 * s.loadFromDb(context); // Only needed on app startup
51 * s.javaScriptEnabled = true;
52 * ... // set any other settings
53 * s.update(); // this will update all the observers
54 *
55 * To remove an observer:
56 * s.deleteObserver(webView.getSettings());
57 */
58class BrowserSettings extends Observable {
59
Leon Scroggins9ac587d2009-04-20 12:53:46 -040060 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080061 // NOTE: these defaults need to be kept in sync with the XML
62 // until the performance of PreferenceManager.setDefaultValues()
63 // is improved.
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080064 // Note: boolean variables are set inside reset function.
65 private boolean loadsImagesAutomatically;
66 private boolean javaScriptEnabled;
Patrick Scotte536b332010-03-22 10:19:32 -040067 private WebSettings.PluginState pluginState;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080068 private boolean javaScriptCanOpenWindowsAutomatically;
69 private boolean showSecurityWarnings;
70 private boolean rememberPasswords;
71 private boolean saveFormData;
72 private boolean openInBackground;
The Android Open Source Project0c908882009-03-03 19:32:16 -080073 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070074 private String homeUrl = "";
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080075 private boolean autoFitPage;
76 private boolean landscapeOnly;
77 private boolean loadsPageInOverviewMode;
78 private boolean showDebugSettings;
Andrei Popescu01068702009-08-03 16:03:24 +010079 // HTML5 API flags
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080080 private boolean appCacheEnabled;
81 private boolean databaseEnabled;
82 private boolean domStorageEnabled;
83 private boolean geolocationEnabled;
84 private boolean workersEnabled; // only affects V8. JSC does not have a similar setting
Andrei Popescu01068702009-08-03 16:03:24 +010085 // HTML5 API configuration params
86 private long appCacheMaxSize = Long.MAX_VALUE;
87 private String appCachePath; // default value set in loadFromDb().
88 private String databasePath; // default value set in loadFromDb()
Steve Block5029cf02009-08-21 13:16:58 +010089 private String geolocationDatabasePath; // default value set in loadFromDb()
Andrei Popescu01068702009-08-03 16:03:24 +010090 private WebStorageSizeManager webStorageSizeManager;
91
92 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -080093
Nicolas Roard78a98e42009-05-11 13:34:17 +010094 private final static String TAG = "BrowserSettings";
95
The Android Open Source Project0c908882009-03-03 19:32:16 -080096 // Development settings
97 public WebSettings.LayoutAlgorithm layoutAlgorithm =
98 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
99 private boolean useWideViewPort = true;
100 private int userAgent = 0;
101 private boolean tracing = false;
102 private boolean lightTouch = false;
103 private boolean navDump = false;
Feng Qianb3c02da2009-06-29 15:58:08 -0700104
Ben Murdochbff2d602009-07-01 20:19:05 +0100105 // By default the error console is shown once the user navigates to about:debug.
106 // The setting can be then toggled from the settings menu.
107 private boolean showConsole = true;
108
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109 // Private preconfigured values
Shimeng (Simon) Wangb4fb25c2010-07-13 15:18:10 -0700110 private static int minimumFontSize = 1;
111 private static int minimumLogicalFontSize = 1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800112 private static int defaultFontSize = 16;
113 private static int defaultFixedFontSize = 13;
114 private static WebSettings.TextSize textSize =
115 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700116 private static WebSettings.ZoomDensity zoomDensity =
117 WebSettings.ZoomDensity.MEDIUM;
Grace Kloba9804c432009-12-02 11:07:40 -0800118 private static int pageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119
120 // Preference keys that are used outside this class
121 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
122 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
123 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
124 public final static String PREF_HOMEPAGE = "homepage";
125 public final static String PREF_CLEAR_FORM_DATA =
126 "privacy_clear_form_data";
127 public final static String PREF_CLEAR_PASSWORDS =
128 "privacy_clear_passwords";
129 public final static String PREF_EXTRAS_RESET_DEFAULTS =
130 "reset_default_preferences";
131 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100132 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800133 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700134 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800135 public final static String PREF_DEFAULT_TEXT_ENCODING =
136 "default_text_encoding";
Steve Blockc6f577f2009-08-20 18:11:08 +0100137 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
138 "privacy_clear_geolocation_access";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800139
140 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700141 "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
142 "like Gecko) Version/5.0 Safari/533.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800143
144 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700145 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
146 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
147
148 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
149 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
150 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
151
152 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
153 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
154 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800155
156 // Value to truncate strings when adding them to a TextView within
157 // a ListView
158 public final static int MAX_TEXTVIEW_LEN = 80;
159
Jeff Davidson43610292010-07-16 16:03:58 -0700160 public static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider";
161
162 public static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/");
163
The Android Open Source Project0c908882009-03-03 19:32:16 -0800164 private TabControl mTabControl;
165
166 // Single instance of the BrowserSettings for use in the Browser app.
167 private static BrowserSettings sSingleton;
168
169 // Private map of WebSettings to Observer objects used when deleting an
170 // observer.
171 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
172 new HashMap<WebSettings,Observer>();
173
174 /*
175 * An observer wrapper for updating a WebSettings object with the new
176 * settings after a call to BrowserSettings.update().
177 */
178 static class Observer implements java.util.Observer {
179 // Private WebSettings object that will be updated.
180 private WebSettings mSettings;
181
182 Observer(WebSettings w) {
183 mSettings = w;
184 }
185
186 public void update(Observable o, Object arg) {
187 BrowserSettings b = (BrowserSettings)o;
188 WebSettings s = mSettings;
189
190 s.setLayoutAlgorithm(b.layoutAlgorithm);
191 if (b.userAgent == 0) {
192 // use the default ua string
193 s.setUserAgentString(null);
194 } else if (b.userAgent == 1) {
195 s.setUserAgentString(DESKTOP_USERAGENT);
196 } else if (b.userAgent == 2) {
197 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700198 } else if (b.userAgent == 3) {
199 s.setUserAgentString(IPAD_USERAGENT);
200 } else if (b.userAgent == 4) {
201 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800202 }
203 s.setUseWideViewPort(b.useWideViewPort);
204 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
205 s.setJavaScriptEnabled(b.javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400206 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800207 s.setJavaScriptCanOpenWindowsAutomatically(
208 b.javaScriptCanOpenWindowsAutomatically);
209 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
210 s.setMinimumFontSize(b.minimumFontSize);
211 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
212 s.setDefaultFontSize(b.defaultFontSize);
213 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
214 s.setNavDump(b.navDump);
215 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700216 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800217 s.setLightTouchEnabled(b.lightTouch);
218 s.setSaveFormData(b.saveFormData);
219 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700220 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800221 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800222
223 // WebView inside Browser doesn't want initial focus to be set.
224 s.setNeedInitialFocus(false);
225 // Browser supports multiple windows
226 s.setSupportMultipleWindows(true);
Grace Kloba61fc77c2010-06-22 09:57:33 -0700227 // enable smooth transition for better performance during panning or
228 // zooming
229 s.setEnableSmoothTransition(true);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100230
Andrei Popescu01068702009-08-03 16:03:24 +0100231 // HTML5 API flags
232 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100233 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100234 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100235 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100236 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100237
Andrei Popescu01068702009-08-03 16:03:24 +0100238 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100239 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100240 s.setAppCachePath(b.appCachePath);
241 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100242 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100243
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800244 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800245 }
246 }
247
248 /**
249 * Load settings from the browser app's database.
250 * NOTE: Strings used for the preferences must match those specified
251 * in the browser_preferences.xml
252 * @param ctx A Context object used to query the browser's settings
253 * database. If the database exists, the saved settings will be
254 * stored in this BrowserSettings object. This will update all
255 * observers of this object.
256 */
257 public void loadFromDb(Context ctx) {
258 SharedPreferences p =
259 PreferenceManager.getDefaultSharedPreferences(ctx);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100260 // Set the default value for the Application Caches path.
261 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100262 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100263 webStorageSizeManager = new WebStorageSizeManager(
264 ctx,
265 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
266 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100267 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100268 // Set the default value for the Database path.
269 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100270 // Set the default value for the Geolocation database path.
271 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800272
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700273 if (p.getString(PREF_HOMEPAGE, "") == "") {
274 // No home page preferences is set, set it to default.
275 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
276 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700277
Grace Kloba9804c432009-12-02 11:07:40 -0800278 // the cost of one cached page is ~3M (measured using nytimes.com). For
279 // low end devices, we only cache one page. For high end devices, we try
280 // to cache more pages, currently choose 5.
281 ActivityManager am = (ActivityManager) ctx
282 .getSystemService(Context.ACTIVITY_SERVICE);
283 if (am.getMemoryClass() > 16) {
Andrei Popescuba676ef2010-03-29 12:12:55 +0100284 pageCacheCapacity = 5;
Grace Kloba9804c432009-12-02 11:07:40 -0800285 } else {
286 pageCacheCapacity = 1;
287 }
288
The Android Open Source Project0c908882009-03-03 19:32:16 -0800289 // Load the defaults from the xml
290 // This call is TOO SLOW, need to manually keep the defaults
291 // in sync
292 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
293 syncSharedPreferences(p);
294 }
295
296 /* package */ void syncSharedPreferences(SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700297
The Android Open Source Project0c908882009-03-03 19:32:16 -0800298 homeUrl =
299 p.getString(PREF_HOMEPAGE, homeUrl);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700300
The Android Open Source Project0c908882009-03-03 19:32:16 -0800301 loadsImagesAutomatically = p.getBoolean("load_images",
302 loadsImagesAutomatically);
303 javaScriptEnabled = p.getBoolean("enable_javascript",
304 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400305 pluginState = WebSettings.PluginState.valueOf(
306 p.getString("plugin_state", pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800307 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
308 "block_popup_windows",
309 !javaScriptCanOpenWindowsAutomatically);
310 showSecurityWarnings = p.getBoolean("show_security_warnings",
311 showSecurityWarnings);
312 rememberPasswords = p.getBoolean("remember_passwords",
313 rememberPasswords);
314 saveFormData = p.getBoolean("save_formdata",
315 saveFormData);
316 boolean accept_cookies = p.getBoolean("accept_cookies",
317 CookieManager.getInstance().acceptCookie());
318 CookieManager.getInstance().setAcceptCookie(accept_cookies);
319 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800320 textSize = WebSettings.TextSize.valueOf(
321 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700322 zoomDensity = WebSettings.ZoomDensity.valueOf(
323 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800324 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700325 loadsPageInOverviewMode = p.getBoolean("load_page",
326 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700327 boolean landscapeOnlyTemp =
328 p.getBoolean("landscape_only", landscapeOnly);
329 if (landscapeOnlyTemp != landscapeOnly) {
330 landscapeOnly = landscapeOnlyTemp;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700331 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800332 useWideViewPort = true; // use wide view port for either setting
333 if (autoFitPage) {
334 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
335 } else {
336 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
337 }
338 defaultTextEncodingName =
339 p.getString(PREF_DEFAULT_TEXT_ENCODING,
340 defaultTextEncodingName);
341
342 showDebugSettings =
343 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
344 // Debug menu items have precidence if the menu is visible
345 if (showDebugSettings) {
346 boolean small_screen = p.getBoolean("small_screen",
347 layoutAlgorithm ==
348 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
349 if (small_screen) {
350 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
351 } else {
352 boolean normal_layout = p.getBoolean("normal_layout",
353 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
354 if (normal_layout) {
355 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
356 } else {
357 layoutAlgorithm =
358 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
359 }
360 }
361 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
362 tracing = p.getBoolean("enable_tracing", tracing);
363 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
364 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800365 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800366 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700367 // JS flags is loaded from DB even if showDebugSettings is false,
368 // so that it can be set once and be effective all the time.
369 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100370
371 // Read the setting for showing/hiding the JS Console always so that should the
372 // user enable debug settings, we already know if we should show the console.
373 // The user will never see the console unless they navigate to about:debug,
374 // regardless of the setting we read here. This setting is only used after debug
375 // is enabled.
376 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700377
Andrei Popescu01068702009-08-03 16:03:24 +0100378 // HTML5 API flags
379 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
380 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
381 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100382 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100383 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100384
Grace Klobad9ee1392009-07-20 11:53:33 -0700385 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800386 }
387
The Android Open Source Project0c908882009-03-03 19:32:16 -0800388 public String getHomePage() {
389 return homeUrl;
390 }
391
Feng Qianb3c02da2009-06-29 15:58:08 -0700392 public String getJsFlags() {
393 return jsFlags;
394 }
395
Andrei Popescu79e82b72009-07-27 12:01:59 +0100396 public WebStorageSizeManager getWebStorageSizeManager() {
397 return webStorageSizeManager;
398 }
399
The Android Open Source Project0c908882009-03-03 19:32:16 -0800400 public void setHomePage(Context context, String url) {
401 Editor ed = PreferenceManager.
402 getDefaultSharedPreferences(context).edit();
403 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700404 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800405 homeUrl = url;
406 }
407
The Android Open Source Project0c908882009-03-03 19:32:16 -0800408 public WebSettings.TextSize getTextSize() {
409 return textSize;
410 }
411
Grace Kloba2f830682009-06-25 11:08:53 -0700412 public WebSettings.ZoomDensity getDefaultZoom() {
413 return zoomDensity;
414 }
415
The Android Open Source Project0c908882009-03-03 19:32:16 -0800416 public boolean openInBackground() {
417 return openInBackground;
418 }
419
420 public boolean showSecurityWarnings() {
421 return showSecurityWarnings;
422 }
423
424 public boolean isTracing() {
425 return tracing;
426 }
427
428 public boolean isLightTouch() {
429 return lightTouch;
430 }
431
432 public boolean isNavDump() {
433 return navDump;
434 }
435
The Android Open Source Project0c908882009-03-03 19:32:16 -0800436 public boolean showDebugSettings() {
437 return showDebugSettings;
438 }
439
440 public void toggleDebugSettings() {
441 showDebugSettings = !showDebugSettings;
442 navDump = showDebugSettings;
443 update();
444 }
445
446 /**
447 * Add a WebSettings object to the list of observers that will be updated
448 * when update() is called.
449 *
450 * @param s A WebSettings object that is strictly tied to the life of a
451 * WebView.
452 */
453 public Observer addObserver(WebSettings s) {
454 Observer old = mWebSettingsToObservers.get(s);
455 if (old != null) {
456 super.deleteObserver(old);
457 }
458 Observer o = new Observer(s);
459 mWebSettingsToObservers.put(s, o);
460 super.addObserver(o);
461 return o;
462 }
463
464 /**
465 * Delete the given WebSettings observer from the list of observers.
466 * @param s The WebSettings object to be deleted.
467 */
468 public void deleteObserver(WebSettings s) {
469 Observer o = mWebSettingsToObservers.get(s);
470 if (o != null) {
471 mWebSettingsToObservers.remove(s);
472 super.deleteObserver(o);
473 }
474 }
475
476 /*
477 * Package level method for obtaining a single app instance of the
478 * BrowserSettings.
479 */
480 /*package*/ static BrowserSettings getInstance() {
481 if (sSingleton == null ) {
482 sSingleton = new BrowserSettings();
483 }
484 return sSingleton;
485 }
486
487 /*
488 * Package level method for associating the BrowserSettings with TabControl
489 */
490 /* package */void setTabControl(TabControl tabControl) {
491 mTabControl = tabControl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800492 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800493 }
494
495 /*
496 * Update all the observers of the object.
497 */
498 /*package*/ void update() {
499 setChanged();
500 notifyObservers();
501 }
502
503 /*package*/ void clearCache(Context context) {
504 WebIconDatabase.getInstance().removeAllIcons();
505 if (mTabControl != null) {
506 WebView current = mTabControl.getCurrentWebView();
507 if (current != null) {
508 current.clearCache(true);
509 }
510 }
511 }
512
513 /*package*/ void clearCookies(Context context) {
514 CookieManager.getInstance().removeAllCookie();
515 }
516
517 /* package */void clearHistory(Context context) {
518 ContentResolver resolver = context.getContentResolver();
519 Browser.clearHistory(resolver);
520 Browser.clearSearches(resolver);
521 }
522
523 /* package */ void clearFormData(Context context) {
524 WebViewDatabase.getInstance(context).clearFormData();
525 if (mTabControl != null) {
Henrik Baard794dc722010-02-19 16:28:06 +0100526 WebView currentTopView = mTabControl.getCurrentTopWebView();
527 if (currentTopView != null) {
528 currentTopView.clearFormData();
529 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800530 }
531 }
532
533 /*package*/ void clearPasswords(Context context) {
534 WebViewDatabase db = WebViewDatabase.getInstance(context);
535 db.clearUsernamePassword();
536 db.clearHttpAuthUsernamePassword();
537 }
538
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800539 private void updateTabControlSettings() {
540 // Enable/disable the error console.
541 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
542 showDebugSettings && showConsole);
543 mTabControl.getBrowserActivity().setRequestedOrientation(
544 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
545 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
546 }
547
Steve Blockf344d032009-07-30 10:50:45 +0100548 private void maybeDisableWebsiteSettings(Context context) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100549 PreferenceActivity activity = (PreferenceActivity) context;
550 final PreferenceScreen screen = (PreferenceScreen)
551 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
552 screen.setEnabled(false);
553 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
554 public void onReceiveValue(Map webStorageOrigins) {
555 if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) {
556 screen.setEnabled(true);
557 }
558 }
559 });
560
Steve Block2a6a0f42009-11-19 15:55:42 +0000561 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
562 public void onReceiveValue(Set<String> geolocationOrigins) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100563 if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) {
564 screen.setEnabled(true);
565 }
566 }
567 });
Steve Blockf344d032009-07-30 10:50:45 +0100568 }
569
Nicolas Roard78a98e42009-05-11 13:34:17 +0100570 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100571 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100572 maybeDisableWebsiteSettings(context);
573 }
574
575 /*package*/ void clearLocationAccess(Context context) {
576 GeolocationPermissions.getInstance().clearAll();
577 maybeDisableWebsiteSettings(context);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100578 }
579
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400580 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800581 reset();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800582 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400583 PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700584 p.edit().clear().apply();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400585 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800586 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700587 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700588 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100589 // reset appcache max size
590 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700591 }
592
593 private String getFactoryResetHomeUrl(Context context) {
594 String url = context.getResources().getString(R.string.homepage_base);
595 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500596 url = url.replace("{CID}",
597 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700598 }
599 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800600 }
601
The Android Open Source Project0c908882009-03-03 19:32:16 -0800602 // Private constructor that does nothing.
603 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800604 reset();
605 }
606
607 private void reset() {
608 // Private variables for settings
609 // NOTE: these defaults need to be kept in sync with the XML
610 // until the performance of PreferenceManager.setDefaultValues()
611 // is improved.
612 loadsImagesAutomatically = true;
613 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400614 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800615 javaScriptCanOpenWindowsAutomatically = false;
616 showSecurityWarnings = true;
617 rememberPasswords = true;
618 saveFormData = true;
619 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800620 autoFitPage = true;
621 landscapeOnly = false;
622 loadsPageInOverviewMode = true;
623 showDebugSettings = false;
624 // HTML5 API flags
625 appCacheEnabled = true;
626 databaseEnabled = true;
627 domStorageEnabled = true;
628 geolocationEnabled = true;
629 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800630 }
631}