blob: 05b05d6b507aff5239d51ec2221278a34cddf024 [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;
Andrei Popescu01068702009-08-03 16:03:24 +010076 // HTML5 API flags
Andrei Popescu5151ac12009-04-17 10:43:07 +010077 private boolean appCacheEnabled = true;
Andrei Popescu01068702009-08-03 16:03:24 +010078 private boolean databaseEnabled = true;
Ben Murdoch734a0662009-06-02 19:11:52 +010079 private boolean domStorageEnabled = true;
Steve Blockf344d032009-07-30 10:50:45 +010080 private boolean geolocationEnabled = true;
Andrei Popescu01068702009-08-03 16:03:24 +010081 private boolean workersEnabled = true; // only affects V8. JSC does not have a similar setting
82 // HTML5 API configuration params
83 private long appCacheMaxSize = Long.MAX_VALUE;
84 private String appCachePath; // default value set in loadFromDb().
85 private String databasePath; // default value set in loadFromDb()
86 private WebStorageSizeManager webStorageSizeManager;
87
88 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -080089
Nicolas Roard78a98e42009-05-11 13:34:17 +010090 private final static String TAG = "BrowserSettings";
91
The Android Open Source Project0c908882009-03-03 19:32:16 -080092 // Development settings
93 public WebSettings.LayoutAlgorithm layoutAlgorithm =
94 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
95 private boolean useWideViewPort = true;
96 private int userAgent = 0;
97 private boolean tracing = false;
98 private boolean lightTouch = false;
99 private boolean navDump = false;
Feng Qianb3c02da2009-06-29 15:58:08 -0700100
Ben Murdochbff2d602009-07-01 20:19:05 +0100101 // By default the error console is shown once the user navigates to about:debug.
102 // The setting can be then toggled from the settings menu.
103 private boolean showConsole = true;
104
The Android Open Source Project0c908882009-03-03 19:32:16 -0800105 // Browser only settings
106 private boolean doFlick = false;
107
108 // Private preconfigured values
109 private static int minimumFontSize = 8;
110 private static int minimumLogicalFontSize = 8;
111 private static int defaultFontSize = 16;
112 private static int defaultFixedFontSize = 13;
113 private static WebSettings.TextSize textSize =
114 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700115 private static WebSettings.ZoomDensity zoomDensity =
116 WebSettings.ZoomDensity.MEDIUM;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800117
118 // Preference keys that are used outside this class
119 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
120 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
121 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
122 public final static String PREF_HOMEPAGE = "homepage";
123 public final static String PREF_CLEAR_FORM_DATA =
124 "privacy_clear_form_data";
125 public final static String PREF_CLEAR_PASSWORDS =
126 "privacy_clear_passwords";
127 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
Andrei Popescu01068702009-08-03 16:03:24 +0100211 // HTML5 API flags
212 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100213 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100214 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100215 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100216
Andrei Popescu01068702009-08-03 16:03:24 +0100217 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100218 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100219 s.setAppCachePath(b.appCachePath);
220 s.setDatabasePath(b.databasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100221
222 // Enable/Disable the error console.
223 b.mTabControl.getBrowserActivity().setShouldShowErrorConsole(
224 b.showDebugSettings && b.showConsole);
Steve Blockf344d032009-07-30 10:50:45 +0100225
226 // Configure the Geolocation permissions manager to deny all
227 // permission requests if Geolocation is disabled in the browser.
228 // TODO(steveblock): Implement
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();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800258
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700259 homeUrl = getFactoryResetHomeUrl(ctx);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700260
The Android Open Source Project0c908882009-03-03 19:32:16 -0800261 // Load the defaults from the xml
262 // This call is TOO SLOW, need to manually keep the defaults
263 // in sync
264 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
265 syncSharedPreferences(p);
266 }
267
268 /* package */ void syncSharedPreferences(SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700269
The Android Open Source Project0c908882009-03-03 19:32:16 -0800270 homeUrl =
271 p.getString(PREF_HOMEPAGE, homeUrl);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700272
The Android Open Source Project0c908882009-03-03 19:32:16 -0800273 loadsImagesAutomatically = p.getBoolean("load_images",
274 loadsImagesAutomatically);
275 javaScriptEnabled = p.getBoolean("enable_javascript",
276 javaScriptEnabled);
277 pluginsEnabled = p.getBoolean("enable_plugins",
278 pluginsEnabled);
279 pluginsPath = p.getString("plugins_path", pluginsPath);
280 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
281 "block_popup_windows",
282 !javaScriptCanOpenWindowsAutomatically);
283 showSecurityWarnings = p.getBoolean("show_security_warnings",
284 showSecurityWarnings);
285 rememberPasswords = p.getBoolean("remember_passwords",
286 rememberPasswords);
287 saveFormData = p.getBoolean("save_formdata",
288 saveFormData);
289 boolean accept_cookies = p.getBoolean("accept_cookies",
290 CookieManager.getInstance().acceptCookie());
291 CookieManager.getInstance().setAcceptCookie(accept_cookies);
292 openInBackground = p.getBoolean("open_in_background", openInBackground);
293 loginInitialized = p.getBoolean("login_initialized", loginInitialized);
294 textSize = WebSettings.TextSize.valueOf(
295 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700296 zoomDensity = WebSettings.ZoomDensity.valueOf(
297 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800298 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700299 boolean landscapeOnlyTemp =
300 p.getBoolean("landscape_only", landscapeOnly);
301 if (landscapeOnlyTemp != landscapeOnly) {
302 landscapeOnly = landscapeOnlyTemp;
303 mTabControl.getBrowserActivity().setRequestedOrientation(
304 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
305 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
306 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800307 useWideViewPort = true; // use wide view port for either setting
308 if (autoFitPage) {
309 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
310 } else {
311 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
312 }
313 defaultTextEncodingName =
314 p.getString(PREF_DEFAULT_TEXT_ENCODING,
315 defaultTextEncodingName);
316
317 showDebugSettings =
318 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
319 // Debug menu items have precidence if the menu is visible
320 if (showDebugSettings) {
321 boolean small_screen = p.getBoolean("small_screen",
322 layoutAlgorithm ==
323 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
324 if (small_screen) {
325 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
326 } else {
327 boolean normal_layout = p.getBoolean("normal_layout",
328 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
329 if (normal_layout) {
330 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
331 } else {
332 layoutAlgorithm =
333 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
334 }
335 }
336 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
337 tracing = p.getBoolean("enable_tracing", tracing);
338 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
339 navDump = p.getBoolean("enable_nav_dump", navDump);
340 doFlick = p.getBoolean("enable_flick", doFlick);
341 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800342 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700343 // JS flags is loaded from DB even if showDebugSettings is false,
344 // so that it can be set once and be effective all the time.
345 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100346
347 // Read the setting for showing/hiding the JS Console always so that should the
348 // user enable debug settings, we already know if we should show the console.
349 // The user will never see the console unless they navigate to about:debug,
350 // regardless of the setting we read here. This setting is only used after debug
351 // is enabled.
352 showConsole = p.getBoolean("javascript_console", showConsole);
353 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
354 showDebugSettings && showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700355
Andrei Popescu01068702009-08-03 16:03:24 +0100356 // HTML5 API flags
357 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
358 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
359 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100360 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100361 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
362 // HTML 5 configuration params
363 appCachePath = p.getString("appcache_path", appCachePath);
364 databasePath = p.getString("database_path", databasePath);
Steve Blockf344d032009-07-30 10:50:45 +0100365
Grace Klobad9ee1392009-07-20 11:53:33 -0700366 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800367 }
368
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}