blob: 3ed6cf00b030df7d06610202de9559ed85306819 [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;
The Android Open Source Project0c908882009-03-03 19:32:16 -080075 private boolean showDebugSettings = false;
Ben Murdoch092dd5d2009-04-22 12:34:12 +010076 private String databasePath; // default value set in loadFromDb()
77 private boolean databaseEnabled = true;
Nicolas Roard78a98e42009-05-11 13:34:17 +010078 private long webStorageDefaultQuota = 5 * 1024 * 1024;
Andrei Popescu5151ac12009-04-17 10:43:07 +010079 // The Browser always enables Application Caches.
80 private boolean appCacheEnabled = true;
81 private String appCachePath; // default value set in loadFromDb().
Andrei Popescu20634ce2009-07-22 16:46:55 +010082 private long appCacheMaxSize = Long.MAX_VALUE;
Andrei Popescu79e82b72009-07-27 12:01:59 +010083 private WebStorageSizeManager webStorageSizeManager;
Ben Murdoch734a0662009-06-02 19:11:52 +010084 private boolean domStorageEnabled = true;
Feng Qianb3c02da2009-06-29 15:58:08 -070085 private String jsFlags = "";
Steve Blockf344d032009-07-30 10:50:45 +010086 private boolean geolocationEnabled = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -080087
Nicolas Roard78a98e42009-05-11 13:34:17 +010088 private final static String TAG = "BrowserSettings";
89
The Android Open Source Project0c908882009-03-03 19:32:16 -080090 // Development settings
91 public WebSettings.LayoutAlgorithm layoutAlgorithm =
92 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
93 private boolean useWideViewPort = true;
94 private int userAgent = 0;
95 private boolean tracing = false;
96 private boolean lightTouch = false;
97 private boolean navDump = false;
Feng Qianb3c02da2009-06-29 15:58:08 -070098
Ben Murdochbff2d602009-07-01 20:19:05 +010099 // By default the error console is shown once the user navigates to about:debug.
100 // The setting can be then toggled from the settings menu.
101 private boolean showConsole = true;
102
The Android Open Source Project0c908882009-03-03 19:32:16 -0800103 // Browser only settings
104 private boolean doFlick = false;
105
106 // Private preconfigured values
107 private static int minimumFontSize = 8;
108 private static int minimumLogicalFontSize = 8;
109 private static int defaultFontSize = 16;
110 private static int defaultFixedFontSize = 13;
111 private static WebSettings.TextSize textSize =
112 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700113 private static WebSettings.ZoomDensity zoomDensity =
114 WebSettings.ZoomDensity.MEDIUM;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800115
116 // Preference keys that are used outside this class
117 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
118 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
119 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
120 public final static String PREF_HOMEPAGE = "homepage";
121 public final static String PREF_CLEAR_FORM_DATA =
122 "privacy_clear_form_data";
123 public final static String PREF_CLEAR_PASSWORDS =
124 "privacy_clear_passwords";
Nicolas Roard78a98e42009-05-11 13:34:17 +0100125 public final static String PREF_DEFAULT_QUOTA =
126 "webstorage_default_quota";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800127 public final static String PREF_EXTRAS_RESET_DEFAULTS =
128 "reset_default_preferences";
129 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
130 public final static String PREF_GEARS_SETTINGS = "gears_settings";
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);
203
204 // WebView inside Browser doesn't want initial focus to be set.
205 s.setNeedInitialFocus(false);
206 // Browser supports multiple windows
207 s.setSupportMultipleWindows(true);
208 // Turn off file access
209 s.setAllowFileAccess(false);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100210
211 s.setDatabasePath(b.databasePath);
212 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100213 s.setDomStorageEnabled(b.domStorageEnabled);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100214 s.setWebStorageDefaultQuota(b.webStorageDefaultQuota);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100215
Andrei Popescu5151ac12009-04-17 10:43:07 +0100216 // Turn on Application Caches.
217 s.setAppCachePath(b.appCachePath);
218 s.setAppCacheEnabled(b.appCacheEnabled);
Andrei Popescu20634ce2009-07-22 16:46:55 +0100219 s.setAppCacheMaxSize(b.appCacheMaxSize);
Ben Murdochbff2d602009-07-01 20:19:05 +0100220
221 // Enable/Disable the error console.
222 b.mTabControl.getBrowserActivity().setShouldShowErrorConsole(
223 b.showDebugSettings && b.showConsole);
Steve Blockf344d032009-07-30 10:50:45 +0100224
225 // Configure the Geolocation permissions manager to deny all
226 // permission requests if Geolocation is disabled in the browser.
227 // TODO(steveblock): Implement
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);
243
244 // Set the default value for the plugins path to the application's
245 // local directory.
246 pluginsPath = ctx.getDir("plugins", 0).getPath();
Andrei Popescu5151ac12009-04-17 10:43:07 +0100247 // Set the default value for the Application Caches path.
248 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100249 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100250 webStorageSizeManager = new WebStorageSizeManager(
251 ctx,
252 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
253 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100254 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100255 // Set the default value for the Database path.
256 databasePath = ctx.getDir("databases", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800257
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700258 homeUrl = getFactoryResetHomeUrl(ctx);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700259
The Android Open Source Project0c908882009-03-03 19:32:16 -0800260 // Load the defaults from the xml
261 // This call is TOO SLOW, need to manually keep the defaults
262 // in sync
263 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
264 syncSharedPreferences(p);
265 }
266
267 /* package */ void syncSharedPreferences(SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700268
The Android Open Source Project0c908882009-03-03 19:32:16 -0800269 homeUrl =
270 p.getString(PREF_HOMEPAGE, homeUrl);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700271
The Android Open Source Project0c908882009-03-03 19:32:16 -0800272 loadsImagesAutomatically = p.getBoolean("load_images",
273 loadsImagesAutomatically);
274 javaScriptEnabled = p.getBoolean("enable_javascript",
275 javaScriptEnabled);
276 pluginsEnabled = p.getBoolean("enable_plugins",
277 pluginsEnabled);
278 pluginsPath = p.getString("plugins_path", pluginsPath);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100279 databasePath = p.getString("database_path", databasePath);
280 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100281 webStorageDefaultQuota = Long.parseLong(p.getString(PREF_DEFAULT_QUOTA,
282 String.valueOf(webStorageDefaultQuota)));
Andrei Popescu5151ac12009-04-17 10:43:07 +0100283 appCacheEnabled = p.getBoolean("enable_appcache",
Ben Murdoch734a0662009-06-02 19:11:52 +0100284 appCacheEnabled);
285 domStorageEnabled = p.getBoolean("enable_domstorage",
286 domStorageEnabled);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100287 appCachePath = p.getString("appcache_path", appCachePath);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800288 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
289 "block_popup_windows",
290 !javaScriptCanOpenWindowsAutomatically);
291 showSecurityWarnings = p.getBoolean("show_security_warnings",
292 showSecurityWarnings);
293 rememberPasswords = p.getBoolean("remember_passwords",
294 rememberPasswords);
295 saveFormData = p.getBoolean("save_formdata",
296 saveFormData);
297 boolean accept_cookies = p.getBoolean("accept_cookies",
298 CookieManager.getInstance().acceptCookie());
299 CookieManager.getInstance().setAcceptCookie(accept_cookies);
300 openInBackground = p.getBoolean("open_in_background", openInBackground);
301 loginInitialized = p.getBoolean("login_initialized", loginInitialized);
302 textSize = WebSettings.TextSize.valueOf(
303 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700304 zoomDensity = WebSettings.ZoomDensity.valueOf(
305 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800306 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700307 boolean landscapeOnlyTemp =
308 p.getBoolean("landscape_only", landscapeOnly);
309 if (landscapeOnlyTemp != landscapeOnly) {
310 landscapeOnly = landscapeOnlyTemp;
311 mTabControl.getBrowserActivity().setRequestedOrientation(
312 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
313 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
314 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800315 useWideViewPort = true; // use wide view port for either setting
316 if (autoFitPage) {
317 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
318 } else {
319 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
320 }
321 defaultTextEncodingName =
322 p.getString(PREF_DEFAULT_TEXT_ENCODING,
323 defaultTextEncodingName);
324
325 showDebugSettings =
326 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
327 // Debug menu items have precidence if the menu is visible
328 if (showDebugSettings) {
329 boolean small_screen = p.getBoolean("small_screen",
330 layoutAlgorithm ==
331 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
332 if (small_screen) {
333 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
334 } else {
335 boolean normal_layout = p.getBoolean("normal_layout",
336 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
337 if (normal_layout) {
338 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
339 } else {
340 layoutAlgorithm =
341 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
342 }
343 }
344 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
345 tracing = p.getBoolean("enable_tracing", tracing);
346 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
347 navDump = p.getBoolean("enable_nav_dump", navDump);
348 doFlick = p.getBoolean("enable_flick", doFlick);
349 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800350 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700351 // JS flags is loaded from DB even if showDebugSettings is false,
352 // so that it can be set once and be effective all the time.
353 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100354
355 // Read the setting for showing/hiding the JS Console always so that should the
356 // user enable debug settings, we already know if we should show the console.
357 // The user will never see the console unless they navigate to about:debug,
358 // regardless of the setting we read here. This setting is only used after debug
359 // is enabled.
360 showConsole = p.getBoolean("javascript_console", showConsole);
361 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
362 showDebugSettings && showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700363
Steve Blockf344d032009-07-30 10:50:45 +0100364 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
365
Grace Klobad9ee1392009-07-20 11:53:33 -0700366 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800367 }
368
369 public String getPluginsPath() {
370 return pluginsPath;
371 }
372
373 public String getHomePage() {
374 return homeUrl;
375 }
376
Feng Qianb3c02da2009-06-29 15:58:08 -0700377 public String getJsFlags() {
378 return jsFlags;
379 }
380
Andrei Popescu79e82b72009-07-27 12:01:59 +0100381 public WebStorageSizeManager getWebStorageSizeManager() {
382 return webStorageSizeManager;
383 }
384
The Android Open Source Project0c908882009-03-03 19:32:16 -0800385 public void setHomePage(Context context, String url) {
386 Editor ed = PreferenceManager.
387 getDefaultSharedPreferences(context).edit();
388 ed.putString(PREF_HOMEPAGE, url);
389 ed.commit();
390 homeUrl = url;
391 }
392
393 public boolean isLoginInitialized() {
394 return loginInitialized;
395 }
396
397 public void setLoginInitialized(Context context) {
398 loginInitialized = true;
399 Editor ed = PreferenceManager.
400 getDefaultSharedPreferences(context).edit();
401 ed.putBoolean("login_initialized", loginInitialized);
402 ed.commit();
403 }
404
405 public WebSettings.TextSize getTextSize() {
406 return textSize;
407 }
408
Grace Kloba2f830682009-06-25 11:08:53 -0700409 public WebSettings.ZoomDensity getDefaultZoom() {
410 return zoomDensity;
411 }
412
The Android Open Source Project0c908882009-03-03 19:32:16 -0800413 public boolean openInBackground() {
414 return openInBackground;
415 }
416
417 public boolean showSecurityWarnings() {
418 return showSecurityWarnings;
419 }
420
421 public boolean isTracing() {
422 return tracing;
423 }
424
425 public boolean isLightTouch() {
426 return lightTouch;
427 }
428
429 public boolean isNavDump() {
430 return navDump;
431 }
432
433 public boolean doFlick() {
434 return doFlick;
435 }
436
437 public boolean showDebugSettings() {
438 return showDebugSettings;
439 }
440
441 public void toggleDebugSettings() {
442 showDebugSettings = !showDebugSettings;
443 navDump = showDebugSettings;
444 update();
445 }
446
447 /**
448 * Add a WebSettings object to the list of observers that will be updated
449 * when update() is called.
450 *
451 * @param s A WebSettings object that is strictly tied to the life of a
452 * WebView.
453 */
454 public Observer addObserver(WebSettings s) {
455 Observer old = mWebSettingsToObservers.get(s);
456 if (old != null) {
457 super.deleteObserver(old);
458 }
459 Observer o = new Observer(s);
460 mWebSettingsToObservers.put(s, o);
461 super.addObserver(o);
462 return o;
463 }
464
465 /**
466 * Delete the given WebSettings observer from the list of observers.
467 * @param s The WebSettings object to be deleted.
468 */
469 public void deleteObserver(WebSettings s) {
470 Observer o = mWebSettingsToObservers.get(s);
471 if (o != null) {
472 mWebSettingsToObservers.remove(s);
473 super.deleteObserver(o);
474 }
475 }
476
477 /*
478 * Package level method for obtaining a single app instance of the
479 * BrowserSettings.
480 */
481 /*package*/ static BrowserSettings getInstance() {
482 if (sSingleton == null ) {
483 sSingleton = new BrowserSettings();
484 }
485 return sSingleton;
486 }
487
488 /*
489 * Package level method for associating the BrowserSettings with TabControl
490 */
491 /* package */void setTabControl(TabControl tabControl) {
492 mTabControl = tabControl;
493 }
494
495 /*
496 * Update all the observers of the object.
497 */
498 /*package*/ void update() {
499 setChanged();
500 notifyObservers();
501 }
502
503 /*package*/ void clearCache(Context context) {
504 WebIconDatabase.getInstance().removeAllIcons();
505 if (mTabControl != null) {
506 WebView current = mTabControl.getCurrentWebView();
507 if (current != null) {
508 current.clearCache(true);
509 }
510 }
511 }
512
513 /*package*/ void clearCookies(Context context) {
514 CookieManager.getInstance().removeAllCookie();
515 }
516
517 /* package */void clearHistory(Context context) {
518 ContentResolver resolver = context.getContentResolver();
519 Browser.clearHistory(resolver);
520 Browser.clearSearches(resolver);
521 }
522
523 /* package */ void clearFormData(Context context) {
524 WebViewDatabase.getInstance(context).clearFormData();
525 if (mTabControl != null) {
526 mTabControl.getCurrentTopWebView().clearFormData();
527 }
528 }
529
530 /*package*/ void clearPasswords(Context context) {
531 WebViewDatabase db = WebViewDatabase.getInstance(context);
532 db.clearUsernamePassword();
533 db.clearHttpAuthUsernamePassword();
534 }
535
Steve Blockf344d032009-07-30 10:50:45 +0100536 private void maybeDisableWebsiteSettings(Context context) {
537 Set webStorageOrigins = WebStorage.getInstance().getOrigins();
538 Set geolocationOrigins =
539 GeolocationPermissions.getInstance().getOrigins();
540 if (((webStorageOrigins == null) || webStorageOrigins.isEmpty()) &&
541 ((geolocationOrigins == null) || geolocationOrigins.isEmpty())) {
542 PreferenceActivity activity = (PreferenceActivity) context;
543 PreferenceScreen screen = (PreferenceScreen)
544 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
545 screen.setEnabled(false);
546 }
547 }
548
Nicolas Roard78a98e42009-05-11 13:34:17 +0100549 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100550 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100551 maybeDisableWebsiteSettings(context);
552 }
553
554 /*package*/ void clearLocationAccess(Context context) {
555 GeolocationPermissions.getInstance().clearAll();
556 maybeDisableWebsiteSettings(context);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100557 }
558
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400559 /*package*/ void resetDefaultPreferences(Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800560 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400561 PreferenceManager.getDefaultSharedPreferences(ctx);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800562 p.edit().clear().commit();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400563 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800564 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700565 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700566 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100567 // reset appcache max size
568 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700569 }
570
571 private String getFactoryResetHomeUrl(Context context) {
572 String url = context.getResources().getString(R.string.homepage_base);
573 if (url.indexOf("{CID}") != -1) {
574 url = url.replace("{CID}", Partner.getString(context
Ramanan Rajeswaran9768ac52009-06-03 19:34:58 -0700575 .getContentResolver(), Partner.CLIENT_ID, "android-google"));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700576 }
577 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800578 }
579
The Android Open Source Project0c908882009-03-03 19:32:16 -0800580 // Private constructor that does nothing.
581 private BrowserSettings() {
582 }
583}