blob: 88bd78a8c18fa34acd6c27269a0f71906b63326c [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;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010025import android.content.ComponentName;
The Android Open Source Project0c908882009-03-03 19:32:16 -080026import android.content.ContentResolver;
27import android.content.Context;
John Reck63bb6872010-12-01 19:29:32 -080028import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.content.pm.ActivityInfo;
30import android.content.SharedPreferences;
31import android.content.SharedPreferences.Editor;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010032import android.database.ContentObserver;
Ben Murdoch0cb81892010-10-08 12:41:33 +010033import android.database.Cursor;
Jeff Davidson43610292010-07-16 16:03:58 -070034import android.net.Uri;
Ben Murdoch0cb81892010-10-08 12:41:33 +010035import android.os.AsyncTask;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010036import android.os.Handler;
Ben Murdoch23da30e2010-10-26 15:18:44 +010037import android.os.Message;
Nicolas Roard78a98e42009-05-11 13:34:17 +010038import android.preference.PreferenceActivity;
Ben Murdoch0cb81892010-10-08 12:41:33 +010039import android.preference.PreferenceManager;
Nicolas Roard78a98e42009-05-11 13:34:17 +010040import android.preference.PreferenceScreen;
Ben Murdoch0cb81892010-10-08 12:41:33 +010041import android.provider.Browser;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010042import android.provider.Settings;
43import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010045import android.webkit.GeolocationPermissions;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010046import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047import android.webkit.WebView;
48import android.webkit.WebViewDatabase;
49import android.webkit.WebIconDatabase;
50import android.webkit.WebSettings;
Ben Murdoch0cb81892010-10-08 12:41:33 +010051import android.webkit.WebSettings.AutoFillProfile;
Nicolas Roard78a98e42009-05-11 13:34:17 +010052import android.webkit.WebStorage;
Ben Murdoch0cb81892010-10-08 12:41:33 +010053import android.widget.Toast;
The Android Open Source Project0c908882009-03-03 19:32:16 -080054
55import java.util.HashMap;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010056import java.util.Map;
57import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080058import java.util.Observable;
59
60/*
61 * Package level class for storing various WebView and Browser settings. To use
62 * this class:
63 * BrowserSettings s = BrowserSettings.getInstance();
64 * s.addObserver(webView.getSettings());
65 * s.loadFromDb(context); // Only needed on app startup
66 * s.javaScriptEnabled = true;
67 * ... // set any other settings
68 * s.update(); // this will update all the observers
69 *
70 * To remove an observer:
71 * s.deleteObserver(webView.getSettings());
72 */
John Reck63bb6872010-12-01 19:29:32 -080073public class BrowserSettings extends Observable implements OnSharedPreferenceChangeListener {
Leon Scroggins9ac587d2009-04-20 12:53:46 -040074 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080075 // NOTE: these defaults need to be kept in sync with the XML
76 // until the performance of PreferenceManager.setDefaultValues()
77 // is improved.
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080078 // Note: boolean variables are set inside reset function.
79 private boolean loadsImagesAutomatically;
80 private boolean javaScriptEnabled;
Patrick Scotte536b332010-03-22 10:19:32 -040081 private WebSettings.PluginState pluginState;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080082 private boolean javaScriptCanOpenWindowsAutomatically;
83 private boolean showSecurityWarnings;
84 private boolean rememberPasswords;
85 private boolean saveFormData;
Ben Murdochce549fc2010-09-09 10:28:08 +010086 private boolean autoFillEnabled;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080087 private boolean openInBackground;
The Android Open Source Project0c908882009-03-03 19:32:16 -080088 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070089 private String homeUrl = "";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010090 private SearchEngine searchEngine;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080091 private boolean autoFitPage;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080092 private boolean loadsPageInOverviewMode;
93 private boolean showDebugSettings;
Andrei Popescu01068702009-08-03 16:03:24 +010094 // HTML5 API flags
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080095 private boolean appCacheEnabled;
96 private boolean databaseEnabled;
97 private boolean domStorageEnabled;
98 private boolean geolocationEnabled;
99 private boolean workersEnabled; // only affects V8. JSC does not have a similar setting
Andrei Popescu01068702009-08-03 16:03:24 +0100100 // HTML5 API configuration params
101 private long appCacheMaxSize = Long.MAX_VALUE;
102 private String appCachePath; // default value set in loadFromDb().
103 private String databasePath; // default value set in loadFromDb()
Steve Block5029cf02009-08-21 13:16:58 +0100104 private String geolocationDatabasePath; // default value set in loadFromDb()
Andrei Popescu01068702009-08-03 16:03:24 +0100105 private WebStorageSizeManager webStorageSizeManager;
106
107 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800108
Nicolas Roard78a98e42009-05-11 13:34:17 +0100109 private final static String TAG = "BrowserSettings";
110
The Android Open Source Project0c908882009-03-03 19:32:16 -0800111 // Development settings
112 public WebSettings.LayoutAlgorithm layoutAlgorithm =
113 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
114 private boolean useWideViewPort = true;
115 private int userAgent = 0;
116 private boolean tracing = false;
117 private boolean lightTouch = false;
118 private boolean navDump = false;
Derek Sollenberger8de82852010-11-18 19:51:50 -0500119 private boolean hardwareAccelerated = true;
Feng Qianb3c02da2009-06-29 15:58:08 -0700120
Michael Kolb376b5412010-12-15 11:52:57 -0800121 // Lab settings
122 private boolean quickControls = false;
John Reckbafe58a2011-01-11 10:26:02 -0800123 private boolean useMostVisitedHomepage = false;
Michael Kolb376b5412010-12-15 11:52:57 -0800124
Ben Murdochbff2d602009-07-01 20:19:05 +0100125 // By default the error console is shown once the user navigates to about:debug.
126 // The setting can be then toggled from the settings menu.
127 private boolean showConsole = true;
128
The Android Open Source Project0c908882009-03-03 19:32:16 -0800129 // Private preconfigured values
Shimeng (Simon) Wangb4fb25c2010-07-13 15:18:10 -0700130 private static int minimumFontSize = 1;
131 private static int minimumLogicalFontSize = 1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800132 private static int defaultFontSize = 16;
133 private static int defaultFixedFontSize = 13;
134 private static WebSettings.TextSize textSize =
135 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700136 private static WebSettings.ZoomDensity zoomDensity =
137 WebSettings.ZoomDensity.MEDIUM;
Grace Kloba9804c432009-12-02 11:07:40 -0800138 private static int pageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800139
Ben Murdoch0cb81892010-10-08 12:41:33 +0100140
Ben Murdoch23da30e2010-10-26 15:18:44 +0100141 private AutoFillProfile autoFillProfile;
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100142 // Default to zero. In the case no profile is set up, the initial
143 // value will come from the AutoFillSettingsFragment when the user
144 // creates a profile. Otherwise, we'll read the ID of the last used
145 // profile from the prefs db.
146 private int autoFillActiveProfileId;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100147 private static final int NO_AUTOFILL_PROFILE_SET = 0;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100148
The Android Open Source Project0c908882009-03-03 19:32:16 -0800149 // Preference keys that are used outside this class
150 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
151 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
152 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
153 public final static String PREF_HOMEPAGE = "homepage";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100154 public final static String PREF_SEARCH_ENGINE = "search_engine";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800155 public final static String PREF_CLEAR_FORM_DATA =
156 "privacy_clear_form_data";
157 public final static String PREF_CLEAR_PASSWORDS =
158 "privacy_clear_passwords";
159 public final static String PREF_EXTRAS_RESET_DEFAULTS =
160 "reset_default_preferences";
161 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100162 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800163 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700164 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800165 public final static String PREF_DEFAULT_TEXT_ENCODING =
166 "default_text_encoding";
Steve Blockc6f577f2009-08-20 18:11:08 +0100167 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
168 "privacy_clear_geolocation_access";
Ben Murdochaf554522010-09-10 22:09:30 +0100169 public final static String PREF_AUTOFILL_ENABLED = "autofill_enabled";
170 public final static String PREF_AUTOFILL_PROFILE = "autofill_profile";
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100171 public final static String PREF_AUTOFILL_ACTIVE_PROFILE_ID = "autofill_active_profile_id";
John Reck63bb6872010-12-01 19:29:32 -0800172 public final static String PREF_HARDWARE_ACCEL = "enable_hardware_accel";
173 public final static String PREF_USER_AGENT = "user_agent";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800174
Michael Kolb376b5412010-12-15 11:52:57 -0800175 public final static String PREF_QUICK_CONTROLS = "enable_quick_controls";
John Reckbafe58a2011-01-11 10:26:02 -0800176 public final static String PREF_MOST_VISITED_HOMEPAGE = "use_most_visited_homepage";
Michael Kolb376b5412010-12-15 11:52:57 -0800177
The Android Open Source Project0c908882009-03-03 19:32:16 -0800178 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700179 "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
180 "like Gecko) Version/5.0 Safari/533.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800181
182 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700183 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
184 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
185
186 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
187 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
188 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
189
190 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
191 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
192 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800193
194 // Value to truncate strings when adding them to a TextView within
195 // a ListView
196 public final static int MAX_TEXTVIEW_LEN = 80;
197
Jeff Davidson43610292010-07-16 16:03:58 -0700198 public static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider";
199
200 public static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/");
201
John Reck63bb6872010-12-01 19:29:32 -0800202 // Set to true to enable some of the about:debug options
John Reck9eed0ef2011-01-11 17:11:37 -0800203 public static final boolean DEV_BUILD = false;
John Reck63bb6872010-12-01 19:29:32 -0800204
Michael Kolb8233fac2010-10-26 16:08:53 -0700205 private Controller mController;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800206
207 // Single instance of the BrowserSettings for use in the Browser app.
208 private static BrowserSettings sSingleton;
209
210 // Private map of WebSettings to Observer objects used when deleting an
211 // observer.
212 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
213 new HashMap<WebSettings,Observer>();
214
Ben Murdochef671652010-11-25 17:17:58 +0000215 private boolean mLoadFromDbComplete;
216
217 public void waitForLoadFromDbToComplete() {
218 synchronized (sSingleton) {
219 while (!mLoadFromDbComplete) {
220 try {
221 sSingleton.wait();
222 } catch (InterruptedException e) { }
223 }
224 }
225 }
226
The Android Open Source Project0c908882009-03-03 19:32:16 -0800227 /*
228 * An observer wrapper for updating a WebSettings object with the new
229 * settings after a call to BrowserSettings.update().
230 */
231 static class Observer implements java.util.Observer {
232 // Private WebSettings object that will be updated.
233 private WebSettings mSettings;
234
235 Observer(WebSettings w) {
236 mSettings = w;
237 }
238
239 public void update(Observable o, Object arg) {
240 BrowserSettings b = (BrowserSettings)o;
241 WebSettings s = mSettings;
242
243 s.setLayoutAlgorithm(b.layoutAlgorithm);
244 if (b.userAgent == 0) {
245 // use the default ua string
246 s.setUserAgentString(null);
247 } else if (b.userAgent == 1) {
248 s.setUserAgentString(DESKTOP_USERAGENT);
249 } else if (b.userAgent == 2) {
250 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700251 } else if (b.userAgent == 3) {
252 s.setUserAgentString(IPAD_USERAGENT);
253 } else if (b.userAgent == 4) {
254 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800255 }
256 s.setUseWideViewPort(b.useWideViewPort);
257 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
258 s.setJavaScriptEnabled(b.javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400259 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800260 s.setJavaScriptCanOpenWindowsAutomatically(
261 b.javaScriptCanOpenWindowsAutomatically);
262 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
263 s.setMinimumFontSize(b.minimumFontSize);
264 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
265 s.setDefaultFontSize(b.defaultFontSize);
266 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
267 s.setNavDump(b.navDump);
268 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700269 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800270 s.setLightTouchEnabled(b.lightTouch);
271 s.setSaveFormData(b.saveFormData);
Ben Murdochce549fc2010-09-09 10:28:08 +0100272 s.setAutoFillEnabled(b.autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800273 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700274 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800275 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800276
277 // WebView inside Browser doesn't want initial focus to be set.
278 s.setNeedInitialFocus(false);
279 // Browser supports multiple windows
280 s.setSupportMultipleWindows(true);
Grace Kloba61fc77c2010-06-22 09:57:33 -0700281 // enable smooth transition for better performance during panning or
282 // zooming
283 s.setEnableSmoothTransition(true);
Patrick Scottb92bbb42011-01-05 11:38:58 -0500284 // disable content url access
285 s.setAllowContentAccess(false);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100286
Andrei Popescu01068702009-08-03 16:03:24 +0100287 // HTML5 API flags
288 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100289 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100290 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100291 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100292 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100293
Andrei Popescu01068702009-08-03 16:03:24 +0100294 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100295 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100296 s.setAppCachePath(b.appCachePath);
297 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100298 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100299
Ben Murdoch0cb81892010-10-08 12:41:33 +0100300 // Active AutoFill profile data.
Ben Murdoch23da30e2010-10-26 15:18:44 +0100301 s.setAutoFillProfile(b.autoFillProfile);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100302
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800303 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800304 }
305 }
306
307 /**
Ben Murdochef671652010-11-25 17:17:58 +0000308 * Load settings from the browser app's database. It is performed in
309 * an AsyncTask as it involves plenty of slow disk IO.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800310 * NOTE: Strings used for the preferences must match those specified
Jeff Hamilton175ab302010-10-04 12:53:49 -0500311 * in the various preference XML files.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800312 * @param ctx A Context object used to query the browser's settings
313 * database. If the database exists, the saved settings will be
314 * stored in this BrowserSettings object. This will update all
315 * observers of this object.
316 */
Ben Murdochef671652010-11-25 17:17:58 +0000317 public void asyncLoadFromDb(final Context ctx) {
318 mLoadFromDbComplete = false;
319 // Run the initial settings load in an AsyncTask as it hits the
320 // disk multiple times through SharedPreferences and SQLite. We
321 // need to be certain though that this has completed before we start
322 // to load pages though, so in the worst case we will block waiting
323 // for it to finish in BrowserActivity.onCreate().
324 new LoadFromDbTask(ctx).execute();
325 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800326
Ben Murdochef671652010-11-25 17:17:58 +0000327 private class LoadFromDbTask extends AsyncTask<Void, Void, Void> {
328 private Context mContext;
329
330 public LoadFromDbTask(Context context) {
331 mContext = context;
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700332 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700333
Ben Murdochef671652010-11-25 17:17:58 +0000334 protected Void doInBackground(Void... unused) {
335 SharedPreferences p =
336 PreferenceManager.getDefaultSharedPreferences(mContext);
337 // Set the default value for the Application Caches path.
338 appCachePath = mContext.getDir("appcache", 0).getPath();
339 // Determine the maximum size of the application cache.
340 webStorageSizeManager = new WebStorageSizeManager(
341 mContext,
342 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
343 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
344 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
345 // Set the default value for the Database path.
346 databasePath = mContext.getDir("databases", 0).getPath();
347 // Set the default value for the Geolocation database path.
348 geolocationDatabasePath = mContext.getDir("geolocation", 0).getPath();
349
John Reckef074262010-12-02 16:09:14 -0800350 if (p.getString(PREF_HOMEPAGE, null) == null) {
Ben Murdochef671652010-11-25 17:17:58 +0000351 // No home page preferences is set, set it to default.
352 setHomePage(mContext, getFactoryResetHomeUrl(mContext));
353 }
354
355 // the cost of one cached page is ~3M (measured using nytimes.com). For
356 // low end devices, we only cache one page. For high end devices, we try
357 // to cache more pages, currently choose 5.
358 ActivityManager am = (ActivityManager) mContext
359 .getSystemService(Context.ACTIVITY_SERVICE);
360 if (am.getMemoryClass() > 16) {
361 pageCacheCapacity = 5;
362 } else {
363 pageCacheCapacity = 1;
364 }
365
366 // Read the last active AutoFill profile id.
367 autoFillActiveProfileId = p.getInt(
368 PREF_AUTOFILL_ACTIVE_PROFILE_ID, autoFillActiveProfileId);
369
370 // Load the autofill profile data from the database. We use a database separate
371 // to the browser preference DB to make it easier to support multiple profiles
372 // and switching between them.
373 AutoFillProfileDatabase autoFillDb = AutoFillProfileDatabase.getInstance(mContext);
374 Cursor c = autoFillDb.getProfile(autoFillActiveProfileId);
375
376 if (c.getCount() > 0) {
377 c.moveToFirst();
378
379 String fullName = c.getString(c.getColumnIndex(
380 AutoFillProfileDatabase.Profiles.FULL_NAME));
381 String email = c.getString(c.getColumnIndex(
382 AutoFillProfileDatabase.Profiles.EMAIL_ADDRESS));
383 String company = c.getString(c.getColumnIndex(
384 AutoFillProfileDatabase.Profiles.COMPANY_NAME));
385 String addressLine1 = c.getString(c.getColumnIndex(
386 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_1));
387 String addressLine2 = c.getString(c.getColumnIndex(
388 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_2));
389 String city = c.getString(c.getColumnIndex(
390 AutoFillProfileDatabase.Profiles.CITY));
391 String state = c.getString(c.getColumnIndex(
392 AutoFillProfileDatabase.Profiles.STATE));
393 String zip = c.getString(c.getColumnIndex(
394 AutoFillProfileDatabase.Profiles.ZIP_CODE));
395 String country = c.getString(c.getColumnIndex(
396 AutoFillProfileDatabase.Profiles.COUNTRY));
397 String phone = c.getString(c.getColumnIndex(
398 AutoFillProfileDatabase.Profiles.PHONE_NUMBER));
399 autoFillProfile = new AutoFillProfile(autoFillActiveProfileId,
400 fullName, email, company, addressLine1, addressLine2, city,
401 state, zip, country, phone);
402 }
403 c.close();
404 autoFillDb.close();
405
406 // PreferenceManager.setDefaultValues is TOO SLOW, need to manually keep
407 // the defaults in sync
John Reck63bb6872010-12-01 19:29:32 -0800408 p.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
Ben Murdochef671652010-11-25 17:17:58 +0000409 syncSharedPreferences(mContext, p);
410
411 synchronized (sSingleton) {
412 mLoadFromDbComplete = true;
413 sSingleton.notify();
414 }
415 return null;
Grace Kloba9804c432009-12-02 11:07:40 -0800416 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800417 }
418
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100419 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700420
The Android Open Source Project0c908882009-03-03 19:32:16 -0800421 homeUrl =
422 p.getString(PREF_HOMEPAGE, homeUrl);
Leon Scroggins III31306602010-09-17 11:10:29 -0400423 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
424 SearchEngine.GOOGLE);
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100425 if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
426 if (searchEngine != null) {
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400427 if (searchEngine.supportsVoiceSearch()) {
428 // One or more tabs could have been in voice search mode.
429 // Clear it, since the new SearchEngine may not support
430 // it, or may handle it differently.
Michael Kolb8233fac2010-10-26 16:08:53 -0700431 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
432 mController.getTabControl().getTab(i).revertVoiceSearchMode();
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400433 }
434 }
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100435 searchEngine.close();
436 }
437 searchEngine = SearchEngines.get(ctx, searchEngineName);
438 }
439 Log.i(TAG, "Selected search engine: " + searchEngine);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700440
The Android Open Source Project0c908882009-03-03 19:32:16 -0800441 loadsImagesAutomatically = p.getBoolean("load_images",
442 loadsImagesAutomatically);
443 javaScriptEnabled = p.getBoolean("enable_javascript",
444 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400445 pluginState = WebSettings.PluginState.valueOf(
446 p.getString("plugin_state", pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800447 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
448 "block_popup_windows",
449 !javaScriptCanOpenWindowsAutomatically);
450 showSecurityWarnings = p.getBoolean("show_security_warnings",
451 showSecurityWarnings);
452 rememberPasswords = p.getBoolean("remember_passwords",
453 rememberPasswords);
454 saveFormData = p.getBoolean("save_formdata",
455 saveFormData);
Ben Murdochaf554522010-09-10 22:09:30 +0100456 autoFillEnabled = p.getBoolean("autofill_enabled", autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800457 boolean accept_cookies = p.getBoolean("accept_cookies",
458 CookieManager.getInstance().acceptCookie());
459 CookieManager.getInstance().setAcceptCookie(accept_cookies);
460 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800461 textSize = WebSettings.TextSize.valueOf(
462 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700463 zoomDensity = WebSettings.ZoomDensity.valueOf(
464 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800465 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700466 loadsPageInOverviewMode = p.getBoolean("load_page",
467 loadsPageInOverviewMode);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800468 useWideViewPort = true; // use wide view port for either setting
469 if (autoFitPage) {
470 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
471 } else {
472 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
473 }
474 defaultTextEncodingName =
475 p.getString(PREF_DEFAULT_TEXT_ENCODING,
476 defaultTextEncodingName);
477
478 showDebugSettings =
479 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
480 // Debug menu items have precidence if the menu is visible
481 if (showDebugSettings) {
482 boolean small_screen = p.getBoolean("small_screen",
483 layoutAlgorithm ==
484 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
485 if (small_screen) {
486 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
487 } else {
488 boolean normal_layout = p.getBoolean("normal_layout",
489 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
490 if (normal_layout) {
491 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
492 } else {
493 layoutAlgorithm =
494 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
495 }
496 }
497 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
498 tracing = p.getBoolean("enable_tracing", tracing);
499 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
500 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800501 }
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500502
Michael Kolb376b5412010-12-15 11:52:57 -0800503 quickControls = p.getBoolean(PREF_QUICK_CONTROLS, quickControls);
John Reckbafe58a2011-01-11 10:26:02 -0800504 useMostVisitedHomepage = p.getBoolean(PREF_MOST_VISITED_HOMEPAGE, useMostVisitedHomepage);
Michael Kolb376b5412010-12-15 11:52:57 -0800505
John Reck63bb6872010-12-01 19:29:32 -0800506 // Only set these on startup if it is a dev build
507 if (DEV_BUILD) {
508 userAgent = Integer.parseInt(p.getString(PREF_USER_AGENT, "0"));
509 hardwareAccelerated = p.getBoolean(PREF_HARDWARE_ACCEL, hardwareAccelerated);
510 }
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500511
Feng Qianb3c02da2009-06-29 15:58:08 -0700512 // JS flags is loaded from DB even if showDebugSettings is false,
513 // so that it can be set once and be effective all the time.
514 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100515
516 // Read the setting for showing/hiding the JS Console always so that should the
517 // user enable debug settings, we already know if we should show the console.
518 // The user will never see the console unless they navigate to about:debug,
519 // regardless of the setting we read here. This setting is only used after debug
520 // is enabled.
521 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700522
Andrei Popescu01068702009-08-03 16:03:24 +0100523 // HTML5 API flags
524 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
525 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
526 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100527 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100528 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100529
Grace Klobad9ee1392009-07-20 11:53:33 -0700530 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800531 }
532
The Android Open Source Project0c908882009-03-03 19:32:16 -0800533 public String getHomePage() {
John Reckbafe58a2011-01-11 10:26:02 -0800534 if (useMostVisitedHomepage) {
535 return HomeProvider.MOST_VISITED;
536 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800537 return homeUrl;
538 }
539
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100540 public SearchEngine getSearchEngine() {
541 return searchEngine;
542 }
543
Feng Qianb3c02da2009-06-29 15:58:08 -0700544 public String getJsFlags() {
545 return jsFlags;
546 }
547
Andrei Popescu79e82b72009-07-27 12:01:59 +0100548 public WebStorageSizeManager getWebStorageSizeManager() {
549 return webStorageSizeManager;
550 }
551
The Android Open Source Project0c908882009-03-03 19:32:16 -0800552 public void setHomePage(Context context, String url) {
553 Editor ed = PreferenceManager.
554 getDefaultSharedPreferences(context).edit();
555 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700556 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800557 homeUrl = url;
558 }
559
The Android Open Source Project0c908882009-03-03 19:32:16 -0800560 public WebSettings.TextSize getTextSize() {
561 return textSize;
562 }
563
Grace Kloba2f830682009-06-25 11:08:53 -0700564 public WebSettings.ZoomDensity getDefaultZoom() {
565 return zoomDensity;
566 }
567
The Android Open Source Project0c908882009-03-03 19:32:16 -0800568 public boolean openInBackground() {
569 return openInBackground;
570 }
571
572 public boolean showSecurityWarnings() {
573 return showSecurityWarnings;
574 }
575
576 public boolean isTracing() {
577 return tracing;
578 }
579
580 public boolean isLightTouch() {
581 return lightTouch;
582 }
583
584 public boolean isNavDump() {
585 return navDump;
586 }
587
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500588 public boolean isHardwareAccelerated() {
589 return hardwareAccelerated;
590 }
591
Michael Kolb376b5412010-12-15 11:52:57 -0800592 public boolean useQuickControls() {
593 return quickControls;
594 }
595
John Reckbafe58a2011-01-11 10:26:02 -0800596 public boolean useMostVisitedHomepage() {
597 return useMostVisitedHomepage;
598 }
599
The Android Open Source Project0c908882009-03-03 19:32:16 -0800600 public boolean showDebugSettings() {
601 return showDebugSettings;
602 }
603
604 public void toggleDebugSettings() {
605 showDebugSettings = !showDebugSettings;
606 navDump = showDebugSettings;
607 update();
608 }
609
Ben Murdoch23da30e2010-10-26 15:18:44 +0100610 public void setAutoFillProfile(Context ctx, AutoFillProfile profile, Message msg) {
611 if (profile != null) {
612 setActiveAutoFillProfileId(ctx, profile.getUniqueId());
613 // Update the AutoFill DB with the new profile.
614 new SaveProfileToDbTask(ctx, msg).execute(profile);
615 } else {
616 // Delete the current profile.
Ben Murdoche8a28332010-11-17 14:16:21 +0000617 if (autoFillProfile != null) {
618 new DeleteProfileFromDbTask(ctx, msg).execute(autoFillProfile.getUniqueId());
619 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
620 }
Ben Murdoch23da30e2010-10-26 15:18:44 +0100621 }
622 autoFillProfile = profile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100623 }
624
625 public AutoFillProfile getAutoFillProfile() {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100626 return autoFillProfile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100627 }
628
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100629 private void setActiveAutoFillProfileId(Context context, int activeProfileId) {
630 autoFillActiveProfileId = activeProfileId;
631 Editor ed = PreferenceManager.
632 getDefaultSharedPreferences(context).edit();
633 ed.putInt(PREF_AUTOFILL_ACTIVE_PROFILE_ID, activeProfileId);
634 ed.apply();
635 }
636
Ben Murdoch8029a772010-11-16 11:58:21 +0000637 /* package */ void disableAutoFill(Context ctx) {
638 autoFillEnabled = false;
639 Editor ed = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
640 ed.putBoolean(PREF_AUTOFILL_ENABLED, false);
641 ed.apply();
642 }
643
The Android Open Source Project0c908882009-03-03 19:32:16 -0800644 /**
645 * Add a WebSettings object to the list of observers that will be updated
646 * when update() is called.
647 *
648 * @param s A WebSettings object that is strictly tied to the life of a
649 * WebView.
650 */
651 public Observer addObserver(WebSettings s) {
652 Observer old = mWebSettingsToObservers.get(s);
653 if (old != null) {
654 super.deleteObserver(old);
655 }
656 Observer o = new Observer(s);
657 mWebSettingsToObservers.put(s, o);
658 super.addObserver(o);
659 return o;
660 }
661
662 /**
663 * Delete the given WebSettings observer from the list of observers.
664 * @param s The WebSettings object to be deleted.
665 */
666 public void deleteObserver(WebSettings s) {
667 Observer o = mWebSettingsToObservers.get(s);
668 if (o != null) {
669 mWebSettingsToObservers.remove(s);
670 super.deleteObserver(o);
671 }
672 }
673
674 /*
John Reck63bb6872010-12-01 19:29:32 -0800675 * Application level method for obtaining a single app instance of the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800676 * BrowserSettings.
677 */
John Reck63bb6872010-12-01 19:29:32 -0800678 public static BrowserSettings getInstance() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800679 if (sSingleton == null ) {
680 sSingleton = new BrowserSettings();
681 }
682 return sSingleton;
683 }
684
685 /*
686 * Package level method for associating the BrowserSettings with TabControl
687 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700688 /* package */void setController(Controller ctrl) {
689 mController = ctrl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800690 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800691 }
692
693 /*
694 * Update all the observers of the object.
695 */
696 /*package*/ void update() {
697 setChanged();
698 notifyObservers();
699 }
700
701 /*package*/ void clearCache(Context context) {
702 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700703 if (mController != null) {
704 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800705 if (current != null) {
706 current.clearCache(true);
707 }
708 }
709 }
710
711 /*package*/ void clearCookies(Context context) {
712 CookieManager.getInstance().removeAllCookie();
713 }
714
715 /* package */void clearHistory(Context context) {
716 ContentResolver resolver = context.getContentResolver();
717 Browser.clearHistory(resolver);
718 Browser.clearSearches(resolver);
719 }
720
721 /* package */ void clearFormData(Context context) {
722 WebViewDatabase.getInstance(context).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700723 if (mController!= null) {
724 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100725 if (currentTopView != null) {
726 currentTopView.clearFormData();
727 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800728 }
729 }
730
731 /*package*/ void clearPasswords(Context context) {
732 WebViewDatabase db = WebViewDatabase.getInstance(context);
733 db.clearUsernamePassword();
734 db.clearHttpAuthUsernamePassword();
735 }
736
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800737 private void updateTabControlSettings() {
738 // Enable/disable the error console.
Michael Kolb8233fac2010-10-26 16:08:53 -0700739 mController.setShouldShowErrorConsole(
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800740 showDebugSettings && showConsole);
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800741 }
742
Nicolas Roard78a98e42009-05-11 13:34:17 +0100743 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100744 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100745 }
746
747 /*package*/ void clearLocationAccess(Context context) {
748 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100749 }
750
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400751 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800752 reset();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500753 SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700754 p.edit().clear().apply();
John Reck035a5642010-12-21 18:51:27 -0800755 PreferenceManager.setDefaultValues(ctx, R.xml.general_preferences, true);
756 PreferenceManager.setDefaultValues(ctx, R.xml.privacy_security_preferences, true);
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500757 PreferenceManager.setDefaultValues(ctx, R.xml.advanced_preferences, true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700758 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700759 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100760 // reset appcache max size
761 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch23da30e2010-10-26 15:18:44 +0100762 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700763 }
764
John Reck33bbb802010-10-26 17:13:42 -0700765 /*package*/ static String getFactoryResetHomeUrl(Context context) {
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700766 String url = context.getResources().getString(R.string.homepage_base);
767 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500768 url = url.replace("{CID}",
769 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700770 }
771 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800772 }
773
The Android Open Source Project0c908882009-03-03 19:32:16 -0800774 // Private constructor that does nothing.
775 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800776 reset();
777 }
778
779 private void reset() {
780 // Private variables for settings
781 // NOTE: these defaults need to be kept in sync with the XML
782 // until the performance of PreferenceManager.setDefaultValues()
783 // is improved.
784 loadsImagesAutomatically = true;
785 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400786 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800787 javaScriptCanOpenWindowsAutomatically = false;
788 showSecurityWarnings = true;
789 rememberPasswords = true;
790 saveFormData = true;
Ben Murdochdc62cb92010-11-23 15:11:29 +0000791 autoFillEnabled = true;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800792 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800793 autoFitPage = true;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800794 loadsPageInOverviewMode = true;
795 showDebugSettings = false;
796 // HTML5 API flags
797 appCacheEnabled = true;
798 databaseEnabled = true;
799 domStorageEnabled = true;
800 geolocationEnabled = true;
801 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800802 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100803
Ben Murdoch23da30e2010-10-26 15:18:44 +0100804 private abstract class AutoFillProfileDbTask<T> extends AsyncTask<T, Void, Void> {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100805 Context mContext;
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100806 AutoFillProfileDatabase mAutoFillProfileDb;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100807 Message mCompleteMessage;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100808
Ben Murdoch23da30e2010-10-26 15:18:44 +0100809 public AutoFillProfileDbTask(Context ctx, Message msg) {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100810 mContext = ctx;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100811 mCompleteMessage = msg;
812 }
813
814 protected void onPostExecute(Void result) {
815 if (mCompleteMessage != null) {
816 mCompleteMessage.sendToTarget();
817 }
818 mAutoFillProfileDb.close();
819 }
820
821 abstract protected Void doInBackground(T... values);
822 }
823
824
825 private class SaveProfileToDbTask extends AutoFillProfileDbTask<AutoFillProfile> {
826 public SaveProfileToDbTask(Context ctx, Message msg) {
827 super(ctx, msg);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100828 }
829
830 protected Void doInBackground(AutoFillProfile... values) {
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100831 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100832 assert autoFillActiveProfileId != NO_AUTOFILL_PROFILE_SET;
833 AutoFillProfile newProfile = values[0];
834 mAutoFillProfileDb.addOrUpdateProfile(autoFillActiveProfileId, newProfile);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100835 return null;
836 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100837 }
838
Ben Murdoch23da30e2010-10-26 15:18:44 +0100839 private class DeleteProfileFromDbTask extends AutoFillProfileDbTask<Integer> {
840 public DeleteProfileFromDbTask(Context ctx, Message msg) {
841 super(ctx, msg);
842 }
843
844 protected Void doInBackground(Integer... values) {
845 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
846 int id = values[0];
847 assert id > 0;
848 mAutoFillProfileDb.dropProfile(id);
849 return null;
850 }
851 }
John Reck63bb6872010-12-01 19:29:32 -0800852
853 @Override
854 public void onSharedPreferenceChanged(
855 SharedPreferences p, String key) {
856 if (PREF_HARDWARE_ACCEL.equals(key)) {
857 hardwareAccelerated = p.getBoolean(PREF_HARDWARE_ACCEL, hardwareAccelerated);
858 } else if (PREF_USER_AGENT.equals(key)) {
859 userAgent = Integer.parseInt(p.getString(PREF_USER_AGENT, "0"));
860 update();
Michael Kolb376b5412010-12-15 11:52:57 -0800861 } else if (PREF_QUICK_CONTROLS.equals(key)) {
862 quickControls = p.getBoolean(PREF_QUICK_CONTROLS, quickControls);
John Reckbafe58a2011-01-11 10:26:02 -0800863 } else if (PREF_MOST_VISITED_HOMEPAGE.equals(key)) {
864 useMostVisitedHomepage = p.getBoolean(PREF_MOST_VISITED_HOMEPAGE, useMostVisitedHomepage);
John Reck63bb6872010-12-01 19:29:32 -0800865 }
866 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800867}