blob: 5de345ff1d6fd18441741f468e8e5502e7c4636f [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
Grace Kloba9804c432009-12-02 11:07:40 -080020import android.app.ActivityManager;
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;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010030import android.webkit.ValueCallback;
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
39import java.util.HashMap;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010040import java.util.Map;
41import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080042import java.util.Observable;
43
44/*
45 * Package level class for storing various WebView and Browser settings. To use
46 * this class:
47 * BrowserSettings s = BrowserSettings.getInstance();
48 * s.addObserver(webView.getSettings());
49 * s.loadFromDb(context); // Only needed on app startup
50 * s.javaScriptEnabled = true;
51 * ... // set any other settings
52 * s.update(); // this will update all the observers
53 *
54 * To remove an observer:
55 * s.deleteObserver(webView.getSettings());
56 */
57class BrowserSettings extends Observable {
58
Leon Scroggins9ac587d2009-04-20 12:53:46 -040059 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080060 // NOTE: these defaults need to be kept in sync with the XML
61 // until the performance of PreferenceManager.setDefaultValues()
62 // is improved.
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080063 // Note: boolean variables are set inside reset function.
64 private boolean loadsImagesAutomatically;
65 private boolean javaScriptEnabled;
Patrick Scotte536b332010-03-22 10:19:32 -040066 private WebSettings.PluginState pluginState;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080067 private boolean javaScriptCanOpenWindowsAutomatically;
68 private boolean showSecurityWarnings;
69 private boolean rememberPasswords;
70 private boolean saveFormData;
71 private boolean openInBackground;
The Android Open Source Project0c908882009-03-03 19:32:16 -080072 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070073 private String homeUrl = "";
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080074 private boolean autoFitPage;
75 private boolean landscapeOnly;
76 private boolean loadsPageInOverviewMode;
77 private boolean showDebugSettings;
Andrei Popescu01068702009-08-03 16:03:24 +010078 // HTML5 API flags
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080079 private boolean appCacheEnabled;
80 private boolean databaseEnabled;
81 private boolean domStorageEnabled;
82 private boolean geolocationEnabled;
83 private boolean workersEnabled; // only affects V8. JSC does not have a similar setting
Andrei Popescu01068702009-08-03 16:03:24 +010084 // 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 // 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;
Grace Kloba9804c432009-12-02 11:07:40 -0800117 private static int pageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118
119 // Preference keys that are used outside this class
120 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
121 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
122 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
123 public final static String PREF_HOMEPAGE = "homepage";
124 public final static String PREF_CLEAR_FORM_DATA =
125 "privacy_clear_form_data";
126 public final static String PREF_CLEAR_PASSWORDS =
127 "privacy_clear_passwords";
128 public final static String PREF_EXTRAS_RESET_DEFAULTS =
129 "reset_default_preferences";
130 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
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 Blockc6f577f2009-08-20 18:11:08 +0100136 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
137 "privacy_clear_geolocation_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);
Patrick Scotte536b332010-03-22 10:19:32 -0400189 s.setPluginState(b.pluginState);
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);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700203 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800204 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800205
206 // WebView inside Browser doesn't want initial focus to be set.
207 s.setNeedInitialFocus(false);
208 // Browser supports multiple windows
209 s.setSupportMultipleWindows(true);
Grace Klobaaf69fdf2010-03-19 15:22:54 -0700210 // Use internal pattern for over scroll background
211 s.setUseWebViewBackgroundForOverscrollBackground(false);
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
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800226 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800227 }
228 }
229
230 /**
231 * Load settings from the browser app's database.
232 * NOTE: Strings used for the preferences must match those specified
233 * in the browser_preferences.xml
234 * @param ctx A Context object used to query the browser's settings
235 * database. If the database exists, the saved settings will be
236 * stored in this BrowserSettings object. This will update all
237 * observers of this object.
238 */
239 public void loadFromDb(Context ctx) {
240 SharedPreferences p =
241 PreferenceManager.getDefaultSharedPreferences(ctx);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100242 // Set the default value for the Application Caches path.
243 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100244 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100245 webStorageSizeManager = new WebStorageSizeManager(
246 ctx,
247 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
248 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100249 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100250 // Set the default value for the Database path.
251 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100252 // Set the default value for the Geolocation database path.
253 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800254
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700255 if (p.getString(PREF_HOMEPAGE, "") == "") {
256 // No home page preferences is set, set it to default.
257 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
258 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700259
Grace Kloba9804c432009-12-02 11:07:40 -0800260 // the cost of one cached page is ~3M (measured using nytimes.com). For
261 // low end devices, we only cache one page. For high end devices, we try
262 // to cache more pages, currently choose 5.
263 ActivityManager am = (ActivityManager) ctx
264 .getSystemService(Context.ACTIVITY_SERVICE);
265 if (am.getMemoryClass() > 16) {
Andrei Popescuba676ef2010-03-29 12:12:55 +0100266 pageCacheCapacity = 5;
Grace Kloba9804c432009-12-02 11:07:40 -0800267 } else {
268 pageCacheCapacity = 1;
269 }
270
The Android Open Source Project0c908882009-03-03 19:32:16 -0800271 // Load the defaults from the xml
272 // This call is TOO SLOW, need to manually keep the defaults
273 // in sync
274 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
275 syncSharedPreferences(p);
276 }
277
278 /* package */ void syncSharedPreferences(SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700279
The Android Open Source Project0c908882009-03-03 19:32:16 -0800280 homeUrl =
281 p.getString(PREF_HOMEPAGE, homeUrl);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700282
The Android Open Source Project0c908882009-03-03 19:32:16 -0800283 loadsImagesAutomatically = p.getBoolean("load_images",
284 loadsImagesAutomatically);
285 javaScriptEnabled = p.getBoolean("enable_javascript",
286 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400287 pluginState = WebSettings.PluginState.valueOf(
288 p.getString("plugin_state", pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800289 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
290 "block_popup_windows",
291 !javaScriptCanOpenWindowsAutomatically);
292 showSecurityWarnings = p.getBoolean("show_security_warnings",
293 showSecurityWarnings);
294 rememberPasswords = p.getBoolean("remember_passwords",
295 rememberPasswords);
296 saveFormData = p.getBoolean("save_formdata",
297 saveFormData);
298 boolean accept_cookies = p.getBoolean("accept_cookies",
299 CookieManager.getInstance().acceptCookie());
300 CookieManager.getInstance().setAcceptCookie(accept_cookies);
301 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800302 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;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700313 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800314 useWideViewPort = true; // use wide view port for either setting
315 if (autoFitPage) {
316 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
317 } else {
318 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
319 }
320 defaultTextEncodingName =
321 p.getString(PREF_DEFAULT_TEXT_ENCODING,
322 defaultTextEncodingName);
323
324 showDebugSettings =
325 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
326 // Debug menu items have precidence if the menu is visible
327 if (showDebugSettings) {
328 boolean small_screen = p.getBoolean("small_screen",
329 layoutAlgorithm ==
330 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
331 if (small_screen) {
332 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
333 } else {
334 boolean normal_layout = p.getBoolean("normal_layout",
335 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
336 if (normal_layout) {
337 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
338 } else {
339 layoutAlgorithm =
340 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
341 }
342 }
343 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
344 tracing = p.getBoolean("enable_tracing", tracing);
345 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
346 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800347 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800348 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700349 // JS flags is loaded from DB even if showDebugSettings is false,
350 // so that it can be set once and be effective all the time.
351 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100352
353 // Read the setting for showing/hiding the JS Console always so that should the
354 // user enable debug settings, we already know if we should show the console.
355 // The user will never see the console unless they navigate to about:debug,
356 // regardless of the setting we read here. This setting is only used after debug
357 // is enabled.
358 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700359
Andrei Popescu01068702009-08-03 16:03:24 +0100360 // HTML5 API flags
361 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
362 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
363 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100364 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100365 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100366
Grace Klobad9ee1392009-07-20 11:53:33 -0700367 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800368 }
369
The Android Open Source Project0c908882009-03-03 19:32:16 -0800370 public String getHomePage() {
371 return homeUrl;
372 }
373
Feng Qianb3c02da2009-06-29 15:58:08 -0700374 public String getJsFlags() {
375 return jsFlags;
376 }
377
Andrei Popescu79e82b72009-07-27 12:01:59 +0100378 public WebStorageSizeManager getWebStorageSizeManager() {
379 return webStorageSizeManager;
380 }
381
The Android Open Source Project0c908882009-03-03 19:32:16 -0800382 public void setHomePage(Context context, String url) {
383 Editor ed = PreferenceManager.
384 getDefaultSharedPreferences(context).edit();
385 ed.putString(PREF_HOMEPAGE, url);
386 ed.commit();
387 homeUrl = url;
388 }
389
The Android Open Source Project0c908882009-03-03 19:32:16 -0800390 public WebSettings.TextSize getTextSize() {
391 return textSize;
392 }
393
Grace Kloba2f830682009-06-25 11:08:53 -0700394 public WebSettings.ZoomDensity getDefaultZoom() {
395 return zoomDensity;
396 }
397
The Android Open Source Project0c908882009-03-03 19:32:16 -0800398 public boolean openInBackground() {
399 return openInBackground;
400 }
401
402 public boolean showSecurityWarnings() {
403 return showSecurityWarnings;
404 }
405
406 public boolean isTracing() {
407 return tracing;
408 }
409
410 public boolean isLightTouch() {
411 return lightTouch;
412 }
413
414 public boolean isNavDump() {
415 return navDump;
416 }
417
The Android Open Source Project0c908882009-03-03 19:32:16 -0800418 public boolean showDebugSettings() {
419 return showDebugSettings;
420 }
421
422 public void toggleDebugSettings() {
423 showDebugSettings = !showDebugSettings;
424 navDump = showDebugSettings;
425 update();
426 }
427
428 /**
429 * Add a WebSettings object to the list of observers that will be updated
430 * when update() is called.
431 *
432 * @param s A WebSettings object that is strictly tied to the life of a
433 * WebView.
434 */
435 public Observer addObserver(WebSettings s) {
436 Observer old = mWebSettingsToObservers.get(s);
437 if (old != null) {
438 super.deleteObserver(old);
439 }
440 Observer o = new Observer(s);
441 mWebSettingsToObservers.put(s, o);
442 super.addObserver(o);
443 return o;
444 }
445
446 /**
447 * Delete the given WebSettings observer from the list of observers.
448 * @param s The WebSettings object to be deleted.
449 */
450 public void deleteObserver(WebSettings s) {
451 Observer o = mWebSettingsToObservers.get(s);
452 if (o != null) {
453 mWebSettingsToObservers.remove(s);
454 super.deleteObserver(o);
455 }
456 }
457
458 /*
459 * Package level method for obtaining a single app instance of the
460 * BrowserSettings.
461 */
462 /*package*/ static BrowserSettings getInstance() {
463 if (sSingleton == null ) {
464 sSingleton = new BrowserSettings();
465 }
466 return sSingleton;
467 }
468
469 /*
470 * Package level method for associating the BrowserSettings with TabControl
471 */
472 /* package */void setTabControl(TabControl tabControl) {
473 mTabControl = tabControl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800474 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800475 }
476
477 /*
478 * Update all the observers of the object.
479 */
480 /*package*/ void update() {
481 setChanged();
482 notifyObservers();
483 }
484
485 /*package*/ void clearCache(Context context) {
486 WebIconDatabase.getInstance().removeAllIcons();
487 if (mTabControl != null) {
488 WebView current = mTabControl.getCurrentWebView();
489 if (current != null) {
490 current.clearCache(true);
491 }
492 }
493 }
494
495 /*package*/ void clearCookies(Context context) {
496 CookieManager.getInstance().removeAllCookie();
497 }
498
499 /* package */void clearHistory(Context context) {
500 ContentResolver resolver = context.getContentResolver();
501 Browser.clearHistory(resolver);
502 Browser.clearSearches(resolver);
503 }
504
505 /* package */ void clearFormData(Context context) {
506 WebViewDatabase.getInstance(context).clearFormData();
507 if (mTabControl != null) {
508 mTabControl.getCurrentTopWebView().clearFormData();
509 }
510 }
511
512 /*package*/ void clearPasswords(Context context) {
513 WebViewDatabase db = WebViewDatabase.getInstance(context);
514 db.clearUsernamePassword();
515 db.clearHttpAuthUsernamePassword();
516 }
517
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800518 private void updateTabControlSettings() {
519 // Enable/disable the error console.
520 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
521 showDebugSettings && showConsole);
522 mTabControl.getBrowserActivity().setRequestedOrientation(
523 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
524 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
525 }
526
Steve Blockf344d032009-07-30 10:50:45 +0100527 private void maybeDisableWebsiteSettings(Context context) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100528 PreferenceActivity activity = (PreferenceActivity) context;
529 final PreferenceScreen screen = (PreferenceScreen)
530 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
531 screen.setEnabled(false);
532 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
533 public void onReceiveValue(Map webStorageOrigins) {
534 if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) {
535 screen.setEnabled(true);
536 }
537 }
538 });
539
Steve Block2a6a0f42009-11-19 15:55:42 +0000540 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
541 public void onReceiveValue(Set<String> geolocationOrigins) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100542 if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) {
543 screen.setEnabled(true);
544 }
545 }
546 });
Steve Blockf344d032009-07-30 10:50:45 +0100547 }
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) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800560 reset();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800561 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400562 PreferenceManager.getDefaultSharedPreferences(ctx);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800563 p.edit().clear().commit();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400564 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800565 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700566 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700567 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100568 // reset appcache max size
569 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700570 }
571
572 private String getFactoryResetHomeUrl(Context context) {
573 String url = context.getResources().getString(R.string.homepage_base);
574 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500575 url = url.replace("{CID}",
576 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700577 }
578 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800579 }
580
The Android Open Source Project0c908882009-03-03 19:32:16 -0800581 // Private constructor that does nothing.
582 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800583 reset();
584 }
585
586 private void reset() {
587 // Private variables for settings
588 // NOTE: these defaults need to be kept in sync with the XML
589 // until the performance of PreferenceManager.setDefaultValues()
590 // is improved.
591 loadsImagesAutomatically = true;
592 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400593 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800594 javaScriptCanOpenWindowsAutomatically = false;
595 showSecurityWarnings = true;
596 rememberPasswords = true;
597 saveFormData = true;
598 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800599 autoFitPage = true;
600 landscapeOnly = false;
601 loadsPageInOverviewMode = true;
602 showDebugSettings = false;
603 // HTML5 API flags
604 appCacheEnabled = true;
605 databaseEnabled = true;
606 domStorageEnabled = true;
607 geolocationEnabled = true;
608 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800609 }
610}