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 android.app.Activity;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.content.pm.ActivityInfo;
23 import android.content.SharedPreferences;
24 import android.content.SharedPreferences.Editor;
25 import android.os.SystemProperties;
26 import android.view.WindowManager;
27 import android.webkit.CacheManager;
28 import android.webkit.CookieManager;
29 import android.webkit.WebViewDatabase;
30 import android.webkit.WebIconDatabase;
31 import android.webkit.WebSettings;
32 import android.preference.PreferenceManager;
33 import android.provider.Browser;
35 import java.util.HashMap;
36 import java.util.Observable;
39 * Package level class for storing various WebView and Browser settings. To use
41 * BrowserSettings s = BrowserSettings.getInstance();
42 * s.addObserver(webView.getSettings());
43 * s.loadFromDb(context); // Only needed on app startup
44 * s.javaScriptEnabled = true;
45 * ... // set any other settings
46 * s.update(); // this will update all the observers
48 * To remove an observer:
49 * s.deleteObserver(webView.getSettings());
51 class BrowserSettings extends Observable {
53 // Public variables for settings
54 // NOTE: these defaults need to be kept in sync with the XML
55 // until the performance of PreferenceManager.setDefaultValues()
57 private boolean loadsImagesAutomatically = true;
58 private boolean javaScriptEnabled = true;
59 private boolean pluginsEnabled = true;
60 private String pluginsPath; // default value set in loadFromDb().
61 private boolean javaScriptCanOpenWindowsAutomatically = false;
62 private boolean showSecurityWarnings = true;
63 private boolean rememberPasswords = true;
64 private boolean saveFormData = true;
65 private boolean openInBackground = false;
66 private String defaultTextEncodingName;
67 private String homeUrl = "http://www.google.com/m?client=ms-" +
68 SystemProperties.get("persist.sys.com.google.clientid", "unknown");
69 private boolean loginInitialized = false;
70 private boolean autoFitPage = true;
71 private boolean showDebugSettings = false;
73 // Development settings
74 public WebSettings.LayoutAlgorithm layoutAlgorithm =
75 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
76 private boolean useWideViewPort = true;
77 private int userAgent = 0;
78 private boolean tracing = false;
79 private boolean lightTouch = false;
80 private boolean navDump = false;
81 // Browser only settings
82 private boolean doFlick = false;
84 // Private preconfigured values
85 private static int minimumFontSize = 8;
86 private static int minimumLogicalFontSize = 8;
87 private static int defaultFontSize = 16;
88 private static int defaultFixedFontSize = 13;
89 private static WebSettings.TextSize textSize =
90 WebSettings.TextSize.NORMAL;
92 // Preference keys that are used outside this class
93 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
94 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
95 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
96 public final static String PREF_HOMEPAGE = "homepage";
97 public final static String PREF_CLEAR_FORM_DATA =
98 "privacy_clear_form_data";
99 public final static String PREF_CLEAR_PASSWORDS =
100 "privacy_clear_passwords";
101 public final static String PREF_EXTRAS_RESET_DEFAULTS =
102 "reset_default_preferences";
103 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
104 public final static String PREF_GEARS_SETTINGS = "gears_settings";
105 public final static String PREF_TEXT_SIZE = "text_size";
106 public final static String PREF_DEFAULT_TEXT_ENCODING =
107 "default_text_encoding";
109 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
110 "U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/525.18 (KHTML, " +
111 "like Gecko) Version/3.1.2 Safari/525.20.1";
113 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
114 "CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 " +
115 "(KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20";
117 // Value to truncate strings when adding them to a TextView within
119 public final static int MAX_TEXTVIEW_LEN = 80;
121 private TabControl mTabControl;
123 // Single instance of the BrowserSettings for use in the Browser app.
124 private static BrowserSettings sSingleton;
126 // Private map of WebSettings to Observer objects used when deleting an
128 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
129 new HashMap<WebSettings,Observer>();
132 * An observer wrapper for updating a WebSettings object with the new
133 * settings after a call to BrowserSettings.update().
135 static class Observer implements java.util.Observer {
136 // Private WebSettings object that will be updated.
137 private WebSettings mSettings;
139 Observer(WebSettings w) {
143 public void update(Observable o, Object arg) {
144 BrowserSettings b = (BrowserSettings)o;
145 WebSettings s = mSettings;
147 s.setLayoutAlgorithm(b.layoutAlgorithm);
148 if (b.userAgent == 0) {
149 // use the default ua string
150 s.setUserAgentString(null);
151 } else if (b.userAgent == 1) {
152 s.setUserAgentString(DESKTOP_USERAGENT);
153 } else if (b.userAgent == 2) {
154 s.setUserAgentString(IPHONE_USERAGENT);
156 s.setUseWideViewPort(b.useWideViewPort);
157 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
158 s.setJavaScriptEnabled(b.javaScriptEnabled);
159 s.setPluginsEnabled(b.pluginsEnabled);
160 s.setPluginsPath(b.pluginsPath);
161 s.setJavaScriptCanOpenWindowsAutomatically(
162 b.javaScriptCanOpenWindowsAutomatically);
163 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
164 s.setMinimumFontSize(b.minimumFontSize);
165 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
166 s.setDefaultFontSize(b.defaultFontSize);
167 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
168 s.setNavDump(b.navDump);
169 s.setTextSize(b.textSize);
170 s.setLightTouchEnabled(b.lightTouch);
171 s.setSaveFormData(b.saveFormData);
172 s.setSavePassword(b.rememberPasswords);
174 // WebView inside Browser doesn't want initial focus to be set.
175 s.setNeedInitialFocus(false);
176 // Browser supports multiple windows
177 s.setSupportMultipleWindows(true);
178 // Turn off file access
179 s.setAllowFileAccess(false);
184 * Load settings from the browser app's database.
185 * NOTE: Strings used for the preferences must match those specified
186 * in the browser_preferences.xml
187 * @param ctx A Context object used to query the browser's settings
188 * database. If the database exists, the saved settings will be
189 * stored in this BrowserSettings object. This will update all
190 * observers of this object.
192 public void loadFromDb(Context ctx) {
193 SharedPreferences p =
194 PreferenceManager.getDefaultSharedPreferences(ctx);
196 // Set the default value for the plugins path to the application's
198 pluginsPath = ctx.getDir("plugins", 0).getPath();
200 // Load the defaults from the xml
201 // This call is TOO SLOW, need to manually keep the defaults
203 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
204 syncSharedPreferences(p);
207 /* package */ void syncSharedPreferences(SharedPreferences p) {
209 p.getString(PREF_HOMEPAGE, homeUrl);
210 loadsImagesAutomatically = p.getBoolean("load_images",
211 loadsImagesAutomatically);
212 javaScriptEnabled = p.getBoolean("enable_javascript",
214 pluginsEnabled = p.getBoolean("enable_plugins",
216 pluginsPath = p.getString("plugins_path", pluginsPath);
217 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
218 "block_popup_windows",
219 !javaScriptCanOpenWindowsAutomatically);
220 showSecurityWarnings = p.getBoolean("show_security_warnings",
221 showSecurityWarnings);
222 rememberPasswords = p.getBoolean("remember_passwords",
224 saveFormData = p.getBoolean("save_formdata",
226 boolean accept_cookies = p.getBoolean("accept_cookies",
227 CookieManager.getInstance().acceptCookie());
228 CookieManager.getInstance().setAcceptCookie(accept_cookies);
229 openInBackground = p.getBoolean("open_in_background", openInBackground);
230 loginInitialized = p.getBoolean("login_initialized", loginInitialized);
231 textSize = WebSettings.TextSize.valueOf(
232 p.getString(PREF_TEXT_SIZE, textSize.name()));
233 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
234 useWideViewPort = true; // use wide view port for either setting
236 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
238 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
240 defaultTextEncodingName =
241 p.getString(PREF_DEFAULT_TEXT_ENCODING,
242 defaultTextEncodingName);
245 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
246 // Debug menu items have precidence if the menu is visible
247 if (showDebugSettings) {
248 boolean small_screen = p.getBoolean("small_screen",
250 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
252 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
254 boolean normal_layout = p.getBoolean("normal_layout",
255 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
257 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
260 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
263 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
264 tracing = p.getBoolean("enable_tracing", tracing);
265 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
266 navDump = p.getBoolean("enable_nav_dump", navDump);
267 doFlick = p.getBoolean("enable_flick", doFlick);
268 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
269 mTabControl.getBrowserActivity().setBaseSearchUrl(
270 p.getString("search_url", ""));
275 public String getPluginsPath() {
279 public String getHomePage() {
283 public void setHomePage(Context context, String url) {
284 Editor ed = PreferenceManager.
285 getDefaultSharedPreferences(context).edit();
286 ed.putString(PREF_HOMEPAGE, url);
291 public boolean isLoginInitialized() {
292 return loginInitialized;
295 public void setLoginInitialized(Context context) {
296 loginInitialized = true;
297 Editor ed = PreferenceManager.
298 getDefaultSharedPreferences(context).edit();
299 ed.putBoolean("login_initialized", loginInitialized);
303 public WebSettings.TextSize getTextSize() {
307 public boolean openInBackground() {
308 return openInBackground;
311 public boolean showSecurityWarnings() {
312 return showSecurityWarnings;
315 public boolean isTracing() {
319 public boolean isLightTouch() {
323 public boolean isNavDump() {
327 public boolean doFlick() {
331 public boolean showDebugSettings() {
332 return showDebugSettings;
335 public void toggleDebugSettings() {
336 showDebugSettings = !showDebugSettings;
337 navDump = showDebugSettings;
342 * Add a WebSettings object to the list of observers that will be updated
343 * when update() is called.
345 * @param s A WebSettings object that is strictly tied to the life of a
348 public Observer addObserver(WebSettings s) {
349 Observer old = mWebSettingsToObservers.get(s);
351 super.deleteObserver(old);
353 Observer o = new Observer(s);
354 mWebSettingsToObservers.put(s, o);
355 super.addObserver(o);
360 * Delete the given WebSettings observer from the list of observers.
361 * @param s The WebSettings object to be deleted.
363 public void deleteObserver(WebSettings s) {
364 Observer o = mWebSettingsToObservers.get(s);
366 mWebSettingsToObservers.remove(s);
367 super.deleteObserver(o);
372 * Package level method for obtaining a single app instance of the
375 /*package*/ static BrowserSettings getInstance() {
376 if (sSingleton == null ) {
377 sSingleton = new BrowserSettings();
383 * Package level method for associating the BrowserSettings with TabControl
385 /* package */void setTabControl(TabControl tabControl) {
386 mTabControl = tabControl;
390 * Update all the observers of the object.
392 /*package*/ void update() {
397 /*package*/ void clearCache(Context context) {
398 WebIconDatabase.getInstance().removeAllIcons();
399 if (mTabControl != null) {
400 mTabControl.getCurrentWebView().clearCache(true);
404 /*package*/ void clearCookies(Context context) {
405 CookieManager.getInstance().removeAllCookie();
408 /* package */void clearHistory(Context context) {
409 ContentResolver resolver = context.getContentResolver();
410 Browser.clearHistory(resolver);
411 Browser.clearSearches(resolver);
414 /* package */ void clearFormData(Context context) {
415 WebViewDatabase.getInstance(context).clearFormData();
416 if (mTabControl != null) {
417 mTabControl.getCurrentTopWebView().clearFormData();
421 /*package*/ void clearPasswords(Context context) {
422 WebViewDatabase db = WebViewDatabase.getInstance(context);
423 db.clearUsernamePassword();
424 db.clearHttpAuthUsernamePassword();
427 /*package*/ void resetDefaultPreferences(Context context) {
428 SharedPreferences p =
429 PreferenceManager.getDefaultSharedPreferences(context);
430 p.edit().clear().commit();
431 PreferenceManager.setDefaultValues(context, R.xml.browser_preferences,
435 // Private constructor that does nothing.
436 private BrowserSettings() {