blob: 1c534a20f77a54b77aafeb94882db3b3c863f943 [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;
Nicolas Roard78a98e42009-05-11 13:34:17 +010026import android.preference.PreferenceActivity;
27import android.preference.PreferenceScreen;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010029import android.webkit.GeolocationPermissions;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010030import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.webkit.WebView;
32import android.webkit.WebViewDatabase;
33import android.webkit.WebIconDatabase;
34import android.webkit.WebSettings;
Nicolas Roard78a98e42009-05-11 13:34:17 +010035import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.preference.PreferenceManager;
37import android.provider.Browser;
38
39import java.util.HashMap;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010040import java.util.Map;
41import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080042import java.util.Observable;
43
44/*
45 * Package level class for storing various WebView and Browser settings. To use
46 * this class:
47 * BrowserSettings s = BrowserSettings.getInstance();
48 * s.addObserver(webView.getSettings());
49 * s.loadFromDb(context); // Only needed on app startup
50 * s.javaScriptEnabled = true;
51 * ... // set any other settings
52 * s.update(); // this will update all the observers
53 *
54 * To remove an observer:
55 * s.deleteObserver(webView.getSettings());
56 */
57class BrowserSettings extends Observable {
58
Leon Scroggins9ac587d2009-04-20 12:53:46 -040059 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080060 // NOTE: these defaults need to be kept in sync with the XML
61 // until the performance of PreferenceManager.setDefaultValues()
62 // is improved.
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080063 // Note: boolean variables are set inside reset function.
64 private boolean loadsImagesAutomatically;
65 private boolean javaScriptEnabled;
66 private boolean pluginsEnabled;
67 private boolean javaScriptCanOpenWindowsAutomatically;
68 private boolean showSecurityWarnings;
69 private boolean rememberPasswords;
70 private boolean saveFormData;
71 private boolean openInBackground;
The Android Open Source Project0c908882009-03-03 19:32:16 -080072 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070073 private String homeUrl = "";
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080074 private boolean loginInitialized;
75 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
110 private static int minimumFontSize = 8;
111 private static int minimumLogicalFontSize = 8;
112 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; " +
Grace Klobaa4fa6ee2009-06-26 00:34:46 -0700141 "U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, " +
142 "like Gecko) Version/4.0 Safari/530.17";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800143
144 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Grace Klobaa4fa6ee2009-06-26 00:34:46 -0700145 "CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 " +
146 "(KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800147
148 // Value to truncate strings when adding them to a TextView within
149 // a ListView
150 public final static int MAX_TEXTVIEW_LEN = 80;
151
152 private TabControl mTabControl;
153
154 // Single instance of the BrowserSettings for use in the Browser app.
155 private static BrowserSettings sSingleton;
156
157 // Private map of WebSettings to Observer objects used when deleting an
158 // observer.
159 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
160 new HashMap<WebSettings,Observer>();
161
162 /*
163 * An observer wrapper for updating a WebSettings object with the new
164 * settings after a call to BrowserSettings.update().
165 */
166 static class Observer implements java.util.Observer {
167 // Private WebSettings object that will be updated.
168 private WebSettings mSettings;
169
170 Observer(WebSettings w) {
171 mSettings = w;
172 }
173
174 public void update(Observable o, Object arg) {
175 BrowserSettings b = (BrowserSettings)o;
176 WebSettings s = mSettings;
177
178 s.setLayoutAlgorithm(b.layoutAlgorithm);
179 if (b.userAgent == 0) {
180 // use the default ua string
181 s.setUserAgentString(null);
182 } else if (b.userAgent == 1) {
183 s.setUserAgentString(DESKTOP_USERAGENT);
184 } else if (b.userAgent == 2) {
185 s.setUserAgentString(IPHONE_USERAGENT);
186 }
187 s.setUseWideViewPort(b.useWideViewPort);
188 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
189 s.setJavaScriptEnabled(b.javaScriptEnabled);
190 s.setPluginsEnabled(b.pluginsEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800191 s.setJavaScriptCanOpenWindowsAutomatically(
192 b.javaScriptCanOpenWindowsAutomatically);
193 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
194 s.setMinimumFontSize(b.minimumFontSize);
195 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
196 s.setDefaultFontSize(b.defaultFontSize);
197 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
198 s.setNavDump(b.navDump);
199 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700200 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800201 s.setLightTouchEnabled(b.lightTouch);
202 s.setSaveFormData(b.saveFormData);
203 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700204 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800205 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800206
207 // WebView inside Browser doesn't want initial focus to be set.
208 s.setNeedInitialFocus(false);
209 // Browser supports multiple windows
210 s.setSupportMultipleWindows(true);
Grace Kloba7205e9d2010-03-09 10:50:37 -0800211 // Use system over scroll background
212 s.setUseSystemOverscrollBackground(true);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100213
Andrei Popescu01068702009-08-03 16:03:24 +0100214 // HTML5 API flags
215 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100216 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100217 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100218 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100219 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100220
Andrei Popescu01068702009-08-03 16:03:24 +0100221 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100222 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100223 s.setAppCachePath(b.appCachePath);
224 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100225 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100226
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800227 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800228 }
229 }
230
231 /**
232 * Load settings from the browser app's database.
233 * NOTE: Strings used for the preferences must match those specified
234 * in the browser_preferences.xml
235 * @param ctx A Context object used to query the browser's settings
236 * database. If the database exists, the saved settings will be
237 * stored in this BrowserSettings object. This will update all
238 * observers of this object.
239 */
240 public void loadFromDb(Context ctx) {
241 SharedPreferences p =
242 PreferenceManager.getDefaultSharedPreferences(ctx);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100243 // Set the default value for the Application Caches path.
244 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100245 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100246 webStorageSizeManager = new WebStorageSizeManager(
247 ctx,
248 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
249 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100250 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100251 // Set the default value for the Database path.
252 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100253 // Set the default value for the Geolocation database path.
254 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800255
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700256 homeUrl = getFactoryResetHomeUrl(ctx);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700257
Grace Kloba9804c432009-12-02 11:07:40 -0800258 // the cost of one cached page is ~3M (measured using nytimes.com). For
259 // low end devices, we only cache one page. For high end devices, we try
260 // to cache more pages, currently choose 5.
261 ActivityManager am = (ActivityManager) ctx
262 .getSystemService(Context.ACTIVITY_SERVICE);
263 if (am.getMemoryClass() > 16) {
264 pageCacheCapacity = 5;
265 } else {
266 pageCacheCapacity = 1;
267 }
268
The Android Open Source Project0c908882009-03-03 19:32:16 -0800269 // Load the defaults from the xml
270 // This call is TOO SLOW, need to manually keep the defaults
271 // in sync
272 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
273 syncSharedPreferences(p);
274 }
275
276 /* package */ void syncSharedPreferences(SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700277
The Android Open Source Project0c908882009-03-03 19:32:16 -0800278 homeUrl =
279 p.getString(PREF_HOMEPAGE, homeUrl);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700280
The Android Open Source Project0c908882009-03-03 19:32:16 -0800281 loadsImagesAutomatically = p.getBoolean("load_images",
282 loadsImagesAutomatically);
283 javaScriptEnabled = p.getBoolean("enable_javascript",
284 javaScriptEnabled);
285 pluginsEnabled = p.getBoolean("enable_plugins",
286 pluginsEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800287 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
288 "block_popup_windows",
289 !javaScriptCanOpenWindowsAutomatically);
290 showSecurityWarnings = p.getBoolean("show_security_warnings",
291 showSecurityWarnings);
292 rememberPasswords = p.getBoolean("remember_passwords",
293 rememberPasswords);
294 saveFormData = p.getBoolean("save_formdata",
295 saveFormData);
296 boolean accept_cookies = p.getBoolean("accept_cookies",
297 CookieManager.getInstance().acceptCookie());
298 CookieManager.getInstance().setAcceptCookie(accept_cookies);
299 openInBackground = p.getBoolean("open_in_background", openInBackground);
300 loginInitialized = p.getBoolean("login_initialized", loginInitialized);
301 textSize = WebSettings.TextSize.valueOf(
302 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700303 zoomDensity = WebSettings.ZoomDensity.valueOf(
304 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800305 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700306 loadsPageInOverviewMode = p.getBoolean("load_page",
307 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700308 boolean landscapeOnlyTemp =
309 p.getBoolean("landscape_only", landscapeOnly);
310 if (landscapeOnlyTemp != landscapeOnly) {
311 landscapeOnly = landscapeOnlyTemp;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700312 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800313 useWideViewPort = true; // use wide view port for either setting
314 if (autoFitPage) {
315 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
316 } else {
317 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
318 }
319 defaultTextEncodingName =
320 p.getString(PREF_DEFAULT_TEXT_ENCODING,
321 defaultTextEncodingName);
322
323 showDebugSettings =
324 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
325 // Debug menu items have precidence if the menu is visible
326 if (showDebugSettings) {
327 boolean small_screen = p.getBoolean("small_screen",
328 layoutAlgorithm ==
329 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
330 if (small_screen) {
331 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
332 } else {
333 boolean normal_layout = p.getBoolean("normal_layout",
334 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
335 if (normal_layout) {
336 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
337 } else {
338 layoutAlgorithm =
339 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
340 }
341 }
342 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
343 tracing = p.getBoolean("enable_tracing", tracing);
344 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
345 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800346 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800347 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700348 // JS flags is loaded from DB even if showDebugSettings is false,
349 // so that it can be set once and be effective all the time.
350 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100351
352 // Read the setting for showing/hiding the JS Console always so that should the
353 // user enable debug settings, we already know if we should show the console.
354 // The user will never see the console unless they navigate to about:debug,
355 // regardless of the setting we read here. This setting is only used after debug
356 // is enabled.
357 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700358
Andrei Popescu01068702009-08-03 16:03:24 +0100359 // HTML5 API flags
360 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
361 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
362 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100363 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100364 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100365
Grace Klobad9ee1392009-07-20 11:53:33 -0700366 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800367 }
368
The Android Open Source Project0c908882009-03-03 19:32:16 -0800369 public String getHomePage() {
370 return homeUrl;
371 }
372
Feng Qianb3c02da2009-06-29 15:58:08 -0700373 public String getJsFlags() {
374 return jsFlags;
375 }
376
Andrei Popescu79e82b72009-07-27 12:01:59 +0100377 public WebStorageSizeManager getWebStorageSizeManager() {
378 return webStorageSizeManager;
379 }
380
The Android Open Source Project0c908882009-03-03 19:32:16 -0800381 public void setHomePage(Context context, String url) {
382 Editor ed = PreferenceManager.
383 getDefaultSharedPreferences(context).edit();
384 ed.putString(PREF_HOMEPAGE, url);
385 ed.commit();
386 homeUrl = url;
387 }
388
389 public boolean isLoginInitialized() {
390 return loginInitialized;
391 }
392
393 public void setLoginInitialized(Context context) {
394 loginInitialized = true;
395 Editor ed = PreferenceManager.
396 getDefaultSharedPreferences(context).edit();
397 ed.putBoolean("login_initialized", loginInitialized);
398 ed.commit();
399 }
400
401 public WebSettings.TextSize getTextSize() {
402 return textSize;
403 }
404
Grace Kloba2f830682009-06-25 11:08:53 -0700405 public WebSettings.ZoomDensity getDefaultZoom() {
406 return zoomDensity;
407 }
408
The Android Open Source Project0c908882009-03-03 19:32:16 -0800409 public boolean openInBackground() {
410 return openInBackground;
411 }
412
413 public boolean showSecurityWarnings() {
414 return showSecurityWarnings;
415 }
416
417 public boolean isTracing() {
418 return tracing;
419 }
420
421 public boolean isLightTouch() {
422 return lightTouch;
423 }
424
425 public boolean isNavDump() {
426 return navDump;
427 }
428
The Android Open Source Project0c908882009-03-03 19:32:16 -0800429 public boolean showDebugSettings() {
430 return showDebugSettings;
431 }
432
433 public void toggleDebugSettings() {
434 showDebugSettings = !showDebugSettings;
435 navDump = showDebugSettings;
436 update();
437 }
438
439 /**
440 * Add a WebSettings object to the list of observers that will be updated
441 * when update() is called.
442 *
443 * @param s A WebSettings object that is strictly tied to the life of a
444 * WebView.
445 */
446 public Observer addObserver(WebSettings s) {
447 Observer old = mWebSettingsToObservers.get(s);
448 if (old != null) {
449 super.deleteObserver(old);
450 }
451 Observer o = new Observer(s);
452 mWebSettingsToObservers.put(s, o);
453 super.addObserver(o);
454 return o;
455 }
456
457 /**
458 * Delete the given WebSettings observer from the list of observers.
459 * @param s The WebSettings object to be deleted.
460 */
461 public void deleteObserver(WebSettings s) {
462 Observer o = mWebSettingsToObservers.get(s);
463 if (o != null) {
464 mWebSettingsToObservers.remove(s);
465 super.deleteObserver(o);
466 }
467 }
468
469 /*
470 * Package level method for obtaining a single app instance of the
471 * BrowserSettings.
472 */
473 /*package*/ static BrowserSettings getInstance() {
474 if (sSingleton == null ) {
475 sSingleton = new BrowserSettings();
476 }
477 return sSingleton;
478 }
479
480 /*
481 * Package level method for associating the BrowserSettings with TabControl
482 */
483 /* package */void setTabControl(TabControl tabControl) {
484 mTabControl = tabControl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800485 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800486 }
487
488 /*
489 * Update all the observers of the object.
490 */
491 /*package*/ void update() {
492 setChanged();
493 notifyObservers();
494 }
495
496 /*package*/ void clearCache(Context context) {
497 WebIconDatabase.getInstance().removeAllIcons();
498 if (mTabControl != null) {
499 WebView current = mTabControl.getCurrentWebView();
500 if (current != null) {
501 current.clearCache(true);
502 }
503 }
504 }
505
506 /*package*/ void clearCookies(Context context) {
507 CookieManager.getInstance().removeAllCookie();
508 }
509
510 /* package */void clearHistory(Context context) {
511 ContentResolver resolver = context.getContentResolver();
512 Browser.clearHistory(resolver);
513 Browser.clearSearches(resolver);
514 }
515
516 /* package */ void clearFormData(Context context) {
517 WebViewDatabase.getInstance(context).clearFormData();
518 if (mTabControl != null) {
519 mTabControl.getCurrentTopWebView().clearFormData();
520 }
521 }
522
523 /*package*/ void clearPasswords(Context context) {
524 WebViewDatabase db = WebViewDatabase.getInstance(context);
525 db.clearUsernamePassword();
526 db.clearHttpAuthUsernamePassword();
527 }
528
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800529 private void updateTabControlSettings() {
530 // Enable/disable the error console.
531 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
532 showDebugSettings && showConsole);
533 mTabControl.getBrowserActivity().setRequestedOrientation(
534 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
535 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
536 }
537
Steve Blockf344d032009-07-30 10:50:45 +0100538 private void maybeDisableWebsiteSettings(Context context) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100539 PreferenceActivity activity = (PreferenceActivity) context;
540 final PreferenceScreen screen = (PreferenceScreen)
541 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
542 screen.setEnabled(false);
543 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
544 public void onReceiveValue(Map webStorageOrigins) {
545 if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) {
546 screen.setEnabled(true);
547 }
548 }
549 });
550
Steve Block2a6a0f42009-11-19 15:55:42 +0000551 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
552 public void onReceiveValue(Set<String> geolocationOrigins) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100553 if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) {
554 screen.setEnabled(true);
555 }
556 }
557 });
Steve Blockf344d032009-07-30 10:50:45 +0100558 }
559
Nicolas Roard78a98e42009-05-11 13:34:17 +0100560 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100561 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100562 maybeDisableWebsiteSettings(context);
563 }
564
565 /*package*/ void clearLocationAccess(Context context) {
566 GeolocationPermissions.getInstance().clearAll();
567 maybeDisableWebsiteSettings(context);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100568 }
569
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400570 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800571 reset();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800572 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400573 PreferenceManager.getDefaultSharedPreferences(ctx);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800574 p.edit().clear().commit();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400575 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800576 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700577 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700578 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100579 // reset appcache max size
580 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700581 }
582
583 private String getFactoryResetHomeUrl(Context context) {
584 String url = context.getResources().getString(R.string.homepage_base);
585 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500586 url = url.replace("{CID}",
587 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700588 }
589 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800590 }
591
The Android Open Source Project0c908882009-03-03 19:32:16 -0800592 // Private constructor that does nothing.
593 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800594 reset();
595 }
596
597 private void reset() {
598 // Private variables for settings
599 // NOTE: these defaults need to be kept in sync with the XML
600 // until the performance of PreferenceManager.setDefaultValues()
601 // is improved.
602 loadsImagesAutomatically = true;
603 javaScriptEnabled = true;
604 pluginsEnabled = true;
605 javaScriptCanOpenWindowsAutomatically = false;
606 showSecurityWarnings = true;
607 rememberPasswords = true;
608 saveFormData = true;
609 openInBackground = false;
610 loginInitialized = false;
611 autoFitPage = true;
612 landscapeOnly = false;
613 loadsPageInOverviewMode = true;
614 showDebugSettings = false;
615 // HTML5 API flags
616 appCacheEnabled = true;
617 databaseEnabled = true;
618 domStorageEnabled = true;
619 geolocationEnabled = true;
620 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800621 }
622}