blob: 7be334572064d7b8e11cd51682b9e0a88dbb68e7 [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);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100284
Andrei Popescu01068702009-08-03 16:03:24 +0100285 // HTML5 API flags
286 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100287 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100288 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100289 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100290 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100291
Andrei Popescu01068702009-08-03 16:03:24 +0100292 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100293 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100294 s.setAppCachePath(b.appCachePath);
295 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100296 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100297
Ben Murdoch0cb81892010-10-08 12:41:33 +0100298 // Active AutoFill profile data.
Ben Murdoch23da30e2010-10-26 15:18:44 +0100299 s.setAutoFillProfile(b.autoFillProfile);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100300
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800301 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800302 }
303 }
304
305 /**
Ben Murdochef671652010-11-25 17:17:58 +0000306 * Load settings from the browser app's database. It is performed in
307 * an AsyncTask as it involves plenty of slow disk IO.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800308 * NOTE: Strings used for the preferences must match those specified
Jeff Hamilton175ab302010-10-04 12:53:49 -0500309 * in the various preference XML files.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800310 * @param ctx A Context object used to query the browser's settings
311 * database. If the database exists, the saved settings will be
312 * stored in this BrowserSettings object. This will update all
313 * observers of this object.
314 */
Ben Murdochef671652010-11-25 17:17:58 +0000315 public void asyncLoadFromDb(final Context ctx) {
316 mLoadFromDbComplete = false;
317 // Run the initial settings load in an AsyncTask as it hits the
318 // disk multiple times through SharedPreferences and SQLite. We
319 // need to be certain though that this has completed before we start
320 // to load pages though, so in the worst case we will block waiting
321 // for it to finish in BrowserActivity.onCreate().
322 new LoadFromDbTask(ctx).execute();
323 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800324
Ben Murdochef671652010-11-25 17:17:58 +0000325 private class LoadFromDbTask extends AsyncTask<Void, Void, Void> {
326 private Context mContext;
327
328 public LoadFromDbTask(Context context) {
329 mContext = context;
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700330 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700331
Ben Murdochef671652010-11-25 17:17:58 +0000332 protected Void doInBackground(Void... unused) {
333 SharedPreferences p =
334 PreferenceManager.getDefaultSharedPreferences(mContext);
335 // Set the default value for the Application Caches path.
336 appCachePath = mContext.getDir("appcache", 0).getPath();
337 // Determine the maximum size of the application cache.
338 webStorageSizeManager = new WebStorageSizeManager(
339 mContext,
340 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
341 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
342 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
343 // Set the default value for the Database path.
344 databasePath = mContext.getDir("databases", 0).getPath();
345 // Set the default value for the Geolocation database path.
346 geolocationDatabasePath = mContext.getDir("geolocation", 0).getPath();
347
John Reckef074262010-12-02 16:09:14 -0800348 if (p.getString(PREF_HOMEPAGE, null) == null) {
Ben Murdochef671652010-11-25 17:17:58 +0000349 // No home page preferences is set, set it to default.
350 setHomePage(mContext, getFactoryResetHomeUrl(mContext));
351 }
352
353 // the cost of one cached page is ~3M (measured using nytimes.com). For
354 // low end devices, we only cache one page. For high end devices, we try
355 // to cache more pages, currently choose 5.
356 ActivityManager am = (ActivityManager) mContext
357 .getSystemService(Context.ACTIVITY_SERVICE);
358 if (am.getMemoryClass() > 16) {
359 pageCacheCapacity = 5;
360 } else {
361 pageCacheCapacity = 1;
362 }
363
364 // Read the last active AutoFill profile id.
365 autoFillActiveProfileId = p.getInt(
366 PREF_AUTOFILL_ACTIVE_PROFILE_ID, autoFillActiveProfileId);
367
368 // Load the autofill profile data from the database. We use a database separate
369 // to the browser preference DB to make it easier to support multiple profiles
370 // and switching between them.
371 AutoFillProfileDatabase autoFillDb = AutoFillProfileDatabase.getInstance(mContext);
372 Cursor c = autoFillDb.getProfile(autoFillActiveProfileId);
373
374 if (c.getCount() > 0) {
375 c.moveToFirst();
376
377 String fullName = c.getString(c.getColumnIndex(
378 AutoFillProfileDatabase.Profiles.FULL_NAME));
379 String email = c.getString(c.getColumnIndex(
380 AutoFillProfileDatabase.Profiles.EMAIL_ADDRESS));
381 String company = c.getString(c.getColumnIndex(
382 AutoFillProfileDatabase.Profiles.COMPANY_NAME));
383 String addressLine1 = c.getString(c.getColumnIndex(
384 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_1));
385 String addressLine2 = c.getString(c.getColumnIndex(
386 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_2));
387 String city = c.getString(c.getColumnIndex(
388 AutoFillProfileDatabase.Profiles.CITY));
389 String state = c.getString(c.getColumnIndex(
390 AutoFillProfileDatabase.Profiles.STATE));
391 String zip = c.getString(c.getColumnIndex(
392 AutoFillProfileDatabase.Profiles.ZIP_CODE));
393 String country = c.getString(c.getColumnIndex(
394 AutoFillProfileDatabase.Profiles.COUNTRY));
395 String phone = c.getString(c.getColumnIndex(
396 AutoFillProfileDatabase.Profiles.PHONE_NUMBER));
397 autoFillProfile = new AutoFillProfile(autoFillActiveProfileId,
398 fullName, email, company, addressLine1, addressLine2, city,
399 state, zip, country, phone);
400 }
401 c.close();
402 autoFillDb.close();
403
404 // PreferenceManager.setDefaultValues is TOO SLOW, need to manually keep
405 // the defaults in sync
John Reck63bb6872010-12-01 19:29:32 -0800406 p.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
Ben Murdochef671652010-11-25 17:17:58 +0000407 syncSharedPreferences(mContext, p);
408
409 synchronized (sSingleton) {
410 mLoadFromDbComplete = true;
411 sSingleton.notify();
412 }
413 return null;
Grace Kloba9804c432009-12-02 11:07:40 -0800414 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800415 }
416
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100417 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700418
The Android Open Source Project0c908882009-03-03 19:32:16 -0800419 homeUrl =
420 p.getString(PREF_HOMEPAGE, homeUrl);
Leon Scroggins III31306602010-09-17 11:10:29 -0400421 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
422 SearchEngine.GOOGLE);
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100423 if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
424 if (searchEngine != null) {
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400425 if (searchEngine.supportsVoiceSearch()) {
426 // One or more tabs could have been in voice search mode.
427 // Clear it, since the new SearchEngine may not support
428 // it, or may handle it differently.
Michael Kolb8233fac2010-10-26 16:08:53 -0700429 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
430 mController.getTabControl().getTab(i).revertVoiceSearchMode();
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400431 }
432 }
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100433 searchEngine.close();
434 }
435 searchEngine = SearchEngines.get(ctx, searchEngineName);
436 }
437 Log.i(TAG, "Selected search engine: " + searchEngine);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700438
The Android Open Source Project0c908882009-03-03 19:32:16 -0800439 loadsImagesAutomatically = p.getBoolean("load_images",
440 loadsImagesAutomatically);
441 javaScriptEnabled = p.getBoolean("enable_javascript",
442 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400443 pluginState = WebSettings.PluginState.valueOf(
444 p.getString("plugin_state", pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800445 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
446 "block_popup_windows",
447 !javaScriptCanOpenWindowsAutomatically);
448 showSecurityWarnings = p.getBoolean("show_security_warnings",
449 showSecurityWarnings);
450 rememberPasswords = p.getBoolean("remember_passwords",
451 rememberPasswords);
452 saveFormData = p.getBoolean("save_formdata",
453 saveFormData);
Ben Murdochaf554522010-09-10 22:09:30 +0100454 autoFillEnabled = p.getBoolean("autofill_enabled", autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800455 boolean accept_cookies = p.getBoolean("accept_cookies",
456 CookieManager.getInstance().acceptCookie());
457 CookieManager.getInstance().setAcceptCookie(accept_cookies);
458 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800459 textSize = WebSettings.TextSize.valueOf(
460 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700461 zoomDensity = WebSettings.ZoomDensity.valueOf(
462 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800463 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700464 loadsPageInOverviewMode = p.getBoolean("load_page",
465 loadsPageInOverviewMode);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800466 useWideViewPort = true; // use wide view port for either setting
467 if (autoFitPage) {
468 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
469 } else {
470 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
471 }
472 defaultTextEncodingName =
473 p.getString(PREF_DEFAULT_TEXT_ENCODING,
474 defaultTextEncodingName);
475
476 showDebugSettings =
477 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
478 // Debug menu items have precidence if the menu is visible
479 if (showDebugSettings) {
480 boolean small_screen = p.getBoolean("small_screen",
481 layoutAlgorithm ==
482 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
483 if (small_screen) {
484 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
485 } else {
486 boolean normal_layout = p.getBoolean("normal_layout",
487 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
488 if (normal_layout) {
489 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
490 } else {
491 layoutAlgorithm =
492 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
493 }
494 }
495 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
496 tracing = p.getBoolean("enable_tracing", tracing);
497 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
498 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800499 }
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500500
Michael Kolb376b5412010-12-15 11:52:57 -0800501 quickControls = p.getBoolean(PREF_QUICK_CONTROLS, quickControls);
John Reckbafe58a2011-01-11 10:26:02 -0800502 useMostVisitedHomepage = p.getBoolean(PREF_MOST_VISITED_HOMEPAGE, useMostVisitedHomepage);
Michael Kolb376b5412010-12-15 11:52:57 -0800503
John Reck63bb6872010-12-01 19:29:32 -0800504 // Only set these on startup if it is a dev build
505 if (DEV_BUILD) {
506 userAgent = Integer.parseInt(p.getString(PREF_USER_AGENT, "0"));
507 hardwareAccelerated = p.getBoolean(PREF_HARDWARE_ACCEL, hardwareAccelerated);
508 }
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500509
Feng Qianb3c02da2009-06-29 15:58:08 -0700510 // JS flags is loaded from DB even if showDebugSettings is false,
511 // so that it can be set once and be effective all the time.
512 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100513
514 // Read the setting for showing/hiding the JS Console always so that should the
515 // user enable debug settings, we already know if we should show the console.
516 // The user will never see the console unless they navigate to about:debug,
517 // regardless of the setting we read here. This setting is only used after debug
518 // is enabled.
519 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700520
Andrei Popescu01068702009-08-03 16:03:24 +0100521 // HTML5 API flags
522 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
523 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
524 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100525 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100526 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100527
Grace Klobad9ee1392009-07-20 11:53:33 -0700528 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800529 }
530
The Android Open Source Project0c908882009-03-03 19:32:16 -0800531 public String getHomePage() {
John Reckbafe58a2011-01-11 10:26:02 -0800532 if (useMostVisitedHomepage) {
533 return HomeProvider.MOST_VISITED;
534 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800535 return homeUrl;
536 }
537
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100538 public SearchEngine getSearchEngine() {
539 return searchEngine;
540 }
541
Feng Qianb3c02da2009-06-29 15:58:08 -0700542 public String getJsFlags() {
543 return jsFlags;
544 }
545
Andrei Popescu79e82b72009-07-27 12:01:59 +0100546 public WebStorageSizeManager getWebStorageSizeManager() {
547 return webStorageSizeManager;
548 }
549
The Android Open Source Project0c908882009-03-03 19:32:16 -0800550 public void setHomePage(Context context, String url) {
551 Editor ed = PreferenceManager.
552 getDefaultSharedPreferences(context).edit();
553 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700554 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800555 homeUrl = url;
556 }
557
The Android Open Source Project0c908882009-03-03 19:32:16 -0800558 public WebSettings.TextSize getTextSize() {
559 return textSize;
560 }
561
Grace Kloba2f830682009-06-25 11:08:53 -0700562 public WebSettings.ZoomDensity getDefaultZoom() {
563 return zoomDensity;
564 }
565
The Android Open Source Project0c908882009-03-03 19:32:16 -0800566 public boolean openInBackground() {
567 return openInBackground;
568 }
569
570 public boolean showSecurityWarnings() {
571 return showSecurityWarnings;
572 }
573
574 public boolean isTracing() {
575 return tracing;
576 }
577
578 public boolean isLightTouch() {
579 return lightTouch;
580 }
581
582 public boolean isNavDump() {
583 return navDump;
584 }
585
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500586 public boolean isHardwareAccelerated() {
587 return hardwareAccelerated;
588 }
589
Michael Kolb376b5412010-12-15 11:52:57 -0800590 public boolean useQuickControls() {
591 return quickControls;
592 }
593
John Reckbafe58a2011-01-11 10:26:02 -0800594 public boolean useMostVisitedHomepage() {
595 return useMostVisitedHomepage;
596 }
597
The Android Open Source Project0c908882009-03-03 19:32:16 -0800598 public boolean showDebugSettings() {
599 return showDebugSettings;
600 }
601
602 public void toggleDebugSettings() {
603 showDebugSettings = !showDebugSettings;
604 navDump = showDebugSettings;
605 update();
606 }
607
Ben Murdoch23da30e2010-10-26 15:18:44 +0100608 public void setAutoFillProfile(Context ctx, AutoFillProfile profile, Message msg) {
609 if (profile != null) {
610 setActiveAutoFillProfileId(ctx, profile.getUniqueId());
611 // Update the AutoFill DB with the new profile.
612 new SaveProfileToDbTask(ctx, msg).execute(profile);
613 } else {
614 // Delete the current profile.
Ben Murdoche8a28332010-11-17 14:16:21 +0000615 if (autoFillProfile != null) {
616 new DeleteProfileFromDbTask(ctx, msg).execute(autoFillProfile.getUniqueId());
617 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
618 }
Ben Murdoch23da30e2010-10-26 15:18:44 +0100619 }
620 autoFillProfile = profile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100621 }
622
623 public AutoFillProfile getAutoFillProfile() {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100624 return autoFillProfile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100625 }
626
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100627 private void setActiveAutoFillProfileId(Context context, int activeProfileId) {
628 autoFillActiveProfileId = activeProfileId;
629 Editor ed = PreferenceManager.
630 getDefaultSharedPreferences(context).edit();
631 ed.putInt(PREF_AUTOFILL_ACTIVE_PROFILE_ID, activeProfileId);
632 ed.apply();
633 }
634
Ben Murdoch8029a772010-11-16 11:58:21 +0000635 /* package */ void disableAutoFill(Context ctx) {
636 autoFillEnabled = false;
637 Editor ed = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
638 ed.putBoolean(PREF_AUTOFILL_ENABLED, false);
639 ed.apply();
640 }
641
The Android Open Source Project0c908882009-03-03 19:32:16 -0800642 /**
643 * Add a WebSettings object to the list of observers that will be updated
644 * when update() is called.
645 *
646 * @param s A WebSettings object that is strictly tied to the life of a
647 * WebView.
648 */
649 public Observer addObserver(WebSettings s) {
650 Observer old = mWebSettingsToObservers.get(s);
651 if (old != null) {
652 super.deleteObserver(old);
653 }
654 Observer o = new Observer(s);
655 mWebSettingsToObservers.put(s, o);
656 super.addObserver(o);
657 return o;
658 }
659
660 /**
661 * Delete the given WebSettings observer from the list of observers.
662 * @param s The WebSettings object to be deleted.
663 */
664 public void deleteObserver(WebSettings s) {
665 Observer o = mWebSettingsToObservers.get(s);
666 if (o != null) {
667 mWebSettingsToObservers.remove(s);
668 super.deleteObserver(o);
669 }
670 }
671
672 /*
John Reck63bb6872010-12-01 19:29:32 -0800673 * Application level method for obtaining a single app instance of the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674 * BrowserSettings.
675 */
John Reck63bb6872010-12-01 19:29:32 -0800676 public static BrowserSettings getInstance() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800677 if (sSingleton == null ) {
678 sSingleton = new BrowserSettings();
679 }
680 return sSingleton;
681 }
682
683 /*
684 * Package level method for associating the BrowserSettings with TabControl
685 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700686 /* package */void setController(Controller ctrl) {
687 mController = ctrl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800688 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800689 }
690
691 /*
692 * Update all the observers of the object.
693 */
694 /*package*/ void update() {
695 setChanged();
696 notifyObservers();
697 }
698
699 /*package*/ void clearCache(Context context) {
700 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700701 if (mController != null) {
702 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800703 if (current != null) {
704 current.clearCache(true);
705 }
706 }
707 }
708
709 /*package*/ void clearCookies(Context context) {
710 CookieManager.getInstance().removeAllCookie();
711 }
712
713 /* package */void clearHistory(Context context) {
714 ContentResolver resolver = context.getContentResolver();
715 Browser.clearHistory(resolver);
716 Browser.clearSearches(resolver);
717 }
718
719 /* package */ void clearFormData(Context context) {
720 WebViewDatabase.getInstance(context).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700721 if (mController!= null) {
722 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100723 if (currentTopView != null) {
724 currentTopView.clearFormData();
725 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800726 }
727 }
728
729 /*package*/ void clearPasswords(Context context) {
730 WebViewDatabase db = WebViewDatabase.getInstance(context);
731 db.clearUsernamePassword();
732 db.clearHttpAuthUsernamePassword();
733 }
734
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800735 private void updateTabControlSettings() {
736 // Enable/disable the error console.
Michael Kolb8233fac2010-10-26 16:08:53 -0700737 mController.setShouldShowErrorConsole(
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800738 showDebugSettings && showConsole);
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800739 }
740
Nicolas Roard78a98e42009-05-11 13:34:17 +0100741 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100742 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100743 }
744
745 /*package*/ void clearLocationAccess(Context context) {
746 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100747 }
748
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400749 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800750 reset();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500751 SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700752 p.edit().clear().apply();
John Reck035a5642010-12-21 18:51:27 -0800753 PreferenceManager.setDefaultValues(ctx, R.xml.general_preferences, true);
754 PreferenceManager.setDefaultValues(ctx, R.xml.privacy_security_preferences, true);
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500755 PreferenceManager.setDefaultValues(ctx, R.xml.advanced_preferences, true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700756 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700757 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100758 // reset appcache max size
759 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch23da30e2010-10-26 15:18:44 +0100760 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700761 }
762
John Reck33bbb802010-10-26 17:13:42 -0700763 /*package*/ static String getFactoryResetHomeUrl(Context context) {
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700764 String url = context.getResources().getString(R.string.homepage_base);
765 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500766 url = url.replace("{CID}",
767 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700768 }
769 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800770 }
771
The Android Open Source Project0c908882009-03-03 19:32:16 -0800772 // Private constructor that does nothing.
773 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800774 reset();
775 }
776
777 private void reset() {
778 // Private variables for settings
779 // NOTE: these defaults need to be kept in sync with the XML
780 // until the performance of PreferenceManager.setDefaultValues()
781 // is improved.
782 loadsImagesAutomatically = true;
783 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400784 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800785 javaScriptCanOpenWindowsAutomatically = false;
786 showSecurityWarnings = true;
787 rememberPasswords = true;
788 saveFormData = true;
Ben Murdochdc62cb92010-11-23 15:11:29 +0000789 autoFillEnabled = true;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800790 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800791 autoFitPage = true;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800792 loadsPageInOverviewMode = true;
793 showDebugSettings = false;
794 // HTML5 API flags
795 appCacheEnabled = true;
796 databaseEnabled = true;
797 domStorageEnabled = true;
798 geolocationEnabled = true;
799 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800800 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100801
Ben Murdoch23da30e2010-10-26 15:18:44 +0100802 private abstract class AutoFillProfileDbTask<T> extends AsyncTask<T, Void, Void> {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100803 Context mContext;
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100804 AutoFillProfileDatabase mAutoFillProfileDb;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100805 Message mCompleteMessage;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100806
Ben Murdoch23da30e2010-10-26 15:18:44 +0100807 public AutoFillProfileDbTask(Context ctx, Message msg) {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100808 mContext = ctx;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100809 mCompleteMessage = msg;
810 }
811
812 protected void onPostExecute(Void result) {
813 if (mCompleteMessage != null) {
814 mCompleteMessage.sendToTarget();
815 }
816 mAutoFillProfileDb.close();
817 }
818
819 abstract protected Void doInBackground(T... values);
820 }
821
822
823 private class SaveProfileToDbTask extends AutoFillProfileDbTask<AutoFillProfile> {
824 public SaveProfileToDbTask(Context ctx, Message msg) {
825 super(ctx, msg);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100826 }
827
828 protected Void doInBackground(AutoFillProfile... values) {
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100829 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100830 assert autoFillActiveProfileId != NO_AUTOFILL_PROFILE_SET;
831 AutoFillProfile newProfile = values[0];
832 mAutoFillProfileDb.addOrUpdateProfile(autoFillActiveProfileId, newProfile);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100833 return null;
834 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100835 }
836
Ben Murdoch23da30e2010-10-26 15:18:44 +0100837 private class DeleteProfileFromDbTask extends AutoFillProfileDbTask<Integer> {
838 public DeleteProfileFromDbTask(Context ctx, Message msg) {
839 super(ctx, msg);
840 }
841
842 protected Void doInBackground(Integer... values) {
843 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
844 int id = values[0];
845 assert id > 0;
846 mAutoFillProfileDb.dropProfile(id);
847 return null;
848 }
849 }
John Reck63bb6872010-12-01 19:29:32 -0800850
851 @Override
852 public void onSharedPreferenceChanged(
853 SharedPreferences p, String key) {
854 if (PREF_HARDWARE_ACCEL.equals(key)) {
855 hardwareAccelerated = p.getBoolean(PREF_HARDWARE_ACCEL, hardwareAccelerated);
856 } else if (PREF_USER_AGENT.equals(key)) {
857 userAgent = Integer.parseInt(p.getString(PREF_USER_AGENT, "0"));
858 update();
Michael Kolb376b5412010-12-15 11:52:57 -0800859 } else if (PREF_QUICK_CONTROLS.equals(key)) {
860 quickControls = p.getBoolean(PREF_QUICK_CONTROLS, quickControls);
John Reckbafe58a2011-01-11 10:26:02 -0800861 } else if (PREF_MOST_VISITED_HOMEPAGE.equals(key)) {
862 useMostVisitedHomepage = p.getBoolean(PREF_MOST_VISITED_HOMEPAGE, useMostVisitedHomepage);
John Reck63bb6872010-12-01 19:29:32 -0800863 }
864 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800865}