blob: 96b6fd27d6dd532e67dcb0774e2ab2a06e95bbec [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;
136 // TODO: For now we only support one autofill profile. Add support for
137 // multiple profiles later.
138 private int mActiveAutoFillProfileId = 1;
139
The Android Open Source Project0c908882009-03-03 19:32:16 -0800140 // Preference keys that are used outside this class
141 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
142 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
143 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
144 public final static String PREF_HOMEPAGE = "homepage";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100145 public final static String PREF_SEARCH_ENGINE = "search_engine";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 public final static String PREF_CLEAR_FORM_DATA =
147 "privacy_clear_form_data";
148 public final static String PREF_CLEAR_PASSWORDS =
149 "privacy_clear_passwords";
150 public final static String PREF_EXTRAS_RESET_DEFAULTS =
151 "reset_default_preferences";
152 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100153 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800154 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700155 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800156 public final static String PREF_DEFAULT_TEXT_ENCODING =
157 "default_text_encoding";
Steve Blockc6f577f2009-08-20 18:11:08 +0100158 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
159 "privacy_clear_geolocation_access";
Ben Murdochaf554522010-09-10 22:09:30 +0100160 public final static String PREF_AUTOFILL_ENABLED = "autofill_enabled";
161 public final static String PREF_AUTOFILL_PROFILE = "autofill_profile";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800162
163 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700164 "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
165 "like Gecko) Version/5.0 Safari/533.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800166
167 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700168 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
169 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
170
171 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
172 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
173 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
174
175 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
176 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
177 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800178
179 // Value to truncate strings when adding them to a TextView within
180 // a ListView
181 public final static int MAX_TEXTVIEW_LEN = 80;
182
Jeff Davidson43610292010-07-16 16:03:58 -0700183 public static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider";
184
185 public static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/");
186
The Android Open Source Project0c908882009-03-03 19:32:16 -0800187 private TabControl mTabControl;
188
189 // Single instance of the BrowserSettings for use in the Browser app.
190 private static BrowserSettings sSingleton;
191
192 // Private map of WebSettings to Observer objects used when deleting an
193 // observer.
194 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
195 new HashMap<WebSettings,Observer>();
196
197 /*
198 * An observer wrapper for updating a WebSettings object with the new
199 * settings after a call to BrowserSettings.update().
200 */
201 static class Observer implements java.util.Observer {
202 // Private WebSettings object that will be updated.
203 private WebSettings mSettings;
204
205 Observer(WebSettings w) {
206 mSettings = w;
207 }
208
209 public void update(Observable o, Object arg) {
210 BrowserSettings b = (BrowserSettings)o;
211 WebSettings s = mSettings;
212
213 s.setLayoutAlgorithm(b.layoutAlgorithm);
214 if (b.userAgent == 0) {
215 // use the default ua string
216 s.setUserAgentString(null);
217 } else if (b.userAgent == 1) {
218 s.setUserAgentString(DESKTOP_USERAGENT);
219 } else if (b.userAgent == 2) {
220 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700221 } else if (b.userAgent == 3) {
222 s.setUserAgentString(IPAD_USERAGENT);
223 } else if (b.userAgent == 4) {
224 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800225 }
226 s.setUseWideViewPort(b.useWideViewPort);
227 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
228 s.setJavaScriptEnabled(b.javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400229 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800230 s.setJavaScriptCanOpenWindowsAutomatically(
231 b.javaScriptCanOpenWindowsAutomatically);
232 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
233 s.setMinimumFontSize(b.minimumFontSize);
234 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
235 s.setDefaultFontSize(b.defaultFontSize);
236 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
237 s.setNavDump(b.navDump);
238 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700239 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800240 s.setLightTouchEnabled(b.lightTouch);
241 s.setSaveFormData(b.saveFormData);
Ben Murdochce549fc2010-09-09 10:28:08 +0100242 s.setAutoFillEnabled(b.autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800243 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700244 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800245 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800246
247 // WebView inside Browser doesn't want initial focus to be set.
248 s.setNeedInitialFocus(false);
249 // Browser supports multiple windows
250 s.setSupportMultipleWindows(true);
Grace Kloba61fc77c2010-06-22 09:57:33 -0700251 // enable smooth transition for better performance during panning or
252 // zooming
253 s.setEnableSmoothTransition(true);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100254
Andrei Popescu01068702009-08-03 16:03:24 +0100255 // HTML5 API flags
256 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100257 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100258 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100259 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100260 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100261
Andrei Popescu01068702009-08-03 16:03:24 +0100262 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100263 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100264 s.setAppCachePath(b.appCachePath);
265 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100266 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100267
Ben Murdoch0cb81892010-10-08 12:41:33 +0100268 // Active AutoFill profile data.
269 s.setAutoFillProfile(b.mAutoFillProfile);
270
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800271 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800272 }
273 }
274
275 /**
276 * Load settings from the browser app's database.
277 * NOTE: Strings used for the preferences must match those specified
Jeff Hamilton175ab302010-10-04 12:53:49 -0500278 * in the various preference XML files.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800279 * @param ctx A Context object used to query the browser's settings
280 * database. If the database exists, the saved settings will be
281 * stored in this BrowserSettings object. This will update all
282 * observers of this object.
283 */
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100284 public void loadFromDb(final Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800285 SharedPreferences p =
286 PreferenceManager.getDefaultSharedPreferences(ctx);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100287 // Set the default value for the Application Caches path.
288 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100289 // Determine the maximum size of the application cache.
Andrei Popescu907c5762009-07-29 14:44:24 +0100290 webStorageSizeManager = new WebStorageSizeManager(
291 ctx,
292 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
293 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100294 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100295 // Set the default value for the Database path.
296 databasePath = ctx.getDir("databases", 0).getPath();
Steve Block5029cf02009-08-21 13:16:58 +0100297 // Set the default value for the Geolocation database path.
298 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800299
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700300 if (p.getString(PREF_HOMEPAGE, "") == "") {
301 // No home page preferences is set, set it to default.
302 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
303 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700304
Grace Kloba9804c432009-12-02 11:07:40 -0800305 // the cost of one cached page is ~3M (measured using nytimes.com). For
306 // low end devices, we only cache one page. For high end devices, we try
307 // to cache more pages, currently choose 5.
308 ActivityManager am = (ActivityManager) ctx
309 .getSystemService(Context.ACTIVITY_SERVICE);
310 if (am.getMemoryClass() > 16) {
Andrei Popescuba676ef2010-03-29 12:12:55 +0100311 pageCacheCapacity = 5;
Grace Kloba9804c432009-12-02 11:07:40 -0800312 } else {
313 pageCacheCapacity = 1;
314 }
315
Ben Murdoch0cb81892010-10-08 12:41:33 +0100316 // Load the autofill profile data from the database. We use a database separate
317 // to the browser preference DB to make it easier to support multiple profiles
318 // and switching between them.
Ben Murdoch0cb81892010-10-08 12:41:33 +0100319 AutoFillProfileDatabase autoFillDb = AutoFillProfileDatabase.getInstance(ctx);
320 Cursor c = autoFillDb.getProfile(mActiveAutoFillProfileId);
321
322 if (c.getCount() > 0) {
323 c.moveToFirst();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100324
325 String fullName = c.getString(c.getColumnIndex(
326 AutoFillProfileDatabase.Profiles.FULL_NAME));
327 String email = c.getString(c.getColumnIndex(
328 AutoFillProfileDatabase.Profiles.EMAIL_ADDRESS));
329 String company = c.getString(c.getColumnIndex(
330 AutoFillProfileDatabase.Profiles.COMPANY_NAME));
331 String addressLine1 = c.getString(c.getColumnIndex(
332 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_1));
333 String addressLine2 = c.getString(c.getColumnIndex(
334 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_2));
335 String city = c.getString(c.getColumnIndex(
336 AutoFillProfileDatabase.Profiles.CITY));
337 String state = c.getString(c.getColumnIndex(
338 AutoFillProfileDatabase.Profiles.STATE));
339 String zip = c.getString(c.getColumnIndex(
340 AutoFillProfileDatabase.Profiles.ZIP_CODE));
341 String country = c.getString(c.getColumnIndex(
342 AutoFillProfileDatabase.Profiles.COUNTRY));
343 String phone = c.getString(c.getColumnIndex(
344 AutoFillProfileDatabase.Profiles.PHONE_NUMBER));
345 mAutoFillProfile = new AutoFillProfile(fullName, email, company,
346 addressLine1, addressLine2, city, state, zip, country,
347 phone);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100348 }
349 c.close();
350 autoFillDb.close();
351
Jeff Hamilton175ab302010-10-04 12:53:49 -0500352 // PreferenceManager.setDefaultValues is TOO SLOW, need to manually keep
353 // the defaults in sync
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100354 syncSharedPreferences(ctx, p);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800355 }
356
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100357 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700358
The Android Open Source Project0c908882009-03-03 19:32:16 -0800359 homeUrl =
360 p.getString(PREF_HOMEPAGE, homeUrl);
Leon Scroggins III31306602010-09-17 11:10:29 -0400361 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
362 SearchEngine.GOOGLE);
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100363 if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
364 if (searchEngine != null) {
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400365 if (searchEngine.supportsVoiceSearch()) {
366 // One or more tabs could have been in voice search mode.
367 // Clear it, since the new SearchEngine may not support
368 // it, or may handle it differently.
369 for (int i = 0; i < mTabControl.getTabCount(); i++) {
370 mTabControl.getTab(i).revertVoiceSearchMode();
371 }
372 }
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100373 searchEngine.close();
374 }
375 searchEngine = SearchEngines.get(ctx, searchEngineName);
376 }
377 Log.i(TAG, "Selected search engine: " + searchEngine);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700378
The Android Open Source Project0c908882009-03-03 19:32:16 -0800379 loadsImagesAutomatically = p.getBoolean("load_images",
380 loadsImagesAutomatically);
381 javaScriptEnabled = p.getBoolean("enable_javascript",
382 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400383 pluginState = WebSettings.PluginState.valueOf(
384 p.getString("plugin_state", pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800385 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
386 "block_popup_windows",
387 !javaScriptCanOpenWindowsAutomatically);
388 showSecurityWarnings = p.getBoolean("show_security_warnings",
389 showSecurityWarnings);
390 rememberPasswords = p.getBoolean("remember_passwords",
391 rememberPasswords);
392 saveFormData = p.getBoolean("save_formdata",
393 saveFormData);
Ben Murdochaf554522010-09-10 22:09:30 +0100394 autoFillEnabled = p.getBoolean("autofill_enabled", autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800395 boolean accept_cookies = p.getBoolean("accept_cookies",
396 CookieManager.getInstance().acceptCookie());
397 CookieManager.getInstance().setAcceptCookie(accept_cookies);
398 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800399 textSize = WebSettings.TextSize.valueOf(
400 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700401 zoomDensity = WebSettings.ZoomDensity.valueOf(
402 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800403 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700404 loadsPageInOverviewMode = p.getBoolean("load_page",
405 loadsPageInOverviewMode);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700406 boolean landscapeOnlyTemp =
407 p.getBoolean("landscape_only", landscapeOnly);
408 if (landscapeOnlyTemp != landscapeOnly) {
409 landscapeOnly = landscapeOnlyTemp;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700410 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800411 useWideViewPort = true; // use wide view port for either setting
412 if (autoFitPage) {
413 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
414 } else {
415 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
416 }
417 defaultTextEncodingName =
418 p.getString(PREF_DEFAULT_TEXT_ENCODING,
419 defaultTextEncodingName);
420
421 showDebugSettings =
422 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
423 // Debug menu items have precidence if the menu is visible
424 if (showDebugSettings) {
425 boolean small_screen = p.getBoolean("small_screen",
426 layoutAlgorithm ==
427 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
428 if (small_screen) {
429 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
430 } else {
431 boolean normal_layout = p.getBoolean("normal_layout",
432 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
433 if (normal_layout) {
434 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
435 } else {
436 layoutAlgorithm =
437 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
438 }
439 }
440 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
441 tracing = p.getBoolean("enable_tracing", tracing);
442 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
443 navDump = p.getBoolean("enable_nav_dump", navDump);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800444 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800445 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700446 // JS flags is loaded from DB even if showDebugSettings is false,
447 // so that it can be set once and be effective all the time.
448 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100449
450 // Read the setting for showing/hiding the JS Console always so that should the
451 // user enable debug settings, we already know if we should show the console.
452 // The user will never see the console unless they navigate to about:debug,
453 // regardless of the setting we read here. This setting is only used after debug
454 // is enabled.
455 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700456
Andrei Popescu01068702009-08-03 16:03:24 +0100457 // HTML5 API flags
458 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
459 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
460 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100461 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100462 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100463
Grace Klobad9ee1392009-07-20 11:53:33 -0700464 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800465 }
466
The Android Open Source Project0c908882009-03-03 19:32:16 -0800467 public String getHomePage() {
468 return homeUrl;
469 }
470
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100471 public SearchEngine getSearchEngine() {
472 return searchEngine;
473 }
474
Feng Qianb3c02da2009-06-29 15:58:08 -0700475 public String getJsFlags() {
476 return jsFlags;
477 }
478
Andrei Popescu79e82b72009-07-27 12:01:59 +0100479 public WebStorageSizeManager getWebStorageSizeManager() {
480 return webStorageSizeManager;
481 }
482
The Android Open Source Project0c908882009-03-03 19:32:16 -0800483 public void setHomePage(Context context, String url) {
484 Editor ed = PreferenceManager.
485 getDefaultSharedPreferences(context).edit();
486 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700487 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800488 homeUrl = url;
489 }
490
The Android Open Source Project0c908882009-03-03 19:32:16 -0800491 public WebSettings.TextSize getTextSize() {
492 return textSize;
493 }
494
Grace Kloba2f830682009-06-25 11:08:53 -0700495 public WebSettings.ZoomDensity getDefaultZoom() {
496 return zoomDensity;
497 }
498
The Android Open Source Project0c908882009-03-03 19:32:16 -0800499 public boolean openInBackground() {
500 return openInBackground;
501 }
502
503 public boolean showSecurityWarnings() {
504 return showSecurityWarnings;
505 }
506
507 public boolean isTracing() {
508 return tracing;
509 }
510
511 public boolean isLightTouch() {
512 return lightTouch;
513 }
514
515 public boolean isNavDump() {
516 return navDump;
517 }
518
The Android Open Source Project0c908882009-03-03 19:32:16 -0800519 public boolean showDebugSettings() {
520 return showDebugSettings;
521 }
522
523 public void toggleDebugSettings() {
524 showDebugSettings = !showDebugSettings;
525 navDump = showDebugSettings;
526 update();
527 }
528
Ben Murdoch0cb81892010-10-08 12:41:33 +0100529 public void setAutoFillProfile(Context ctx, AutoFillProfile profile) {
530 mAutoFillProfile = profile;
531 // Update the AutoFill DB
532 new SaveProfileToDbTask(ctx).execute(mAutoFillProfile);
533
534 }
535
536 public AutoFillProfile getAutoFillProfile() {
537 return mAutoFillProfile;
538 }
539
The Android Open Source Project0c908882009-03-03 19:32:16 -0800540 /**
541 * Add a WebSettings object to the list of observers that will be updated
542 * when update() is called.
543 *
544 * @param s A WebSettings object that is strictly tied to the life of a
545 * WebView.
546 */
547 public Observer addObserver(WebSettings s) {
548 Observer old = mWebSettingsToObservers.get(s);
549 if (old != null) {
550 super.deleteObserver(old);
551 }
552 Observer o = new Observer(s);
553 mWebSettingsToObservers.put(s, o);
554 super.addObserver(o);
555 return o;
556 }
557
558 /**
559 * Delete the given WebSettings observer from the list of observers.
560 * @param s The WebSettings object to be deleted.
561 */
562 public void deleteObserver(WebSettings s) {
563 Observer o = mWebSettingsToObservers.get(s);
564 if (o != null) {
565 mWebSettingsToObservers.remove(s);
566 super.deleteObserver(o);
567 }
568 }
569
570 /*
571 * Package level method for obtaining a single app instance of the
572 * BrowserSettings.
573 */
574 /*package*/ static BrowserSettings getInstance() {
575 if (sSingleton == null ) {
576 sSingleton = new BrowserSettings();
577 }
578 return sSingleton;
579 }
580
581 /*
582 * Package level method for associating the BrowserSettings with TabControl
583 */
584 /* package */void setTabControl(TabControl tabControl) {
585 mTabControl = tabControl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800586 updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800587 }
588
589 /*
590 * Update all the observers of the object.
591 */
592 /*package*/ void update() {
593 setChanged();
594 notifyObservers();
595 }
596
597 /*package*/ void clearCache(Context context) {
598 WebIconDatabase.getInstance().removeAllIcons();
599 if (mTabControl != null) {
600 WebView current = mTabControl.getCurrentWebView();
601 if (current != null) {
602 current.clearCache(true);
603 }
604 }
605 }
606
607 /*package*/ void clearCookies(Context context) {
608 CookieManager.getInstance().removeAllCookie();
609 }
610
611 /* package */void clearHistory(Context context) {
612 ContentResolver resolver = context.getContentResolver();
613 Browser.clearHistory(resolver);
614 Browser.clearSearches(resolver);
615 }
616
617 /* package */ void clearFormData(Context context) {
618 WebViewDatabase.getInstance(context).clearFormData();
619 if (mTabControl != null) {
Henrik Baard794dc722010-02-19 16:28:06 +0100620 WebView currentTopView = mTabControl.getCurrentTopWebView();
621 if (currentTopView != null) {
622 currentTopView.clearFormData();
623 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800624 }
625 }
626
627 /*package*/ void clearPasswords(Context context) {
628 WebViewDatabase db = WebViewDatabase.getInstance(context);
629 db.clearUsernamePassword();
630 db.clearHttpAuthUsernamePassword();
631 }
632
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800633 private void updateTabControlSettings() {
634 // Enable/disable the error console.
635 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
636 showDebugSettings && showConsole);
637 mTabControl.getBrowserActivity().setRequestedOrientation(
638 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
639 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
640 }
641
Nicolas Roard78a98e42009-05-11 13:34:17 +0100642 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100643 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100644 }
645
646 /*package*/ void clearLocationAccess(Context context) {
647 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100648 }
649
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400650 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800651 reset();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500652 SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700653 p.edit().clear().apply();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500654 PreferenceManager.setDefaultValues(ctx, R.xml.page_content_preferences, true);
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500655 PreferenceManager.setDefaultValues(ctx, R.xml.personal_preferences, true);
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500656 PreferenceManager.setDefaultValues(ctx, R.xml.privacy_preferences, true);
657 PreferenceManager.setDefaultValues(ctx, R.xml.security_preferences, true);
658 PreferenceManager.setDefaultValues(ctx, R.xml.advanced_preferences, true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700659 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700660 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100661 // reset appcache max size
662 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700663 }
664
665 private String getFactoryResetHomeUrl(Context context) {
666 String url = context.getResources().getString(R.string.homepage_base);
667 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500668 url = url.replace("{CID}",
669 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700670 }
671 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800672 }
673
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674 // Private constructor that does nothing.
675 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800676 reset();
677 }
678
679 private void reset() {
680 // Private variables for settings
681 // NOTE: these defaults need to be kept in sync with the XML
682 // until the performance of PreferenceManager.setDefaultValues()
683 // is improved.
684 loadsImagesAutomatically = true;
685 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400686 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800687 javaScriptCanOpenWindowsAutomatically = false;
688 showSecurityWarnings = true;
689 rememberPasswords = true;
690 saveFormData = true;
Ben Murdochce549fc2010-09-09 10:28:08 +0100691 autoFillEnabled = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800692 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800693 autoFitPage = true;
694 landscapeOnly = false;
695 loadsPageInOverviewMode = true;
696 showDebugSettings = false;
697 // HTML5 API flags
698 appCacheEnabled = true;
699 databaseEnabled = true;
700 domStorageEnabled = true;
701 geolocationEnabled = true;
702 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800703 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100704
705 private class SaveProfileToDbTask extends AsyncTask<AutoFillProfile, Void, Void> {
706
707 Context mContext;
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100708 AutoFillProfileDatabase mAutoFillProfileDb;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100709
710 public SaveProfileToDbTask(Context ctx) {
711 mContext = ctx;
712 }
713
714 protected Void doInBackground(AutoFillProfile... values) {
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100715 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
716 mAutoFillProfileDb.addOrUpdateProfile(mActiveAutoFillProfileId, values[0]);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100717 return null;
718 }
719
720 protected void onPostExecute(Void result) {
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100721 Toast.makeText(mContext, R.string.autofill_profile_successful_save,
722 Toast.LENGTH_SHORT).show();
723 mAutoFillProfileDb.close();
Ben Murdoch0cb81892010-10-08 12:41:33 +0100724 }
725 }
726
The Android Open Source Project0c908882009-03-03 19:32:16 -0800727}