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