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