blob: d3c1c00b83e6a78b6e4f844d1e0c7d0b6f86ce87 [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;
Ben Murdoch0cb81892010-10-08 12:41:33 +010031import android.database.Cursor;
Jeff Davidson43610292010-07-16 16:03:58 -070032import android.net.Uri;
Ben Murdoch0cb81892010-10-08 12:41:33 +010033import android.os.AsyncTask;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010034import android.os.Handler;
Nicolas Roard78a98e42009-05-11 13:34:17 +010035import android.preference.PreferenceActivity;
Ben Murdoch0cb81892010-10-08 12:41:33 +010036import android.preference.PreferenceManager;
Nicolas Roard78a98e42009-05-11 13:34:17 +010037import android.preference.PreferenceScreen;
Ben Murdoch0cb81892010-10-08 12:41:33 +010038import android.provider.Browser;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010039import android.provider.Settings;
40import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010042import android.webkit.GeolocationPermissions;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010043import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044import android.webkit.WebView;
45import android.webkit.WebViewDatabase;
46import android.webkit.WebIconDatabase;
47import android.webkit.WebSettings;
Ben Murdoch0cb81892010-10-08 12:41:33 +010048import android.webkit.WebSettings.AutoFillProfile;
Nicolas Roard78a98e42009-05-11 13:34:17 +010049import android.webkit.WebStorage;
Ben Murdoch0cb81892010-10-08 12:41:33 +010050import android.widget.Toast;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051
52import java.util.HashMap;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010053import java.util.Map;
54import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080055import java.util.Observable;
56
57/*
58 * Package level class for storing various WebView and Browser settings. To use
59 * this class:
60 * BrowserSettings s = BrowserSettings.getInstance();
61 * s.addObserver(webView.getSettings());
62 * s.loadFromDb(context); // Only needed on app startup
63 * s.javaScriptEnabled = true;
64 * ... // set any other settings
65 * s.update(); // this will update all the observers
66 *
67 * To remove an observer:
68 * s.deleteObserver(webView.getSettings());
69 */
Jeff Hamilton462b8e82010-09-23 14:33:43 -050070public class BrowserSettings extends Observable {
The Android Open Source Project0c908882009-03-03 19:32:16 -080071
Leon Scroggins9ac587d2009-04-20 12:53:46 -040072 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080073 // NOTE: these defaults need to be kept in sync with the XML
74 // until the performance of PreferenceManager.setDefaultValues()
75 // is improved.
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080076 // Note: boolean variables are set inside reset function.
77 private boolean loadsImagesAutomatically;
78 private boolean javaScriptEnabled;
Patrick Scotte536b332010-03-22 10:19:32 -040079 private WebSettings.PluginState pluginState;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080080 private boolean javaScriptCanOpenWindowsAutomatically;
81 private boolean showSecurityWarnings;
82 private boolean rememberPasswords;
83 private boolean saveFormData;
Ben Murdochce549fc2010-09-09 10:28:08 +010084 private boolean autoFillEnabled;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080085 private boolean openInBackground;
The Android Open Source Project0c908882009-03-03 19:32:16 -080086 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070087 private String homeUrl = "";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010088 private SearchEngine searchEngine;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080089 private boolean autoFitPage;
90 private boolean landscapeOnly;
91 private boolean loadsPageInOverviewMode;
92 private boolean showDebugSettings;
Andrei Popescu01068702009-08-03 16:03:24 +010093 // HTML5 API flags
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080094 private boolean appCacheEnabled;
95 private boolean databaseEnabled;
96 private boolean domStorageEnabled;
97 private boolean geolocationEnabled;
98 private boolean workersEnabled; // only affects V8. JSC does not have a similar setting
Andrei Popescu01068702009-08-03 16:03:24 +010099 // HTML5 API configuration params
100 private long appCacheMaxSize = Long.MAX_VALUE;
101 private String appCachePath; // default value set in loadFromDb().
102 private String databasePath; // default value set in loadFromDb()
Steve Block5029cf02009-08-21 13:16:58 +0100103 private String geolocationDatabasePath; // default value set in loadFromDb()
Andrei Popescu01068702009-08-03 16:03:24 +0100104 private WebStorageSizeManager webStorageSizeManager;
105
106 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800107
Nicolas Roard78a98e42009-05-11 13:34:17 +0100108 private final static String TAG = "BrowserSettings";
109
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110 // Development settings
111 public WebSettings.LayoutAlgorithm layoutAlgorithm =
112 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
113 private boolean useWideViewPort = true;
114 private int userAgent = 0;
115 private boolean tracing = false;
116 private boolean lightTouch = false;
117 private boolean navDump = false;
Feng Qianb3c02da2009-06-29 15:58:08 -0700118
Ben Murdochbff2d602009-07-01 20:19:05 +0100119 // By default the error console is shown once the user navigates to about:debug.
120 // The setting can be then toggled from the settings menu.
121 private boolean showConsole = true;
122
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123 // Private preconfigured values
Shimeng (Simon) Wangb4fb25c2010-07-13 15:18:10 -0700124 private static int minimumFontSize = 1;
125 private static int minimumLogicalFontSize = 1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800126 private static int defaultFontSize = 16;
127 private static int defaultFixedFontSize = 13;
128 private static WebSettings.TextSize textSize =
129 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700130 private static WebSettings.ZoomDensity zoomDensity =
131 WebSettings.ZoomDensity.MEDIUM;
Grace Kloba9804c432009-12-02 11:07:40 -0800132 private static int pageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800133
Ben Murdoch0cb81892010-10-08 12:41:33 +0100134
135 private AutoFillProfile mAutoFillProfile;
136 // TODO: For now we only support one autofill profile. Add support for
137 // multiple profiles later.
138 private int mActiveAutoFillProfileId = 1;
139
The Android Open Source Project0c908882009-03-03 19:32:16 -0800140 // Preference keys that are used outside this class
141 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
142 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
143 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
144 public final static String PREF_HOMEPAGE = "homepage";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100145 public final static String PREF_SEARCH_ENGINE = "search_engine";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 public final static String PREF_CLEAR_FORM_DATA =
147 "privacy_clear_form_data";
148 public final static String PREF_CLEAR_PASSWORDS =
149 "privacy_clear_passwords";
150 public final static String PREF_EXTRAS_RESET_DEFAULTS =
151 "reset_default_preferences";
152 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100153 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800154 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700155 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800156 public final static String PREF_DEFAULT_TEXT_ENCODING =
157 "default_text_encoding";
Steve Blockc6f577f2009-08-20 18:11:08 +0100158 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
159 "privacy_clear_geolocation_access";
Ben Murdochaf554522010-09-10 22:09:30 +0100160 public final static String PREF_AUTOFILL_ENABLED = "autofill_enabled";
161 public final static String PREF_AUTOFILL_PROFILE = "autofill_profile";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800162
163 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700164 "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
165 "like Gecko) Version/5.0 Safari/533.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800166
167 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700168 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
169 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
170
171 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
172 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
173 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
174
175 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
176 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
177 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800178
179 // Value to truncate strings when adding them to a TextView within
180 // a ListView
181 public final static int MAX_TEXTVIEW_LEN = 80;
182
Jeff Davidson43610292010-07-16 16:03:58 -0700183 public static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider";
184
185 public static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/");
186
The Android Open Source Project0c908882009-03-03 19:32:16 -0800187 private TabControl mTabControl;
188
189 // Single instance of the BrowserSettings for use in the Browser app.
190 private static BrowserSettings sSingleton;
191
192 // Private map of WebSettings to Observer objects used when deleting an
193 // observer.
194 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
195 new HashMap<WebSettings,Observer>();
196
197 /*
198 * An observer wrapper for updating a WebSettings object with the new
199 * settings after a call to BrowserSettings.update().
200 */
201 static class Observer implements java.util.Observer {
202 // Private WebSettings object that will be updated.
203 private WebSettings mSettings;
204
205 Observer(WebSettings w) {
206 mSettings = w;
207 }
208
209 public void update(Observable o, Object arg) {
210 BrowserSettings b = (BrowserSettings)o;
211 WebSettings s = mSettings;
212
213 s.setLayoutAlgorithm(b.layoutAlgorithm);
214 if (b.userAgent == 0) {
215 // use the default ua string
216 s.setUserAgentString(null);
217 } else if (b.userAgent == 1) {
218 s.setUserAgentString(DESKTOP_USERAGENT);
219 } else if (b.userAgent == 2) {
220 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700221 } else if (b.userAgent == 3) {
222 s.setUserAgentString(IPAD_USERAGENT);
223 } else if (b.userAgent == 4) {
224 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800225 }
226 s.setUseWideViewPort(b.useWideViewPort);
227 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
228 s.setJavaScriptEnabled(b.javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400229 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800230 s.setJavaScriptCanOpenWindowsAutomatically(
231 b.javaScriptCanOpenWindowsAutomatically);
232 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
233 s.setMinimumFontSize(b.minimumFontSize);
234 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
235 s.setDefaultFontSize(b.defaultFontSize);
236 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
237 s.setNavDump(b.navDump);
238 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700239 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800240 s.setLightTouchEnabled(b.lightTouch);
241 s.setSaveFormData(b.saveFormData);
Ben Murdochce549fc2010-09-09 10:28:08 +0100242 s.setAutoFillEnabled(b.autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800243 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700244 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800245 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800246
247 // WebView inside Browser doesn't want initial focus to be set.
248 s.setNeedInitialFocus(false);
249 // Browser supports multiple windows
250 s.setSupportMultipleWindows(true);
Grace Kloba61fc77c2010-06-22 09:57:33 -0700251 // enable smooth transition for better performance during panning or
252 // zooming
253 s.setEnableSmoothTransition(true);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100254
Andrei Popescu01068702009-08-03 16:03:24 +0100255 // HTML5 API flags
256 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100257 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100258 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100259 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100260 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100261
Andrei Popescu01068702009-08-03 16:03:24 +0100262 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100263 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100264 s.setAppCachePath(b.appCachePath);
265 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100266 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100267
Ben Murdoch0cb81892010-10-08 12:41:33 +0100268 // Active AutoFill profile data.
269 s.setAutoFillProfile(b.mAutoFillProfile);
270
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800271 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800272 }
273 }
274
275 /**
276 * Load settings from the browser app's database.
277 * NOTE: Strings used for the preferences must match those specified
Jeff Hamilton175ab302010-10-04 12:53:49 -0500278 * in the various preference XML files.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800279 * @param ctx A Context object used to query the browser's settings
280 * database. If the database exists, the saved settings will be
281 * stored in this BrowserSettings object. This will update all
282 * observers of this object.
283 */
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100284 public void loadFromDb(final Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800285 SharedPreferences p =
286 PreferenceManager.getDefaultSharedPreferences(ctx);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100287 // Set the default value for the Application Caches path.
288 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100289 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100290 webStorageSizeManager = new WebStorageSizeManager(
291 ctx,
292 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
293 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100294 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100295 // Set the default value for the Database path.
296 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100297 // Set the default value for the Geolocation database path.
298 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800299
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700300 if (p.getString(PREF_HOMEPAGE, "") == "") {
301 // No home page preferences is set, set it to default.
302 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
303 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700304
Grace Kloba9804c432009-12-02 11:07:40 -0800305 // the cost of one cached page is ~3M (measured using nytimes.com). For
306 // low end devices, we only cache one page. For high end devices, we try
307 // to cache more pages, currently choose 5.
308 ActivityManager am = (ActivityManager) ctx
309 .getSystemService(Context.ACTIVITY_SERVICE);
310 if (am.getMemoryClass() > 16) {
Andrei Popescuba676ef2010-03-29 12:12:55 +0100311 pageCacheCapacity = 5;
Grace Kloba9804c432009-12-02 11:07:40 -0800312 } else {
313 pageCacheCapacity = 1;
314 }
315
Ben Murdoch0cb81892010-10-08 12:41:33 +0100316 // Load the autofill profile data from the database. We use a database separate
317 // to the browser preference DB to make it easier to support multiple profiles
318 // and switching between them.
319 mAutoFillProfile = new AutoFillProfile();
320 AutoFillProfileDatabase autoFillDb = AutoFillProfileDatabase.getInstance(ctx);
321 Cursor c = autoFillDb.getProfile(mActiveAutoFillProfileId);
322
323 if (c.getCount() > 0) {
324 c.moveToFirst();
325 mAutoFillProfile.setFullName(c.getString(c.getColumnIndex(
326 AutoFillProfileDatabase.Profiles.FULL_NAME)));
327 mAutoFillProfile.setEmailAddress(c.getString(c.getColumnIndex(
328 AutoFillProfileDatabase.Profiles.EMAIL_ADDRESS)));
329 }
330 c.close();
331 autoFillDb.close();
332
Jeff Hamilton175ab302010-10-04 12:53:49 -0500333 // PreferenceManager.setDefaultValues is TOO SLOW, need to manually keep
334 // the defaults in sync
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100335 syncSharedPreferences(ctx, p);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800336 }
337
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100338 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700339
The Android Open Source Project0c908882009-03-03 19:32:16 -0800340 homeUrl =
341 p.getString(PREF_HOMEPAGE, homeUrl);
Leon Scroggins III31306602010-09-17 11:10:29 -0400342 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
343 SearchEngine.GOOGLE);
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100344 if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
345 if (searchEngine != null) {
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400346 if (searchEngine.supportsVoiceSearch()) {
347 // One or more tabs could have been in voice search mode.
348 // Clear it, since the new SearchEngine may not support
349 // it, or may handle it differently.
350 for (int i = 0; i < mTabControl.getTabCount(); i++) {
351 mTabControl.getTab(i).revertVoiceSearchMode();
352 }
353 }
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100354 searchEngine.close();
355 }
356 searchEngine = SearchEngines.get(ctx, searchEngineName);
357 }
358 Log.i(TAG, "Selected search engine: " + searchEngine);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700359
The Android Open Source Project0c908882009-03-03 19:32:16 -0800360 loadsImagesAutomatically = p.getBoolean("load_images",
361 loadsImagesAutomatically);
362 javaScriptEnabled = p.getBoolean("enable_javascript",
363 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400364 pluginState = WebSettings.PluginState.valueOf(
365 p.getString("plugin_state", pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800366 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
367 "block_popup_windows",
368 !javaScriptCanOpenWindowsAutomatically);
369 showSecurityWarnings = p.getBoolean("show_security_warnings",
370 showSecurityWarnings);
371 rememberPasswords = p.getBoolean("remember_passwords",
372 rememberPasswords);
373 saveFormData = p.getBoolean("save_formdata",
374 saveFormData);
Ben Murdochaf554522010-09-10 22:09:30 +0100375 autoFillEnabled = p.getBoolean("autofill_enabled", autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800376 boolean accept_cookies = p.getBoolean("accept_cookies",
377 CookieManager.getInstance().acceptCookie());
378 CookieManager.getInstance().setAcceptCookie(accept_cookies);
379 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800380 textSize = WebSettings.TextSize.valueOf(
381 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700382 zoomDensity = WebSettings.ZoomDensity.valueOf(
383 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800384 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700385 loadsPageInOverviewMode = p.getBoolean("load_page",
386 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700387 boolean landscapeOnlyTemp =
388 p.getBoolean("landscape_only", landscapeOnly);
389 if (landscapeOnlyTemp != landscapeOnly) {
390 landscapeOnly = landscapeOnlyTemp;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700391 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800392 useWideViewPort = true; // use wide view port for either setting
393 if (autoFitPage) {
394 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
395 } else {
396 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
397 }
398 defaultTextEncodingName =
399 p.getString(PREF_DEFAULT_TEXT_ENCODING,
400 defaultTextEncodingName);
401
402 showDebugSettings =
403 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
404 // Debug menu items have precidence if the menu is visible
405 if (showDebugSettings) {
406 boolean small_screen = p.getBoolean("small_screen",
407 layoutAlgorithm ==
408 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
409 if (small_screen) {
410 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
411 } else {
412 boolean normal_layout = p.getBoolean("normal_layout",
413 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
414 if (normal_layout) {
415 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
416 } else {
417 layoutAlgorithm =
418 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
419 }
420 }
421 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
422 tracing = p.getBoolean("enable_tracing", tracing);
423 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
424 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800425 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800426 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700427 // JS flags is loaded from DB even if showDebugSettings is false,
428 // so that it can be set once and be effective all the time.
429 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100430
431 // Read the setting for showing/hiding the JS Console always so that should the
432 // user enable debug settings, we already know if we should show the console.
433 // The user will never see the console unless they navigate to about:debug,
434 // regardless of the setting we read here. This setting is only used after debug
435 // is enabled.
436 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700437
Andrei Popescu01068702009-08-03 16:03:24 +0100438 // HTML5 API flags
439 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
440 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
441 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100442 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100443 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100444
Grace Klobad9ee1392009-07-20 11:53:33 -0700445 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800446 }
447
The Android Open Source Project0c908882009-03-03 19:32:16 -0800448 public String getHomePage() {
449 return homeUrl;
450 }
451
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100452 public SearchEngine getSearchEngine() {
453 return searchEngine;
454 }
455
Feng Qianb3c02da2009-06-29 15:58:08 -0700456 public String getJsFlags() {
457 return jsFlags;
458 }
459
Andrei Popescu79e82b72009-07-27 12:01:59 +0100460 public WebStorageSizeManager getWebStorageSizeManager() {
461 return webStorageSizeManager;
462 }
463
The Android Open Source Project0c908882009-03-03 19:32:16 -0800464 public void setHomePage(Context context, String url) {
465 Editor ed = PreferenceManager.
466 getDefaultSharedPreferences(context).edit();
467 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700468 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800469 homeUrl = url;
470 }
471
The Android Open Source Project0c908882009-03-03 19:32:16 -0800472 public WebSettings.TextSize getTextSize() {
473 return textSize;
474 }
475
Grace Kloba2f830682009-06-25 11:08:53 -0700476 public WebSettings.ZoomDensity getDefaultZoom() {
477 return zoomDensity;
478 }
479
The Android Open Source Project0c908882009-03-03 19:32:16 -0800480 public boolean openInBackground() {
481 return openInBackground;
482 }
483
484 public boolean showSecurityWarnings() {
485 return showSecurityWarnings;
486 }
487
488 public boolean isTracing() {
489 return tracing;
490 }
491
492 public boolean isLightTouch() {
493 return lightTouch;
494 }
495
496 public boolean isNavDump() {
497 return navDump;
498 }
499
The Android Open Source Project0c908882009-03-03 19:32:16 -0800500 public boolean showDebugSettings() {
501 return showDebugSettings;
502 }
503
504 public void toggleDebugSettings() {
505 showDebugSettings = !showDebugSettings;
506 navDump = showDebugSettings;
507 update();
508 }
509
Ben Murdoch0cb81892010-10-08 12:41:33 +0100510 public void setAutoFillProfile(Context ctx, AutoFillProfile profile) {
511 mAutoFillProfile = profile;
512 // Update the AutoFill DB
513 new SaveProfileToDbTask(ctx).execute(mAutoFillProfile);
514
515 }
516
517 public AutoFillProfile getAutoFillProfile() {
518 return mAutoFillProfile;
519 }
520
The Android Open Source Project0c908882009-03-03 19:32:16 -0800521 /**
522 * Add a WebSettings object to the list of observers that will be updated
523 * when update() is called.
524 *
525 * @param s A WebSettings object that is strictly tied to the life of a
526 * WebView.
527 */
528 public Observer addObserver(WebSettings s) {
529 Observer old = mWebSettingsToObservers.get(s);
530 if (old != null) {
531 super.deleteObserver(old);
532 }
533 Observer o = new Observer(s);
534 mWebSettingsToObservers.put(s, o);
535 super.addObserver(o);
536 return o;
537 }
538
539 /**
540 * Delete the given WebSettings observer from the list of observers.
541 * @param s The WebSettings object to be deleted.
542 */
543 public void deleteObserver(WebSettings s) {
544 Observer o = mWebSettingsToObservers.get(s);
545 if (o != null) {
546 mWebSettingsToObservers.remove(s);
547 super.deleteObserver(o);
548 }
549 }
550
551 /*
552 * Package level method for obtaining a single app instance of the
553 * BrowserSettings.
554 */
555 /*package*/ static BrowserSettings getInstance() {
556 if (sSingleton == null ) {
557 sSingleton = new BrowserSettings();
558 }
559 return sSingleton;
560 }
561
562 /*
563 * Package level method for associating the BrowserSettings with TabControl
564 */
565 /* package */void setTabControl(TabControl tabControl) {
566 mTabControl = tabControl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800567 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800568 }
569
570 /*
571 * Update all the observers of the object.
572 */
573 /*package*/ void update() {
574 setChanged();
575 notifyObservers();
576 }
577
578 /*package*/ void clearCache(Context context) {
579 WebIconDatabase.getInstance().removeAllIcons();
580 if (mTabControl != null) {
581 WebView current = mTabControl.getCurrentWebView();
582 if (current != null) {
583 current.clearCache(true);
584 }
585 }
586 }
587
588 /*package*/ void clearCookies(Context context) {
589 CookieManager.getInstance().removeAllCookie();
590 }
591
592 /* package */void clearHistory(Context context) {
593 ContentResolver resolver = context.getContentResolver();
594 Browser.clearHistory(resolver);
595 Browser.clearSearches(resolver);
596 }
597
598 /* package */ void clearFormData(Context context) {
599 WebViewDatabase.getInstance(context).clearFormData();
600 if (mTabControl != null) {
Henrik Baard794dc722010-02-19 16:28:06 +0100601 WebView currentTopView = mTabControl.getCurrentTopWebView();
602 if (currentTopView != null) {
603 currentTopView.clearFormData();
604 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800605 }
606 }
607
608 /*package*/ void clearPasswords(Context context) {
609 WebViewDatabase db = WebViewDatabase.getInstance(context);
610 db.clearUsernamePassword();
611 db.clearHttpAuthUsernamePassword();
612 }
613
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800614 private void updateTabControlSettings() {
615 // Enable/disable the error console.
616 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
617 showDebugSettings && showConsole);
618 mTabControl.getBrowserActivity().setRequestedOrientation(
619 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
620 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
621 }
622
Nicolas Roard78a98e42009-05-11 13:34:17 +0100623 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100624 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100625 }
626
627 /*package*/ void clearLocationAccess(Context context) {
628 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100629 }
630
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400631 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800632 reset();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500633 SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700634 p.edit().clear().apply();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500635 PreferenceManager.setDefaultValues(ctx, R.xml.page_content_preferences, true);
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500636 PreferenceManager.setDefaultValues(ctx, R.xml.personal_preferences, true);
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500637 PreferenceManager.setDefaultValues(ctx, R.xml.privacy_preferences, true);
638 PreferenceManager.setDefaultValues(ctx, R.xml.security_preferences, true);
639 PreferenceManager.setDefaultValues(ctx, R.xml.advanced_preferences, true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700640 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700641 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100642 // reset appcache max size
643 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700644 }
645
646 private String getFactoryResetHomeUrl(Context context) {
647 String url = context.getResources().getString(R.string.homepage_base);
648 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500649 url = url.replace("{CID}",
650 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700651 }
652 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800653 }
654
The Android Open Source Project0c908882009-03-03 19:32:16 -0800655 // Private constructor that does nothing.
656 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800657 reset();
658 }
659
660 private void reset() {
661 // Private variables for settings
662 // NOTE: these defaults need to be kept in sync with the XML
663 // until the performance of PreferenceManager.setDefaultValues()
664 // is improved.
665 loadsImagesAutomatically = true;
666 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400667 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800668 javaScriptCanOpenWindowsAutomatically = false;
669 showSecurityWarnings = true;
670 rememberPasswords = true;
671 saveFormData = true;
Ben Murdochce549fc2010-09-09 10:28:08 +0100672 autoFillEnabled = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800673 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800674 autoFitPage = true;
675 landscapeOnly = false;
676 loadsPageInOverviewMode = true;
677 showDebugSettings = false;
678 // HTML5 API flags
679 appCacheEnabled = true;
680 databaseEnabled = true;
681 domStorageEnabled = true;
682 geolocationEnabled = true;
683 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800684 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100685
686 private class SaveProfileToDbTask extends AsyncTask<AutoFillProfile, Void, Void> {
687
688 Context mContext;
689
690 public SaveProfileToDbTask(Context ctx) {
691 mContext = ctx;
692 }
693
694 protected Void doInBackground(AutoFillProfile... values) {
695 AutoFillProfileDatabase db =
696 AutoFillProfileDatabase.getInstance(mContext);
697 db.addOrUpdateProfile(mActiveAutoFillProfileId, values[0]);
698 return null;
699 }
700
701 protected void onPostExecute(Void result) {
702 String message = mContext.getString(R.string.autofill_profile_successful_save);
703 Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
704 }
705 }
706
The Android Open Source Project0c908882009-03-03 19:32:16 -0800707}