blob: aa54ccd2688d1f2dbf05dbe7a41822e78863215f [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()
87 private WebStorageSizeManager webStorageSizeManager;
88
89 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -080090
Nicolas Roard78a98e42009-05-11 13:34:17 +010091 private final static String TAG = "BrowserSettings";
92
The Android Open Source Project0c908882009-03-03 19:32:16 -080093 // Development settings
94 public WebSettings.LayoutAlgorithm layoutAlgorithm =
95 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
96 private boolean useWideViewPort = true;
97 private int userAgent = 0;
98 private boolean tracing = false;
99 private boolean lightTouch = false;
100 private boolean navDump = false;
Feng Qianb3c02da2009-06-29 15:58:08 -0700101
Ben Murdochbff2d602009-07-01 20:19:05 +0100102 // By default the error console is shown once the user navigates to about:debug.
103 // The setting can be then toggled from the settings menu.
104 private boolean showConsole = true;
105
The Android Open Source Project0c908882009-03-03 19:32:16 -0800106 // Browser only settings
107 private boolean doFlick = false;
108
109 // 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;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118
119 // Preference keys that are used outside this class
120 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
121 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
122 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
123 public final static String PREF_HOMEPAGE = "homepage";
124 public final static String PREF_CLEAR_FORM_DATA =
125 "privacy_clear_form_data";
126 public final static String PREF_CLEAR_PASSWORDS =
127 "privacy_clear_passwords";
128 public final static String PREF_EXTRAS_RESET_DEFAULTS =
129 "reset_default_preferences";
130 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100131 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800132 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700133 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800134 public final static String PREF_DEFAULT_TEXT_ENCODING =
135 "default_text_encoding";
Steve Blockf344d032009-07-30 10:50:45 +0100136 public final static String PREF_CLEAR_LOCATION_ACCESS =
137 "privacy_clear_location_access";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800138
139 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Grace Klobaa4fa6ee2009-06-26 00:34:46 -0700140 "U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, " +
141 "like Gecko) Version/4.0 Safari/530.17";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800142
143 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Grace Klobaa4fa6ee2009-06-26 00:34:46 -0700144 "CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 " +
145 "(KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146
147 // Value to truncate strings when adding them to a TextView within
148 // a ListView
149 public final static int MAX_TEXTVIEW_LEN = 80;
150
151 private TabControl mTabControl;
152
153 // Single instance of the BrowserSettings for use in the Browser app.
154 private static BrowserSettings sSingleton;
155
156 // Private map of WebSettings to Observer objects used when deleting an
157 // observer.
158 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
159 new HashMap<WebSettings,Observer>();
160
161 /*
162 * An observer wrapper for updating a WebSettings object with the new
163 * settings after a call to BrowserSettings.update().
164 */
165 static class Observer implements java.util.Observer {
166 // Private WebSettings object that will be updated.
167 private WebSettings mSettings;
168
169 Observer(WebSettings w) {
170 mSettings = w;
171 }
172
173 public void update(Observable o, Object arg) {
174 BrowserSettings b = (BrowserSettings)o;
175 WebSettings s = mSettings;
176
177 s.setLayoutAlgorithm(b.layoutAlgorithm);
178 if (b.userAgent == 0) {
179 // use the default ua string
180 s.setUserAgentString(null);
181 } else if (b.userAgent == 1) {
182 s.setUserAgentString(DESKTOP_USERAGENT);
183 } else if (b.userAgent == 2) {
184 s.setUserAgentString(IPHONE_USERAGENT);
185 }
186 s.setUseWideViewPort(b.useWideViewPort);
187 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
188 s.setJavaScriptEnabled(b.javaScriptEnabled);
189 s.setPluginsEnabled(b.pluginsEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 s.setJavaScriptCanOpenWindowsAutomatically(
191 b.javaScriptCanOpenWindowsAutomatically);
192 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
193 s.setMinimumFontSize(b.minimumFontSize);
194 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
195 s.setDefaultFontSize(b.defaultFontSize);
196 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
197 s.setNavDump(b.navDump);
198 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700199 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800200 s.setLightTouchEnabled(b.lightTouch);
201 s.setSaveFormData(b.saveFormData);
202 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700203 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800204
205 // WebView inside Browser doesn't want initial focus to be set.
206 s.setNeedInitialFocus(false);
207 // Browser supports multiple windows
208 s.setSupportMultipleWindows(true);
209 // Turn off file access
210 s.setAllowFileAccess(false);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100211
Andrei Popescu01068702009-08-03 16:03:24 +0100212 // HTML5 API flags
213 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100214 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100215 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100216 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100217
Andrei Popescu01068702009-08-03 16:03:24 +0100218 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100219 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100220 s.setAppCachePath(b.appCachePath);
221 s.setDatabasePath(b.databasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100222
223 // Enable/Disable the error console.
224 b.mTabControl.getBrowserActivity().setShouldShowErrorConsole(
225 b.showDebugSettings && b.showConsole);
Steve Blockf344d032009-07-30 10:50:45 +0100226
227 // Configure the Geolocation permissions manager to deny all
228 // permission requests if Geolocation is disabled in the browser.
229 // TODO(steveblock): Implement
The Android Open Source Project0c908882009-03-03 19:32:16 -0800230 }
231 }
232
233 /**
234 * Load settings from the browser app's database.
235 * NOTE: Strings used for the preferences must match those specified
236 * in the browser_preferences.xml
237 * @param ctx A Context object used to query the browser's settings
238 * database. If the database exists, the saved settings will be
239 * stored in this BrowserSettings object. This will update all
240 * observers of this object.
241 */
242 public void loadFromDb(Context ctx) {
243 SharedPreferences p =
244 PreferenceManager.getDefaultSharedPreferences(ctx);
245
246 // Set the default value for the plugins path to the application's
247 // local directory.
248 pluginsPath = ctx.getDir("plugins", 0).getPath();
Andrei Popescu5151ac12009-04-17 10:43:07 +0100249 // Set the default value for the Application Caches path.
250 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100251 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100252 webStorageSizeManager = new WebStorageSizeManager(
253 ctx,
254 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
255 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100256 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100257 // Set the default value for the Database path.
258 databasePath = ctx.getDir("databases", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800259
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700260 homeUrl = getFactoryResetHomeUrl(ctx);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700261
The Android Open Source Project0c908882009-03-03 19:32:16 -0800262 // Load the defaults from the xml
263 // This call is TOO SLOW, need to manually keep the defaults
264 // in sync
265 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
266 syncSharedPreferences(p);
267 }
268
269 /* package */ void syncSharedPreferences(SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700270
The Android Open Source Project0c908882009-03-03 19:32:16 -0800271 homeUrl =
272 p.getString(PREF_HOMEPAGE, homeUrl);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700273
The Android Open Source Project0c908882009-03-03 19:32:16 -0800274 loadsImagesAutomatically = p.getBoolean("load_images",
275 loadsImagesAutomatically);
276 javaScriptEnabled = p.getBoolean("enable_javascript",
277 javaScriptEnabled);
278 pluginsEnabled = p.getBoolean("enable_plugins",
279 pluginsEnabled);
280 pluginsPath = p.getString("plugins_path", pluginsPath);
281 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
282 "block_popup_windows",
283 !javaScriptCanOpenWindowsAutomatically);
284 showSecurityWarnings = p.getBoolean("show_security_warnings",
285 showSecurityWarnings);
286 rememberPasswords = p.getBoolean("remember_passwords",
287 rememberPasswords);
288 saveFormData = p.getBoolean("save_formdata",
289 saveFormData);
290 boolean accept_cookies = p.getBoolean("accept_cookies",
291 CookieManager.getInstance().acceptCookie());
292 CookieManager.getInstance().setAcceptCookie(accept_cookies);
293 openInBackground = p.getBoolean("open_in_background", openInBackground);
294 loginInitialized = p.getBoolean("login_initialized", loginInitialized);
295 textSize = WebSettings.TextSize.valueOf(
296 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700297 zoomDensity = WebSettings.ZoomDensity.valueOf(
298 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800299 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700300 loadsPageInOverviewMode = p.getBoolean("load_page",
301 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700302 boolean landscapeOnlyTemp =
303 p.getBoolean("landscape_only", landscapeOnly);
304 if (landscapeOnlyTemp != landscapeOnly) {
305 landscapeOnly = landscapeOnlyTemp;
306 mTabControl.getBrowserActivity().setRequestedOrientation(
307 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
308 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
309 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800310 useWideViewPort = true; // use wide view port for either setting
311 if (autoFitPage) {
312 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
313 } else {
314 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
315 }
316 defaultTextEncodingName =
317 p.getString(PREF_DEFAULT_TEXT_ENCODING,
318 defaultTextEncodingName);
319
320 showDebugSettings =
321 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
322 // Debug menu items have precidence if the menu is visible
323 if (showDebugSettings) {
324 boolean small_screen = p.getBoolean("small_screen",
325 layoutAlgorithm ==
326 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
327 if (small_screen) {
328 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
329 } else {
330 boolean normal_layout = p.getBoolean("normal_layout",
331 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
332 if (normal_layout) {
333 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
334 } else {
335 layoutAlgorithm =
336 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
337 }
338 }
339 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
340 tracing = p.getBoolean("enable_tracing", tracing);
341 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
342 navDump = p.getBoolean("enable_nav_dump", navDump);
343 doFlick = p.getBoolean("enable_flick", doFlick);
344 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800345 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700346 // JS flags is loaded from DB even if showDebugSettings is false,
347 // so that it can be set once and be effective all the time.
348 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100349
350 // Read the setting for showing/hiding the JS Console always so that should the
351 // user enable debug settings, we already know if we should show the console.
352 // The user will never see the console unless they navigate to about:debug,
353 // regardless of the setting we read here. This setting is only used after debug
354 // is enabled.
355 showConsole = p.getBoolean("javascript_console", showConsole);
356 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
357 showDebugSettings && 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);
365 // HTML 5 configuration params
366 appCachePath = p.getString("appcache_path", appCachePath);
367 databasePath = p.getString("database_path", databasePath);
Steve Blockf344d032009-07-30 10:50:45 +0100368
Grace Klobad9ee1392009-07-20 11:53:33 -0700369 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800370 }
371
372 public String getPluginsPath() {
373 return pluginsPath;
374 }
375
376 public String getHomePage() {
377 return homeUrl;
378 }
379
Feng Qianb3c02da2009-06-29 15:58:08 -0700380 public String getJsFlags() {
381 return jsFlags;
382 }
383
Andrei Popescu79e82b72009-07-27 12:01:59 +0100384 public WebStorageSizeManager getWebStorageSizeManager() {
385 return webStorageSizeManager;
386 }
387
The Android Open Source Project0c908882009-03-03 19:32:16 -0800388 public void setHomePage(Context context, String url) {
389 Editor ed = PreferenceManager.
390 getDefaultSharedPreferences(context).edit();
391 ed.putString(PREF_HOMEPAGE, url);
392 ed.commit();
393 homeUrl = url;
394 }
395
396 public boolean isLoginInitialized() {
397 return loginInitialized;
398 }
399
400 public void setLoginInitialized(Context context) {
401 loginInitialized = true;
402 Editor ed = PreferenceManager.
403 getDefaultSharedPreferences(context).edit();
404 ed.putBoolean("login_initialized", loginInitialized);
405 ed.commit();
406 }
407
408 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
436 public boolean doFlick() {
437 return doFlick;
438 }
439
440 public boolean showDebugSettings() {
441 return showDebugSettings;
442 }
443
444 public void toggleDebugSettings() {
445 showDebugSettings = !showDebugSettings;
446 navDump = showDebugSettings;
447 update();
448 }
449
450 /**
451 * Add a WebSettings object to the list of observers that will be updated
452 * when update() is called.
453 *
454 * @param s A WebSettings object that is strictly tied to the life of a
455 * WebView.
456 */
457 public Observer addObserver(WebSettings s) {
458 Observer old = mWebSettingsToObservers.get(s);
459 if (old != null) {
460 super.deleteObserver(old);
461 }
462 Observer o = new Observer(s);
463 mWebSettingsToObservers.put(s, o);
464 super.addObserver(o);
465 return o;
466 }
467
468 /**
469 * Delete the given WebSettings observer from the list of observers.
470 * @param s The WebSettings object to be deleted.
471 */
472 public void deleteObserver(WebSettings s) {
473 Observer o = mWebSettingsToObservers.get(s);
474 if (o != null) {
475 mWebSettingsToObservers.remove(s);
476 super.deleteObserver(o);
477 }
478 }
479
480 /*
481 * Package level method for obtaining a single app instance of the
482 * BrowserSettings.
483 */
484 /*package*/ static BrowserSettings getInstance() {
485 if (sSingleton == null ) {
486 sSingleton = new BrowserSettings();
487 }
488 return sSingleton;
489 }
490
491 /*
492 * Package level method for associating the BrowserSettings with TabControl
493 */
494 /* package */void setTabControl(TabControl tabControl) {
495 mTabControl = tabControl;
496 }
497
498 /*
499 * Update all the observers of the object.
500 */
501 /*package*/ void update() {
502 setChanged();
503 notifyObservers();
504 }
505
506 /*package*/ void clearCache(Context context) {
507 WebIconDatabase.getInstance().removeAllIcons();
508 if (mTabControl != null) {
509 WebView current = mTabControl.getCurrentWebView();
510 if (current != null) {
511 current.clearCache(true);
512 }
513 }
514 }
515
516 /*package*/ void clearCookies(Context context) {
517 CookieManager.getInstance().removeAllCookie();
518 }
519
520 /* package */void clearHistory(Context context) {
521 ContentResolver resolver = context.getContentResolver();
522 Browser.clearHistory(resolver);
523 Browser.clearSearches(resolver);
524 }
525
526 /* package */ void clearFormData(Context context) {
527 WebViewDatabase.getInstance(context).clearFormData();
528 if (mTabControl != null) {
529 mTabControl.getCurrentTopWebView().clearFormData();
530 }
531 }
532
533 /*package*/ void clearPasswords(Context context) {
534 WebViewDatabase db = WebViewDatabase.getInstance(context);
535 db.clearUsernamePassword();
536 db.clearHttpAuthUsernamePassword();
537 }
538
Steve Blockf344d032009-07-30 10:50:45 +0100539 private void maybeDisableWebsiteSettings(Context context) {
540 Set webStorageOrigins = WebStorage.getInstance().getOrigins();
541 Set geolocationOrigins =
542 GeolocationPermissions.getInstance().getOrigins();
543 if (((webStorageOrigins == null) || webStorageOrigins.isEmpty()) &&
544 ((geolocationOrigins == null) || geolocationOrigins.isEmpty())) {
545 PreferenceActivity activity = (PreferenceActivity) context;
546 PreferenceScreen screen = (PreferenceScreen)
547 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
548 screen.setEnabled(false);
549 }
550 }
551
Nicolas Roard78a98e42009-05-11 13:34:17 +0100552 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100553 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100554 maybeDisableWebsiteSettings(context);
555 }
556
557 /*package*/ void clearLocationAccess(Context context) {
558 GeolocationPermissions.getInstance().clearAll();
559 maybeDisableWebsiteSettings(context);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100560 }
561
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400562 /*package*/ void resetDefaultPreferences(Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800563 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400564 PreferenceManager.getDefaultSharedPreferences(ctx);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800565 p.edit().clear().commit();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400566 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800567 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700568 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700569 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100570 // reset appcache max size
571 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700572 }
573
574 private String getFactoryResetHomeUrl(Context context) {
575 String url = context.getResources().getString(R.string.homepage_base);
576 if (url.indexOf("{CID}") != -1) {
577 url = url.replace("{CID}", Partner.getString(context
Ramanan Rajeswaran9768ac52009-06-03 19:34:58 -0700578 .getContentResolver(), Partner.CLIENT_ID, "android-google"));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700579 }
580 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800581 }
582
The Android Open Source Project0c908882009-03-03 19:32:16 -0800583 // Private constructor that does nothing.
584 private BrowserSettings() {
585 }
586}