blob: 6f48923c2a7e370b23d25bb59fe8785427f0b651 [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;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010031import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.webkit.WebView;
33import android.webkit.WebViewDatabase;
34import android.webkit.WebIconDatabase;
35import android.webkit.WebSettings;
Nicolas Roard78a98e42009-05-11 13:34:17 +010036import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.preference.PreferenceManager;
38import android.provider.Browser;
39
40import java.util.HashMap;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010041import java.util.Map;
42import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import java.util.Observable;
44
45/*
46 * Package level class for storing various WebView and Browser settings. To use
47 * this class:
48 * BrowserSettings s = BrowserSettings.getInstance();
49 * s.addObserver(webView.getSettings());
50 * s.loadFromDb(context); // Only needed on app startup
51 * s.javaScriptEnabled = true;
52 * ... // set any other settings
53 * s.update(); // this will update all the observers
54 *
55 * To remove an observer:
56 * s.deleteObserver(webView.getSettings());
57 */
58class BrowserSettings extends Observable {
59
Leon Scroggins9ac587d2009-04-20 12:53:46 -040060 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080061 // NOTE: these defaults need to be kept in sync with the XML
62 // until the performance of PreferenceManager.setDefaultValues()
63 // is improved.
64 private boolean loadsImagesAutomatically = true;
65 private boolean javaScriptEnabled = true;
66 private boolean pluginsEnabled = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -080067 private boolean javaScriptCanOpenWindowsAutomatically = false;
68 private boolean showSecurityWarnings = true;
69 private boolean rememberPasswords = true;
70 private boolean saveFormData = true;
71 private boolean openInBackground = false;
72 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070073 private String homeUrl = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -080074 private boolean loginInitialized = false;
75 private boolean autoFitPage = true;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -070076 private boolean landscapeOnly = false;
Grace Kloba5b4b8f12009-08-05 17:19:17 -070077 private boolean loadsPageInOverviewMode = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -080078 private boolean showDebugSettings = false;
Andrei Popescu01068702009-08-03 16:03:24 +010079 // HTML5 API flags
Andrei Popescu5151ac12009-04-17 10:43:07 +010080 private boolean appCacheEnabled = true;
Andrei Popescu01068702009-08-03 16:03:24 +010081 private boolean databaseEnabled = true;
Ben Murdoch734a0662009-06-02 19:11:52 +010082 private boolean domStorageEnabled = true;
Steve Blockf344d032009-07-30 10:50:45 +010083 private boolean geolocationEnabled = true;
Andrei Popescu01068702009-08-03 16:03:24 +010084 private boolean workersEnabled = true; // only affects V8. JSC does not have a similar setting
85 // HTML5 API configuration params
86 private long appCacheMaxSize = Long.MAX_VALUE;
87 private String appCachePath; // default value set in loadFromDb().
88 private String databasePath; // default value set in loadFromDb()
Steve Block5029cf02009-08-21 13:16:58 +010089 private String geolocationDatabasePath; // default value set in loadFromDb()
Andrei Popescu01068702009-08-03 16:03:24 +010090 private WebStorageSizeManager webStorageSizeManager;
91
92 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -080093
Nicolas Roard78a98e42009-05-11 13:34:17 +010094 private final static String TAG = "BrowserSettings";
95
The Android Open Source Project0c908882009-03-03 19:32:16 -080096 // Development settings
97 public WebSettings.LayoutAlgorithm layoutAlgorithm =
98 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
99 private boolean useWideViewPort = true;
100 private int userAgent = 0;
101 private boolean tracing = false;
102 private boolean lightTouch = false;
103 private boolean navDump = false;
Feng Qianb3c02da2009-06-29 15:58:08 -0700104
Ben Murdochbff2d602009-07-01 20:19:05 +0100105 // By default the error console is shown once the user navigates to about:debug.
106 // The setting can be then toggled from the settings menu.
107 private boolean showConsole = true;
108
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109 // 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;
Grace Kloba79565032009-11-24 14:23:39 -0800118 private static int pageCacheCapacity = 10;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119
120 // Preference keys that are used outside this class
121 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
122 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
123 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
124 public final static String PREF_HOMEPAGE = "homepage";
125 public final static String PREF_CLEAR_FORM_DATA =
126 "privacy_clear_form_data";
127 public final static String PREF_CLEAR_PASSWORDS =
128 "privacy_clear_passwords";
129 public final static String PREF_EXTRAS_RESET_DEFAULTS =
130 "reset_default_preferences";
131 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
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 Blockc6f577f2009-08-20 18:11:08 +0100137 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
138 "privacy_clear_geolocation_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);
Grace Kloba79565032009-11-24 14:23:39 -0800205 s.setPageCacheCapacity(pageCacheCapacity);
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);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100243 // Set the default value for the Application Caches path.
244 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100245 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100246 webStorageSizeManager = new WebStorageSizeManager(
247 ctx,
248 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
249 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100250 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100251 // Set the default value for the Database path.
252 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100253 // Set the default value for the Geolocation database path.
254 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800255
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700256 homeUrl = getFactoryResetHomeUrl(ctx);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700257
The Android Open Source Project0c908882009-03-03 19:32:16 -0800258 // Load the defaults from the xml
259 // This call is TOO SLOW, need to manually keep the defaults
260 // in sync
261 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
262 syncSharedPreferences(p);
263 }
264
265 /* package */ void syncSharedPreferences(SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700266
The Android Open Source Project0c908882009-03-03 19:32:16 -0800267 homeUrl =
268 p.getString(PREF_HOMEPAGE, homeUrl);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700269
The Android Open Source Project0c908882009-03-03 19:32:16 -0800270 loadsImagesAutomatically = p.getBoolean("load_images",
271 loadsImagesAutomatically);
272 javaScriptEnabled = p.getBoolean("enable_javascript",
273 javaScriptEnabled);
274 pluginsEnabled = p.getBoolean("enable_plugins",
275 pluginsEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800276 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
277 "block_popup_windows",
278 !javaScriptCanOpenWindowsAutomatically);
279 showSecurityWarnings = p.getBoolean("show_security_warnings",
280 showSecurityWarnings);
281 rememberPasswords = p.getBoolean("remember_passwords",
282 rememberPasswords);
283 saveFormData = p.getBoolean("save_formdata",
284 saveFormData);
285 boolean accept_cookies = p.getBoolean("accept_cookies",
286 CookieManager.getInstance().acceptCookie());
287 CookieManager.getInstance().setAcceptCookie(accept_cookies);
288 openInBackground = p.getBoolean("open_in_background", openInBackground);
289 loginInitialized = p.getBoolean("login_initialized", loginInitialized);
290 textSize = WebSettings.TextSize.valueOf(
291 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700292 zoomDensity = WebSettings.ZoomDensity.valueOf(
293 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800294 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700295 loadsPageInOverviewMode = p.getBoolean("load_page",
296 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700297 boolean landscapeOnlyTemp =
298 p.getBoolean("landscape_only", landscapeOnly);
299 if (landscapeOnlyTemp != landscapeOnly) {
300 landscapeOnly = landscapeOnlyTemp;
301 mTabControl.getBrowserActivity().setRequestedOrientation(
302 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
303 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
304 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800305 useWideViewPort = true; // use wide view port for either setting
306 if (autoFitPage) {
307 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
308 } else {
309 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
310 }
311 defaultTextEncodingName =
312 p.getString(PREF_DEFAULT_TEXT_ENCODING,
313 defaultTextEncodingName);
314
315 showDebugSettings =
316 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
317 // Debug menu items have precidence if the menu is visible
318 if (showDebugSettings) {
319 boolean small_screen = p.getBoolean("small_screen",
320 layoutAlgorithm ==
321 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
322 if (small_screen) {
323 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
324 } else {
325 boolean normal_layout = p.getBoolean("normal_layout",
326 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
327 if (normal_layout) {
328 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
329 } else {
330 layoutAlgorithm =
331 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
332 }
333 }
334 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
335 tracing = p.getBoolean("enable_tracing", tracing);
336 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
337 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800338 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800339 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700340 // JS flags is loaded from DB even if showDebugSettings is false,
341 // so that it can be set once and be effective all the time.
342 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100343
344 // Read the setting for showing/hiding the JS Console always so that should the
345 // user enable debug settings, we already know if we should show the console.
346 // The user will never see the console unless they navigate to about:debug,
347 // regardless of the setting we read here. This setting is only used after debug
348 // is enabled.
349 showConsole = p.getBoolean("javascript_console", showConsole);
350 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
351 showDebugSettings && showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700352
Andrei Popescu01068702009-08-03 16:03:24 +0100353 // HTML5 API flags
354 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
355 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
356 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100357 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100358 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100359
Grace Klobad9ee1392009-07-20 11:53:33 -0700360 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800361 }
362
The Android Open Source Project0c908882009-03-03 19:32:16 -0800363 public String getHomePage() {
364 return homeUrl;
365 }
366
Feng Qianb3c02da2009-06-29 15:58:08 -0700367 public String getJsFlags() {
368 return jsFlags;
369 }
370
Andrei Popescu79e82b72009-07-27 12:01:59 +0100371 public WebStorageSizeManager getWebStorageSizeManager() {
372 return webStorageSizeManager;
373 }
374
The Android Open Source Project0c908882009-03-03 19:32:16 -0800375 public void setHomePage(Context context, String url) {
376 Editor ed = PreferenceManager.
377 getDefaultSharedPreferences(context).edit();
378 ed.putString(PREF_HOMEPAGE, url);
379 ed.commit();
380 homeUrl = url;
381 }
382
383 public boolean isLoginInitialized() {
384 return loginInitialized;
385 }
386
387 public void setLoginInitialized(Context context) {
388 loginInitialized = true;
389 Editor ed = PreferenceManager.
390 getDefaultSharedPreferences(context).edit();
391 ed.putBoolean("login_initialized", loginInitialized);
392 ed.commit();
393 }
394
395 public WebSettings.TextSize getTextSize() {
396 return textSize;
397 }
398
Grace Kloba2f830682009-06-25 11:08:53 -0700399 public WebSettings.ZoomDensity getDefaultZoom() {
400 return zoomDensity;
401 }
402
The Android Open Source Project0c908882009-03-03 19:32:16 -0800403 public boolean openInBackground() {
404 return openInBackground;
405 }
406
407 public boolean showSecurityWarnings() {
408 return showSecurityWarnings;
409 }
410
411 public boolean isTracing() {
412 return tracing;
413 }
414
415 public boolean isLightTouch() {
416 return lightTouch;
417 }
418
419 public boolean isNavDump() {
420 return navDump;
421 }
422
The Android Open Source Project0c908882009-03-03 19:32:16 -0800423 public boolean showDebugSettings() {
424 return showDebugSettings;
425 }
426
427 public void toggleDebugSettings() {
428 showDebugSettings = !showDebugSettings;
429 navDump = showDebugSettings;
430 update();
431 }
432
433 /**
434 * Add a WebSettings object to the list of observers that will be updated
435 * when update() is called.
436 *
437 * @param s A WebSettings object that is strictly tied to the life of a
438 * WebView.
439 */
440 public Observer addObserver(WebSettings s) {
441 Observer old = mWebSettingsToObservers.get(s);
442 if (old != null) {
443 super.deleteObserver(old);
444 }
445 Observer o = new Observer(s);
446 mWebSettingsToObservers.put(s, o);
447 super.addObserver(o);
448 return o;
449 }
450
451 /**
452 * Delete the given WebSettings observer from the list of observers.
453 * @param s The WebSettings object to be deleted.
454 */
455 public void deleteObserver(WebSettings s) {
456 Observer o = mWebSettingsToObservers.get(s);
457 if (o != null) {
458 mWebSettingsToObservers.remove(s);
459 super.deleteObserver(o);
460 }
461 }
462
463 /*
464 * Package level method for obtaining a single app instance of the
465 * BrowserSettings.
466 */
467 /*package*/ static BrowserSettings getInstance() {
468 if (sSingleton == null ) {
469 sSingleton = new BrowserSettings();
470 }
471 return sSingleton;
472 }
473
474 /*
475 * Package level method for associating the BrowserSettings with TabControl
476 */
477 /* package */void setTabControl(TabControl tabControl) {
478 mTabControl = tabControl;
479 }
480
481 /*
482 * Update all the observers of the object.
483 */
484 /*package*/ void update() {
485 setChanged();
486 notifyObservers();
487 }
488
489 /*package*/ void clearCache(Context context) {
490 WebIconDatabase.getInstance().removeAllIcons();
491 if (mTabControl != null) {
492 WebView current = mTabControl.getCurrentWebView();
493 if (current != null) {
494 current.clearCache(true);
495 }
496 }
497 }
498
499 /*package*/ void clearCookies(Context context) {
500 CookieManager.getInstance().removeAllCookie();
501 }
502
503 /* package */void clearHistory(Context context) {
504 ContentResolver resolver = context.getContentResolver();
505 Browser.clearHistory(resolver);
506 Browser.clearSearches(resolver);
507 }
508
509 /* package */ void clearFormData(Context context) {
510 WebViewDatabase.getInstance(context).clearFormData();
511 if (mTabControl != null) {
512 mTabControl.getCurrentTopWebView().clearFormData();
513 }
514 }
515
516 /*package*/ void clearPasswords(Context context) {
517 WebViewDatabase db = WebViewDatabase.getInstance(context);
518 db.clearUsernamePassword();
519 db.clearHttpAuthUsernamePassword();
520 }
521
Steve Blockf344d032009-07-30 10:50:45 +0100522 private void maybeDisableWebsiteSettings(Context context) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100523 PreferenceActivity activity = (PreferenceActivity) context;
524 final PreferenceScreen screen = (PreferenceScreen)
525 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
526 screen.setEnabled(false);
527 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
528 public void onReceiveValue(Map webStorageOrigins) {
529 if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) {
530 screen.setEnabled(true);
531 }
532 }
533 });
534
Steve Block2a6a0f42009-11-19 15:55:42 +0000535 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
536 public void onReceiveValue(Set<String> geolocationOrigins) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100537 if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) {
538 screen.setEnabled(true);
539 }
540 }
541 });
Steve Blockf344d032009-07-30 10:50:45 +0100542 }
543
Nicolas Roard78a98e42009-05-11 13:34:17 +0100544 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100545 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100546 maybeDisableWebsiteSettings(context);
547 }
548
549 /*package*/ void clearLocationAccess(Context context) {
550 GeolocationPermissions.getInstance().clearAll();
551 maybeDisableWebsiteSettings(context);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100552 }
553
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400554 /*package*/ void resetDefaultPreferences(Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800555 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400556 PreferenceManager.getDefaultSharedPreferences(ctx);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800557 p.edit().clear().commit();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400558 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800559 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700560 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700561 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100562 // reset appcache max size
563 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700564 }
565
566 private String getFactoryResetHomeUrl(Context context) {
567 String url = context.getResources().getString(R.string.homepage_base);
568 if (url.indexOf("{CID}") != -1) {
569 url = url.replace("{CID}", Partner.getString(context
Ramanan Rajeswaran9768ac52009-06-03 19:34:58 -0700570 .getContentResolver(), Partner.CLIENT_ID, "android-google"));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700571 }
572 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800573 }
574
The Android Open Source Project0c908882009-03-03 19:32:16 -0800575 // Private constructor that does nothing.
576 private BrowserSettings() {
577 }
578}