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.app.Activity; |
| 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; |
| 27 | import android.os.SystemProperties; |
| 28 | import android.view.WindowManager; |
| 29 | import android.webkit.CacheManager; |
| 30 | import android.webkit.CookieManager; |
| 31 | import android.webkit.WebView; |
| 32 | import android.webkit.WebViewDatabase; |
| 33 | import android.webkit.WebIconDatabase; |
| 34 | import android.webkit.WebSettings; |
| 35 | import android.preference.PreferenceManager; |
| 36 | import android.provider.Browser; |
| 37 | |
| 38 | import java.util.HashMap; |
| 39 | import java.util.Observable; |
| 40 | |
| 41 | /* |
| 42 | * Package level class for storing various WebView and Browser settings. To use |
| 43 | * this class: |
| 44 | * BrowserSettings s = BrowserSettings.getInstance(); |
| 45 | * s.addObserver(webView.getSettings()); |
| 46 | * s.loadFromDb(context); // Only needed on app startup |
| 47 | * s.javaScriptEnabled = true; |
| 48 | * ... // set any other settings |
| 49 | * s.update(); // this will update all the observers |
| 50 | * |
| 51 | * To remove an observer: |
| 52 | * s.deleteObserver(webView.getSettings()); |
| 53 | */ |
| 54 | class BrowserSettings extends Observable { |
| 55 | |
| 56 | // Public variables for settings |
| 57 | // NOTE: these defaults need to be kept in sync with the XML |
| 58 | // until the performance of PreferenceManager.setDefaultValues() |
| 59 | // is improved. |
| 60 | private boolean loadsImagesAutomatically = true; |
| 61 | private boolean javaScriptEnabled = true; |
| 62 | private boolean pluginsEnabled = true; |
| 63 | private String pluginsPath; // default value set in loadFromDb(). |
| 64 | private boolean javaScriptCanOpenWindowsAutomatically = false; |
| 65 | private boolean showSecurityWarnings = true; |
| 66 | private boolean rememberPasswords = true; |
| 67 | private boolean saveFormData = true; |
| 68 | private boolean openInBackground = false; |
| 69 | private String defaultTextEncodingName; |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 70 | private String homeUrl = "http://www.google.com/m?client=ms-"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 71 | private boolean loginInitialized = false; |
| 72 | private boolean autoFitPage = true; |
Leon Scroggins | b0cd072 | 2009-03-31 14:31:22 -0700 | [diff] [blame^] | 73 | private boolean landscapeOnly = false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 74 | private boolean showDebugSettings = false; |
| 75 | |
| 76 | // Development settings |
| 77 | public WebSettings.LayoutAlgorithm layoutAlgorithm = |
| 78 | WebSettings.LayoutAlgorithm.NARROW_COLUMNS; |
| 79 | private boolean useWideViewPort = true; |
| 80 | private int userAgent = 0; |
| 81 | private boolean tracing = false; |
| 82 | private boolean lightTouch = false; |
| 83 | private boolean navDump = false; |
| 84 | // Browser only settings |
| 85 | private boolean doFlick = false; |
| 86 | |
| 87 | // Private preconfigured values |
| 88 | private static int minimumFontSize = 8; |
| 89 | private static int minimumLogicalFontSize = 8; |
| 90 | private static int defaultFontSize = 16; |
| 91 | private static int defaultFixedFontSize = 13; |
| 92 | private static WebSettings.TextSize textSize = |
| 93 | WebSettings.TextSize.NORMAL; |
| 94 | |
| 95 | // Preference keys that are used outside this class |
| 96 | public final static String PREF_CLEAR_CACHE = "privacy_clear_cache"; |
| 97 | public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies"; |
| 98 | public final static String PREF_CLEAR_HISTORY = "privacy_clear_history"; |
| 99 | public final static String PREF_HOMEPAGE = "homepage"; |
| 100 | public final static String PREF_CLEAR_FORM_DATA = |
| 101 | "privacy_clear_form_data"; |
| 102 | public final static String PREF_CLEAR_PASSWORDS = |
| 103 | "privacy_clear_passwords"; |
| 104 | public final static String PREF_EXTRAS_RESET_DEFAULTS = |
| 105 | "reset_default_preferences"; |
| 106 | public final static String PREF_DEBUG_SETTINGS = "debug_menu"; |
| 107 | public final static String PREF_GEARS_SETTINGS = "gears_settings"; |
| 108 | public final static String PREF_TEXT_SIZE = "text_size"; |
| 109 | public final static String PREF_DEFAULT_TEXT_ENCODING = |
| 110 | "default_text_encoding"; |
| 111 | |
| 112 | private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " + |
| 113 | "U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/525.18 (KHTML, " + |
| 114 | "like Gecko) Version/3.1.2 Safari/525.20.1"; |
| 115 | |
| 116 | private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " + |
| 117 | "CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 " + |
| 118 | "(KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20"; |
| 119 | |
| 120 | // Value to truncate strings when adding them to a TextView within |
| 121 | // a ListView |
| 122 | public final static int MAX_TEXTVIEW_LEN = 80; |
| 123 | |
| 124 | private TabControl mTabControl; |
| 125 | |
| 126 | // Single instance of the BrowserSettings for use in the Browser app. |
| 127 | private static BrowserSettings sSingleton; |
| 128 | |
| 129 | // Private map of WebSettings to Observer objects used when deleting an |
| 130 | // observer. |
| 131 | private HashMap<WebSettings,Observer> mWebSettingsToObservers = |
| 132 | new HashMap<WebSettings,Observer>(); |
| 133 | |
| 134 | /* |
| 135 | * An observer wrapper for updating a WebSettings object with the new |
| 136 | * settings after a call to BrowserSettings.update(). |
| 137 | */ |
| 138 | static class Observer implements java.util.Observer { |
| 139 | // Private WebSettings object that will be updated. |
| 140 | private WebSettings mSettings; |
| 141 | |
| 142 | Observer(WebSettings w) { |
| 143 | mSettings = w; |
| 144 | } |
| 145 | |
| 146 | public void update(Observable o, Object arg) { |
| 147 | BrowserSettings b = (BrowserSettings)o; |
| 148 | WebSettings s = mSettings; |
| 149 | |
| 150 | s.setLayoutAlgorithm(b.layoutAlgorithm); |
| 151 | if (b.userAgent == 0) { |
| 152 | // use the default ua string |
| 153 | s.setUserAgentString(null); |
| 154 | } else if (b.userAgent == 1) { |
| 155 | s.setUserAgentString(DESKTOP_USERAGENT); |
| 156 | } else if (b.userAgent == 2) { |
| 157 | s.setUserAgentString(IPHONE_USERAGENT); |
| 158 | } |
| 159 | s.setUseWideViewPort(b.useWideViewPort); |
| 160 | s.setLoadsImagesAutomatically(b.loadsImagesAutomatically); |
| 161 | s.setJavaScriptEnabled(b.javaScriptEnabled); |
| 162 | s.setPluginsEnabled(b.pluginsEnabled); |
| 163 | s.setPluginsPath(b.pluginsPath); |
| 164 | s.setJavaScriptCanOpenWindowsAutomatically( |
| 165 | b.javaScriptCanOpenWindowsAutomatically); |
| 166 | s.setDefaultTextEncodingName(b.defaultTextEncodingName); |
| 167 | s.setMinimumFontSize(b.minimumFontSize); |
| 168 | s.setMinimumLogicalFontSize(b.minimumLogicalFontSize); |
| 169 | s.setDefaultFontSize(b.defaultFontSize); |
| 170 | s.setDefaultFixedFontSize(b.defaultFixedFontSize); |
| 171 | s.setNavDump(b.navDump); |
| 172 | s.setTextSize(b.textSize); |
| 173 | s.setLightTouchEnabled(b.lightTouch); |
| 174 | s.setSaveFormData(b.saveFormData); |
| 175 | s.setSavePassword(b.rememberPasswords); |
| 176 | |
| 177 | // WebView inside Browser doesn't want initial focus to be set. |
| 178 | s.setNeedInitialFocus(false); |
| 179 | // Browser supports multiple windows |
| 180 | s.setSupportMultipleWindows(true); |
| 181 | // Turn off file access |
| 182 | s.setAllowFileAccess(false); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Load settings from the browser app's database. |
| 188 | * NOTE: Strings used for the preferences must match those specified |
| 189 | * in the browser_preferences.xml |
| 190 | * @param ctx A Context object used to query the browser's settings |
| 191 | * database. If the database exists, the saved settings will be |
| 192 | * stored in this BrowserSettings object. This will update all |
| 193 | * observers of this object. |
| 194 | */ |
| 195 | public void loadFromDb(Context ctx) { |
| 196 | SharedPreferences p = |
| 197 | PreferenceManager.getDefaultSharedPreferences(ctx); |
| 198 | |
| 199 | // Set the default value for the plugins path to the application's |
| 200 | // local directory. |
| 201 | pluginsPath = ctx.getDir("plugins", 0).getPath(); |
| 202 | |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 203 | homeUrl += Partner.getString(ctx.getContentResolver(), Partner.CLIENT_ID); |
| 204 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 205 | // Load the defaults from the xml |
| 206 | // This call is TOO SLOW, need to manually keep the defaults |
| 207 | // in sync |
| 208 | //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences); |
| 209 | syncSharedPreferences(p); |
| 210 | } |
| 211 | |
| 212 | /* package */ void syncSharedPreferences(SharedPreferences p) { |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 213 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 214 | homeUrl = |
| 215 | p.getString(PREF_HOMEPAGE, homeUrl); |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 216 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 217 | loadsImagesAutomatically = p.getBoolean("load_images", |
| 218 | loadsImagesAutomatically); |
| 219 | javaScriptEnabled = p.getBoolean("enable_javascript", |
| 220 | javaScriptEnabled); |
| 221 | pluginsEnabled = p.getBoolean("enable_plugins", |
| 222 | pluginsEnabled); |
| 223 | pluginsPath = p.getString("plugins_path", pluginsPath); |
| 224 | javaScriptCanOpenWindowsAutomatically = !p.getBoolean( |
| 225 | "block_popup_windows", |
| 226 | !javaScriptCanOpenWindowsAutomatically); |
| 227 | showSecurityWarnings = p.getBoolean("show_security_warnings", |
| 228 | showSecurityWarnings); |
| 229 | rememberPasswords = p.getBoolean("remember_passwords", |
| 230 | rememberPasswords); |
| 231 | saveFormData = p.getBoolean("save_formdata", |
| 232 | saveFormData); |
| 233 | boolean accept_cookies = p.getBoolean("accept_cookies", |
| 234 | CookieManager.getInstance().acceptCookie()); |
| 235 | CookieManager.getInstance().setAcceptCookie(accept_cookies); |
| 236 | openInBackground = p.getBoolean("open_in_background", openInBackground); |
| 237 | loginInitialized = p.getBoolean("login_initialized", loginInitialized); |
| 238 | textSize = WebSettings.TextSize.valueOf( |
| 239 | p.getString(PREF_TEXT_SIZE, textSize.name())); |
| 240 | autoFitPage = p.getBoolean("autofit_pages", autoFitPage); |
Leon Scroggins | b0cd072 | 2009-03-31 14:31:22 -0700 | [diff] [blame^] | 241 | boolean landscapeOnlyTemp = |
| 242 | p.getBoolean("landscape_only", landscapeOnly); |
| 243 | if (landscapeOnlyTemp != landscapeOnly) { |
| 244 | landscapeOnly = landscapeOnlyTemp; |
| 245 | mTabControl.getBrowserActivity().setRequestedOrientation( |
| 246 | landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE |
| 247 | : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); |
| 248 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 249 | useWideViewPort = true; // use wide view port for either setting |
| 250 | if (autoFitPage) { |
| 251 | layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS; |
| 252 | } else { |
| 253 | layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL; |
| 254 | } |
| 255 | defaultTextEncodingName = |
| 256 | p.getString(PREF_DEFAULT_TEXT_ENCODING, |
| 257 | defaultTextEncodingName); |
| 258 | |
| 259 | showDebugSettings = |
| 260 | p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings); |
| 261 | // Debug menu items have precidence if the menu is visible |
| 262 | if (showDebugSettings) { |
| 263 | boolean small_screen = p.getBoolean("small_screen", |
| 264 | layoutAlgorithm == |
| 265 | WebSettings.LayoutAlgorithm.SINGLE_COLUMN); |
| 266 | if (small_screen) { |
| 267 | layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN; |
| 268 | } else { |
| 269 | boolean normal_layout = p.getBoolean("normal_layout", |
| 270 | layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL); |
| 271 | if (normal_layout) { |
| 272 | layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL; |
| 273 | } else { |
| 274 | layoutAlgorithm = |
| 275 | WebSettings.LayoutAlgorithm.NARROW_COLUMNS; |
| 276 | } |
| 277 | } |
| 278 | useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort); |
| 279 | tracing = p.getBoolean("enable_tracing", tracing); |
| 280 | lightTouch = p.getBoolean("enable_light_touch", lightTouch); |
| 281 | navDump = p.getBoolean("enable_nav_dump", navDump); |
| 282 | doFlick = p.getBoolean("enable_flick", doFlick); |
| 283 | userAgent = Integer.parseInt(p.getString("user_agent", "0")); |
| 284 | mTabControl.getBrowserActivity().setBaseSearchUrl( |
| 285 | p.getString("search_url", "")); |
| 286 | } |
| 287 | update(); |
| 288 | } |
| 289 | |
| 290 | public String getPluginsPath() { |
| 291 | return pluginsPath; |
| 292 | } |
| 293 | |
| 294 | public String getHomePage() { |
| 295 | return homeUrl; |
| 296 | } |
| 297 | |
| 298 | public void setHomePage(Context context, String url) { |
| 299 | Editor ed = PreferenceManager. |
| 300 | getDefaultSharedPreferences(context).edit(); |
| 301 | ed.putString(PREF_HOMEPAGE, url); |
| 302 | ed.commit(); |
| 303 | homeUrl = url; |
| 304 | } |
| 305 | |
| 306 | public boolean isLoginInitialized() { |
| 307 | return loginInitialized; |
| 308 | } |
| 309 | |
| 310 | public void setLoginInitialized(Context context) { |
| 311 | loginInitialized = true; |
| 312 | Editor ed = PreferenceManager. |
| 313 | getDefaultSharedPreferences(context).edit(); |
| 314 | ed.putBoolean("login_initialized", loginInitialized); |
| 315 | ed.commit(); |
| 316 | } |
| 317 | |
| 318 | public WebSettings.TextSize getTextSize() { |
| 319 | return textSize; |
| 320 | } |
| 321 | |
| 322 | public boolean openInBackground() { |
| 323 | return openInBackground; |
| 324 | } |
| 325 | |
| 326 | public boolean showSecurityWarnings() { |
| 327 | return showSecurityWarnings; |
| 328 | } |
| 329 | |
| 330 | public boolean isTracing() { |
| 331 | return tracing; |
| 332 | } |
| 333 | |
| 334 | public boolean isLightTouch() { |
| 335 | return lightTouch; |
| 336 | } |
| 337 | |
| 338 | public boolean isNavDump() { |
| 339 | return navDump; |
| 340 | } |
| 341 | |
| 342 | public boolean doFlick() { |
| 343 | return doFlick; |
| 344 | } |
| 345 | |
| 346 | public boolean showDebugSettings() { |
| 347 | return showDebugSettings; |
| 348 | } |
| 349 | |
| 350 | public void toggleDebugSettings() { |
| 351 | showDebugSettings = !showDebugSettings; |
| 352 | navDump = showDebugSettings; |
| 353 | update(); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Add a WebSettings object to the list of observers that will be updated |
| 358 | * when update() is called. |
| 359 | * |
| 360 | * @param s A WebSettings object that is strictly tied to the life of a |
| 361 | * WebView. |
| 362 | */ |
| 363 | public Observer addObserver(WebSettings s) { |
| 364 | Observer old = mWebSettingsToObservers.get(s); |
| 365 | if (old != null) { |
| 366 | super.deleteObserver(old); |
| 367 | } |
| 368 | Observer o = new Observer(s); |
| 369 | mWebSettingsToObservers.put(s, o); |
| 370 | super.addObserver(o); |
| 371 | return o; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Delete the given WebSettings observer from the list of observers. |
| 376 | * @param s The WebSettings object to be deleted. |
| 377 | */ |
| 378 | public void deleteObserver(WebSettings s) { |
| 379 | Observer o = mWebSettingsToObservers.get(s); |
| 380 | if (o != null) { |
| 381 | mWebSettingsToObservers.remove(s); |
| 382 | super.deleteObserver(o); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * Package level method for obtaining a single app instance of the |
| 388 | * BrowserSettings. |
| 389 | */ |
| 390 | /*package*/ static BrowserSettings getInstance() { |
| 391 | if (sSingleton == null ) { |
| 392 | sSingleton = new BrowserSettings(); |
| 393 | } |
| 394 | return sSingleton; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * Package level method for associating the BrowserSettings with TabControl |
| 399 | */ |
| 400 | /* package */void setTabControl(TabControl tabControl) { |
| 401 | mTabControl = tabControl; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * Update all the observers of the object. |
| 406 | */ |
| 407 | /*package*/ void update() { |
| 408 | setChanged(); |
| 409 | notifyObservers(); |
| 410 | } |
| 411 | |
| 412 | /*package*/ void clearCache(Context context) { |
| 413 | WebIconDatabase.getInstance().removeAllIcons(); |
| 414 | if (mTabControl != null) { |
| 415 | WebView current = mTabControl.getCurrentWebView(); |
| 416 | if (current != null) { |
| 417 | current.clearCache(true); |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /*package*/ void clearCookies(Context context) { |
| 423 | CookieManager.getInstance().removeAllCookie(); |
| 424 | } |
| 425 | |
| 426 | /* package */void clearHistory(Context context) { |
| 427 | ContentResolver resolver = context.getContentResolver(); |
| 428 | Browser.clearHistory(resolver); |
| 429 | Browser.clearSearches(resolver); |
| 430 | } |
| 431 | |
| 432 | /* package */ void clearFormData(Context context) { |
| 433 | WebViewDatabase.getInstance(context).clearFormData(); |
| 434 | if (mTabControl != null) { |
| 435 | mTabControl.getCurrentTopWebView().clearFormData(); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /*package*/ void clearPasswords(Context context) { |
| 440 | WebViewDatabase db = WebViewDatabase.getInstance(context); |
| 441 | db.clearUsernamePassword(); |
| 442 | db.clearHttpAuthUsernamePassword(); |
| 443 | } |
| 444 | |
| 445 | /*package*/ void resetDefaultPreferences(Context context) { |
| 446 | SharedPreferences p = |
| 447 | PreferenceManager.getDefaultSharedPreferences(context); |
| 448 | p.edit().clear().commit(); |
| 449 | PreferenceManager.setDefaultValues(context, R.xml.browser_preferences, |
| 450 | true); |
| 451 | } |
| 452 | |
| 453 | // Private constructor that does nothing. |
| 454 | private BrowserSettings() { |
| 455 | } |
| 456 | } |