blob: 941b28b8ab0e163555238ea30969e1329c7cc7cd [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
Grace Kloba9804c432009-12-02 11:07:40 -080022import android.app.ActivityManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.content.ContentResolver;
24import android.content.Context;
25import android.content.pm.ActivityInfo;
26import android.content.SharedPreferences;
27import android.content.SharedPreferences.Editor;
Nicolas Roard78a98e42009-05-11 13:34:17 +010028import android.preference.PreferenceActivity;
29import android.preference.PreferenceScreen;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010031import android.webkit.GeolocationPermissions;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010032import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.webkit.WebView;
34import android.webkit.WebViewDatabase;
35import android.webkit.WebIconDatabase;
36import android.webkit.WebSettings;
Nicolas Roard78a98e42009-05-11 13:34:17 +010037import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.preference.PreferenceManager;
39import android.provider.Browser;
40
41import java.util.HashMap;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010042import java.util.Map;
43import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044import java.util.Observable;
45
46/*
47 * Package level class for storing various WebView and Browser settings. To use
48 * this class:
49 * BrowserSettings s = BrowserSettings.getInstance();
50 * s.addObserver(webView.getSettings());
51 * s.loadFromDb(context); // Only needed on app startup
52 * s.javaScriptEnabled = true;
53 * ... // set any other settings
54 * s.update(); // this will update all the observers
55 *
56 * To remove an observer:
57 * s.deleteObserver(webView.getSettings());
58 */
59class BrowserSettings extends Observable {
60
Leon Scroggins9ac587d2009-04-20 12:53:46 -040061 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080062 // NOTE: these defaults need to be kept in sync with the XML
63 // until the performance of PreferenceManager.setDefaultValues()
64 // is improved.
65 private boolean loadsImagesAutomatically = true;
66 private boolean javaScriptEnabled = true;
67 private boolean pluginsEnabled = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -080068 private boolean javaScriptCanOpenWindowsAutomatically = false;
69 private boolean showSecurityWarnings = true;
70 private boolean rememberPasswords = true;
71 private boolean saveFormData = true;
72 private boolean openInBackground = false;
73 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070074 private String homeUrl = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -080075 private boolean loginInitialized = false;
76 private boolean autoFitPage = true;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -070077 private boolean landscapeOnly = false;
Grace Kloba5b4b8f12009-08-05 17:19:17 -070078 private boolean loadsPageInOverviewMode = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -080079 private boolean showDebugSettings = false;
Andrei Popescu01068702009-08-03 16:03:24 +010080 // HTML5 API flags
Andrei Popescu5151ac12009-04-17 10:43:07 +010081 private boolean appCacheEnabled = true;
Andrei Popescu01068702009-08-03 16:03:24 +010082 private boolean databaseEnabled = true;
Ben Murdoch734a0662009-06-02 19:11:52 +010083 private boolean domStorageEnabled = true;
Steve Blockf344d032009-07-30 10:50:45 +010084 private boolean geolocationEnabled = true;
Andrei Popescu01068702009-08-03 16:03:24 +010085 private boolean workersEnabled = true; // only affects V8. JSC does not have a similar setting
86 // HTML5 API configuration params
87 private long appCacheMaxSize = Long.MAX_VALUE;
88 private String appCachePath; // default value set in loadFromDb().
89 private String databasePath; // default value set in loadFromDb()
Steve Block5029cf02009-08-21 13:16:58 +010090 private String geolocationDatabasePath; // default value set in loadFromDb()
Andrei Popescu01068702009-08-03 16:03:24 +010091 private WebStorageSizeManager webStorageSizeManager;
92
93 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -080094
Nicolas Roard78a98e42009-05-11 13:34:17 +010095 private final static String TAG = "BrowserSettings";
96
The Android Open Source Project0c908882009-03-03 19:32:16 -080097 // Development settings
98 public WebSettings.LayoutAlgorithm layoutAlgorithm =
99 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
100 private boolean useWideViewPort = true;
101 private int userAgent = 0;
102 private boolean tracing = false;
103 private boolean lightTouch = false;
104 private boolean navDump = false;
Feng Qianb3c02da2009-06-29 15:58:08 -0700105
Ben Murdochbff2d602009-07-01 20:19:05 +0100106 // By default the error console is shown once the user navigates to about:debug.
107 // The setting can be then toggled from the settings menu.
108 private boolean showConsole = true;
109
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110 // Private preconfigured values
111 private static int minimumFontSize = 8;
112 private static int minimumLogicalFontSize = 8;
113 private static int defaultFontSize = 16;
114 private static int defaultFixedFontSize = 13;
115 private static WebSettings.TextSize textSize =
116 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700117 private static WebSettings.ZoomDensity zoomDensity =
118 WebSettings.ZoomDensity.MEDIUM;
Grace Kloba9804c432009-12-02 11:07:40 -0800119 private static int pageCacheCapacity;
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);
Grace Kloba79565032009-11-24 14:23:39 -0800206 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800207
208 // WebView inside Browser doesn't want initial focus to be set.
209 s.setNeedInitialFocus(false);
210 // Browser supports multiple windows
211 s.setSupportMultipleWindows(true);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100212
Andrei Popescu01068702009-08-03 16:03:24 +0100213 // HTML5 API flags
214 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100215 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100216 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100217 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100218 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100219
Andrei Popescu01068702009-08-03 16:03:24 +0100220 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100221 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100222 s.setAppCachePath(b.appCachePath);
223 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100224 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100225
226 // Enable/Disable the error console.
227 b.mTabControl.getBrowserActivity().setShouldShowErrorConsole(
228 b.showDebugSettings && b.showConsole);
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);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100244 // Set the default value for the Application Caches path.
245 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100246 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100247 webStorageSizeManager = new WebStorageSizeManager(
248 ctx,
249 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
250 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100251 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100252 // Set the default value for the Database path.
253 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100254 // Set the default value for the Geolocation database path.
255 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800256
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700257 homeUrl = getFactoryResetHomeUrl(ctx);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700258
Grace Kloba9804c432009-12-02 11:07:40 -0800259 // the cost of one cached page is ~3M (measured using nytimes.com). For
260 // low end devices, we only cache one page. For high end devices, we try
261 // to cache more pages, currently choose 5.
262 ActivityManager am = (ActivityManager) ctx
263 .getSystemService(Context.ACTIVITY_SERVICE);
264 if (am.getMemoryClass() > 16) {
265 pageCacheCapacity = 5;
266 } else {
267 pageCacheCapacity = 1;
268 }
269
The Android Open Source Project0c908882009-03-03 19:32:16 -0800270 // Load the defaults from the xml
271 // This call is TOO SLOW, need to manually keep the defaults
272 // in sync
273 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
274 syncSharedPreferences(p);
275 }
276
277 /* package */ void syncSharedPreferences(SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700278
The Android Open Source Project0c908882009-03-03 19:32:16 -0800279 homeUrl =
280 p.getString(PREF_HOMEPAGE, homeUrl);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700281
The Android Open Source Project0c908882009-03-03 19:32:16 -0800282 loadsImagesAutomatically = p.getBoolean("load_images",
283 loadsImagesAutomatically);
284 javaScriptEnabled = p.getBoolean("enable_javascript",
285 javaScriptEnabled);
286 pluginsEnabled = p.getBoolean("enable_plugins",
287 pluginsEnabled);
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);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700307 loadsPageInOverviewMode = p.getBoolean("load_page",
308 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700309 boolean landscapeOnlyTemp =
310 p.getBoolean("landscape_only", landscapeOnly);
311 if (landscapeOnlyTemp != landscapeOnly) {
312 landscapeOnly = landscapeOnlyTemp;
313 mTabControl.getBrowserActivity().setRequestedOrientation(
314 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
315 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
316 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800317 useWideViewPort = true; // use wide view port for either setting
318 if (autoFitPage) {
319 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
320 } else {
321 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
322 }
323 defaultTextEncodingName =
324 p.getString(PREF_DEFAULT_TEXT_ENCODING,
325 defaultTextEncodingName);
326
327 showDebugSettings =
328 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
329 // Debug menu items have precidence if the menu is visible
330 if (showDebugSettings) {
331 boolean small_screen = p.getBoolean("small_screen",
332 layoutAlgorithm ==
333 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
334 if (small_screen) {
335 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
336 } else {
337 boolean normal_layout = p.getBoolean("normal_layout",
338 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
339 if (normal_layout) {
340 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
341 } else {
342 layoutAlgorithm =
343 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
344 }
345 }
346 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
347 tracing = p.getBoolean("enable_tracing", tracing);
348 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
349 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800350 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800351 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700352 // JS flags is loaded from DB even if showDebugSettings is false,
353 // so that it can be set once and be effective all the time.
354 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100355
356 // Read the setting for showing/hiding the JS Console always so that should the
357 // user enable debug settings, we already know if we should show the console.
358 // The user will never see the console unless they navigate to about:debug,
359 // regardless of the setting we read here. This setting is only used after debug
360 // is enabled.
361 showConsole = p.getBoolean("javascript_console", showConsole);
362 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
363 showDebugSettings && showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700364
Andrei Popescu01068702009-08-03 16:03:24 +0100365 // HTML5 API flags
366 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
367 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
368 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100369 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100370 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100371
Grace Klobad9ee1392009-07-20 11:53:33 -0700372 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800373 }
374
The Android Open Source Project0c908882009-03-03 19:32:16 -0800375 public String getHomePage() {
376 return homeUrl;
377 }
378
Feng Qianb3c02da2009-06-29 15:58:08 -0700379 public String getJsFlags() {
380 return jsFlags;
381 }
382
Andrei Popescu79e82b72009-07-27 12:01:59 +0100383 public WebStorageSizeManager getWebStorageSizeManager() {
384 return webStorageSizeManager;
385 }
386
The Android Open Source Project0c908882009-03-03 19:32:16 -0800387 public void setHomePage(Context context, String url) {
388 Editor ed = PreferenceManager.
389 getDefaultSharedPreferences(context).edit();
390 ed.putString(PREF_HOMEPAGE, url);
391 ed.commit();
392 homeUrl = url;
393 }
394
395 public boolean isLoginInitialized() {
396 return loginInitialized;
397 }
398
399 public void setLoginInitialized(Context context) {
400 loginInitialized = true;
401 Editor ed = PreferenceManager.
402 getDefaultSharedPreferences(context).edit();
403 ed.putBoolean("login_initialized", loginInitialized);
404 ed.commit();
405 }
406
407 public WebSettings.TextSize getTextSize() {
408 return textSize;
409 }
410
Grace Kloba2f830682009-06-25 11:08:53 -0700411 public WebSettings.ZoomDensity getDefaultZoom() {
412 return zoomDensity;
413 }
414
The Android Open Source Project0c908882009-03-03 19:32:16 -0800415 public boolean openInBackground() {
416 return openInBackground;
417 }
418
419 public boolean showSecurityWarnings() {
420 return showSecurityWarnings;
421 }
422
423 public boolean isTracing() {
424 return tracing;
425 }
426
427 public boolean isLightTouch() {
428 return lightTouch;
429 }
430
431 public boolean isNavDump() {
432 return navDump;
433 }
434
The Android Open Source Project0c908882009-03-03 19:32:16 -0800435 public boolean showDebugSettings() {
436 return showDebugSettings;
437 }
438
439 public void toggleDebugSettings() {
440 showDebugSettings = !showDebugSettings;
441 navDump = showDebugSettings;
442 update();
443 }
444
445 /**
446 * Add a WebSettings object to the list of observers that will be updated
447 * when update() is called.
448 *
449 * @param s A WebSettings object that is strictly tied to the life of a
450 * WebView.
451 */
452 public Observer addObserver(WebSettings s) {
453 Observer old = mWebSettingsToObservers.get(s);
454 if (old != null) {
455 super.deleteObserver(old);
456 }
457 Observer o = new Observer(s);
458 mWebSettingsToObservers.put(s, o);
459 super.addObserver(o);
460 return o;
461 }
462
463 /**
464 * Delete the given WebSettings observer from the list of observers.
465 * @param s The WebSettings object to be deleted.
466 */
467 public void deleteObserver(WebSettings s) {
468 Observer o = mWebSettingsToObservers.get(s);
469 if (o != null) {
470 mWebSettingsToObservers.remove(s);
471 super.deleteObserver(o);
472 }
473 }
474
475 /*
476 * Package level method for obtaining a single app instance of the
477 * BrowserSettings.
478 */
479 /*package*/ static BrowserSettings getInstance() {
480 if (sSingleton == null ) {
481 sSingleton = new BrowserSettings();
482 }
483 return sSingleton;
484 }
485
486 /*
487 * Package level method for associating the BrowserSettings with TabControl
488 */
489 /* package */void setTabControl(TabControl tabControl) {
490 mTabControl = tabControl;
491 }
492
493 /*
494 * Update all the observers of the object.
495 */
496 /*package*/ void update() {
497 setChanged();
498 notifyObservers();
499 }
500
501 /*package*/ void clearCache(Context context) {
502 WebIconDatabase.getInstance().removeAllIcons();
503 if (mTabControl != null) {
504 WebView current = mTabControl.getCurrentWebView();
505 if (current != null) {
506 current.clearCache(true);
507 }
508 }
509 }
510
511 /*package*/ void clearCookies(Context context) {
512 CookieManager.getInstance().removeAllCookie();
513 }
514
515 /* package */void clearHistory(Context context) {
516 ContentResolver resolver = context.getContentResolver();
517 Browser.clearHistory(resolver);
518 Browser.clearSearches(resolver);
519 }
520
521 /* package */ void clearFormData(Context context) {
522 WebViewDatabase.getInstance(context).clearFormData();
523 if (mTabControl != null) {
524 mTabControl.getCurrentTopWebView().clearFormData();
525 }
526 }
527
528 /*package*/ void clearPasswords(Context context) {
529 WebViewDatabase db = WebViewDatabase.getInstance(context);
530 db.clearUsernamePassword();
531 db.clearHttpAuthUsernamePassword();
532 }
533
Steve Blockf344d032009-07-30 10:50:45 +0100534 private void maybeDisableWebsiteSettings(Context context) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100535 PreferenceActivity activity = (PreferenceActivity) context;
536 final PreferenceScreen screen = (PreferenceScreen)
537 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
538 screen.setEnabled(false);
539 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
540 public void onReceiveValue(Map webStorageOrigins) {
541 if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) {
542 screen.setEnabled(true);
543 }
544 }
545 });
546
Steve Block2a6a0f42009-11-19 15:55:42 +0000547 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
548 public void onReceiveValue(Set<String> geolocationOrigins) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100549 if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) {
550 screen.setEnabled(true);
551 }
552 }
553 });
Steve Blockf344d032009-07-30 10:50:45 +0100554 }
555
Nicolas Roard78a98e42009-05-11 13:34:17 +0100556 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100557 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100558 maybeDisableWebsiteSettings(context);
559 }
560
561 /*package*/ void clearLocationAccess(Context context) {
562 GeolocationPermissions.getInstance().clearAll();
563 maybeDisableWebsiteSettings(context);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100564 }
565
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400566 /*package*/ void resetDefaultPreferences(Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800567 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400568 PreferenceManager.getDefaultSharedPreferences(ctx);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800569 p.edit().clear().commit();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400570 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800571 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700572 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700573 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100574 // reset appcache max size
575 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700576 }
577
578 private String getFactoryResetHomeUrl(Context context) {
579 String url = context.getResources().getString(R.string.homepage_base);
580 if (url.indexOf("{CID}") != -1) {
581 url = url.replace("{CID}", Partner.getString(context
Ramanan Rajeswaran9768ac52009-06-03 19:34:58 -0700582 .getContentResolver(), Partner.CLIENT_ID, "android-google"));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700583 }
584 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800585 }
586
The Android Open Source Project0c908882009-03-03 19:32:16 -0800587 // Private constructor that does nothing.
588 private BrowserSettings() {
589 }
590}