blob: ad1d1c0572a54d2891e4caaeb0c3a2d7286249f7 [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; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700140 "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
141 "like Gecko) Version/5.0 Safari/533.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800142
143 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700144 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
145 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
146
147 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
148 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
149 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
150
151 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
152 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
153 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800154
155 // Value to truncate strings when adding them to a TextView within
156 // a ListView
157 public final static int MAX_TEXTVIEW_LEN = 80;
158
159 private TabControl mTabControl;
160
161 // Single instance of the BrowserSettings for use in the Browser app.
162 private static BrowserSettings sSingleton;
163
164 // Private map of WebSettings to Observer objects used when deleting an
165 // observer.
166 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
167 new HashMap<WebSettings,Observer>();
168
169 /*
170 * An observer wrapper for updating a WebSettings object with the new
171 * settings after a call to BrowserSettings.update().
172 */
173 static class Observer implements java.util.Observer {
174 // Private WebSettings object that will be updated.
175 private WebSettings mSettings;
176
177 Observer(WebSettings w) {
178 mSettings = w;
179 }
180
181 public void update(Observable o, Object arg) {
182 BrowserSettings b = (BrowserSettings)o;
183 WebSettings s = mSettings;
184
185 s.setLayoutAlgorithm(b.layoutAlgorithm);
186 if (b.userAgent == 0) {
187 // use the default ua string
188 s.setUserAgentString(null);
189 } else if (b.userAgent == 1) {
190 s.setUserAgentString(DESKTOP_USERAGENT);
191 } else if (b.userAgent == 2) {
192 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700193 } else if (b.userAgent == 3) {
194 s.setUserAgentString(IPAD_USERAGENT);
195 } else if (b.userAgent == 4) {
196 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800197 }
198 s.setUseWideViewPort(b.useWideViewPort);
199 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
200 s.setJavaScriptEnabled(b.javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400201 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800202 s.setJavaScriptCanOpenWindowsAutomatically(
203 b.javaScriptCanOpenWindowsAutomatically);
204 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
205 s.setMinimumFontSize(b.minimumFontSize);
206 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
207 s.setDefaultFontSize(b.defaultFontSize);
208 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
209 s.setNavDump(b.navDump);
210 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700211 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800212 s.setLightTouchEnabled(b.lightTouch);
213 s.setSaveFormData(b.saveFormData);
214 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700215 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800216 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800217
218 // WebView inside Browser doesn't want initial focus to be set.
219 s.setNeedInitialFocus(false);
220 // Browser supports multiple windows
221 s.setSupportMultipleWindows(true);
Grace Kloba61fc77c2010-06-22 09:57:33 -0700222 // enable smooth transition for better performance during panning or
223 // zooming
224 s.setEnableSmoothTransition(true);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100225
Andrei Popescu01068702009-08-03 16:03:24 +0100226 // HTML5 API flags
227 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100228 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100229 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100230 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100231 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100232
Andrei Popescu01068702009-08-03 16:03:24 +0100233 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100234 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100235 s.setAppCachePath(b.appCachePath);
236 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100237 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100238
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800239 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800240 }
241 }
242
243 /**
244 * Load settings from the browser app's database.
245 * NOTE: Strings used for the preferences must match those specified
246 * in the browser_preferences.xml
247 * @param ctx A Context object used to query the browser's settings
248 * database. If the database exists, the saved settings will be
249 * stored in this BrowserSettings object. This will update all
250 * observers of this object.
251 */
252 public void loadFromDb(Context ctx) {
253 SharedPreferences p =
254 PreferenceManager.getDefaultSharedPreferences(ctx);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100255 // Set the default value for the Application Caches path.
256 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100257 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100258 webStorageSizeManager = new WebStorageSizeManager(
259 ctx,
260 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
261 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100262 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100263 // Set the default value for the Database path.
264 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100265 // Set the default value for the Geolocation database path.
266 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800267
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700268 if (p.getString(PREF_HOMEPAGE, "") == "") {
269 // No home page preferences is set, set it to default.
270 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
271 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700272
Grace Kloba9804c432009-12-02 11:07:40 -0800273 // the cost of one cached page is ~3M (measured using nytimes.com). For
274 // low end devices, we only cache one page. For high end devices, we try
275 // to cache more pages, currently choose 5.
276 ActivityManager am = (ActivityManager) ctx
277 .getSystemService(Context.ACTIVITY_SERVICE);
278 if (am.getMemoryClass() > 16) {
Andrei Popescuba676ef2010-03-29 12:12:55 +0100279 pageCacheCapacity = 5;
Grace Kloba9804c432009-12-02 11:07:40 -0800280 } else {
281 pageCacheCapacity = 1;
282 }
283
The Android Open Source Project0c908882009-03-03 19:32:16 -0800284 // Load the defaults from the xml
285 // This call is TOO SLOW, need to manually keep the defaults
286 // in sync
287 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
288 syncSharedPreferences(p);
289 }
290
291 /* package */ void syncSharedPreferences(SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700292
The Android Open Source Project0c908882009-03-03 19:32:16 -0800293 homeUrl =
294 p.getString(PREF_HOMEPAGE, homeUrl);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700295
The Android Open Source Project0c908882009-03-03 19:32:16 -0800296 loadsImagesAutomatically = p.getBoolean("load_images",
297 loadsImagesAutomatically);
298 javaScriptEnabled = p.getBoolean("enable_javascript",
299 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400300 pluginState = WebSettings.PluginState.valueOf(
301 p.getString("plugin_state", pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800302 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
303 "block_popup_windows",
304 !javaScriptCanOpenWindowsAutomatically);
305 showSecurityWarnings = p.getBoolean("show_security_warnings",
306 showSecurityWarnings);
307 rememberPasswords = p.getBoolean("remember_passwords",
308 rememberPasswords);
309 saveFormData = p.getBoolean("save_formdata",
310 saveFormData);
311 boolean accept_cookies = p.getBoolean("accept_cookies",
312 CookieManager.getInstance().acceptCookie());
313 CookieManager.getInstance().setAcceptCookie(accept_cookies);
314 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800315 textSize = WebSettings.TextSize.valueOf(
316 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700317 zoomDensity = WebSettings.ZoomDensity.valueOf(
318 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800319 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700320 loadsPageInOverviewMode = p.getBoolean("load_page",
321 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700322 boolean landscapeOnlyTemp =
323 p.getBoolean("landscape_only", landscapeOnly);
324 if (landscapeOnlyTemp != landscapeOnly) {
325 landscapeOnly = landscapeOnlyTemp;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700326 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800327 useWideViewPort = true; // use wide view port for either setting
328 if (autoFitPage) {
329 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
330 } else {
331 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
332 }
333 defaultTextEncodingName =
334 p.getString(PREF_DEFAULT_TEXT_ENCODING,
335 defaultTextEncodingName);
336
337 showDebugSettings =
338 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
339 // Debug menu items have precidence if the menu is visible
340 if (showDebugSettings) {
341 boolean small_screen = p.getBoolean("small_screen",
342 layoutAlgorithm ==
343 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
344 if (small_screen) {
345 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
346 } else {
347 boolean normal_layout = p.getBoolean("normal_layout",
348 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
349 if (normal_layout) {
350 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
351 } else {
352 layoutAlgorithm =
353 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
354 }
355 }
356 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
357 tracing = p.getBoolean("enable_tracing", tracing);
358 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
359 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800360 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800361 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700362 // JS flags is loaded from DB even if showDebugSettings is false,
363 // so that it can be set once and be effective all the time.
364 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100365
366 // Read the setting for showing/hiding the JS Console always so that should the
367 // user enable debug settings, we already know if we should show the console.
368 // The user will never see the console unless they navigate to about:debug,
369 // regardless of the setting we read here. This setting is only used after debug
370 // is enabled.
371 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700372
Andrei Popescu01068702009-08-03 16:03:24 +0100373 // HTML5 API flags
374 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
375 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
376 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100377 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100378 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100379
Grace Klobad9ee1392009-07-20 11:53:33 -0700380 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800381 }
382
The Android Open Source Project0c908882009-03-03 19:32:16 -0800383 public String getHomePage() {
384 return homeUrl;
385 }
386
Feng Qianb3c02da2009-06-29 15:58:08 -0700387 public String getJsFlags() {
388 return jsFlags;
389 }
390
Andrei Popescu79e82b72009-07-27 12:01:59 +0100391 public WebStorageSizeManager getWebStorageSizeManager() {
392 return webStorageSizeManager;
393 }
394
The Android Open Source Project0c908882009-03-03 19:32:16 -0800395 public void setHomePage(Context context, String url) {
396 Editor ed = PreferenceManager.
397 getDefaultSharedPreferences(context).edit();
398 ed.putString(PREF_HOMEPAGE, url);
399 ed.commit();
400 homeUrl = url;
401 }
402
The Android Open Source Project0c908882009-03-03 19:32:16 -0800403 public WebSettings.TextSize getTextSize() {
404 return textSize;
405 }
406
Grace Kloba2f830682009-06-25 11:08:53 -0700407 public WebSettings.ZoomDensity getDefaultZoom() {
408 return zoomDensity;
409 }
410
The Android Open Source Project0c908882009-03-03 19:32:16 -0800411 public boolean openInBackground() {
412 return openInBackground;
413 }
414
415 public boolean showSecurityWarnings() {
416 return showSecurityWarnings;
417 }
418
419 public boolean isTracing() {
420 return tracing;
421 }
422
423 public boolean isLightTouch() {
424 return lightTouch;
425 }
426
427 public boolean isNavDump() {
428 return navDump;
429 }
430
The Android Open Source Project0c908882009-03-03 19:32:16 -0800431 public boolean showDebugSettings() {
432 return showDebugSettings;
433 }
434
435 public void toggleDebugSettings() {
436 showDebugSettings = !showDebugSettings;
437 navDump = showDebugSettings;
438 update();
439 }
440
441 /**
442 * Add a WebSettings object to the list of observers that will be updated
443 * when update() is called.
444 *
445 * @param s A WebSettings object that is strictly tied to the life of a
446 * WebView.
447 */
448 public Observer addObserver(WebSettings s) {
449 Observer old = mWebSettingsToObservers.get(s);
450 if (old != null) {
451 super.deleteObserver(old);
452 }
453 Observer o = new Observer(s);
454 mWebSettingsToObservers.put(s, o);
455 super.addObserver(o);
456 return o;
457 }
458
459 /**
460 * Delete the given WebSettings observer from the list of observers.
461 * @param s The WebSettings object to be deleted.
462 */
463 public void deleteObserver(WebSettings s) {
464 Observer o = mWebSettingsToObservers.get(s);
465 if (o != null) {
466 mWebSettingsToObservers.remove(s);
467 super.deleteObserver(o);
468 }
469 }
470
471 /*
472 * Package level method for obtaining a single app instance of the
473 * BrowserSettings.
474 */
475 /*package*/ static BrowserSettings getInstance() {
476 if (sSingleton == null ) {
477 sSingleton = new BrowserSettings();
478 }
479 return sSingleton;
480 }
481
482 /*
483 * Package level method for associating the BrowserSettings with TabControl
484 */
485 /* package */void setTabControl(TabControl tabControl) {
486 mTabControl = tabControl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800487 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800488 }
489
490 /*
491 * Update all the observers of the object.
492 */
493 /*package*/ void update() {
494 setChanged();
495 notifyObservers();
496 }
497
498 /*package*/ void clearCache(Context context) {
499 WebIconDatabase.getInstance().removeAllIcons();
500 if (mTabControl != null) {
501 WebView current = mTabControl.getCurrentWebView();
502 if (current != null) {
503 current.clearCache(true);
504 }
505 }
506 }
507
508 /*package*/ void clearCookies(Context context) {
509 CookieManager.getInstance().removeAllCookie();
510 }
511
512 /* package */void clearHistory(Context context) {
513 ContentResolver resolver = context.getContentResolver();
514 Browser.clearHistory(resolver);
515 Browser.clearSearches(resolver);
516 }
517
518 /* package */ void clearFormData(Context context) {
519 WebViewDatabase.getInstance(context).clearFormData();
520 if (mTabControl != null) {
Henrik Baard794dc722010-02-19 16:28:06 +0100521 WebView currentTopView = mTabControl.getCurrentTopWebView();
522 if (currentTopView != null) {
523 currentTopView.clearFormData();
524 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800525 }
526 }
527
528 /*package*/ void clearPasswords(Context context) {
529 WebViewDatabase db = WebViewDatabase.getInstance(context);
530 db.clearUsernamePassword();
531 db.clearHttpAuthUsernamePassword();
532 }
533
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800534 private void updateTabControlSettings() {
535 // Enable/disable the error console.
536 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
537 showDebugSettings && showConsole);
538 mTabControl.getBrowserActivity().setRequestedOrientation(
539 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
540 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
541 }
542
Steve Blockf344d032009-07-30 10:50:45 +0100543 private void maybeDisableWebsiteSettings(Context context) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100544 PreferenceActivity activity = (PreferenceActivity) context;
545 final PreferenceScreen screen = (PreferenceScreen)
546 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
547 screen.setEnabled(false);
548 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
549 public void onReceiveValue(Map webStorageOrigins) {
550 if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) {
551 screen.setEnabled(true);
552 }
553 }
554 });
555
Steve Block2a6a0f42009-11-19 15:55:42 +0000556 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
557 public void onReceiveValue(Set<String> geolocationOrigins) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100558 if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) {
559 screen.setEnabled(true);
560 }
561 }
562 });
Steve Blockf344d032009-07-30 10:50:45 +0100563 }
564
Nicolas Roard78a98e42009-05-11 13:34:17 +0100565 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100566 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100567 maybeDisableWebsiteSettings(context);
568 }
569
570 /*package*/ void clearLocationAccess(Context context) {
571 GeolocationPermissions.getInstance().clearAll();
572 maybeDisableWebsiteSettings(context);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100573 }
574
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400575 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800576 reset();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800577 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400578 PreferenceManager.getDefaultSharedPreferences(ctx);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800579 p.edit().clear().commit();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400580 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800581 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700582 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700583 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100584 // reset appcache max size
585 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700586 }
587
588 private String getFactoryResetHomeUrl(Context context) {
589 String url = context.getResources().getString(R.string.homepage_base);
590 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500591 url = url.replace("{CID}",
592 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700593 }
594 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800595 }
596
The Android Open Source Project0c908882009-03-03 19:32:16 -0800597 // Private constructor that does nothing.
598 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800599 reset();
600 }
601
602 private void reset() {
603 // Private variables for settings
604 // NOTE: these defaults need to be kept in sync with the XML
605 // until the performance of PreferenceManager.setDefaultValues()
606 // is improved.
607 loadsImagesAutomatically = true;
608 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400609 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800610 javaScriptCanOpenWindowsAutomatically = false;
611 showSecurityWarnings = true;
612 rememberPasswords = true;
613 saveFormData = true;
614 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800615 autoFitPage = true;
616 landscapeOnly = false;
617 loadsPageInOverviewMode = true;
618 showDebugSettings = false;
619 // HTML5 API flags
620 appCacheEnabled = true;
621 databaseEnabled = true;
622 domStorageEnabled = true;
623 geolocationEnabled = true;
624 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800625 }
626}