blob: b79153190db65651ce4271004738286477cefb9a [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 Reck40badf22011-04-20 15:31:13 -070062// TODO: Really need to refactor this :/
John Reck63bb6872010-12-01 19:29:32 -080063public class BrowserSettings extends Observable implements OnSharedPreferenceChangeListener {
Leon Scroggins9ac587d2009-04-20 12:53:46 -040064 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080065 // NOTE: these defaults need to be kept in sync with the XML
66 // until the performance of PreferenceManager.setDefaultValues()
67 // is improved.
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080068 // Note: boolean variables are set inside reset function.
69 private boolean loadsImagesAutomatically;
70 private boolean javaScriptEnabled;
Patrick Scotte536b332010-03-22 10:19:32 -040071 private WebSettings.PluginState pluginState;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080072 private boolean javaScriptCanOpenWindowsAutomatically;
73 private boolean showSecurityWarnings;
74 private boolean rememberPasswords;
75 private boolean saveFormData;
Ben Murdochce549fc2010-09-09 10:28:08 +010076 private boolean autoFillEnabled;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080077 private boolean openInBackground;
The Android Open Source Project0c908882009-03-03 19:32:16 -080078 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070079 private String homeUrl = "";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010080 private SearchEngine searchEngine;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080081 private boolean autoFitPage;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080082 private boolean loadsPageInOverviewMode;
83 private boolean showDebugSettings;
Andrei Popescu01068702009-08-03 16:03:24 +010084 // HTML5 API flags
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080085 private boolean appCacheEnabled;
86 private boolean databaseEnabled;
87 private boolean domStorageEnabled;
88 private boolean geolocationEnabled;
89 private boolean workersEnabled; // only affects V8. JSC does not have a similar setting
Andrei Popescu01068702009-08-03 16:03:24 +010090 // HTML5 API configuration params
91 private long appCacheMaxSize = Long.MAX_VALUE;
92 private String appCachePath; // default value set in loadFromDb().
93 private String databasePath; // default value set in loadFromDb()
Steve Block5029cf02009-08-21 13:16:58 +010094 private String geolocationDatabasePath; // default value set in loadFromDb()
Andrei Popescu01068702009-08-03 16:03:24 +010095 private WebStorageSizeManager webStorageSizeManager;
John Reck40badf22011-04-20 15:31:13 -070096 // Accessibility settings
97 private int minimumFontSize = 1;
98 private WebSettings.TextSize textSize = WebSettings.TextSize.NORMAL;
Andrei Popescu01068702009-08-03 16:03:24 +010099
100 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800101
Nicolas Roard78a98e42009-05-11 13:34:17 +0100102 private final static String TAG = "BrowserSettings";
103
The Android Open Source Project0c908882009-03-03 19:32:16 -0800104 // Development settings
105 public WebSettings.LayoutAlgorithm layoutAlgorithm =
106 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
107 private boolean useWideViewPort = true;
108 private int userAgent = 0;
109 private boolean tracing = false;
110 private boolean lightTouch = false;
111 private boolean navDump = false;
Derek Sollenberger8de82852010-11-18 19:51:50 -0500112 private boolean hardwareAccelerated = true;
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800113 private boolean showVisualIndicator = false;
Michael Kolb376b5412010-12-15 11:52:57 -0800114 // Lab settings
115 private boolean quickControls = false;
John Reckbafe58a2011-01-11 10:26:02 -0800116 private boolean useMostVisitedHomepage = false;
Narayan Kamath5119edd2011-02-23 15:49:17 +0000117 private boolean useInstant = false;
Michael Kolb376b5412010-12-15 11:52:57 -0800118
Ben Murdochbff2d602009-07-01 20:19:05 +0100119 // By default the error console is shown once the user navigates to about:debug.
120 // The setting can be then toggled from the settings menu.
121 private boolean showConsole = true;
122
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123 // Private preconfigured values
Shimeng (Simon) Wangb4fb25c2010-07-13 15:18:10 -0700124 private static int minimumLogicalFontSize = 1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800125 private static int defaultFontSize = 16;
126 private static int defaultFixedFontSize = 13;
Grace Kloba2f830682009-06-25 11:08:53 -0700127 private static WebSettings.ZoomDensity zoomDensity =
128 WebSettings.ZoomDensity.MEDIUM;
Grace Kloba9804c432009-12-02 11:07:40 -0800129 private static int pageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800130
Ben Murdoch0cb81892010-10-08 12:41:33 +0100131
Ben Murdoch23da30e2010-10-26 15:18:44 +0100132 private AutoFillProfile autoFillProfile;
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100133 // Default to zero. In the case no profile is set up, the initial
134 // value will come from the AutoFillSettingsFragment when the user
135 // creates a profile. Otherwise, we'll read the ID of the last used
136 // profile from the prefs db.
137 private int autoFillActiveProfileId;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100138 private static final int NO_AUTOFILL_PROFILE_SET = 0;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100139
The Android Open Source Project0c908882009-03-03 19:32:16 -0800140 // Preference keys that are used outside this class
141 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
142 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
143 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
144 public final static String PREF_HOMEPAGE = "homepage";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100145 public final static String PREF_SEARCH_ENGINE = "search_engine";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 public final static String PREF_CLEAR_FORM_DATA =
147 "privacy_clear_form_data";
148 public final static String PREF_CLEAR_PASSWORDS =
149 "privacy_clear_passwords";
150 public final static String PREF_EXTRAS_RESET_DEFAULTS =
151 "reset_default_preferences";
152 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100153 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800154 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700155 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800156 public final static String PREF_DEFAULT_TEXT_ENCODING =
157 "default_text_encoding";
Steve Blockc6f577f2009-08-20 18:11:08 +0100158 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
159 "privacy_clear_geolocation_access";
Ben Murdochaf554522010-09-10 22:09:30 +0100160 public final static String PREF_AUTOFILL_ENABLED = "autofill_enabled";
161 public final static String PREF_AUTOFILL_PROFILE = "autofill_profile";
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100162 public final static String PREF_AUTOFILL_ACTIVE_PROFILE_ID = "autofill_active_profile_id";
John Reck63bb6872010-12-01 19:29:32 -0800163 public final static String PREF_HARDWARE_ACCEL = "enable_hardware_accel";
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800164 public final static String PREF_VISUAL_INDICATOR = "enable_visual_indicator";
John Reck63bb6872010-12-01 19:29:32 -0800165 public final static String PREF_USER_AGENT = "user_agent";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800166
Michael Kolb376b5412010-12-15 11:52:57 -0800167 public final static String PREF_QUICK_CONTROLS = "enable_quick_controls";
John Reckbafe58a2011-01-11 10:26:02 -0800168 public final static String PREF_MOST_VISITED_HOMEPAGE = "use_most_visited_homepage";
John Reck282431b2011-01-20 17:38:37 -0800169 public final static String PREF_PLUGIN_STATE = "plugin_state";
Narayan Kamath5119edd2011-02-23 15:49:17 +0000170 public final static String PREF_USE_INSTANT = "use_instant_search";
John Reck40badf22011-04-20 15:31:13 -0700171 public final static String PREF_MIN_FONT_SIZE = "min_font_size";
Michael Kolb376b5412010-12-15 11:52:57 -0800172
The Android Open Source Project0c908882009-03-03 19:32:16 -0800173 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700174 "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
175 "like Gecko) Version/5.0 Safari/533.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800176
177 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700178 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
179 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
180
181 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
182 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
183 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
184
185 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
186 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
187 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800188
189 // Value to truncate strings when adding them to a TextView within
190 // a ListView
191 public final static int MAX_TEXTVIEW_LEN = 80;
192
Jeff Davidson43610292010-07-16 16:03:58 -0700193 public static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider";
194
195 public static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/");
196
John Reck63bb6872010-12-01 19:29:32 -0800197 // Set to true to enable some of the about:debug options
John Recke55880d2011-03-18 12:49:03 -0700198 public static final boolean DEV_BUILD = true;
John Reck63bb6872010-12-01 19:29:32 -0800199
Michael Kolb8233fac2010-10-26 16:08:53 -0700200 private Controller mController;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800201
202 // Single instance of the BrowserSettings for use in the Browser app.
203 private static BrowserSettings sSingleton;
204
205 // Private map of WebSettings to Observer objects used when deleting an
206 // observer.
207 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
208 new HashMap<WebSettings,Observer>();
209
Ben Murdochef671652010-11-25 17:17:58 +0000210 private boolean mLoadFromDbComplete;
211
212 public void waitForLoadFromDbToComplete() {
213 synchronized (sSingleton) {
214 while (!mLoadFromDbComplete) {
215 try {
216 sSingleton.wait();
217 } catch (InterruptedException e) { }
218 }
219 }
220 }
221
The Android Open Source Project0c908882009-03-03 19:32:16 -0800222 /*
223 * An observer wrapper for updating a WebSettings object with the new
224 * settings after a call to BrowserSettings.update().
225 */
John Reck40badf22011-04-20 15:31:13 -0700226 public static class Observer implements java.util.Observer {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800227 // Private WebSettings object that will be updated.
228 private WebSettings mSettings;
229
230 Observer(WebSettings w) {
231 mSettings = w;
232 }
233
234 public void update(Observable o, Object arg) {
235 BrowserSettings b = (BrowserSettings)o;
236 WebSettings s = mSettings;
237
238 s.setLayoutAlgorithm(b.layoutAlgorithm);
239 if (b.userAgent == 0) {
240 // use the default ua string
241 s.setUserAgentString(null);
242 } else if (b.userAgent == 1) {
243 s.setUserAgentString(DESKTOP_USERAGENT);
244 } else if (b.userAgent == 2) {
245 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700246 } else if (b.userAgent == 3) {
247 s.setUserAgentString(IPAD_USERAGENT);
248 } else if (b.userAgent == 4) {
249 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800250 }
251 s.setUseWideViewPort(b.useWideViewPort);
252 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
253 s.setJavaScriptEnabled(b.javaScriptEnabled);
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800254 s.setShowVisualIndicator(b.showVisualIndicator);
Patrick Scotte536b332010-03-22 10:19:32 -0400255 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800256 s.setJavaScriptCanOpenWindowsAutomatically(
257 b.javaScriptCanOpenWindowsAutomatically);
258 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
259 s.setMinimumFontSize(b.minimumFontSize);
260 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
261 s.setDefaultFontSize(b.defaultFontSize);
262 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
263 s.setNavDump(b.navDump);
264 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700265 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800266 s.setLightTouchEnabled(b.lightTouch);
267 s.setSaveFormData(b.saveFormData);
Ben Murdochce549fc2010-09-09 10:28:08 +0100268 s.setAutoFillEnabled(b.autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800269 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700270 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800271 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800272
273 // WebView inside Browser doesn't want initial focus to be set.
274 s.setNeedInitialFocus(false);
275 // Browser supports multiple windows
276 s.setSupportMultipleWindows(true);
Grace Kloba61fc77c2010-06-22 09:57:33 -0700277 // enable smooth transition for better performance during panning or
278 // zooming
279 s.setEnableSmoothTransition(true);
Patrick Scottb92bbb42011-01-05 11:38:58 -0500280 // disable content url access
281 s.setAllowContentAccess(false);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100282
Andrei Popescu01068702009-08-03 16:03:24 +0100283 // HTML5 API flags
284 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100285 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100286 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100287 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100288 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100289
Andrei Popescu01068702009-08-03 16:03:24 +0100290 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100291 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100292 s.setAppCachePath(b.appCachePath);
293 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100294 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100295
Ben Murdoch0cb81892010-10-08 12:41:33 +0100296 // Active AutoFill profile data.
Ben Murdoch23da30e2010-10-26 15:18:44 +0100297 s.setAutoFillProfile(b.autoFillProfile);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100298
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800299 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800300 }
301 }
302
303 /**
Ben Murdochef671652010-11-25 17:17:58 +0000304 * Load settings from the browser app's database. It is performed in
305 * an AsyncTask as it involves plenty of slow disk IO.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800306 * NOTE: Strings used for the preferences must match those specified
Jeff Hamilton175ab302010-10-04 12:53:49 -0500307 * in the various preference XML files.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800308 * @param ctx A Context object used to query the browser's settings
309 * database. If the database exists, the saved settings will be
310 * stored in this BrowserSettings object. This will update all
311 * observers of this object.
312 */
Ben Murdochef671652010-11-25 17:17:58 +0000313 public void asyncLoadFromDb(final Context ctx) {
314 mLoadFromDbComplete = false;
315 // Run the initial settings load in an AsyncTask as it hits the
316 // disk multiple times through SharedPreferences and SQLite. We
317 // need to be certain though that this has completed before we start
318 // to load pages though, so in the worst case we will block waiting
319 // for it to finish in BrowserActivity.onCreate().
320 new LoadFromDbTask(ctx).execute();
321 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800322
Ben Murdochef671652010-11-25 17:17:58 +0000323 private class LoadFromDbTask extends AsyncTask<Void, Void, Void> {
324 private Context mContext;
325
326 public LoadFromDbTask(Context context) {
327 mContext = context;
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700328 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700329
Ben Murdochef671652010-11-25 17:17:58 +0000330 protected Void doInBackground(Void... unused) {
331 SharedPreferences p =
332 PreferenceManager.getDefaultSharedPreferences(mContext);
333 // Set the default value for the Application Caches path.
334 appCachePath = mContext.getDir("appcache", 0).getPath();
335 // Determine the maximum size of the application cache.
336 webStorageSizeManager = new WebStorageSizeManager(
337 mContext,
338 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
339 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
340 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
341 // Set the default value for the Database path.
342 databasePath = mContext.getDir("databases", 0).getPath();
343 // Set the default value for the Geolocation database path.
344 geolocationDatabasePath = mContext.getDir("geolocation", 0).getPath();
345
John Reckef074262010-12-02 16:09:14 -0800346 if (p.getString(PREF_HOMEPAGE, null) == null) {
Ben Murdochef671652010-11-25 17:17:58 +0000347 // No home page preferences is set, set it to default.
348 setHomePage(mContext, getFactoryResetHomeUrl(mContext));
349 }
350
351 // the cost of one cached page is ~3M (measured using nytimes.com). For
352 // low end devices, we only cache one page. For high end devices, we try
353 // to cache more pages, currently choose 5.
354 ActivityManager am = (ActivityManager) mContext
355 .getSystemService(Context.ACTIVITY_SERVICE);
356 if (am.getMemoryClass() > 16) {
357 pageCacheCapacity = 5;
358 } else {
359 pageCacheCapacity = 1;
360 }
361
362 // Read the last active AutoFill profile id.
363 autoFillActiveProfileId = p.getInt(
364 PREF_AUTOFILL_ACTIVE_PROFILE_ID, autoFillActiveProfileId);
365
366 // Load the autofill profile data from the database. We use a database separate
367 // to the browser preference DB to make it easier to support multiple profiles
368 // and switching between them.
369 AutoFillProfileDatabase autoFillDb = AutoFillProfileDatabase.getInstance(mContext);
370 Cursor c = autoFillDb.getProfile(autoFillActiveProfileId);
371
372 if (c.getCount() > 0) {
373 c.moveToFirst();
374
375 String fullName = c.getString(c.getColumnIndex(
376 AutoFillProfileDatabase.Profiles.FULL_NAME));
377 String email = c.getString(c.getColumnIndex(
378 AutoFillProfileDatabase.Profiles.EMAIL_ADDRESS));
379 String company = c.getString(c.getColumnIndex(
380 AutoFillProfileDatabase.Profiles.COMPANY_NAME));
381 String addressLine1 = c.getString(c.getColumnIndex(
382 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_1));
383 String addressLine2 = c.getString(c.getColumnIndex(
384 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_2));
385 String city = c.getString(c.getColumnIndex(
386 AutoFillProfileDatabase.Profiles.CITY));
387 String state = c.getString(c.getColumnIndex(
388 AutoFillProfileDatabase.Profiles.STATE));
389 String zip = c.getString(c.getColumnIndex(
390 AutoFillProfileDatabase.Profiles.ZIP_CODE));
391 String country = c.getString(c.getColumnIndex(
392 AutoFillProfileDatabase.Profiles.COUNTRY));
393 String phone = c.getString(c.getColumnIndex(
394 AutoFillProfileDatabase.Profiles.PHONE_NUMBER));
395 autoFillProfile = new AutoFillProfile(autoFillActiveProfileId,
396 fullName, email, company, addressLine1, addressLine2, city,
397 state, zip, country, phone);
398 }
399 c.close();
400 autoFillDb.close();
401
402 // PreferenceManager.setDefaultValues is TOO SLOW, need to manually keep
403 // the defaults in sync
John Reck63bb6872010-12-01 19:29:32 -0800404 p.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
Ben Murdochef671652010-11-25 17:17:58 +0000405 syncSharedPreferences(mContext, p);
406
407 synchronized (sSingleton) {
408 mLoadFromDbComplete = true;
409 sSingleton.notify();
410 }
411 return null;
Grace Kloba9804c432009-12-02 11:07:40 -0800412 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800413 }
414
Narayan Kamath5119edd2011-02-23 15:49:17 +0000415 private void updateSearchEngine(Context ctx, String searchEngineName, boolean force) {
416 if (force || searchEngine == null ||
417 !searchEngine.getName().equals(searchEngineName)) {
418 if (searchEngine != null) {
419 if (searchEngine.supportsVoiceSearch()) {
420 // One or more tabs could have been in voice search mode.
421 // Clear it, since the new SearchEngine may not support
422 // it, or may handle it differently.
423 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
424 mController.getTabControl().getTab(i).revertVoiceSearchMode();
425 }
426 }
427 searchEngine.close();
428 }
429 searchEngine = SearchEngines.get(ctx, searchEngineName);
430
431 if (mController != null && (searchEngine instanceof InstantSearchEngine)) {
432 ((InstantSearchEngine) searchEngine).setController(mController);
433 }
434 }
435 }
436
John Reck40badf22011-04-20 15:31:13 -0700437 public void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700438
The Android Open Source Project0c908882009-03-03 19:32:16 -0800439 homeUrl =
440 p.getString(PREF_HOMEPAGE, homeUrl);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000441
442 useInstant = p.getBoolean(PREF_USE_INSTANT, useInstant);
Leon Scroggins III31306602010-09-17 11:10:29 -0400443 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
Narayan Kamath5119edd2011-02-23 15:49:17 +0000444 SearchEngine.GOOGLE);
445 updateSearchEngine(ctx, searchEngineName, false);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700446
The Android Open Source Project0c908882009-03-03 19:32:16 -0800447 loadsImagesAutomatically = p.getBoolean("load_images",
448 loadsImagesAutomatically);
449 javaScriptEnabled = p.getBoolean("enable_javascript",
450 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400451 pluginState = WebSettings.PluginState.valueOf(
John Reck282431b2011-01-20 17:38:37 -0800452 p.getString(PREF_PLUGIN_STATE, pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800453 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
454 "block_popup_windows",
455 !javaScriptCanOpenWindowsAutomatically);
456 showSecurityWarnings = p.getBoolean("show_security_warnings",
457 showSecurityWarnings);
458 rememberPasswords = p.getBoolean("remember_passwords",
459 rememberPasswords);
460 saveFormData = p.getBoolean("save_formdata",
461 saveFormData);
Ben Murdochaf554522010-09-10 22:09:30 +0100462 autoFillEnabled = p.getBoolean("autofill_enabled", autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800463 boolean accept_cookies = p.getBoolean("accept_cookies",
464 CookieManager.getInstance().acceptCookie());
465 CookieManager.getInstance().setAcceptCookie(accept_cookies);
466 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800467 textSize = WebSettings.TextSize.valueOf(
468 p.getString(PREF_TEXT_SIZE, textSize.name()));
John Reck40badf22011-04-20 15:31:13 -0700469 minimumFontSize = p.getInt(PREF_MIN_FONT_SIZE, 1);
Grace Kloba2f830682009-06-25 11:08:53 -0700470 zoomDensity = WebSettings.ZoomDensity.valueOf(
471 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800472 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700473 loadsPageInOverviewMode = p.getBoolean("load_page",
474 loadsPageInOverviewMode);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800475 useWideViewPort = true; // use wide view port for either setting
476 if (autoFitPage) {
477 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
478 } else {
479 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
480 }
481 defaultTextEncodingName =
482 p.getString(PREF_DEFAULT_TEXT_ENCODING,
483 defaultTextEncodingName);
484
485 showDebugSettings =
486 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
487 // Debug menu items have precidence if the menu is visible
488 if (showDebugSettings) {
489 boolean small_screen = p.getBoolean("small_screen",
490 layoutAlgorithm ==
491 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
492 if (small_screen) {
493 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
494 } else {
495 boolean normal_layout = p.getBoolean("normal_layout",
496 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
497 if (normal_layout) {
498 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
499 } else {
500 layoutAlgorithm =
501 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
502 }
503 }
504 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
505 tracing = p.getBoolean("enable_tracing", tracing);
506 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
507 navDump = p.getBoolean("enable_nav_dump", navDump);
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800508 showVisualIndicator = p.getBoolean(PREF_VISUAL_INDICATOR, showVisualIndicator);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800509 }
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500510
Michael Kolb376b5412010-12-15 11:52:57 -0800511 quickControls = p.getBoolean(PREF_QUICK_CONTROLS, quickControls);
John Reckbafe58a2011-01-11 10:26:02 -0800512 useMostVisitedHomepage = p.getBoolean(PREF_MOST_VISITED_HOMEPAGE, useMostVisitedHomepage);
Michael Kolb376b5412010-12-15 11:52:57 -0800513
John Reck3db2e072011-01-24 14:57:18 -0800514 // Only set these if this is a dev build or debug is enabled
515 if (DEV_BUILD || showDebugSettings()) {
John Reck63bb6872010-12-01 19:29:32 -0800516 userAgent = Integer.parseInt(p.getString(PREF_USER_AGENT, "0"));
517 hardwareAccelerated = p.getBoolean(PREF_HARDWARE_ACCEL, hardwareAccelerated);
518 }
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500519
Feng Qianb3c02da2009-06-29 15:58:08 -0700520 // JS flags is loaded from DB even if showDebugSettings is false,
521 // so that it can be set once and be effective all the time.
522 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100523
524 // Read the setting for showing/hiding the JS Console always so that should the
525 // user enable debug settings, we already know if we should show the console.
526 // The user will never see the console unless they navigate to about:debug,
527 // regardless of the setting we read here. This setting is only used after debug
528 // is enabled.
529 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700530
Andrei Popescu01068702009-08-03 16:03:24 +0100531 // HTML5 API flags
532 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
533 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
534 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100535 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100536 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100537
Grace Klobad9ee1392009-07-20 11:53:33 -0700538 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800539 }
540
The Android Open Source Project0c908882009-03-03 19:32:16 -0800541 public String getHomePage() {
John Reckbafe58a2011-01-11 10:26:02 -0800542 if (useMostVisitedHomepage) {
543 return HomeProvider.MOST_VISITED;
544 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800545 return homeUrl;
546 }
547
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100548 public SearchEngine getSearchEngine() {
549 return searchEngine;
550 }
551
Feng Qianb3c02da2009-06-29 15:58:08 -0700552 public String getJsFlags() {
553 return jsFlags;
554 }
555
Andrei Popescu79e82b72009-07-27 12:01:59 +0100556 public WebStorageSizeManager getWebStorageSizeManager() {
557 return webStorageSizeManager;
558 }
559
The Android Open Source Project0c908882009-03-03 19:32:16 -0800560 public void setHomePage(Context context, String url) {
561 Editor ed = PreferenceManager.
562 getDefaultSharedPreferences(context).edit();
563 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700564 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800565 homeUrl = url;
566 }
567
The Android Open Source Project0c908882009-03-03 19:32:16 -0800568 public WebSettings.TextSize getTextSize() {
569 return textSize;
570 }
571
John Reck40badf22011-04-20 15:31:13 -0700572 public int getMinimumFontSize() {
573 return minimumFontSize;
574 }
575
Grace Kloba2f830682009-06-25 11:08:53 -0700576 public WebSettings.ZoomDensity getDefaultZoom() {
577 return zoomDensity;
578 }
579
The Android Open Source Project0c908882009-03-03 19:32:16 -0800580 public boolean openInBackground() {
581 return openInBackground;
582 }
583
584 public boolean showSecurityWarnings() {
585 return showSecurityWarnings;
586 }
587
588 public boolean isTracing() {
589 return tracing;
590 }
591
592 public boolean isLightTouch() {
593 return lightTouch;
594 }
595
596 public boolean isNavDump() {
597 return navDump;
598 }
599
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500600 public boolean isHardwareAccelerated() {
601 return hardwareAccelerated;
602 }
603
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800604 public boolean showVisualIndicator() {
605 return showVisualIndicator;
606 }
607
Michael Kolb376b5412010-12-15 11:52:57 -0800608 public boolean useQuickControls() {
609 return quickControls;
610 }
611
John Reckbafe58a2011-01-11 10:26:02 -0800612 public boolean useMostVisitedHomepage() {
613 return useMostVisitedHomepage;
614 }
615
Narayan Kamath5119edd2011-02-23 15:49:17 +0000616 public boolean useInstant() {
617 return useInstant;
618 }
619
The Android Open Source Project0c908882009-03-03 19:32:16 -0800620 public boolean showDebugSettings() {
621 return showDebugSettings;
622 }
623
John Reck3db2e072011-01-24 14:57:18 -0800624 public void toggleDebugSettings(Context context) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800625 showDebugSettings = !showDebugSettings;
626 navDump = showDebugSettings;
John Reck3db2e072011-01-24 14:57:18 -0800627 syncSharedPreferences(context,
628 PreferenceManager.getDefaultSharedPreferences(context));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800629 update();
630 }
631
Ben Murdoch23da30e2010-10-26 15:18:44 +0100632 public void setAutoFillProfile(Context ctx, AutoFillProfile profile, Message msg) {
633 if (profile != null) {
634 setActiveAutoFillProfileId(ctx, profile.getUniqueId());
635 // Update the AutoFill DB with the new profile.
636 new SaveProfileToDbTask(ctx, msg).execute(profile);
637 } else {
638 // Delete the current profile.
Ben Murdoche8a28332010-11-17 14:16:21 +0000639 if (autoFillProfile != null) {
640 new DeleteProfileFromDbTask(ctx, msg).execute(autoFillProfile.getUniqueId());
641 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
642 }
Ben Murdoch23da30e2010-10-26 15:18:44 +0100643 }
644 autoFillProfile = profile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100645 }
646
647 public AutoFillProfile getAutoFillProfile() {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100648 return autoFillProfile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100649 }
650
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100651 private void setActiveAutoFillProfileId(Context context, int activeProfileId) {
652 autoFillActiveProfileId = activeProfileId;
653 Editor ed = PreferenceManager.
654 getDefaultSharedPreferences(context).edit();
655 ed.putInt(PREF_AUTOFILL_ACTIVE_PROFILE_ID, activeProfileId);
656 ed.apply();
657 }
658
Ben Murdoch8029a772010-11-16 11:58:21 +0000659 /* package */ void disableAutoFill(Context ctx) {
660 autoFillEnabled = false;
661 Editor ed = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
662 ed.putBoolean(PREF_AUTOFILL_ENABLED, false);
663 ed.apply();
664 }
665
The Android Open Source Project0c908882009-03-03 19:32:16 -0800666 /**
667 * Add a WebSettings object to the list of observers that will be updated
668 * when update() is called.
669 *
670 * @param s A WebSettings object that is strictly tied to the life of a
671 * WebView.
672 */
673 public Observer addObserver(WebSettings s) {
674 Observer old = mWebSettingsToObservers.get(s);
675 if (old != null) {
676 super.deleteObserver(old);
677 }
678 Observer o = new Observer(s);
679 mWebSettingsToObservers.put(s, o);
680 super.addObserver(o);
681 return o;
682 }
683
684 /**
685 * Delete the given WebSettings observer from the list of observers.
686 * @param s The WebSettings object to be deleted.
687 */
688 public void deleteObserver(WebSettings s) {
689 Observer o = mWebSettingsToObservers.get(s);
690 if (o != null) {
691 mWebSettingsToObservers.remove(s);
692 super.deleteObserver(o);
693 }
694 }
695
696 /*
John Reck63bb6872010-12-01 19:29:32 -0800697 * Application level method for obtaining a single app instance of the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800698 * BrowserSettings.
699 */
John Reck63bb6872010-12-01 19:29:32 -0800700 public static BrowserSettings getInstance() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800701 if (sSingleton == null ) {
702 sSingleton = new BrowserSettings();
703 }
704 return sSingleton;
705 }
706
707 /*
708 * Package level method for associating the BrowserSettings with TabControl
709 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700710 /* package */void setController(Controller ctrl) {
711 mController = ctrl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800712 updateTabControlSettings();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000713
714 if (mController != null && (searchEngine instanceof InstantSearchEngine)) {
715 ((InstantSearchEngine) searchEngine).setController(mController);
716 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800717 }
718
719 /*
720 * Update all the observers of the object.
721 */
722 /*package*/ void update() {
723 setChanged();
724 notifyObservers();
725 }
726
727 /*package*/ void clearCache(Context context) {
728 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700729 if (mController != null) {
730 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800731 if (current != null) {
732 current.clearCache(true);
733 }
734 }
735 }
736
737 /*package*/ void clearCookies(Context context) {
738 CookieManager.getInstance().removeAllCookie();
739 }
740
741 /* package */void clearHistory(Context context) {
742 ContentResolver resolver = context.getContentResolver();
743 Browser.clearHistory(resolver);
744 Browser.clearSearches(resolver);
745 }
746
747 /* package */ void clearFormData(Context context) {
748 WebViewDatabase.getInstance(context).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700749 if (mController!= null) {
750 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100751 if (currentTopView != null) {
752 currentTopView.clearFormData();
753 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800754 }
755 }
756
757 /*package*/ void clearPasswords(Context context) {
758 WebViewDatabase db = WebViewDatabase.getInstance(context);
759 db.clearUsernamePassword();
760 db.clearHttpAuthUsernamePassword();
761 }
762
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800763 private void updateTabControlSettings() {
764 // Enable/disable the error console.
Michael Kolb8233fac2010-10-26 16:08:53 -0700765 mController.setShouldShowErrorConsole(
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800766 showDebugSettings && showConsole);
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800767 }
768
Nicolas Roard78a98e42009-05-11 13:34:17 +0100769 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100770 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100771 }
772
773 /*package*/ void clearLocationAccess(Context context) {
774 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100775 }
776
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400777 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800778 reset();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500779 SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700780 p.edit().clear().apply();
John Reck035a5642010-12-21 18:51:27 -0800781 PreferenceManager.setDefaultValues(ctx, R.xml.general_preferences, true);
782 PreferenceManager.setDefaultValues(ctx, R.xml.privacy_security_preferences, true);
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500783 PreferenceManager.setDefaultValues(ctx, R.xml.advanced_preferences, true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700784 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700785 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100786 // reset appcache max size
787 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch23da30e2010-10-26 15:18:44 +0100788 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700789 }
790
John Reck33bbb802010-10-26 17:13:42 -0700791 /*package*/ static String getFactoryResetHomeUrl(Context context) {
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700792 String url = context.getResources().getString(R.string.homepage_base);
793 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500794 url = url.replace("{CID}",
795 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700796 }
797 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800798 }
799
The Android Open Source Project0c908882009-03-03 19:32:16 -0800800 // Private constructor that does nothing.
801 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800802 reset();
803 }
804
805 private void reset() {
806 // Private variables for settings
807 // NOTE: these defaults need to be kept in sync with the XML
808 // until the performance of PreferenceManager.setDefaultValues()
809 // is improved.
810 loadsImagesAutomatically = true;
811 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400812 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800813 javaScriptCanOpenWindowsAutomatically = false;
814 showSecurityWarnings = true;
815 rememberPasswords = true;
816 saveFormData = true;
Ben Murdochdc62cb92010-11-23 15:11:29 +0000817 autoFillEnabled = true;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800818 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800819 autoFitPage = true;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800820 loadsPageInOverviewMode = true;
821 showDebugSettings = false;
822 // HTML5 API flags
823 appCacheEnabled = true;
824 databaseEnabled = true;
825 domStorageEnabled = true;
826 geolocationEnabled = true;
827 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800828 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100829
Ben Murdoch23da30e2010-10-26 15:18:44 +0100830 private abstract class AutoFillProfileDbTask<T> extends AsyncTask<T, Void, Void> {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100831 Context mContext;
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100832 AutoFillProfileDatabase mAutoFillProfileDb;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100833 Message mCompleteMessage;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100834
Ben Murdoch23da30e2010-10-26 15:18:44 +0100835 public AutoFillProfileDbTask(Context ctx, Message msg) {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100836 mContext = ctx;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100837 mCompleteMessage = msg;
838 }
839
840 protected void onPostExecute(Void result) {
841 if (mCompleteMessage != null) {
842 mCompleteMessage.sendToTarget();
843 }
844 mAutoFillProfileDb.close();
845 }
846
847 abstract protected Void doInBackground(T... values);
848 }
849
850
851 private class SaveProfileToDbTask extends AutoFillProfileDbTask<AutoFillProfile> {
852 public SaveProfileToDbTask(Context ctx, Message msg) {
853 super(ctx, msg);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100854 }
855
856 protected Void doInBackground(AutoFillProfile... values) {
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100857 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100858 assert autoFillActiveProfileId != NO_AUTOFILL_PROFILE_SET;
859 AutoFillProfile newProfile = values[0];
860 mAutoFillProfileDb.addOrUpdateProfile(autoFillActiveProfileId, newProfile);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100861 return null;
862 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100863 }
864
Ben Murdoch23da30e2010-10-26 15:18:44 +0100865 private class DeleteProfileFromDbTask extends AutoFillProfileDbTask<Integer> {
866 public DeleteProfileFromDbTask(Context ctx, Message msg) {
867 super(ctx, msg);
868 }
869
870 protected Void doInBackground(Integer... values) {
871 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
872 int id = values[0];
873 assert id > 0;
874 mAutoFillProfileDb.dropProfile(id);
875 return null;
876 }
877 }
John Reck63bb6872010-12-01 19:29:32 -0800878
879 @Override
880 public void onSharedPreferenceChanged(
881 SharedPreferences p, String key) {
882 if (PREF_HARDWARE_ACCEL.equals(key)) {
883 hardwareAccelerated = p.getBoolean(PREF_HARDWARE_ACCEL, hardwareAccelerated);
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800884 } else if (PREF_VISUAL_INDICATOR.equals(key)) {
885 showVisualIndicator = p.getBoolean(PREF_VISUAL_INDICATOR, showVisualIndicator);
John Reck63bb6872010-12-01 19:29:32 -0800886 } else if (PREF_USER_AGENT.equals(key)) {
887 userAgent = Integer.parseInt(p.getString(PREF_USER_AGENT, "0"));
888 update();
Michael Kolb376b5412010-12-15 11:52:57 -0800889 } else if (PREF_QUICK_CONTROLS.equals(key)) {
890 quickControls = p.getBoolean(PREF_QUICK_CONTROLS, quickControls);
John Reckbafe58a2011-01-11 10:26:02 -0800891 } else if (PREF_MOST_VISITED_HOMEPAGE.equals(key)) {
892 useMostVisitedHomepage = p.getBoolean(PREF_MOST_VISITED_HOMEPAGE, useMostVisitedHomepage);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000893 } else if (PREF_USE_INSTANT.equals(key)) {
894 useInstant = p.getBoolean(PREF_USE_INSTANT, useInstant);
895 updateSearchEngine(mController.getActivity(), SearchEngine.GOOGLE, true);
896 } else if (PREF_SEARCH_ENGINE.equals(key)) {
897 final String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
898 SearchEngine.GOOGLE);
899 updateSearchEngine(mController.getActivity(), searchEngineName, false);
John Reck63bb6872010-12-01 19:29:32 -0800900 }
901 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800902}