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