blob: 9ae2a250456ade06471fb7864ce1426ed5447a48 [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
John Reckbafe58a2011-01-11 10:26:02 -080020import com.android.browser.homepages.HomeProvider;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010021import com.android.browser.search.SearchEngine;
22import com.android.browser.search.SearchEngines;
23
Grace Kloba9804c432009-12-02 11:07:40 -080024import android.app.ActivityManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.content.ContentResolver;
Ed Tama94de4f2011-05-10 19:37:10 -070026import android.content.ContentUris;
27import android.content.ContentValues;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.content.Context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.content.SharedPreferences;
30import android.content.SharedPreferences.Editor;
John Reck812d2d62011-01-18 14:16:15 -080031import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Ed Tama94de4f2011-05-10 19:37:10 -070032import android.content.pm.PackageManager;
Ben Murdoch0cb81892010-10-08 12:41:33 +010033import android.database.Cursor;
Jeff Davidson43610292010-07-16 16:03:58 -070034import android.net.Uri;
Ben Murdoch0cb81892010-10-08 12:41:33 +010035import android.os.AsyncTask;
Ben Murdoch23da30e2010-10-26 15:18:44 +010036import android.os.Message;
Ben Murdoch0cb81892010-10-08 12:41:33 +010037import android.preference.PreferenceManager;
Ben Murdoch0cb81892010-10-08 12:41:33 +010038import android.provider.Browser;
Ed Tama94de4f2011-05-10 19:37:10 -070039import android.provider.BrowserContract.Bookmarks;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010040import 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;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import android.webkit.WebIconDatabase;
44import android.webkit.WebSettings;
Ben Murdoch0cb81892010-10-08 12:41:33 +010045import android.webkit.WebSettings.AutoFillProfile;
Nicolas Roard78a98e42009-05-11 13:34:17 +010046import android.webkit.WebStorage;
John Reck812d2d62011-01-18 14:16:15 -080047import android.webkit.WebView;
48import android.webkit.WebViewDatabase;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049
50import java.util.HashMap;
51import java.util.Observable;
52
53/*
54 * Package level class for storing various WebView and Browser settings. To use
55 * this class:
56 * BrowserSettings s = BrowserSettings.getInstance();
57 * s.addObserver(webView.getSettings());
58 * s.loadFromDb(context); // Only needed on app startup
59 * s.javaScriptEnabled = true;
60 * ... // set any other settings
61 * s.update(); // this will update all the observers
62 *
63 * To remove an observer:
64 * s.deleteObserver(webView.getSettings());
65 */
John Reck63bb6872010-12-01 19:29:32 -080066public class BrowserSettings extends Observable implements OnSharedPreferenceChangeListener {
Leon Scroggins9ac587d2009-04-20 12:53:46 -040067 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080068 // NOTE: these defaults need to be kept in sync with the XML
69 // until the performance of PreferenceManager.setDefaultValues()
70 // is improved.
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080071 // Note: boolean variables are set inside reset function.
72 private boolean loadsImagesAutomatically;
73 private boolean javaScriptEnabled;
Patrick Scotte536b332010-03-22 10:19:32 -040074 private WebSettings.PluginState pluginState;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080075 private boolean javaScriptCanOpenWindowsAutomatically;
76 private boolean showSecurityWarnings;
77 private boolean rememberPasswords;
78 private boolean saveFormData;
Ben Murdochce549fc2010-09-09 10:28:08 +010079 private boolean autoFillEnabled;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080080 private boolean openInBackground;
The Android Open Source Project0c908882009-03-03 19:32:16 -080081 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070082 private String homeUrl = "";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010083 private SearchEngine searchEngine;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080084 private boolean autoFitPage;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080085 private boolean loadsPageInOverviewMode;
86 private boolean showDebugSettings;
Andrei Popescu01068702009-08-03 16:03:24 +010087 // HTML5 API flags
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -080088 private boolean appCacheEnabled;
89 private boolean databaseEnabled;
90 private boolean domStorageEnabled;
91 private boolean geolocationEnabled;
92 private boolean workersEnabled; // only affects V8. JSC does not have a similar setting
Andrei Popescu01068702009-08-03 16:03:24 +010093 // HTML5 API configuration params
94 private long appCacheMaxSize = Long.MAX_VALUE;
95 private String appCachePath; // default value set in loadFromDb().
96 private String databasePath; // default value set in loadFromDb()
Steve Block5029cf02009-08-21 13:16:58 +010097 private String geolocationDatabasePath; // default value set in loadFromDb()
Andrei Popescu01068702009-08-03 16:03:24 +010098 private WebStorageSizeManager webStorageSizeManager;
99
100 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800101
Nicolas Roard78a98e42009-05-11 13:34:17 +0100102 private final static String TAG = "BrowserSettings";
103
The Android Open Source Project0c908882009-03-03 19:32:16 -0800104 // Development settings
105 public WebSettings.LayoutAlgorithm layoutAlgorithm =
106 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
107 private boolean useWideViewPort = true;
108 private int userAgent = 0;
109 private boolean tracing = false;
110 private boolean lightTouch = false;
111 private boolean navDump = false;
Derek Sollenberger8de82852010-11-18 19:51:50 -0500112 private boolean hardwareAccelerated = true;
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800113 private boolean showVisualIndicator = false;
Michael Kolb376b5412010-12-15 11:52:57 -0800114 // Lab settings
115 private boolean quickControls = false;
John Reckbafe58a2011-01-11 10:26:02 -0800116 private boolean useMostVisitedHomepage = false;
Narayan Kamath5119edd2011-02-23 15:49:17 +0000117 private boolean useInstant = false;
Michael Kolb376b5412010-12-15 11:52:57 -0800118
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
Ben Murdoch23da30e2010-10-26 15:18:44 +0100135 private AutoFillProfile autoFillProfile;
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 Murdoch23da30e2010-10-26 15:18:44 +0100141 private static final int NO_AUTOFILL_PROFILE_SET = 0;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100142
The Android Open Source Project0c908882009-03-03 19:32:16 -0800143 // Preference keys that are used outside this class
144 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
145 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
146 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
147 public final static String PREF_HOMEPAGE = "homepage";
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100148 public final static String PREF_SEARCH_ENGINE = "search_engine";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800149 public final static String PREF_CLEAR_FORM_DATA =
150 "privacy_clear_form_data";
151 public final static String PREF_CLEAR_PASSWORDS =
152 "privacy_clear_passwords";
153 public final static String PREF_EXTRAS_RESET_DEFAULTS =
154 "reset_default_preferences";
155 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100156 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800157 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700158 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800159 public final static String PREF_DEFAULT_TEXT_ENCODING =
160 "default_text_encoding";
Steve Blockc6f577f2009-08-20 18:11:08 +0100161 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
162 "privacy_clear_geolocation_access";
Ben Murdochaf554522010-09-10 22:09:30 +0100163 public final static String PREF_AUTOFILL_ENABLED = "autofill_enabled";
164 public final static String PREF_AUTOFILL_PROFILE = "autofill_profile";
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100165 public final static String PREF_AUTOFILL_ACTIVE_PROFILE_ID = "autofill_active_profile_id";
John Reck63bb6872010-12-01 19:29:32 -0800166 public final static String PREF_HARDWARE_ACCEL = "enable_hardware_accel";
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800167 public final static String PREF_VISUAL_INDICATOR = "enable_visual_indicator";
John Reck63bb6872010-12-01 19:29:32 -0800168 public final static String PREF_USER_AGENT = "user_agent";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800169
Michael Kolb376b5412010-12-15 11:52:57 -0800170 public final static String PREF_QUICK_CONTROLS = "enable_quick_controls";
John Reckbafe58a2011-01-11 10:26:02 -0800171 public final static String PREF_MOST_VISITED_HOMEPAGE = "use_most_visited_homepage";
John Reck282431b2011-01-20 17:38:37 -0800172 public final static String PREF_PLUGIN_STATE = "plugin_state";
Narayan Kamath5119edd2011-02-23 15:49:17 +0000173 public final static String PREF_USE_INSTANT = "use_instant_search";
Michael Kolb376b5412010-12-15 11:52:57 -0800174
The Android Open Source Project0c908882009-03-03 19:32:16 -0800175 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700176 "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
177 "like Gecko) Version/5.0 Safari/533.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800178
179 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Bart Searsf6915fb2010-07-08 19:58:22 -0700180 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
181 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
182
183 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
184 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
185 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
186
187 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
188 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
189 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190
191 // Value to truncate strings when adding them to a TextView within
192 // a ListView
193 public final static int MAX_TEXTVIEW_LEN = 80;
194
Jeff Davidson43610292010-07-16 16:03:58 -0700195 public static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider";
196
197 public static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/");
198
John Reck63bb6872010-12-01 19:29:32 -0800199 // Set to true to enable some of the about:debug options
John Reck9eed0ef2011-01-11 17:11:37 -0800200 public static final boolean DEV_BUILD = false;
John Reck63bb6872010-12-01 19:29:32 -0800201
Michael Kolb8233fac2010-10-26 16:08:53 -0700202 private Controller mController;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800203
204 // Single instance of the BrowserSettings for use in the Browser app.
205 private static BrowserSettings sSingleton;
206
207 // Private map of WebSettings to Observer objects used when deleting an
208 // observer.
209 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
210 new HashMap<WebSettings,Observer>();
211
Ed Tama94de4f2011-05-10 19:37:10 -0700212 private String mRlzValue = "";
213
Ben Murdochef671652010-11-25 17:17:58 +0000214 private boolean mLoadFromDbComplete;
215
216 public void waitForLoadFromDbToComplete() {
217 synchronized (sSingleton) {
218 while (!mLoadFromDbComplete) {
219 try {
220 sSingleton.wait();
221 } catch (InterruptedException e) { }
222 }
223 }
224 }
225
The Android Open Source Project0c908882009-03-03 19:32:16 -0800226 /*
227 * An observer wrapper for updating a WebSettings object with the new
228 * settings after a call to BrowserSettings.update().
229 */
230 static class Observer implements java.util.Observer {
231 // Private WebSettings object that will be updated.
232 private WebSettings mSettings;
233
234 Observer(WebSettings w) {
235 mSettings = w;
236 }
237
238 public void update(Observable o, Object arg) {
239 BrowserSettings b = (BrowserSettings)o;
240 WebSettings s = mSettings;
241
242 s.setLayoutAlgorithm(b.layoutAlgorithm);
243 if (b.userAgent == 0) {
244 // use the default ua string
245 s.setUserAgentString(null);
246 } else if (b.userAgent == 1) {
247 s.setUserAgentString(DESKTOP_USERAGENT);
248 } else if (b.userAgent == 2) {
249 s.setUserAgentString(IPHONE_USERAGENT);
Bart Searsf6915fb2010-07-08 19:58:22 -0700250 } else if (b.userAgent == 3) {
251 s.setUserAgentString(IPAD_USERAGENT);
252 } else if (b.userAgent == 4) {
253 s.setUserAgentString(FROYO_USERAGENT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800254 }
255 s.setUseWideViewPort(b.useWideViewPort);
256 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
257 s.setJavaScriptEnabled(b.javaScriptEnabled);
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800258 s.setShowVisualIndicator(b.showVisualIndicator);
Patrick Scotte536b332010-03-22 10:19:32 -0400259 s.setPluginState(b.pluginState);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800260 s.setJavaScriptCanOpenWindowsAutomatically(
261 b.javaScriptCanOpenWindowsAutomatically);
262 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
263 s.setMinimumFontSize(b.minimumFontSize);
264 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
265 s.setDefaultFontSize(b.defaultFontSize);
266 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
267 s.setNavDump(b.navDump);
268 s.setTextSize(b.textSize);
Grace Kloba2f830682009-06-25 11:08:53 -0700269 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800270 s.setLightTouchEnabled(b.lightTouch);
271 s.setSaveFormData(b.saveFormData);
Ben Murdochce549fc2010-09-09 10:28:08 +0100272 s.setAutoFillEnabled(b.autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800273 s.setSavePassword(b.rememberPasswords);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700274 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
Grace Kloba79565032009-11-24 14:23:39 -0800275 s.setPageCacheCapacity(pageCacheCapacity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800276
277 // WebView inside Browser doesn't want initial focus to be set.
278 s.setNeedInitialFocus(false);
279 // Browser supports multiple windows
280 s.setSupportMultipleWindows(true);
Grace Kloba61fc77c2010-06-22 09:57:33 -0700281 // enable smooth transition for better performance during panning or
282 // zooming
283 s.setEnableSmoothTransition(true);
Patrick Scottb92bbb42011-01-05 11:38:58 -0500284 // disable content url access
285 s.setAllowContentAccess(false);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100286
Andrei Popescu01068702009-08-03 16:03:24 +0100287 // HTML5 API flags
288 s.setAppCacheEnabled(b.appCacheEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100289 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100290 s.setDomStorageEnabled(b.domStorageEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100291 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
Steve Block97b08022009-08-21 10:26:25 +0100292 s.setGeolocationEnabled(b.geolocationEnabled);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100293
Andrei Popescu01068702009-08-03 16:03:24 +0100294 // HTML5 configuration parameters.
Andrei Popescu20634ce2009-07-22 16:46:55 +0100295 s.setAppCacheMaxSize(b.appCacheMaxSize);
Andrei Popescu01068702009-08-03 16:03:24 +0100296 s.setAppCachePath(b.appCachePath);
297 s.setDatabasePath(b.databasePath);
Steve Block5029cf02009-08-21 13:16:58 +0100298 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
Ben Murdochbff2d602009-07-01 20:19:05 +0100299
Ben Murdoch0cb81892010-10-08 12:41:33 +0100300 // Active AutoFill profile data.
Ben Murdoch23da30e2010-10-26 15:18:44 +0100301 s.setAutoFillProfile(b.autoFillProfile);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100302
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800303 b.updateTabControlSettings();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800304 }
305 }
306
307 /**
Ben Murdochef671652010-11-25 17:17:58 +0000308 * Load settings from the browser app's database. It is performed in
309 * an AsyncTask as it involves plenty of slow disk IO.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800310 * NOTE: Strings used for the preferences must match those specified
Jeff Hamilton175ab302010-10-04 12:53:49 -0500311 * in the various preference XML files.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800312 * @param ctx A Context object used to query the browser's settings
313 * database. If the database exists, the saved settings will be
314 * stored in this BrowserSettings object. This will update all
315 * observers of this object.
316 */
Ben Murdochef671652010-11-25 17:17:58 +0000317 public void asyncLoadFromDb(final Context ctx) {
318 mLoadFromDbComplete = false;
319 // Run the initial settings load in an AsyncTask as it hits the
320 // disk multiple times through SharedPreferences and SQLite. We
321 // need to be certain though that this has completed before we start
322 // to load pages though, so in the worst case we will block waiting
323 // for it to finish in BrowserActivity.onCreate().
324 new LoadFromDbTask(ctx).execute();
325 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800326
Ben Murdochef671652010-11-25 17:17:58 +0000327 private class LoadFromDbTask extends AsyncTask<Void, Void, Void> {
328 private Context mContext;
329
330 public LoadFromDbTask(Context context) {
331 mContext = context;
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700332 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700333
Ben Murdochef671652010-11-25 17:17:58 +0000334 protected Void doInBackground(Void... unused) {
335 SharedPreferences p =
336 PreferenceManager.getDefaultSharedPreferences(mContext);
337 // Set the default value for the Application Caches path.
338 appCachePath = mContext.getDir("appcache", 0).getPath();
339 // Determine the maximum size of the application cache.
340 webStorageSizeManager = new WebStorageSizeManager(
341 mContext,
342 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
343 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
344 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
345 // Set the default value for the Database path.
346 databasePath = mContext.getDir("databases", 0).getPath();
347 // Set the default value for the Geolocation database path.
348 geolocationDatabasePath = mContext.getDir("geolocation", 0).getPath();
349
John Reckef074262010-12-02 16:09:14 -0800350 if (p.getString(PREF_HOMEPAGE, null) == null) {
Ben Murdochef671652010-11-25 17:17:58 +0000351 // No home page preferences is set, set it to default.
352 setHomePage(mContext, getFactoryResetHomeUrl(mContext));
353 }
354
355 // the cost of one cached page is ~3M (measured using nytimes.com). For
356 // low end devices, we only cache one page. For high end devices, we try
357 // to cache more pages, currently choose 5.
358 ActivityManager am = (ActivityManager) mContext
359 .getSystemService(Context.ACTIVITY_SERVICE);
360 if (am.getMemoryClass() > 16) {
361 pageCacheCapacity = 5;
362 } else {
363 pageCacheCapacity = 1;
364 }
365
366 // Read the last active AutoFill profile id.
367 autoFillActiveProfileId = p.getInt(
368 PREF_AUTOFILL_ACTIVE_PROFILE_ID, autoFillActiveProfileId);
369
370 // Load the autofill profile data from the database. We use a database separate
371 // to the browser preference DB to make it easier to support multiple profiles
372 // and switching between them.
373 AutoFillProfileDatabase autoFillDb = AutoFillProfileDatabase.getInstance(mContext);
374 Cursor c = autoFillDb.getProfile(autoFillActiveProfileId);
375
376 if (c.getCount() > 0) {
377 c.moveToFirst();
378
379 String fullName = c.getString(c.getColumnIndex(
380 AutoFillProfileDatabase.Profiles.FULL_NAME));
381 String email = c.getString(c.getColumnIndex(
382 AutoFillProfileDatabase.Profiles.EMAIL_ADDRESS));
383 String company = c.getString(c.getColumnIndex(
384 AutoFillProfileDatabase.Profiles.COMPANY_NAME));
385 String addressLine1 = c.getString(c.getColumnIndex(
386 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_1));
387 String addressLine2 = c.getString(c.getColumnIndex(
388 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_2));
389 String city = c.getString(c.getColumnIndex(
390 AutoFillProfileDatabase.Profiles.CITY));
391 String state = c.getString(c.getColumnIndex(
392 AutoFillProfileDatabase.Profiles.STATE));
393 String zip = c.getString(c.getColumnIndex(
394 AutoFillProfileDatabase.Profiles.ZIP_CODE));
395 String country = c.getString(c.getColumnIndex(
396 AutoFillProfileDatabase.Profiles.COUNTRY));
397 String phone = c.getString(c.getColumnIndex(
398 AutoFillProfileDatabase.Profiles.PHONE_NUMBER));
399 autoFillProfile = new AutoFillProfile(autoFillActiveProfileId,
400 fullName, email, company, addressLine1, addressLine2, city,
401 state, zip, country, phone);
402 }
403 c.close();
404 autoFillDb.close();
405
406 // PreferenceManager.setDefaultValues is TOO SLOW, need to manually keep
407 // the defaults in sync
John Reck63bb6872010-12-01 19:29:32 -0800408 p.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
Ben Murdochef671652010-11-25 17:17:58 +0000409 syncSharedPreferences(mContext, p);
410
411 synchronized (sSingleton) {
412 mLoadFromDbComplete = true;
413 sSingleton.notify();
414 }
415 return null;
Grace Kloba9804c432009-12-02 11:07:40 -0800416 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800417 }
418
Narayan Kamath5119edd2011-02-23 15:49:17 +0000419 private void updateSearchEngine(Context ctx, String searchEngineName, boolean force) {
420 if (force || searchEngine == null ||
421 !searchEngine.getName().equals(searchEngineName)) {
422 if (searchEngine != null) {
423 if (searchEngine.supportsVoiceSearch()) {
424 // One or more tabs could have been in voice search mode.
425 // Clear it, since the new SearchEngine may not support
426 // it, or may handle it differently.
427 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
428 mController.getTabControl().getTab(i).revertVoiceSearchMode();
429 }
430 }
431 searchEngine.close();
432 }
433 searchEngine = SearchEngines.get(ctx, searchEngineName);
434
435 if (mController != null && (searchEngine instanceof InstantSearchEngine)) {
436 ((InstantSearchEngine) searchEngine).setController(mController);
437 }
438 }
439 }
440
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100441 /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700442
The Android Open Source Project0c908882009-03-03 19:32:16 -0800443 homeUrl =
444 p.getString(PREF_HOMEPAGE, homeUrl);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000445
446 useInstant = p.getBoolean(PREF_USE_INSTANT, useInstant);
Leon Scroggins III31306602010-09-17 11:10:29 -0400447 String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
Narayan Kamath5119edd2011-02-23 15:49:17 +0000448 SearchEngine.GOOGLE);
449 updateSearchEngine(ctx, searchEngineName, false);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700450
The Android Open Source Project0c908882009-03-03 19:32:16 -0800451 loadsImagesAutomatically = p.getBoolean("load_images",
452 loadsImagesAutomatically);
453 javaScriptEnabled = p.getBoolean("enable_javascript",
454 javaScriptEnabled);
Patrick Scotte536b332010-03-22 10:19:32 -0400455 pluginState = WebSettings.PluginState.valueOf(
John Reck282431b2011-01-20 17:38:37 -0800456 p.getString(PREF_PLUGIN_STATE, pluginState.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800457 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
458 "block_popup_windows",
459 !javaScriptCanOpenWindowsAutomatically);
460 showSecurityWarnings = p.getBoolean("show_security_warnings",
461 showSecurityWarnings);
462 rememberPasswords = p.getBoolean("remember_passwords",
463 rememberPasswords);
464 saveFormData = p.getBoolean("save_formdata",
465 saveFormData);
Ben Murdochaf554522010-09-10 22:09:30 +0100466 autoFillEnabled = p.getBoolean("autofill_enabled", autoFillEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800467 boolean accept_cookies = p.getBoolean("accept_cookies",
468 CookieManager.getInstance().acceptCookie());
469 CookieManager.getInstance().setAcceptCookie(accept_cookies);
470 openInBackground = p.getBoolean("open_in_background", openInBackground);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800471 textSize = WebSettings.TextSize.valueOf(
472 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700473 zoomDensity = WebSettings.ZoomDensity.valueOf(
474 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800475 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Grace Kloba5b4b8f12009-08-05 17:19:17 -0700476 loadsPageInOverviewMode = p.getBoolean("load_page",
477 loadsPageInOverviewMode);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800478 useWideViewPort = true; // use wide view port for either setting
479 if (autoFitPage) {
480 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
481 } else {
482 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
483 }
484 defaultTextEncodingName =
485 p.getString(PREF_DEFAULT_TEXT_ENCODING,
486 defaultTextEncodingName);
487
488 showDebugSettings =
489 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
490 // Debug menu items have precidence if the menu is visible
491 if (showDebugSettings) {
492 boolean small_screen = p.getBoolean("small_screen",
493 layoutAlgorithm ==
494 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
495 if (small_screen) {
496 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
497 } else {
498 boolean normal_layout = p.getBoolean("normal_layout",
499 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
500 if (normal_layout) {
501 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
502 } else {
503 layoutAlgorithm =
504 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
505 }
506 }
507 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
508 tracing = p.getBoolean("enable_tracing", tracing);
509 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
510 navDump = p.getBoolean("enable_nav_dump", navDump);
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800511 showVisualIndicator = p.getBoolean(PREF_VISUAL_INDICATOR, showVisualIndicator);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800512 }
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500513
Michael Kolb376b5412010-12-15 11:52:57 -0800514 quickControls = p.getBoolean(PREF_QUICK_CONTROLS, quickControls);
John Reckbafe58a2011-01-11 10:26:02 -0800515 useMostVisitedHomepage = p.getBoolean(PREF_MOST_VISITED_HOMEPAGE, useMostVisitedHomepage);
Michael Kolb376b5412010-12-15 11:52:57 -0800516
John Reck3db2e072011-01-24 14:57:18 -0800517 // Only set these if this is a dev build or debug is enabled
518 if (DEV_BUILD || showDebugSettings()) {
John Reck63bb6872010-12-01 19:29:32 -0800519 userAgent = Integer.parseInt(p.getString(PREF_USER_AGENT, "0"));
520 hardwareAccelerated = p.getBoolean(PREF_HARDWARE_ACCEL, hardwareAccelerated);
521 }
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500522
Feng Qianb3c02da2009-06-29 15:58:08 -0700523 // JS flags is loaded from DB even if showDebugSettings is false,
524 // so that it can be set once and be effective all the time.
525 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100526
527 // Read the setting for showing/hiding the JS Console always so that should the
528 // user enable debug settings, we already know if we should show the console.
529 // The user will never see the console unless they navigate to about:debug,
530 // regardless of the setting we read here. This setting is only used after debug
531 // is enabled.
532 showConsole = p.getBoolean("javascript_console", showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700533
Andrei Popescu01068702009-08-03 16:03:24 +0100534 // HTML5 API flags
535 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
536 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
537 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100538 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
Andrei Popescu01068702009-08-03 16:03:24 +0100539 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
Steve Blockf344d032009-07-30 10:50:45 +0100540
Grace Klobad9ee1392009-07-20 11:53:33 -0700541 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800542 }
543
The Android Open Source Project0c908882009-03-03 19:32:16 -0800544 public String getHomePage() {
John Reckbafe58a2011-01-11 10:26:02 -0800545 if (useMostVisitedHomepage) {
546 return HomeProvider.MOST_VISITED;
547 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800548 return homeUrl;
549 }
550
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100551 public SearchEngine getSearchEngine() {
552 return searchEngine;
553 }
554
Feng Qianb3c02da2009-06-29 15:58:08 -0700555 public String getJsFlags() {
556 return jsFlags;
557 }
558
Andrei Popescu79e82b72009-07-27 12:01:59 +0100559 public WebStorageSizeManager getWebStorageSizeManager() {
560 return webStorageSizeManager;
561 }
562
The Android Open Source Project0c908882009-03-03 19:32:16 -0800563 public void setHomePage(Context context, String url) {
564 Editor ed = PreferenceManager.
565 getDefaultSharedPreferences(context).edit();
566 ed.putString(PREF_HOMEPAGE, url);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700567 ed.apply();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800568 homeUrl = url;
569 }
570
The Android Open Source Project0c908882009-03-03 19:32:16 -0800571 public WebSettings.TextSize getTextSize() {
572 return textSize;
573 }
574
Grace Kloba2f830682009-06-25 11:08:53 -0700575 public WebSettings.ZoomDensity getDefaultZoom() {
576 return zoomDensity;
577 }
578
The Android Open Source Project0c908882009-03-03 19:32:16 -0800579 public boolean openInBackground() {
580 return openInBackground;
581 }
582
583 public boolean showSecurityWarnings() {
584 return showSecurityWarnings;
585 }
586
587 public boolean isTracing() {
588 return tracing;
589 }
590
591 public boolean isLightTouch() {
592 return lightTouch;
593 }
594
595 public boolean isNavDump() {
596 return navDump;
597 }
598
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500599 public boolean isHardwareAccelerated() {
600 return hardwareAccelerated;
601 }
602
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800603 public boolean showVisualIndicator() {
604 return showVisualIndicator;
605 }
606
Michael Kolb376b5412010-12-15 11:52:57 -0800607 public boolean useQuickControls() {
608 return quickControls;
609 }
610
John Reckbafe58a2011-01-11 10:26:02 -0800611 public boolean useMostVisitedHomepage() {
612 return useMostVisitedHomepage;
613 }
614
Narayan Kamath5119edd2011-02-23 15:49:17 +0000615 public boolean useInstant() {
616 return useInstant;
617 }
618
The Android Open Source Project0c908882009-03-03 19:32:16 -0800619 public boolean showDebugSettings() {
620 return showDebugSettings;
621 }
622
John Reck3db2e072011-01-24 14:57:18 -0800623 public void toggleDebugSettings(Context context) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800624 showDebugSettings = !showDebugSettings;
625 navDump = showDebugSettings;
John Reck3db2e072011-01-24 14:57:18 -0800626 syncSharedPreferences(context,
627 PreferenceManager.getDefaultSharedPreferences(context));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800628 update();
629 }
630
Ben Murdoch23da30e2010-10-26 15:18:44 +0100631 public void setAutoFillProfile(Context ctx, AutoFillProfile profile, Message msg) {
632 if (profile != null) {
633 setActiveAutoFillProfileId(ctx, profile.getUniqueId());
634 // Update the AutoFill DB with the new profile.
635 new SaveProfileToDbTask(ctx, msg).execute(profile);
636 } else {
637 // Delete the current profile.
Ben Murdoche8a28332010-11-17 14:16:21 +0000638 if (autoFillProfile != null) {
639 new DeleteProfileFromDbTask(ctx, msg).execute(autoFillProfile.getUniqueId());
640 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
641 }
Ben Murdoch23da30e2010-10-26 15:18:44 +0100642 }
643 autoFillProfile = profile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100644 }
645
646 public AutoFillProfile getAutoFillProfile() {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100647 return autoFillProfile;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100648 }
649
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100650 private void setActiveAutoFillProfileId(Context context, int activeProfileId) {
651 autoFillActiveProfileId = activeProfileId;
652 Editor ed = PreferenceManager.
653 getDefaultSharedPreferences(context).edit();
654 ed.putInt(PREF_AUTOFILL_ACTIVE_PROFILE_ID, activeProfileId);
655 ed.apply();
656 }
657
Ben Murdoch8029a772010-11-16 11:58:21 +0000658 /* package */ void disableAutoFill(Context ctx) {
659 autoFillEnabled = false;
660 Editor ed = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
661 ed.putBoolean(PREF_AUTOFILL_ENABLED, false);
662 ed.apply();
663 }
664
The Android Open Source Project0c908882009-03-03 19:32:16 -0800665 /**
666 * Add a WebSettings object to the list of observers that will be updated
667 * when update() is called.
668 *
669 * @param s A WebSettings object that is strictly tied to the life of a
670 * WebView.
671 */
672 public Observer addObserver(WebSettings s) {
673 Observer old = mWebSettingsToObservers.get(s);
674 if (old != null) {
675 super.deleteObserver(old);
676 }
677 Observer o = new Observer(s);
678 mWebSettingsToObservers.put(s, o);
679 super.addObserver(o);
680 return o;
681 }
682
683 /**
684 * Delete the given WebSettings observer from the list of observers.
685 * @param s The WebSettings object to be deleted.
686 */
687 public void deleteObserver(WebSettings s) {
688 Observer o = mWebSettingsToObservers.get(s);
689 if (o != null) {
690 mWebSettingsToObservers.remove(s);
691 super.deleteObserver(o);
692 }
693 }
694
695 /*
John Reck63bb6872010-12-01 19:29:32 -0800696 * Application level method for obtaining a single app instance of the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800697 * BrowserSettings.
698 */
John Reck63bb6872010-12-01 19:29:32 -0800699 public static BrowserSettings getInstance() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800700 if (sSingleton == null ) {
701 sSingleton = new BrowserSettings();
702 }
703 return sSingleton;
704 }
705
706 /*
707 * Package level method for associating the BrowserSettings with TabControl
708 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700709 /* package */void setController(Controller ctrl) {
710 mController = ctrl;
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800711 updateTabControlSettings();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000712
713 if (mController != null && (searchEngine instanceof InstantSearchEngine)) {
714 ((InstantSearchEngine) searchEngine).setController(mController);
715 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800716 }
717
718 /*
719 * Update all the observers of the object.
720 */
721 /*package*/ void update() {
722 setChanged();
723 notifyObservers();
724 }
725
726 /*package*/ void clearCache(Context context) {
727 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700728 if (mController != null) {
729 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800730 if (current != null) {
731 current.clearCache(true);
732 }
733 }
734 }
735
736 /*package*/ void clearCookies(Context context) {
737 CookieManager.getInstance().removeAllCookie();
738 }
739
740 /* package */void clearHistory(Context context) {
741 ContentResolver resolver = context.getContentResolver();
742 Browser.clearHistory(resolver);
743 Browser.clearSearches(resolver);
744 }
745
746 /* package */ void clearFormData(Context context) {
747 WebViewDatabase.getInstance(context).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700748 if (mController!= null) {
749 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100750 if (currentTopView != null) {
751 currentTopView.clearFormData();
752 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800753 }
754 }
755
756 /*package*/ void clearPasswords(Context context) {
757 WebViewDatabase db = WebViewDatabase.getInstance(context);
758 db.clearUsernamePassword();
759 db.clearHttpAuthUsernamePassword();
760 }
761
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800762 private void updateTabControlSettings() {
763 // Enable/disable the error console.
Michael Kolb8233fac2010-10-26 16:08:53 -0700764 mController.setShouldShowErrorConsole(
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800765 showDebugSettings && showConsole);
Shimeng (Simon) Wang161974f2010-03-09 14:30:07 -0800766 }
767
Nicolas Roard78a98e42009-05-11 13:34:17 +0100768 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100769 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100770 }
771
772 /*package*/ void clearLocationAccess(Context context) {
773 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100774 }
775
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400776 /*package*/ void resetDefaultPreferences(Context ctx) {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800777 reset();
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500778 SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700779 p.edit().clear().apply();
John Reck035a5642010-12-21 18:51:27 -0800780 PreferenceManager.setDefaultValues(ctx, R.xml.general_preferences, true);
781 PreferenceManager.setDefaultValues(ctx, R.xml.privacy_security_preferences, true);
Jeff Hamilton462b8e82010-09-23 14:33:43 -0500782 PreferenceManager.setDefaultValues(ctx, R.xml.advanced_preferences, true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700783 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700784 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Andrei Popescu79e82b72009-07-27 12:01:59 +0100785 // reset appcache max size
786 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
Ben Murdoch23da30e2010-10-26 15:18:44 +0100787 setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700788 }
789
John Reck33bbb802010-10-26 17:13:42 -0700790 /*package*/ static String getFactoryResetHomeUrl(Context context) {
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700791 String url = context.getResources().getString(R.string.homepage_base);
792 if (url.indexOf("{CID}") != -1) {
Patrick Scott43914692010-02-19 10:10:10 -0500793 url = url.replace("{CID}",
794 BrowserProvider.getClientId(context.getContentResolver()));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700795 }
796 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800797 }
798
The Android Open Source Project0c908882009-03-03 19:32:16 -0800799 // Private constructor that does nothing.
800 private BrowserSettings() {
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800801 reset();
802 }
803
804 private void reset() {
805 // Private variables for settings
806 // NOTE: these defaults need to be kept in sync with the XML
807 // until the performance of PreferenceManager.setDefaultValues()
808 // is improved.
809 loadsImagesAutomatically = true;
810 javaScriptEnabled = true;
Patrick Scotte536b332010-03-22 10:19:32 -0400811 pluginState = WebSettings.PluginState.ON;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800812 javaScriptCanOpenWindowsAutomatically = false;
813 showSecurityWarnings = true;
814 rememberPasswords = true;
815 saveFormData = true;
Ben Murdochdc62cb92010-11-23 15:11:29 +0000816 autoFillEnabled = true;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800817 openInBackground = false;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800818 autoFitPage = true;
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800819 loadsPageInOverviewMode = true;
820 showDebugSettings = false;
821 // HTML5 API flags
822 appCacheEnabled = true;
823 databaseEnabled = true;
824 domStorageEnabled = true;
825 geolocationEnabled = true;
826 workersEnabled = true; // only affects V8. JSC does not have a similar setting
The Android Open Source Project0c908882009-03-03 19:32:16 -0800827 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100828
Ben Murdoch23da30e2010-10-26 15:18:44 +0100829 private abstract class AutoFillProfileDbTask<T> extends AsyncTask<T, Void, Void> {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100830 Context mContext;
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100831 AutoFillProfileDatabase mAutoFillProfileDb;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100832 Message mCompleteMessage;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100833
Ben Murdoch23da30e2010-10-26 15:18:44 +0100834 public AutoFillProfileDbTask(Context ctx, Message msg) {
Ben Murdoch0cb81892010-10-08 12:41:33 +0100835 mContext = ctx;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100836 mCompleteMessage = msg;
837 }
838
839 protected void onPostExecute(Void result) {
840 if (mCompleteMessage != null) {
841 mCompleteMessage.sendToTarget();
842 }
843 mAutoFillProfileDb.close();
844 }
845
846 abstract protected Void doInBackground(T... values);
847 }
848
849
850 private class SaveProfileToDbTask extends AutoFillProfileDbTask<AutoFillProfile> {
851 public SaveProfileToDbTask(Context ctx, Message msg) {
852 super(ctx, msg);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100853 }
854
855 protected Void doInBackground(AutoFillProfile... values) {
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100856 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100857 assert autoFillActiveProfileId != NO_AUTOFILL_PROFILE_SET;
858 AutoFillProfile newProfile = values[0];
859 mAutoFillProfileDb.addOrUpdateProfile(autoFillActiveProfileId, newProfile);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100860 return null;
861 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100862 }
863
Ben Murdoch23da30e2010-10-26 15:18:44 +0100864 private class DeleteProfileFromDbTask extends AutoFillProfileDbTask<Integer> {
865 public DeleteProfileFromDbTask(Context ctx, Message msg) {
866 super(ctx, msg);
867 }
868
869 protected Void doInBackground(Integer... values) {
870 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
871 int id = values[0];
872 assert id > 0;
873 mAutoFillProfileDb.dropProfile(id);
874 return null;
875 }
876 }
John Reck63bb6872010-12-01 19:29:32 -0800877
878 @Override
879 public void onSharedPreferenceChanged(
880 SharedPreferences p, String key) {
881 if (PREF_HARDWARE_ACCEL.equals(key)) {
882 hardwareAccelerated = p.getBoolean(PREF_HARDWARE_ACCEL, hardwareAccelerated);
Teng-Hui Zhu930ea222011-02-16 11:22:21 -0800883 } else if (PREF_VISUAL_INDICATOR.equals(key)) {
884 showVisualIndicator = p.getBoolean(PREF_VISUAL_INDICATOR, showVisualIndicator);
John Reck63bb6872010-12-01 19:29:32 -0800885 } else if (PREF_USER_AGENT.equals(key)) {
886 userAgent = Integer.parseInt(p.getString(PREF_USER_AGENT, "0"));
887 update();
Michael Kolb376b5412010-12-15 11:52:57 -0800888 } else if (PREF_QUICK_CONTROLS.equals(key)) {
889 quickControls = p.getBoolean(PREF_QUICK_CONTROLS, quickControls);
John Reckbafe58a2011-01-11 10:26:02 -0800890 } else if (PREF_MOST_VISITED_HOMEPAGE.equals(key)) {
891 useMostVisitedHomepage = p.getBoolean(PREF_MOST_VISITED_HOMEPAGE, useMostVisitedHomepage);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000892 } else if (PREF_USE_INSTANT.equals(key)) {
893 useInstant = p.getBoolean(PREF_USE_INSTANT, useInstant);
894 updateSearchEngine(mController.getActivity(), SearchEngine.GOOGLE, true);
895 } else if (PREF_SEARCH_ENGINE.equals(key)) {
896 final String searchEngineName = p.getString(PREF_SEARCH_ENGINE,
897 SearchEngine.GOOGLE);
898 updateSearchEngine(mController.getActivity(), searchEngineName, false);
John Reck63bb6872010-12-01 19:29:32 -0800899 }
900 }
Ed Tama94de4f2011-05-10 19:37:10 -0700901
902 /*package*/ String getRlzValue() {
903 return mRlzValue;
904 }
905
906 /*package*/ void updateRlzValues(Context context) {
907 // Use AsyncTask because this queries both RlzProvider and Bookmarks URIs
908 new RlzUpdateTask(context).execute();
909 }
910
911 private class RlzUpdateTask extends AsyncTask<Void, Void, Void> {
912 private final Context context;
913
914 public RlzUpdateTask(Context context) {
915 this.context = context;
916 }
917
918 @Override
919 protected Void doInBackground(Void...unused) {
920 String rlz = retrieveRlzValue(context);
921 if (!rlz.isEmpty()) {
922 mRlzValue = rlz;
923 updateHomePageRlzParameter(context);
924 updateBookmarksRlzParameter(context);
925 }
926 return null;
927 }
928 }
929
930 // Update RLZ value if present in Home page
931 private void updateHomePageRlzParameter(Context context) {
932 Uri uri = Uri.parse(homeUrl);
933 if ((uri.getQueryParameter("rlz") != null) && UrlUtils.isGoogleUri(uri)) {
934 String newHomeUrl = updateRlzParameter(homeUrl);
935 if (!homeUrl.equals(newHomeUrl)) {
936 setHomePage(context, newHomeUrl);
937 }
938 }
939 }
940
941 // Update RLZ value if present in bookmarks
942 private void updateBookmarksRlzParameter(Context context) {
943 Cursor cur = null;
944 try {
945 cur = context.getContentResolver().query(Bookmarks.CONTENT_URI_DEFAULT_FOLDER,
946 new String[] { Bookmarks._ID, Bookmarks.URL }, "url LIKE '%rlz=%'", null, null);
947 if ((cur == null) || (cur.getCount() == 0)) {
948 return;
949 }
950 for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
951 long id = cur.getLong(0);
952 String url = cur.getString(1);
953 if ((url == null) || url.isEmpty()) {
954 continue;
955 }
956
957 Uri uri = Uri.parse(url);
958 if ((uri.getQueryParameter("rlz") != null) && UrlUtils.isGoogleUri(uri)) {
959 String newUrl = updateRlzParameter(url);
960 if (!url.equals(newUrl)) {
961 ContentValues values = new ContentValues();
962 values.put(Bookmarks.URL, newUrl);
963 Uri bookmarkUri = ContentUris.withAppendedId(
964 BookmarkUtils.getBookmarksUri(context), id);
965 context.getContentResolver().update(bookmarkUri, values, null, null);
966 }
967 }
968 }
969 } finally {
970 if (cur != null) {
971 cur.close();
972 }
973 }
974 }
975
976 private String updateRlzParameter(String url) {
977 Uri uri = Uri.parse(url);
978 String oldRlz = uri.getQueryParameter("rlz");
979 if (oldRlz != null) {
980 return url.replace("rlz=" + oldRlz, "rlz=" + mRlzValue);
981 }
982 return url;
983 }
984
985 // Retrieve the RLZ value from the Rlz Provider
986 private static String retrieveRlzValue(Context context) {
987 String rlz = "";
988 PackageManager pm = context.getPackageManager();
989 if (pm.resolveContentProvider(RLZ_PROVIDER, 0) == null) {
990 return rlz;
991 }
992
993 String ap = context.getResources().getString(R.string.rlz_access_point);
994 if (ap.isEmpty()) {
995 return rlz;
996 }
997
998 Uri rlzUri = Uri.withAppendedPath(RLZ_PROVIDER_URI, ap);
999 Cursor cur = null;
1000 try {
1001 cur = context.getContentResolver().query(rlzUri, null, null, null, null);
1002 if (cur != null && cur.moveToFirst() && !cur.isNull(0)) {
1003 rlz = cur.getString(0);
1004 }
1005 } finally {
1006 if (cur != null) {
1007 cur.close();
1008 }
1009 }
1010 return rlz;
1011 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001012}