blob: 6f5caed9f5c8a28bf43b777af5d85e7aa9bb7764 [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";
131 public final static String PREF_GEARS_SETTINGS = "gears_settings";
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 Blockf344d032009-07-30 10:50:45 +0100137 public final static String PREF_CLEAR_LOCATION_ACCESS =
138 "privacy_clear_location_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.
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100218
Andrei Popescu01068702009-08-03 16:03:24 +0100219 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100220 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100221 s.setAppCachePath(b.appCachePath);
222 s.setDatabasePath(b.databasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100223
224 // Enable/Disable the error console.
225 b.mTabControl.getBrowserActivity().setShouldShowErrorConsole(
226 b.showDebugSettings && b.showConsole);
Steve Blockf344d032009-07-30 10:50:45 +0100227
228 // Configure the Geolocation permissions manager to deny all
229 // permission requests if Geolocation is disabled in the browser.
230 // TODO(steveblock): Implement
The Android Open Source Project0c908882009-03-03 19:32:16 -0800231 }
232 }
233
234 /**
235 * Load settings from the browser app's database.
236 * NOTE: Strings used for the preferences must match those specified
237 * in the browser_preferences.xml
238 * @param ctx A Context object used to query the browser's settings
239 * database. If the database exists, the saved settings will be
240 * stored in this BrowserSettings object. This will update all
241 * observers of this object.
242 */
243 public void loadFromDb(Context ctx) {
244 SharedPreferences p =
245 PreferenceManager.getDefaultSharedPreferences(ctx);
246
247 // Set the default value for the plugins path to the application's
248 // local directory.
249 pluginsPath = ctx.getDir("plugins", 0).getPath();
Andrei Popescu5151ac12009-04-17 10:43:07 +0100250 // Set the default value for the Application Caches path.
251 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100252 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100253 webStorageSizeManager = new WebStorageSizeManager(
254 ctx,
255 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
256 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100257 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100258 // Set the default value for the Database path.
259 databasePath = ctx.getDir("databases", 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);
366 // HTML 5 configuration params
367 appCachePath = p.getString("appcache_path", appCachePath);
368 databasePath = p.getString("database_path", databasePath);
Steve Blockf344d032009-07-30 10:50:45 +0100369
Grace Klobad9ee1392009-07-20 11:53:33 -0700370 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800371 }
372
373 public String getPluginsPath() {
374 return pluginsPath;
375 }
376
377 public String getHomePage() {
378 return homeUrl;
379 }
380
Feng Qianb3c02da2009-06-29 15:58:08 -0700381 public String getJsFlags() {
382 return jsFlags;
383 }
384
Andrei Popescu79e82b72009-07-27 12:01:59 +0100385 public WebStorageSizeManager getWebStorageSizeManager() {
386 return webStorageSizeManager;
387 }
388
The Android Open Source Project0c908882009-03-03 19:32:16 -0800389 public void setHomePage(Context context, String url) {
390 Editor ed = PreferenceManager.
391 getDefaultSharedPreferences(context).edit();
392 ed.putString(PREF_HOMEPAGE, url);
393 ed.commit();
394 homeUrl = url;
395 }
396
397 public boolean isLoginInitialized() {
398 return loginInitialized;
399 }
400
401 public void setLoginInitialized(Context context) {
402 loginInitialized = true;
403 Editor ed = PreferenceManager.
404 getDefaultSharedPreferences(context).edit();
405 ed.putBoolean("login_initialized", loginInitialized);
406 ed.commit();
407 }
408
409 public WebSettings.TextSize getTextSize() {
410 return textSize;
411 }
412
Grace Kloba2f830682009-06-25 11:08:53 -0700413 public WebSettings.ZoomDensity getDefaultZoom() {
414 return zoomDensity;
415 }
416
The Android Open Source Project0c908882009-03-03 19:32:16 -0800417 public boolean openInBackground() {
418 return openInBackground;
419 }
420
421 public boolean showSecurityWarnings() {
422 return showSecurityWarnings;
423 }
424
425 public boolean isTracing() {
426 return tracing;
427 }
428
429 public boolean isLightTouch() {
430 return lightTouch;
431 }
432
433 public boolean isNavDump() {
434 return navDump;
435 }
436
437 public boolean doFlick() {
438 return doFlick;
439 }
440
441 public boolean showDebugSettings() {
442 return showDebugSettings;
443 }
444
445 public void toggleDebugSettings() {
446 showDebugSettings = !showDebugSettings;
447 navDump = showDebugSettings;
448 update();
449 }
450
451 /**
452 * Add a WebSettings object to the list of observers that will be updated
453 * when update() is called.
454 *
455 * @param s A WebSettings object that is strictly tied to the life of a
456 * WebView.
457 */
458 public Observer addObserver(WebSettings s) {
459 Observer old = mWebSettingsToObservers.get(s);
460 if (old != null) {
461 super.deleteObserver(old);
462 }
463 Observer o = new Observer(s);
464 mWebSettingsToObservers.put(s, o);
465 super.addObserver(o);
466 return o;
467 }
468
469 /**
470 * Delete the given WebSettings observer from the list of observers.
471 * @param s The WebSettings object to be deleted.
472 */
473 public void deleteObserver(WebSettings s) {
474 Observer o = mWebSettingsToObservers.get(s);
475 if (o != null) {
476 mWebSettingsToObservers.remove(s);
477 super.deleteObserver(o);
478 }
479 }
480
481 /*
482 * Package level method for obtaining a single app instance of the
483 * BrowserSettings.
484 */
485 /*package*/ static BrowserSettings getInstance() {
486 if (sSingleton == null ) {
487 sSingleton = new BrowserSettings();
488 }
489 return sSingleton;
490 }
491
492 /*
493 * Package level method for associating the BrowserSettings with TabControl
494 */
495 /* package */void setTabControl(TabControl tabControl) {
496 mTabControl = tabControl;
497 }
498
499 /*
500 * Update all the observers of the object.
501 */
502 /*package*/ void update() {
503 setChanged();
504 notifyObservers();
505 }
506
507 /*package*/ void clearCache(Context context) {
508 WebIconDatabase.getInstance().removeAllIcons();
509 if (mTabControl != null) {
510 WebView current = mTabControl.getCurrentWebView();
511 if (current != null) {
512 current.clearCache(true);
513 }
514 }
515 }
516
517 /*package*/ void clearCookies(Context context) {
518 CookieManager.getInstance().removeAllCookie();
519 }
520
521 /* package */void clearHistory(Context context) {
522 ContentResolver resolver = context.getContentResolver();
523 Browser.clearHistory(resolver);
524 Browser.clearSearches(resolver);
525 }
526
527 /* package */ void clearFormData(Context context) {
528 WebViewDatabase.getInstance(context).clearFormData();
529 if (mTabControl != null) {
530 mTabControl.getCurrentTopWebView().clearFormData();
531 }
532 }
533
534 /*package*/ void clearPasswords(Context context) {
535 WebViewDatabase db = WebViewDatabase.getInstance(context);
536 db.clearUsernamePassword();
537 db.clearHttpAuthUsernamePassword();
538 }
539
Steve Blockf344d032009-07-30 10:50:45 +0100540 private void maybeDisableWebsiteSettings(Context context) {
541 Set webStorageOrigins = WebStorage.getInstance().getOrigins();
542 Set geolocationOrigins =
543 GeolocationPermissions.getInstance().getOrigins();
544 if (((webStorageOrigins == null) || webStorageOrigins.isEmpty()) &&
545 ((geolocationOrigins == null) || geolocationOrigins.isEmpty())) {
546 PreferenceActivity activity = (PreferenceActivity) context;
547 PreferenceScreen screen = (PreferenceScreen)
548 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
549 screen.setEnabled(false);
550 }
551 }
552
Nicolas Roard78a98e42009-05-11 13:34:17 +0100553 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100554 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100555 maybeDisableWebsiteSettings(context);
556 }
557
558 /*package*/ void clearLocationAccess(Context context) {
559 GeolocationPermissions.getInstance().clearAll();
560 maybeDisableWebsiteSettings(context);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100561 }
562
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400563 /*package*/ void resetDefaultPreferences(Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800564 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400565 PreferenceManager.getDefaultSharedPreferences(ctx);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800566 p.edit().clear().commit();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400567 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800568 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700569 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700570 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100571 // reset appcache max size
572 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700573 }
574
575 private String getFactoryResetHomeUrl(Context context) {
576 String url = context.getResources().getString(R.string.homepage_base);
577 if (url.indexOf("{CID}") != -1) {
578 url = url.replace("{CID}", Partner.getString(context
Ramanan Rajeswaran9768ac52009-06-03 19:34:58 -0700579 .getContentResolver(), Partner.CLIENT_ID, "android-google"));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700580 }
581 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800582 }
583
The Android Open Source Project0c908882009-03-03 19:32:16 -0800584 // Private constructor that does nothing.
585 private BrowserSettings() {
586 }
587}