blob: 91b08a3bb1a66cce904d8092810bbf733c54c90b [file] [log] [blame]
Cary Clarkf2407c62009-09-04 12:25:10 -04001
The Android Open Source Project0c908882009-03-03 19:32:16 -08002/*
3 * Copyright (C) 2007 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.browser;
19
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070020import com.google.android.providers.GoogleSettings.Partner;
21
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.content.ContentResolver;
23import android.content.Context;
24import android.content.pm.ActivityInfo;
25import android.content.SharedPreferences;
26import android.content.SharedPreferences.Editor;
Nicolas Roard78a98e42009-05-11 13:34:17 +010027import android.preference.PreferenceActivity;
28import android.preference.PreferenceScreen;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010030import android.webkit.GeolocationPermissions;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.webkit.WebView;
32import android.webkit.WebViewDatabase;
33import android.webkit.WebIconDatabase;
34import android.webkit.WebSettings;
Nicolas Roard78a98e42009-05-11 13:34:17 +010035import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.preference.PreferenceManager;
37import android.provider.Browser;
38
Steve Blockf344d032009-07-30 10:50:45 +010039import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080040import java.util.HashMap;
41import java.util.Observable;
42
43/*
44 * Package level class for storing various WebView and Browser settings. To use
45 * this class:
46 * BrowserSettings s = BrowserSettings.getInstance();
47 * s.addObserver(webView.getSettings());
48 * s.loadFromDb(context); // Only needed on app startup
49 * s.javaScriptEnabled = true;
50 * ... // set any other settings
51 * s.update(); // this will update all the observers
52 *
53 * To remove an observer:
54 * s.deleteObserver(webView.getSettings());
55 */
56class BrowserSettings extends Observable {
57
Leon Scroggins9ac587d2009-04-20 12:53:46 -040058 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080059 // NOTE: these defaults need to be kept in sync with the XML
60 // until the performance of PreferenceManager.setDefaultValues()
61 // is improved.
62 private boolean loadsImagesAutomatically = true;
63 private boolean javaScriptEnabled = true;
64 private boolean pluginsEnabled = true;
65 private String pluginsPath; // default value set in loadFromDb().
66 private boolean javaScriptCanOpenWindowsAutomatically = false;
67 private boolean showSecurityWarnings = true;
68 private boolean rememberPasswords = true;
69 private boolean saveFormData = true;
70 private boolean openInBackground = false;
71 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070072 private String homeUrl = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -080073 private boolean loginInitialized = false;
74 private boolean autoFitPage = true;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -070075 private boolean landscapeOnly = false;
Grace Kloba5b4b8f12009-08-05 17:19:17 -070076 private boolean loadsPageInOverviewMode = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -080077 private boolean showDebugSettings = false;
Andrei Popescu01068702009-08-03 16:03:24 +010078 // HTML5 API flags
Andrei Popescu5151ac12009-04-17 10:43:07 +010079 private boolean appCacheEnabled = true;
Andrei Popescu01068702009-08-03 16:03:24 +010080 private boolean databaseEnabled = true;
Ben Murdoch734a0662009-06-02 19:11:52 +010081 private boolean domStorageEnabled = true;
Steve Blockf344d032009-07-30 10:50:45 +010082 private boolean geolocationEnabled = true;
Andrei Popescu01068702009-08-03 16:03:24 +010083 private boolean workersEnabled = true; // only affects V8. JSC does not have a similar setting
84 // HTML5 API configuration params
85 private long appCacheMaxSize = Long.MAX_VALUE;
86 private String appCachePath; // default value set in loadFromDb().
87 private String databasePath; // default value set in loadFromDb()
Steve Block5029cf02009-08-21 13:16:58 +010088 private String geolocationDatabasePath; // default value set in loadFromDb()
Andrei Popescu01068702009-08-03 16:03:24 +010089 private WebStorageSizeManager webStorageSizeManager;
90
91 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -080092
Nicolas Roard78a98e42009-05-11 13:34:17 +010093 private final static String TAG = "BrowserSettings";
94
The Android Open Source Project0c908882009-03-03 19:32:16 -080095 // Development settings
96 public WebSettings.LayoutAlgorithm layoutAlgorithm =
97 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
98 private boolean useWideViewPort = true;
99 private int userAgent = 0;
100 private boolean tracing = false;
101 private boolean lightTouch = false;
102 private boolean navDump = false;
Feng Qianb3c02da2009-06-29 15:58:08 -0700103
Ben Murdochbff2d602009-07-01 20:19:05 +0100104 // By default the error console is shown once the user navigates to about:debug.
105 // The setting can be then toggled from the settings menu.
106 private boolean showConsole = true;
107
The Android Open Source Project0c908882009-03-03 19:32:16 -0800108 // Browser only settings
109 private boolean doFlick = false;
110
111 // Private preconfigured values
112 private static int minimumFontSize = 8;
113 private static int minimumLogicalFontSize = 8;
114 private static int defaultFontSize = 16;
115 private static int defaultFixedFontSize = 13;
116 private static WebSettings.TextSize textSize =
117 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700118 private static WebSettings.ZoomDensity zoomDensity =
119 WebSettings.ZoomDensity.MEDIUM;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800120
121 // Preference keys that are used outside this class
122 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
123 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
124 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
125 public final static String PREF_HOMEPAGE = "homepage";
126 public final static String PREF_CLEAR_FORM_DATA =
127 "privacy_clear_form_data";
128 public final static String PREF_CLEAR_PASSWORDS =
129 "privacy_clear_passwords";
130 public final static String PREF_EXTRAS_RESET_DEFAULTS =
131 "reset_default_preferences";
132 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100133 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800134 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700135 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800136 public final static String PREF_DEFAULT_TEXT_ENCODING =
137 "default_text_encoding";
Steve Blockc6f577f2009-08-20 18:11:08 +0100138 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
139 "privacy_clear_geolocation_access";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800140
141 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Grace Klobaa4fa6ee2009-06-26 00:34:46 -0700142 "U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, " +
143 "like Gecko) Version/4.0 Safari/530.17";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800144
145 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Grace Klobaa4fa6ee2009-06-26 00:34:46 -0700146 "CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 " +
147 "(KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800148
149 // Value to truncate strings when adding them to a TextView within
150 // a ListView
151 public final static int MAX_TEXTVIEW_LEN = 80;
152
153 private TabControl mTabControl;
154
155 // Single instance of the BrowserSettings for use in the Browser app.
156 private static BrowserSettings sSingleton;
157
158 // Private map of WebSettings to Observer objects used when deleting an
159 // observer.
160 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
161 new HashMap<WebSettings,Observer>();
162
163 /*
164 * An observer wrapper for updating a WebSettings object with the new
165 * settings after a call to BrowserSettings.update().
166 */
167 static class Observer implements java.util.Observer {
168 // Private WebSettings object that will be updated.
169 private WebSettings mSettings;
170
171 Observer(WebSettings w) {
172 mSettings = w;
173 }
174
175 public void update(Observable o, Object arg) {
176 BrowserSettings b = (BrowserSettings)o;
177 WebSettings s = mSettings;
178
179 s.setLayoutAlgorithm(b.layoutAlgorithm);
180 if (b.userAgent == 0) {
181 // use the default ua string
182 s.setUserAgentString(null);
183 } else if (b.userAgent == 1) {
184 s.setUserAgentString(DESKTOP_USERAGENT);
185 } else if (b.userAgent == 2) {
186 s.setUserAgentString(IPHONE_USERAGENT);
187 }
188 s.setUseWideViewPort(b.useWideViewPort);
189 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
190 s.setJavaScriptEnabled(b.javaScriptEnabled);
191 s.setPluginsEnabled(b.pluginsEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800192 s.setJavaScriptCanOpenWindowsAutomatically(
193 b.javaScriptCanOpenWindowsAutomatically);
194 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
195 s.setMinimumFontSize(b.minimumFontSize);
196 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
197 s.setDefaultFontSize(b.defaultFontSize);
198 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
199 s.setNavDump(b.navDump);
200 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700201 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800202 s.setLightTouchEnabled(b.lightTouch);
203 s.setSaveFormData(b.saveFormData);
204 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700205 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800206
207 // WebView inside Browser doesn't want initial focus to be set.
208 s.setNeedInitialFocus(false);
209 // Browser supports multiple windows
210 s.setSupportMultipleWindows(true);
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.
Steve Block97b08022009-08-21 10:26:25 +0100217 s.setGeolocationEnabled(b.geolocationEnabled);
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);
Steve Block5029cf02009-08-21 13:16:58 +0100223 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100224
225 // Enable/Disable the error console.
226 b.mTabControl.getBrowserActivity().setShouldShowErrorConsole(
227 b.showDebugSettings && b.showConsole);
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();
Steve Block5029cf02009-08-21 13:16:58 +0100257 // Set the default value for the Geolocation database path.
258 geolocationDatabasePath = ctx.getDir("geolocation", 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);
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}