Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 1 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 2 | /* |
| 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 | |
| 18 | package com.android.browser; |
| 19 | |
John Reck | bafe58a | 2011-01-11 10:26:02 -0800 | [diff] [blame] | 20 | import com.android.browser.homepages.HomeProvider; |
Bjorn Bringert | d69f51d | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 21 | import com.android.browser.search.SearchEngine; |
| 22 | import com.android.browser.search.SearchEngines; |
| 23 | |
Grace Kloba | 9804c43 | 2009-12-02 11:07:40 -0800 | [diff] [blame] | 24 | import android.app.ActivityManager; |
Bjorn Bringert | d69f51d | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 25 | import android.content.ComponentName; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 26 | import android.content.ContentResolver; |
| 27 | import android.content.Context; |
John Reck | 63bb687 | 2010-12-01 19:29:32 -0800 | [diff] [blame] | 28 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 29 | import android.content.pm.ActivityInfo; |
| 30 | import android.content.SharedPreferences; |
| 31 | import android.content.SharedPreferences.Editor; |
Bjorn Bringert | d69f51d | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 32 | import android.database.ContentObserver; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 33 | import android.database.Cursor; |
Jeff Davidson | 4361029 | 2010-07-16 16:03:58 -0700 | [diff] [blame] | 34 | import android.net.Uri; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 35 | import android.os.AsyncTask; |
Bjorn Bringert | d69f51d | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 36 | import android.os.Handler; |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 37 | import android.os.Message; |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 38 | import android.preference.PreferenceActivity; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 39 | import android.preference.PreferenceManager; |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 40 | import android.preference.PreferenceScreen; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 41 | import android.provider.Browser; |
Bjorn Bringert | d69f51d | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 42 | import android.provider.Settings; |
| 43 | import android.util.Log; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 44 | import android.webkit.CookieManager; |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 45 | import android.webkit.GeolocationPermissions; |
Nicolas Roard | 99b3ae1 | 2009-09-22 15:17:52 +0100 | [diff] [blame] | 46 | import android.webkit.ValueCallback; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 47 | import android.webkit.WebView; |
| 48 | import android.webkit.WebViewDatabase; |
| 49 | import android.webkit.WebIconDatabase; |
| 50 | import android.webkit.WebSettings; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 51 | import android.webkit.WebSettings.AutoFillProfile; |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 52 | import android.webkit.WebStorage; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 53 | import android.widget.Toast; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 54 | |
| 55 | import java.util.HashMap; |
Nicolas Roard | 99b3ae1 | 2009-09-22 15:17:52 +0100 | [diff] [blame] | 56 | import java.util.Map; |
| 57 | import java.util.Set; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 58 | import java.util.Observable; |
| 59 | |
| 60 | /* |
| 61 | * Package level class for storing various WebView and Browser settings. To use |
| 62 | * this class: |
| 63 | * BrowserSettings s = BrowserSettings.getInstance(); |
| 64 | * s.addObserver(webView.getSettings()); |
| 65 | * s.loadFromDb(context); // Only needed on app startup |
| 66 | * s.javaScriptEnabled = true; |
| 67 | * ... // set any other settings |
| 68 | * s.update(); // this will update all the observers |
| 69 | * |
| 70 | * To remove an observer: |
| 71 | * s.deleteObserver(webView.getSettings()); |
| 72 | */ |
John Reck | 63bb687 | 2010-12-01 19:29:32 -0800 | [diff] [blame] | 73 | public class BrowserSettings extends Observable implements OnSharedPreferenceChangeListener { |
Leon Scroggins | 9ac587d | 2009-04-20 12:53:46 -0400 | [diff] [blame] | 74 | // Private variables for settings |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 75 | // NOTE: these defaults need to be kept in sync with the XML |
| 76 | // until the performance of PreferenceManager.setDefaultValues() |
| 77 | // is improved. |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 78 | // Note: boolean variables are set inside reset function. |
| 79 | private boolean loadsImagesAutomatically; |
| 80 | private boolean javaScriptEnabled; |
Patrick Scott | e536b33 | 2010-03-22 10:19:32 -0400 | [diff] [blame] | 81 | private WebSettings.PluginState pluginState; |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 82 | private boolean javaScriptCanOpenWindowsAutomatically; |
| 83 | private boolean showSecurityWarnings; |
| 84 | private boolean rememberPasswords; |
| 85 | private boolean saveFormData; |
Ben Murdoch | ce549fc | 2010-09-09 10:28:08 +0100 | [diff] [blame] | 86 | private boolean autoFillEnabled; |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 87 | private boolean openInBackground; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 88 | private String defaultTextEncodingName; |
Grace Kloba | f2c5c1b | 2009-05-26 10:48:31 -0700 | [diff] [blame] | 89 | private String homeUrl = ""; |
Bjorn Bringert | d69f51d | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 90 | private SearchEngine searchEngine; |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 91 | private boolean autoFitPage; |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 92 | private boolean loadsPageInOverviewMode; |
| 93 | private boolean showDebugSettings; |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 94 | // HTML5 API flags |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 95 | private boolean appCacheEnabled; |
| 96 | private boolean databaseEnabled; |
| 97 | private boolean domStorageEnabled; |
| 98 | private boolean geolocationEnabled; |
| 99 | private boolean workersEnabled; // only affects V8. JSC does not have a similar setting |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 100 | // HTML5 API configuration params |
| 101 | private long appCacheMaxSize = Long.MAX_VALUE; |
| 102 | private String appCachePath; // default value set in loadFromDb(). |
| 103 | private String databasePath; // default value set in loadFromDb() |
Steve Block | 5029cf0 | 2009-08-21 13:16:58 +0100 | [diff] [blame] | 104 | private String geolocationDatabasePath; // default value set in loadFromDb() |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 105 | private WebStorageSizeManager webStorageSizeManager; |
| 106 | |
| 107 | private String jsFlags = ""; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 108 | |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 109 | private final static String TAG = "BrowserSettings"; |
| 110 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 111 | // Development settings |
| 112 | public WebSettings.LayoutAlgorithm layoutAlgorithm = |
| 113 | WebSettings.LayoutAlgorithm.NARROW_COLUMNS; |
| 114 | private boolean useWideViewPort = true; |
| 115 | private int userAgent = 0; |
| 116 | private boolean tracing = false; |
| 117 | private boolean lightTouch = false; |
| 118 | private boolean navDump = false; |
Derek Sollenberger | 8de8285 | 2010-11-18 19:51:50 -0500 | [diff] [blame] | 119 | private boolean hardwareAccelerated = true; |
Feng Qian | b3c02da | 2009-06-29 15:58:08 -0700 | [diff] [blame] | 120 | |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 121 | // Lab settings |
| 122 | private boolean quickControls = false; |
John Reck | bafe58a | 2011-01-11 10:26:02 -0800 | [diff] [blame] | 123 | private boolean useMostVisitedHomepage = false; |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 124 | |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 125 | // By default the error console is shown once the user navigates to about:debug. |
| 126 | // The setting can be then toggled from the settings menu. |
| 127 | private boolean showConsole = true; |
| 128 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 129 | // Private preconfigured values |
Shimeng (Simon) Wang | b4fb25c | 2010-07-13 15:18:10 -0700 | [diff] [blame] | 130 | private static int minimumFontSize = 1; |
| 131 | private static int minimumLogicalFontSize = 1; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 132 | private static int defaultFontSize = 16; |
| 133 | private static int defaultFixedFontSize = 13; |
| 134 | private static WebSettings.TextSize textSize = |
| 135 | WebSettings.TextSize.NORMAL; |
Grace Kloba | 2f83068 | 2009-06-25 11:08:53 -0700 | [diff] [blame] | 136 | private static WebSettings.ZoomDensity zoomDensity = |
| 137 | WebSettings.ZoomDensity.MEDIUM; |
Grace Kloba | 9804c43 | 2009-12-02 11:07:40 -0800 | [diff] [blame] | 138 | private static int pageCacheCapacity; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 139 | |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 140 | |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 141 | private AutoFillProfile autoFillProfile; |
Ben Murdoch | 6fa32ba | 2010-10-20 14:01:25 +0100 | [diff] [blame] | 142 | // Default to zero. In the case no profile is set up, the initial |
| 143 | // value will come from the AutoFillSettingsFragment when the user |
| 144 | // creates a profile. Otherwise, we'll read the ID of the last used |
| 145 | // profile from the prefs db. |
| 146 | private int autoFillActiveProfileId; |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 147 | private static final int NO_AUTOFILL_PROFILE_SET = 0; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 148 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 149 | // Preference keys that are used outside this class |
| 150 | public final static String PREF_CLEAR_CACHE = "privacy_clear_cache"; |
| 151 | public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies"; |
| 152 | public final static String PREF_CLEAR_HISTORY = "privacy_clear_history"; |
| 153 | public final static String PREF_HOMEPAGE = "homepage"; |
Bjorn Bringert | d69f51d | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 154 | public final static String PREF_SEARCH_ENGINE = "search_engine"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 155 | public final static String PREF_CLEAR_FORM_DATA = |
| 156 | "privacy_clear_form_data"; |
| 157 | public final static String PREF_CLEAR_PASSWORDS = |
| 158 | "privacy_clear_passwords"; |
| 159 | public final static String PREF_EXTRAS_RESET_DEFAULTS = |
| 160 | "reset_default_preferences"; |
| 161 | public final static String PREF_DEBUG_SETTINGS = "debug_menu"; |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 162 | public final static String PREF_WEBSITE_SETTINGS = "website_settings"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 163 | public final static String PREF_TEXT_SIZE = "text_size"; |
Grace Kloba | 2f83068 | 2009-06-25 11:08:53 -0700 | [diff] [blame] | 164 | public final static String PREF_DEFAULT_ZOOM = "default_zoom"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 165 | public final static String PREF_DEFAULT_TEXT_ENCODING = |
| 166 | "default_text_encoding"; |
Steve Block | c6f577f | 2009-08-20 18:11:08 +0100 | [diff] [blame] | 167 | public final static String PREF_CLEAR_GEOLOCATION_ACCESS = |
| 168 | "privacy_clear_geolocation_access"; |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 169 | public final static String PREF_AUTOFILL_ENABLED = "autofill_enabled"; |
| 170 | public final static String PREF_AUTOFILL_PROFILE = "autofill_profile"; |
Ben Murdoch | 6fa32ba | 2010-10-20 14:01:25 +0100 | [diff] [blame] | 171 | public final static String PREF_AUTOFILL_ACTIVE_PROFILE_ID = "autofill_active_profile_id"; |
John Reck | 63bb687 | 2010-12-01 19:29:32 -0800 | [diff] [blame] | 172 | public final static String PREF_HARDWARE_ACCEL = "enable_hardware_accel"; |
| 173 | public final static String PREF_USER_AGENT = "user_agent"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 174 | |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 175 | public final static String PREF_QUICK_CONTROLS = "enable_quick_controls"; |
John Reck | bafe58a | 2011-01-11 10:26:02 -0800 | [diff] [blame] | 176 | public final static String PREF_MOST_VISITED_HOMEPAGE = "use_most_visited_homepage"; |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 177 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 178 | private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " + |
Bart Sears | f6915fb | 2010-07-08 19:58:22 -0700 | [diff] [blame] | 179 | "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " + |
| 180 | "like Gecko) Version/5.0 Safari/533.16"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 181 | |
| 182 | private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " + |
Bart Sears | f6915fb | 2010-07-08 19:58:22 -0700 | [diff] [blame] | 183 | "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " + |
| 184 | "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7"; |
| 185 | |
| 186 | private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " + |
| 187 | "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " + |
| 188 | "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10"; |
| 189 | |
| 190 | private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " + |
| 191 | "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " + |
| 192 | "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 193 | |
| 194 | // Value to truncate strings when adding them to a TextView within |
| 195 | // a ListView |
| 196 | public final static int MAX_TEXTVIEW_LEN = 80; |
| 197 | |
Jeff Davidson | 4361029 | 2010-07-16 16:03:58 -0700 | [diff] [blame] | 198 | public static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider"; |
| 199 | |
| 200 | public static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/"); |
| 201 | |
John Reck | 63bb687 | 2010-12-01 19:29:32 -0800 | [diff] [blame] | 202 | // Set to true to enable some of the about:debug options |
John Reck | 9eed0ef | 2011-01-11 17:11:37 -0800 | [diff] [blame^] | 203 | public static final boolean DEV_BUILD = false; |
John Reck | 63bb687 | 2010-12-01 19:29:32 -0800 | [diff] [blame] | 204 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 205 | private Controller mController; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 206 | |
| 207 | // Single instance of the BrowserSettings for use in the Browser app. |
| 208 | private static BrowserSettings sSingleton; |
| 209 | |
| 210 | // Private map of WebSettings to Observer objects used when deleting an |
| 211 | // observer. |
| 212 | private HashMap<WebSettings,Observer> mWebSettingsToObservers = |
| 213 | new HashMap<WebSettings,Observer>(); |
| 214 | |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 215 | private boolean mLoadFromDbComplete; |
| 216 | |
| 217 | public void waitForLoadFromDbToComplete() { |
| 218 | synchronized (sSingleton) { |
| 219 | while (!mLoadFromDbComplete) { |
| 220 | try { |
| 221 | sSingleton.wait(); |
| 222 | } catch (InterruptedException e) { } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 227 | /* |
| 228 | * An observer wrapper for updating a WebSettings object with the new |
| 229 | * settings after a call to BrowserSettings.update(). |
| 230 | */ |
| 231 | static class Observer implements java.util.Observer { |
| 232 | // Private WebSettings object that will be updated. |
| 233 | private WebSettings mSettings; |
| 234 | |
| 235 | Observer(WebSettings w) { |
| 236 | mSettings = w; |
| 237 | } |
| 238 | |
| 239 | public void update(Observable o, Object arg) { |
| 240 | BrowserSettings b = (BrowserSettings)o; |
| 241 | WebSettings s = mSettings; |
| 242 | |
| 243 | s.setLayoutAlgorithm(b.layoutAlgorithm); |
| 244 | if (b.userAgent == 0) { |
| 245 | // use the default ua string |
| 246 | s.setUserAgentString(null); |
| 247 | } else if (b.userAgent == 1) { |
| 248 | s.setUserAgentString(DESKTOP_USERAGENT); |
| 249 | } else if (b.userAgent == 2) { |
| 250 | s.setUserAgentString(IPHONE_USERAGENT); |
Bart Sears | f6915fb | 2010-07-08 19:58:22 -0700 | [diff] [blame] | 251 | } else if (b.userAgent == 3) { |
| 252 | s.setUserAgentString(IPAD_USERAGENT); |
| 253 | } else if (b.userAgent == 4) { |
| 254 | s.setUserAgentString(FROYO_USERAGENT); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 255 | } |
| 256 | s.setUseWideViewPort(b.useWideViewPort); |
| 257 | s.setLoadsImagesAutomatically(b.loadsImagesAutomatically); |
| 258 | s.setJavaScriptEnabled(b.javaScriptEnabled); |
Patrick Scott | e536b33 | 2010-03-22 10:19:32 -0400 | [diff] [blame] | 259 | s.setPluginState(b.pluginState); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 260 | 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 Kloba | 2f83068 | 2009-06-25 11:08:53 -0700 | [diff] [blame] | 269 | s.setDefaultZoom(b.zoomDensity); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 270 | s.setLightTouchEnabled(b.lightTouch); |
| 271 | s.setSaveFormData(b.saveFormData); |
Ben Murdoch | ce549fc | 2010-09-09 10:28:08 +0100 | [diff] [blame] | 272 | s.setAutoFillEnabled(b.autoFillEnabled); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 273 | s.setSavePassword(b.rememberPasswords); |
Grace Kloba | 5b4b8f1 | 2009-08-05 17:19:17 -0700 | [diff] [blame] | 274 | s.setLoadWithOverviewMode(b.loadsPageInOverviewMode); |
Grace Kloba | 7956503 | 2009-11-24 14:23:39 -0800 | [diff] [blame] | 275 | s.setPageCacheCapacity(pageCacheCapacity); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 276 | |
| 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 Kloba | 61fc77c | 2010-06-22 09:57:33 -0700 | [diff] [blame] | 281 | // enable smooth transition for better performance during panning or |
| 282 | // zooming |
| 283 | s.setEnableSmoothTransition(true); |
Ben Murdoch | 092dd5d | 2009-04-22 12:34:12 +0100 | [diff] [blame] | 284 | |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 285 | // HTML5 API flags |
| 286 | s.setAppCacheEnabled(b.appCacheEnabled); |
Ben Murdoch | 092dd5d | 2009-04-22 12:34:12 +0100 | [diff] [blame] | 287 | s.setDatabaseEnabled(b.databaseEnabled); |
Ben Murdoch | 734a066 | 2009-06-02 19:11:52 +0100 | [diff] [blame] | 288 | s.setDomStorageEnabled(b.domStorageEnabled); |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 289 | s.setWorkersEnabled(b.workersEnabled); // This only affects V8. |
Steve Block | 97b0802 | 2009-08-21 10:26:25 +0100 | [diff] [blame] | 290 | s.setGeolocationEnabled(b.geolocationEnabled); |
Ben Murdoch | 092dd5d | 2009-04-22 12:34:12 +0100 | [diff] [blame] | 291 | |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 292 | // HTML5 configuration parameters. |
Andrei Popescu | 20634ce | 2009-07-22 16:46:55 +0100 | [diff] [blame] | 293 | s.setAppCacheMaxSize(b.appCacheMaxSize); |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 294 | s.setAppCachePath(b.appCachePath); |
| 295 | s.setDatabasePath(b.databasePath); |
Steve Block | 5029cf0 | 2009-08-21 13:16:58 +0100 | [diff] [blame] | 296 | s.setGeolocationDatabasePath(b.geolocationDatabasePath); |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 297 | |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 298 | // Active AutoFill profile data. |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 299 | s.setAutoFillProfile(b.autoFillProfile); |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 300 | |
Shimeng (Simon) Wang | 161974f | 2010-03-09 14:30:07 -0800 | [diff] [blame] | 301 | b.updateTabControlSettings(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 306 | * Load settings from the browser app's database. It is performed in |
| 307 | * an AsyncTask as it involves plenty of slow disk IO. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 308 | * NOTE: Strings used for the preferences must match those specified |
Jeff Hamilton | 175ab30 | 2010-10-04 12:53:49 -0500 | [diff] [blame] | 309 | * in the various preference XML files. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 310 | * @param ctx A Context object used to query the browser's settings |
| 311 | * database. If the database exists, the saved settings will be |
| 312 | * stored in this BrowserSettings object. This will update all |
| 313 | * observers of this object. |
| 314 | */ |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 315 | public void asyncLoadFromDb(final Context ctx) { |
| 316 | mLoadFromDbComplete = false; |
| 317 | // Run the initial settings load in an AsyncTask as it hits the |
| 318 | // disk multiple times through SharedPreferences and SQLite. We |
| 319 | // need to be certain though that this has completed before we start |
| 320 | // to load pages though, so in the worst case we will block waiting |
| 321 | // for it to finish in BrowserActivity.onCreate(). |
| 322 | new LoadFromDbTask(ctx).execute(); |
| 323 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 324 | |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 325 | private class LoadFromDbTask extends AsyncTask<Void, Void, Void> { |
| 326 | private Context mContext; |
| 327 | |
| 328 | public LoadFromDbTask(Context context) { |
| 329 | mContext = context; |
Shimeng (Simon) Wang | e83e906 | 2010-03-29 16:13:09 -0700 | [diff] [blame] | 330 | } |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 331 | |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 332 | protected Void doInBackground(Void... unused) { |
| 333 | SharedPreferences p = |
| 334 | PreferenceManager.getDefaultSharedPreferences(mContext); |
| 335 | // Set the default value for the Application Caches path. |
| 336 | appCachePath = mContext.getDir("appcache", 0).getPath(); |
| 337 | // Determine the maximum size of the application cache. |
| 338 | webStorageSizeManager = new WebStorageSizeManager( |
| 339 | mContext, |
| 340 | new WebStorageSizeManager.StatFsDiskInfo(appCachePath), |
| 341 | new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath)); |
| 342 | appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize(); |
| 343 | // Set the default value for the Database path. |
| 344 | databasePath = mContext.getDir("databases", 0).getPath(); |
| 345 | // Set the default value for the Geolocation database path. |
| 346 | geolocationDatabasePath = mContext.getDir("geolocation", 0).getPath(); |
| 347 | |
John Reck | ef07426 | 2010-12-02 16:09:14 -0800 | [diff] [blame] | 348 | if (p.getString(PREF_HOMEPAGE, null) == null) { |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 349 | // No home page preferences is set, set it to default. |
| 350 | setHomePage(mContext, getFactoryResetHomeUrl(mContext)); |
| 351 | } |
| 352 | |
| 353 | // the cost of one cached page is ~3M (measured using nytimes.com). For |
| 354 | // low end devices, we only cache one page. For high end devices, we try |
| 355 | // to cache more pages, currently choose 5. |
| 356 | ActivityManager am = (ActivityManager) mContext |
| 357 | .getSystemService(Context.ACTIVITY_SERVICE); |
| 358 | if (am.getMemoryClass() > 16) { |
| 359 | pageCacheCapacity = 5; |
| 360 | } else { |
| 361 | pageCacheCapacity = 1; |
| 362 | } |
| 363 | |
| 364 | // Read the last active AutoFill profile id. |
| 365 | autoFillActiveProfileId = p.getInt( |
| 366 | PREF_AUTOFILL_ACTIVE_PROFILE_ID, autoFillActiveProfileId); |
| 367 | |
| 368 | // Load the autofill profile data from the database. We use a database separate |
| 369 | // to the browser preference DB to make it easier to support multiple profiles |
| 370 | // and switching between them. |
| 371 | AutoFillProfileDatabase autoFillDb = AutoFillProfileDatabase.getInstance(mContext); |
| 372 | Cursor c = autoFillDb.getProfile(autoFillActiveProfileId); |
| 373 | |
| 374 | if (c.getCount() > 0) { |
| 375 | c.moveToFirst(); |
| 376 | |
| 377 | String fullName = c.getString(c.getColumnIndex( |
| 378 | AutoFillProfileDatabase.Profiles.FULL_NAME)); |
| 379 | String email = c.getString(c.getColumnIndex( |
| 380 | AutoFillProfileDatabase.Profiles.EMAIL_ADDRESS)); |
| 381 | String company = c.getString(c.getColumnIndex( |
| 382 | AutoFillProfileDatabase.Profiles.COMPANY_NAME)); |
| 383 | String addressLine1 = c.getString(c.getColumnIndex( |
| 384 | AutoFillProfileDatabase.Profiles.ADDRESS_LINE_1)); |
| 385 | String addressLine2 = c.getString(c.getColumnIndex( |
| 386 | AutoFillProfileDatabase.Profiles.ADDRESS_LINE_2)); |
| 387 | String city = c.getString(c.getColumnIndex( |
| 388 | AutoFillProfileDatabase.Profiles.CITY)); |
| 389 | String state = c.getString(c.getColumnIndex( |
| 390 | AutoFillProfileDatabase.Profiles.STATE)); |
| 391 | String zip = c.getString(c.getColumnIndex( |
| 392 | AutoFillProfileDatabase.Profiles.ZIP_CODE)); |
| 393 | String country = c.getString(c.getColumnIndex( |
| 394 | AutoFillProfileDatabase.Profiles.COUNTRY)); |
| 395 | String phone = c.getString(c.getColumnIndex( |
| 396 | AutoFillProfileDatabase.Profiles.PHONE_NUMBER)); |
| 397 | autoFillProfile = new AutoFillProfile(autoFillActiveProfileId, |
| 398 | fullName, email, company, addressLine1, addressLine2, city, |
| 399 | state, zip, country, phone); |
| 400 | } |
| 401 | c.close(); |
| 402 | autoFillDb.close(); |
| 403 | |
| 404 | // PreferenceManager.setDefaultValues is TOO SLOW, need to manually keep |
| 405 | // the defaults in sync |
John Reck | 63bb687 | 2010-12-01 19:29:32 -0800 | [diff] [blame] | 406 | p.registerOnSharedPreferenceChangeListener(BrowserSettings.this); |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 407 | syncSharedPreferences(mContext, p); |
| 408 | |
| 409 | synchronized (sSingleton) { |
| 410 | mLoadFromDbComplete = true; |
| 411 | sSingleton.notify(); |
| 412 | } |
| 413 | return null; |
Grace Kloba | 9804c43 | 2009-12-02 11:07:40 -0800 | [diff] [blame] | 414 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Bjorn Bringert | d69f51d | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 417 | /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) { |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 418 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 419 | homeUrl = |
| 420 | p.getString(PREF_HOMEPAGE, homeUrl); |
Leon Scroggins III | 3130660 | 2010-09-17 11:10:29 -0400 | [diff] [blame] | 421 | String searchEngineName = p.getString(PREF_SEARCH_ENGINE, |
| 422 | SearchEngine.GOOGLE); |
Bjorn Bringert | d69f51d | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 423 | if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) { |
| 424 | if (searchEngine != null) { |
Leon Scroggins III | 95d9bfd | 2010-09-14 14:02:36 -0400 | [diff] [blame] | 425 | if (searchEngine.supportsVoiceSearch()) { |
| 426 | // One or more tabs could have been in voice search mode. |
| 427 | // Clear it, since the new SearchEngine may not support |
| 428 | // it, or may handle it differently. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 429 | for (int i = 0; i < mController.getTabControl().getTabCount(); i++) { |
| 430 | mController.getTabControl().getTab(i).revertVoiceSearchMode(); |
Leon Scroggins III | 95d9bfd | 2010-09-14 14:02:36 -0400 | [diff] [blame] | 431 | } |
| 432 | } |
Bjorn Bringert | d69f51d | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 433 | searchEngine.close(); |
| 434 | } |
| 435 | searchEngine = SearchEngines.get(ctx, searchEngineName); |
| 436 | } |
| 437 | Log.i(TAG, "Selected search engine: " + searchEngine); |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 438 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 439 | loadsImagesAutomatically = p.getBoolean("load_images", |
| 440 | loadsImagesAutomatically); |
| 441 | javaScriptEnabled = p.getBoolean("enable_javascript", |
| 442 | javaScriptEnabled); |
Patrick Scott | e536b33 | 2010-03-22 10:19:32 -0400 | [diff] [blame] | 443 | pluginState = WebSettings.PluginState.valueOf( |
| 444 | p.getString("plugin_state", pluginState.name())); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 445 | javaScriptCanOpenWindowsAutomatically = !p.getBoolean( |
| 446 | "block_popup_windows", |
| 447 | !javaScriptCanOpenWindowsAutomatically); |
| 448 | showSecurityWarnings = p.getBoolean("show_security_warnings", |
| 449 | showSecurityWarnings); |
| 450 | rememberPasswords = p.getBoolean("remember_passwords", |
| 451 | rememberPasswords); |
| 452 | saveFormData = p.getBoolean("save_formdata", |
| 453 | saveFormData); |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 454 | autoFillEnabled = p.getBoolean("autofill_enabled", autoFillEnabled); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 455 | boolean accept_cookies = p.getBoolean("accept_cookies", |
| 456 | CookieManager.getInstance().acceptCookie()); |
| 457 | CookieManager.getInstance().setAcceptCookie(accept_cookies); |
| 458 | openInBackground = p.getBoolean("open_in_background", openInBackground); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 459 | textSize = WebSettings.TextSize.valueOf( |
| 460 | p.getString(PREF_TEXT_SIZE, textSize.name())); |
Grace Kloba | 2f83068 | 2009-06-25 11:08:53 -0700 | [diff] [blame] | 461 | zoomDensity = WebSettings.ZoomDensity.valueOf( |
| 462 | p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name())); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 463 | autoFitPage = p.getBoolean("autofit_pages", autoFitPage); |
Grace Kloba | 5b4b8f1 | 2009-08-05 17:19:17 -0700 | [diff] [blame] | 464 | loadsPageInOverviewMode = p.getBoolean("load_page", |
| 465 | loadsPageInOverviewMode); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 466 | useWideViewPort = true; // use wide view port for either setting |
| 467 | if (autoFitPage) { |
| 468 | layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS; |
| 469 | } else { |
| 470 | layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL; |
| 471 | } |
| 472 | defaultTextEncodingName = |
| 473 | p.getString(PREF_DEFAULT_TEXT_ENCODING, |
| 474 | defaultTextEncodingName); |
| 475 | |
| 476 | showDebugSettings = |
| 477 | p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings); |
| 478 | // Debug menu items have precidence if the menu is visible |
| 479 | if (showDebugSettings) { |
| 480 | boolean small_screen = p.getBoolean("small_screen", |
| 481 | layoutAlgorithm == |
| 482 | WebSettings.LayoutAlgorithm.SINGLE_COLUMN); |
| 483 | if (small_screen) { |
| 484 | layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN; |
| 485 | } else { |
| 486 | boolean normal_layout = p.getBoolean("normal_layout", |
| 487 | layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL); |
| 488 | if (normal_layout) { |
| 489 | layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL; |
| 490 | } else { |
| 491 | layoutAlgorithm = |
| 492 | WebSettings.LayoutAlgorithm.NARROW_COLUMNS; |
| 493 | } |
| 494 | } |
| 495 | useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort); |
| 496 | tracing = p.getBoolean("enable_tracing", tracing); |
| 497 | lightTouch = p.getBoolean("enable_light_touch", lightTouch); |
| 498 | navDump = p.getBoolean("enable_nav_dump", navDump); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 499 | } |
Derek Sollenberger | ffa561e | 2010-11-16 14:19:01 -0500 | [diff] [blame] | 500 | |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 501 | quickControls = p.getBoolean(PREF_QUICK_CONTROLS, quickControls); |
John Reck | bafe58a | 2011-01-11 10:26:02 -0800 | [diff] [blame] | 502 | useMostVisitedHomepage = p.getBoolean(PREF_MOST_VISITED_HOMEPAGE, useMostVisitedHomepage); |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 503 | |
John Reck | 63bb687 | 2010-12-01 19:29:32 -0800 | [diff] [blame] | 504 | // Only set these on startup if it is a dev build |
| 505 | if (DEV_BUILD) { |
| 506 | userAgent = Integer.parseInt(p.getString(PREF_USER_AGENT, "0")); |
| 507 | hardwareAccelerated = p.getBoolean(PREF_HARDWARE_ACCEL, hardwareAccelerated); |
| 508 | } |
Derek Sollenberger | ffa561e | 2010-11-16 14:19:01 -0500 | [diff] [blame] | 509 | |
Feng Qian | b3c02da | 2009-06-29 15:58:08 -0700 | [diff] [blame] | 510 | // JS flags is loaded from DB even if showDebugSettings is false, |
| 511 | // so that it can be set once and be effective all the time. |
| 512 | jsFlags = p.getString("js_engine_flags", ""); |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 513 | |
| 514 | // Read the setting for showing/hiding the JS Console always so that should the |
| 515 | // user enable debug settings, we already know if we should show the console. |
| 516 | // The user will never see the console unless they navigate to about:debug, |
| 517 | // regardless of the setting we read here. This setting is only used after debug |
| 518 | // is enabled. |
| 519 | showConsole = p.getBoolean("javascript_console", showConsole); |
Grace Kloba | d9ee139 | 2009-07-20 11:53:33 -0700 | [diff] [blame] | 520 | |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 521 | // HTML5 API flags |
| 522 | appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled); |
| 523 | databaseEnabled = p.getBoolean("enable_database", databaseEnabled); |
| 524 | domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled); |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 525 | geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled); |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 526 | workersEnabled = p.getBoolean("enable_workers", workersEnabled); |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 527 | |
Grace Kloba | d9ee139 | 2009-07-20 11:53:33 -0700 | [diff] [blame] | 528 | update(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 529 | } |
| 530 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 531 | public String getHomePage() { |
John Reck | bafe58a | 2011-01-11 10:26:02 -0800 | [diff] [blame] | 532 | if (useMostVisitedHomepage) { |
| 533 | return HomeProvider.MOST_VISITED; |
| 534 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 535 | return homeUrl; |
| 536 | } |
| 537 | |
Bjorn Bringert | d69f51d | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 538 | public SearchEngine getSearchEngine() { |
| 539 | return searchEngine; |
| 540 | } |
| 541 | |
Feng Qian | b3c02da | 2009-06-29 15:58:08 -0700 | [diff] [blame] | 542 | public String getJsFlags() { |
| 543 | return jsFlags; |
| 544 | } |
| 545 | |
Andrei Popescu | 79e82b7 | 2009-07-27 12:01:59 +0100 | [diff] [blame] | 546 | public WebStorageSizeManager getWebStorageSizeManager() { |
| 547 | return webStorageSizeManager; |
| 548 | } |
| 549 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 550 | public void setHomePage(Context context, String url) { |
| 551 | Editor ed = PreferenceManager. |
| 552 | getDefaultSharedPreferences(context).edit(); |
| 553 | ed.putString(PREF_HOMEPAGE, url); |
Brad Fitzpatrick | 1a6e0e8 | 2010-09-01 16:29:03 -0700 | [diff] [blame] | 554 | ed.apply(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 555 | homeUrl = url; |
| 556 | } |
| 557 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 558 | public WebSettings.TextSize getTextSize() { |
| 559 | return textSize; |
| 560 | } |
| 561 | |
Grace Kloba | 2f83068 | 2009-06-25 11:08:53 -0700 | [diff] [blame] | 562 | public WebSettings.ZoomDensity getDefaultZoom() { |
| 563 | return zoomDensity; |
| 564 | } |
| 565 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 566 | public boolean openInBackground() { |
| 567 | return openInBackground; |
| 568 | } |
| 569 | |
| 570 | public boolean showSecurityWarnings() { |
| 571 | return showSecurityWarnings; |
| 572 | } |
| 573 | |
| 574 | public boolean isTracing() { |
| 575 | return tracing; |
| 576 | } |
| 577 | |
| 578 | public boolean isLightTouch() { |
| 579 | return lightTouch; |
| 580 | } |
| 581 | |
| 582 | public boolean isNavDump() { |
| 583 | return navDump; |
| 584 | } |
| 585 | |
Derek Sollenberger | ffa561e | 2010-11-16 14:19:01 -0500 | [diff] [blame] | 586 | public boolean isHardwareAccelerated() { |
| 587 | return hardwareAccelerated; |
| 588 | } |
| 589 | |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 590 | public boolean useQuickControls() { |
| 591 | return quickControls; |
| 592 | } |
| 593 | |
John Reck | bafe58a | 2011-01-11 10:26:02 -0800 | [diff] [blame] | 594 | public boolean useMostVisitedHomepage() { |
| 595 | return useMostVisitedHomepage; |
| 596 | } |
| 597 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 598 | public boolean showDebugSettings() { |
| 599 | return showDebugSettings; |
| 600 | } |
| 601 | |
| 602 | public void toggleDebugSettings() { |
| 603 | showDebugSettings = !showDebugSettings; |
| 604 | navDump = showDebugSettings; |
| 605 | update(); |
| 606 | } |
| 607 | |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 608 | public void setAutoFillProfile(Context ctx, AutoFillProfile profile, Message msg) { |
| 609 | if (profile != null) { |
| 610 | setActiveAutoFillProfileId(ctx, profile.getUniqueId()); |
| 611 | // Update the AutoFill DB with the new profile. |
| 612 | new SaveProfileToDbTask(ctx, msg).execute(profile); |
| 613 | } else { |
| 614 | // Delete the current profile. |
Ben Murdoch | e8a2833 | 2010-11-17 14:16:21 +0000 | [diff] [blame] | 615 | if (autoFillProfile != null) { |
| 616 | new DeleteProfileFromDbTask(ctx, msg).execute(autoFillProfile.getUniqueId()); |
| 617 | setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET); |
| 618 | } |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 619 | } |
| 620 | autoFillProfile = profile; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | public AutoFillProfile getAutoFillProfile() { |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 624 | return autoFillProfile; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 625 | } |
| 626 | |
Ben Murdoch | 6fa32ba | 2010-10-20 14:01:25 +0100 | [diff] [blame] | 627 | private void setActiveAutoFillProfileId(Context context, int activeProfileId) { |
| 628 | autoFillActiveProfileId = activeProfileId; |
| 629 | Editor ed = PreferenceManager. |
| 630 | getDefaultSharedPreferences(context).edit(); |
| 631 | ed.putInt(PREF_AUTOFILL_ACTIVE_PROFILE_ID, activeProfileId); |
| 632 | ed.apply(); |
| 633 | } |
| 634 | |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 635 | /* package */ void disableAutoFill(Context ctx) { |
| 636 | autoFillEnabled = false; |
| 637 | Editor ed = PreferenceManager.getDefaultSharedPreferences(ctx).edit(); |
| 638 | ed.putBoolean(PREF_AUTOFILL_ENABLED, false); |
| 639 | ed.apply(); |
| 640 | } |
| 641 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 642 | /** |
| 643 | * Add a WebSettings object to the list of observers that will be updated |
| 644 | * when update() is called. |
| 645 | * |
| 646 | * @param s A WebSettings object that is strictly tied to the life of a |
| 647 | * WebView. |
| 648 | */ |
| 649 | public Observer addObserver(WebSettings s) { |
| 650 | Observer old = mWebSettingsToObservers.get(s); |
| 651 | if (old != null) { |
| 652 | super.deleteObserver(old); |
| 653 | } |
| 654 | Observer o = new Observer(s); |
| 655 | mWebSettingsToObservers.put(s, o); |
| 656 | super.addObserver(o); |
| 657 | return o; |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * Delete the given WebSettings observer from the list of observers. |
| 662 | * @param s The WebSettings object to be deleted. |
| 663 | */ |
| 664 | public void deleteObserver(WebSettings s) { |
| 665 | Observer o = mWebSettingsToObservers.get(s); |
| 666 | if (o != null) { |
| 667 | mWebSettingsToObservers.remove(s); |
| 668 | super.deleteObserver(o); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | /* |
John Reck | 63bb687 | 2010-12-01 19:29:32 -0800 | [diff] [blame] | 673 | * Application level method for obtaining a single app instance of the |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 674 | * BrowserSettings. |
| 675 | */ |
John Reck | 63bb687 | 2010-12-01 19:29:32 -0800 | [diff] [blame] | 676 | public static BrowserSettings getInstance() { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 677 | if (sSingleton == null ) { |
| 678 | sSingleton = new BrowserSettings(); |
| 679 | } |
| 680 | return sSingleton; |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | * Package level method for associating the BrowserSettings with TabControl |
| 685 | */ |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 686 | /* package */void setController(Controller ctrl) { |
| 687 | mController = ctrl; |
Shimeng (Simon) Wang | 161974f | 2010-03-09 14:30:07 -0800 | [diff] [blame] | 688 | updateTabControlSettings(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* |
| 692 | * Update all the observers of the object. |
| 693 | */ |
| 694 | /*package*/ void update() { |
| 695 | setChanged(); |
| 696 | notifyObservers(); |
| 697 | } |
| 698 | |
| 699 | /*package*/ void clearCache(Context context) { |
| 700 | WebIconDatabase.getInstance().removeAllIcons(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 701 | if (mController != null) { |
| 702 | WebView current = mController.getCurrentWebView(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 703 | if (current != null) { |
| 704 | current.clearCache(true); |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | /*package*/ void clearCookies(Context context) { |
| 710 | CookieManager.getInstance().removeAllCookie(); |
| 711 | } |
| 712 | |
| 713 | /* package */void clearHistory(Context context) { |
| 714 | ContentResolver resolver = context.getContentResolver(); |
| 715 | Browser.clearHistory(resolver); |
| 716 | Browser.clearSearches(resolver); |
| 717 | } |
| 718 | |
| 719 | /* package */ void clearFormData(Context context) { |
| 720 | WebViewDatabase.getInstance(context).clearFormData(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 721 | if (mController!= null) { |
| 722 | WebView currentTopView = mController.getCurrentTopWebView(); |
Henrik Baard | 794dc72 | 2010-02-19 16:28:06 +0100 | [diff] [blame] | 723 | if (currentTopView != null) { |
| 724 | currentTopView.clearFormData(); |
| 725 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | |
| 729 | /*package*/ void clearPasswords(Context context) { |
| 730 | WebViewDatabase db = WebViewDatabase.getInstance(context); |
| 731 | db.clearUsernamePassword(); |
| 732 | db.clearHttpAuthUsernamePassword(); |
| 733 | } |
| 734 | |
Shimeng (Simon) Wang | 161974f | 2010-03-09 14:30:07 -0800 | [diff] [blame] | 735 | private void updateTabControlSettings() { |
| 736 | // Enable/disable the error console. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 737 | mController.setShouldShowErrorConsole( |
Shimeng (Simon) Wang | 161974f | 2010-03-09 14:30:07 -0800 | [diff] [blame] | 738 | showDebugSettings && showConsole); |
Shimeng (Simon) Wang | 161974f | 2010-03-09 14:30:07 -0800 | [diff] [blame] | 739 | } |
| 740 | |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 741 | /*package*/ void clearDatabases(Context context) { |
Andrei Popescu | 824faeb | 2009-07-21 18:24:06 +0100 | [diff] [blame] | 742 | WebStorage.getInstance().deleteAllData(); |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | /*package*/ void clearLocationAccess(Context context) { |
| 746 | GeolocationPermissions.getInstance().clearAll(); |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 747 | } |
| 748 | |
Leon Scroggins | 9ac587d | 2009-04-20 12:53:46 -0400 | [diff] [blame] | 749 | /*package*/ void resetDefaultPreferences(Context ctx) { |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 750 | reset(); |
Jeff Hamilton | 462b8e8 | 2010-09-23 14:33:43 -0500 | [diff] [blame] | 751 | SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx); |
Brad Fitzpatrick | 1a6e0e8 | 2010-09-01 16:29:03 -0700 | [diff] [blame] | 752 | p.edit().clear().apply(); |
John Reck | 035a564 | 2010-12-21 18:51:27 -0800 | [diff] [blame] | 753 | PreferenceManager.setDefaultValues(ctx, R.xml.general_preferences, true); |
| 754 | PreferenceManager.setDefaultValues(ctx, R.xml.privacy_security_preferences, true); |
Jeff Hamilton | 462b8e8 | 2010-09-23 14:33:43 -0500 | [diff] [blame] | 755 | PreferenceManager.setDefaultValues(ctx, R.xml.advanced_preferences, true); |
Grace Kloba | f2c5c1b | 2009-05-26 10:48:31 -0700 | [diff] [blame] | 756 | // reset homeUrl |
Grace Kloba | a3f90f0 | 2009-05-26 11:32:53 -0700 | [diff] [blame] | 757 | setHomePage(ctx, getFactoryResetHomeUrl(ctx)); |
Andrei Popescu | 79e82b7 | 2009-07-27 12:01:59 +0100 | [diff] [blame] | 758 | // reset appcache max size |
| 759 | appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize(); |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 760 | setActiveAutoFillProfileId(ctx, NO_AUTOFILL_PROFILE_SET); |
Grace Kloba | f2c5c1b | 2009-05-26 10:48:31 -0700 | [diff] [blame] | 761 | } |
| 762 | |
John Reck | 33bbb80 | 2010-10-26 17:13:42 -0700 | [diff] [blame] | 763 | /*package*/ static String getFactoryResetHomeUrl(Context context) { |
Grace Kloba | f2c5c1b | 2009-05-26 10:48:31 -0700 | [diff] [blame] | 764 | String url = context.getResources().getString(R.string.homepage_base); |
| 765 | if (url.indexOf("{CID}") != -1) { |
Patrick Scott | 4391469 | 2010-02-19 10:10:10 -0500 | [diff] [blame] | 766 | url = url.replace("{CID}", |
| 767 | BrowserProvider.getClientId(context.getContentResolver())); |
Grace Kloba | f2c5c1b | 2009-05-26 10:48:31 -0700 | [diff] [blame] | 768 | } |
| 769 | return url; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 770 | } |
| 771 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 772 | // Private constructor that does nothing. |
| 773 | private BrowserSettings() { |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 774 | reset(); |
| 775 | } |
| 776 | |
| 777 | private void reset() { |
| 778 | // Private variables for settings |
| 779 | // NOTE: these defaults need to be kept in sync with the XML |
| 780 | // until the performance of PreferenceManager.setDefaultValues() |
| 781 | // is improved. |
| 782 | loadsImagesAutomatically = true; |
| 783 | javaScriptEnabled = true; |
Patrick Scott | e536b33 | 2010-03-22 10:19:32 -0400 | [diff] [blame] | 784 | pluginState = WebSettings.PluginState.ON; |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 785 | javaScriptCanOpenWindowsAutomatically = false; |
| 786 | showSecurityWarnings = true; |
| 787 | rememberPasswords = true; |
| 788 | saveFormData = true; |
Ben Murdoch | dc62cb9 | 2010-11-23 15:11:29 +0000 | [diff] [blame] | 789 | autoFillEnabled = true; |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 790 | openInBackground = false; |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 791 | autoFitPage = true; |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 792 | loadsPageInOverviewMode = true; |
| 793 | showDebugSettings = false; |
| 794 | // HTML5 API flags |
| 795 | appCacheEnabled = true; |
| 796 | databaseEnabled = true; |
| 797 | domStorageEnabled = true; |
| 798 | geolocationEnabled = true; |
| 799 | workersEnabled = true; // only affects V8. JSC does not have a similar setting |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 800 | } |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 801 | |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 802 | private abstract class AutoFillProfileDbTask<T> extends AsyncTask<T, Void, Void> { |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 803 | Context mContext; |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame] | 804 | AutoFillProfileDatabase mAutoFillProfileDb; |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 805 | Message mCompleteMessage; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 806 | |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 807 | public AutoFillProfileDbTask(Context ctx, Message msg) { |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 808 | mContext = ctx; |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 809 | mCompleteMessage = msg; |
| 810 | } |
| 811 | |
| 812 | protected void onPostExecute(Void result) { |
| 813 | if (mCompleteMessage != null) { |
| 814 | mCompleteMessage.sendToTarget(); |
| 815 | } |
| 816 | mAutoFillProfileDb.close(); |
| 817 | } |
| 818 | |
| 819 | abstract protected Void doInBackground(T... values); |
| 820 | } |
| 821 | |
| 822 | |
| 823 | private class SaveProfileToDbTask extends AutoFillProfileDbTask<AutoFillProfile> { |
| 824 | public SaveProfileToDbTask(Context ctx, Message msg) { |
| 825 | super(ctx, msg); |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | protected Void doInBackground(AutoFillProfile... values) { |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame] | 829 | mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext); |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 830 | assert autoFillActiveProfileId != NO_AUTOFILL_PROFILE_SET; |
| 831 | AutoFillProfile newProfile = values[0]; |
| 832 | mAutoFillProfileDb.addOrUpdateProfile(autoFillActiveProfileId, newProfile); |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 833 | return null; |
| 834 | } |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 835 | } |
| 836 | |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 837 | private class DeleteProfileFromDbTask extends AutoFillProfileDbTask<Integer> { |
| 838 | public DeleteProfileFromDbTask(Context ctx, Message msg) { |
| 839 | super(ctx, msg); |
| 840 | } |
| 841 | |
| 842 | protected Void doInBackground(Integer... values) { |
| 843 | mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext); |
| 844 | int id = values[0]; |
| 845 | assert id > 0; |
| 846 | mAutoFillProfileDb.dropProfile(id); |
| 847 | return null; |
| 848 | } |
| 849 | } |
John Reck | 63bb687 | 2010-12-01 19:29:32 -0800 | [diff] [blame] | 850 | |
| 851 | @Override |
| 852 | public void onSharedPreferenceChanged( |
| 853 | SharedPreferences p, String key) { |
| 854 | if (PREF_HARDWARE_ACCEL.equals(key)) { |
| 855 | hardwareAccelerated = p.getBoolean(PREF_HARDWARE_ACCEL, hardwareAccelerated); |
| 856 | } else if (PREF_USER_AGENT.equals(key)) { |
| 857 | userAgent = Integer.parseInt(p.getString(PREF_USER_AGENT, "0")); |
| 858 | update(); |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 859 | } else if (PREF_QUICK_CONTROLS.equals(key)) { |
| 860 | quickControls = p.getBoolean(PREF_QUICK_CONTROLS, quickControls); |
John Reck | bafe58a | 2011-01-11 10:26:02 -0800 | [diff] [blame] | 861 | } else if (PREF_MOST_VISITED_HOMEPAGE.equals(key)) { |
| 862 | useMostVisitedHomepage = p.getBoolean(PREF_MOST_VISITED_HOMEPAGE, useMostVisitedHomepage); |
John Reck | 63bb687 | 2010-12-01 19:29:32 -0800 | [diff] [blame] | 863 | } |
| 864 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 865 | } |