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 | |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 20 | import com.android.browser.search.SearchEngine; |
| 21 | import com.android.browser.search.SearchEngines; |
| 22 | |
Grace Kloba | 9804c43 | 2009-12-02 11:07:40 -0800 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 24 | import android.content.ComponentName; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 25 | import android.content.ContentResolver; |
| 26 | import android.content.Context; |
| 27 | import android.content.pm.ActivityInfo; |
| 28 | import android.content.SharedPreferences; |
| 29 | import android.content.SharedPreferences.Editor; |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 30 | import android.database.ContentObserver; |
| 31 | import android.os.Handler; |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 32 | import android.preference.PreferenceActivity; |
| 33 | import android.preference.PreferenceScreen; |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 34 | import android.provider.Settings; |
| 35 | import android.util.Log; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 36 | import android.webkit.CookieManager; |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 37 | import android.webkit.GeolocationPermissions; |
Nicolas Roard | 99b3ae1 | 2009-09-22 15:17:52 +0100 | [diff] [blame] | 38 | import android.webkit.ValueCallback; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 39 | import android.webkit.WebView; |
| 40 | import android.webkit.WebViewDatabase; |
| 41 | import android.webkit.WebIconDatabase; |
| 42 | import android.webkit.WebSettings; |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 43 | import android.webkit.WebStorage; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 44 | import android.preference.PreferenceManager; |
| 45 | import android.provider.Browser; |
| 46 | |
| 47 | import java.util.HashMap; |
Nicolas Roard | 99b3ae1 | 2009-09-22 15:17:52 +0100 | [diff] [blame] | 48 | import java.util.Map; |
| 49 | import java.util.Set; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 50 | import java.util.Observable; |
| 51 | |
| 52 | /* |
| 53 | * Package level class for storing various WebView and Browser settings. To use |
| 54 | * this class: |
| 55 | * BrowserSettings s = BrowserSettings.getInstance(); |
| 56 | * s.addObserver(webView.getSettings()); |
| 57 | * s.loadFromDb(context); // Only needed on app startup |
| 58 | * s.javaScriptEnabled = true; |
| 59 | * ... // set any other settings |
| 60 | * s.update(); // this will update all the observers |
| 61 | * |
| 62 | * To remove an observer: |
| 63 | * s.deleteObserver(webView.getSettings()); |
| 64 | */ |
| 65 | class BrowserSettings extends Observable { |
| 66 | |
Leon Scroggins | 9ac587d | 2009-04-20 12:53:46 -0400 | [diff] [blame] | 67 | // Private variables for settings |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 68 | // NOTE: these defaults need to be kept in sync with the XML |
| 69 | // until the performance of PreferenceManager.setDefaultValues() |
| 70 | // is improved. |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 71 | // Note: boolean variables are set inside reset function. |
| 72 | private boolean loadsImagesAutomatically; |
| 73 | private boolean javaScriptEnabled; |
Patrick Scott | e536b33 | 2010-03-22 10:19:32 -0400 | [diff] [blame] | 74 | private WebSettings.PluginState pluginState; |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 75 | private boolean javaScriptCanOpenWindowsAutomatically; |
| 76 | private boolean showSecurityWarnings; |
| 77 | private boolean rememberPasswords; |
| 78 | private boolean saveFormData; |
| 79 | private boolean openInBackground; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 80 | private String defaultTextEncodingName; |
Grace Kloba | f2c5c1b | 2009-05-26 10:48:31 -0700 | [diff] [blame] | 81 | private String homeUrl = ""; |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 82 | private SearchEngine searchEngine; |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 83 | private boolean autoFitPage; |
| 84 | private boolean landscapeOnly; |
| 85 | private boolean loadsPageInOverviewMode; |
| 86 | private boolean showDebugSettings; |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 87 | // HTML5 API flags |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 88 | 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 Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 93 | // 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 Block | 5029cf0 | 2009-08-21 13:16:58 +0100 | [diff] [blame] | 97 | private String geolocationDatabasePath; // default value set in loadFromDb() |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 98 | private WebStorageSizeManager webStorageSizeManager; |
| 99 | |
| 100 | private String jsFlags = ""; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 101 | |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 102 | private final static String TAG = "BrowserSettings"; |
| 103 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 104 | // 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; |
Feng Qian | b3c02da | 2009-06-29 15:58:08 -0700 | [diff] [blame] | 112 | |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 113 | // By default the error console is shown once the user navigates to about:debug. |
| 114 | // The setting can be then toggled from the settings menu. |
| 115 | private boolean showConsole = true; |
| 116 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 117 | // Private preconfigured values |
| 118 | private static int minimumFontSize = 8; |
| 119 | private static int minimumLogicalFontSize = 8; |
| 120 | private static int defaultFontSize = 16; |
| 121 | private static int defaultFixedFontSize = 13; |
| 122 | private static WebSettings.TextSize textSize = |
| 123 | WebSettings.TextSize.NORMAL; |
Grace Kloba | 2f83068 | 2009-06-25 11:08:53 -0700 | [diff] [blame] | 124 | private static WebSettings.ZoomDensity zoomDensity = |
| 125 | WebSettings.ZoomDensity.MEDIUM; |
Grace Kloba | 9804c43 | 2009-12-02 11:07:40 -0800 | [diff] [blame] | 126 | private static int pageCacheCapacity; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 127 | |
| 128 | // Preference keys that are used outside this class |
| 129 | public final static String PREF_CLEAR_CACHE = "privacy_clear_cache"; |
| 130 | public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies"; |
| 131 | public final static String PREF_CLEAR_HISTORY = "privacy_clear_history"; |
| 132 | public final static String PREF_HOMEPAGE = "homepage"; |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 133 | public final static String PREF_SEARCH_ENGINE = "search_engine"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 134 | public final static String PREF_CLEAR_FORM_DATA = |
| 135 | "privacy_clear_form_data"; |
| 136 | public final static String PREF_CLEAR_PASSWORDS = |
| 137 | "privacy_clear_passwords"; |
| 138 | public final static String PREF_EXTRAS_RESET_DEFAULTS = |
| 139 | "reset_default_preferences"; |
| 140 | public final static String PREF_DEBUG_SETTINGS = "debug_menu"; |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 141 | public final static String PREF_WEBSITE_SETTINGS = "website_settings"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 142 | public final static String PREF_TEXT_SIZE = "text_size"; |
Grace Kloba | 2f83068 | 2009-06-25 11:08:53 -0700 | [diff] [blame] | 143 | public final static String PREF_DEFAULT_ZOOM = "default_zoom"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 144 | public final static String PREF_DEFAULT_TEXT_ENCODING = |
| 145 | "default_text_encoding"; |
Steve Block | c6f577f | 2009-08-20 18:11:08 +0100 | [diff] [blame] | 146 | public final static String PREF_CLEAR_GEOLOCATION_ACCESS = |
| 147 | "privacy_clear_geolocation_access"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 148 | |
| 149 | private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " + |
Grace Kloba | a4fa6ee | 2009-06-26 00:34:46 -0700 | [diff] [blame] | 150 | "U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, " + |
| 151 | "like Gecko) Version/4.0 Safari/530.17"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 152 | |
| 153 | private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " + |
Grace Kloba | a4fa6ee | 2009-06-26 00:34:46 -0700 | [diff] [blame] | 154 | "CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 " + |
| 155 | "(KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 156 | |
| 157 | // Value to truncate strings when adding them to a TextView within |
| 158 | // a ListView |
| 159 | public final static int MAX_TEXTVIEW_LEN = 80; |
| 160 | |
| 161 | private TabControl mTabControl; |
| 162 | |
| 163 | // Single instance of the BrowserSettings for use in the Browser app. |
| 164 | private static BrowserSettings sSingleton; |
| 165 | |
| 166 | // Private map of WebSettings to Observer objects used when deleting an |
| 167 | // observer. |
| 168 | private HashMap<WebSettings,Observer> mWebSettingsToObservers = |
| 169 | new HashMap<WebSettings,Observer>(); |
| 170 | |
| 171 | /* |
| 172 | * An observer wrapper for updating a WebSettings object with the new |
| 173 | * settings after a call to BrowserSettings.update(). |
| 174 | */ |
| 175 | static class Observer implements java.util.Observer { |
| 176 | // Private WebSettings object that will be updated. |
| 177 | private WebSettings mSettings; |
| 178 | |
| 179 | Observer(WebSettings w) { |
| 180 | mSettings = w; |
| 181 | } |
| 182 | |
| 183 | public void update(Observable o, Object arg) { |
| 184 | BrowserSettings b = (BrowserSettings)o; |
| 185 | WebSettings s = mSettings; |
| 186 | |
| 187 | s.setLayoutAlgorithm(b.layoutAlgorithm); |
| 188 | if (b.userAgent == 0) { |
| 189 | // use the default ua string |
| 190 | s.setUserAgentString(null); |
| 191 | } else if (b.userAgent == 1) { |
| 192 | s.setUserAgentString(DESKTOP_USERAGENT); |
| 193 | } else if (b.userAgent == 2) { |
| 194 | s.setUserAgentString(IPHONE_USERAGENT); |
| 195 | } |
| 196 | s.setUseWideViewPort(b.useWideViewPort); |
| 197 | s.setLoadsImagesAutomatically(b.loadsImagesAutomatically); |
| 198 | s.setJavaScriptEnabled(b.javaScriptEnabled); |
Patrick Scott | e536b33 | 2010-03-22 10:19:32 -0400 | [diff] [blame] | 199 | s.setPluginState(b.pluginState); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 200 | s.setJavaScriptCanOpenWindowsAutomatically( |
| 201 | b.javaScriptCanOpenWindowsAutomatically); |
| 202 | s.setDefaultTextEncodingName(b.defaultTextEncodingName); |
| 203 | s.setMinimumFontSize(b.minimumFontSize); |
| 204 | s.setMinimumLogicalFontSize(b.minimumLogicalFontSize); |
| 205 | s.setDefaultFontSize(b.defaultFontSize); |
| 206 | s.setDefaultFixedFontSize(b.defaultFixedFontSize); |
| 207 | s.setNavDump(b.navDump); |
| 208 | s.setTextSize(b.textSize); |
Grace Kloba | 2f83068 | 2009-06-25 11:08:53 -0700 | [diff] [blame] | 209 | s.setDefaultZoom(b.zoomDensity); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 210 | s.setLightTouchEnabled(b.lightTouch); |
| 211 | s.setSaveFormData(b.saveFormData); |
| 212 | s.setSavePassword(b.rememberPasswords); |
Grace Kloba | 5b4b8f1 | 2009-08-05 17:19:17 -0700 | [diff] [blame] | 213 | s.setLoadWithOverviewMode(b.loadsPageInOverviewMode); |
Grace Kloba | 7956503 | 2009-11-24 14:23:39 -0800 | [diff] [blame] | 214 | s.setPageCacheCapacity(pageCacheCapacity); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 215 | |
| 216 | // WebView inside Browser doesn't want initial focus to be set. |
| 217 | s.setNeedInitialFocus(false); |
| 218 | // Browser supports multiple windows |
| 219 | s.setSupportMultipleWindows(true); |
Ben Murdoch | 092dd5d | 2009-04-22 12:34:12 +0100 | [diff] [blame] | 220 | |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 221 | // HTML5 API flags |
| 222 | s.setAppCacheEnabled(b.appCacheEnabled); |
Ben Murdoch | 092dd5d | 2009-04-22 12:34:12 +0100 | [diff] [blame] | 223 | s.setDatabaseEnabled(b.databaseEnabled); |
Ben Murdoch | 734a066 | 2009-06-02 19:11:52 +0100 | [diff] [blame] | 224 | s.setDomStorageEnabled(b.domStorageEnabled); |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 225 | s.setWorkersEnabled(b.workersEnabled); // This only affects V8. |
Steve Block | 97b0802 | 2009-08-21 10:26:25 +0100 | [diff] [blame] | 226 | s.setGeolocationEnabled(b.geolocationEnabled); |
Ben Murdoch | 092dd5d | 2009-04-22 12:34:12 +0100 | [diff] [blame] | 227 | |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 228 | // HTML5 configuration parameters. |
Andrei Popescu | 20634ce | 2009-07-22 16:46:55 +0100 | [diff] [blame] | 229 | s.setAppCacheMaxSize(b.appCacheMaxSize); |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 230 | s.setAppCachePath(b.appCachePath); |
| 231 | s.setDatabasePath(b.databasePath); |
Steve Block | 5029cf0 | 2009-08-21 13:16:58 +0100 | [diff] [blame] | 232 | s.setGeolocationDatabasePath(b.geolocationDatabasePath); |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 233 | |
Shimeng (Simon) Wang | 161974f | 2010-03-09 14:30:07 -0800 | [diff] [blame] | 234 | b.updateTabControlSettings(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Load settings from the browser app's database. |
| 240 | * NOTE: Strings used for the preferences must match those specified |
| 241 | * in the browser_preferences.xml |
| 242 | * @param ctx A Context object used to query the browser's settings |
| 243 | * database. If the database exists, the saved settings will be |
| 244 | * stored in this BrowserSettings object. This will update all |
| 245 | * observers of this object. |
| 246 | */ |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 247 | public void loadFromDb(final Context ctx) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 248 | SharedPreferences p = |
| 249 | PreferenceManager.getDefaultSharedPreferences(ctx); |
Andrei Popescu | 5151ac1 | 2009-04-17 10:43:07 +0100 | [diff] [blame] | 250 | // Set the default value for the Application Caches path. |
| 251 | appCachePath = ctx.getDir("appcache", 0).getPath(); |
Andrei Popescu | 20634ce | 2009-07-22 16:46:55 +0100 | [diff] [blame] | 252 | // Determine the maximum size of the application cache. |
Andrei Popescu | 907c576 | 2009-07-29 14:44:24 +0100 | [diff] [blame] | 253 | webStorageSizeManager = new WebStorageSizeManager( |
| 254 | ctx, |
| 255 | new WebStorageSizeManager.StatFsDiskInfo(appCachePath), |
| 256 | new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath)); |
Andrei Popescu | 79e82b7 | 2009-07-27 12:01:59 +0100 | [diff] [blame] | 257 | appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize(); |
Ben Murdoch | 092dd5d | 2009-04-22 12:34:12 +0100 | [diff] [blame] | 258 | // Set the default value for the Database path. |
| 259 | databasePath = ctx.getDir("databases", 0).getPath(); |
Steve Block | 5029cf0 | 2009-08-21 13:16:58 +0100 | [diff] [blame] | 260 | // Set the default value for the Geolocation database path. |
| 261 | geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 262 | |
Shimeng (Simon) Wang | e83e906 | 2010-03-29 16:13:09 -0700 | [diff] [blame] | 263 | if (p.getString(PREF_HOMEPAGE, "") == "") { |
| 264 | // No home page preferences is set, set it to default. |
| 265 | setHomePage(ctx, getFactoryResetHomeUrl(ctx)); |
| 266 | } |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 267 | |
Grace Kloba | 9804c43 | 2009-12-02 11:07:40 -0800 | [diff] [blame] | 268 | // the cost of one cached page is ~3M (measured using nytimes.com). For |
| 269 | // low end devices, we only cache one page. For high end devices, we try |
| 270 | // to cache more pages, currently choose 5. |
| 271 | ActivityManager am = (ActivityManager) ctx |
| 272 | .getSystemService(Context.ACTIVITY_SERVICE); |
| 273 | if (am.getMemoryClass() > 16) { |
Andrei Popescu | ba676ef | 2010-03-29 12:12:55 +0100 | [diff] [blame] | 274 | pageCacheCapacity = 5; |
Grace Kloba | 9804c43 | 2009-12-02 11:07:40 -0800 | [diff] [blame] | 275 | } else { |
| 276 | pageCacheCapacity = 1; |
| 277 | } |
| 278 | |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 279 | // Load the defaults from the xml |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 280 | // This call is TOO SLOW, need to manually keep the defaults |
| 281 | // in sync |
| 282 | //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences); |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 283 | syncSharedPreferences(ctx, p); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 286 | /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) { |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 287 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 288 | homeUrl = |
| 289 | p.getString(PREF_HOMEPAGE, homeUrl); |
Leon Scroggins III | 3130660 | 2010-09-17 11:10:29 -0400 | [diff] [blame] | 290 | String searchEngineName = p.getString(PREF_SEARCH_ENGINE, |
| 291 | SearchEngine.GOOGLE); |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 292 | if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) { |
| 293 | if (searchEngine != null) { |
Leon Scroggins III | 95d9bfd | 2010-09-14 14:02:36 -0400 | [diff] [blame] | 294 | if (searchEngine.supportsVoiceSearch()) { |
| 295 | // One or more tabs could have been in voice search mode. |
| 296 | // Clear it, since the new SearchEngine may not support |
| 297 | // it, or may handle it differently. |
| 298 | for (int i = 0; i < mTabControl.getTabCount(); i++) { |
| 299 | mTabControl.getTab(i).revertVoiceSearchMode(); |
| 300 | } |
| 301 | } |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 302 | searchEngine.close(); |
| 303 | } |
| 304 | searchEngine = SearchEngines.get(ctx, searchEngineName); |
| 305 | } |
| 306 | Log.i(TAG, "Selected search engine: " + searchEngine); |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 307 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 308 | loadsImagesAutomatically = p.getBoolean("load_images", |
| 309 | loadsImagesAutomatically); |
| 310 | javaScriptEnabled = p.getBoolean("enable_javascript", |
| 311 | javaScriptEnabled); |
Patrick Scott | e536b33 | 2010-03-22 10:19:32 -0400 | [diff] [blame] | 312 | pluginState = WebSettings.PluginState.valueOf( |
| 313 | p.getString("plugin_state", pluginState.name())); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 314 | javaScriptCanOpenWindowsAutomatically = !p.getBoolean( |
| 315 | "block_popup_windows", |
| 316 | !javaScriptCanOpenWindowsAutomatically); |
| 317 | showSecurityWarnings = p.getBoolean("show_security_warnings", |
| 318 | showSecurityWarnings); |
| 319 | rememberPasswords = p.getBoolean("remember_passwords", |
| 320 | rememberPasswords); |
| 321 | saveFormData = p.getBoolean("save_formdata", |
| 322 | saveFormData); |
| 323 | boolean accept_cookies = p.getBoolean("accept_cookies", |
| 324 | CookieManager.getInstance().acceptCookie()); |
| 325 | CookieManager.getInstance().setAcceptCookie(accept_cookies); |
| 326 | openInBackground = p.getBoolean("open_in_background", openInBackground); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 327 | textSize = WebSettings.TextSize.valueOf( |
| 328 | p.getString(PREF_TEXT_SIZE, textSize.name())); |
Grace Kloba | 2f83068 | 2009-06-25 11:08:53 -0700 | [diff] [blame] | 329 | zoomDensity = WebSettings.ZoomDensity.valueOf( |
| 330 | p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name())); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 331 | autoFitPage = p.getBoolean("autofit_pages", autoFitPage); |
Grace Kloba | 5b4b8f1 | 2009-08-05 17:19:17 -0700 | [diff] [blame] | 332 | loadsPageInOverviewMode = p.getBoolean("load_page", |
| 333 | loadsPageInOverviewMode); |
Leon Scroggins | b0cd072 | 2009-03-31 14:31:22 -0700 | [diff] [blame] | 334 | boolean landscapeOnlyTemp = |
| 335 | p.getBoolean("landscape_only", landscapeOnly); |
| 336 | if (landscapeOnlyTemp != landscapeOnly) { |
| 337 | landscapeOnly = landscapeOnlyTemp; |
Leon Scroggins | b0cd072 | 2009-03-31 14:31:22 -0700 | [diff] [blame] | 338 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 339 | useWideViewPort = true; // use wide view port for either setting |
| 340 | if (autoFitPage) { |
| 341 | layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS; |
| 342 | } else { |
| 343 | layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL; |
| 344 | } |
| 345 | defaultTextEncodingName = |
| 346 | p.getString(PREF_DEFAULT_TEXT_ENCODING, |
| 347 | defaultTextEncodingName); |
| 348 | |
| 349 | showDebugSettings = |
| 350 | p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings); |
| 351 | // Debug menu items have precidence if the menu is visible |
| 352 | if (showDebugSettings) { |
| 353 | boolean small_screen = p.getBoolean("small_screen", |
| 354 | layoutAlgorithm == |
| 355 | WebSettings.LayoutAlgorithm.SINGLE_COLUMN); |
| 356 | if (small_screen) { |
| 357 | layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN; |
| 358 | } else { |
| 359 | boolean normal_layout = p.getBoolean("normal_layout", |
| 360 | layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL); |
| 361 | if (normal_layout) { |
| 362 | layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL; |
| 363 | } else { |
| 364 | layoutAlgorithm = |
| 365 | WebSettings.LayoutAlgorithm.NARROW_COLUMNS; |
| 366 | } |
| 367 | } |
| 368 | useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort); |
| 369 | tracing = p.getBoolean("enable_tracing", tracing); |
| 370 | lightTouch = p.getBoolean("enable_light_touch", lightTouch); |
| 371 | navDump = p.getBoolean("enable_nav_dump", navDump); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 372 | userAgent = Integer.parseInt(p.getString("user_agent", "0")); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 373 | } |
Feng Qian | b3c02da | 2009-06-29 15:58:08 -0700 | [diff] [blame] | 374 | // JS flags is loaded from DB even if showDebugSettings is false, |
| 375 | // so that it can be set once and be effective all the time. |
| 376 | jsFlags = p.getString("js_engine_flags", ""); |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 377 | |
| 378 | // Read the setting for showing/hiding the JS Console always so that should the |
| 379 | // user enable debug settings, we already know if we should show the console. |
| 380 | // The user will never see the console unless they navigate to about:debug, |
| 381 | // regardless of the setting we read here. This setting is only used after debug |
| 382 | // is enabled. |
| 383 | showConsole = p.getBoolean("javascript_console", showConsole); |
Grace Kloba | d9ee139 | 2009-07-20 11:53:33 -0700 | [diff] [blame] | 384 | |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 385 | // HTML5 API flags |
| 386 | appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled); |
| 387 | databaseEnabled = p.getBoolean("enable_database", databaseEnabled); |
| 388 | domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled); |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 389 | geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled); |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 390 | workersEnabled = p.getBoolean("enable_workers", workersEnabled); |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 391 | |
Grace Kloba | d9ee139 | 2009-07-20 11:53:33 -0700 | [diff] [blame] | 392 | update(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 393 | } |
| 394 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 395 | public String getHomePage() { |
| 396 | return homeUrl; |
| 397 | } |
| 398 | |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 399 | public SearchEngine getSearchEngine() { |
| 400 | return searchEngine; |
| 401 | } |
| 402 | |
Feng Qian | b3c02da | 2009-06-29 15:58:08 -0700 | [diff] [blame] | 403 | public String getJsFlags() { |
| 404 | return jsFlags; |
| 405 | } |
| 406 | |
Andrei Popescu | 79e82b7 | 2009-07-27 12:01:59 +0100 | [diff] [blame] | 407 | public WebStorageSizeManager getWebStorageSizeManager() { |
| 408 | return webStorageSizeManager; |
| 409 | } |
| 410 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 411 | public void setHomePage(Context context, String url) { |
| 412 | Editor ed = PreferenceManager. |
| 413 | getDefaultSharedPreferences(context).edit(); |
| 414 | ed.putString(PREF_HOMEPAGE, url); |
| 415 | ed.commit(); |
| 416 | homeUrl = url; |
| 417 | } |
| 418 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 419 | public WebSettings.TextSize getTextSize() { |
| 420 | return textSize; |
| 421 | } |
| 422 | |
Grace Kloba | 2f83068 | 2009-06-25 11:08:53 -0700 | [diff] [blame] | 423 | public WebSettings.ZoomDensity getDefaultZoom() { |
| 424 | return zoomDensity; |
| 425 | } |
| 426 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 427 | public boolean openInBackground() { |
| 428 | return openInBackground; |
| 429 | } |
| 430 | |
| 431 | public boolean showSecurityWarnings() { |
| 432 | return showSecurityWarnings; |
| 433 | } |
| 434 | |
| 435 | public boolean isTracing() { |
| 436 | return tracing; |
| 437 | } |
| 438 | |
| 439 | public boolean isLightTouch() { |
| 440 | return lightTouch; |
| 441 | } |
| 442 | |
| 443 | public boolean isNavDump() { |
| 444 | return navDump; |
| 445 | } |
| 446 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 447 | public boolean showDebugSettings() { |
| 448 | return showDebugSettings; |
| 449 | } |
| 450 | |
| 451 | public void toggleDebugSettings() { |
| 452 | showDebugSettings = !showDebugSettings; |
| 453 | navDump = showDebugSettings; |
| 454 | update(); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Add a WebSettings object to the list of observers that will be updated |
| 459 | * when update() is called. |
| 460 | * |
| 461 | * @param s A WebSettings object that is strictly tied to the life of a |
| 462 | * WebView. |
| 463 | */ |
| 464 | public Observer addObserver(WebSettings s) { |
| 465 | Observer old = mWebSettingsToObservers.get(s); |
| 466 | if (old != null) { |
| 467 | super.deleteObserver(old); |
| 468 | } |
| 469 | Observer o = new Observer(s); |
| 470 | mWebSettingsToObservers.put(s, o); |
| 471 | super.addObserver(o); |
| 472 | return o; |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Delete the given WebSettings observer from the list of observers. |
| 477 | * @param s The WebSettings object to be deleted. |
| 478 | */ |
| 479 | public void deleteObserver(WebSettings s) { |
| 480 | Observer o = mWebSettingsToObservers.get(s); |
| 481 | if (o != null) { |
| 482 | mWebSettingsToObservers.remove(s); |
| 483 | super.deleteObserver(o); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | /* |
| 488 | * Package level method for obtaining a single app instance of the |
| 489 | * BrowserSettings. |
| 490 | */ |
| 491 | /*package*/ static BrowserSettings getInstance() { |
| 492 | if (sSingleton == null ) { |
| 493 | sSingleton = new BrowserSettings(); |
| 494 | } |
| 495 | return sSingleton; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * Package level method for associating the BrowserSettings with TabControl |
| 500 | */ |
| 501 | /* package */void setTabControl(TabControl tabControl) { |
| 502 | mTabControl = tabControl; |
Shimeng (Simon) Wang | 161974f | 2010-03-09 14:30:07 -0800 | [diff] [blame] | 503 | updateTabControlSettings(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Update all the observers of the object. |
| 508 | */ |
| 509 | /*package*/ void update() { |
| 510 | setChanged(); |
| 511 | notifyObservers(); |
| 512 | } |
| 513 | |
| 514 | /*package*/ void clearCache(Context context) { |
| 515 | WebIconDatabase.getInstance().removeAllIcons(); |
| 516 | if (mTabControl != null) { |
| 517 | WebView current = mTabControl.getCurrentWebView(); |
| 518 | if (current != null) { |
| 519 | current.clearCache(true); |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | /*package*/ void clearCookies(Context context) { |
| 525 | CookieManager.getInstance().removeAllCookie(); |
| 526 | } |
| 527 | |
| 528 | /* package */void clearHistory(Context context) { |
| 529 | ContentResolver resolver = context.getContentResolver(); |
| 530 | Browser.clearHistory(resolver); |
| 531 | Browser.clearSearches(resolver); |
| 532 | } |
| 533 | |
| 534 | /* package */ void clearFormData(Context context) { |
| 535 | WebViewDatabase.getInstance(context).clearFormData(); |
| 536 | if (mTabControl != null) { |
| 537 | mTabControl.getCurrentTopWebView().clearFormData(); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | /*package*/ void clearPasswords(Context context) { |
| 542 | WebViewDatabase db = WebViewDatabase.getInstance(context); |
| 543 | db.clearUsernamePassword(); |
| 544 | db.clearHttpAuthUsernamePassword(); |
| 545 | } |
| 546 | |
Shimeng (Simon) Wang | 161974f | 2010-03-09 14:30:07 -0800 | [diff] [blame] | 547 | private void updateTabControlSettings() { |
| 548 | // Enable/disable the error console. |
| 549 | mTabControl.getBrowserActivity().setShouldShowErrorConsole( |
| 550 | showDebugSettings && showConsole); |
| 551 | mTabControl.getBrowserActivity().setRequestedOrientation( |
| 552 | landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE |
| 553 | : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); |
| 554 | } |
| 555 | |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 556 | private void maybeDisableWebsiteSettings(Context context) { |
Nicolas Roard | 99b3ae1 | 2009-09-22 15:17:52 +0100 | [diff] [blame] | 557 | PreferenceActivity activity = (PreferenceActivity) context; |
| 558 | final PreferenceScreen screen = (PreferenceScreen) |
| 559 | activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS); |
| 560 | screen.setEnabled(false); |
| 561 | WebStorage.getInstance().getOrigins(new ValueCallback<Map>() { |
| 562 | public void onReceiveValue(Map webStorageOrigins) { |
| 563 | if ((webStorageOrigins != null) && !webStorageOrigins.isEmpty()) { |
| 564 | screen.setEnabled(true); |
| 565 | } |
| 566 | } |
| 567 | }); |
| 568 | |
Steve Block | 2a6a0f4 | 2009-11-19 15:55:42 +0000 | [diff] [blame] | 569 | GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() { |
| 570 | public void onReceiveValue(Set<String> geolocationOrigins) { |
Nicolas Roard | 99b3ae1 | 2009-09-22 15:17:52 +0100 | [diff] [blame] | 571 | if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) { |
| 572 | screen.setEnabled(true); |
| 573 | } |
| 574 | } |
| 575 | }); |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 576 | } |
| 577 | |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 578 | /*package*/ void clearDatabases(Context context) { |
Andrei Popescu | 824faeb | 2009-07-21 18:24:06 +0100 | [diff] [blame] | 579 | WebStorage.getInstance().deleteAllData(); |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 580 | maybeDisableWebsiteSettings(context); |
| 581 | } |
| 582 | |
| 583 | /*package*/ void clearLocationAccess(Context context) { |
| 584 | GeolocationPermissions.getInstance().clearAll(); |
| 585 | maybeDisableWebsiteSettings(context); |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 586 | } |
| 587 | |
Leon Scroggins | 9ac587d | 2009-04-20 12:53:46 -0400 | [diff] [blame] | 588 | /*package*/ void resetDefaultPreferences(Context ctx) { |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 589 | reset(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 590 | SharedPreferences p = |
Leon Scroggins | 9ac587d | 2009-04-20 12:53:46 -0400 | [diff] [blame] | 591 | PreferenceManager.getDefaultSharedPreferences(ctx); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 592 | p.edit().clear().commit(); |
Leon Scroggins | 9ac587d | 2009-04-20 12:53:46 -0400 | [diff] [blame] | 593 | PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences, |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 594 | true); |
Grace Kloba | f2c5c1b | 2009-05-26 10:48:31 -0700 | [diff] [blame] | 595 | // reset homeUrl |
Grace Kloba | a3f90f0 | 2009-05-26 11:32:53 -0700 | [diff] [blame] | 596 | setHomePage(ctx, getFactoryResetHomeUrl(ctx)); |
Andrei Popescu | 79e82b7 | 2009-07-27 12:01:59 +0100 | [diff] [blame] | 597 | // reset appcache max size |
| 598 | appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize(); |
Grace Kloba | f2c5c1b | 2009-05-26 10:48:31 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | private String getFactoryResetHomeUrl(Context context) { |
| 602 | String url = context.getResources().getString(R.string.homepage_base); |
| 603 | if (url.indexOf("{CID}") != -1) { |
Patrick Scott | 4391469 | 2010-02-19 10:10:10 -0500 | [diff] [blame] | 604 | url = url.replace("{CID}", |
| 605 | BrowserProvider.getClientId(context.getContentResolver())); |
Grace Kloba | f2c5c1b | 2009-05-26 10:48:31 -0700 | [diff] [blame] | 606 | } |
| 607 | return url; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 608 | } |
| 609 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 610 | // Private constructor that does nothing. |
| 611 | private BrowserSettings() { |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 612 | reset(); |
| 613 | } |
| 614 | |
| 615 | private void reset() { |
| 616 | // Private variables for settings |
| 617 | // NOTE: these defaults need to be kept in sync with the XML |
| 618 | // until the performance of PreferenceManager.setDefaultValues() |
| 619 | // is improved. |
| 620 | loadsImagesAutomatically = true; |
| 621 | javaScriptEnabled = true; |
Patrick Scott | e536b33 | 2010-03-22 10:19:32 -0400 | [diff] [blame] | 622 | pluginState = WebSettings.PluginState.ON; |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 623 | javaScriptCanOpenWindowsAutomatically = false; |
| 624 | showSecurityWarnings = true; |
| 625 | rememberPasswords = true; |
| 626 | saveFormData = true; |
| 627 | openInBackground = false; |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 628 | autoFitPage = true; |
| 629 | landscapeOnly = false; |
| 630 | loadsPageInOverviewMode = true; |
| 631 | showDebugSettings = false; |
| 632 | // HTML5 API flags |
| 633 | appCacheEnabled = true; |
| 634 | databaseEnabled = true; |
| 635 | domStorageEnabled = true; |
| 636 | geolocationEnabled = true; |
| 637 | 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] | 638 | } |
| 639 | } |