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.WebView;
30 import android.webkit.WebViewDatabase;
31 import android.webkit.WebIconDatabase;
32 import android.webkit.WebSettings;
33 import android.webkit.WebStorage;
34 import android.preference.PreferenceManager;
35 import android.provider.Browser;
37 import java.util.HashMap;
38 import java.util.Observable;
41 * Package level class for storing various WebView and Browser settings. To use
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
50 * To remove an observer:
51 * s.deleteObserver(webView.getSettings());
53 class BrowserSettings extends Observable {
55 // Private variables for settings
56 // NOTE: these defaults need to be kept in sync with the XML
57 // until the performance of PreferenceManager.setDefaultValues()
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;
69 private String homeUrl = "";
70 private boolean loginInitialized = false;
71 private boolean autoFitPage = true;
72 private boolean landscapeOnly = false;
73 private boolean showDebugSettings = false;
74 private String databasePath; // default value set in loadFromDb()
75 private boolean databaseEnabled = true;
76 private long webStorageDefaultQuota = 5 * 1024 * 1024;
77 // The Browser always enables Application Caches.
78 private boolean appCacheEnabled = true;
79 private String appCachePath; // default value set in loadFromDb().
80 private boolean domStorageEnabled = true;
81 private String jsFlags = "";
83 private final static String TAG = "BrowserSettings";
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;
94 // By default the error console is shown once the user navigates to about:debug.
95 // The setting can be then toggled from the settings menu.
96 private boolean showConsole = true;
98 // Browser only settings
99 private boolean doFlick = false;
101 // Private preconfigured values
102 private static int minimumFontSize = 8;
103 private static int minimumLogicalFontSize = 8;
104 private static int defaultFontSize = 16;
105 private static int defaultFixedFontSize = 13;
106 private static WebSettings.TextSize textSize =
107 WebSettings.TextSize.NORMAL;
108 private static WebSettings.ZoomDensity zoomDensity =
109 WebSettings.ZoomDensity.MEDIUM;
111 // Preference keys that are used outside this class
112 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
113 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
114 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
115 public final static String PREF_HOMEPAGE = "homepage";
116 public final static String PREF_CLEAR_FORM_DATA =
117 "privacy_clear_form_data";
118 public final static String PREF_CLEAR_PASSWORDS =
119 "privacy_clear_passwords";
120 public final static String PREF_DEFAULT_QUOTA =
121 "webstorage_default_quota";
122 public final static String PREF_EXTRAS_RESET_DEFAULTS =
123 "reset_default_preferences";
124 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
125 public final static String PREF_GEARS_SETTINGS = "gears_settings";
126 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
127 public final static String PREF_TEXT_SIZE = "text_size";
128 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
129 public final static String PREF_DEFAULT_TEXT_ENCODING =
130 "default_text_encoding";
132 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
133 "U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, " +
134 "like Gecko) Version/4.0 Safari/530.17";
136 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
137 "CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 " +
138 "(KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16";
140 // Value to truncate strings when adding them to a TextView within
142 public final static int MAX_TEXTVIEW_LEN = 80;
144 private TabControl mTabControl;
146 // Single instance of the BrowserSettings for use in the Browser app.
147 private static BrowserSettings sSingleton;
149 // Private map of WebSettings to Observer objects used when deleting an
151 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
152 new HashMap<WebSettings,Observer>();
155 * An observer wrapper for updating a WebSettings object with the new
156 * settings after a call to BrowserSettings.update().
158 static class Observer implements java.util.Observer {
159 // Private WebSettings object that will be updated.
160 private WebSettings mSettings;
162 Observer(WebSettings w) {
166 public void update(Observable o, Object arg) {
167 BrowserSettings b = (BrowserSettings)o;
168 WebSettings s = mSettings;
170 s.setLayoutAlgorithm(b.layoutAlgorithm);
171 if (b.userAgent == 0) {
172 // use the default ua string
173 s.setUserAgentString(null);
174 } else if (b.userAgent == 1) {
175 s.setUserAgentString(DESKTOP_USERAGENT);
176 } else if (b.userAgent == 2) {
177 s.setUserAgentString(IPHONE_USERAGENT);
179 s.setUseWideViewPort(b.useWideViewPort);
180 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
181 s.setJavaScriptEnabled(b.javaScriptEnabled);
182 s.setPluginsEnabled(b.pluginsEnabled);
183 s.setJavaScriptCanOpenWindowsAutomatically(
184 b.javaScriptCanOpenWindowsAutomatically);
185 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
186 s.setMinimumFontSize(b.minimumFontSize);
187 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
188 s.setDefaultFontSize(b.defaultFontSize);
189 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
190 s.setNavDump(b.navDump);
191 s.setTextSize(b.textSize);
192 s.setDefaultZoom(b.zoomDensity);
193 s.setLightTouchEnabled(b.lightTouch);
194 s.setSaveFormData(b.saveFormData);
195 s.setSavePassword(b.rememberPasswords);
197 // WebView inside Browser doesn't want initial focus to be set.
198 s.setNeedInitialFocus(false);
199 // Browser supports multiple windows
200 s.setSupportMultipleWindows(true);
201 // Turn off file access
202 s.setAllowFileAccess(false);
204 s.setDatabasePath(b.databasePath);
205 s.setDatabaseEnabled(b.databaseEnabled);
206 s.setDomStorageEnabled(b.domStorageEnabled);
207 s.setWebStorageDefaultQuota(b.webStorageDefaultQuota);
209 // Turn on Application Caches.
210 s.setAppCachePath(b.appCachePath);
211 s.setAppCacheEnabled(b.appCacheEnabled);
213 // Enable/Disable the error console.
214 b.mTabControl.getBrowserActivity().setShouldShowErrorConsole(
215 b.showDebugSettings && b.showConsole);
220 * Load settings from the browser app's database.
221 * NOTE: Strings used for the preferences must match those specified
222 * in the browser_preferences.xml
223 * @param ctx A Context object used to query the browser's settings
224 * database. If the database exists, the saved settings will be
225 * stored in this BrowserSettings object. This will update all
226 * observers of this object.
228 public void loadFromDb(Context ctx) {
229 SharedPreferences p =
230 PreferenceManager.getDefaultSharedPreferences(ctx);
232 // Set the default value for the plugins path to the application's
234 pluginsPath = ctx.getDir("plugins", 0).getPath();
235 // Set the default value for the Application Caches path.
236 appCachePath = ctx.getDir("appcache", 0).getPath();
237 // Set the default value for the Database path.
238 databasePath = ctx.getDir("databases", 0).getPath();
240 homeUrl = getFactoryResetHomeUrl(ctx);
242 // Load the defaults from the xml
243 // This call is TOO SLOW, need to manually keep the defaults
245 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
246 syncSharedPreferences(p);
249 /* package */ void syncSharedPreferences(SharedPreferences p) {
252 p.getString(PREF_HOMEPAGE, homeUrl);
254 loadsImagesAutomatically = p.getBoolean("load_images",
255 loadsImagesAutomatically);
256 javaScriptEnabled = p.getBoolean("enable_javascript",
258 pluginsEnabled = p.getBoolean("enable_plugins",
260 pluginsPath = p.getString("plugins_path", pluginsPath);
261 databasePath = p.getString("database_path", databasePath);
262 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
263 webStorageDefaultQuota = Long.parseLong(p.getString(PREF_DEFAULT_QUOTA,
264 String.valueOf(webStorageDefaultQuota)));
265 appCacheEnabled = p.getBoolean("enable_appcache",
267 domStorageEnabled = p.getBoolean("enable_domstorage",
269 appCachePath = p.getString("appcache_path", appCachePath);
270 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
271 "block_popup_windows",
272 !javaScriptCanOpenWindowsAutomatically);
273 showSecurityWarnings = p.getBoolean("show_security_warnings",
274 showSecurityWarnings);
275 rememberPasswords = p.getBoolean("remember_passwords",
277 saveFormData = p.getBoolean("save_formdata",
279 boolean accept_cookies = p.getBoolean("accept_cookies",
280 CookieManager.getInstance().acceptCookie());
281 CookieManager.getInstance().setAcceptCookie(accept_cookies);
282 openInBackground = p.getBoolean("open_in_background", openInBackground);
283 loginInitialized = p.getBoolean("login_initialized", loginInitialized);
284 textSize = WebSettings.TextSize.valueOf(
285 p.getString(PREF_TEXT_SIZE, textSize.name()));
286 zoomDensity = WebSettings.ZoomDensity.valueOf(
287 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
288 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
289 boolean landscapeOnlyTemp =
290 p.getBoolean("landscape_only", landscapeOnly);
291 if (landscapeOnlyTemp != landscapeOnly) {
292 landscapeOnly = landscapeOnlyTemp;
293 mTabControl.getBrowserActivity().setRequestedOrientation(
294 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
295 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
297 useWideViewPort = true; // use wide view port for either setting
299 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
301 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
303 defaultTextEncodingName =
304 p.getString(PREF_DEFAULT_TEXT_ENCODING,
305 defaultTextEncodingName);
308 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
309 // Debug menu items have precidence if the menu is visible
310 if (showDebugSettings) {
311 boolean small_screen = p.getBoolean("small_screen",
313 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
315 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
317 boolean normal_layout = p.getBoolean("normal_layout",
318 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
320 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
323 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
326 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
327 tracing = p.getBoolean("enable_tracing", tracing);
328 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
329 navDump = p.getBoolean("enable_nav_dump", navDump);
330 doFlick = p.getBoolean("enable_flick", doFlick);
331 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
333 // JS flags is loaded from DB even if showDebugSettings is false,
334 // so that it can be set once and be effective all the time.
335 jsFlags = p.getString("js_engine_flags", "");
337 // Read the setting for showing/hiding the JS Console always so that should the
338 // user enable debug settings, we already know if we should show the console.
339 // The user will never see the console unless they navigate to about:debug,
340 // regardless of the setting we read here. This setting is only used after debug
342 showConsole = p.getBoolean("javascript_console", showConsole);
343 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
344 showDebugSettings && showConsole);
347 public String getPluginsPath() {
351 public String getHomePage() {
355 public String getJsFlags() {
359 public void setHomePage(Context context, String url) {
360 Editor ed = PreferenceManager.
361 getDefaultSharedPreferences(context).edit();
362 ed.putString(PREF_HOMEPAGE, url);
367 public boolean isLoginInitialized() {
368 return loginInitialized;
371 public void setLoginInitialized(Context context) {
372 loginInitialized = true;
373 Editor ed = PreferenceManager.
374 getDefaultSharedPreferences(context).edit();
375 ed.putBoolean("login_initialized", loginInitialized);
379 public WebSettings.TextSize getTextSize() {
383 public WebSettings.ZoomDensity getDefaultZoom() {
387 public boolean openInBackground() {
388 return openInBackground;
391 public boolean showSecurityWarnings() {
392 return showSecurityWarnings;
395 public boolean isTracing() {
399 public boolean isLightTouch() {
403 public boolean isNavDump() {
407 public boolean doFlick() {
411 public boolean showDebugSettings() {
412 return showDebugSettings;
415 public void toggleDebugSettings() {
416 showDebugSettings = !showDebugSettings;
417 navDump = showDebugSettings;
422 * Add a WebSettings object to the list of observers that will be updated
423 * when update() is called.
425 * @param s A WebSettings object that is strictly tied to the life of a
428 public Observer addObserver(WebSettings s) {
429 Observer old = mWebSettingsToObservers.get(s);
431 super.deleteObserver(old);
433 Observer o = new Observer(s);
434 mWebSettingsToObservers.put(s, o);
435 super.addObserver(o);
440 * Delete the given WebSettings observer from the list of observers.
441 * @param s The WebSettings object to be deleted.
443 public void deleteObserver(WebSettings s) {
444 Observer o = mWebSettingsToObservers.get(s);
446 mWebSettingsToObservers.remove(s);
447 super.deleteObserver(o);
452 * Package level method for obtaining a single app instance of the
455 /*package*/ static BrowserSettings getInstance() {
456 if (sSingleton == null ) {
457 sSingleton = new BrowserSettings();
463 * Package level method for associating the BrowserSettings with TabControl
465 /* package */void setTabControl(TabControl tabControl) {
466 mTabControl = tabControl;
470 * Update all the observers of the object.
472 /*package*/ void update() {
477 /*package*/ void clearCache(Context context) {
478 WebIconDatabase.getInstance().removeAllIcons();
479 if (mTabControl != null) {
480 WebView current = mTabControl.getCurrentWebView();
481 if (current != null) {
482 current.clearCache(true);
487 /*package*/ void clearCookies(Context context) {
488 CookieManager.getInstance().removeAllCookie();
491 /* package */void clearHistory(Context context) {
492 ContentResolver resolver = context.getContentResolver();
493 Browser.clearHistory(resolver);
494 Browser.clearSearches(resolver);
497 /* package */ void clearFormData(Context context) {
498 WebViewDatabase.getInstance(context).clearFormData();
499 if (mTabControl != null) {
500 mTabControl.getCurrentTopWebView().clearFormData();
504 /*package*/ void clearPasswords(Context context) {
505 WebViewDatabase db = WebViewDatabase.getInstance(context);
506 db.clearUsernamePassword();
507 db.clearHttpAuthUsernamePassword();
510 /*package*/ void clearDatabases(Context context) {
511 WebStorage.getInstance().deleteAllDatabases();
512 // Remove all listed databases from the preferences
513 PreferenceActivity activity = (PreferenceActivity) context;
514 PreferenceScreen screen = (PreferenceScreen)
515 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
517 screen.setEnabled(false);
520 /*package*/ void resetDefaultPreferences(Context ctx) {
521 SharedPreferences p =
522 PreferenceManager.getDefaultSharedPreferences(ctx);
523 p.edit().clear().commit();
524 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
527 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
530 private String getFactoryResetHomeUrl(Context context) {
531 String url = context.getResources().getString(R.string.homepage_base);
532 if (url.indexOf("{CID}") != -1) {
533 url = url.replace("{CID}", Partner.getString(context
534 .getContentResolver(), Partner.CLIENT_ID, "android-google"));
539 // Private constructor that does nothing.
540 private BrowserSettings() {