blob: b7ff0d372e73b81510ab9e34b5cb0315116ce93c [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070019import com.google.android.providers.GoogleSettings.Partner;
20
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;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.webkit.WebView;
31import android.webkit.WebViewDatabase;
32import android.webkit.WebIconDatabase;
33import android.webkit.WebSettings;
Nicolas Roard78a98e42009-05-11 13:34:17 +010034import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.preference.PreferenceManager;
36import android.provider.Browser;
37
Steve Blockf344d032009-07-30 10:50:45 +010038import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import java.util.HashMap;
40import java.util.Observable;
41
42/*
43 * Package level class for storing various WebView and Browser settings. To use
44 * this class:
45 * BrowserSettings s = BrowserSettings.getInstance();
46 * s.addObserver(webView.getSettings());
47 * s.loadFromDb(context); // Only needed on app startup
48 * s.javaScriptEnabled = true;
49 * ... // set any other settings
50 * s.update(); // this will update all the observers
51 *
52 * To remove an observer:
53 * s.deleteObserver(webView.getSettings());
54 */
55class BrowserSettings extends Observable {
56
Leon Scroggins9ac587d2009-04-20 12:53:46 -040057 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080058 // NOTE: these defaults need to be kept in sync with the XML
59 // until the performance of PreferenceManager.setDefaultValues()
60 // is improved.
61 private boolean loadsImagesAutomatically = true;
62 private boolean javaScriptEnabled = true;
63 private boolean pluginsEnabled = true;
64 private String pluginsPath; // default value set in loadFromDb().
65 private boolean javaScriptCanOpenWindowsAutomatically = false;
66 private boolean showSecurityWarnings = true;
67 private boolean rememberPasswords = true;
68 private boolean saveFormData = true;
69 private boolean openInBackground = false;
70 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070071 private String homeUrl = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -080072 private boolean loginInitialized = false;
73 private boolean autoFitPage = true;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -070074 private boolean landscapeOnly = false;
Grace Kloba5b4b8f12009-08-05 17:19:17 -070075 private boolean loadsPageInOverviewMode = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -080076 private boolean showDebugSettings = false;
Andrei Popescu01068702009-08-03 16:03:24 +010077 // HTML5 API flags
Andrei Popescu5151ac12009-04-17 10:43:07 +010078 private boolean appCacheEnabled = true;
Andrei Popescu01068702009-08-03 16:03:24 +010079 private boolean databaseEnabled = true;
Ben Murdoch734a0662009-06-02 19:11:52 +010080 private boolean domStorageEnabled = true;
Steve Blockf344d032009-07-30 10:50:45 +010081 private boolean geolocationEnabled = true;
Andrei Popescu01068702009-08-03 16:03:24 +010082 private boolean workersEnabled = true; // only affects V8. JSC does not have a similar setting
83 // HTML5 API configuration params
84 private long appCacheMaxSize = Long.MAX_VALUE;
85 private String appCachePath; // default value set in loadFromDb().
86 private String databasePath; // default value set in loadFromDb()
Steve Block5029cf02009-08-21 13:16:58 +010087 private String geolocationDatabasePath; // default value set in loadFromDb()
Andrei Popescu01068702009-08-03 16:03:24 +010088 private WebStorageSizeManager webStorageSizeManager;
89
90 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -080091
Nicolas Roard78a98e42009-05-11 13:34:17 +010092 private final static String TAG = "BrowserSettings";
93
The Android Open Source Project0c908882009-03-03 19:32:16 -080094 // Development settings
95 public WebSettings.LayoutAlgorithm layoutAlgorithm =
96 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
97 private boolean useWideViewPort = true;
98 private int userAgent = 0;
99 private boolean tracing = false;
100 private boolean lightTouch = false;
101 private boolean navDump = false;
Feng Qianb3c02da2009-06-29 15:58:08 -0700102
Ben Murdochbff2d602009-07-01 20:19:05 +0100103 // By default the error console is shown once the user navigates to about:debug.
104 // The setting can be then toggled from the settings menu.
105 private boolean showConsole = true;
106
The Android Open Source Project0c908882009-03-03 19:32:16 -0800107 // Browser only settings
108 private boolean doFlick = false;
109
110 // Private preconfigured values
111 private static int minimumFontSize = 8;
112 private static int minimumLogicalFontSize = 8;
113 private static int defaultFontSize = 16;
114 private static int defaultFixedFontSize = 13;
115 private static WebSettings.TextSize textSize =
116 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700117 private static WebSettings.ZoomDensity zoomDensity =
118 WebSettings.ZoomDensity.MEDIUM;
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);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800205
206 // WebView inside Browser doesn't want initial focus to be set.
207 s.setNeedInitialFocus(false);
208 // Browser supports multiple windows
209 s.setSupportMultipleWindows(true);
210 // Turn off file access
211 s.setAllowFileAccess(false);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100212
Andrei Popescu01068702009-08-03 16:03:24 +0100213 // HTML5 API flags
214 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100215 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100216 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100217 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100218 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100219
Andrei Popescu01068702009-08-03 16:03:24 +0100220 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100221 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100222 s.setAppCachePath(b.appCachePath);
223 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100224 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100225
226 // Enable/Disable the error console.
227 b.mTabControl.getBrowserActivity().setShouldShowErrorConsole(
228 b.showDebugSettings && b.showConsole);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800229 }
230 }
231
232 /**
233 * Load settings from the browser app's database.
234 * NOTE: Strings used for the preferences must match those specified
235 * in the browser_preferences.xml
236 * @param ctx A Context object used to query the browser's settings
237 * database. If the database exists, the saved settings will be
238 * stored in this BrowserSettings object. This will update all
239 * observers of this object.
240 */
241 public void loadFromDb(Context ctx) {
242 SharedPreferences p =
243 PreferenceManager.getDefaultSharedPreferences(ctx);
244
245 // Set the default value for the plugins path to the application's
246 // local directory.
247 pluginsPath = ctx.getDir("plugins", 0).getPath();
Andrei Popescu5151ac12009-04-17 10:43:07 +0100248 // Set the default value for the Application Caches path.
249 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100250 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100251 webStorageSizeManager = new WebStorageSizeManager(
252 ctx,
253 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
254 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100255 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100256 // Set the default value for the Database path.
257 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100258 // Set the default value for the Geolocation database path.
259 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800260
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700261 homeUrl = getFactoryResetHomeUrl(ctx);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700262
The Android Open Source Project0c908882009-03-03 19:32:16 -0800263 // Load the defaults from the xml
264 // This call is TOO SLOW, need to manually keep the defaults
265 // in sync
266 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
267 syncSharedPreferences(p);
268 }
269
270 /* package */ void syncSharedPreferences(SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700271
The Android Open Source Project0c908882009-03-03 19:32:16 -0800272 homeUrl =
273 p.getString(PREF_HOMEPAGE, homeUrl);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700274
The Android Open Source Project0c908882009-03-03 19:32:16 -0800275 loadsImagesAutomatically = p.getBoolean("load_images",
276 loadsImagesAutomatically);
277 javaScriptEnabled = p.getBoolean("enable_javascript",
278 javaScriptEnabled);
279 pluginsEnabled = p.getBoolean("enable_plugins",
280 pluginsEnabled);
281 pluginsPath = p.getString("plugins_path", pluginsPath);
282 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
283 "block_popup_windows",
284 !javaScriptCanOpenWindowsAutomatically);
285 showSecurityWarnings = p.getBoolean("show_security_warnings",
286 showSecurityWarnings);
287 rememberPasswords = p.getBoolean("remember_passwords",
288 rememberPasswords);
289 saveFormData = p.getBoolean("save_formdata",
290 saveFormData);
291 boolean accept_cookies = p.getBoolean("accept_cookies",
292 CookieManager.getInstance().acceptCookie());
293 CookieManager.getInstance().setAcceptCookie(accept_cookies);
294 openInBackground = p.getBoolean("open_in_background", openInBackground);
295 loginInitialized = p.getBoolean("login_initialized", loginInitialized);
296 textSize = WebSettings.TextSize.valueOf(
297 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700298 zoomDensity = WebSettings.ZoomDensity.valueOf(
299 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800300 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700301 loadsPageInOverviewMode = p.getBoolean("load_page",
302 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700303 boolean landscapeOnlyTemp =
304 p.getBoolean("landscape_only", landscapeOnly);
305 if (landscapeOnlyTemp != landscapeOnly) {
306 landscapeOnly = landscapeOnlyTemp;
307 mTabControl.getBrowserActivity().setRequestedOrientation(
308 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
309 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
310 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800311 useWideViewPort = true; // use wide view port for either setting
312 if (autoFitPage) {
313 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
314 } else {
315 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
316 }
317 defaultTextEncodingName =
318 p.getString(PREF_DEFAULT_TEXT_ENCODING,
319 defaultTextEncodingName);
320
321 showDebugSettings =
322 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
323 // Debug menu items have precidence if the menu is visible
324 if (showDebugSettings) {
325 boolean small_screen = p.getBoolean("small_screen",
326 layoutAlgorithm ==
327 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
328 if (small_screen) {
329 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
330 } else {
331 boolean normal_layout = p.getBoolean("normal_layout",
332 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
333 if (normal_layout) {
334 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
335 } else {
336 layoutAlgorithm =
337 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
338 }
339 }
340 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
341 tracing = p.getBoolean("enable_tracing", tracing);
342 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
343 navDump = p.getBoolean("enable_nav_dump", navDump);
344 doFlick = p.getBoolean("enable_flick", doFlick);
345 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800346 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700347 // JS flags is loaded from DB even if showDebugSettings is false,
348 // so that it can be set once and be effective all the time.
349 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100350
351 // Read the setting for showing/hiding the JS Console always so that should the
352 // user enable debug settings, we already know if we should show the console.
353 // The user will never see the console unless they navigate to about:debug,
354 // regardless of the setting we read here. This setting is only used after debug
355 // is enabled.
356 showConsole = p.getBoolean("javascript_console", showConsole);
357 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
358 showDebugSettings && showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700359
Andrei Popescu01068702009-08-03 16:03:24 +0100360 // HTML5 API flags
361 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
362 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
363 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100364 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100365 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100366
Grace Klobad9ee1392009-07-20 11:53:33 -0700367 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800368 }
369
370 public String getPluginsPath() {
371 return pluginsPath;
372 }
373
374 public String getHomePage() {
375 return homeUrl;
376 }
377
Feng Qianb3c02da2009-06-29 15:58:08 -0700378 public String getJsFlags() {
379 return jsFlags;
380 }
381
Andrei Popescu79e82b72009-07-27 12:01:59 +0100382 public WebStorageSizeManager getWebStorageSizeManager() {
383 return webStorageSizeManager;
384 }
385
The Android Open Source Project0c908882009-03-03 19:32:16 -0800386 public void setHomePage(Context context, String url) {
387 Editor ed = PreferenceManager.
388 getDefaultSharedPreferences(context).edit();
389 ed.putString(PREF_HOMEPAGE, url);
390 ed.commit();
391 homeUrl = url;
392 }
393
394 public boolean isLoginInitialized() {
395 return loginInitialized;
396 }
397
398 public void setLoginInitialized(Context context) {
399 loginInitialized = true;
400 Editor ed = PreferenceManager.
401 getDefaultSharedPreferences(context).edit();
402 ed.putBoolean("login_initialized", loginInitialized);
403 ed.commit();
404 }
405
406 public WebSettings.TextSize getTextSize() {
407 return textSize;
408 }
409
Grace Kloba2f830682009-06-25 11:08:53 -0700410 public WebSettings.ZoomDensity getDefaultZoom() {
411 return zoomDensity;
412 }
413
The Android Open Source Project0c908882009-03-03 19:32:16 -0800414 public boolean openInBackground() {
415 return openInBackground;
416 }
417
418 public boolean showSecurityWarnings() {
419 return showSecurityWarnings;
420 }
421
422 public boolean isTracing() {
423 return tracing;
424 }
425
426 public boolean isLightTouch() {
427 return lightTouch;
428 }
429
430 public boolean isNavDump() {
431 return navDump;
432 }
433
434 public boolean doFlick() {
435 return doFlick;
436 }
437
438 public boolean showDebugSettings() {
439 return showDebugSettings;
440 }
441
442 public void toggleDebugSettings() {
443 showDebugSettings = !showDebugSettings;
444 navDump = showDebugSettings;
445 update();
446 }
447
448 /**
449 * Add a WebSettings object to the list of observers that will be updated
450 * when update() is called.
451 *
452 * @param s A WebSettings object that is strictly tied to the life of a
453 * WebView.
454 */
455 public Observer addObserver(WebSettings s) {
456 Observer old = mWebSettingsToObservers.get(s);
457 if (old != null) {
458 super.deleteObserver(old);
459 }
460 Observer o = new Observer(s);
461 mWebSettingsToObservers.put(s, o);
462 super.addObserver(o);
463 return o;
464 }
465
466 /**
467 * Delete the given WebSettings observer from the list of observers.
468 * @param s The WebSettings object to be deleted.
469 */
470 public void deleteObserver(WebSettings s) {
471 Observer o = mWebSettingsToObservers.get(s);
472 if (o != null) {
473 mWebSettingsToObservers.remove(s);
474 super.deleteObserver(o);
475 }
476 }
477
478 /*
479 * Package level method for obtaining a single app instance of the
480 * BrowserSettings.
481 */
482 /*package*/ static BrowserSettings getInstance() {
483 if (sSingleton == null ) {
484 sSingleton = new BrowserSettings();
485 }
486 return sSingleton;
487 }
488
489 /*
490 * Package level method for associating the BrowserSettings with TabControl
491 */
492 /* package */void setTabControl(TabControl tabControl) {
493 mTabControl = tabControl;
494 }
495
496 /*
497 * Update all the observers of the object.
498 */
499 /*package*/ void update() {
500 setChanged();
501 notifyObservers();
502 }
503
504 /*package*/ void clearCache(Context context) {
505 WebIconDatabase.getInstance().removeAllIcons();
506 if (mTabControl != null) {
507 WebView current = mTabControl.getCurrentWebView();
508 if (current != null) {
509 current.clearCache(true);
510 }
511 }
512 }
513
514 /*package*/ void clearCookies(Context context) {
515 CookieManager.getInstance().removeAllCookie();
516 }
517
518 /* package */void clearHistory(Context context) {
519 ContentResolver resolver = context.getContentResolver();
520 Browser.clearHistory(resolver);
521 Browser.clearSearches(resolver);
522 }
523
524 /* package */ void clearFormData(Context context) {
525 WebViewDatabase.getInstance(context).clearFormData();
526 if (mTabControl != null) {
527 mTabControl.getCurrentTopWebView().clearFormData();
528 }
529 }
530
531 /*package*/ void clearPasswords(Context context) {
532 WebViewDatabase db = WebViewDatabase.getInstance(context);
533 db.clearUsernamePassword();
534 db.clearHttpAuthUsernamePassword();
535 }
536
Steve Blockf344d032009-07-30 10:50:45 +0100537 private void maybeDisableWebsiteSettings(Context context) {
538 Set webStorageOrigins = WebStorage.getInstance().getOrigins();
539 Set geolocationOrigins =
540 GeolocationPermissions.getInstance().getOrigins();
541 if (((webStorageOrigins == null) || webStorageOrigins.isEmpty()) &&
542 ((geolocationOrigins == null) || geolocationOrigins.isEmpty())) {
543 PreferenceActivity activity = (PreferenceActivity) context;
544 PreferenceScreen screen = (PreferenceScreen)
545 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
546 screen.setEnabled(false);
547 }
548 }
549
Nicolas Roard78a98e42009-05-11 13:34:17 +0100550 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100551 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100552 maybeDisableWebsiteSettings(context);
553 }
554
555 /*package*/ void clearLocationAccess(Context context) {
556 GeolocationPermissions.getInstance().clearAll();
557 maybeDisableWebsiteSettings(context);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100558 }
559
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400560 /*package*/ void resetDefaultPreferences(Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800561 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400562 PreferenceManager.getDefaultSharedPreferences(ctx);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800563 p.edit().clear().commit();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400564 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800565 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700566 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700567 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100568 // reset appcache max size
569 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700570 }
571
572 private String getFactoryResetHomeUrl(Context context) {
573 String url = context.getResources().getString(R.string.homepage_base);
574 if (url.indexOf("{CID}") != -1) {
575 url = url.replace("{CID}", Partner.getString(context
Ramanan Rajeswaran9768ac52009-06-03 19:34:58 -0700576 .getContentResolver(), Partner.CLIENT_ID, "android-google"));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700577 }
578 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800579 }
580
The Android Open Source Project0c908882009-03-03 19:32:16 -0800581 // Private constructor that does nothing.
582 private BrowserSettings() {
583 }
584}