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