blob: db0f73ed75578566e8baf0e8d14e081b11840d6f [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;
91 private boolean landscapeOnly;
92 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;
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
The Android Open Source Project0c908882009-03-03 19:32:16 -0800192 private TabControl mTabControl;
193
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.
378 for (int i = 0; i < mTabControl.getTabCount(); i++) {
379 mTabControl.getTab(i).revertVoiceSearchMode();
380 }
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);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700415 boolean landscapeOnlyTemp =
416 p.getBoolean("landscape_only", landscapeOnly);
417 if (landscapeOnlyTemp != landscapeOnly) {
418 landscapeOnly = landscapeOnlyTemp;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700419 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800420 useWideViewPort = true; // use wide view port for either setting
421 if (autoFitPage) {
422 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
423 } else {
424 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
425 }
426 defaultTextEncodingName =
427 p.getString(PREF_DEFAULT_TEXT_ENCODING,
428 defaultTextEncodingName);
429
430 showDebugSettings =
431 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
432 // Debug menu items have precidence if the menu is visible
433 if (showDebugSettings) {
434 boolean small_screen = p.getBoolean("small_screen",
435 layoutAlgorithm ==
436 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
437 if (small_screen) {
438 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
439 } else {
440 boolean normal_layout = p.getBoolean("normal_layout",
441 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
442 if (normal_layout) {
443 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
444 } else {
445 layoutAlgorithm =
446 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
447 }
448 }
449 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
450 tracing = p.getBoolean("enable_tracing", tracing);
451 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
452 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800453 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800454 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700455 // JS flags is loaded from DB even if showDebugSettings is false,
456 // so that it can be set once and be effective all the time.
457 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100458
459 // Read the setting for showing/hiding the JS Console always so that should the
460 // user enable debug settings, we already know if we should show the console.
461 // The user will never see the console unless they navigate to about:debug,
462 // regardless of the setting we read here. This setting is only used after debug
463 // is enabled.
464 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700465
Andrei Popescu01068702009-08-03 16:03:24 +0100466 // HTML5 API flags
467 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
468 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
469 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100470 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100471 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100472
Grace Klobad9ee1392009-07-20 11:53:33 -0700473 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800474 }
475
The Android Open Source Project0c908882009-03-03 19:32:16 -0800476 public String getHomePage() {
477 return homeUrl;
478 }
479
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100480 public SearchEngine getSearchEngine() {
481 return searchEngine;
482 }
483
Feng Qianb3c02da2009-06-29 15:58:08 -0700484 public String getJsFlags() {
485 return jsFlags;
486 }
487
Andrei Popescu79e82b72009-07-27 12:01:59 +0100488 public WebStorageSizeManager getWebStorageSizeManager() {
489 return webStorageSizeManager;
490 }
491
The Android Open Source Project0c908882009-03-03 19:32:16 -0800492 public void setHomePage(Context context, String url) {
493 Editor ed = PreferenceManager.
494 getDefaultSharedPreferences(context).edit();
495 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700496 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800497 homeUrl = url;
498 }
499
The Android Open Source Project0c908882009-03-03 19:32:16 -0800500 public WebSettings.TextSize getTextSize() {
501 return textSize;
502 }
503
Grace Kloba2f830682009-06-25 11:08:53 -0700504 public WebSettings.ZoomDensity getDefaultZoom() {
505 return zoomDensity;
506 }
507
The Android Open Source Project0c908882009-03-03 19:32:16 -0800508 public boolean openInBackground() {
509 return openInBackground;
510 }
511
512 public boolean showSecurityWarnings() {
513 return showSecurityWarnings;
514 }
515
516 public boolean isTracing() {
517 return tracing;
518 }
519
520 public boolean isLightTouch() {
521 return lightTouch;
522 }
523
524 public boolean isNavDump() {
525 return navDump;
526 }
527
The Android Open Source Project0c908882009-03-03 19:32:16 -0800528 public boolean showDebugSettings() {
529 return showDebugSettings;
530 }
531
532 public void toggleDebugSettings() {
533 showDebugSettings = !showDebugSettings;
534 navDump = showDebugSettings;
535 update();
536 }
537
Ben Murdoch23da30e2010-10-26 15:18:44 +0100538 public void setAutoFillProfile(Context ctx, AutoFillProfile profile, Message msg) {
539 if (profile != null) {
540 setActiveAutoFillProfileId(ctx, profile.getUniqueId());
541 // Update the AutoFill DB with the new profile.
542 new SaveProfileToDbTask(ctx, msg).execute(profile);
543 } else {
544 // Delete the current profile.
545 new DeleteProfileFromDbTask(ctx, msg).execute(autoFillProfile.getUniqueId());
546 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
547 }
548 autoFillProfile = profile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100549 }
550
551 public AutoFillProfile getAutoFillProfile() {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100552 return autoFillProfile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100553 }
554
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100555 private void setActiveAutoFillProfileId(Context context, int activeProfileId) {
556 autoFillActiveProfileId = activeProfileId;
557 Editor ed = PreferenceManager.
558 getDefaultSharedPreferences(context).edit();
559 ed.putInt(PREF_AUTOFILL_ACTIVE_PROFILE_ID, activeProfileId);
560 ed.apply();
561 }
562
The Android Open Source Project0c908882009-03-03 19:32:16 -0800563 /**
564 * Add a WebSettings object to the list of observers that will be updated
565 * when update() is called.
566 *
567 * @param s A WebSettings object that is strictly tied to the life of a
568 * WebView.
569 */
570 public Observer addObserver(WebSettings s) {
571 Observer old = mWebSettingsToObservers.get(s);
572 if (old != null) {
573 super.deleteObserver(old);
574 }
575 Observer o = new Observer(s);
576 mWebSettingsToObservers.put(s, o);
577 super.addObserver(o);
578 return o;
579 }
580
581 /**
582 * Delete the given WebSettings observer from the list of observers.
583 * @param s The WebSettings object to be deleted.
584 */
585 public void deleteObserver(WebSettings s) {
586 Observer o = mWebSettingsToObservers.get(s);
587 if (o != null) {
588 mWebSettingsToObservers.remove(s);
589 super.deleteObserver(o);
590 }
591 }
592
593 /*
594 * Package level method for obtaining a single app instance of the
595 * BrowserSettings.
596 */
597 /*package*/ static BrowserSettings getInstance() {
598 if (sSingleton == null ) {
599 sSingleton = new BrowserSettings();
600 }
601 return sSingleton;
602 }
603
604 /*
605 * Package level method for associating the BrowserSettings with TabControl
606 */
607 /* package */void setTabControl(TabControl tabControl) {
608 mTabControl = tabControl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800609 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800610 }
611
612 /*
613 * Update all the observers of the object.
614 */
615 /*package*/ void update() {
616 setChanged();
617 notifyObservers();
618 }
619
620 /*package*/ void clearCache(Context context) {
621 WebIconDatabase.getInstance().removeAllIcons();
622 if (mTabControl != null) {
623 WebView current = mTabControl.getCurrentWebView();
624 if (current != null) {
625 current.clearCache(true);
626 }
627 }
628 }
629
630 /*package*/ void clearCookies(Context context) {
631 CookieManager.getInstance().removeAllCookie();
632 }
633
634 /* package */void clearHistory(Context context) {
635 ContentResolver resolver = context.getContentResolver();
636 Browser.clearHistory(resolver);
637 Browser.clearSearches(resolver);
638 }
639
640 /* package */ void clearFormData(Context context) {
641 WebViewDatabase.getInstance(context).clearFormData();
642 if (mTabControl != null) {
Henrik Baard794dc722010-02-19 16:28:06 +0100643 WebView currentTopView = mTabControl.getCurrentTopWebView();
644 if (currentTopView != null) {
645 currentTopView.clearFormData();
646 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800647 }
648 }
649
650 /*package*/ void clearPasswords(Context context) {
651 WebViewDatabase db = WebViewDatabase.getInstance(context);
652 db.clearUsernamePassword();
653 db.clearHttpAuthUsernamePassword();
654 }
655
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800656 private void updateTabControlSettings() {
657 // Enable/disable the error console.
658 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
659 showDebugSettings && showConsole);
660 mTabControl.getBrowserActivity().setRequestedOrientation(
661 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
662 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
663 }
664
Nicolas Roard78a98e42009-05-11 13:34:17 +0100665 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100666 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100667 }
668
669 /*package*/ void clearLocationAccess(Context context) {
670 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100671 }
672
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400673 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800674 reset();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500675 SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700676 p.edit().clear().apply();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500677 PreferenceManager.setDefaultValues(ctx, R.xml.page_content_preferences, true);
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500678 PreferenceManager.setDefaultValues(ctx, R.xml.personal_preferences, true);
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500679 PreferenceManager.setDefaultValues(ctx, R.xml.privacy_preferences, true);
680 PreferenceManager.setDefaultValues(ctx, R.xml.security_preferences, true);
681 PreferenceManager.setDefaultValues(ctx, R.xml.advanced_preferences, true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700682 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700683 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100684 // reset appcache max size
685 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch23da30e2010-10-26 15:18:44 +0100686 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700687 }
688
John Reck33bbb802010-10-26 17:13:42 -0700689 /*package*/ static String getFactoryResetHomeUrl(Context context) {
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700690 String url = context.getResources().getString(R.string.homepage_base);
691 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500692 url = url.replace("{CID}",
693 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700694 }
695 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800696 }
697
The Android Open Source Project0c908882009-03-03 19:32:16 -0800698 // Private constructor that does nothing.
699 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800700 reset();
701 }
702
703 private void reset() {
704 // Private variables for settings
705 // NOTE: these defaults need to be kept in sync with the XML
706 // until the performance of PreferenceManager.setDefaultValues()
707 // is improved.
708 loadsImagesAutomatically = true;
709 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400710 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800711 javaScriptCanOpenWindowsAutomatically = false;
712 showSecurityWarnings = true;
713 rememberPasswords = true;
714 saveFormData = true;
Ben Murdochce549fc2010-09-09 10:28:08 +0100715 autoFillEnabled = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800716 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800717 autoFitPage = true;
718 landscapeOnly = false;
719 loadsPageInOverviewMode = true;
720 showDebugSettings = false;
721 // HTML5 API flags
722 appCacheEnabled = true;
723 databaseEnabled = true;
724 domStorageEnabled = true;
725 geolocationEnabled = true;
726 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800727 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100728
Ben Murdoch23da30e2010-10-26 15:18:44 +0100729 private abstract class AutoFillProfileDbTask<T> extends AsyncTask<T, Void, Void> {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100730 Context mContext;
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100731 AutoFillProfileDatabase mAutoFillProfileDb;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100732 Message mCompleteMessage;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100733
Ben Murdoch23da30e2010-10-26 15:18:44 +0100734 public AutoFillProfileDbTask(Context ctx, Message msg) {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100735 mContext = ctx;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100736 mCompleteMessage = msg;
737 }
738
739 protected void onPostExecute(Void result) {
740 if (mCompleteMessage != null) {
741 mCompleteMessage.sendToTarget();
742 }
743 mAutoFillProfileDb.close();
744 }
745
746 abstract protected Void doInBackground(T... values);
747 }
748
749
750 private class SaveProfileToDbTask extends AutoFillProfileDbTask<AutoFillProfile> {
751 public SaveProfileToDbTask(Context ctx, Message msg) {
752 super(ctx, msg);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100753 }
754
755 protected Void doInBackground(AutoFillProfile... values) {
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100756 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100757 assert autoFillActiveProfileId != NO_AUTOFILL_PROFILE_SET;
758 AutoFillProfile newProfile = values[0];
759 mAutoFillProfileDb.addOrUpdateProfile(autoFillActiveProfileId, newProfile);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100760 return null;
761 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100762 }
763
Ben Murdoch23da30e2010-10-26 15:18:44 +0100764 private class DeleteProfileFromDbTask extends AutoFillProfileDbTask<Integer> {
765 public DeleteProfileFromDbTask(Context ctx, Message msg) {
766 super(ctx, msg);
767 }
768
769 protected Void doInBackground(Integer... values) {
770 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
771 int id = values[0];
772 assert id > 0;
773 mAutoFillProfileDb.dropProfile(id);
774 return null;
775 }
776 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800777}