blob: ba2f3fe8503d9f80ceadfe0186295d91f3058b9c [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
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010020import com.android.browser.search.SearchEngine;
21import com.android.browser.search.SearchEngines;
22
Grace Kloba9804c432009-12-02 11:07:40 -080023import android.app.ActivityManager;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010024import android.content.ComponentName;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.content.ContentResolver;
26import android.content.Context;
27import android.content.pm.ActivityInfo;
28import android.content.SharedPreferences;
29import android.content.SharedPreferences.Editor;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010030import android.database.ContentObserver;
Ben Murdoch0cb81892010-10-08 12:41:33 +010031import android.database.Cursor;
Jeff Davidson43610292010-07-16 16:03:58 -070032import android.net.Uri;
Ben Murdoch0cb81892010-10-08 12:41:33 +010033import android.os.AsyncTask;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010034import android.os.Handler;
Ben Murdoch23da30e2010-10-26 15:18:44 +010035import android.os.Message;
Nicolas Roard78a98e42009-05-11 13:34:17 +010036import android.preference.PreferenceActivity;
Ben Murdoch0cb81892010-10-08 12:41:33 +010037import android.preference.PreferenceManager;
Nicolas Roard78a98e42009-05-11 13:34:17 +010038import android.preference.PreferenceScreen;
Ben Murdoch0cb81892010-10-08 12:41:33 +010039import android.provider.Browser;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010040import android.provider.Settings;
41import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080042import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010043import android.webkit.GeolocationPermissions;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010044import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.webkit.WebView;
46import android.webkit.WebViewDatabase;
47import android.webkit.WebIconDatabase;
48import android.webkit.WebSettings;
Ben Murdoch0cb81892010-10-08 12:41:33 +010049import android.webkit.WebSettings.AutoFillProfile;
Nicolas Roard78a98e42009-05-11 13:34:17 +010050import android.webkit.WebStorage;
Ben Murdoch0cb81892010-10-08 12:41:33 +010051import android.widget.Toast;
The Android Open Source Project0c908882009-03-03 19:32:16 -080052
53import java.util.HashMap;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010054import java.util.Map;
55import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080056import java.util.Observable;
57
58/*
59 * Package level class for storing various WebView and Browser settings. To use
60 * this class:
61 * BrowserSettings s = BrowserSettings.getInstance();
62 * s.addObserver(webView.getSettings());
63 * s.loadFromDb(context); // Only needed on app startup
64 * s.javaScriptEnabled = true;
65 * ... // set any other settings
66 * s.update(); // this will update all the observers
67 *
68 * To remove an observer:
69 * s.deleteObserver(webView.getSettings());
70 */
Jeff Hamilton462b8e82010-09-23 14:33:43 -050071public class BrowserSettings extends Observable {
The Android Open Source Project0c908882009-03-03 19:32:16 -080072
Leon Scroggins9ac587d2009-04-20 12:53:46 -040073 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080074 // NOTE: these defaults need to be kept in sync with the XML
75 // until the performance of PreferenceManager.setDefaultValues()
76 // is improved.
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080077 // Note: boolean variables are set inside reset function.
78 private boolean loadsImagesAutomatically;
79 private boolean javaScriptEnabled;
Patrick Scotte536b332010-03-22 10:19:32 -040080 private WebSettings.PluginState pluginState;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080081 private boolean javaScriptCanOpenWindowsAutomatically;
82 private boolean showSecurityWarnings;
83 private boolean rememberPasswords;
84 private boolean saveFormData;
Ben Murdochce549fc2010-09-09 10:28:08 +010085 private boolean autoFillEnabled;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080086 private boolean openInBackground;
The Android Open Source Project0c908882009-03-03 19:32:16 -080087 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070088 private String homeUrl = "";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010089 private SearchEngine searchEngine;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080090 private boolean autoFitPage;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080091 private boolean loadsPageInOverviewMode;
92 private boolean showDebugSettings;
Andrei Popescu01068702009-08-03 16:03:24 +010093 // HTML5 API flags
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080094 private boolean appCacheEnabled;
95 private boolean databaseEnabled;
96 private boolean domStorageEnabled;
97 private boolean geolocationEnabled;
98 private boolean workersEnabled; // only affects V8. JSC does not have a similar setting
Andrei Popescu01068702009-08-03 16:03:24 +010099 // HTML5 API configuration params
100 private long appCacheMaxSize = Long.MAX_VALUE;
101 private String appCachePath; // default value set in loadFromDb().
102 private String databasePath; // default value set in loadFromDb()
Steve Block5029cf02009-08-21 13:16:58 +0100103 private String geolocationDatabasePath; // default value set in loadFromDb()
Andrei Popescu01068702009-08-03 16:03:24 +0100104 private WebStorageSizeManager webStorageSizeManager;
105
106 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800107
Nicolas Roard78a98e42009-05-11 13:34:17 +0100108 private final static String TAG = "BrowserSettings";
109
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110 // Development settings
111 public WebSettings.LayoutAlgorithm layoutAlgorithm =
112 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
113 private boolean useWideViewPort = true;
114 private int userAgent = 0;
115 private boolean tracing = false;
116 private boolean lightTouch = false;
117 private boolean navDump = false;
Derek Sollenberger8de82852010-11-18 19:51:50 -0500118 private boolean hardwareAccelerated = true;
Feng Qianb3c02da2009-06-29 15:58:08 -0700119
Ben Murdochbff2d602009-07-01 20:19:05 +0100120 // By default the error console is shown once the user navigates to about:debug.
121 // The setting can be then toggled from the settings menu.
122 private boolean showConsole = true;
123
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124 // Private preconfigured values
Shimeng (Simon) Wangb4fb25c2010-07-13 15:18:10 -0700125 private static int minimumFontSize = 1;
126 private static int minimumLogicalFontSize = 1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800127 private static int defaultFontSize = 16;
128 private static int defaultFixedFontSize = 13;
129 private static WebSettings.TextSize textSize =
130 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700131 private static WebSettings.ZoomDensity zoomDensity =
132 WebSettings.ZoomDensity.MEDIUM;
Grace Kloba9804c432009-12-02 11:07:40 -0800133 private static int pageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800134
Ben Murdoch0cb81892010-10-08 12:41:33 +0100135
Ben Murdoch23da30e2010-10-26 15:18:44 +0100136 private AutoFillProfile autoFillProfile;
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100137 // Default to zero. In the case no profile is set up, the initial
138 // value will come from the AutoFillSettingsFragment when the user
139 // creates a profile. Otherwise, we'll read the ID of the last used
140 // profile from the prefs db.
141 private int autoFillActiveProfileId;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100142 private static final int NO_AUTOFILL_PROFILE_SET = 0;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100143
The Android Open Source Project0c908882009-03-03 19:32:16 -0800144 // Preference keys that are used outside this class
145 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
146 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
147 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
148 public final static String PREF_HOMEPAGE = "homepage";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100149 public final static String PREF_SEARCH_ENGINE = "search_engine";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800150 public final static String PREF_CLEAR_FORM_DATA =
151 "privacy_clear_form_data";
152 public final static String PREF_CLEAR_PASSWORDS =
153 "privacy_clear_passwords";
154 public final static String PREF_EXTRAS_RESET_DEFAULTS =
155 "reset_default_preferences";
156 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100157 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800158 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700159 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800160 public final static String PREF_DEFAULT_TEXT_ENCODING =
161 "default_text_encoding";
Steve Blockc6f577f2009-08-20 18:11:08 +0100162 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
163 "privacy_clear_geolocation_access";
Ben Murdochaf554522010-09-10 22:09:30 +0100164 public final static String PREF_AUTOFILL_ENABLED = "autofill_enabled";
165 public final static String PREF_AUTOFILL_PROFILE = "autofill_profile";
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100166 public final static String PREF_AUTOFILL_ACTIVE_PROFILE_ID = "autofill_active_profile_id";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800167
168 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700169 "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
170 "like Gecko) Version/5.0 Safari/533.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800171
172 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700173 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
174 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
175
176 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
177 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
178 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
179
180 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
181 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
182 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800183
184 // Value to truncate strings when adding them to a TextView within
185 // a ListView
186 public final static int MAX_TEXTVIEW_LEN = 80;
187
Jeff Davidson43610292010-07-16 16:03:58 -0700188 public static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider";
189
190 public static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/");
191
Michael Kolb8233fac2010-10-26 16:08:53 -0700192 private Controller mController;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800193
194 // Single instance of the BrowserSettings for use in the Browser app.
195 private static BrowserSettings sSingleton;
196
197 // Private map of WebSettings to Observer objects used when deleting an
198 // observer.
199 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
200 new HashMap<WebSettings,Observer>();
201
Ben Murdochef671652010-11-25 17:17:58 +0000202 private boolean mLoadFromDbComplete;
203
204 public void waitForLoadFromDbToComplete() {
205 synchronized (sSingleton) {
206 while (!mLoadFromDbComplete) {
207 try {
208 sSingleton.wait();
209 } catch (InterruptedException e) { }
210 }
211 }
212 }
213
The Android Open Source Project0c908882009-03-03 19:32:16 -0800214 /*
215 * An observer wrapper for updating a WebSettings object with the new
216 * settings after a call to BrowserSettings.update().
217 */
218 static class Observer implements java.util.Observer {
219 // Private WebSettings object that will be updated.
220 private WebSettings mSettings;
221
222 Observer(WebSettings w) {
223 mSettings = w;
224 }
225
226 public void update(Observable o, Object arg) {
227 BrowserSettings b = (BrowserSettings)o;
228 WebSettings s = mSettings;
229
230 s.setLayoutAlgorithm(b.layoutAlgorithm);
231 if (b.userAgent == 0) {
232 // use the default ua string
233 s.setUserAgentString(null);
234 } else if (b.userAgent == 1) {
235 s.setUserAgentString(DESKTOP_USERAGENT);
236 } else if (b.userAgent == 2) {
237 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700238 } else if (b.userAgent == 3) {
239 s.setUserAgentString(IPAD_USERAGENT);
240 } else if (b.userAgent == 4) {
241 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800242 }
243 s.setUseWideViewPort(b.useWideViewPort);
244 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
245 s.setJavaScriptEnabled(b.javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400246 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800247 s.setJavaScriptCanOpenWindowsAutomatically(
248 b.javaScriptCanOpenWindowsAutomatically);
249 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
250 s.setMinimumFontSize(b.minimumFontSize);
251 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
252 s.setDefaultFontSize(b.defaultFontSize);
253 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
254 s.setNavDump(b.navDump);
255 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700256 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800257 s.setLightTouchEnabled(b.lightTouch);
258 s.setSaveFormData(b.saveFormData);
Ben Murdochce549fc2010-09-09 10:28:08 +0100259 s.setAutoFillEnabled(b.autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800260 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700261 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800262 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800263
264 // WebView inside Browser doesn't want initial focus to be set.
265 s.setNeedInitialFocus(false);
266 // Browser supports multiple windows
267 s.setSupportMultipleWindows(true);
Grace Kloba61fc77c2010-06-22 09:57:33 -0700268 // enable smooth transition for better performance during panning or
269 // zooming
270 s.setEnableSmoothTransition(true);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100271
Andrei Popescu01068702009-08-03 16:03:24 +0100272 // HTML5 API flags
273 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100274 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100275 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100276 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100277 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100278
Andrei Popescu01068702009-08-03 16:03:24 +0100279 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100280 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100281 s.setAppCachePath(b.appCachePath);
282 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100283 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100284
Ben Murdoch0cb81892010-10-08 12:41:33 +0100285 // Active AutoFill profile data.
Ben Murdoch23da30e2010-10-26 15:18:44 +0100286 s.setAutoFillProfile(b.autoFillProfile);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100287
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800288 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800289 }
290 }
291
292 /**
Ben Murdochef671652010-11-25 17:17:58 +0000293 * Load settings from the browser app's database. It is performed in
294 * an AsyncTask as it involves plenty of slow disk IO.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800295 * NOTE: Strings used for the preferences must match those specified
Jeff Hamilton175ab302010-10-04 12:53:49 -0500296 * in the various preference XML files.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800297 * @param ctx A Context object used to query the browser's settings
298 * database. If the database exists, the saved settings will be
299 * stored in this BrowserSettings object. This will update all
300 * observers of this object.
301 */
Ben Murdochef671652010-11-25 17:17:58 +0000302 public void asyncLoadFromDb(final Context ctx) {
303 mLoadFromDbComplete = false;
304 // Run the initial settings load in an AsyncTask as it hits the
305 // disk multiple times through SharedPreferences and SQLite. We
306 // need to be certain though that this has completed before we start
307 // to load pages though, so in the worst case we will block waiting
308 // for it to finish in BrowserActivity.onCreate().
309 new LoadFromDbTask(ctx).execute();
310 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800311
Ben Murdochef671652010-11-25 17:17:58 +0000312 private class LoadFromDbTask extends AsyncTask<Void, Void, Void> {
313 private Context mContext;
314
315 public LoadFromDbTask(Context context) {
316 mContext = context;
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700317 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700318
Ben Murdochef671652010-11-25 17:17:58 +0000319 protected Void doInBackground(Void... unused) {
320 SharedPreferences p =
321 PreferenceManager.getDefaultSharedPreferences(mContext);
322 // Set the default value for the Application Caches path.
323 appCachePath = mContext.getDir("appcache", 0).getPath();
324 // Determine the maximum size of the application cache.
325 webStorageSizeManager = new WebStorageSizeManager(
326 mContext,
327 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
328 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
329 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
330 // Set the default value for the Database path.
331 databasePath = mContext.getDir("databases", 0).getPath();
332 // Set the default value for the Geolocation database path.
333 geolocationDatabasePath = mContext.getDir("geolocation", 0).getPath();
334
335 if (p.getString(PREF_HOMEPAGE, "") == "") {
336 // No home page preferences is set, set it to default.
337 setHomePage(mContext, getFactoryResetHomeUrl(mContext));
338 }
339
340 // the cost of one cached page is ~3M (measured using nytimes.com). For
341 // low end devices, we only cache one page. For high end devices, we try
342 // to cache more pages, currently choose 5.
343 ActivityManager am = (ActivityManager) mContext
344 .getSystemService(Context.ACTIVITY_SERVICE);
345 if (am.getMemoryClass() > 16) {
346 pageCacheCapacity = 5;
347 } else {
348 pageCacheCapacity = 1;
349 }
350
351 // Read the last active AutoFill profile id.
352 autoFillActiveProfileId = p.getInt(
353 PREF_AUTOFILL_ACTIVE_PROFILE_ID, autoFillActiveProfileId);
354
355 // Load the autofill profile data from the database. We use a database separate
356 // to the browser preference DB to make it easier to support multiple profiles
357 // and switching between them.
358 AutoFillProfileDatabase autoFillDb = AutoFillProfileDatabase.getInstance(mContext);
359 Cursor c = autoFillDb.getProfile(autoFillActiveProfileId);
360
361 if (c.getCount() > 0) {
362 c.moveToFirst();
363
364 String fullName = c.getString(c.getColumnIndex(
365 AutoFillProfileDatabase.Profiles.FULL_NAME));
366 String email = c.getString(c.getColumnIndex(
367 AutoFillProfileDatabase.Profiles.EMAIL_ADDRESS));
368 String company = c.getString(c.getColumnIndex(
369 AutoFillProfileDatabase.Profiles.COMPANY_NAME));
370 String addressLine1 = c.getString(c.getColumnIndex(
371 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_1));
372 String addressLine2 = c.getString(c.getColumnIndex(
373 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_2));
374 String city = c.getString(c.getColumnIndex(
375 AutoFillProfileDatabase.Profiles.CITY));
376 String state = c.getString(c.getColumnIndex(
377 AutoFillProfileDatabase.Profiles.STATE));
378 String zip = c.getString(c.getColumnIndex(
379 AutoFillProfileDatabase.Profiles.ZIP_CODE));
380 String country = c.getString(c.getColumnIndex(
381 AutoFillProfileDatabase.Profiles.COUNTRY));
382 String phone = c.getString(c.getColumnIndex(
383 AutoFillProfileDatabase.Profiles.PHONE_NUMBER));
384 autoFillProfile = new AutoFillProfile(autoFillActiveProfileId,
385 fullName, email, company, addressLine1, addressLine2, city,
386 state, zip, country, phone);
387 }
388 c.close();
389 autoFillDb.close();
390
391 // PreferenceManager.setDefaultValues is TOO SLOW, need to manually keep
392 // the defaults in sync
393 syncSharedPreferences(mContext, p);
394
395 synchronized (sSingleton) {
396 mLoadFromDbComplete = true;
397 sSingleton.notify();
398 }
399 return null;
Grace Kloba9804c432009-12-02 11:07:40 -0800400 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800401 }
402
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100403 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700404
The Android Open Source Project0c908882009-03-03 19:32:16 -0800405 homeUrl =
406 p.getString(PREF_HOMEPAGE, homeUrl);
Leon Scroggins III31306602010-09-17 11:10:29 -0400407 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
408 SearchEngine.GOOGLE);
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100409 if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
410 if (searchEngine != null) {
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400411 if (searchEngine.supportsVoiceSearch()) {
412 // One or more tabs could have been in voice search mode.
413 // Clear it, since the new SearchEngine may not support
414 // it, or may handle it differently.
Michael Kolb8233fac2010-10-26 16:08:53 -0700415 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
416 mController.getTabControl().getTab(i).revertVoiceSearchMode();
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400417 }
418 }
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100419 searchEngine.close();
420 }
421 searchEngine = SearchEngines.get(ctx, searchEngineName);
422 }
423 Log.i(TAG, "Selected search engine: " + searchEngine);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700424
The Android Open Source Project0c908882009-03-03 19:32:16 -0800425 loadsImagesAutomatically = p.getBoolean("load_images",
426 loadsImagesAutomatically);
427 javaScriptEnabled = p.getBoolean("enable_javascript",
428 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400429 pluginState = WebSettings.PluginState.valueOf(
430 p.getString("plugin_state", pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800431 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
432 "block_popup_windows",
433 !javaScriptCanOpenWindowsAutomatically);
434 showSecurityWarnings = p.getBoolean("show_security_warnings",
435 showSecurityWarnings);
436 rememberPasswords = p.getBoolean("remember_passwords",
437 rememberPasswords);
438 saveFormData = p.getBoolean("save_formdata",
439 saveFormData);
Ben Murdochaf554522010-09-10 22:09:30 +0100440 autoFillEnabled = p.getBoolean("autofill_enabled", autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800441 boolean accept_cookies = p.getBoolean("accept_cookies",
442 CookieManager.getInstance().acceptCookie());
443 CookieManager.getInstance().setAcceptCookie(accept_cookies);
444 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800445 textSize = WebSettings.TextSize.valueOf(
446 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700447 zoomDensity = WebSettings.ZoomDensity.valueOf(
448 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800449 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700450 loadsPageInOverviewMode = p.getBoolean("load_page",
451 loadsPageInOverviewMode);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800452 useWideViewPort = true; // use wide view port for either setting
453 if (autoFitPage) {
454 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
455 } else {
456 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
457 }
458 defaultTextEncodingName =
459 p.getString(PREF_DEFAULT_TEXT_ENCODING,
460 defaultTextEncodingName);
461
462 showDebugSettings =
463 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
464 // Debug menu items have precidence if the menu is visible
465 if (showDebugSettings) {
466 boolean small_screen = p.getBoolean("small_screen",
467 layoutAlgorithm ==
468 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
469 if (small_screen) {
470 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
471 } else {
472 boolean normal_layout = p.getBoolean("normal_layout",
473 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
474 if (normal_layout) {
475 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
476 } else {
477 layoutAlgorithm =
478 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
479 }
480 }
481 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
482 tracing = p.getBoolean("enable_tracing", tracing);
483 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
484 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800485 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800486 }
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500487
488 // This setting can only be modified when the debug settings have been
489 // enabled but it is read and used by the browser at startup so we must
490 // initialize it regardless of the status of the debug settings.
491 hardwareAccelerated = p.getBoolean("enable_hardware_accel", hardwareAccelerated);
492
Feng Qianb3c02da2009-06-29 15:58:08 -0700493 // JS flags is loaded from DB even if showDebugSettings is false,
494 // so that it can be set once and be effective all the time.
495 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100496
497 // Read the setting for showing/hiding the JS Console always so that should the
498 // user enable debug settings, we already know if we should show the console.
499 // The user will never see the console unless they navigate to about:debug,
500 // regardless of the setting we read here. This setting is only used after debug
501 // is enabled.
502 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700503
Andrei Popescu01068702009-08-03 16:03:24 +0100504 // HTML5 API flags
505 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
506 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
507 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100508 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100509 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100510
Grace Klobad9ee1392009-07-20 11:53:33 -0700511 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800512 }
513
The Android Open Source Project0c908882009-03-03 19:32:16 -0800514 public String getHomePage() {
515 return homeUrl;
516 }
517
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100518 public SearchEngine getSearchEngine() {
519 return searchEngine;
520 }
521
Feng Qianb3c02da2009-06-29 15:58:08 -0700522 public String getJsFlags() {
523 return jsFlags;
524 }
525
Andrei Popescu79e82b72009-07-27 12:01:59 +0100526 public WebStorageSizeManager getWebStorageSizeManager() {
527 return webStorageSizeManager;
528 }
529
The Android Open Source Project0c908882009-03-03 19:32:16 -0800530 public void setHomePage(Context context, String url) {
531 Editor ed = PreferenceManager.
532 getDefaultSharedPreferences(context).edit();
533 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700534 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800535 homeUrl = url;
536 }
537
The Android Open Source Project0c908882009-03-03 19:32:16 -0800538 public WebSettings.TextSize getTextSize() {
539 return textSize;
540 }
541
Grace Kloba2f830682009-06-25 11:08:53 -0700542 public WebSettings.ZoomDensity getDefaultZoom() {
543 return zoomDensity;
544 }
545
The Android Open Source Project0c908882009-03-03 19:32:16 -0800546 public boolean openInBackground() {
547 return openInBackground;
548 }
549
550 public boolean showSecurityWarnings() {
551 return showSecurityWarnings;
552 }
553
554 public boolean isTracing() {
555 return tracing;
556 }
557
558 public boolean isLightTouch() {
559 return lightTouch;
560 }
561
562 public boolean isNavDump() {
563 return navDump;
564 }
565
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500566 public boolean isHardwareAccelerated() {
567 return hardwareAccelerated;
568 }
569
The Android Open Source Project0c908882009-03-03 19:32:16 -0800570 public boolean showDebugSettings() {
571 return showDebugSettings;
572 }
573
574 public void toggleDebugSettings() {
575 showDebugSettings = !showDebugSettings;
576 navDump = showDebugSettings;
577 update();
578 }
579
Ben Murdoch23da30e2010-10-26 15:18:44 +0100580 public void setAutoFillProfile(Context ctx, AutoFillProfile profile, Message msg) {
581 if (profile != null) {
582 setActiveAutoFillProfileId(ctx, profile.getUniqueId());
583 // Update the AutoFill DB with the new profile.
584 new SaveProfileToDbTask(ctx, msg).execute(profile);
585 } else {
586 // Delete the current profile.
Ben Murdoche8a28332010-11-17 14:16:21 +0000587 if (autoFillProfile != null) {
588 new DeleteProfileFromDbTask(ctx, msg).execute(autoFillProfile.getUniqueId());
589 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
590 }
Ben Murdoch23da30e2010-10-26 15:18:44 +0100591 }
592 autoFillProfile = profile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100593 }
594
595 public AutoFillProfile getAutoFillProfile() {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100596 return autoFillProfile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100597 }
598
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100599 private void setActiveAutoFillProfileId(Context context, int activeProfileId) {
600 autoFillActiveProfileId = activeProfileId;
601 Editor ed = PreferenceManager.
602 getDefaultSharedPreferences(context).edit();
603 ed.putInt(PREF_AUTOFILL_ACTIVE_PROFILE_ID, activeProfileId);
604 ed.apply();
605 }
606
Ben Murdoch8029a772010-11-16 11:58:21 +0000607 /* package */ void disableAutoFill(Context ctx) {
608 autoFillEnabled = false;
609 Editor ed = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
610 ed.putBoolean(PREF_AUTOFILL_ENABLED, false);
611 ed.apply();
612 }
613
The Android Open Source Project0c908882009-03-03 19:32:16 -0800614 /**
615 * Add a WebSettings object to the list of observers that will be updated
616 * when update() is called.
617 *
618 * @param s A WebSettings object that is strictly tied to the life of a
619 * WebView.
620 */
621 public Observer addObserver(WebSettings s) {
622 Observer old = mWebSettingsToObservers.get(s);
623 if (old != null) {
624 super.deleteObserver(old);
625 }
626 Observer o = new Observer(s);
627 mWebSettingsToObservers.put(s, o);
628 super.addObserver(o);
629 return o;
630 }
631
632 /**
633 * Delete the given WebSettings observer from the list of observers.
634 * @param s The WebSettings object to be deleted.
635 */
636 public void deleteObserver(WebSettings s) {
637 Observer o = mWebSettingsToObservers.get(s);
638 if (o != null) {
639 mWebSettingsToObservers.remove(s);
640 super.deleteObserver(o);
641 }
642 }
643
644 /*
645 * Package level method for obtaining a single app instance of the
646 * BrowserSettings.
647 */
648 /*package*/ static BrowserSettings getInstance() {
649 if (sSingleton == null ) {
650 sSingleton = new BrowserSettings();
651 }
652 return sSingleton;
653 }
654
655 /*
656 * Package level method for associating the BrowserSettings with TabControl
657 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700658 /* package */void setController(Controller ctrl) {
659 mController = ctrl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800660 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800661 }
662
663 /*
664 * Update all the observers of the object.
665 */
666 /*package*/ void update() {
667 setChanged();
668 notifyObservers();
669 }
670
671 /*package*/ void clearCache(Context context) {
672 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700673 if (mController != null) {
674 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800675 if (current != null) {
676 current.clearCache(true);
677 }
678 }
679 }
680
681 /*package*/ void clearCookies(Context context) {
682 CookieManager.getInstance().removeAllCookie();
683 }
684
685 /* package */void clearHistory(Context context) {
686 ContentResolver resolver = context.getContentResolver();
687 Browser.clearHistory(resolver);
688 Browser.clearSearches(resolver);
689 }
690
691 /* package */ void clearFormData(Context context) {
692 WebViewDatabase.getInstance(context).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700693 if (mController!= null) {
694 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100695 if (currentTopView != null) {
696 currentTopView.clearFormData();
697 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800698 }
699 }
700
701 /*package*/ void clearPasswords(Context context) {
702 WebViewDatabase db = WebViewDatabase.getInstance(context);
703 db.clearUsernamePassword();
704 db.clearHttpAuthUsernamePassword();
705 }
706
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800707 private void updateTabControlSettings() {
708 // Enable/disable the error console.
Michael Kolb8233fac2010-10-26 16:08:53 -0700709 mController.setShouldShowErrorConsole(
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800710 showDebugSettings && showConsole);
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800711 }
712
Nicolas Roard78a98e42009-05-11 13:34:17 +0100713 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100714 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100715 }
716
717 /*package*/ void clearLocationAccess(Context context) {
718 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100719 }
720
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400721 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800722 reset();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500723 SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700724 p.edit().clear().apply();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500725 PreferenceManager.setDefaultValues(ctx, R.xml.page_content_preferences, true);
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500726 PreferenceManager.setDefaultValues(ctx, R.xml.personal_preferences, true);
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500727 PreferenceManager.setDefaultValues(ctx, R.xml.privacy_preferences, true);
728 PreferenceManager.setDefaultValues(ctx, R.xml.security_preferences, true);
729 PreferenceManager.setDefaultValues(ctx, R.xml.advanced_preferences, true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700730 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700731 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100732 // reset appcache max size
733 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch23da30e2010-10-26 15:18:44 +0100734 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700735 }
736
John Reck33bbb802010-10-26 17:13:42 -0700737 /*package*/ static String getFactoryResetHomeUrl(Context context) {
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700738 String url = context.getResources().getString(R.string.homepage_base);
739 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500740 url = url.replace("{CID}",
741 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700742 }
743 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800744 }
745
The Android Open Source Project0c908882009-03-03 19:32:16 -0800746 // Private constructor that does nothing.
747 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800748 reset();
749 }
750
751 private void reset() {
752 // Private variables for settings
753 // NOTE: these defaults need to be kept in sync with the XML
754 // until the performance of PreferenceManager.setDefaultValues()
755 // is improved.
756 loadsImagesAutomatically = true;
757 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400758 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800759 javaScriptCanOpenWindowsAutomatically = false;
760 showSecurityWarnings = true;
761 rememberPasswords = true;
762 saveFormData = true;
Ben Murdochdc62cb92010-11-23 15:11:29 +0000763 autoFillEnabled = true;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800764 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800765 autoFitPage = true;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800766 loadsPageInOverviewMode = true;
767 showDebugSettings = false;
768 // HTML5 API flags
769 appCacheEnabled = true;
770 databaseEnabled = true;
771 domStorageEnabled = true;
772 geolocationEnabled = true;
773 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800774 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100775
Ben Murdoch23da30e2010-10-26 15:18:44 +0100776 private abstract class AutoFillProfileDbTask<T> extends AsyncTask<T, Void, Void> {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100777 Context mContext;
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100778 AutoFillProfileDatabase mAutoFillProfileDb;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100779 Message mCompleteMessage;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100780
Ben Murdoch23da30e2010-10-26 15:18:44 +0100781 public AutoFillProfileDbTask(Context ctx, Message msg) {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100782 mContext = ctx;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100783 mCompleteMessage = msg;
784 }
785
786 protected void onPostExecute(Void result) {
787 if (mCompleteMessage != null) {
788 mCompleteMessage.sendToTarget();
789 }
790 mAutoFillProfileDb.close();
791 }
792
793 abstract protected Void doInBackground(T... values);
794 }
795
796
797 private class SaveProfileToDbTask extends AutoFillProfileDbTask<AutoFillProfile> {
798 public SaveProfileToDbTask(Context ctx, Message msg) {
799 super(ctx, msg);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100800 }
801
802 protected Void doInBackground(AutoFillProfile... values) {
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100803 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100804 assert autoFillActiveProfileId != NO_AUTOFILL_PROFILE_SET;
805 AutoFillProfile newProfile = values[0];
806 mAutoFillProfileDb.addOrUpdateProfile(autoFillActiveProfileId, newProfile);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100807 return null;
808 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100809 }
810
Ben Murdoch23da30e2010-10-26 15:18:44 +0100811 private class DeleteProfileFromDbTask extends AutoFillProfileDbTask<Integer> {
812 public DeleteProfileFromDbTask(Context ctx, Message msg) {
813 super(ctx, msg);
814 }
815
816 protected Void doInBackground(Integer... values) {
817 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
818 int id = values[0];
819 assert id > 0;
820 mAutoFillProfileDb.dropProfile(id);
821 return null;
822 }
823 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800824}