blob: 3b8aaa4bec47209b3f221c1e35aec783e9a3ecbf [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;
Nicolas Roard78a98e42009-05-11 13:34:17 +010035import android.preference.PreferenceActivity;
Ben Murdoch0cb81892010-10-08 12:41:33 +010036import android.preference.PreferenceManager;
Nicolas Roard78a98e42009-05-11 13:34:17 +010037import android.preference.PreferenceScreen;
Ben Murdoch0cb81892010-10-08 12:41:33 +010038import android.provider.Browser;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010039import android.provider.Settings;
40import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010042import android.webkit.GeolocationPermissions;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010043import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044import android.webkit.WebView;
45import android.webkit.WebViewDatabase;
46import android.webkit.WebIconDatabase;
47import android.webkit.WebSettings;
Ben Murdoch0cb81892010-10-08 12:41:33 +010048import android.webkit.WebSettings.AutoFillProfile;
Nicolas Roard78a98e42009-05-11 13:34:17 +010049import android.webkit.WebStorage;
Ben Murdoch0cb81892010-10-08 12:41:33 +010050import android.widget.Toast;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051
52import java.util.HashMap;
Nicolas Roard99b3ae12009-09-22 15:17:52 +010053import java.util.Map;
54import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080055import java.util.Observable;
56
57/*
58 * Package level class for storing various WebView and Browser settings. To use
59 * this class:
60 * BrowserSettings s = BrowserSettings.getInstance();
61 * s.addObserver(webView.getSettings());
62 * s.loadFromDb(context); // Only needed on app startup
63 * s.javaScriptEnabled = true;
64 * ... // set any other settings
65 * s.update(); // this will update all the observers
66 *
67 * To remove an observer:
68 * s.deleteObserver(webView.getSettings());
69 */
Jeff Hamilton462b8e82010-09-23 14:33:43 -050070public class BrowserSettings extends Observable {
The Android Open Source Project0c908882009-03-03 19:32:16 -080071
Leon Scroggins9ac587d2009-04-20 12:53:46 -040072 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080073 // NOTE: these defaults need to be kept in sync with the XML
74 // until the performance of PreferenceManager.setDefaultValues()
75 // is improved.
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080076 // Note: boolean variables are set inside reset function.
77 private boolean loadsImagesAutomatically;
78 private boolean javaScriptEnabled;
Patrick Scotte536b332010-03-22 10:19:32 -040079 private WebSettings.PluginState pluginState;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080080 private boolean javaScriptCanOpenWindowsAutomatically;
81 private boolean showSecurityWarnings;
82 private boolean rememberPasswords;
83 private boolean saveFormData;
Ben Murdochce549fc2010-09-09 10:28:08 +010084 private boolean autoFillEnabled;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080085 private boolean openInBackground;
The Android Open Source Project0c908882009-03-03 19:32:16 -080086 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070087 private String homeUrl = "";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010088 private SearchEngine searchEngine;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080089 private boolean autoFitPage;
90 private boolean landscapeOnly;
91 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;
Feng Qianb3c02da2009-06-29 15:58:08 -0700118
Ben Murdochbff2d602009-07-01 20:19:05 +0100119 // By default the error console is shown once the user navigates to about:debug.
120 // The setting can be then toggled from the settings menu.
121 private boolean showConsole = true;
122
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123 // Private preconfigured values
Shimeng (Simon) Wangb4fb25c2010-07-13 15:18:10 -0700124 private static int minimumFontSize = 1;
125 private static int minimumLogicalFontSize = 1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800126 private static int defaultFontSize = 16;
127 private static int defaultFixedFontSize = 13;
128 private static WebSettings.TextSize textSize =
129 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700130 private static WebSettings.ZoomDensity zoomDensity =
131 WebSettings.ZoomDensity.MEDIUM;
Grace Kloba9804c432009-12-02 11:07:40 -0800132 private static int pageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800133
Ben Murdoch0cb81892010-10-08 12:41:33 +0100134
135 private AutoFillProfile mAutoFillProfile;
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100136 // Default to zero. In the case no profile is set up, the initial
137 // value will come from the AutoFillSettingsFragment when the user
138 // creates a profile. Otherwise, we'll read the ID of the last used
139 // profile from the prefs db.
140 private int autoFillActiveProfileId;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100141
The Android Open Source Project0c908882009-03-03 19:32:16 -0800142 // Preference keys that are used outside this class
143 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
144 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
145 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
146 public final static String PREF_HOMEPAGE = "homepage";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100147 public final static String PREF_SEARCH_ENGINE = "search_engine";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800148 public final static String PREF_CLEAR_FORM_DATA =
149 "privacy_clear_form_data";
150 public final static String PREF_CLEAR_PASSWORDS =
151 "privacy_clear_passwords";
152 public final static String PREF_EXTRAS_RESET_DEFAULTS =
153 "reset_default_preferences";
154 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100155 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800156 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700157 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800158 public final static String PREF_DEFAULT_TEXT_ENCODING =
159 "default_text_encoding";
Steve Blockc6f577f2009-08-20 18:11:08 +0100160 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
161 "privacy_clear_geolocation_access";
Ben Murdochaf554522010-09-10 22:09:30 +0100162 public final static String PREF_AUTOFILL_ENABLED = "autofill_enabled";
163 public final static String PREF_AUTOFILL_PROFILE = "autofill_profile";
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100164 public final static String PREF_AUTOFILL_ACTIVE_PROFILE_ID = "autofill_active_profile_id";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800165
166 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700167 "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
168 "like Gecko) Version/5.0 Safari/533.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800169
170 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700171 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
172 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
173
174 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
175 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
176 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
177
178 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
179 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
180 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800181
182 // Value to truncate strings when adding them to a TextView within
183 // a ListView
184 public final static int MAX_TEXTVIEW_LEN = 80;
185
Jeff Davidson43610292010-07-16 16:03:58 -0700186 public static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider";
187
188 public static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/");
189
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 private TabControl mTabControl;
191
192 // Single instance of the BrowserSettings for use in the Browser app.
193 private static BrowserSettings sSingleton;
194
195 // Private map of WebSettings to Observer objects used when deleting an
196 // observer.
197 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
198 new HashMap<WebSettings,Observer>();
199
200 /*
201 * An observer wrapper for updating a WebSettings object with the new
202 * settings after a call to BrowserSettings.update().
203 */
204 static class Observer implements java.util.Observer {
205 // Private WebSettings object that will be updated.
206 private WebSettings mSettings;
207
208 Observer(WebSettings w) {
209 mSettings = w;
210 }
211
212 public void update(Observable o, Object arg) {
213 BrowserSettings b = (BrowserSettings)o;
214 WebSettings s = mSettings;
215
216 s.setLayoutAlgorithm(b.layoutAlgorithm);
217 if (b.userAgent == 0) {
218 // use the default ua string
219 s.setUserAgentString(null);
220 } else if (b.userAgent == 1) {
221 s.setUserAgentString(DESKTOP_USERAGENT);
222 } else if (b.userAgent == 2) {
223 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700224 } else if (b.userAgent == 3) {
225 s.setUserAgentString(IPAD_USERAGENT);
226 } else if (b.userAgent == 4) {
227 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800228 }
229 s.setUseWideViewPort(b.useWideViewPort);
230 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
231 s.setJavaScriptEnabled(b.javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400232 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800233 s.setJavaScriptCanOpenWindowsAutomatically(
234 b.javaScriptCanOpenWindowsAutomatically);
235 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
236 s.setMinimumFontSize(b.minimumFontSize);
237 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
238 s.setDefaultFontSize(b.defaultFontSize);
239 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
240 s.setNavDump(b.navDump);
241 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700242 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800243 s.setLightTouchEnabled(b.lightTouch);
244 s.setSaveFormData(b.saveFormData);
Ben Murdochce549fc2010-09-09 10:28:08 +0100245 s.setAutoFillEnabled(b.autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800246 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700247 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800248 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800249
250 // WebView inside Browser doesn't want initial focus to be set.
251 s.setNeedInitialFocus(false);
252 // Browser supports multiple windows
253 s.setSupportMultipleWindows(true);
Grace Kloba61fc77c2010-06-22 09:57:33 -0700254 // enable smooth transition for better performance during panning or
255 // zooming
256 s.setEnableSmoothTransition(true);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100257
Andrei Popescu01068702009-08-03 16:03:24 +0100258 // HTML5 API flags
259 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100260 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100261 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100262 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100263 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100264
Andrei Popescu01068702009-08-03 16:03:24 +0100265 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100266 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100267 s.setAppCachePath(b.appCachePath);
268 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100269 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100270
Ben Murdoch0cb81892010-10-08 12:41:33 +0100271 // Active AutoFill profile data.
272 s.setAutoFillProfile(b.mAutoFillProfile);
273
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800274 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800275 }
276 }
277
278 /**
279 * Load settings from the browser app's database.
280 * NOTE: Strings used for the preferences must match those specified
Jeff Hamilton175ab302010-10-04 12:53:49 -0500281 * in the various preference XML files.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800282 * @param ctx A Context object used to query the browser's settings
283 * database. If the database exists, the saved settings will be
284 * stored in this BrowserSettings object. This will update all
285 * observers of this object.
286 */
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100287 public void loadFromDb(final Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800288 SharedPreferences p =
289 PreferenceManager.getDefaultSharedPreferences(ctx);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100290 // Set the default value for the Application Caches path.
291 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100292 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100293 webStorageSizeManager = new WebStorageSizeManager(
294 ctx,
295 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
296 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100297 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100298 // Set the default value for the Database path.
299 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100300 // Set the default value for the Geolocation database path.
301 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800302
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700303 if (p.getString(PREF_HOMEPAGE, "") == "") {
304 // No home page preferences is set, set it to default.
305 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
306 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700307
Grace Kloba9804c432009-12-02 11:07:40 -0800308 // the cost of one cached page is ~3M (measured using nytimes.com). For
309 // low end devices, we only cache one page. For high end devices, we try
310 // to cache more pages, currently choose 5.
311 ActivityManager am = (ActivityManager) ctx
312 .getSystemService(Context.ACTIVITY_SERVICE);
313 if (am.getMemoryClass() > 16) {
Andrei Popescuba676ef2010-03-29 12:12:55 +0100314 pageCacheCapacity = 5;
Grace Kloba9804c432009-12-02 11:07:40 -0800315 } else {
316 pageCacheCapacity = 1;
317 }
318
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100319 // Read the last active AutoFill profile id.
320 autoFillActiveProfileId = p.getInt(
321 PREF_AUTOFILL_ACTIVE_PROFILE_ID, autoFillActiveProfileId);
322
Ben Murdoch0cb81892010-10-08 12:41:33 +0100323 // Load the autofill profile data from the database. We use a database separate
324 // to the browser preference DB to make it easier to support multiple profiles
325 // and switching between them.
Ben Murdoch0cb81892010-10-08 12:41:33 +0100326 AutoFillProfileDatabase autoFillDb = AutoFillProfileDatabase.getInstance(ctx);
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100327 Cursor c = autoFillDb.getProfile(autoFillActiveProfileId);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100328
329 if (c.getCount() > 0) {
330 c.moveToFirst();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100331
332 String fullName = c.getString(c.getColumnIndex(
333 AutoFillProfileDatabase.Profiles.FULL_NAME));
334 String email = c.getString(c.getColumnIndex(
335 AutoFillProfileDatabase.Profiles.EMAIL_ADDRESS));
336 String company = c.getString(c.getColumnIndex(
337 AutoFillProfileDatabase.Profiles.COMPANY_NAME));
338 String addressLine1 = c.getString(c.getColumnIndex(
339 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_1));
340 String addressLine2 = c.getString(c.getColumnIndex(
341 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_2));
342 String city = c.getString(c.getColumnIndex(
343 AutoFillProfileDatabase.Profiles.CITY));
344 String state = c.getString(c.getColumnIndex(
345 AutoFillProfileDatabase.Profiles.STATE));
346 String zip = c.getString(c.getColumnIndex(
347 AutoFillProfileDatabase.Profiles.ZIP_CODE));
348 String country = c.getString(c.getColumnIndex(
349 AutoFillProfileDatabase.Profiles.COUNTRY));
350 String phone = c.getString(c.getColumnIndex(
351 AutoFillProfileDatabase.Profiles.PHONE_NUMBER));
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100352 mAutoFillProfile = new AutoFillProfile(autoFillActiveProfileId,
353 fullName, email, company, addressLine1, addressLine2, city,
354 state, zip, country, phone);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100355 }
356 c.close();
357 autoFillDb.close();
358
Jeff Hamilton175ab302010-10-04 12:53:49 -0500359 // PreferenceManager.setDefaultValues is TOO SLOW, need to manually keep
360 // the defaults in sync
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100361 syncSharedPreferences(ctx, p);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800362 }
363
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100364 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700365
The Android Open Source Project0c908882009-03-03 19:32:16 -0800366 homeUrl =
367 p.getString(PREF_HOMEPAGE, homeUrl);
Leon Scroggins III31306602010-09-17 11:10:29 -0400368 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
369 SearchEngine.GOOGLE);
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100370 if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
371 if (searchEngine != null) {
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400372 if (searchEngine.supportsVoiceSearch()) {
373 // One or more tabs could have been in voice search mode.
374 // Clear it, since the new SearchEngine may not support
375 // it, or may handle it differently.
376 for (int i = 0; i < mTabControl.getTabCount(); i++) {
377 mTabControl.getTab(i).revertVoiceSearchMode();
378 }
379 }
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100380 searchEngine.close();
381 }
382 searchEngine = SearchEngines.get(ctx, searchEngineName);
383 }
384 Log.i(TAG, "Selected search engine: " + searchEngine);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700385
The Android Open Source Project0c908882009-03-03 19:32:16 -0800386 loadsImagesAutomatically = p.getBoolean("load_images",
387 loadsImagesAutomatically);
388 javaScriptEnabled = p.getBoolean("enable_javascript",
389 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400390 pluginState = WebSettings.PluginState.valueOf(
391 p.getString("plugin_state", pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800392 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
393 "block_popup_windows",
394 !javaScriptCanOpenWindowsAutomatically);
395 showSecurityWarnings = p.getBoolean("show_security_warnings",
396 showSecurityWarnings);
397 rememberPasswords = p.getBoolean("remember_passwords",
398 rememberPasswords);
399 saveFormData = p.getBoolean("save_formdata",
400 saveFormData);
Ben Murdochaf554522010-09-10 22:09:30 +0100401 autoFillEnabled = p.getBoolean("autofill_enabled", autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800402 boolean accept_cookies = p.getBoolean("accept_cookies",
403 CookieManager.getInstance().acceptCookie());
404 CookieManager.getInstance().setAcceptCookie(accept_cookies);
405 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800406 textSize = WebSettings.TextSize.valueOf(
407 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700408 zoomDensity = WebSettings.ZoomDensity.valueOf(
409 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800410 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700411 loadsPageInOverviewMode = p.getBoolean("load_page",
412 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700413 boolean landscapeOnlyTemp =
414 p.getBoolean("landscape_only", landscapeOnly);
415 if (landscapeOnlyTemp != landscapeOnly) {
416 landscapeOnly = landscapeOnlyTemp;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700417 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800418 useWideViewPort = true; // use wide view port for either setting
419 if (autoFitPage) {
420 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
421 } else {
422 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
423 }
424 defaultTextEncodingName =
425 p.getString(PREF_DEFAULT_TEXT_ENCODING,
426 defaultTextEncodingName);
427
428 showDebugSettings =
429 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
430 // Debug menu items have precidence if the menu is visible
431 if (showDebugSettings) {
432 boolean small_screen = p.getBoolean("small_screen",
433 layoutAlgorithm ==
434 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
435 if (small_screen) {
436 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
437 } else {
438 boolean normal_layout = p.getBoolean("normal_layout",
439 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
440 if (normal_layout) {
441 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
442 } else {
443 layoutAlgorithm =
444 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
445 }
446 }
447 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
448 tracing = p.getBoolean("enable_tracing", tracing);
449 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
450 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800451 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800452 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700453 // JS flags is loaded from DB even if showDebugSettings is false,
454 // so that it can be set once and be effective all the time.
455 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100456
457 // Read the setting for showing/hiding the JS Console always so that should the
458 // user enable debug settings, we already know if we should show the console.
459 // The user will never see the console unless they navigate to about:debug,
460 // regardless of the setting we read here. This setting is only used after debug
461 // is enabled.
462 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700463
Andrei Popescu01068702009-08-03 16:03:24 +0100464 // HTML5 API flags
465 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
466 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
467 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100468 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100469 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100470
Grace Klobad9ee1392009-07-20 11:53:33 -0700471 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800472 }
473
The Android Open Source Project0c908882009-03-03 19:32:16 -0800474 public String getHomePage() {
475 return homeUrl;
476 }
477
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100478 public SearchEngine getSearchEngine() {
479 return searchEngine;
480 }
481
Feng Qianb3c02da2009-06-29 15:58:08 -0700482 public String getJsFlags() {
483 return jsFlags;
484 }
485
Andrei Popescu79e82b72009-07-27 12:01:59 +0100486 public WebStorageSizeManager getWebStorageSizeManager() {
487 return webStorageSizeManager;
488 }
489
The Android Open Source Project0c908882009-03-03 19:32:16 -0800490 public void setHomePage(Context context, String url) {
491 Editor ed = PreferenceManager.
492 getDefaultSharedPreferences(context).edit();
493 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700494 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800495 homeUrl = url;
496 }
497
The Android Open Source Project0c908882009-03-03 19:32:16 -0800498 public WebSettings.TextSize getTextSize() {
499 return textSize;
500 }
501
Grace Kloba2f830682009-06-25 11:08:53 -0700502 public WebSettings.ZoomDensity getDefaultZoom() {
503 return zoomDensity;
504 }
505
The Android Open Source Project0c908882009-03-03 19:32:16 -0800506 public boolean openInBackground() {
507 return openInBackground;
508 }
509
510 public boolean showSecurityWarnings() {
511 return showSecurityWarnings;
512 }
513
514 public boolean isTracing() {
515 return tracing;
516 }
517
518 public boolean isLightTouch() {
519 return lightTouch;
520 }
521
522 public boolean isNavDump() {
523 return navDump;
524 }
525
The Android Open Source Project0c908882009-03-03 19:32:16 -0800526 public boolean showDebugSettings() {
527 return showDebugSettings;
528 }
529
530 public void toggleDebugSettings() {
531 showDebugSettings = !showDebugSettings;
532 navDump = showDebugSettings;
533 update();
534 }
535
Ben Murdoch0cb81892010-10-08 12:41:33 +0100536 public void setAutoFillProfile(Context ctx, AutoFillProfile profile) {
537 mAutoFillProfile = profile;
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100538 setActiveAutoFillProfileId(ctx, profile.getUniqueId());
Ben Murdoch0cb81892010-10-08 12:41:33 +0100539 // Update the AutoFill DB
540 new SaveProfileToDbTask(ctx).execute(mAutoFillProfile);
541
542 }
543
544 public AutoFillProfile getAutoFillProfile() {
545 return mAutoFillProfile;
546 }
547
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100548 private void setActiveAutoFillProfileId(Context context, int activeProfileId) {
549 autoFillActiveProfileId = activeProfileId;
550 Editor ed = PreferenceManager.
551 getDefaultSharedPreferences(context).edit();
552 ed.putInt(PREF_AUTOFILL_ACTIVE_PROFILE_ID, activeProfileId);
553 ed.apply();
554 }
555
The Android Open Source Project0c908882009-03-03 19:32:16 -0800556 /**
557 * Add a WebSettings object to the list of observers that will be updated
558 * when update() is called.
559 *
560 * @param s A WebSettings object that is strictly tied to the life of a
561 * WebView.
562 */
563 public Observer addObserver(WebSettings s) {
564 Observer old = mWebSettingsToObservers.get(s);
565 if (old != null) {
566 super.deleteObserver(old);
567 }
568 Observer o = new Observer(s);
569 mWebSettingsToObservers.put(s, o);
570 super.addObserver(o);
571 return o;
572 }
573
574 /**
575 * Delete the given WebSettings observer from the list of observers.
576 * @param s The WebSettings object to be deleted.
577 */
578 public void deleteObserver(WebSettings s) {
579 Observer o = mWebSettingsToObservers.get(s);
580 if (o != null) {
581 mWebSettingsToObservers.remove(s);
582 super.deleteObserver(o);
583 }
584 }
585
586 /*
587 * Package level method for obtaining a single app instance of the
588 * BrowserSettings.
589 */
590 /*package*/ static BrowserSettings getInstance() {
591 if (sSingleton == null ) {
592 sSingleton = new BrowserSettings();
593 }
594 return sSingleton;
595 }
596
597 /*
598 * Package level method for associating the BrowserSettings with TabControl
599 */
600 /* package */void setTabControl(TabControl tabControl) {
601 mTabControl = tabControl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800602 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800603 }
604
605 /*
606 * Update all the observers of the object.
607 */
608 /*package*/ void update() {
609 setChanged();
610 notifyObservers();
611 }
612
613 /*package*/ void clearCache(Context context) {
614 WebIconDatabase.getInstance().removeAllIcons();
615 if (mTabControl != null) {
616 WebView current = mTabControl.getCurrentWebView();
617 if (current != null) {
618 current.clearCache(true);
619 }
620 }
621 }
622
623 /*package*/ void clearCookies(Context context) {
624 CookieManager.getInstance().removeAllCookie();
625 }
626
627 /* package */void clearHistory(Context context) {
628 ContentResolver resolver = context.getContentResolver();
629 Browser.clearHistory(resolver);
630 Browser.clearSearches(resolver);
631 }
632
633 /* package */ void clearFormData(Context context) {
634 WebViewDatabase.getInstance(context).clearFormData();
635 if (mTabControl != null) {
Henrik Baard794dc722010-02-19 16:28:06 +0100636 WebView currentTopView = mTabControl.getCurrentTopWebView();
637 if (currentTopView != null) {
638 currentTopView.clearFormData();
639 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800640 }
641 }
642
643 /*package*/ void clearPasswords(Context context) {
644 WebViewDatabase db = WebViewDatabase.getInstance(context);
645 db.clearUsernamePassword();
646 db.clearHttpAuthUsernamePassword();
647 }
648
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800649 private void updateTabControlSettings() {
650 // Enable/disable the error console.
651 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
652 showDebugSettings && showConsole);
653 mTabControl.getBrowserActivity().setRequestedOrientation(
654 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
655 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
656 }
657
Nicolas Roard78a98e42009-05-11 13:34:17 +0100658 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100659 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100660 }
661
662 /*package*/ void clearLocationAccess(Context context) {
663 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100664 }
665
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400666 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800667 reset();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500668 SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700669 p.edit().clear().apply();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500670 PreferenceManager.setDefaultValues(ctx, R.xml.page_content_preferences, true);
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500671 PreferenceManager.setDefaultValues(ctx, R.xml.personal_preferences, true);
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500672 PreferenceManager.setDefaultValues(ctx, R.xml.privacy_preferences, true);
673 PreferenceManager.setDefaultValues(ctx, R.xml.security_preferences, true);
674 PreferenceManager.setDefaultValues(ctx, R.xml.advanced_preferences, true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700675 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700676 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100677 // reset appcache max size
678 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700679 }
680
681 private String getFactoryResetHomeUrl(Context context) {
682 String url = context.getResources().getString(R.string.homepage_base);
683 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500684 url = url.replace("{CID}",
685 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700686 }
687 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800688 }
689
The Android Open Source Project0c908882009-03-03 19:32:16 -0800690 // Private constructor that does nothing.
691 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800692 reset();
693 }
694
695 private void reset() {
696 // Private variables for settings
697 // NOTE: these defaults need to be kept in sync with the XML
698 // until the performance of PreferenceManager.setDefaultValues()
699 // is improved.
700 loadsImagesAutomatically = true;
701 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400702 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800703 javaScriptCanOpenWindowsAutomatically = false;
704 showSecurityWarnings = true;
705 rememberPasswords = true;
706 saveFormData = true;
Ben Murdochce549fc2010-09-09 10:28:08 +0100707 autoFillEnabled = false;
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100708 // TODO: Should remove the autofill profile fully and
709 // set the active profile id to 0.
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800710 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800711 autoFitPage = true;
712 landscapeOnly = false;
713 loadsPageInOverviewMode = true;
714 showDebugSettings = false;
715 // HTML5 API flags
716 appCacheEnabled = true;
717 databaseEnabled = true;
718 domStorageEnabled = true;
719 geolocationEnabled = true;
720 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800721 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100722
723 private class SaveProfileToDbTask extends AsyncTask<AutoFillProfile, Void, Void> {
724
725 Context mContext;
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100726 AutoFillProfileDatabase mAutoFillProfileDb;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100727
728 public SaveProfileToDbTask(Context ctx) {
729 mContext = ctx;
730 }
731
732 protected Void doInBackground(AutoFillProfile... values) {
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100733 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100734 assert autoFillActiveProfileId > 0;
735 mAutoFillProfileDb.addOrUpdateProfile(autoFillActiveProfileId, values[0]);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100736 return null;
737 }
738
739 protected void onPostExecute(Void result) {
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100740 Toast.makeText(mContext, R.string.autofill_profile_successful_save,
741 Toast.LENGTH_SHORT).show();
742 mAutoFillProfileDb.close();
Ben Murdoch0cb81892010-10-08 12:41:33 +0100743 }
744 }
745
The Android Open Source Project0c908882009-03-03 19:32:16 -0800746}