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.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;
38 import java.util.HashMap;
39 import java.util.Observable;
42 * Package level class for storing various WebView and Browser settings. To use
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
51 * To remove an observer:
52 * s.deleteObserver(webView.getSettings());
54 class BrowserSettings extends Observable {
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()
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;
70 private String homeUrl = "http://www.google.com/m?client=ms-";
71 private boolean loginInitialized = false;
72 private boolean autoFitPage = true;
73 private boolean showDebugSettings = false;
75 // Development settings
76 public WebSettings.LayoutAlgorithm layoutAlgorithm =
77 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
78 private boolean useWideViewPort = true;
79 private int userAgent = 0;
80 private boolean tracing = false;
81 private boolean lightTouch = false;
82 private boolean navDump = false;
83 // Browser only settings
84 private boolean doFlick = false;
86 // Private preconfigured values
87 private static int minimumFontSize = 8;
88 private static int minimumLogicalFontSize = 8;
89 private static int defaultFontSize = 16;
90 private static int defaultFixedFontSize = 13;
91 private static WebSettings.TextSize textSize =
92 WebSettings.TextSize.NORMAL;
94 // Preference keys that are used outside this class
95 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
96 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
97 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
98 public final static String PREF_HOMEPAGE = "homepage";
99 public final static String PREF_CLEAR_FORM_DATA =
100 "privacy_clear_form_data";
101 public final static String PREF_CLEAR_PASSWORDS =
102 "privacy_clear_passwords";
103 public final static String PREF_EXTRAS_RESET_DEFAULTS =
104 "reset_default_preferences";
105 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
106 public final static String PREF_GEARS_SETTINGS = "gears_settings";
107 public final static String PREF_TEXT_SIZE = "text_size";
108 public final static String PREF_DEFAULT_TEXT_ENCODING =
109 "default_text_encoding";
111 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
112 "U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/525.18 (KHTML, " +
113 "like Gecko) Version/3.1.2 Safari/525.20.1";
115 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
116 "CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 " +
117 "(KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20";
119 // Value to truncate strings when adding them to a TextView within
121 public final static int MAX_TEXTVIEW_LEN = 80;
123 private TabControl mTabControl;
125 // Single instance of the BrowserSettings for use in the Browser app.
126 private static BrowserSettings sSingleton;
128 // Private map of WebSettings to Observer objects used when deleting an
130 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
131 new HashMap<WebSettings,Observer>();
134 * An observer wrapper for updating a WebSettings object with the new
135 * settings after a call to BrowserSettings.update().
137 static class Observer implements java.util.Observer {
138 // Private WebSettings object that will be updated.
139 private WebSettings mSettings;
141 Observer(WebSettings w) {
145 public void update(Observable o, Object arg) {
146 BrowserSettings b = (BrowserSettings)o;
147 WebSettings s = mSettings;
149 s.setLayoutAlgorithm(b.layoutAlgorithm);
150 if (b.userAgent == 0) {
151 // use the default ua string
152 s.setUserAgentString(null);
153 } else if (b.userAgent == 1) {
154 s.setUserAgentString(DESKTOP_USERAGENT);
155 } else if (b.userAgent == 2) {
156 s.setUserAgentString(IPHONE_USERAGENT);
158 s.setUseWideViewPort(b.useWideViewPort);
159 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
160 s.setJavaScriptEnabled(b.javaScriptEnabled);
161 s.setPluginsEnabled(b.pluginsEnabled);
162 s.setPluginsPath(b.pluginsPath);
163 s.setJavaScriptCanOpenWindowsAutomatically(
164 b.javaScriptCanOpenWindowsAutomatically);
165 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
166 s.setMinimumFontSize(b.minimumFontSize);
167 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
168 s.setDefaultFontSize(b.defaultFontSize);
169 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
170 s.setNavDump(b.navDump);
171 s.setTextSize(b.textSize);
172 s.setLightTouchEnabled(b.lightTouch);
173 s.setSaveFormData(b.saveFormData);
174 s.setSavePassword(b.rememberPasswords);
176 // WebView inside Browser doesn't want initial focus to be set.
177 s.setNeedInitialFocus(false);
178 // Browser supports multiple windows
179 s.setSupportMultipleWindows(true);
180 // Turn off file access
181 s.setAllowFileAccess(false);
186 * Load settings from the browser app's database.
187 * NOTE: Strings used for the preferences must match those specified
188 * in the browser_preferences.xml
189 * @param ctx A Context object used to query the browser's settings
190 * database. If the database exists, the saved settings will be
191 * stored in this BrowserSettings object. This will update all
192 * observers of this object.
194 public void loadFromDb(Context ctx) {
195 SharedPreferences p =
196 PreferenceManager.getDefaultSharedPreferences(ctx);
198 // Set the default value for the plugins path to the application's
200 pluginsPath = ctx.getDir("plugins", 0).getPath();
202 homeUrl += Partner.getString(ctx.getContentResolver(), Partner.CLIENT_ID);
204 // Load the defaults from the xml
205 // This call is TOO SLOW, need to manually keep the defaults
207 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
208 syncSharedPreferences(p);
211 /* package */ void syncSharedPreferences(SharedPreferences p) {
214 p.getString(PREF_HOMEPAGE, homeUrl);
216 loadsImagesAutomatically = p.getBoolean("load_images",
217 loadsImagesAutomatically);
218 javaScriptEnabled = p.getBoolean("enable_javascript",
220 pluginsEnabled = p.getBoolean("enable_plugins",
222 pluginsPath = p.getString("plugins_path", pluginsPath);
223 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
224 "block_popup_windows",
225 !javaScriptCanOpenWindowsAutomatically);
226 showSecurityWarnings = p.getBoolean("show_security_warnings",
227 showSecurityWarnings);
228 rememberPasswords = p.getBoolean("remember_passwords",
230 saveFormData = p.getBoolean("save_formdata",
232 boolean accept_cookies = p.getBoolean("accept_cookies",
233 CookieManager.getInstance().acceptCookie());
234 CookieManager.getInstance().setAcceptCookie(accept_cookies);
235 openInBackground = p.getBoolean("open_in_background", openInBackground);
236 loginInitialized = p.getBoolean("login_initialized", loginInitialized);
237 textSize = WebSettings.TextSize.valueOf(
238 p.getString(PREF_TEXT_SIZE, textSize.name()));
239 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
240 useWideViewPort = true; // use wide view port for either setting
242 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
244 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
246 defaultTextEncodingName =
247 p.getString(PREF_DEFAULT_TEXT_ENCODING,
248 defaultTextEncodingName);
251 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
252 // Debug menu items have precidence if the menu is visible
253 if (showDebugSettings) {
254 boolean small_screen = p.getBoolean("small_screen",
256 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
258 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
260 boolean normal_layout = p.getBoolean("normal_layout",
261 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
263 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
266 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
269 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
270 tracing = p.getBoolean("enable_tracing", tracing);
271 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
272 navDump = p.getBoolean("enable_nav_dump", navDump);
273 doFlick = p.getBoolean("enable_flick", doFlick);
274 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
275 mTabControl.getBrowserActivity().setBaseSearchUrl(
276 p.getString("search_url", ""));
281 public String getPluginsPath() {
285 public String getHomePage() {
289 public void setHomePage(Context context, String url) {
290 Editor ed = PreferenceManager.
291 getDefaultSharedPreferences(context).edit();
292 ed.putString(PREF_HOMEPAGE, url);
297 public boolean isLoginInitialized() {
298 return loginInitialized;
301 public void setLoginInitialized(Context context) {
302 loginInitialized = true;
303 Editor ed = PreferenceManager.
304 getDefaultSharedPreferences(context).edit();
305 ed.putBoolean("login_initialized", loginInitialized);
309 public WebSettings.TextSize getTextSize() {
313 public boolean openInBackground() {
314 return openInBackground;
317 public boolean showSecurityWarnings() {
318 return showSecurityWarnings;
321 public boolean isTracing() {
325 public boolean isLightTouch() {
329 public boolean isNavDump() {
333 public boolean doFlick() {
337 public boolean showDebugSettings() {
338 return showDebugSettings;
341 public void toggleDebugSettings() {
342 showDebugSettings = !showDebugSettings;
343 navDump = showDebugSettings;
348 * Add a WebSettings object to the list of observers that will be updated
349 * when update() is called.
351 * @param s A WebSettings object that is strictly tied to the life of a
354 public Observer addObserver(WebSettings s) {
355 Observer old = mWebSettingsToObservers.get(s);
357 super.deleteObserver(old);
359 Observer o = new Observer(s);
360 mWebSettingsToObservers.put(s, o);
361 super.addObserver(o);
366 * Delete the given WebSettings observer from the list of observers.
367 * @param s The WebSettings object to be deleted.
369 public void deleteObserver(WebSettings s) {
370 Observer o = mWebSettingsToObservers.get(s);
372 mWebSettingsToObservers.remove(s);
373 super.deleteObserver(o);
378 * Package level method for obtaining a single app instance of the
381 /*package*/ static BrowserSettings getInstance() {
382 if (sSingleton == null ) {
383 sSingleton = new BrowserSettings();
389 * Package level method for associating the BrowserSettings with TabControl
391 /* package */void setTabControl(TabControl tabControl) {
392 mTabControl = tabControl;
396 * Update all the observers of the object.
398 /*package*/ void update() {
403 /*package*/ void clearCache(Context context) {
404 WebIconDatabase.getInstance().removeAllIcons();
405 if (mTabControl != null) {
406 WebView current = mTabControl.getCurrentWebView();
407 if (current != null) {
408 current.clearCache(true);
413 /*package*/ void clearCookies(Context context) {
414 CookieManager.getInstance().removeAllCookie();
417 /* package */void clearHistory(Context context) {
418 ContentResolver resolver = context.getContentResolver();
419 Browser.clearHistory(resolver);
420 Browser.clearSearches(resolver);
423 /* package */ void clearFormData(Context context) {
424 WebViewDatabase.getInstance(context).clearFormData();
425 if (mTabControl != null) {
426 mTabControl.getCurrentTopWebView().clearFormData();
430 /*package*/ void clearPasswords(Context context) {
431 WebViewDatabase db = WebViewDatabase.getInstance(context);
432 db.clearUsernamePassword();
433 db.clearHttpAuthUsernamePassword();
436 /*package*/ void resetDefaultPreferences(Context context) {
437 SharedPreferences p =
438 PreferenceManager.getDefaultSharedPreferences(context);
439 p.edit().clear().commit();
440 PreferenceManager.setDefaultValues(context, R.xml.browser_preferences,
444 // Private constructor that does nothing.
445 private BrowserSettings() {