blob: 693ad004b325a33a785e54b27a3cffb23aeea63c [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 Sollenbergerffa561e2010-11-16 14:19:01 -0500118 private boolean hardwareAccelerated = false;
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
202 /*
203 * An observer wrapper for updating a WebSettings object with the new
204 * settings after a call to BrowserSettings.update().
205 */
206 static class Observer implements java.util.Observer {
207 // Private WebSettings object that will be updated.
208 private WebSettings mSettings;
209
210 Observer(WebSettings w) {
211 mSettings = w;
212 }
213
214 public void update(Observable o, Object arg) {
215 BrowserSettings b = (BrowserSettings)o;
216 WebSettings s = mSettings;
217
218 s.setLayoutAlgorithm(b.layoutAlgorithm);
219 if (b.userAgent == 0) {
220 // use the default ua string
221 s.setUserAgentString(null);
222 } else if (b.userAgent == 1) {
223 s.setUserAgentString(DESKTOP_USERAGENT);
224 } else if (b.userAgent == 2) {
225 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700226 } else if (b.userAgent == 3) {
227 s.setUserAgentString(IPAD_USERAGENT);
228 } else if (b.userAgent == 4) {
229 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800230 }
231 s.setUseWideViewPort(b.useWideViewPort);
232 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
233 s.setJavaScriptEnabled(b.javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400234 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800235 s.setJavaScriptCanOpenWindowsAutomatically(
236 b.javaScriptCanOpenWindowsAutomatically);
237 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
238 s.setMinimumFontSize(b.minimumFontSize);
239 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
240 s.setDefaultFontSize(b.defaultFontSize);
241 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
242 s.setNavDump(b.navDump);
243 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700244 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800245 s.setLightTouchEnabled(b.lightTouch);
246 s.setSaveFormData(b.saveFormData);
Ben Murdochce549fc2010-09-09 10:28:08 +0100247 s.setAutoFillEnabled(b.autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800248 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700249 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800250 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800251
252 // WebView inside Browser doesn't want initial focus to be set.
253 s.setNeedInitialFocus(false);
254 // Browser supports multiple windows
255 s.setSupportMultipleWindows(true);
Grace Kloba61fc77c2010-06-22 09:57:33 -0700256 // enable smooth transition for better performance during panning or
257 // zooming
258 s.setEnableSmoothTransition(true);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100259
Andrei Popescu01068702009-08-03 16:03:24 +0100260 // HTML5 API flags
261 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100262 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100263 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100264 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100265 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100266
Andrei Popescu01068702009-08-03 16:03:24 +0100267 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100268 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100269 s.setAppCachePath(b.appCachePath);
270 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100271 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100272
Ben Murdoch0cb81892010-10-08 12:41:33 +0100273 // Active AutoFill profile data.
Ben Murdoch23da30e2010-10-26 15:18:44 +0100274 s.setAutoFillProfile(b.autoFillProfile);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100275
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800276 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800277 }
278 }
279
280 /**
281 * Load settings from the browser app's database.
282 * NOTE: Strings used for the preferences must match those specified
Jeff Hamilton175ab302010-10-04 12:53:49 -0500283 * in the various preference XML files.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800284 * @param ctx A Context object used to query the browser's settings
285 * database. If the database exists, the saved settings will be
286 * stored in this BrowserSettings object. This will update all
287 * observers of this object.
288 */
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100289 public void loadFromDb(final Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800290 SharedPreferences p =
291 PreferenceManager.getDefaultSharedPreferences(ctx);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100292 // Set the default value for the Application Caches path.
293 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100294 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100295 webStorageSizeManager = new WebStorageSizeManager(
296 ctx,
297 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
298 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100299 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100300 // Set the default value for the Database path.
301 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100302 // Set the default value for the Geolocation database path.
303 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800304
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700305 if (p.getString(PREF_HOMEPAGE, "") == "") {
306 // No home page preferences is set, set it to default.
307 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
308 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700309
Grace Kloba9804c432009-12-02 11:07:40 -0800310 // the cost of one cached page is ~3M (measured using nytimes.com). For
311 // low end devices, we only cache one page. For high end devices, we try
312 // to cache more pages, currently choose 5.
313 ActivityManager am = (ActivityManager) ctx
314 .getSystemService(Context.ACTIVITY_SERVICE);
315 if (am.getMemoryClass() > 16) {
Andrei Popescuba676ef2010-03-29 12:12:55 +0100316 pageCacheCapacity = 5;
Grace Kloba9804c432009-12-02 11:07:40 -0800317 } else {
318 pageCacheCapacity = 1;
319 }
320
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100321 // Read the last active AutoFill profile id.
322 autoFillActiveProfileId = p.getInt(
323 PREF_AUTOFILL_ACTIVE_PROFILE_ID, autoFillActiveProfileId);
324
Ben Murdoch0cb81892010-10-08 12:41:33 +0100325 // Load the autofill profile data from the database. We use a database separate
326 // to the browser preference DB to make it easier to support multiple profiles
327 // and switching between them.
Ben Murdoch0cb81892010-10-08 12:41:33 +0100328 AutoFillProfileDatabase autoFillDb = AutoFillProfileDatabase.getInstance(ctx);
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100329 Cursor c = autoFillDb.getProfile(autoFillActiveProfileId);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100330
331 if (c.getCount() > 0) {
332 c.moveToFirst();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100333
334 String fullName = c.getString(c.getColumnIndex(
335 AutoFillProfileDatabase.Profiles.FULL_NAME));
336 String email = c.getString(c.getColumnIndex(
337 AutoFillProfileDatabase.Profiles.EMAIL_ADDRESS));
338 String company = c.getString(c.getColumnIndex(
339 AutoFillProfileDatabase.Profiles.COMPANY_NAME));
340 String addressLine1 = c.getString(c.getColumnIndex(
341 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_1));
342 String addressLine2 = c.getString(c.getColumnIndex(
343 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_2));
344 String city = c.getString(c.getColumnIndex(
345 AutoFillProfileDatabase.Profiles.CITY));
346 String state = c.getString(c.getColumnIndex(
347 AutoFillProfileDatabase.Profiles.STATE));
348 String zip = c.getString(c.getColumnIndex(
349 AutoFillProfileDatabase.Profiles.ZIP_CODE));
350 String country = c.getString(c.getColumnIndex(
351 AutoFillProfileDatabase.Profiles.COUNTRY));
352 String phone = c.getString(c.getColumnIndex(
353 AutoFillProfileDatabase.Profiles.PHONE_NUMBER));
Ben Murdoch23da30e2010-10-26 15:18:44 +0100354 autoFillProfile = new AutoFillProfile(autoFillActiveProfileId,
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100355 fullName, email, company, addressLine1, addressLine2, city,
356 state, zip, country, phone);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100357 }
358 c.close();
359 autoFillDb.close();
360
Jeff Hamilton175ab302010-10-04 12:53:49 -0500361 // PreferenceManager.setDefaultValues is TOO SLOW, need to manually keep
362 // the defaults in sync
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100363 syncSharedPreferences(ctx, p);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800364 }
365
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100366 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700367
The Android Open Source Project0c908882009-03-03 19:32:16 -0800368 homeUrl =
369 p.getString(PREF_HOMEPAGE, homeUrl);
Leon Scroggins III31306602010-09-17 11:10:29 -0400370 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
371 SearchEngine.GOOGLE);
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100372 if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
373 if (searchEngine != null) {
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400374 if (searchEngine.supportsVoiceSearch()) {
375 // One or more tabs could have been in voice search mode.
376 // Clear it, since the new SearchEngine may not support
377 // it, or may handle it differently.
Michael Kolb8233fac2010-10-26 16:08:53 -0700378 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
379 mController.getTabControl().getTab(i).revertVoiceSearchMode();
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400380 }
381 }
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100382 searchEngine.close();
383 }
384 searchEngine = SearchEngines.get(ctx, searchEngineName);
385 }
386 Log.i(TAG, "Selected search engine: " + searchEngine);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700387
The Android Open Source Project0c908882009-03-03 19:32:16 -0800388 loadsImagesAutomatically = p.getBoolean("load_images",
389 loadsImagesAutomatically);
390 javaScriptEnabled = p.getBoolean("enable_javascript",
391 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400392 pluginState = WebSettings.PluginState.valueOf(
393 p.getString("plugin_state", pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800394 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
395 "block_popup_windows",
396 !javaScriptCanOpenWindowsAutomatically);
397 showSecurityWarnings = p.getBoolean("show_security_warnings",
398 showSecurityWarnings);
399 rememberPasswords = p.getBoolean("remember_passwords",
400 rememberPasswords);
401 saveFormData = p.getBoolean("save_formdata",
402 saveFormData);
Ben Murdochaf554522010-09-10 22:09:30 +0100403 autoFillEnabled = p.getBoolean("autofill_enabled", autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800404 boolean accept_cookies = p.getBoolean("accept_cookies",
405 CookieManager.getInstance().acceptCookie());
406 CookieManager.getInstance().setAcceptCookie(accept_cookies);
407 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800408 textSize = WebSettings.TextSize.valueOf(
409 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700410 zoomDensity = WebSettings.ZoomDensity.valueOf(
411 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800412 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700413 loadsPageInOverviewMode = p.getBoolean("load_page",
414 loadsPageInOverviewMode);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800415 useWideViewPort = true; // use wide view port for either setting
416 if (autoFitPage) {
417 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
418 } else {
419 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
420 }
421 defaultTextEncodingName =
422 p.getString(PREF_DEFAULT_TEXT_ENCODING,
423 defaultTextEncodingName);
424
425 showDebugSettings =
426 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
427 // Debug menu items have precidence if the menu is visible
428 if (showDebugSettings) {
429 boolean small_screen = p.getBoolean("small_screen",
430 layoutAlgorithm ==
431 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
432 if (small_screen) {
433 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
434 } else {
435 boolean normal_layout = p.getBoolean("normal_layout",
436 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
437 if (normal_layout) {
438 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
439 } else {
440 layoutAlgorithm =
441 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
442 }
443 }
444 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
445 tracing = p.getBoolean("enable_tracing", tracing);
446 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
447 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800448 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800449 }
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500450
451 // This setting can only be modified when the debug settings have been
452 // enabled but it is read and used by the browser at startup so we must
453 // initialize it regardless of the status of the debug settings.
454 hardwareAccelerated = p.getBoolean("enable_hardware_accel", hardwareAccelerated);
455
Feng Qianb3c02da2009-06-29 15:58:08 -0700456 // JS flags is loaded from DB even if showDebugSettings is false,
457 // so that it can be set once and be effective all the time.
458 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100459
460 // Read the setting for showing/hiding the JS Console always so that should the
461 // user enable debug settings, we already know if we should show the console.
462 // The user will never see the console unless they navigate to about:debug,
463 // regardless of the setting we read here. This setting is only used after debug
464 // is enabled.
465 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700466
Andrei Popescu01068702009-08-03 16:03:24 +0100467 // HTML5 API flags
468 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
469 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
470 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100471 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100472 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100473
Grace Klobad9ee1392009-07-20 11:53:33 -0700474 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800475 }
476
The Android Open Source Project0c908882009-03-03 19:32:16 -0800477 public String getHomePage() {
478 return homeUrl;
479 }
480
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100481 public SearchEngine getSearchEngine() {
482 return searchEngine;
483 }
484
Feng Qianb3c02da2009-06-29 15:58:08 -0700485 public String getJsFlags() {
486 return jsFlags;
487 }
488
Andrei Popescu79e82b72009-07-27 12:01:59 +0100489 public WebStorageSizeManager getWebStorageSizeManager() {
490 return webStorageSizeManager;
491 }
492
The Android Open Source Project0c908882009-03-03 19:32:16 -0800493 public void setHomePage(Context context, String url) {
494 Editor ed = PreferenceManager.
495 getDefaultSharedPreferences(context).edit();
496 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700497 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800498 homeUrl = url;
499 }
500
The Android Open Source Project0c908882009-03-03 19:32:16 -0800501 public WebSettings.TextSize getTextSize() {
502 return textSize;
503 }
504
Grace Kloba2f830682009-06-25 11:08:53 -0700505 public WebSettings.ZoomDensity getDefaultZoom() {
506 return zoomDensity;
507 }
508
The Android Open Source Project0c908882009-03-03 19:32:16 -0800509 public boolean openInBackground() {
510 return openInBackground;
511 }
512
513 public boolean showSecurityWarnings() {
514 return showSecurityWarnings;
515 }
516
517 public boolean isTracing() {
518 return tracing;
519 }
520
521 public boolean isLightTouch() {
522 return lightTouch;
523 }
524
525 public boolean isNavDump() {
526 return navDump;
527 }
528
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500529 public boolean isHardwareAccelerated() {
530 return hardwareAccelerated;
531 }
532
The Android Open Source Project0c908882009-03-03 19:32:16 -0800533 public boolean showDebugSettings() {
534 return showDebugSettings;
535 }
536
537 public void toggleDebugSettings() {
538 showDebugSettings = !showDebugSettings;
539 navDump = showDebugSettings;
540 update();
541 }
542
Ben Murdoch23da30e2010-10-26 15:18:44 +0100543 public void setAutoFillProfile(Context ctx, AutoFillProfile profile, Message msg) {
544 if (profile != null) {
545 setActiveAutoFillProfileId(ctx, profile.getUniqueId());
546 // Update the AutoFill DB with the new profile.
547 new SaveProfileToDbTask(ctx, msg).execute(profile);
548 } else {
549 // Delete the current profile.
550 new DeleteProfileFromDbTask(ctx, msg).execute(autoFillProfile.getUniqueId());
551 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
552 }
553 autoFillProfile = profile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100554 }
555
556 public AutoFillProfile getAutoFillProfile() {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100557 return autoFillProfile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100558 }
559
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100560 private void setActiveAutoFillProfileId(Context context, int activeProfileId) {
561 autoFillActiveProfileId = activeProfileId;
562 Editor ed = PreferenceManager.
563 getDefaultSharedPreferences(context).edit();
564 ed.putInt(PREF_AUTOFILL_ACTIVE_PROFILE_ID, activeProfileId);
565 ed.apply();
566 }
567
The Android Open Source Project0c908882009-03-03 19:32:16 -0800568 /**
569 * Add a WebSettings object to the list of observers that will be updated
570 * when update() is called.
571 *
572 * @param s A WebSettings object that is strictly tied to the life of a
573 * WebView.
574 */
575 public Observer addObserver(WebSettings s) {
576 Observer old = mWebSettingsToObservers.get(s);
577 if (old != null) {
578 super.deleteObserver(old);
579 }
580 Observer o = new Observer(s);
581 mWebSettingsToObservers.put(s, o);
582 super.addObserver(o);
583 return o;
584 }
585
586 /**
587 * Delete the given WebSettings observer from the list of observers.
588 * @param s The WebSettings object to be deleted.
589 */
590 public void deleteObserver(WebSettings s) {
591 Observer o = mWebSettingsToObservers.get(s);
592 if (o != null) {
593 mWebSettingsToObservers.remove(s);
594 super.deleteObserver(o);
595 }
596 }
597
598 /*
599 * Package level method for obtaining a single app instance of the
600 * BrowserSettings.
601 */
602 /*package*/ static BrowserSettings getInstance() {
603 if (sSingleton == null ) {
604 sSingleton = new BrowserSettings();
605 }
606 return sSingleton;
607 }
608
609 /*
610 * Package level method for associating the BrowserSettings with TabControl
611 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700612 /* package */void setController(Controller ctrl) {
613 mController = ctrl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800614 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800615 }
616
617 /*
618 * Update all the observers of the object.
619 */
620 /*package*/ void update() {
621 setChanged();
622 notifyObservers();
623 }
624
625 /*package*/ void clearCache(Context context) {
626 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700627 if (mController != null) {
628 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800629 if (current != null) {
630 current.clearCache(true);
631 }
632 }
633 }
634
635 /*package*/ void clearCookies(Context context) {
636 CookieManager.getInstance().removeAllCookie();
637 }
638
639 /* package */void clearHistory(Context context) {
640 ContentResolver resolver = context.getContentResolver();
641 Browser.clearHistory(resolver);
642 Browser.clearSearches(resolver);
643 }
644
645 /* package */ void clearFormData(Context context) {
646 WebViewDatabase.getInstance(context).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700647 if (mController!= null) {
648 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100649 if (currentTopView != null) {
650 currentTopView.clearFormData();
651 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800652 }
653 }
654
655 /*package*/ void clearPasswords(Context context) {
656 WebViewDatabase db = WebViewDatabase.getInstance(context);
657 db.clearUsernamePassword();
658 db.clearHttpAuthUsernamePassword();
659 }
660
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800661 private void updateTabControlSettings() {
662 // Enable/disable the error console.
Michael Kolb8233fac2010-10-26 16:08:53 -0700663 mController.setShouldShowErrorConsole(
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800664 showDebugSettings && showConsole);
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800665 }
666
Nicolas Roard78a98e42009-05-11 13:34:17 +0100667 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100668 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100669 }
670
671 /*package*/ void clearLocationAccess(Context context) {
672 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100673 }
674
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400675 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800676 reset();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500677 SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700678 p.edit().clear().apply();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500679 PreferenceManager.setDefaultValues(ctx, R.xml.page_content_preferences, true);
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500680 PreferenceManager.setDefaultValues(ctx, R.xml.personal_preferences, true);
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500681 PreferenceManager.setDefaultValues(ctx, R.xml.privacy_preferences, true);
682 PreferenceManager.setDefaultValues(ctx, R.xml.security_preferences, true);
683 PreferenceManager.setDefaultValues(ctx, R.xml.advanced_preferences, true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700684 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700685 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100686 // reset appcache max size
687 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch23da30e2010-10-26 15:18:44 +0100688 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700689 }
690
John Reck33bbb802010-10-26 17:13:42 -0700691 /*package*/ static String getFactoryResetHomeUrl(Context context) {
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700692 String url = context.getResources().getString(R.string.homepage_base);
693 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500694 url = url.replace("{CID}",
695 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700696 }
697 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800698 }
699
The Android Open Source Project0c908882009-03-03 19:32:16 -0800700 // Private constructor that does nothing.
701 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800702 reset();
703 }
704
705 private void reset() {
706 // Private variables for settings
707 // NOTE: these defaults need to be kept in sync with the XML
708 // until the performance of PreferenceManager.setDefaultValues()
709 // is improved.
710 loadsImagesAutomatically = true;
711 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400712 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800713 javaScriptCanOpenWindowsAutomatically = false;
714 showSecurityWarnings = true;
715 rememberPasswords = true;
716 saveFormData = true;
Ben Murdochce549fc2010-09-09 10:28:08 +0100717 autoFillEnabled = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800718 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800719 autoFitPage = true;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800720 loadsPageInOverviewMode = true;
721 showDebugSettings = false;
722 // HTML5 API flags
723 appCacheEnabled = true;
724 databaseEnabled = true;
725 domStorageEnabled = true;
726 geolocationEnabled = true;
727 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800728 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100729
Ben Murdoch23da30e2010-10-26 15:18:44 +0100730 private abstract class AutoFillProfileDbTask<T> extends AsyncTask<T, Void, Void> {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100731 Context mContext;
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100732 AutoFillProfileDatabase mAutoFillProfileDb;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100733 Message mCompleteMessage;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100734
Ben Murdoch23da30e2010-10-26 15:18:44 +0100735 public AutoFillProfileDbTask(Context ctx, Message msg) {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100736 mContext = ctx;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100737 mCompleteMessage = msg;
738 }
739
740 protected void onPostExecute(Void result) {
741 if (mCompleteMessage != null) {
742 mCompleteMessage.sendToTarget();
743 }
744 mAutoFillProfileDb.close();
745 }
746
747 abstract protected Void doInBackground(T... values);
748 }
749
750
751 private class SaveProfileToDbTask extends AutoFillProfileDbTask<AutoFillProfile> {
752 public SaveProfileToDbTask(Context ctx, Message msg) {
753 super(ctx, msg);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100754 }
755
756 protected Void doInBackground(AutoFillProfile... values) {
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100757 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100758 assert autoFillActiveProfileId != NO_AUTOFILL_PROFILE_SET;
759 AutoFillProfile newProfile = values[0];
760 mAutoFillProfileDb.addOrUpdateProfile(autoFillActiveProfileId, newProfile);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100761 return null;
762 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100763 }
764
Ben Murdoch23da30e2010-10-26 15:18:44 +0100765 private class DeleteProfileFromDbTask extends AutoFillProfileDbTask<Integer> {
766 public DeleteProfileFromDbTask(Context ctx, Message msg) {
767 super(ctx, msg);
768 }
769
770 protected Void doInBackground(Integer... values) {
771 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
772 int id = values[0];
773 assert id > 0;
774 mAutoFillProfileDb.dropProfile(id);
775 return null;
776 }
777 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800778}