blob: 7d5bbf67b540daaf7334b62ed7a98f5684c2ae20 [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 Bringertd69f51d2010-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 Bringertd69f51d2010-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 Bringertd69f51d2010-09-13 14:06:41 +010030import android.database.ContentObserver;
Jeff Davidson43610292010-07-16 16:03:58 -070031import android.net.Uri;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010032import android.os.Handler;
Nicolas Roard78a98e42009-05-11 13:34:17 +010033import android.preference.PreferenceActivity;
34import android.preference.PreferenceScreen;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010035import android.provider.Settings;
36import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010038import android.webkit.GeolocationPermissions;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010039import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080040import android.webkit.WebView;
41import android.webkit.WebViewDatabase;
42import android.webkit.WebIconDatabase;
43import android.webkit.WebSettings;
Nicolas Roard78a98e42009-05-11 13:34:17 +010044import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.preference.PreferenceManager;
46import android.provider.Browser;
47
48import java.util.HashMap;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010049import java.util.Map;
50import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051import java.util.Observable;
52
53/*
54 * Package level class for storing various WebView and Browser settings. To use
55 * this class:
56 * BrowserSettings s = BrowserSettings.getInstance();
57 * s.addObserver(webView.getSettings());
58 * s.loadFromDb(context); // Only needed on app startup
59 * s.javaScriptEnabled = true;
60 * ... // set any other settings
61 * s.update(); // this will update all the observers
62 *
63 * To remove an observer:
64 * s.deleteObserver(webView.getSettings());
65 */
66class BrowserSettings extends Observable {
67
Leon Scroggins9ac587d2009-04-20 12:53:46 -040068 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080069 // NOTE: these defaults need to be kept in sync with the XML
70 // until the performance of PreferenceManager.setDefaultValues()
71 // is improved.
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080072 // Note: boolean variables are set inside reset function.
73 private boolean loadsImagesAutomatically;
74 private boolean javaScriptEnabled;
Patrick Scotte536b332010-03-22 10:19:32 -040075 private WebSettings.PluginState pluginState;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080076 private boolean javaScriptCanOpenWindowsAutomatically;
77 private boolean showSecurityWarnings;
78 private boolean rememberPasswords;
79 private boolean saveFormData;
80 private boolean openInBackground;
The Android Open Source Project0c908882009-03-03 19:32:16 -080081 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070082 private String homeUrl = "";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010083 private SearchEngine searchEngine;
84 private boolean showSearchSuggestions;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080085 private boolean autoFitPage;
86 private boolean landscapeOnly;
87 private boolean loadsPageInOverviewMode;
88 private boolean showDebugSettings;
Andrei Popescu01068702009-08-03 16:03:24 +010089 // HTML5 API flags
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080090 private boolean appCacheEnabled;
91 private boolean databaseEnabled;
92 private boolean domStorageEnabled;
93 private boolean geolocationEnabled;
94 private boolean workersEnabled; // only affects V8. JSC does not have a similar setting
Andrei Popescu01068702009-08-03 16:03:24 +010095 // HTML5 API configuration params
96 private long appCacheMaxSize = Long.MAX_VALUE;
97 private String appCachePath; // default value set in loadFromDb().
98 private String databasePath; // default value set in loadFromDb()
Steve Block5029cf02009-08-21 13:16:58 +010099 private String geolocationDatabasePath; // default value set in loadFromDb()
Andrei Popescu01068702009-08-03 16:03:24 +0100100 private WebStorageSizeManager webStorageSizeManager;
101
102 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800103
Nicolas Roard78a98e42009-05-11 13:34:17 +0100104 private final static String TAG = "BrowserSettings";
105
The Android Open Source Project0c908882009-03-03 19:32:16 -0800106 // Development settings
107 public WebSettings.LayoutAlgorithm layoutAlgorithm =
108 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
109 private boolean useWideViewPort = true;
110 private int userAgent = 0;
111 private boolean tracing = false;
112 private boolean lightTouch = false;
113 private boolean navDump = false;
Feng Qianb3c02da2009-06-29 15:58:08 -0700114
Ben Murdochbff2d602009-07-01 20:19:05 +0100115 // By default the error console is shown once the user navigates to about:debug.
116 // The setting can be then toggled from the settings menu.
117 private boolean showConsole = true;
118
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119 // Private preconfigured values
Shimeng (Simon) Wangb4fb25c2010-07-13 15:18:10 -0700120 private static int minimumFontSize = 1;
121 private static int minimumLogicalFontSize = 1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800122 private static int defaultFontSize = 16;
123 private static int defaultFixedFontSize = 13;
124 private static WebSettings.TextSize textSize =
125 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700126 private static WebSettings.ZoomDensity zoomDensity =
127 WebSettings.ZoomDensity.MEDIUM;
Grace Kloba9804c432009-12-02 11:07:40 -0800128 private static int pageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800129
130 // Preference keys that are used outside this class
131 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
132 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
133 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
134 public final static String PREF_HOMEPAGE = "homepage";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100135 public final static String PREF_SEARCH_ENGINE = "search_engine";
136 public final static String PREF_SHOW_SEARCH_SUGGESTIONS = "show_search_suggestions";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800137 public final static String PREF_CLEAR_FORM_DATA =
138 "privacy_clear_form_data";
139 public final static String PREF_CLEAR_PASSWORDS =
140 "privacy_clear_passwords";
141 public final static String PREF_EXTRAS_RESET_DEFAULTS =
142 "reset_default_preferences";
143 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100144 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800145 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700146 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800147 public final static String PREF_DEFAULT_TEXT_ENCODING =
148 "default_text_encoding";
Steve Blockc6f577f2009-08-20 18:11:08 +0100149 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
150 "privacy_clear_geolocation_access";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800151
152 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700153 "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
154 "like Gecko) Version/5.0 Safari/533.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800155
156 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700157 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
158 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
159
160 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
161 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
162 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
163
164 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
165 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
166 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800167
168 // Value to truncate strings when adding them to a TextView within
169 // a ListView
170 public final static int MAX_TEXTVIEW_LEN = 80;
171
Jeff Davidson43610292010-07-16 16:03:58 -0700172 public static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider";
173
174 public static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/");
175
The Android Open Source Project0c908882009-03-03 19:32:16 -0800176 private TabControl mTabControl;
177
178 // Single instance of the BrowserSettings for use in the Browser app.
179 private static BrowserSettings sSingleton;
180
181 // Private map of WebSettings to Observer objects used when deleting an
182 // observer.
183 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
184 new HashMap<WebSettings,Observer>();
185
186 /*
187 * An observer wrapper for updating a WebSettings object with the new
188 * settings after a call to BrowserSettings.update().
189 */
190 static class Observer implements java.util.Observer {
191 // Private WebSettings object that will be updated.
192 private WebSettings mSettings;
193
194 Observer(WebSettings w) {
195 mSettings = w;
196 }
197
198 public void update(Observable o, Object arg) {
199 BrowserSettings b = (BrowserSettings)o;
200 WebSettings s = mSettings;
201
202 s.setLayoutAlgorithm(b.layoutAlgorithm);
203 if (b.userAgent == 0) {
204 // use the default ua string
205 s.setUserAgentString(null);
206 } else if (b.userAgent == 1) {
207 s.setUserAgentString(DESKTOP_USERAGENT);
208 } else if (b.userAgent == 2) {
209 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700210 } else if (b.userAgent == 3) {
211 s.setUserAgentString(IPAD_USERAGENT);
212 } else if (b.userAgent == 4) {
213 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800214 }
215 s.setUseWideViewPort(b.useWideViewPort);
216 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
217 s.setJavaScriptEnabled(b.javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400218 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800219 s.setJavaScriptCanOpenWindowsAutomatically(
220 b.javaScriptCanOpenWindowsAutomatically);
221 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
222 s.setMinimumFontSize(b.minimumFontSize);
223 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
224 s.setDefaultFontSize(b.defaultFontSize);
225 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
226 s.setNavDump(b.navDump);
227 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700228 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800229 s.setLightTouchEnabled(b.lightTouch);
230 s.setSaveFormData(b.saveFormData);
231 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700232 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800233 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800234
235 // WebView inside Browser doesn't want initial focus to be set.
236 s.setNeedInitialFocus(false);
237 // Browser supports multiple windows
238 s.setSupportMultipleWindows(true);
Grace Kloba61fc77c2010-06-22 09:57:33 -0700239 // enable smooth transition for better performance during panning or
240 // zooming
241 s.setEnableSmoothTransition(true);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100242
Andrei Popescu01068702009-08-03 16:03:24 +0100243 // HTML5 API flags
244 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100245 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100246 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100247 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100248 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100249
Andrei Popescu01068702009-08-03 16:03:24 +0100250 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100251 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100252 s.setAppCachePath(b.appCachePath);
253 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100254 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100255
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800256 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800257 }
258 }
259
260 /**
261 * Load settings from the browser app's database.
262 * NOTE: Strings used for the preferences must match those specified
263 * in the browser_preferences.xml
264 * @param ctx A Context object used to query the browser's settings
265 * database. If the database exists, the saved settings will be
266 * stored in this BrowserSettings object. This will update all
267 * observers of this object.
268 */
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100269 public void loadFromDb(final Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800270 SharedPreferences p =
271 PreferenceManager.getDefaultSharedPreferences(ctx);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100272 // Set the default value for the Application Caches path.
273 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100274 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100275 webStorageSizeManager = new WebStorageSizeManager(
276 ctx,
277 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
278 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100279 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100280 // Set the default value for the Database path.
281 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100282 // Set the default value for the Geolocation database path.
283 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800284
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700285 if (p.getString(PREF_HOMEPAGE, "") == "") {
286 // No home page preferences is set, set it to default.
287 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
288 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700289
Grace Kloba9804c432009-12-02 11:07:40 -0800290 // the cost of one cached page is ~3M (measured using nytimes.com). For
291 // low end devices, we only cache one page. For high end devices, we try
292 // to cache more pages, currently choose 5.
293 ActivityManager am = (ActivityManager) ctx
294 .getSystemService(Context.ACTIVITY_SERVICE);
295 if (am.getMemoryClass() > 16) {
Andrei Popescuba676ef2010-03-29 12:12:55 +0100296 pageCacheCapacity = 5;
Grace Kloba9804c432009-12-02 11:07:40 -0800297 } else {
298 pageCacheCapacity = 1;
299 }
300
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100301 final ContentResolver cr = ctx.getContentResolver();
302 cr.registerContentObserver(
303 Settings.System.getUriFor(Settings.System.SHOW_WEB_SUGGESTIONS), false,
304 new ContentObserver(new Handler()) {
305 @Override
306 public void onChange(boolean selfChange) {
307 SharedPreferences p =
308 PreferenceManager.getDefaultSharedPreferences(ctx);
309 updateShowWebSuggestions(cr, p);
310 }
311 });
312 updateShowWebSuggestions(cr, p);
313
314 // Load the defaults from the xml
The Android Open Source Project0c908882009-03-03 19:32:16 -0800315 // This call is TOO SLOW, need to manually keep the defaults
316 // in sync
317 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100318 syncSharedPreferences(ctx, p);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800319 }
320
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100321 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700322
The Android Open Source Project0c908882009-03-03 19:32:16 -0800323 homeUrl =
324 p.getString(PREF_HOMEPAGE, homeUrl);
Leon Scroggins III31306602010-09-17 11:10:29 -0400325 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
326 SearchEngine.GOOGLE);
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100327 if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
328 if (searchEngine != null) {
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400329 if (searchEngine.supportsVoiceSearch()) {
330 // One or more tabs could have been in voice search mode.
331 // Clear it, since the new SearchEngine may not support
332 // it, or may handle it differently.
333 for (int i = 0; i < mTabControl.getTabCount(); i++) {
334 mTabControl.getTab(i).revertVoiceSearchMode();
335 }
336 }
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100337 searchEngine.close();
338 }
339 searchEngine = SearchEngines.get(ctx, searchEngineName);
340 }
341 Log.i(TAG, "Selected search engine: " + searchEngine);
342 showSearchSuggestions = p.getBoolean(PREF_SHOW_SEARCH_SUGGESTIONS, true);
343 // Persist to system settings
344 saveShowWebSuggestions(ctx.getContentResolver());
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700345
The Android Open Source Project0c908882009-03-03 19:32:16 -0800346 loadsImagesAutomatically = p.getBoolean("load_images",
347 loadsImagesAutomatically);
348 javaScriptEnabled = p.getBoolean("enable_javascript",
349 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400350 pluginState = WebSettings.PluginState.valueOf(
351 p.getString("plugin_state", pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800352 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
353 "block_popup_windows",
354 !javaScriptCanOpenWindowsAutomatically);
355 showSecurityWarnings = p.getBoolean("show_security_warnings",
356 showSecurityWarnings);
357 rememberPasswords = p.getBoolean("remember_passwords",
358 rememberPasswords);
359 saveFormData = p.getBoolean("save_formdata",
360 saveFormData);
361 boolean accept_cookies = p.getBoolean("accept_cookies",
362 CookieManager.getInstance().acceptCookie());
363 CookieManager.getInstance().setAcceptCookie(accept_cookies);
364 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800365 textSize = WebSettings.TextSize.valueOf(
366 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700367 zoomDensity = WebSettings.ZoomDensity.valueOf(
368 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800369 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700370 loadsPageInOverviewMode = p.getBoolean("load_page",
371 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700372 boolean landscapeOnlyTemp =
373 p.getBoolean("landscape_only", landscapeOnly);
374 if (landscapeOnlyTemp != landscapeOnly) {
375 landscapeOnly = landscapeOnlyTemp;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700376 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800377 useWideViewPort = true; // use wide view port for either setting
378 if (autoFitPage) {
379 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
380 } else {
381 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
382 }
383 defaultTextEncodingName =
384 p.getString(PREF_DEFAULT_TEXT_ENCODING,
385 defaultTextEncodingName);
386
387 showDebugSettings =
388 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
389 // Debug menu items have precidence if the menu is visible
390 if (showDebugSettings) {
391 boolean small_screen = p.getBoolean("small_screen",
392 layoutAlgorithm ==
393 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
394 if (small_screen) {
395 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
396 } else {
397 boolean normal_layout = p.getBoolean("normal_layout",
398 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
399 if (normal_layout) {
400 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
401 } else {
402 layoutAlgorithm =
403 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
404 }
405 }
406 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
407 tracing = p.getBoolean("enable_tracing", tracing);
408 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
409 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800410 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800411 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700412 // JS flags is loaded from DB even if showDebugSettings is false,
413 // so that it can be set once and be effective all the time.
414 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100415
416 // Read the setting for showing/hiding the JS Console always so that should the
417 // user enable debug settings, we already know if we should show the console.
418 // The user will never see the console unless they navigate to about:debug,
419 // regardless of the setting we read here. This setting is only used after debug
420 // is enabled.
421 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700422
Andrei Popescu01068702009-08-03 16:03:24 +0100423 // HTML5 API flags
424 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
425 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
426 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100427 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100428 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100429
Grace Klobad9ee1392009-07-20 11:53:33 -0700430 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800431 }
432
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100433 private void saveShowWebSuggestions(ContentResolver cr) {
434 int value = showSearchSuggestions ? 1 : 0;
435 Settings.System.putInt(cr, Settings.System.SHOW_WEB_SUGGESTIONS, value);
436 }
437
438 private void updateShowWebSuggestions(ContentResolver cr, SharedPreferences p) {
439 showSearchSuggestions =
440 Settings.System.getInt(cr,
441 Settings.System.SHOW_WEB_SUGGESTIONS, 1) == 1;
442 p.edit().putBoolean(PREF_SHOW_SEARCH_SUGGESTIONS, showSearchSuggestions).commit();
443 }
444
The Android Open Source Project0c908882009-03-03 19:32:16 -0800445 public String getHomePage() {
446 return homeUrl;
447 }
448
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100449 public SearchEngine getSearchEngine() {
450 return searchEngine;
451 }
452
453 public boolean getShowSearchSuggestions() {
454 return showSearchSuggestions;
455 }
456
Feng Qianb3c02da2009-06-29 15:58:08 -0700457 public String getJsFlags() {
458 return jsFlags;
459 }
460
Andrei Popescu79e82b72009-07-27 12:01:59 +0100461 public WebStorageSizeManager getWebStorageSizeManager() {
462 return webStorageSizeManager;
463 }
464
The Android Open Source Project0c908882009-03-03 19:32:16 -0800465 public void setHomePage(Context context, String url) {
466 Editor ed = PreferenceManager.
467 getDefaultSharedPreferences(context).edit();
468 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700469 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800470 homeUrl = url;
471 }
472
The Android Open Source Project0c908882009-03-03 19:32:16 -0800473 public WebSettings.TextSize getTextSize() {
474 return textSize;
475 }
476
Grace Kloba2f830682009-06-25 11:08:53 -0700477 public WebSettings.ZoomDensity getDefaultZoom() {
478 return zoomDensity;
479 }
480
The Android Open Source Project0c908882009-03-03 19:32:16 -0800481 public boolean openInBackground() {
482 return openInBackground;
483 }
484
485 public boolean showSecurityWarnings() {
486 return showSecurityWarnings;
487 }
488
489 public boolean isTracing() {
490 return tracing;
491 }
492
493 public boolean isLightTouch() {
494 return lightTouch;
495 }
496
497 public boolean isNavDump() {
498 return navDump;
499 }
500
The Android Open Source Project0c908882009-03-03 19:32:16 -0800501 public boolean showDebugSettings() {
502 return showDebugSettings;
503 }
504
505 public void toggleDebugSettings() {
506 showDebugSettings = !showDebugSettings;
507 navDump = showDebugSettings;
508 update();
509 }
510
511 /**
512 * Add a WebSettings object to the list of observers that will be updated
513 * when update() is called.
514 *
515 * @param s A WebSettings object that is strictly tied to the life of a
516 * WebView.
517 */
518 public Observer addObserver(WebSettings s) {
519 Observer old = mWebSettingsToObservers.get(s);
520 if (old != null) {
521 super.deleteObserver(old);
522 }
523 Observer o = new Observer(s);
524 mWebSettingsToObservers.put(s, o);
525 super.addObserver(o);
526 return o;
527 }
528
529 /**
530 * Delete the given WebSettings observer from the list of observers.
531 * @param s The WebSettings object to be deleted.
532 */
533 public void deleteObserver(WebSettings s) {
534 Observer o = mWebSettingsToObservers.get(s);
535 if (o != null) {
536 mWebSettingsToObservers.remove(s);
537 super.deleteObserver(o);
538 }
539 }
540
541 /*
542 * Package level method for obtaining a single app instance of the
543 * BrowserSettings.
544 */
545 /*package*/ static BrowserSettings getInstance() {
546 if (sSingleton == null ) {
547 sSingleton = new BrowserSettings();
548 }
549 return sSingleton;
550 }
551
552 /*
553 * Package level method for associating the BrowserSettings with TabControl
554 */
555 /* package */void setTabControl(TabControl tabControl) {
556 mTabControl = tabControl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800557 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800558 }
559
560 /*
561 * Update all the observers of the object.
562 */
563 /*package*/ void update() {
564 setChanged();
565 notifyObservers();
566 }
567
568 /*package*/ void clearCache(Context context) {
569 WebIconDatabase.getInstance().removeAllIcons();
570 if (mTabControl != null) {
571 WebView current = mTabControl.getCurrentWebView();
572 if (current != null) {
573 current.clearCache(true);
574 }
575 }
576 }
577
578 /*package*/ void clearCookies(Context context) {
579 CookieManager.getInstance().removeAllCookie();
580 }
581
582 /* package */void clearHistory(Context context) {
583 ContentResolver resolver = context.getContentResolver();
584 Browser.clearHistory(resolver);
585 Browser.clearSearches(resolver);
586 }
587
588 /* package */ void clearFormData(Context context) {
589 WebViewDatabase.getInstance(context).clearFormData();
590 if (mTabControl != null) {
Henrik Baard794dc722010-02-19 16:28:06 +0100591 WebView currentTopView = mTabControl.getCurrentTopWebView();
592 if (currentTopView != null) {
593 currentTopView.clearFormData();
594 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800595 }
596 }
597
598 /*package*/ void clearPasswords(Context context) {
599 WebViewDatabase db = WebViewDatabase.getInstance(context);
600 db.clearUsernamePassword();
601 db.clearHttpAuthUsernamePassword();
602 }
603
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800604 private void updateTabControlSettings() {
605 // Enable/disable the error console.
606 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
607 showDebugSettings && showConsole);
608 mTabControl.getBrowserActivity().setRequestedOrientation(
609 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
610 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
611 }
612
Steve Blockf344d032009-07-30 10:50:45 +0100613 private void maybeDisableWebsiteSettings(Context context) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100614 PreferenceActivity activity = (PreferenceActivity) context;
615 final PreferenceScreen screen = (PreferenceScreen)
616 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
617 screen.setEnabled(false);
618 WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
619 public void onReceiveValue(Map webStorageOrigins) {
620 if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) {
621 screen.setEnabled(true);
622 }
623 }
624 });
625
Steve Block2a6a0f42009-11-19 15:55:42 +0000626 GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
627 public void onReceiveValue(Set<String> geolocationOrigins) {
Nicolas Roard99b3ae12009-09-22 15:17:52 +0100628 if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) {
629 screen.setEnabled(true);
630 }
631 }
632 });
Steve Blockf344d032009-07-30 10:50:45 +0100633 }
634
Nicolas Roard78a98e42009-05-11 13:34:17 +0100635 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100636 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100637 maybeDisableWebsiteSettings(context);
638 }
639
640 /*package*/ void clearLocationAccess(Context context) {
641 GeolocationPermissions.getInstance().clearAll();
642 maybeDisableWebsiteSettings(context);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100643 }
644
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400645 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800646 reset();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800647 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400648 PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700649 p.edit().clear().apply();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400650 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800651 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700652 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700653 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100654 // reset appcache max size
655 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700656 }
657
658 private String getFactoryResetHomeUrl(Context context) {
659 String url = context.getResources().getString(R.string.homepage_base);
660 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500661 url = url.replace("{CID}",
662 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700663 }
664 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800665 }
666
The Android Open Source Project0c908882009-03-03 19:32:16 -0800667 // Private constructor that does nothing.
668 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800669 reset();
670 }
671
672 private void reset() {
673 // Private variables for settings
674 // NOTE: these defaults need to be kept in sync with the XML
675 // until the performance of PreferenceManager.setDefaultValues()
676 // is improved.
677 loadsImagesAutomatically = true;
678 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400679 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800680 javaScriptCanOpenWindowsAutomatically = false;
681 showSecurityWarnings = true;
682 rememberPasswords = true;
683 saveFormData = true;
684 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800685 autoFitPage = true;
686 landscapeOnly = false;
687 loadsPageInOverviewMode = true;
688 showDebugSettings = false;
689 // HTML5 API flags
690 appCacheEnabled = true;
691 databaseEnabled = true;
692 domStorageEnabled = true;
693 geolocationEnabled = true;
694 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800695 }
696}