blob: 75e0cfb4d03c634dd3e27ee24009b446e2fd5e7a [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
John Reckbafe58a2011-01-11 10:26:02 -080020import com.android.browser.homepages.HomeProvider;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010021import com.android.browser.search.SearchEngine;
22import com.android.browser.search.SearchEngines;
23
Grace Kloba9804c432009-12-02 11:07:40 -080024import android.app.ActivityManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.content.ContentResolver;
26import android.content.Context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080027import android.content.SharedPreferences;
28import android.content.SharedPreferences.Editor;
John Reck812d2d62011-01-18 14:16:15 -080029import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Ben Murdoch0cb81892010-10-08 12:41:33 +010030import android.database.Cursor;
Jeff Davidson43610292010-07-16 16:03:58 -070031import android.net.Uri;
Ben Murdoch0cb81892010-10-08 12:41:33 +010032import android.os.AsyncTask;
Ben Murdoch23da30e2010-10-26 15:18:44 +010033import android.os.Message;
Ben Murdoch0cb81892010-10-08 12:41:33 +010034import android.preference.PreferenceManager;
Ben Murdoch0cb81892010-10-08 12:41:33 +010035import android.provider.Browser;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010036import 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;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.webkit.WebIconDatabase;
40import android.webkit.WebSettings;
Ben Murdoch0cb81892010-10-08 12:41:33 +010041import android.webkit.WebSettings.AutoFillProfile;
Nicolas Roard78a98e42009-05-11 13:34:17 +010042import android.webkit.WebStorage;
John Reck812d2d62011-01-18 14:16:15 -080043import android.webkit.WebView;
44import android.webkit.WebViewDatabase;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045
46import java.util.HashMap;
47import java.util.Observable;
48
49/*
50 * Package level class for storing various WebView and Browser settings. To use
51 * this class:
52 * BrowserSettings s = BrowserSettings.getInstance();
53 * s.addObserver(webView.getSettings());
54 * s.loadFromDb(context); // Only needed on app startup
55 * s.javaScriptEnabled = true;
56 * ... // set any other settings
57 * s.update(); // this will update all the observers
58 *
59 * To remove an observer:
60 * s.deleteObserver(webView.getSettings());
61 */
John Reck63bb6872010-12-01 19:29:32 -080062public class BrowserSettings extends Observable implements OnSharedPreferenceChangeListener {
Leon Scroggins9ac587d2009-04-20 12:53:46 -040063 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080064 // NOTE: these defaults need to be kept in sync with the XML
65 // until the performance of PreferenceManager.setDefaultValues()
66 // is improved.
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080067 // Note: boolean variables are set inside reset function.
68 private boolean loadsImagesAutomatically;
69 private boolean javaScriptEnabled;
Patrick Scotte536b332010-03-22 10:19:32 -040070 private WebSettings.PluginState pluginState;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080071 private boolean javaScriptCanOpenWindowsAutomatically;
72 private boolean showSecurityWarnings;
73 private boolean rememberPasswords;
74 private boolean saveFormData;
Ben Murdochce549fc2010-09-09 10:28:08 +010075 private boolean autoFillEnabled;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080076 private boolean openInBackground;
The Android Open Source Project0c908882009-03-03 19:32:16 -080077 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070078 private String homeUrl = "";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010079 private SearchEngine searchEngine;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080080 private boolean autoFitPage;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080081 private boolean loadsPageInOverviewMode;
82 private boolean showDebugSettings;
Andrei Popescu01068702009-08-03 16:03:24 +010083 // HTML5 API flags
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080084 private boolean appCacheEnabled;
85 private boolean databaseEnabled;
86 private boolean domStorageEnabled;
87 private boolean geolocationEnabled;
88 private boolean workersEnabled; // only affects V8. JSC does not have a similar setting
Andrei Popescu01068702009-08-03 16:03:24 +010089 // HTML5 API configuration params
90 private long appCacheMaxSize = Long.MAX_VALUE;
91 private String appCachePath; // default value set in loadFromDb().
92 private String databasePath; // default value set in loadFromDb()
Steve Block5029cf02009-08-21 13:16:58 +010093 private String geolocationDatabasePath; // default value set in loadFromDb()
Andrei Popescu01068702009-08-03 16:03:24 +010094 private WebStorageSizeManager webStorageSizeManager;
Patrick Scott539e2ec2011-01-13 11:27:38 -050095 // Autologin settings
96 private boolean autoLoginEnabled;
97 private String autoLoginAccount;
Andrei Popescu01068702009-08-03 16:03:24 +010098
99 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800100
Nicolas Roard78a98e42009-05-11 13:34:17 +0100101 private final static String TAG = "BrowserSettings";
102
The Android Open Source Project0c908882009-03-03 19:32:16 -0800103 // Development settings
104 public WebSettings.LayoutAlgorithm layoutAlgorithm =
105 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
106 private boolean useWideViewPort = true;
107 private int userAgent = 0;
108 private boolean tracing = false;
109 private boolean lightTouch = false;
110 private boolean navDump = false;
Derek Sollenberger8de82852010-11-18 19:51:50 -0500111 private boolean hardwareAccelerated = true;
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800112 private boolean showVisualIndicator = false;
Michael Kolb376b5412010-12-15 11:52:57 -0800113 // Lab settings
114 private boolean quickControls = false;
John Reckbafe58a2011-01-11 10:26:02 -0800115 private boolean useMostVisitedHomepage = false;
Narayan Kamath5119edd2011-02-23 15:49:17 +0000116 private boolean useInstant = false;
Michael Kolb376b5412010-12-15 11:52:57 -0800117
Ben Murdochbff2d602009-07-01 20:19:05 +0100118 // By default the error console is shown once the user navigates to about:debug.
119 // The setting can be then toggled from the settings menu.
120 private boolean showConsole = true;
121
The Android Open Source Project0c908882009-03-03 19:32:16 -0800122 // Private preconfigured values
Shimeng (Simon) Wangb4fb25c2010-07-13 15:18:10 -0700123 private static int minimumFontSize = 1;
124 private static int minimumLogicalFontSize = 1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800125 private static int defaultFontSize = 16;
126 private static int defaultFixedFontSize = 13;
127 private static WebSettings.TextSize textSize =
128 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700129 private static WebSettings.ZoomDensity zoomDensity =
130 WebSettings.ZoomDensity.MEDIUM;
Grace Kloba9804c432009-12-02 11:07:40 -0800131 private static int pageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800132
Ben Murdoch0cb81892010-10-08 12:41:33 +0100133
Ben Murdoch23da30e2010-10-26 15:18:44 +0100134 private AutoFillProfile autoFillProfile;
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100135 // Default to zero. In the case no profile is set up, the initial
136 // value will come from the AutoFillSettingsFragment when the user
137 // creates a profile. Otherwise, we'll read the ID of the last used
138 // profile from the prefs db.
139 private int autoFillActiveProfileId;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100140 private static final int NO_AUTOFILL_PROFILE_SET = 0;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100141
The Android Open Source Project0c908882009-03-03 19:32:16 -0800142 // Preference keys that are used outside this class
143 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
144 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
145 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
146 public final static String PREF_HOMEPAGE = "homepage";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100147 public final static String PREF_SEARCH_ENGINE = "search_engine";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800148 public final static String PREF_CLEAR_FORM_DATA =
149 "privacy_clear_form_data";
150 public final static String PREF_CLEAR_PASSWORDS =
151 "privacy_clear_passwords";
152 public final static String PREF_EXTRAS_RESET_DEFAULTS =
153 "reset_default_preferences";
154 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100155 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800156 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700157 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800158 public final static String PREF_DEFAULT_TEXT_ENCODING =
159 "default_text_encoding";
Steve Blockc6f577f2009-08-20 18:11:08 +0100160 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
161 "privacy_clear_geolocation_access";
Ben Murdochaf554522010-09-10 22:09:30 +0100162 public final static String PREF_AUTOFILL_ENABLED = "autofill_enabled";
163 public final static String PREF_AUTOFILL_PROFILE = "autofill_profile";
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100164 public final static String PREF_AUTOFILL_ACTIVE_PROFILE_ID = "autofill_active_profile_id";
John Reck63bb6872010-12-01 19:29:32 -0800165 public final static String PREF_HARDWARE_ACCEL = "enable_hardware_accel";
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800166 public final static String PREF_VISUAL_INDICATOR = "enable_visual_indicator";
John Reck63bb6872010-12-01 19:29:32 -0800167 public final static String PREF_USER_AGENT = "user_agent";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800168
Michael Kolb376b5412010-12-15 11:52:57 -0800169 public final static String PREF_QUICK_CONTROLS = "enable_quick_controls";
John Reckbafe58a2011-01-11 10:26:02 -0800170 public final static String PREF_MOST_VISITED_HOMEPAGE = "use_most_visited_homepage";
Patrick Scott539e2ec2011-01-13 11:27:38 -0500171 public final static String PREF_AUTOLOGIN = "enable_autologin";
172 public final static String PREF_AUTOLOGIN_ACCOUNT = "autologin_account";
John Reck282431b2011-01-20 17:38:37 -0800173 public final static String PREF_PLUGIN_STATE = "plugin_state";
Narayan Kamath5119edd2011-02-23 15:49:17 +0000174 public final static String PREF_USE_INSTANT = "use_instant_search";
Michael Kolb376b5412010-12-15 11:52:57 -0800175
The Android Open Source Project0c908882009-03-03 19:32:16 -0800176 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700177 "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
178 "like Gecko) Version/5.0 Safari/533.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800179
180 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700181 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
182 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
183
184 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
185 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
186 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
187
188 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
189 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
190 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800191
192 // Value to truncate strings when adding them to a TextView within
193 // a ListView
194 public final static int MAX_TEXTVIEW_LEN = 80;
195
Jeff Davidson43610292010-07-16 16:03:58 -0700196 public static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider";
197
198 public static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/");
199
John Reck63bb6872010-12-01 19:29:32 -0800200 // Set to true to enable some of the about:debug options
John Reck9eed0ef2011-01-11 17:11:37 -0800201 public static final boolean DEV_BUILD = false;
John Reck63bb6872010-12-01 19:29:32 -0800202
Michael Kolb8233fac2010-10-26 16:08:53 -0700203 private Controller mController;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800204
205 // Single instance of the BrowserSettings for use in the Browser app.
206 private static BrowserSettings sSingleton;
207
208 // Private map of WebSettings to Observer objects used when deleting an
209 // observer.
210 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
211 new HashMap<WebSettings,Observer>();
212
Ben Murdochef671652010-11-25 17:17:58 +0000213 private boolean mLoadFromDbComplete;
214
215 public void waitForLoadFromDbToComplete() {
216 synchronized (sSingleton) {
217 while (!mLoadFromDbComplete) {
218 try {
219 sSingleton.wait();
220 } catch (InterruptedException e) { }
221 }
222 }
223 }
224
The Android Open Source Project0c908882009-03-03 19:32:16 -0800225 /*
226 * An observer wrapper for updating a WebSettings object with the new
227 * settings after a call to BrowserSettings.update().
228 */
229 static class Observer implements java.util.Observer {
230 // Private WebSettings object that will be updated.
231 private WebSettings mSettings;
232
233 Observer(WebSettings w) {
234 mSettings = w;
235 }
236
237 public void update(Observable o, Object arg) {
238 BrowserSettings b = (BrowserSettings)o;
239 WebSettings s = mSettings;
240
241 s.setLayoutAlgorithm(b.layoutAlgorithm);
242 if (b.userAgent == 0) {
243 // use the default ua string
244 s.setUserAgentString(null);
245 } else if (b.userAgent == 1) {
246 s.setUserAgentString(DESKTOP_USERAGENT);
247 } else if (b.userAgent == 2) {
248 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700249 } else if (b.userAgent == 3) {
250 s.setUserAgentString(IPAD_USERAGENT);
251 } else if (b.userAgent == 4) {
252 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800253 }
254 s.setUseWideViewPort(b.useWideViewPort);
255 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
256 s.setJavaScriptEnabled(b.javaScriptEnabled);
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800257 s.setShowVisualIndicator(b.showVisualIndicator);
Patrick Scotte536b332010-03-22 10:19:32 -0400258 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800259 s.setJavaScriptCanOpenWindowsAutomatically(
260 b.javaScriptCanOpenWindowsAutomatically);
261 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
262 s.setMinimumFontSize(b.minimumFontSize);
263 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
264 s.setDefaultFontSize(b.defaultFontSize);
265 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
266 s.setNavDump(b.navDump);
267 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700268 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800269 s.setLightTouchEnabled(b.lightTouch);
270 s.setSaveFormData(b.saveFormData);
Ben Murdochce549fc2010-09-09 10:28:08 +0100271 s.setAutoFillEnabled(b.autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800272 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700273 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800274 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800275
276 // WebView inside Browser doesn't want initial focus to be set.
277 s.setNeedInitialFocus(false);
278 // Browser supports multiple windows
279 s.setSupportMultipleWindows(true);
Grace Kloba61fc77c2010-06-22 09:57:33 -0700280 // enable smooth transition for better performance during panning or
281 // zooming
282 s.setEnableSmoothTransition(true);
Patrick Scottb92bbb42011-01-05 11:38:58 -0500283 // disable content url access
284 s.setAllowContentAccess(false);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100285
Andrei Popescu01068702009-08-03 16:03:24 +0100286 // HTML5 API flags
287 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100288 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100289 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100290 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100291 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100292
Andrei Popescu01068702009-08-03 16:03:24 +0100293 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100294 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100295 s.setAppCachePath(b.appCachePath);
296 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100297 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100298
Ben Murdoch0cb81892010-10-08 12:41:33 +0100299 // Active AutoFill profile data.
Ben Murdoch23da30e2010-10-26 15:18:44 +0100300 s.setAutoFillProfile(b.autoFillProfile);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100301
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800302 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800303 }
304 }
305
306 /**
Ben Murdochef671652010-11-25 17:17:58 +0000307 * Load settings from the browser app's database. It is performed in
308 * an AsyncTask as it involves plenty of slow disk IO.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800309 * NOTE: Strings used for the preferences must match those specified
Jeff Hamilton175ab302010-10-04 12:53:49 -0500310 * in the various preference XML files.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800311 * @param ctx A Context object used to query the browser's settings
312 * database. If the database exists, the saved settings will be
313 * stored in this BrowserSettings object. This will update all
314 * observers of this object.
315 */
Ben Murdochef671652010-11-25 17:17:58 +0000316 public void asyncLoadFromDb(final Context ctx) {
317 mLoadFromDbComplete = false;
318 // Run the initial settings load in an AsyncTask as it hits the
319 // disk multiple times through SharedPreferences and SQLite. We
320 // need to be certain though that this has completed before we start
321 // to load pages though, so in the worst case we will block waiting
322 // for it to finish in BrowserActivity.onCreate().
323 new LoadFromDbTask(ctx).execute();
324 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800325
Ben Murdochef671652010-11-25 17:17:58 +0000326 private class LoadFromDbTask extends AsyncTask<Void, Void, Void> {
327 private Context mContext;
328
329 public LoadFromDbTask(Context context) {
330 mContext = context;
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700331 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700332
Ben Murdochef671652010-11-25 17:17:58 +0000333 protected Void doInBackground(Void... unused) {
334 SharedPreferences p =
335 PreferenceManager.getDefaultSharedPreferences(mContext);
336 // Set the default value for the Application Caches path.
337 appCachePath = mContext.getDir("appcache", 0).getPath();
338 // Determine the maximum size of the application cache.
339 webStorageSizeManager = new WebStorageSizeManager(
340 mContext,
341 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
342 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
343 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
344 // Set the default value for the Database path.
345 databasePath = mContext.getDir("databases", 0).getPath();
346 // Set the default value for the Geolocation database path.
347 geolocationDatabasePath = mContext.getDir("geolocation", 0).getPath();
348
John Reckef074262010-12-02 16:09:14 -0800349 if (p.getString(PREF_HOMEPAGE, null) == null) {
Ben Murdochef671652010-11-25 17:17:58 +0000350 // No home page preferences is set, set it to default.
351 setHomePage(mContext, getFactoryResetHomeUrl(mContext));
352 }
353
354 // the cost of one cached page is ~3M (measured using nytimes.com). For
355 // low end devices, we only cache one page. For high end devices, we try
356 // to cache more pages, currently choose 5.
357 ActivityManager am = (ActivityManager) mContext
358 .getSystemService(Context.ACTIVITY_SERVICE);
359 if (am.getMemoryClass() > 16) {
360 pageCacheCapacity = 5;
361 } else {
362 pageCacheCapacity = 1;
363 }
364
365 // Read the last active AutoFill profile id.
366 autoFillActiveProfileId = p.getInt(
367 PREF_AUTOFILL_ACTIVE_PROFILE_ID, autoFillActiveProfileId);
368
369 // Load the autofill profile data from the database. We use a database separate
370 // to the browser preference DB to make it easier to support multiple profiles
371 // and switching between them.
372 AutoFillProfileDatabase autoFillDb = AutoFillProfileDatabase.getInstance(mContext);
373 Cursor c = autoFillDb.getProfile(autoFillActiveProfileId);
374
375 if (c.getCount() > 0) {
376 c.moveToFirst();
377
378 String fullName = c.getString(c.getColumnIndex(
379 AutoFillProfileDatabase.Profiles.FULL_NAME));
380 String email = c.getString(c.getColumnIndex(
381 AutoFillProfileDatabase.Profiles.EMAIL_ADDRESS));
382 String company = c.getString(c.getColumnIndex(
383 AutoFillProfileDatabase.Profiles.COMPANY_NAME));
384 String addressLine1 = c.getString(c.getColumnIndex(
385 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_1));
386 String addressLine2 = c.getString(c.getColumnIndex(
387 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_2));
388 String city = c.getString(c.getColumnIndex(
389 AutoFillProfileDatabase.Profiles.CITY));
390 String state = c.getString(c.getColumnIndex(
391 AutoFillProfileDatabase.Profiles.STATE));
392 String zip = c.getString(c.getColumnIndex(
393 AutoFillProfileDatabase.Profiles.ZIP_CODE));
394 String country = c.getString(c.getColumnIndex(
395 AutoFillProfileDatabase.Profiles.COUNTRY));
396 String phone = c.getString(c.getColumnIndex(
397 AutoFillProfileDatabase.Profiles.PHONE_NUMBER));
398 autoFillProfile = new AutoFillProfile(autoFillActiveProfileId,
399 fullName, email, company, addressLine1, addressLine2, city,
400 state, zip, country, phone);
401 }
402 c.close();
403 autoFillDb.close();
404
405 // PreferenceManager.setDefaultValues is TOO SLOW, need to manually keep
406 // the defaults in sync
John Reck63bb6872010-12-01 19:29:32 -0800407 p.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
Ben Murdochef671652010-11-25 17:17:58 +0000408 syncSharedPreferences(mContext, p);
409
410 synchronized (sSingleton) {
411 mLoadFromDbComplete = true;
412 sSingleton.notify();
413 }
414 return null;
Grace Kloba9804c432009-12-02 11:07:40 -0800415 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800416 }
417
Narayan Kamath5119edd2011-02-23 15:49:17 +0000418 private void updateSearchEngine(Context ctx, String searchEngineName, boolean force) {
419 if (force || searchEngine == null ||
420 !searchEngine.getName().equals(searchEngineName)) {
421 if (searchEngine != null) {
422 if (searchEngine.supportsVoiceSearch()) {
423 // One or more tabs could have been in voice search mode.
424 // Clear it, since the new SearchEngine may not support
425 // it, or may handle it differently.
426 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
427 mController.getTabControl().getTab(i).revertVoiceSearchMode();
428 }
429 }
430 searchEngine.close();
431 }
432 searchEngine = SearchEngines.get(ctx, searchEngineName);
433
434 if (mController != null && (searchEngine instanceof InstantSearchEngine)) {
435 ((InstantSearchEngine) searchEngine).setController(mController);
436 }
437 }
438 }
439
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100440 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700441
The Android Open Source Project0c908882009-03-03 19:32:16 -0800442 homeUrl =
443 p.getString(PREF_HOMEPAGE, homeUrl);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000444
445 useInstant = p.getBoolean(PREF_USE_INSTANT, useInstant);
Leon Scroggins III31306602010-09-17 11:10:29 -0400446 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
Narayan Kamath5119edd2011-02-23 15:49:17 +0000447 SearchEngine.GOOGLE);
448 updateSearchEngine(ctx, searchEngineName, false);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700449
The Android Open Source Project0c908882009-03-03 19:32:16 -0800450 loadsImagesAutomatically = p.getBoolean("load_images",
451 loadsImagesAutomatically);
452 javaScriptEnabled = p.getBoolean("enable_javascript",
453 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400454 pluginState = WebSettings.PluginState.valueOf(
John Reck282431b2011-01-20 17:38:37 -0800455 p.getString(PREF_PLUGIN_STATE, pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800456 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
457 "block_popup_windows",
458 !javaScriptCanOpenWindowsAutomatically);
459 showSecurityWarnings = p.getBoolean("show_security_warnings",
460 showSecurityWarnings);
461 rememberPasswords = p.getBoolean("remember_passwords",
462 rememberPasswords);
463 saveFormData = p.getBoolean("save_formdata",
464 saveFormData);
Ben Murdochaf554522010-09-10 22:09:30 +0100465 autoFillEnabled = p.getBoolean("autofill_enabled", autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800466 boolean accept_cookies = p.getBoolean("accept_cookies",
467 CookieManager.getInstance().acceptCookie());
468 CookieManager.getInstance().setAcceptCookie(accept_cookies);
469 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800470 textSize = WebSettings.TextSize.valueOf(
471 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700472 zoomDensity = WebSettings.ZoomDensity.valueOf(
473 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800474 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700475 loadsPageInOverviewMode = p.getBoolean("load_page",
476 loadsPageInOverviewMode);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800477 useWideViewPort = true; // use wide view port for either setting
478 if (autoFitPage) {
479 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
480 } else {
481 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
482 }
483 defaultTextEncodingName =
484 p.getString(PREF_DEFAULT_TEXT_ENCODING,
485 defaultTextEncodingName);
486
487 showDebugSettings =
488 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
489 // Debug menu items have precidence if the menu is visible
490 if (showDebugSettings) {
491 boolean small_screen = p.getBoolean("small_screen",
492 layoutAlgorithm ==
493 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
494 if (small_screen) {
495 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
496 } else {
497 boolean normal_layout = p.getBoolean("normal_layout",
498 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
499 if (normal_layout) {
500 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
501 } else {
502 layoutAlgorithm =
503 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
504 }
505 }
506 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
507 tracing = p.getBoolean("enable_tracing", tracing);
508 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
509 navDump = p.getBoolean("enable_nav_dump", navDump);
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800510 showVisualIndicator = p.getBoolean(PREF_VISUAL_INDICATOR, showVisualIndicator);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800511 }
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500512
Michael Kolb376b5412010-12-15 11:52:57 -0800513 quickControls = p.getBoolean(PREF_QUICK_CONTROLS, quickControls);
John Reckbafe58a2011-01-11 10:26:02 -0800514 useMostVisitedHomepage = p.getBoolean(PREF_MOST_VISITED_HOMEPAGE, useMostVisitedHomepage);
Michael Kolb376b5412010-12-15 11:52:57 -0800515
John Reck3db2e072011-01-24 14:57:18 -0800516 // Only set these if this is a dev build or debug is enabled
517 if (DEV_BUILD || showDebugSettings()) {
John Reck63bb6872010-12-01 19:29:32 -0800518 userAgent = Integer.parseInt(p.getString(PREF_USER_AGENT, "0"));
519 hardwareAccelerated = p.getBoolean(PREF_HARDWARE_ACCEL, hardwareAccelerated);
520 }
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500521
Feng Qianb3c02da2009-06-29 15:58:08 -0700522 // JS flags is loaded from DB even if showDebugSettings is false,
523 // so that it can be set once and be effective all the time.
524 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100525
526 // Read the setting for showing/hiding the JS Console always so that should the
527 // user enable debug settings, we already know if we should show the console.
528 // The user will never see the console unless they navigate to about:debug,
529 // regardless of the setting we read here. This setting is only used after debug
530 // is enabled.
531 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700532
Andrei Popescu01068702009-08-03 16:03:24 +0100533 // HTML5 API flags
534 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
535 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
536 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100537 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100538 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100539
Patrick Scott539e2ec2011-01-13 11:27:38 -0500540 // Autologin account settings. The account preference may be null until
541 // the user explicitly changes the account in the settings.
542 autoLoginEnabled = p.getBoolean(PREF_AUTOLOGIN, autoLoginEnabled);
543 autoLoginAccount = p.getString(PREF_AUTOLOGIN_ACCOUNT, autoLoginAccount);
544
Grace Klobad9ee1392009-07-20 11:53:33 -0700545 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800546 }
547
The Android Open Source Project0c908882009-03-03 19:32:16 -0800548 public String getHomePage() {
John Reckbafe58a2011-01-11 10:26:02 -0800549 if (useMostVisitedHomepage) {
550 return HomeProvider.MOST_VISITED;
551 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800552 return homeUrl;
553 }
554
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100555 public SearchEngine getSearchEngine() {
556 return searchEngine;
557 }
558
Feng Qianb3c02da2009-06-29 15:58:08 -0700559 public String getJsFlags() {
560 return jsFlags;
561 }
562
Andrei Popescu79e82b72009-07-27 12:01:59 +0100563 public WebStorageSizeManager getWebStorageSizeManager() {
564 return webStorageSizeManager;
565 }
566
The Android Open Source Project0c908882009-03-03 19:32:16 -0800567 public void setHomePage(Context context, String url) {
568 Editor ed = PreferenceManager.
569 getDefaultSharedPreferences(context).edit();
570 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700571 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800572 homeUrl = url;
573 }
574
The Android Open Source Project0c908882009-03-03 19:32:16 -0800575 public WebSettings.TextSize getTextSize() {
576 return textSize;
577 }
578
Grace Kloba2f830682009-06-25 11:08:53 -0700579 public WebSettings.ZoomDensity getDefaultZoom() {
580 return zoomDensity;
581 }
582
The Android Open Source Project0c908882009-03-03 19:32:16 -0800583 public boolean openInBackground() {
584 return openInBackground;
585 }
586
587 public boolean showSecurityWarnings() {
588 return showSecurityWarnings;
589 }
590
591 public boolean isTracing() {
592 return tracing;
593 }
594
595 public boolean isLightTouch() {
596 return lightTouch;
597 }
598
599 public boolean isNavDump() {
600 return navDump;
601 }
602
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500603 public boolean isHardwareAccelerated() {
604 return hardwareAccelerated;
605 }
606
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800607 public boolean showVisualIndicator() {
608 return showVisualIndicator;
609 }
610
Michael Kolb376b5412010-12-15 11:52:57 -0800611 public boolean useQuickControls() {
612 return quickControls;
613 }
614
John Reckbafe58a2011-01-11 10:26:02 -0800615 public boolean useMostVisitedHomepage() {
616 return useMostVisitedHomepage;
617 }
618
Narayan Kamath5119edd2011-02-23 15:49:17 +0000619 public boolean useInstant() {
620 return useInstant;
621 }
622
The Android Open Source Project0c908882009-03-03 19:32:16 -0800623 public boolean showDebugSettings() {
624 return showDebugSettings;
625 }
626
John Reck3db2e072011-01-24 14:57:18 -0800627 public void toggleDebugSettings(Context context) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800628 showDebugSettings = !showDebugSettings;
629 navDump = showDebugSettings;
John Reck3db2e072011-01-24 14:57:18 -0800630 syncSharedPreferences(context,
631 PreferenceManager.getDefaultSharedPreferences(context));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800632 update();
633 }
634
Patrick Scott539e2ec2011-01-13 11:27:38 -0500635 public boolean isAutoLoginEnabled() {
636 return autoLoginEnabled;
637 }
638
639 public String getAutoLoginAccount(Context context) {
640 // Each time we attempt to get the account, we need to verify that the
641 // account is still valid.
642 return GoogleAccountLogin.validateAccount(context, autoLoginAccount);
643 }
644
645 public void setAutoLoginAccount(Context context, String name) {
646 Editor ed = PreferenceManager.
647 getDefaultSharedPreferences(context).edit();
648 ed.putString(PREF_AUTOLOGIN_ACCOUNT, name);
649 ed.apply();
650 autoLoginAccount = name;
651 }
652
John Reck812d2d62011-01-18 14:16:15 -0800653 public void setAutoLoginEnabled(Context context, boolean enable) {
654 Editor ed = PreferenceManager.
655 getDefaultSharedPreferences(context).edit();
656 ed.putBoolean(PREF_AUTOLOGIN, enable);
657 ed.apply();
658 autoLoginEnabled = enable;
659 }
660
Ben Murdoch23da30e2010-10-26 15:18:44 +0100661 public void setAutoFillProfile(Context ctx, AutoFillProfile profile, Message msg) {
662 if (profile != null) {
663 setActiveAutoFillProfileId(ctx, profile.getUniqueId());
664 // Update the AutoFill DB with the new profile.
665 new SaveProfileToDbTask(ctx, msg).execute(profile);
666 } else {
667 // Delete the current profile.
Ben Murdoche8a28332010-11-17 14:16:21 +0000668 if (autoFillProfile != null) {
669 new DeleteProfileFromDbTask(ctx, msg).execute(autoFillProfile.getUniqueId());
670 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
671 }
Ben Murdoch23da30e2010-10-26 15:18:44 +0100672 }
673 autoFillProfile = profile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100674 }
675
676 public AutoFillProfile getAutoFillProfile() {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100677 return autoFillProfile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100678 }
679
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100680 private void setActiveAutoFillProfileId(Context context, int activeProfileId) {
681 autoFillActiveProfileId = activeProfileId;
682 Editor ed = PreferenceManager.
683 getDefaultSharedPreferences(context).edit();
684 ed.putInt(PREF_AUTOFILL_ACTIVE_PROFILE_ID, activeProfileId);
685 ed.apply();
686 }
687
Ben Murdoch8029a772010-11-16 11:58:21 +0000688 /* package */ void disableAutoFill(Context ctx) {
689 autoFillEnabled = false;
690 Editor ed = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
691 ed.putBoolean(PREF_AUTOFILL_ENABLED, false);
692 ed.apply();
693 }
694
The Android Open Source Project0c908882009-03-03 19:32:16 -0800695 /**
696 * Add a WebSettings object to the list of observers that will be updated
697 * when update() is called.
698 *
699 * @param s A WebSettings object that is strictly tied to the life of a
700 * WebView.
701 */
702 public Observer addObserver(WebSettings s) {
703 Observer old = mWebSettingsToObservers.get(s);
704 if (old != null) {
705 super.deleteObserver(old);
706 }
707 Observer o = new Observer(s);
708 mWebSettingsToObservers.put(s, o);
709 super.addObserver(o);
710 return o;
711 }
712
713 /**
714 * Delete the given WebSettings observer from the list of observers.
715 * @param s The WebSettings object to be deleted.
716 */
717 public void deleteObserver(WebSettings s) {
718 Observer o = mWebSettingsToObservers.get(s);
719 if (o != null) {
720 mWebSettingsToObservers.remove(s);
721 super.deleteObserver(o);
722 }
723 }
724
725 /*
John Reck63bb6872010-12-01 19:29:32 -0800726 * Application level method for obtaining a single app instance of the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800727 * BrowserSettings.
728 */
John Reck63bb6872010-12-01 19:29:32 -0800729 public static BrowserSettings getInstance() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800730 if (sSingleton == null ) {
731 sSingleton = new BrowserSettings();
732 }
733 return sSingleton;
734 }
735
736 /*
737 * Package level method for associating the BrowserSettings with TabControl
738 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700739 /* package */void setController(Controller ctrl) {
740 mController = ctrl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800741 updateTabControlSettings();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000742
743 if (mController != null && (searchEngine instanceof InstantSearchEngine)) {
744 ((InstantSearchEngine) searchEngine).setController(mController);
745 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800746 }
747
748 /*
749 * Update all the observers of the object.
750 */
751 /*package*/ void update() {
752 setChanged();
753 notifyObservers();
754 }
755
756 /*package*/ void clearCache(Context context) {
757 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700758 if (mController != null) {
759 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800760 if (current != null) {
761 current.clearCache(true);
762 }
763 }
764 }
765
766 /*package*/ void clearCookies(Context context) {
767 CookieManager.getInstance().removeAllCookie();
768 }
769
770 /* package */void clearHistory(Context context) {
771 ContentResolver resolver = context.getContentResolver();
772 Browser.clearHistory(resolver);
773 Browser.clearSearches(resolver);
774 }
775
776 /* package */ void clearFormData(Context context) {
777 WebViewDatabase.getInstance(context).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700778 if (mController!= null) {
779 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100780 if (currentTopView != null) {
781 currentTopView.clearFormData();
782 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800783 }
784 }
785
786 /*package*/ void clearPasswords(Context context) {
787 WebViewDatabase db = WebViewDatabase.getInstance(context);
788 db.clearUsernamePassword();
789 db.clearHttpAuthUsernamePassword();
790 }
791
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800792 private void updateTabControlSettings() {
793 // Enable/disable the error console.
Michael Kolb8233fac2010-10-26 16:08:53 -0700794 mController.setShouldShowErrorConsole(
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800795 showDebugSettings && showConsole);
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800796 }
797
Nicolas Roard78a98e42009-05-11 13:34:17 +0100798 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100799 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100800 }
801
802 /*package*/ void clearLocationAccess(Context context) {
803 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100804 }
805
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400806 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800807 reset();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500808 SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700809 p.edit().clear().apply();
John Reck035a5642010-12-21 18:51:27 -0800810 PreferenceManager.setDefaultValues(ctx, R.xml.general_preferences, true);
811 PreferenceManager.setDefaultValues(ctx, R.xml.privacy_security_preferences, true);
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500812 PreferenceManager.setDefaultValues(ctx, R.xml.advanced_preferences, true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700813 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700814 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100815 // reset appcache max size
816 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch23da30e2010-10-26 15:18:44 +0100817 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700818 }
819
John Reck33bbb802010-10-26 17:13:42 -0700820 /*package*/ static String getFactoryResetHomeUrl(Context context) {
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700821 String url = context.getResources().getString(R.string.homepage_base);
822 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500823 url = url.replace("{CID}",
824 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700825 }
826 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800827 }
828
The Android Open Source Project0c908882009-03-03 19:32:16 -0800829 // Private constructor that does nothing.
830 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800831 reset();
832 }
833
834 private void reset() {
835 // Private variables for settings
836 // NOTE: these defaults need to be kept in sync with the XML
837 // until the performance of PreferenceManager.setDefaultValues()
838 // is improved.
839 loadsImagesAutomatically = true;
840 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400841 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800842 javaScriptCanOpenWindowsAutomatically = false;
843 showSecurityWarnings = true;
844 rememberPasswords = true;
845 saveFormData = true;
Ben Murdochdc62cb92010-11-23 15:11:29 +0000846 autoFillEnabled = true;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800847 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800848 autoFitPage = true;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800849 loadsPageInOverviewMode = true;
850 showDebugSettings = false;
851 // HTML5 API flags
852 appCacheEnabled = true;
853 databaseEnabled = true;
854 domStorageEnabled = true;
855 geolocationEnabled = true;
856 workersEnabled = true; // only affects V8. JSC does not have a similar setting
Patrick Scott539e2ec2011-01-13 11:27:38 -0500857 // Autologin default is true. The account will be populated when
858 // reading from the DB as that is when a context is available.
859 autoLoginEnabled = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800860 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100861
Ben Murdoch23da30e2010-10-26 15:18:44 +0100862 private abstract class AutoFillProfileDbTask<T> extends AsyncTask<T, Void, Void> {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100863 Context mContext;
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100864 AutoFillProfileDatabase mAutoFillProfileDb;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100865 Message mCompleteMessage;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100866
Ben Murdoch23da30e2010-10-26 15:18:44 +0100867 public AutoFillProfileDbTask(Context ctx, Message msg) {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100868 mContext = ctx;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100869 mCompleteMessage = msg;
870 }
871
872 protected void onPostExecute(Void result) {
873 if (mCompleteMessage != null) {
874 mCompleteMessage.sendToTarget();
875 }
876 mAutoFillProfileDb.close();
877 }
878
879 abstract protected Void doInBackground(T... values);
880 }
881
882
883 private class SaveProfileToDbTask extends AutoFillProfileDbTask<AutoFillProfile> {
884 public SaveProfileToDbTask(Context ctx, Message msg) {
885 super(ctx, msg);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100886 }
887
888 protected Void doInBackground(AutoFillProfile... values) {
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100889 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100890 assert autoFillActiveProfileId != NO_AUTOFILL_PROFILE_SET;
891 AutoFillProfile newProfile = values[0];
892 mAutoFillProfileDb.addOrUpdateProfile(autoFillActiveProfileId, newProfile);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100893 return null;
894 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100895 }
896
Ben Murdoch23da30e2010-10-26 15:18:44 +0100897 private class DeleteProfileFromDbTask extends AutoFillProfileDbTask<Integer> {
898 public DeleteProfileFromDbTask(Context ctx, Message msg) {
899 super(ctx, msg);
900 }
901
902 protected Void doInBackground(Integer... values) {
903 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
904 int id = values[0];
905 assert id > 0;
906 mAutoFillProfileDb.dropProfile(id);
907 return null;
908 }
909 }
John Reck63bb6872010-12-01 19:29:32 -0800910
911 @Override
912 public void onSharedPreferenceChanged(
913 SharedPreferences p, String key) {
914 if (PREF_HARDWARE_ACCEL.equals(key)) {
915 hardwareAccelerated = p.getBoolean(PREF_HARDWARE_ACCEL, hardwareAccelerated);
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800916 } else if (PREF_VISUAL_INDICATOR.equals(key)) {
917 showVisualIndicator = p.getBoolean(PREF_VISUAL_INDICATOR, showVisualIndicator);
John Reck63bb6872010-12-01 19:29:32 -0800918 } else if (PREF_USER_AGENT.equals(key)) {
919 userAgent = Integer.parseInt(p.getString(PREF_USER_AGENT, "0"));
920 update();
Michael Kolb376b5412010-12-15 11:52:57 -0800921 } else if (PREF_QUICK_CONTROLS.equals(key)) {
922 quickControls = p.getBoolean(PREF_QUICK_CONTROLS, quickControls);
John Reckbafe58a2011-01-11 10:26:02 -0800923 } else if (PREF_MOST_VISITED_HOMEPAGE.equals(key)) {
924 useMostVisitedHomepage = p.getBoolean(PREF_MOST_VISITED_HOMEPAGE, useMostVisitedHomepage);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000925 } else if (PREF_USE_INSTANT.equals(key)) {
926 useInstant = p.getBoolean(PREF_USE_INSTANT, useInstant);
927 updateSearchEngine(mController.getActivity(), SearchEngine.GOOGLE, true);
928 } else if (PREF_SEARCH_ENGINE.equals(key)) {
929 final String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
930 SearchEngine.GOOGLE);
931 updateSearchEngine(mController.getActivity(), searchEngineName, false);
John Reck63bb6872010-12-01 19:29:32 -0800932 }
933 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800934}