blob: f3bc48a7777ba7b7b30cbb65e22f2a4360f337f6 [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;
Michael Kolb376b5412010-12-15 11:52:57 -0800116
Ben Murdochbff2d602009-07-01 20:19:05 +0100117 // By default the error console is shown once the user navigates to about:debug.
118 // The setting can be then toggled from the settings menu.
119 private boolean showConsole = true;
120
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121 // Private preconfigured values
Shimeng (Simon) Wangb4fb25c2010-07-13 15:18:10 -0700122 private static int minimumFontSize = 1;
123 private static int minimumLogicalFontSize = 1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124 private static int defaultFontSize = 16;
125 private static int defaultFixedFontSize = 13;
126 private static WebSettings.TextSize textSize =
127 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700128 private static WebSettings.ZoomDensity zoomDensity =
129 WebSettings.ZoomDensity.MEDIUM;
Grace Kloba9804c432009-12-02 11:07:40 -0800130 private static int pageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800131
Ben Murdoch0cb81892010-10-08 12:41:33 +0100132
Ben Murdoch23da30e2010-10-26 15:18:44 +0100133 private AutoFillProfile autoFillProfile;
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100134 // Default to zero. In the case no profile is set up, the initial
135 // value will come from the AutoFillSettingsFragment when the user
136 // creates a profile. Otherwise, we'll read the ID of the last used
137 // profile from the prefs db.
138 private int autoFillActiveProfileId;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100139 private static final int NO_AUTOFILL_PROFILE_SET = 0;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100140
The Android Open Source Project0c908882009-03-03 19:32:16 -0800141 // Preference keys that are used outside this class
142 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
143 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
144 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
145 public final static String PREF_HOMEPAGE = "homepage";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100146 public final static String PREF_SEARCH_ENGINE = "search_engine";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800147 public final static String PREF_CLEAR_FORM_DATA =
148 "privacy_clear_form_data";
149 public final static String PREF_CLEAR_PASSWORDS =
150 "privacy_clear_passwords";
151 public final static String PREF_EXTRAS_RESET_DEFAULTS =
152 "reset_default_preferences";
153 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100154 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800155 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700156 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800157 public final static String PREF_DEFAULT_TEXT_ENCODING =
158 "default_text_encoding";
Steve Blockc6f577f2009-08-20 18:11:08 +0100159 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
160 "privacy_clear_geolocation_access";
Ben Murdochaf554522010-09-10 22:09:30 +0100161 public final static String PREF_AUTOFILL_ENABLED = "autofill_enabled";
162 public final static String PREF_AUTOFILL_PROFILE = "autofill_profile";
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100163 public final static String PREF_AUTOFILL_ACTIVE_PROFILE_ID = "autofill_active_profile_id";
John Reck63bb6872010-12-01 19:29:32 -0800164 public final static String PREF_HARDWARE_ACCEL = "enable_hardware_accel";
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800165 public final static String PREF_VISUAL_INDICATOR = "enable_visual_indicator";
John Reck63bb6872010-12-01 19:29:32 -0800166 public final static String PREF_USER_AGENT = "user_agent";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800167
Michael Kolb376b5412010-12-15 11:52:57 -0800168 public final static String PREF_QUICK_CONTROLS = "enable_quick_controls";
John Reckbafe58a2011-01-11 10:26:02 -0800169 public final static String PREF_MOST_VISITED_HOMEPAGE = "use_most_visited_homepage";
Patrick Scott539e2ec2011-01-13 11:27:38 -0500170 public final static String PREF_AUTOLOGIN = "enable_autologin";
171 public final static String PREF_AUTOLOGIN_ACCOUNT = "autologin_account";
John Reck282431b2011-01-20 17:38:37 -0800172 public final static String PREF_PLUGIN_STATE = "plugin_state";
Michael Kolb376b5412010-12-15 11:52:57 -0800173
The Android Open Source Project0c908882009-03-03 19:32:16 -0800174 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700175 "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
176 "like Gecko) Version/5.0 Safari/533.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800177
178 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700179 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
180 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
181
182 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
183 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
184 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
185
186 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
187 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
188 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800189
190 // Value to truncate strings when adding them to a TextView within
191 // a ListView
192 public final static int MAX_TEXTVIEW_LEN = 80;
193
Jeff Davidson43610292010-07-16 16:03:58 -0700194 public static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider";
195
196 public static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/");
197
John Reck63bb6872010-12-01 19:29:32 -0800198 // Set to true to enable some of the about:debug options
John Reck9eed0ef2011-01-11 17:11:37 -0800199 public static final boolean DEV_BUILD = false;
John Reck63bb6872010-12-01 19:29:32 -0800200
Michael Kolb8233fac2010-10-26 16:08:53 -0700201 private Controller mController;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800202
203 // Single instance of the BrowserSettings for use in the Browser app.
204 private static BrowserSettings sSingleton;
205
206 // Private map of WebSettings to Observer objects used when deleting an
207 // observer.
208 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
209 new HashMap<WebSettings,Observer>();
210
Ben Murdochef671652010-11-25 17:17:58 +0000211 private boolean mLoadFromDbComplete;
212
213 public void waitForLoadFromDbToComplete() {
214 synchronized (sSingleton) {
215 while (!mLoadFromDbComplete) {
216 try {
217 sSingleton.wait();
218 } catch (InterruptedException e) { }
219 }
220 }
221 }
222
The Android Open Source Project0c908882009-03-03 19:32:16 -0800223 /*
224 * An observer wrapper for updating a WebSettings object with the new
225 * settings after a call to BrowserSettings.update().
226 */
227 static class Observer implements java.util.Observer {
228 // Private WebSettings object that will be updated.
229 private WebSettings mSettings;
230
231 Observer(WebSettings w) {
232 mSettings = w;
233 }
234
235 public void update(Observable o, Object arg) {
236 BrowserSettings b = (BrowserSettings)o;
237 WebSettings s = mSettings;
238
239 s.setLayoutAlgorithm(b.layoutAlgorithm);
240 if (b.userAgent == 0) {
241 // use the default ua string
242 s.setUserAgentString(null);
243 } else if (b.userAgent == 1) {
244 s.setUserAgentString(DESKTOP_USERAGENT);
245 } else if (b.userAgent == 2) {
246 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700247 } else if (b.userAgent == 3) {
248 s.setUserAgentString(IPAD_USERAGENT);
249 } else if (b.userAgent == 4) {
250 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800251 }
252 s.setUseWideViewPort(b.useWideViewPort);
253 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
254 s.setJavaScriptEnabled(b.javaScriptEnabled);
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800255 s.setShowVisualIndicator(b.showVisualIndicator);
Patrick Scotte536b332010-03-22 10:19:32 -0400256 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800257 s.setJavaScriptCanOpenWindowsAutomatically(
258 b.javaScriptCanOpenWindowsAutomatically);
259 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
260 s.setMinimumFontSize(b.minimumFontSize);
261 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
262 s.setDefaultFontSize(b.defaultFontSize);
263 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
264 s.setNavDump(b.navDump);
265 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700266 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800267 s.setLightTouchEnabled(b.lightTouch);
268 s.setSaveFormData(b.saveFormData);
Ben Murdochce549fc2010-09-09 10:28:08 +0100269 s.setAutoFillEnabled(b.autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800270 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700271 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800272 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800273
274 // WebView inside Browser doesn't want initial focus to be set.
275 s.setNeedInitialFocus(false);
276 // Browser supports multiple windows
277 s.setSupportMultipleWindows(true);
Grace Kloba61fc77c2010-06-22 09:57:33 -0700278 // enable smooth transition for better performance during panning or
279 // zooming
280 s.setEnableSmoothTransition(true);
Patrick Scottb92bbb42011-01-05 11:38:58 -0500281 // disable content url access
282 s.setAllowContentAccess(false);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100283
Andrei Popescu01068702009-08-03 16:03:24 +0100284 // HTML5 API flags
285 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100286 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100287 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100288 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100289 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100290
Andrei Popescu01068702009-08-03 16:03:24 +0100291 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100292 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100293 s.setAppCachePath(b.appCachePath);
294 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100295 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100296
Ben Murdoch0cb81892010-10-08 12:41:33 +0100297 // Active AutoFill profile data.
Ben Murdoch23da30e2010-10-26 15:18:44 +0100298 s.setAutoFillProfile(b.autoFillProfile);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100299
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800300 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800301 }
302 }
303
304 /**
Ben Murdochef671652010-11-25 17:17:58 +0000305 * Load settings from the browser app's database. It is performed in
306 * an AsyncTask as it involves plenty of slow disk IO.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800307 * NOTE: Strings used for the preferences must match those specified
Jeff Hamilton175ab302010-10-04 12:53:49 -0500308 * in the various preference XML files.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800309 * @param ctx A Context object used to query the browser's settings
310 * database. If the database exists, the saved settings will be
311 * stored in this BrowserSettings object. This will update all
312 * observers of this object.
313 */
Ben Murdochef671652010-11-25 17:17:58 +0000314 public void asyncLoadFromDb(final Context ctx) {
315 mLoadFromDbComplete = false;
316 // Run the initial settings load in an AsyncTask as it hits the
317 // disk multiple times through SharedPreferences and SQLite. We
318 // need to be certain though that this has completed before we start
319 // to load pages though, so in the worst case we will block waiting
320 // for it to finish in BrowserActivity.onCreate().
321 new LoadFromDbTask(ctx).execute();
322 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800323
Ben Murdochef671652010-11-25 17:17:58 +0000324 private class LoadFromDbTask extends AsyncTask<Void, Void, Void> {
325 private Context mContext;
326
327 public LoadFromDbTask(Context context) {
328 mContext = context;
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700329 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700330
Ben Murdochef671652010-11-25 17:17:58 +0000331 protected Void doInBackground(Void... unused) {
332 SharedPreferences p =
333 PreferenceManager.getDefaultSharedPreferences(mContext);
334 // Set the default value for the Application Caches path.
335 appCachePath = mContext.getDir("appcache", 0).getPath();
336 // Determine the maximum size of the application cache.
337 webStorageSizeManager = new WebStorageSizeManager(
338 mContext,
339 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
340 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
341 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
342 // Set the default value for the Database path.
343 databasePath = mContext.getDir("databases", 0).getPath();
344 // Set the default value for the Geolocation database path.
345 geolocationDatabasePath = mContext.getDir("geolocation", 0).getPath();
346
John Reckef074262010-12-02 16:09:14 -0800347 if (p.getString(PREF_HOMEPAGE, null) == null) {
Ben Murdochef671652010-11-25 17:17:58 +0000348 // No home page preferences is set, set it to default.
349 setHomePage(mContext, getFactoryResetHomeUrl(mContext));
350 }
351
352 // the cost of one cached page is ~3M (measured using nytimes.com). For
353 // low end devices, we only cache one page. For high end devices, we try
354 // to cache more pages, currently choose 5.
355 ActivityManager am = (ActivityManager) mContext
356 .getSystemService(Context.ACTIVITY_SERVICE);
357 if (am.getMemoryClass() > 16) {
358 pageCacheCapacity = 5;
359 } else {
360 pageCacheCapacity = 1;
361 }
362
363 // Read the last active AutoFill profile id.
364 autoFillActiveProfileId = p.getInt(
365 PREF_AUTOFILL_ACTIVE_PROFILE_ID, autoFillActiveProfileId);
366
367 // Load the autofill profile data from the database. We use a database separate
368 // to the browser preference DB to make it easier to support multiple profiles
369 // and switching between them.
370 AutoFillProfileDatabase autoFillDb = AutoFillProfileDatabase.getInstance(mContext);
371 Cursor c = autoFillDb.getProfile(autoFillActiveProfileId);
372
373 if (c.getCount() > 0) {
374 c.moveToFirst();
375
376 String fullName = c.getString(c.getColumnIndex(
377 AutoFillProfileDatabase.Profiles.FULL_NAME));
378 String email = c.getString(c.getColumnIndex(
379 AutoFillProfileDatabase.Profiles.EMAIL_ADDRESS));
380 String company = c.getString(c.getColumnIndex(
381 AutoFillProfileDatabase.Profiles.COMPANY_NAME));
382 String addressLine1 = c.getString(c.getColumnIndex(
383 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_1));
384 String addressLine2 = c.getString(c.getColumnIndex(
385 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_2));
386 String city = c.getString(c.getColumnIndex(
387 AutoFillProfileDatabase.Profiles.CITY));
388 String state = c.getString(c.getColumnIndex(
389 AutoFillProfileDatabase.Profiles.STATE));
390 String zip = c.getString(c.getColumnIndex(
391 AutoFillProfileDatabase.Profiles.ZIP_CODE));
392 String country = c.getString(c.getColumnIndex(
393 AutoFillProfileDatabase.Profiles.COUNTRY));
394 String phone = c.getString(c.getColumnIndex(
395 AutoFillProfileDatabase.Profiles.PHONE_NUMBER));
396 autoFillProfile = new AutoFillProfile(autoFillActiveProfileId,
397 fullName, email, company, addressLine1, addressLine2, city,
398 state, zip, country, phone);
399 }
400 c.close();
401 autoFillDb.close();
402
403 // PreferenceManager.setDefaultValues is TOO SLOW, need to manually keep
404 // the defaults in sync
John Reck63bb6872010-12-01 19:29:32 -0800405 p.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
Ben Murdochef671652010-11-25 17:17:58 +0000406 syncSharedPreferences(mContext, p);
407
408 synchronized (sSingleton) {
409 mLoadFromDbComplete = true;
410 sSingleton.notify();
411 }
412 return null;
Grace Kloba9804c432009-12-02 11:07:40 -0800413 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800414 }
415
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100416 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700417
The Android Open Source Project0c908882009-03-03 19:32:16 -0800418 homeUrl =
419 p.getString(PREF_HOMEPAGE, homeUrl);
Leon Scroggins III31306602010-09-17 11:10:29 -0400420 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
421 SearchEngine.GOOGLE);
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100422 if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
423 if (searchEngine != null) {
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400424 if (searchEngine.supportsVoiceSearch()) {
425 // One or more tabs could have been in voice search mode.
426 // Clear it, since the new SearchEngine may not support
427 // it, or may handle it differently.
Michael Kolb8233fac2010-10-26 16:08:53 -0700428 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
429 mController.getTabControl().getTab(i).revertVoiceSearchMode();
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400430 }
431 }
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100432 searchEngine.close();
433 }
434 searchEngine = SearchEngines.get(ctx, searchEngineName);
435 }
436 Log.i(TAG, "Selected search engine: " + searchEngine);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700437
The Android Open Source Project0c908882009-03-03 19:32:16 -0800438 loadsImagesAutomatically = p.getBoolean("load_images",
439 loadsImagesAutomatically);
440 javaScriptEnabled = p.getBoolean("enable_javascript",
441 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400442 pluginState = WebSettings.PluginState.valueOf(
John Reck282431b2011-01-20 17:38:37 -0800443 p.getString(PREF_PLUGIN_STATE, pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800444 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
445 "block_popup_windows",
446 !javaScriptCanOpenWindowsAutomatically);
447 showSecurityWarnings = p.getBoolean("show_security_warnings",
448 showSecurityWarnings);
449 rememberPasswords = p.getBoolean("remember_passwords",
450 rememberPasswords);
451 saveFormData = p.getBoolean("save_formdata",
452 saveFormData);
Ben Murdochaf554522010-09-10 22:09:30 +0100453 autoFillEnabled = p.getBoolean("autofill_enabled", autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800454 boolean accept_cookies = p.getBoolean("accept_cookies",
455 CookieManager.getInstance().acceptCookie());
456 CookieManager.getInstance().setAcceptCookie(accept_cookies);
457 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800458 textSize = WebSettings.TextSize.valueOf(
459 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700460 zoomDensity = WebSettings.ZoomDensity.valueOf(
461 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800462 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700463 loadsPageInOverviewMode = p.getBoolean("load_page",
464 loadsPageInOverviewMode);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800465 useWideViewPort = true; // use wide view port for either setting
466 if (autoFitPage) {
467 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
468 } else {
469 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
470 }
471 defaultTextEncodingName =
472 p.getString(PREF_DEFAULT_TEXT_ENCODING,
473 defaultTextEncodingName);
474
475 showDebugSettings =
476 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
477 // Debug menu items have precidence if the menu is visible
478 if (showDebugSettings) {
479 boolean small_screen = p.getBoolean("small_screen",
480 layoutAlgorithm ==
481 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
482 if (small_screen) {
483 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
484 } else {
485 boolean normal_layout = p.getBoolean("normal_layout",
486 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
487 if (normal_layout) {
488 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
489 } else {
490 layoutAlgorithm =
491 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
492 }
493 }
494 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
495 tracing = p.getBoolean("enable_tracing", tracing);
496 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
497 navDump = p.getBoolean("enable_nav_dump", navDump);
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800498 showVisualIndicator = p.getBoolean(PREF_VISUAL_INDICATOR, showVisualIndicator);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800499 }
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500500
Michael Kolb376b5412010-12-15 11:52:57 -0800501 quickControls = p.getBoolean(PREF_QUICK_CONTROLS, quickControls);
John Reckbafe58a2011-01-11 10:26:02 -0800502 useMostVisitedHomepage = p.getBoolean(PREF_MOST_VISITED_HOMEPAGE, useMostVisitedHomepage);
Michael Kolb376b5412010-12-15 11:52:57 -0800503
John Reck3db2e072011-01-24 14:57:18 -0800504 // Only set these if this is a dev build or debug is enabled
505 if (DEV_BUILD || showDebugSettings()) {
John Reck63bb6872010-12-01 19:29:32 -0800506 userAgent = Integer.parseInt(p.getString(PREF_USER_AGENT, "0"));
507 hardwareAccelerated = p.getBoolean(PREF_HARDWARE_ACCEL, hardwareAccelerated);
508 }
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500509
Feng Qianb3c02da2009-06-29 15:58:08 -0700510 // JS flags is loaded from DB even if showDebugSettings is false,
511 // so that it can be set once and be effective all the time.
512 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100513
514 // Read the setting for showing/hiding the JS Console always so that should the
515 // user enable debug settings, we already know if we should show the console.
516 // The user will never see the console unless they navigate to about:debug,
517 // regardless of the setting we read here. This setting is only used after debug
518 // is enabled.
519 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700520
Andrei Popescu01068702009-08-03 16:03:24 +0100521 // HTML5 API flags
522 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
523 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
524 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100525 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100526 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100527
Patrick Scott539e2ec2011-01-13 11:27:38 -0500528 // Autologin account settings. The account preference may be null until
529 // the user explicitly changes the account in the settings.
530 autoLoginEnabled = p.getBoolean(PREF_AUTOLOGIN, autoLoginEnabled);
531 autoLoginAccount = p.getString(PREF_AUTOLOGIN_ACCOUNT, autoLoginAccount);
532
Grace Klobad9ee1392009-07-20 11:53:33 -0700533 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800534 }
535
The Android Open Source Project0c908882009-03-03 19:32:16 -0800536 public String getHomePage() {
John Reckbafe58a2011-01-11 10:26:02 -0800537 if (useMostVisitedHomepage) {
538 return HomeProvider.MOST_VISITED;
539 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800540 return homeUrl;
541 }
542
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100543 public SearchEngine getSearchEngine() {
544 return searchEngine;
545 }
546
Feng Qianb3c02da2009-06-29 15:58:08 -0700547 public String getJsFlags() {
548 return jsFlags;
549 }
550
Andrei Popescu79e82b72009-07-27 12:01:59 +0100551 public WebStorageSizeManager getWebStorageSizeManager() {
552 return webStorageSizeManager;
553 }
554
The Android Open Source Project0c908882009-03-03 19:32:16 -0800555 public void setHomePage(Context context, String url) {
556 Editor ed = PreferenceManager.
557 getDefaultSharedPreferences(context).edit();
558 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700559 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800560 homeUrl = url;
561 }
562
The Android Open Source Project0c908882009-03-03 19:32:16 -0800563 public WebSettings.TextSize getTextSize() {
564 return textSize;
565 }
566
Grace Kloba2f830682009-06-25 11:08:53 -0700567 public WebSettings.ZoomDensity getDefaultZoom() {
568 return zoomDensity;
569 }
570
The Android Open Source Project0c908882009-03-03 19:32:16 -0800571 public boolean openInBackground() {
572 return openInBackground;
573 }
574
575 public boolean showSecurityWarnings() {
576 return showSecurityWarnings;
577 }
578
579 public boolean isTracing() {
580 return tracing;
581 }
582
583 public boolean isLightTouch() {
584 return lightTouch;
585 }
586
587 public boolean isNavDump() {
588 return navDump;
589 }
590
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500591 public boolean isHardwareAccelerated() {
592 return hardwareAccelerated;
593 }
594
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800595 public boolean showVisualIndicator() {
596 return showVisualIndicator;
597 }
598
Michael Kolb376b5412010-12-15 11:52:57 -0800599 public boolean useQuickControls() {
600 return quickControls;
601 }
602
John Reckbafe58a2011-01-11 10:26:02 -0800603 public boolean useMostVisitedHomepage() {
604 return useMostVisitedHomepage;
605 }
606
The Android Open Source Project0c908882009-03-03 19:32:16 -0800607 public boolean showDebugSettings() {
608 return showDebugSettings;
609 }
610
John Reck3db2e072011-01-24 14:57:18 -0800611 public void toggleDebugSettings(Context context) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800612 showDebugSettings = !showDebugSettings;
613 navDump = showDebugSettings;
John Reck3db2e072011-01-24 14:57:18 -0800614 syncSharedPreferences(context,
615 PreferenceManager.getDefaultSharedPreferences(context));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800616 update();
617 }
618
Patrick Scott539e2ec2011-01-13 11:27:38 -0500619 public boolean isAutoLoginEnabled() {
620 return autoLoginEnabled;
621 }
622
623 public String getAutoLoginAccount(Context context) {
624 // Each time we attempt to get the account, we need to verify that the
625 // account is still valid.
626 return GoogleAccountLogin.validateAccount(context, autoLoginAccount);
627 }
628
629 public void setAutoLoginAccount(Context context, String name) {
630 Editor ed = PreferenceManager.
631 getDefaultSharedPreferences(context).edit();
632 ed.putString(PREF_AUTOLOGIN_ACCOUNT, name);
633 ed.apply();
634 autoLoginAccount = name;
635 }
636
John Reck812d2d62011-01-18 14:16:15 -0800637 public void setAutoLoginEnabled(Context context, boolean enable) {
638 Editor ed = PreferenceManager.
639 getDefaultSharedPreferences(context).edit();
640 ed.putBoolean(PREF_AUTOLOGIN, enable);
641 ed.apply();
642 autoLoginEnabled = enable;
643 }
644
Ben Murdoch23da30e2010-10-26 15:18:44 +0100645 public void setAutoFillProfile(Context ctx, AutoFillProfile profile, Message msg) {
646 if (profile != null) {
647 setActiveAutoFillProfileId(ctx, profile.getUniqueId());
648 // Update the AutoFill DB with the new profile.
649 new SaveProfileToDbTask(ctx, msg).execute(profile);
650 } else {
651 // Delete the current profile.
Ben Murdoche8a28332010-11-17 14:16:21 +0000652 if (autoFillProfile != null) {
653 new DeleteProfileFromDbTask(ctx, msg).execute(autoFillProfile.getUniqueId());
654 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
655 }
Ben Murdoch23da30e2010-10-26 15:18:44 +0100656 }
657 autoFillProfile = profile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100658 }
659
660 public AutoFillProfile getAutoFillProfile() {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100661 return autoFillProfile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100662 }
663
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100664 private void setActiveAutoFillProfileId(Context context, int activeProfileId) {
665 autoFillActiveProfileId = activeProfileId;
666 Editor ed = PreferenceManager.
667 getDefaultSharedPreferences(context).edit();
668 ed.putInt(PREF_AUTOFILL_ACTIVE_PROFILE_ID, activeProfileId);
669 ed.apply();
670 }
671
Ben Murdoch8029a772010-11-16 11:58:21 +0000672 /* package */ void disableAutoFill(Context ctx) {
673 autoFillEnabled = false;
674 Editor ed = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
675 ed.putBoolean(PREF_AUTOFILL_ENABLED, false);
676 ed.apply();
677 }
678
The Android Open Source Project0c908882009-03-03 19:32:16 -0800679 /**
680 * Add a WebSettings object to the list of observers that will be updated
681 * when update() is called.
682 *
683 * @param s A WebSettings object that is strictly tied to the life of a
684 * WebView.
685 */
686 public Observer addObserver(WebSettings s) {
687 Observer old = mWebSettingsToObservers.get(s);
688 if (old != null) {
689 super.deleteObserver(old);
690 }
691 Observer o = new Observer(s);
692 mWebSettingsToObservers.put(s, o);
693 super.addObserver(o);
694 return o;
695 }
696
697 /**
698 * Delete the given WebSettings observer from the list of observers.
699 * @param s The WebSettings object to be deleted.
700 */
701 public void deleteObserver(WebSettings s) {
702 Observer o = mWebSettingsToObservers.get(s);
703 if (o != null) {
704 mWebSettingsToObservers.remove(s);
705 super.deleteObserver(o);
706 }
707 }
708
709 /*
John Reck63bb6872010-12-01 19:29:32 -0800710 * Application level method for obtaining a single app instance of the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800711 * BrowserSettings.
712 */
John Reck63bb6872010-12-01 19:29:32 -0800713 public static BrowserSettings getInstance() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800714 if (sSingleton == null ) {
715 sSingleton = new BrowserSettings();
716 }
717 return sSingleton;
718 }
719
720 /*
721 * Package level method for associating the BrowserSettings with TabControl
722 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700723 /* package */void setController(Controller ctrl) {
724 mController = ctrl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800725 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800726 }
727
728 /*
729 * Update all the observers of the object.
730 */
731 /*package*/ void update() {
732 setChanged();
733 notifyObservers();
734 }
735
736 /*package*/ void clearCache(Context context) {
737 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700738 if (mController != null) {
739 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800740 if (current != null) {
741 current.clearCache(true);
742 }
743 }
744 }
745
746 /*package*/ void clearCookies(Context context) {
747 CookieManager.getInstance().removeAllCookie();
748 }
749
750 /* package */void clearHistory(Context context) {
751 ContentResolver resolver = context.getContentResolver();
752 Browser.clearHistory(resolver);
753 Browser.clearSearches(resolver);
754 }
755
756 /* package */ void clearFormData(Context context) {
757 WebViewDatabase.getInstance(context).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700758 if (mController!= null) {
759 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100760 if (currentTopView != null) {
761 currentTopView.clearFormData();
762 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800763 }
764 }
765
766 /*package*/ void clearPasswords(Context context) {
767 WebViewDatabase db = WebViewDatabase.getInstance(context);
768 db.clearUsernamePassword();
769 db.clearHttpAuthUsernamePassword();
770 }
771
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800772 private void updateTabControlSettings() {
773 // Enable/disable the error console.
Michael Kolb8233fac2010-10-26 16:08:53 -0700774 mController.setShouldShowErrorConsole(
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800775 showDebugSettings && showConsole);
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800776 }
777
Nicolas Roard78a98e42009-05-11 13:34:17 +0100778 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100779 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100780 }
781
782 /*package*/ void clearLocationAccess(Context context) {
783 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100784 }
785
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400786 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800787 reset();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500788 SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700789 p.edit().clear().apply();
John Reck035a5642010-12-21 18:51:27 -0800790 PreferenceManager.setDefaultValues(ctx, R.xml.general_preferences, true);
791 PreferenceManager.setDefaultValues(ctx, R.xml.privacy_security_preferences, true);
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500792 PreferenceManager.setDefaultValues(ctx, R.xml.advanced_preferences, true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700793 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700794 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100795 // reset appcache max size
796 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch23da30e2010-10-26 15:18:44 +0100797 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700798 }
799
John Reck33bbb802010-10-26 17:13:42 -0700800 /*package*/ static String getFactoryResetHomeUrl(Context context) {
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700801 String url = context.getResources().getString(R.string.homepage_base);
802 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500803 url = url.replace("{CID}",
804 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700805 }
806 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800807 }
808
The Android Open Source Project0c908882009-03-03 19:32:16 -0800809 // Private constructor that does nothing.
810 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800811 reset();
812 }
813
814 private void reset() {
815 // Private variables for settings
816 // NOTE: these defaults need to be kept in sync with the XML
817 // until the performance of PreferenceManager.setDefaultValues()
818 // is improved.
819 loadsImagesAutomatically = true;
820 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400821 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800822 javaScriptCanOpenWindowsAutomatically = false;
823 showSecurityWarnings = true;
824 rememberPasswords = true;
825 saveFormData = true;
Ben Murdochdc62cb92010-11-23 15:11:29 +0000826 autoFillEnabled = true;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800827 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800828 autoFitPage = true;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800829 loadsPageInOverviewMode = true;
830 showDebugSettings = false;
831 // HTML5 API flags
832 appCacheEnabled = true;
833 databaseEnabled = true;
834 domStorageEnabled = true;
835 geolocationEnabled = true;
836 workersEnabled = true; // only affects V8. JSC does not have a similar setting
Patrick Scott539e2ec2011-01-13 11:27:38 -0500837 // Autologin default is true. The account will be populated when
838 // reading from the DB as that is when a context is available.
839 autoLoginEnabled = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800840 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100841
Ben Murdoch23da30e2010-10-26 15:18:44 +0100842 private abstract class AutoFillProfileDbTask<T> extends AsyncTask<T, Void, Void> {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100843 Context mContext;
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100844 AutoFillProfileDatabase mAutoFillProfileDb;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100845 Message mCompleteMessage;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100846
Ben Murdoch23da30e2010-10-26 15:18:44 +0100847 public AutoFillProfileDbTask(Context ctx, Message msg) {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100848 mContext = ctx;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100849 mCompleteMessage = msg;
850 }
851
852 protected void onPostExecute(Void result) {
853 if (mCompleteMessage != null) {
854 mCompleteMessage.sendToTarget();
855 }
856 mAutoFillProfileDb.close();
857 }
858
859 abstract protected Void doInBackground(T... values);
860 }
861
862
863 private class SaveProfileToDbTask extends AutoFillProfileDbTask<AutoFillProfile> {
864 public SaveProfileToDbTask(Context ctx, Message msg) {
865 super(ctx, msg);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100866 }
867
868 protected Void doInBackground(AutoFillProfile... values) {
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100869 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100870 assert autoFillActiveProfileId != NO_AUTOFILL_PROFILE_SET;
871 AutoFillProfile newProfile = values[0];
872 mAutoFillProfileDb.addOrUpdateProfile(autoFillActiveProfileId, newProfile);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100873 return null;
874 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100875 }
876
Ben Murdoch23da30e2010-10-26 15:18:44 +0100877 private class DeleteProfileFromDbTask extends AutoFillProfileDbTask<Integer> {
878 public DeleteProfileFromDbTask(Context ctx, Message msg) {
879 super(ctx, msg);
880 }
881
882 protected Void doInBackground(Integer... values) {
883 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
884 int id = values[0];
885 assert id > 0;
886 mAutoFillProfileDb.dropProfile(id);
887 return null;
888 }
889 }
John Reck63bb6872010-12-01 19:29:32 -0800890
891 @Override
892 public void onSharedPreferenceChanged(
893 SharedPreferences p, String key) {
894 if (PREF_HARDWARE_ACCEL.equals(key)) {
895 hardwareAccelerated = p.getBoolean(PREF_HARDWARE_ACCEL, hardwareAccelerated);
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800896 } else if (PREF_VISUAL_INDICATOR.equals(key)) {
897 showVisualIndicator = p.getBoolean(PREF_VISUAL_INDICATOR, showVisualIndicator);
John Reck63bb6872010-12-01 19:29:32 -0800898 } else if (PREF_USER_AGENT.equals(key)) {
899 userAgent = Integer.parseInt(p.getString(PREF_USER_AGENT, "0"));
900 update();
Michael Kolb376b5412010-12-15 11:52:57 -0800901 } else if (PREF_QUICK_CONTROLS.equals(key)) {
902 quickControls = p.getBoolean(PREF_QUICK_CONTROLS, quickControls);
John Reckbafe58a2011-01-11 10:26:02 -0800903 } else if (PREF_MOST_VISITED_HOMEPAGE.equals(key)) {
904 useMostVisitedHomepage = p.getBoolean(PREF_MOST_VISITED_HOMEPAGE, useMostVisitedHomepage);
John Reck63bb6872010-12-01 19:29:32 -0800905 }
906 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800907}