2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.browser;
19 import com.google.android.providers.GoogleSettings.Partner;
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;
26 import android.preference.PreferenceActivity;
27 import android.preference.PreferenceScreen;
28 import android.webkit.CookieManager;
29 import android.webkit.GeolocationPermissions;
30 import android.webkit.WebView;
31 import android.webkit.WebViewDatabase;
32 import android.webkit.WebIconDatabase;
33 import android.webkit.WebSettings;
34 import android.webkit.WebStorage;
35 import android.preference.PreferenceManager;
36 import android.provider.Browser;
39 import java.util.HashMap;
40 import java.util.Observable;
43 * Package level class for storing various WebView and Browser settings. To use
45 * BrowserSettings s = BrowserSettings.getInstance();
46 * s.addObserver(webView.getSettings());
47 * s.loadFromDb(context); // Only needed on app startup
48 * s.javaScriptEnabled = true;
49 * ... // set any other settings
50 * s.update(); // this will update all the observers
52 * To remove an observer:
53 * s.deleteObserver(webView.getSettings());
55 class BrowserSettings extends Observable {
57 // Private variables for settings
58 // NOTE: these defaults need to be kept in sync with the XML
59 // until the performance of PreferenceManager.setDefaultValues()
61 private boolean loadsImagesAutomatically = true;
62 private boolean javaScriptEnabled = true;
63 private boolean pluginsEnabled = true;
64 private String pluginsPath; // default value set in loadFromDb().
65 private boolean javaScriptCanOpenWindowsAutomatically = false;
66 private boolean showSecurityWarnings = true;
67 private boolean rememberPasswords = true;
68 private boolean saveFormData = true;
69 private boolean openInBackground = false;
70 private String defaultTextEncodingName;
71 private String homeUrl = "";
72 private boolean loginInitialized = false;
73 private boolean autoFitPage = true;
74 private boolean landscapeOnly = false;
75 private boolean loadsPageInOverviewMode = true;
76 private boolean showDebugSettings = false;
78 private boolean appCacheEnabled = true;
79 private boolean databaseEnabled = true;
80 private boolean domStorageEnabled = true;
81 private boolean geolocationEnabled = true;
82 private boolean workersEnabled = true; // only affects V8. JSC does not have a similar setting
83 // HTML5 API configuration params
84 private long appCacheMaxSize = Long.MAX_VALUE;
85 private String appCachePath; // default value set in loadFromDb().
86 private String databasePath; // default value set in loadFromDb()
87 private String geolocationDatabasePath; // default value set in loadFromDb()
88 private WebStorageSizeManager webStorageSizeManager;
90 private String jsFlags = "";
92 private final static String TAG = "BrowserSettings";
94 // Development settings
95 public WebSettings.LayoutAlgorithm layoutAlgorithm =
96 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
97 private boolean useWideViewPort = true;
98 private int userAgent = 0;
99 private boolean tracing = false;
100 private boolean lightTouch = false;
101 private boolean navDump = false;
103 // By default the error console is shown once the user navigates to about:debug.
104 // The setting can be then toggled from the settings menu.
105 private boolean showConsole = true;
107 // Browser only settings
108 private boolean doFlick = false;
110 // Private preconfigured values
111 private static int minimumFontSize = 8;
112 private static int minimumLogicalFontSize = 8;
113 private static int defaultFontSize = 16;
114 private static int defaultFixedFontSize = 13;
115 private static WebSettings.TextSize textSize =
116 WebSettings.TextSize.NORMAL;
117 private static WebSettings.ZoomDensity zoomDensity =
118 WebSettings.ZoomDensity.MEDIUM;
120 // Preference keys that are used outside this class
121 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
122 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
123 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
124 public final static String PREF_HOMEPAGE = "homepage";
125 public final static String PREF_CLEAR_FORM_DATA =
126 "privacy_clear_form_data";
127 public final static String PREF_CLEAR_PASSWORDS =
128 "privacy_clear_passwords";
129 public final static String PREF_EXTRAS_RESET_DEFAULTS =
130 "reset_default_preferences";
131 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
132 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
133 public final static String PREF_TEXT_SIZE = "text_size";
134 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
135 public final static String PREF_DEFAULT_TEXT_ENCODING =
136 "default_text_encoding";
137 public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
138 "privacy_clear_geolocation_access";
140 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
141 "U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, " +
142 "like Gecko) Version/4.0 Safari/530.17";
144 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
145 "CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 " +
146 "(KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16";
148 // Value to truncate strings when adding them to a TextView within
150 public final static int MAX_TEXTVIEW_LEN = 80;
152 private TabControl mTabControl;
154 // Single instance of the BrowserSettings for use in the Browser app.
155 private static BrowserSettings sSingleton;
157 // Private map of WebSettings to Observer objects used when deleting an
159 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
160 new HashMap<WebSettings,Observer>();
163 * An observer wrapper for updating a WebSettings object with the new
164 * settings after a call to BrowserSettings.update().
166 static class Observer implements java.util.Observer {
167 // Private WebSettings object that will be updated.
168 private WebSettings mSettings;
170 Observer(WebSettings w) {
174 public void update(Observable o, Object arg) {
175 BrowserSettings b = (BrowserSettings)o;
176 WebSettings s = mSettings;
178 s.setLayoutAlgorithm(b.layoutAlgorithm);
179 if (b.userAgent == 0) {
180 // use the default ua string
181 s.setUserAgentString(null);
182 } else if (b.userAgent == 1) {
183 s.setUserAgentString(DESKTOP_USERAGENT);
184 } else if (b.userAgent == 2) {
185 s.setUserAgentString(IPHONE_USERAGENT);
187 s.setUseWideViewPort(b.useWideViewPort);
188 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
189 s.setJavaScriptEnabled(b.javaScriptEnabled);
190 s.setPluginsEnabled(b.pluginsEnabled);
191 s.setJavaScriptCanOpenWindowsAutomatically(
192 b.javaScriptCanOpenWindowsAutomatically);
193 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
194 s.setMinimumFontSize(b.minimumFontSize);
195 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
196 s.setDefaultFontSize(b.defaultFontSize);
197 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
198 s.setNavDump(b.navDump);
199 s.setTextSize(b.textSize);
200 s.setDefaultZoom(b.zoomDensity);
201 s.setLightTouchEnabled(b.lightTouch);
202 s.setSaveFormData(b.saveFormData);
203 s.setSavePassword(b.rememberPasswords);
204 s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
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);
210 // Turn off file access
211 s.setAllowFileAccess(false);
214 s.setAppCacheEnabled(b.appCacheEnabled);
215 s.setDatabaseEnabled(b.databaseEnabled);
216 s.setDomStorageEnabled(b.domStorageEnabled);
217 s.setWorkersEnabled(b.workersEnabled); // This only affects V8.
218 s.setGeolocationEnabled(b.geolocationEnabled);
220 // HTML5 configuration parameters.
221 s.setAppCacheMaxSize(b.appCacheMaxSize);
222 s.setAppCachePath(b.appCachePath);
223 s.setDatabasePath(b.databasePath);
224 s.setGeolocationDatabasePath(b.geolocationDatabasePath);
226 // Enable/Disable the error console.
227 b.mTabControl.getBrowserActivity().setShouldShowErrorConsole(
228 b.showDebugSettings && b.showConsole);
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.
241 public void loadFromDb(Context ctx) {
242 SharedPreferences p =
243 PreferenceManager.getDefaultSharedPreferences(ctx);
245 // Set the default value for the plugins path to the application's
247 pluginsPath = ctx.getDir("plugins", 0).getPath();
248 // Set the default value for the Application Caches path.
249 appCachePath = ctx.getDir("appcache", 0).getPath();
250 // Determine the maximum size of the application cache.
251 webStorageSizeManager = new WebStorageSizeManager(
253 new WebStorageSizeManager.StatFsDiskInfo(appCachePath),
254 new WebStorageSizeManager.WebKitAppCacheInfo(appCachePath));
255 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
256 // Set the default value for the Database path.
257 databasePath = ctx.getDir("databases", 0).getPath();
258 // Set the default value for the Geolocation database path.
259 geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
261 homeUrl = getFactoryResetHomeUrl(ctx);
263 // Load the defaults from the xml
264 // This call is TOO SLOW, need to manually keep the defaults
266 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
267 syncSharedPreferences(p);
270 /* package */ void syncSharedPreferences(SharedPreferences p) {
273 p.getString(PREF_HOMEPAGE, homeUrl);
275 loadsImagesAutomatically = p.getBoolean("load_images",
276 loadsImagesAutomatically);
277 javaScriptEnabled = p.getBoolean("enable_javascript",
279 pluginsEnabled = p.getBoolean("enable_plugins",
281 pluginsPath = p.getString("plugins_path", pluginsPath);
282 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
283 "block_popup_windows",
284 !javaScriptCanOpenWindowsAutomatically);
285 showSecurityWarnings = p.getBoolean("show_security_warnings",
286 showSecurityWarnings);
287 rememberPasswords = p.getBoolean("remember_passwords",
289 saveFormData = p.getBoolean("save_formdata",
291 boolean accept_cookies = p.getBoolean("accept_cookies",
292 CookieManager.getInstance().acceptCookie());
293 CookieManager.getInstance().setAcceptCookie(accept_cookies);
294 openInBackground = p.getBoolean("open_in_background", openInBackground);
295 loginInitialized = p.getBoolean("login_initialized", loginInitialized);
296 textSize = WebSettings.TextSize.valueOf(
297 p.getString(PREF_TEXT_SIZE, textSize.name()));
298 zoomDensity = WebSettings.ZoomDensity.valueOf(
299 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
300 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
301 loadsPageInOverviewMode = p.getBoolean("load_page",
302 loadsPageInOverviewMode);
303 boolean landscapeOnlyTemp =
304 p.getBoolean("landscape_only", landscapeOnly);
305 if (landscapeOnlyTemp != landscapeOnly) {
306 landscapeOnly = landscapeOnlyTemp;
307 mTabControl.getBrowserActivity().setRequestedOrientation(
308 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
309 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
311 useWideViewPort = true; // use wide view port for either setting
313 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
315 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
317 defaultTextEncodingName =
318 p.getString(PREF_DEFAULT_TEXT_ENCODING,
319 defaultTextEncodingName);
322 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
323 // Debug menu items have precidence if the menu is visible
324 if (showDebugSettings) {
325 boolean small_screen = p.getBoolean("small_screen",
327 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
329 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
331 boolean normal_layout = p.getBoolean("normal_layout",
332 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
334 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
337 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
340 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
341 tracing = p.getBoolean("enable_tracing", tracing);
342 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
343 navDump = p.getBoolean("enable_nav_dump", navDump);
344 doFlick = p.getBoolean("enable_flick", doFlick);
345 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
347 // JS flags is loaded from DB even if showDebugSettings is false,
348 // so that it can be set once and be effective all the time.
349 jsFlags = p.getString("js_engine_flags", "");
351 // Read the setting for showing/hiding the JS Console always so that should the
352 // user enable debug settings, we already know if we should show the console.
353 // The user will never see the console unless they navigate to about:debug,
354 // regardless of the setting we read here. This setting is only used after debug
356 showConsole = p.getBoolean("javascript_console", showConsole);
357 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
358 showDebugSettings && showConsole);
361 appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
362 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
363 domStorageEnabled = p.getBoolean("enable_domstorage", domStorageEnabled);
364 geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
365 workersEnabled = p.getBoolean("enable_workers", workersEnabled);
370 public String getPluginsPath() {
374 public String getHomePage() {
378 public String getJsFlags() {
382 public WebStorageSizeManager getWebStorageSizeManager() {
383 return webStorageSizeManager;
386 public void setHomePage(Context context, String url) {
387 Editor ed = PreferenceManager.
388 getDefaultSharedPreferences(context).edit();
389 ed.putString(PREF_HOMEPAGE, url);
394 public boolean isLoginInitialized() {
395 return loginInitialized;
398 public void setLoginInitialized(Context context) {
399 loginInitialized = true;
400 Editor ed = PreferenceManager.
401 getDefaultSharedPreferences(context).edit();
402 ed.putBoolean("login_initialized", loginInitialized);
406 public WebSettings.TextSize getTextSize() {
410 public WebSettings.ZoomDensity getDefaultZoom() {
414 public boolean openInBackground() {
415 return openInBackground;
418 public boolean showSecurityWarnings() {
419 return showSecurityWarnings;
422 public boolean isTracing() {
426 public boolean isLightTouch() {
430 public boolean isNavDump() {
434 public boolean doFlick() {
438 public boolean showDebugSettings() {
439 return showDebugSettings;
442 public void toggleDebugSettings() {
443 showDebugSettings = !showDebugSettings;
444 navDump = showDebugSettings;
449 * Add a WebSettings object to the list of observers that will be updated
450 * when update() is called.
452 * @param s A WebSettings object that is strictly tied to the life of a
455 public Observer addObserver(WebSettings s) {
456 Observer old = mWebSettingsToObservers.get(s);
458 super.deleteObserver(old);
460 Observer o = new Observer(s);
461 mWebSettingsToObservers.put(s, o);
462 super.addObserver(o);
467 * Delete the given WebSettings observer from the list of observers.
468 * @param s The WebSettings object to be deleted.
470 public void deleteObserver(WebSettings s) {
471 Observer o = mWebSettingsToObservers.get(s);
473 mWebSettingsToObservers.remove(s);
474 super.deleteObserver(o);
479 * Package level method for obtaining a single app instance of the
482 /*package*/ static BrowserSettings getInstance() {
483 if (sSingleton == null ) {
484 sSingleton = new BrowserSettings();
490 * Package level method for associating the BrowserSettings with TabControl
492 /* package */void setTabControl(TabControl tabControl) {
493 mTabControl = tabControl;
497 * Update all the observers of the object.
499 /*package*/ void update() {
504 /*package*/ void clearCache(Context context) {
505 WebIconDatabase.getInstance().removeAllIcons();
506 if (mTabControl != null) {
507 WebView current = mTabControl.getCurrentWebView();
508 if (current != null) {
509 current.clearCache(true);
514 /*package*/ void clearCookies(Context context) {
515 CookieManager.getInstance().removeAllCookie();
518 /* package */void clearHistory(Context context) {
519 ContentResolver resolver = context.getContentResolver();
520 Browser.clearHistory(resolver);
521 Browser.clearSearches(resolver);
524 /* package */ void clearFormData(Context context) {
525 WebViewDatabase.getInstance(context).clearFormData();
526 if (mTabControl != null) {
527 mTabControl.getCurrentTopWebView().clearFormData();
531 /*package*/ void clearPasswords(Context context) {
532 WebViewDatabase db = WebViewDatabase.getInstance(context);
533 db.clearUsernamePassword();
534 db.clearHttpAuthUsernamePassword();
537 private void maybeDisableWebsiteSettings(Context context) {
538 Set webStorageOrigins = WebStorage.getInstance().getOrigins();
539 Set geolocationOrigins =
540 GeolocationPermissions.getInstance().getOrigins();
541 if (((webStorageOrigins == null) || webStorageOrigins.isEmpty()) &&
542 ((geolocationOrigins == null) || geolocationOrigins.isEmpty())) {
543 PreferenceActivity activity = (PreferenceActivity) context;
544 PreferenceScreen screen = (PreferenceScreen)
545 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
546 screen.setEnabled(false);
550 /*package*/ void clearDatabases(Context context) {
551 WebStorage.getInstance().deleteAllData();
552 maybeDisableWebsiteSettings(context);
555 /*package*/ void clearLocationAccess(Context context) {
556 GeolocationPermissions.getInstance().clearAll();
557 maybeDisableWebsiteSettings(context);
560 /*package*/ void resetDefaultPreferences(Context ctx) {
561 SharedPreferences p =
562 PreferenceManager.getDefaultSharedPreferences(ctx);
563 p.edit().clear().commit();
564 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
567 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
568 // reset appcache max size
569 appCacheMaxSize = webStorageSizeManager.getAppCacheMaxSize();
572 private String getFactoryResetHomeUrl(Context context) {
573 String url = context.getResources().getString(R.string.homepage_base);
574 if (url.indexOf("{CID}") != -1) {
575 url = url.replace("{CID}", Partner.getString(context
576 .getContentResolver(), Partner.CLIENT_ID, "android-google"));
581 // Private constructor that does nothing.
582 private BrowserSettings() {