blob: 6263eb3c143ef473403129197f5259358bbfe316 [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
Bjorn Bringertd2670652010-09-13 14:06:41 +010020import com.android.browser.search.SearchEngine;
21import com.android.browser.search.SearchEngines;
22
Grace Kloba9804c432009-12-02 11:07:40 -080023import android.app.ActivityManager;
Bjorn Bringertd2670652010-09-13 14:06:41 +010024import android.content.ComponentName;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.content.ContentResolver;
26import android.content.Context;
27import android.content.pm.ActivityInfo;
28import android.content.SharedPreferences;
29import android.content.SharedPreferences.Editor;
Bjorn Bringertd2670652010-09-13 14:06:41 +010030import android.database.ContentObserver;
31import android.os.Handler;
Nicolas Roard78a98e42009-05-11 13:34:17 +010032import android.preference.PreferenceActivity;
33import android.preference.PreferenceScreen;
Bjorn Bringertd2670652010-09-13 14:06:41 +010034import android.provider.Settings;
35import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010037import android.webkit.GeolocationPermissions;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010038import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.webkit.WebView;
40import android.webkit.WebViewDatabase;
41import android.webkit.WebIconDatabase;
42import android.webkit.WebSettings;
Nicolas Roard78a98e42009-05-11 13:34:17 +010043import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044import android.preference.PreferenceManager;
45import android.provider.Browser;
46
47import java.util.HashMap;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010048import java.util.Map;
49import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050import java.util.Observable;
51
52/*
53 * Package level class for storing various WebView and Browser settings. To use
54 * this class:
55 * BrowserSettings s = BrowserSettings.getInstance();
56 * s.addObserver(webView.getSettings());
57 * s.loadFromDb(context); // Only needed on app startup
58 * s.javaScriptEnabled = true;
59 * ... // set any other settings
60 * s.update(); // this will update all the observers
61 *
62 * To remove an observer:
63 * s.deleteObserver(webView.getSettings());
64 */
65class BrowserSettings extends Observable {
66
Leon Scroggins9ac587d2009-04-20 12:53:46 -040067 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080068 // NOTE: these defaults need to be kept in sync with the XML
69 // until the performance of PreferenceManager.setDefaultValues()
70 // is improved.
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080071 // Note: boolean variables are set inside reset function.
72 private boolean loadsImagesAutomatically;
73 private boolean javaScriptEnabled;
Patrick Scotte536b332010-03-22 10:19:32 -040074 private WebSettings.PluginState pluginState;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080075 private boolean javaScriptCanOpenWindowsAutomatically;
76 private boolean showSecurityWarnings;
77 private boolean rememberPasswords;
78 private boolean saveFormData;
79 private boolean openInBackground;
The Android Open Source Project0c908882009-03-03 19:32:16 -080080 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070081 private String homeUrl = "";
Bjorn Bringertd2670652010-09-13 14:06:41 +010082 private SearchEngine searchEngine;
83 private boolean showSearchSuggestions;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080084 private boolean autoFitPage;
85 private boolean landscapeOnly;
86 private boolean loadsPageInOverviewMode;
87 private boolean showDebugSettings;
Andrei Popescu01068702009-08-03 16:03:24 +010088 // HTML5 API flags
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080089 private boolean appCacheEnabled;
90 private boolean databaseEnabled;
91 private boolean domStorageEnabled;
92 private boolean geolocationEnabled;
93 private boolean workersEnabled; // only affects V8. JSC does not have a similar setting
Andrei Popescu01068702009-08-03 16:03:24 +010094 // HTML5 API configuration params
95 private long appCacheMaxSize = Long.MAX_VALUE;
96 private String appCachePath; // default value set in loadFromDb().
97 private String databasePath; // default value set in loadFromDb()
Steve Block5029cf02009-08-21 13:16:58 +010098 private String geolocationDatabasePath; // default value set in loadFromDb()
Andrei Popescu01068702009-08-03 16:03:24 +010099 private WebStorageSizeManager webStorageSizeManager;
100
101 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800102
Nicolas Roard78a98e42009-05-11 13:34:17 +0100103 private final static String TAG = "BrowserSettings";
104
The Android Open Source Project0c908882009-03-03 19:32:16 -0800105 // Development settings
106 public WebSettings.LayoutAlgorithm layoutAlgorithm =
107 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
108 private boolean useWideViewPort = true;
109 private int userAgent = 0;
110 private boolean tracing = false;
111 private boolean lightTouch = false;
112 private boolean navDump = false;
Feng Qianb3c02da2009-06-29 15:58:08 -0700113
Ben Murdochbff2d602009-07-01 20:19:05 +0100114 // By default the error console is shown once the user navigates to about:debug.
115 // The setting can be then toggled from the settings menu.
116 private boolean showConsole = true;
117
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118 // Private preconfigured values
119 private static int minimumFontSize = 8;
120 private static int minimumLogicalFontSize = 8;
121 private static int defaultFontSize = 16;
122 private static int defaultFixedFontSize = 13;
123 private static WebSettings.TextSize textSize =
124 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700125 private static WebSettings.ZoomDensity zoomDensity =
126 WebSettings.ZoomDensity.MEDIUM;
Grace Kloba9804c432009-12-02 11:07:40 -0800127 private static int pageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800128
129 // Preference keys that are used outside this class
130 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
131 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
132 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
133 public final static String PREF_HOMEPAGE = "homepage";
Bjorn Bringertd2670652010-09-13 14:06:41 +0100134 public final static String PREF_SEARCH_ENGINE = "search_engine";
135 public final static String PREF_SHOW_SEARCH_SUGGESTIONS = "show_search_suggestions";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800136 public final static String PREF_CLEAR_FORM_DATA =
137 "privacy_clear_form_data";
138 public final static String PREF_CLEAR_PASSWORDS =
139 "privacy_clear_passwords";
140 public final static String PREF_EXTRAS_RESET_DEFAULTS =
141 "reset_default_preferences";
142 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100143 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800144 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700145 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 public final static String PREF_DEFAULT_TEXT_ENCODING =
147 "default_text_encoding";
Steve Blockc6f577f2009-08-20 18:11:08 +0100148 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
149 "privacy_clear_geolocation_access";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800150
151 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Grace Klobaa4fa6ee2009-06-26 00:34:46 -0700152 "U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, " +
153 "like Gecko) Version/4.0 Safari/530.17";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800154
155 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Grace Klobaa4fa6ee2009-06-26 00:34:46 -0700156 "CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 " +
157 "(KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800158
159 // Value to truncate strings when adding them to a TextView within
160 // a ListView
161 public final static int MAX_TEXTVIEW_LEN = 80;
162
163 private TabControl mTabControl;
164
165 // Single instance of the BrowserSettings for use in the Browser app.
166 private static BrowserSettings sSingleton;
167
168 // Private map of WebSettings to Observer objects used when deleting an
169 // observer.
170 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
171 new HashMap<WebSettings,Observer>();
172
173 /*
174 * An observer wrapper for updating a WebSettings object with the new
175 * settings after a call to BrowserSettings.update().
176 */
177 static class Observer implements java.util.Observer {
178 // Private WebSettings object that will be updated.
179 private WebSettings mSettings;
180
181 Observer(WebSettings w) {
182 mSettings = w;
183 }
184
185 public void update(Observable o, Object arg) {
186 BrowserSettings b = (BrowserSettings)o;
187 WebSettings s = mSettings;
188
189 s.setLayoutAlgorithm(b.layoutAlgorithm);
190 if (b.userAgent == 0) {
191 // use the default ua string
192 s.setUserAgentString(null);
193 } else if (b.userAgent == 1) {
194 s.setUserAgentString(DESKTOP_USERAGENT);
195 } else if (b.userAgent == 2) {
196 s.setUserAgentString(IPHONE_USERAGENT);
197 }
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);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100222
Andrei Popescu01068702009-08-03 16:03:24 +0100223 // HTML5 API flags
224 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100225 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100226 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100227 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100228 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100229
Andrei Popescu01068702009-08-03 16:03:24 +0100230 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100231 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100232 s.setAppCachePath(b.appCachePath);
233 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100234 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100235
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800236 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800237 }
238 }
239
240 /**
241 * Load settings from the browser app's database.
242 * NOTE: Strings used for the preferences must match those specified
243 * in the browser_preferences.xml
244 * @param ctx A Context object used to query the browser's settings
245 * database. If the database exists, the saved settings will be
246 * stored in this BrowserSettings object. This will update all
247 * observers of this object.
248 */
Bjorn Bringertd2670652010-09-13 14:06:41 +0100249 public void loadFromDb(final Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800250 SharedPreferences p =
251 PreferenceManager.getDefaultSharedPreferences(ctx);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100252 // Set the default value for the Application Caches path.
253 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100254 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100255 webStorageSizeManager = new WebStorageSizeManager(
256 ctx,
257 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
258 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100259 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100260 // Set the default value for the Database path.
261 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100262 // Set the default value for the Geolocation database path.
263 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800264
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700265 if (p.getString(PREF_HOMEPAGE, "") == "") {
266 // No home page preferences is set, set it to default.
267 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
268 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700269
Grace Kloba9804c432009-12-02 11:07:40 -0800270 // the cost of one cached page is ~3M (measured using nytimes.com). For
271 // low end devices, we only cache one page. For high end devices, we try
272 // to cache more pages, currently choose 5.
273 ActivityManager am = (ActivityManager) ctx
274 .getSystemService(Context.ACTIVITY_SERVICE);
275 if (am.getMemoryClass() > 16) {
Andrei Popescuba676ef2010-03-29 12:12:55 +0100276 pageCacheCapacity = 5;
Grace Kloba9804c432009-12-02 11:07:40 -0800277 } else {
278 pageCacheCapacity = 1;
279 }
280
Bjorn Bringertd2670652010-09-13 14:06:41 +0100281 final ContentResolver cr = ctx.getContentResolver();
282 cr.registerContentObserver(
283 Settings.System.getUriFor(Settings.System.SHOW_WEB_SUGGESTIONS), false,
284 new ContentObserver(new Handler()) {
285 @Override
286 public void onChange(boolean selfChange) {
287 SharedPreferences p =
288 PreferenceManager.getDefaultSharedPreferences(ctx);
289 updateShowWebSuggestions(cr, p);
290 }
291 });
292 updateShowWebSuggestions(cr, p);
293
294 // Load the defaults from the xml
The Android Open Source Project0c908882009-03-03 19:32:16 -0800295 // This call is TOO SLOW, need to manually keep the defaults
296 // in sync
297 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
Bjorn Bringertd2670652010-09-13 14:06:41 +0100298 syncSharedPreferences(ctx, p);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800299 }
300
Bjorn Bringertd2670652010-09-13 14:06:41 +0100301 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700302
The Android Open Source Project0c908882009-03-03 19:32:16 -0800303 homeUrl =
304 p.getString(PREF_HOMEPAGE, homeUrl);
Bjorn Bringertd2670652010-09-13 14:06:41 +0100305 String searchEngineName = p.getString(PREF_SEARCH_ENGINE, null);
306 if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
307 if (searchEngine != null) {
308 searchEngine.close();
309 }
310 searchEngine = SearchEngines.get(ctx, searchEngineName);
311 }
312 Log.i(TAG, "Selected search engine: " + searchEngine);
313 showSearchSuggestions = p.getBoolean(PREF_SHOW_SEARCH_SUGGESTIONS, true);
314 // Persist to system settings
315 saveShowWebSuggestions(ctx.getContentResolver());
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700316
The Android Open Source Project0c908882009-03-03 19:32:16 -0800317 loadsImagesAutomatically = p.getBoolean("load_images",
318 loadsImagesAutomatically);
319 javaScriptEnabled = p.getBoolean("enable_javascript",
320 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400321 pluginState = WebSettings.PluginState.valueOf(
322 p.getString("plugin_state", pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800323 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
324 "block_popup_windows",
325 !javaScriptCanOpenWindowsAutomatically);
326 showSecurityWarnings = p.getBoolean("show_security_warnings",
327 showSecurityWarnings);
328 rememberPasswords = p.getBoolean("remember_passwords",
329 rememberPasswords);
330 saveFormData = p.getBoolean("save_formdata",
331 saveFormData);
332 boolean accept_cookies = p.getBoolean("accept_cookies",
333 CookieManager.getInstance().acceptCookie());
334 CookieManager.getInstance().setAcceptCookie(accept_cookies);
335 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800336 textSize = WebSettings.TextSize.valueOf(
337 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700338 zoomDensity = WebSettings.ZoomDensity.valueOf(
339 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800340 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700341 loadsPageInOverviewMode = p.getBoolean("load_page",
342 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700343 boolean landscapeOnlyTemp =
344 p.getBoolean("landscape_only", landscapeOnly);
345 if (landscapeOnlyTemp != landscapeOnly) {
346 landscapeOnly = landscapeOnlyTemp;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700347 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800348 useWideViewPort = true; // use wide view port for either setting
349 if (autoFitPage) {
350 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
351 } else {
352 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
353 }
354 defaultTextEncodingName =
355 p.getString(PREF_DEFAULT_TEXT_ENCODING,
356 defaultTextEncodingName);
357
358 showDebugSettings =
359 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
360 // Debug menu items have precidence if the menu is visible
361 if (showDebugSettings) {
362 boolean small_screen = p.getBoolean("small_screen",
363 layoutAlgorithm ==
364 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
365 if (small_screen) {
366 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
367 } else {
368 boolean normal_layout = p.getBoolean("normal_layout",
369 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
370 if (normal_layout) {
371 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
372 } else {
373 layoutAlgorithm =
374 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
375 }
376 }
377 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
378 tracing = p.getBoolean("enable_tracing", tracing);
379 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
380 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800381 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800382 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700383 // JS flags is loaded from DB even if showDebugSettings is false,
384 // so that it can be set once and be effective all the time.
385 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100386
387 // Read the setting for showing/hiding the JS Console always so that should the
388 // user enable debug settings, we already know if we should show the console.
389 // The user will never see the console unless they navigate to about:debug,
390 // regardless of the setting we read here. This setting is only used after debug
391 // is enabled.
392 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700393
Andrei Popescu01068702009-08-03 16:03:24 +0100394 // HTML5 API flags
395 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
396 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
397 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100398 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100399 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100400
Grace Klobad9ee1392009-07-20 11:53:33 -0700401 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800402 }
403
Bjorn Bringertd2670652010-09-13 14:06:41 +0100404 private void saveShowWebSuggestions(ContentResolver cr) {
405 int value = showSearchSuggestions ? 1 : 0;
406 Settings.System.putInt(cr, Settings.System.SHOW_WEB_SUGGESTIONS, value);
407 }
408
409 private void updateShowWebSuggestions(ContentResolver cr, SharedPreferences p) {
410 showSearchSuggestions =
411 Settings.System.getInt(cr,
412 Settings.System.SHOW_WEB_SUGGESTIONS, 1) == 1;
413 p.edit().putBoolean(PREF_SHOW_SEARCH_SUGGESTIONS, showSearchSuggestions).commit();
414 }
415
The Android Open Source Project0c908882009-03-03 19:32:16 -0800416 public String getHomePage() {
417 return homeUrl;
418 }
419
Bjorn Bringertd2670652010-09-13 14:06:41 +0100420 public SearchEngine getSearchEngine() {
421 return searchEngine;
422 }
423
424 public boolean getShowSearchSuggestions() {
425 return showSearchSuggestions;
426 }
427
Feng Qianb3c02da2009-06-29 15:58:08 -0700428 public String getJsFlags() {
429 return jsFlags;
430 }
431
Andrei Popescu79e82b72009-07-27 12:01:59 +0100432 public WebStorageSizeManager getWebStorageSizeManager() {
433 return webStorageSizeManager;
434 }
435
The Android Open Source Project0c908882009-03-03 19:32:16 -0800436 public void setHomePage(Context context, String url) {
437 Editor ed = PreferenceManager.
438 getDefaultSharedPreferences(context).edit();
439 ed.putString(PREF_HOMEPAGE, url);
440 ed.commit();
441 homeUrl = url;
442 }
443
The Android Open Source Project0c908882009-03-03 19:32:16 -0800444 public WebSettings.TextSize getTextSize() {
445 return textSize;
446 }
447
Grace Kloba2f830682009-06-25 11:08:53 -0700448 public WebSettings.ZoomDensity getDefaultZoom() {
449 return zoomDensity;
450 }
451
The Android Open Source Project0c908882009-03-03 19:32:16 -0800452 public boolean openInBackground() {
453 return openInBackground;
454 }
455
456 public boolean showSecurityWarnings() {
457 return showSecurityWarnings;
458 }
459
460 public boolean isTracing() {
461 return tracing;
462 }
463
464 public boolean isLightTouch() {
465 return lightTouch;
466 }
467
468 public boolean isNavDump() {
469 return navDump;
470 }
471
The Android Open Source Project0c908882009-03-03 19:32:16 -0800472 public boolean showDebugSettings() {
473 return showDebugSettings;
474 }
475
476 public void toggleDebugSettings() {
477 showDebugSettings = !showDebugSettings;
478 navDump = showDebugSettings;
479 update();
480 }
481
482 /**
483 * Add a WebSettings object to the list of observers that will be updated
484 * when update() is called.
485 *
486 * @param s A WebSettings object that is strictly tied to the life of a
487 * WebView.
488 */
489 public Observer addObserver(WebSettings s) {
490 Observer old = mWebSettingsToObservers.get(s);
491 if (old != null) {
492 super.deleteObserver(old);
493 }
494 Observer o = new Observer(s);
495 mWebSettingsToObservers.put(s, o);
496 super.addObserver(o);
497 return o;
498 }
499
500 /**
501 * Delete the given WebSettings observer from the list of observers.
502 * @param s The WebSettings object to be deleted.
503 */
504 public void deleteObserver(WebSettings s) {
505 Observer o = mWebSettingsToObservers.get(s);
506 if (o != null) {
507 mWebSettingsToObservers.remove(s);
508 super.deleteObserver(o);
509 }
510 }
511
512 /*
513 * Package level method for obtaining a single app instance of the
514 * BrowserSettings.
515 */
516 /*package*/ static BrowserSettings getInstance() {
517 if (sSingleton == null ) {
518 sSingleton = new BrowserSettings();
519 }
520 return sSingleton;
521 }
522
523 /*
524 * Package level method for associating the BrowserSettings with TabControl
525 */
526 /* package */void setTabControl(TabControl tabControl) {
527 mTabControl = tabControl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800528 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800529 }
530
531 /*
532 * Update all the observers of the object.
533 */
534 /*package*/ void update() {
535 setChanged();
536 notifyObservers();
537 }
538
539 /*package*/ void clearCache(Context context) {
540 WebIconDatabase.getInstance().removeAllIcons();
541 if (mTabControl != null) {
542 WebView current = mTabControl.getCurrentWebView();
543 if (current != null) {
544 current.clearCache(true);
545 }
546 }
547 }
548
549 /*package*/ void clearCookies(Context context) {
550 CookieManager.getInstance().removeAllCookie();
551 }
552
553 /* package */void clearHistory(Context context) {
554 ContentResolver resolver = context.getContentResolver();
555 Browser.clearHistory(resolver);
556 Browser.clearSearches(resolver);
557 }
558
559 /* package */ void clearFormData(Context context) {
560 WebViewDatabase.getInstance(context).clearFormData();
561 if (mTabControl != null) {
562 mTabControl.getCurrentTopWebView().clearFormData();
563 }
564 }
565
566 /*package*/ void clearPasswords(Context context) {
567 WebViewDatabase db = WebViewDatabase.getInstance(context);
568 db.clearUsernamePassword();
569 db.clearHttpAuthUsernamePassword();
570 }
571
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800572 private void updateTabControlSettings() {
573 // Enable/disable the error console.
574 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
575 showDebugSettings && showConsole);
576 mTabControl.getBrowserActivity().setRequestedOrientation(
577 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
578 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
579 }
580
Steve Blockf344d032009-07-30 10:50:45 +0100581 private void maybeDisableWebsiteSettings(Context context) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100582 PreferenceActivity activity = (PreferenceActivity) context;
583 final PreferenceScreen screen = (PreferenceScreen)
584 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
585 screen.setEnabled(false);
586 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
587 public void onReceiveValue(Map webStorageOrigins) {
588 if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) {
589 screen.setEnabled(true);
590 }
591 }
592 });
593
Steve Block2a6a0f42009-11-19 15:55:42 +0000594 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
595 public void onReceiveValue(Set<String> geolocationOrigins) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100596 if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) {
597 screen.setEnabled(true);
598 }
599 }
600 });
Steve Blockf344d032009-07-30 10:50:45 +0100601 }
602
Nicolas Roard78a98e42009-05-11 13:34:17 +0100603 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100604 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100605 maybeDisableWebsiteSettings(context);
606 }
607
608 /*package*/ void clearLocationAccess(Context context) {
609 GeolocationPermissions.getInstance().clearAll();
610 maybeDisableWebsiteSettings(context);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100611 }
612
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400613 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800614 reset();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800615 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400616 PreferenceManager.getDefaultSharedPreferences(ctx);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800617 p.edit().clear().commit();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400618 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800619 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700620 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700621 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100622 // reset appcache max size
623 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700624 }
625
626 private String getFactoryResetHomeUrl(Context context) {
627 String url = context.getResources().getString(R.string.homepage_base);
628 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500629 url = url.replace("{CID}",
630 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700631 }
632 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800633 }
634
The Android Open Source Project0c908882009-03-03 19:32:16 -0800635 // Private constructor that does nothing.
636 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800637 reset();
638 }
639
640 private void reset() {
641 // Private variables for settings
642 // NOTE: these defaults need to be kept in sync with the XML
643 // until the performance of PreferenceManager.setDefaultValues()
644 // is improved.
645 loadsImagesAutomatically = true;
646 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400647 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800648 javaScriptCanOpenWindowsAutomatically = false;
649 showSecurityWarnings = true;
650 rememberPasswords = true;
651 saveFormData = true;
652 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800653 autoFitPage = true;
654 landscapeOnly = false;
655 loadsPageInOverviewMode = true;
656 showDebugSettings = false;
657 // HTML5 API flags
658 appCacheEnabled = true;
659 databaseEnabled = true;
660 domStorageEnabled = true;
661 geolocationEnabled = true;
662 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800663 }
664}