blob: 34cab941c53baa6c97a709255ea57caadf47123c [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
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070019import com.google.android.providers.GoogleSettings.Partner;
20
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.app.Activity;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.content.pm.ActivityInfo;
25import android.content.SharedPreferences;
26import android.content.SharedPreferences.Editor;
27import android.os.SystemProperties;
Nicolas Roard78a98e42009-05-11 13:34:17 +010028import android.preference.PreferenceActivity;
29import android.preference.PreferenceScreen;
30import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.view.WindowManager;
32import android.webkit.CacheManager;
33import android.webkit.CookieManager;
34import android.webkit.WebView;
35import android.webkit.WebViewDatabase;
36import android.webkit.WebIconDatabase;
37import android.webkit.WebSettings;
Nicolas Roard78a98e42009-05-11 13:34:17 +010038import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.preference.PreferenceManager;
40import android.provider.Browser;
41
42import java.util.HashMap;
43import java.util.Observable;
44
45/*
46 * Package level class for storing various WebView and Browser settings. To use
47 * this class:
48 * BrowserSettings s = BrowserSettings.getInstance();
49 * s.addObserver(webView.getSettings());
50 * s.loadFromDb(context); // Only needed on app startup
51 * s.javaScriptEnabled = true;
52 * ... // set any other settings
53 * s.update(); // this will update all the observers
54 *
55 * To remove an observer:
56 * s.deleteObserver(webView.getSettings());
57 */
58class BrowserSettings extends Observable {
59
Leon Scroggins9ac587d2009-04-20 12:53:46 -040060 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080061 // NOTE: these defaults need to be kept in sync with the XML
62 // until the performance of PreferenceManager.setDefaultValues()
63 // is improved.
64 private boolean loadsImagesAutomatically = true;
65 private boolean javaScriptEnabled = true;
66 private boolean pluginsEnabled = true;
67 private String pluginsPath; // default value set in loadFromDb().
68 private boolean javaScriptCanOpenWindowsAutomatically = false;
69 private boolean showSecurityWarnings = true;
70 private boolean rememberPasswords = true;
71 private boolean saveFormData = true;
72 private boolean openInBackground = false;
73 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070074 private String homeUrl = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -080075 private boolean loginInitialized = false;
76 private boolean autoFitPage = true;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -070077 private boolean landscapeOnly = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -080078 private boolean showDebugSettings = false;
Ben Murdoch092dd5d2009-04-22 12:34:12 +010079 private String databasePath; // default value set in loadFromDb()
80 private boolean databaseEnabled = true;
Nicolas Roard78a98e42009-05-11 13:34:17 +010081 private long webStorageDefaultQuota = 5 * 1024 * 1024;
Andrei Popescu5151ac12009-04-17 10:43:07 +010082 // The Browser always enables Application Caches.
83 private boolean appCacheEnabled = true;
84 private String appCachePath; // default value set in loadFromDb().
The Android Open Source Project0c908882009-03-03 19:32:16 -080085
Nicolas Roard78a98e42009-05-11 13:34:17 +010086 private final static String TAG = "BrowserSettings";
87
The Android Open Source Project0c908882009-03-03 19:32:16 -080088 // Development settings
89 public WebSettings.LayoutAlgorithm layoutAlgorithm =
90 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
91 private boolean useWideViewPort = true;
92 private int userAgent = 0;
93 private boolean tracing = false;
94 private boolean lightTouch = false;
95 private boolean navDump = false;
96 // Browser only settings
97 private boolean doFlick = false;
98
99 // Private preconfigured values
100 private static int minimumFontSize = 8;
101 private static int minimumLogicalFontSize = 8;
102 private static int defaultFontSize = 16;
103 private static int defaultFixedFontSize = 13;
104 private static WebSettings.TextSize textSize =
105 WebSettings.TextSize.NORMAL;
106
107 // Preference keys that are used outside this class
108 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
109 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
110 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
111 public final static String PREF_HOMEPAGE = "homepage";
112 public final static String PREF_CLEAR_FORM_DATA =
113 "privacy_clear_form_data";
114 public final static String PREF_CLEAR_PASSWORDS =
115 "privacy_clear_passwords";
Nicolas Roard78a98e42009-05-11 13:34:17 +0100116 public final static String PREF_CLEAR_DATABASES =
117 "webstorage_clear_databases";
118 public final static String PREF_CLEAR_ALL_DATA =
119 "webstorage_clear_all_data";
120 public final static String PREF_MANAGE_QUOTA =
121 "webstorage_manage_quota";
122 public final static String PREF_DEFAULT_QUOTA =
123 "webstorage_default_quota";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124 public final static String PREF_EXTRAS_RESET_DEFAULTS =
125 "reset_default_preferences";
126 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
127 public final static String PREF_GEARS_SETTINGS = "gears_settings";
Nicolas Roard78a98e42009-05-11 13:34:17 +0100128 public final static String PREF_WEBSTORAGE_SETTINGS = "webstorage_manage_databases";
Nicolas Roard6f480422009-05-12 16:31:33 +0100129 public final static String PREF_WEBSTORAGE_CLEAR_ALL = "webstorage_clear_databases";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800130 public final static String PREF_TEXT_SIZE = "text_size";
131 public final static String PREF_DEFAULT_TEXT_ENCODING =
132 "default_text_encoding";
133
134 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
135 "U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/525.18 (KHTML, " +
136 "like Gecko) Version/3.1.2 Safari/525.20.1";
137
138 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
139 "CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 " +
140 "(KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20";
141
142 // Value to truncate strings when adding them to a TextView within
143 // a ListView
144 public final static int MAX_TEXTVIEW_LEN = 80;
145
146 private TabControl mTabControl;
147
148 // Single instance of the BrowserSettings for use in the Browser app.
149 private static BrowserSettings sSingleton;
150
151 // Private map of WebSettings to Observer objects used when deleting an
152 // observer.
153 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
154 new HashMap<WebSettings,Observer>();
155
156 /*
157 * An observer wrapper for updating a WebSettings object with the new
158 * settings after a call to BrowserSettings.update().
159 */
160 static class Observer implements java.util.Observer {
161 // Private WebSettings object that will be updated.
162 private WebSettings mSettings;
163
164 Observer(WebSettings w) {
165 mSettings = w;
166 }
167
168 public void update(Observable o, Object arg) {
169 BrowserSettings b = (BrowserSettings)o;
170 WebSettings s = mSettings;
171
172 s.setLayoutAlgorithm(b.layoutAlgorithm);
173 if (b.userAgent == 0) {
174 // use the default ua string
175 s.setUserAgentString(null);
176 } else if (b.userAgent == 1) {
177 s.setUserAgentString(DESKTOP_USERAGENT);
178 } else if (b.userAgent == 2) {
179 s.setUserAgentString(IPHONE_USERAGENT);
180 }
181 s.setUseWideViewPort(b.useWideViewPort);
182 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
183 s.setJavaScriptEnabled(b.javaScriptEnabled);
184 s.setPluginsEnabled(b.pluginsEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800185 s.setJavaScriptCanOpenWindowsAutomatically(
186 b.javaScriptCanOpenWindowsAutomatically);
187 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
188 s.setMinimumFontSize(b.minimumFontSize);
189 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
190 s.setDefaultFontSize(b.defaultFontSize);
191 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
192 s.setNavDump(b.navDump);
193 s.setTextSize(b.textSize);
194 s.setLightTouchEnabled(b.lightTouch);
195 s.setSaveFormData(b.saveFormData);
196 s.setSavePassword(b.rememberPasswords);
197
198 // WebView inside Browser doesn't want initial focus to be set.
199 s.setNeedInitialFocus(false);
200 // Browser supports multiple windows
201 s.setSupportMultipleWindows(true);
202 // Turn off file access
203 s.setAllowFileAccess(false);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100204
205 s.setDatabasePath(b.databasePath);
206 s.setDatabaseEnabled(b.databaseEnabled);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100207 s.setWebStorageDefaultQuota(b.webStorageDefaultQuota);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100208
Andrei Popescu5151ac12009-04-17 10:43:07 +0100209 // Turn on Application Caches.
210 s.setAppCachePath(b.appCachePath);
211 s.setAppCacheEnabled(b.appCacheEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800212 }
213 }
214
215 /**
216 * Load settings from the browser app's database.
217 * NOTE: Strings used for the preferences must match those specified
218 * in the browser_preferences.xml
219 * @param ctx A Context object used to query the browser's settings
220 * database. If the database exists, the saved settings will be
221 * stored in this BrowserSettings object. This will update all
222 * observers of this object.
223 */
224 public void loadFromDb(Context ctx) {
225 SharedPreferences p =
226 PreferenceManager.getDefaultSharedPreferences(ctx);
227
228 // Set the default value for the plugins path to the application's
229 // local directory.
230 pluginsPath = ctx.getDir("plugins", 0).getPath();
Andrei Popescu5151ac12009-04-17 10:43:07 +0100231 // Set the default value for the Application Caches path.
232 appCachePath = ctx.getDir("appcache", 0).getPath();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100233 // Set the default value for the Database path.
234 databasePath = ctx.getDir("databases", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800235
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700236 homeUrl = getFactoryResetHomeUrl(ctx);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700237
The Android Open Source Project0c908882009-03-03 19:32:16 -0800238 // Load the defaults from the xml
239 // This call is TOO SLOW, need to manually keep the defaults
240 // in sync
241 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
242 syncSharedPreferences(p);
243 }
244
245 /* package */ void syncSharedPreferences(SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700246
The Android Open Source Project0c908882009-03-03 19:32:16 -0800247 homeUrl =
248 p.getString(PREF_HOMEPAGE, homeUrl);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700249
The Android Open Source Project0c908882009-03-03 19:32:16 -0800250 loadsImagesAutomatically = p.getBoolean("load_images",
251 loadsImagesAutomatically);
252 javaScriptEnabled = p.getBoolean("enable_javascript",
253 javaScriptEnabled);
254 pluginsEnabled = p.getBoolean("enable_plugins",
255 pluginsEnabled);
256 pluginsPath = p.getString("plugins_path", pluginsPath);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100257 databasePath = p.getString("database_path", databasePath);
258 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100259 webStorageDefaultQuota = Long.parseLong(p.getString(PREF_DEFAULT_QUOTA,
260 String.valueOf(webStorageDefaultQuota)));
Andrei Popescu5151ac12009-04-17 10:43:07 +0100261 appCacheEnabled = p.getBoolean("enable_appcache",
262 appCacheEnabled);
263 appCachePath = p.getString("appcache_path", appCachePath);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800264 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
265 "block_popup_windows",
266 !javaScriptCanOpenWindowsAutomatically);
267 showSecurityWarnings = p.getBoolean("show_security_warnings",
268 showSecurityWarnings);
269 rememberPasswords = p.getBoolean("remember_passwords",
270 rememberPasswords);
271 saveFormData = p.getBoolean("save_formdata",
272 saveFormData);
273 boolean accept_cookies = p.getBoolean("accept_cookies",
274 CookieManager.getInstance().acceptCookie());
275 CookieManager.getInstance().setAcceptCookie(accept_cookies);
276 openInBackground = p.getBoolean("open_in_background", openInBackground);
277 loginInitialized = p.getBoolean("login_initialized", loginInitialized);
278 textSize = WebSettings.TextSize.valueOf(
279 p.getString(PREF_TEXT_SIZE, textSize.name()));
280 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700281 boolean landscapeOnlyTemp =
282 p.getBoolean("landscape_only", landscapeOnly);
283 if (landscapeOnlyTemp != landscapeOnly) {
284 landscapeOnly = landscapeOnlyTemp;
285 mTabControl.getBrowserActivity().setRequestedOrientation(
286 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
287 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
288 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800289 useWideViewPort = true; // use wide view port for either setting
290 if (autoFitPage) {
291 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
292 } else {
293 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
294 }
295 defaultTextEncodingName =
296 p.getString(PREF_DEFAULT_TEXT_ENCODING,
297 defaultTextEncodingName);
298
299 showDebugSettings =
300 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
301 // Debug menu items have precidence if the menu is visible
302 if (showDebugSettings) {
303 boolean small_screen = p.getBoolean("small_screen",
304 layoutAlgorithm ==
305 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
306 if (small_screen) {
307 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
308 } else {
309 boolean normal_layout = p.getBoolean("normal_layout",
310 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
311 if (normal_layout) {
312 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
313 } else {
314 layoutAlgorithm =
315 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
316 }
317 }
318 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
319 tracing = p.getBoolean("enable_tracing", tracing);
320 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
321 navDump = p.getBoolean("enable_nav_dump", navDump);
322 doFlick = p.getBoolean("enable_flick", doFlick);
323 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
324 mTabControl.getBrowserActivity().setBaseSearchUrl(
325 p.getString("search_url", ""));
326 }
327 update();
328 }
329
330 public String getPluginsPath() {
331 return pluginsPath;
332 }
333
334 public String getHomePage() {
335 return homeUrl;
336 }
337
338 public void setHomePage(Context context, String url) {
339 Editor ed = PreferenceManager.
340 getDefaultSharedPreferences(context).edit();
341 ed.putString(PREF_HOMEPAGE, url);
342 ed.commit();
343 homeUrl = url;
344 }
345
346 public boolean isLoginInitialized() {
347 return loginInitialized;
348 }
349
350 public void setLoginInitialized(Context context) {
351 loginInitialized = true;
352 Editor ed = PreferenceManager.
353 getDefaultSharedPreferences(context).edit();
354 ed.putBoolean("login_initialized", loginInitialized);
355 ed.commit();
356 }
357
358 public WebSettings.TextSize getTextSize() {
359 return textSize;
360 }
361
362 public boolean openInBackground() {
363 return openInBackground;
364 }
365
366 public boolean showSecurityWarnings() {
367 return showSecurityWarnings;
368 }
369
370 public boolean isTracing() {
371 return tracing;
372 }
373
374 public boolean isLightTouch() {
375 return lightTouch;
376 }
377
378 public boolean isNavDump() {
379 return navDump;
380 }
381
382 public boolean doFlick() {
383 return doFlick;
384 }
385
386 public boolean showDebugSettings() {
387 return showDebugSettings;
388 }
389
390 public void toggleDebugSettings() {
391 showDebugSettings = !showDebugSettings;
392 navDump = showDebugSettings;
393 update();
394 }
395
396 /**
397 * Add a WebSettings object to the list of observers that will be updated
398 * when update() is called.
399 *
400 * @param s A WebSettings object that is strictly tied to the life of a
401 * WebView.
402 */
403 public Observer addObserver(WebSettings s) {
404 Observer old = mWebSettingsToObservers.get(s);
405 if (old != null) {
406 super.deleteObserver(old);
407 }
408 Observer o = new Observer(s);
409 mWebSettingsToObservers.put(s, o);
410 super.addObserver(o);
411 return o;
412 }
413
414 /**
415 * Delete the given WebSettings observer from the list of observers.
416 * @param s The WebSettings object to be deleted.
417 */
418 public void deleteObserver(WebSettings s) {
419 Observer o = mWebSettingsToObservers.get(s);
420 if (o != null) {
421 mWebSettingsToObservers.remove(s);
422 super.deleteObserver(o);
423 }
424 }
425
426 /*
427 * Package level method for obtaining a single app instance of the
428 * BrowserSettings.
429 */
430 /*package*/ static BrowserSettings getInstance() {
431 if (sSingleton == null ) {
432 sSingleton = new BrowserSettings();
433 }
434 return sSingleton;
435 }
436
437 /*
438 * Package level method for associating the BrowserSettings with TabControl
439 */
440 /* package */void setTabControl(TabControl tabControl) {
441 mTabControl = tabControl;
442 }
443
444 /*
445 * Update all the observers of the object.
446 */
447 /*package*/ void update() {
448 setChanged();
449 notifyObservers();
450 }
451
452 /*package*/ void clearCache(Context context) {
453 WebIconDatabase.getInstance().removeAllIcons();
454 if (mTabControl != null) {
455 WebView current = mTabControl.getCurrentWebView();
456 if (current != null) {
457 current.clearCache(true);
458 }
459 }
460 }
461
462 /*package*/ void clearCookies(Context context) {
463 CookieManager.getInstance().removeAllCookie();
464 }
465
466 /* package */void clearHistory(Context context) {
467 ContentResolver resolver = context.getContentResolver();
468 Browser.clearHistory(resolver);
469 Browser.clearSearches(resolver);
470 }
471
472 /* package */ void clearFormData(Context context) {
473 WebViewDatabase.getInstance(context).clearFormData();
474 if (mTabControl != null) {
475 mTabControl.getCurrentTopWebView().clearFormData();
476 }
477 }
478
479 /*package*/ void clearPasswords(Context context) {
480 WebViewDatabase db = WebViewDatabase.getInstance(context);
481 db.clearUsernamePassword();
482 db.clearHttpAuthUsernamePassword();
483 }
484
Nicolas Roard78a98e42009-05-11 13:34:17 +0100485 /*package*/ void clearDatabases(Context context) {
486 WebStorage.getInstance().deleteAllDatabases();
487 // Remove all listed databases from the preferences
488 PreferenceActivity activity = (PreferenceActivity) context;
489 PreferenceScreen screen = (PreferenceScreen)
490 activity.findPreference(BrowserSettings.PREF_WEBSTORAGE_SETTINGS);
491 screen.removeAll();
Nicolas Roard6f480422009-05-12 16:31:33 +0100492 screen.setEnabled(false);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100493 }
494
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400495 /*package*/ void resetDefaultPreferences(Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800496 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400497 PreferenceManager.getDefaultSharedPreferences(ctx);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800498 p.edit().clear().commit();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400499 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800500 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700501 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700502 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700503 }
504
505 private String getFactoryResetHomeUrl(Context context) {
506 String url = context.getResources().getString(R.string.homepage_base);
507 if (url.indexOf("{CID}") != -1) {
508 url = url.replace("{CID}", Partner.getString(context
Ramanan Rajeswaran9768ac52009-06-03 19:34:58 -0700509 .getContentResolver(), Partner.CLIENT_ID, "android-google"));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700510 }
511 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800512 }
513
514 // Private constructor that does nothing.
515 private BrowserSettings() {
516 }
517}