blob: 5276ef951ef7c543ed283a129335ade1faebd2ee [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().
Ben Murdoch734a0662009-06-02 19:11:52 +010085 private boolean domStorageEnabled = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -080086
Nicolas Roard78a98e42009-05-11 13:34:17 +010087 private final static String TAG = "BrowserSettings";
88
The Android Open Source Project0c908882009-03-03 19:32:16 -080089 // Development settings
90 public WebSettings.LayoutAlgorithm layoutAlgorithm =
91 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
92 private boolean useWideViewPort = true;
93 private int userAgent = 0;
94 private boolean tracing = false;
95 private boolean lightTouch = false;
96 private boolean navDump = false;
97 // Browser only settings
98 private boolean doFlick = false;
99
100 // Private preconfigured values
101 private static int minimumFontSize = 8;
102 private static int minimumLogicalFontSize = 8;
103 private static int defaultFontSize = 16;
104 private static int defaultFixedFontSize = 13;
105 private static WebSettings.TextSize textSize =
106 WebSettings.TextSize.NORMAL;
107
108 // Preference keys that are used outside this class
109 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
110 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
111 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
112 public final static String PREF_HOMEPAGE = "homepage";
113 public final static String PREF_CLEAR_FORM_DATA =
114 "privacy_clear_form_data";
115 public final static String PREF_CLEAR_PASSWORDS =
116 "privacy_clear_passwords";
Nicolas Roard78a98e42009-05-11 13:34:17 +0100117 public final static String PREF_CLEAR_DATABASES =
118 "webstorage_clear_databases";
119 public final static String PREF_CLEAR_ALL_DATA =
120 "webstorage_clear_all_data";
121 public final static String PREF_MANAGE_QUOTA =
122 "webstorage_manage_quota";
123 public final static String PREF_DEFAULT_QUOTA =
124 "webstorage_default_quota";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800125 public final static String PREF_EXTRAS_RESET_DEFAULTS =
126 "reset_default_preferences";
127 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
128 public final static String PREF_GEARS_SETTINGS = "gears_settings";
Nicolas Roard78a98e42009-05-11 13:34:17 +0100129 public final static String PREF_WEBSTORAGE_SETTINGS = "webstorage_manage_databases";
Nicolas Roard6f480422009-05-12 16:31:33 +0100130 public final static String PREF_WEBSTORAGE_CLEAR_ALL = "webstorage_clear_databases";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800131 public final static String PREF_TEXT_SIZE = "text_size";
132 public final static String PREF_DEFAULT_TEXT_ENCODING =
133 "default_text_encoding";
134
135 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
136 "U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/525.18 (KHTML, " +
137 "like Gecko) Version/3.1.2 Safari/525.20.1";
138
139 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
140 "CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 " +
141 "(KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20";
142
143 // Value to truncate strings when adding them to a TextView within
144 // a ListView
145 public final static int MAX_TEXTVIEW_LEN = 80;
146
147 private TabControl mTabControl;
148
149 // Single instance of the BrowserSettings for use in the Browser app.
150 private static BrowserSettings sSingleton;
151
152 // Private map of WebSettings to Observer objects used when deleting an
153 // observer.
154 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
155 new HashMap<WebSettings,Observer>();
156
157 /*
158 * An observer wrapper for updating a WebSettings object with the new
159 * settings after a call to BrowserSettings.update().
160 */
161 static class Observer implements java.util.Observer {
162 // Private WebSettings object that will be updated.
163 private WebSettings mSettings;
164
165 Observer(WebSettings w) {
166 mSettings = w;
167 }
168
169 public void update(Observable o, Object arg) {
170 BrowserSettings b = (BrowserSettings)o;
171 WebSettings s = mSettings;
172
173 s.setLayoutAlgorithm(b.layoutAlgorithm);
174 if (b.userAgent == 0) {
175 // use the default ua string
176 s.setUserAgentString(null);
177 } else if (b.userAgent == 1) {
178 s.setUserAgentString(DESKTOP_USERAGENT);
179 } else if (b.userAgent == 2) {
180 s.setUserAgentString(IPHONE_USERAGENT);
181 }
182 s.setUseWideViewPort(b.useWideViewPort);
183 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
184 s.setJavaScriptEnabled(b.javaScriptEnabled);
185 s.setPluginsEnabled(b.pluginsEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186 s.setJavaScriptCanOpenWindowsAutomatically(
187 b.javaScriptCanOpenWindowsAutomatically);
188 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
189 s.setMinimumFontSize(b.minimumFontSize);
190 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
191 s.setDefaultFontSize(b.defaultFontSize);
192 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
193 s.setNavDump(b.navDump);
194 s.setTextSize(b.textSize);
195 s.setLightTouchEnabled(b.lightTouch);
196 s.setSaveFormData(b.saveFormData);
197 s.setSavePassword(b.rememberPasswords);
198
199 // WebView inside Browser doesn't want initial focus to be set.
200 s.setNeedInitialFocus(false);
201 // Browser supports multiple windows
202 s.setSupportMultipleWindows(true);
203 // Turn off file access
204 s.setAllowFileAccess(false);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100205
206 s.setDatabasePath(b.databasePath);
207 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100208 s.setDomStorageEnabled(b.domStorageEnabled);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100209 s.setWebStorageDefaultQuota(b.webStorageDefaultQuota);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100210
Andrei Popescu5151ac12009-04-17 10:43:07 +0100211 // Turn on Application Caches.
212 s.setAppCachePath(b.appCachePath);
213 s.setAppCacheEnabled(b.appCacheEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800214 }
215 }
216
217 /**
218 * Load settings from the browser app's database.
219 * NOTE: Strings used for the preferences must match those specified
220 * in the browser_preferences.xml
221 * @param ctx A Context object used to query the browser's settings
222 * database. If the database exists, the saved settings will be
223 * stored in this BrowserSettings object. This will update all
224 * observers of this object.
225 */
226 public void loadFromDb(Context ctx) {
227 SharedPreferences p =
228 PreferenceManager.getDefaultSharedPreferences(ctx);
229
230 // Set the default value for the plugins path to the application's
231 // local directory.
232 pluginsPath = ctx.getDir("plugins", 0).getPath();
Andrei Popescu5151ac12009-04-17 10:43:07 +0100233 // Set the default value for the Application Caches path.
234 appCachePath = ctx.getDir("appcache", 0).getPath();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100235 // Set the default value for the Database path.
236 databasePath = ctx.getDir("databases", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800237
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700238 homeUrl = getFactoryResetHomeUrl(ctx);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700239
The Android Open Source Project0c908882009-03-03 19:32:16 -0800240 // Load the defaults from the xml
241 // This call is TOO SLOW, need to manually keep the defaults
242 // in sync
243 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
244 syncSharedPreferences(p);
245 }
246
247 /* package */ void syncSharedPreferences(SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700248
The Android Open Source Project0c908882009-03-03 19:32:16 -0800249 homeUrl =
250 p.getString(PREF_HOMEPAGE, homeUrl);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700251
The Android Open Source Project0c908882009-03-03 19:32:16 -0800252 loadsImagesAutomatically = p.getBoolean("load_images",
253 loadsImagesAutomatically);
254 javaScriptEnabled = p.getBoolean("enable_javascript",
255 javaScriptEnabled);
256 pluginsEnabled = p.getBoolean("enable_plugins",
257 pluginsEnabled);
258 pluginsPath = p.getString("plugins_path", pluginsPath);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100259 databasePath = p.getString("database_path", databasePath);
260 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100261 webStorageDefaultQuota = Long.parseLong(p.getString(PREF_DEFAULT_QUOTA,
262 String.valueOf(webStorageDefaultQuota)));
Andrei Popescu5151ac12009-04-17 10:43:07 +0100263 appCacheEnabled = p.getBoolean("enable_appcache",
Ben Murdoch734a0662009-06-02 19:11:52 +0100264 appCacheEnabled);
265 domStorageEnabled = p.getBoolean("enable_domstorage",
266 domStorageEnabled);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100267 appCachePath = p.getString("appcache_path", appCachePath);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800268 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
269 "block_popup_windows",
270 !javaScriptCanOpenWindowsAutomatically);
271 showSecurityWarnings = p.getBoolean("show_security_warnings",
272 showSecurityWarnings);
273 rememberPasswords = p.getBoolean("remember_passwords",
274 rememberPasswords);
275 saveFormData = p.getBoolean("save_formdata",
276 saveFormData);
277 boolean accept_cookies = p.getBoolean("accept_cookies",
278 CookieManager.getInstance().acceptCookie());
279 CookieManager.getInstance().setAcceptCookie(accept_cookies);
280 openInBackground = p.getBoolean("open_in_background", openInBackground);
281 loginInitialized = p.getBoolean("login_initialized", loginInitialized);
282 textSize = WebSettings.TextSize.valueOf(
283 p.getString(PREF_TEXT_SIZE, textSize.name()));
284 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700285 boolean landscapeOnlyTemp =
286 p.getBoolean("landscape_only", landscapeOnly);
287 if (landscapeOnlyTemp != landscapeOnly) {
288 landscapeOnly = landscapeOnlyTemp;
289 mTabControl.getBrowserActivity().setRequestedOrientation(
290 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
291 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
292 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800293 useWideViewPort = true; // use wide view port for either setting
294 if (autoFitPage) {
295 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
296 } else {
297 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
298 }
299 defaultTextEncodingName =
300 p.getString(PREF_DEFAULT_TEXT_ENCODING,
301 defaultTextEncodingName);
302
303 showDebugSettings =
304 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
305 // Debug menu items have precidence if the menu is visible
306 if (showDebugSettings) {
307 boolean small_screen = p.getBoolean("small_screen",
308 layoutAlgorithm ==
309 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
310 if (small_screen) {
311 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
312 } else {
313 boolean normal_layout = p.getBoolean("normal_layout",
314 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
315 if (normal_layout) {
316 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
317 } else {
318 layoutAlgorithm =
319 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
320 }
321 }
322 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
323 tracing = p.getBoolean("enable_tracing", tracing);
324 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
325 navDump = p.getBoolean("enable_nav_dump", navDump);
326 doFlick = p.getBoolean("enable_flick", doFlick);
327 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
328 mTabControl.getBrowserActivity().setBaseSearchUrl(
329 p.getString("search_url", ""));
330 }
331 update();
332 }
333
334 public String getPluginsPath() {
335 return pluginsPath;
336 }
337
338 public String getHomePage() {
339 return homeUrl;
340 }
341
342 public void setHomePage(Context context, String url) {
343 Editor ed = PreferenceManager.
344 getDefaultSharedPreferences(context).edit();
345 ed.putString(PREF_HOMEPAGE, url);
346 ed.commit();
347 homeUrl = url;
348 }
349
350 public boolean isLoginInitialized() {
351 return loginInitialized;
352 }
353
354 public void setLoginInitialized(Context context) {
355 loginInitialized = true;
356 Editor ed = PreferenceManager.
357 getDefaultSharedPreferences(context).edit();
358 ed.putBoolean("login_initialized", loginInitialized);
359 ed.commit();
360 }
361
362 public WebSettings.TextSize getTextSize() {
363 return textSize;
364 }
365
366 public boolean openInBackground() {
367 return openInBackground;
368 }
369
370 public boolean showSecurityWarnings() {
371 return showSecurityWarnings;
372 }
373
374 public boolean isTracing() {
375 return tracing;
376 }
377
378 public boolean isLightTouch() {
379 return lightTouch;
380 }
381
382 public boolean isNavDump() {
383 return navDump;
384 }
385
386 public boolean doFlick() {
387 return doFlick;
388 }
389
390 public boolean showDebugSettings() {
391 return showDebugSettings;
392 }
393
394 public void toggleDebugSettings() {
395 showDebugSettings = !showDebugSettings;
396 navDump = showDebugSettings;
397 update();
398 }
399
400 /**
401 * Add a WebSettings object to the list of observers that will be updated
402 * when update() is called.
403 *
404 * @param s A WebSettings object that is strictly tied to the life of a
405 * WebView.
406 */
407 public Observer addObserver(WebSettings s) {
408 Observer old = mWebSettingsToObservers.get(s);
409 if (old != null) {
410 super.deleteObserver(old);
411 }
412 Observer o = new Observer(s);
413 mWebSettingsToObservers.put(s, o);
414 super.addObserver(o);
415 return o;
416 }
417
418 /**
419 * Delete the given WebSettings observer from the list of observers.
420 * @param s The WebSettings object to be deleted.
421 */
422 public void deleteObserver(WebSettings s) {
423 Observer o = mWebSettingsToObservers.get(s);
424 if (o != null) {
425 mWebSettingsToObservers.remove(s);
426 super.deleteObserver(o);
427 }
428 }
429
430 /*
431 * Package level method for obtaining a single app instance of the
432 * BrowserSettings.
433 */
434 /*package*/ static BrowserSettings getInstance() {
435 if (sSingleton == null ) {
436 sSingleton = new BrowserSettings();
437 }
438 return sSingleton;
439 }
440
441 /*
442 * Package level method for associating the BrowserSettings with TabControl
443 */
444 /* package */void setTabControl(TabControl tabControl) {
445 mTabControl = tabControl;
446 }
447
448 /*
449 * Update all the observers of the object.
450 */
451 /*package*/ void update() {
452 setChanged();
453 notifyObservers();
454 }
455
456 /*package*/ void clearCache(Context context) {
457 WebIconDatabase.getInstance().removeAllIcons();
458 if (mTabControl != null) {
459 WebView current = mTabControl.getCurrentWebView();
460 if (current != null) {
461 current.clearCache(true);
462 }
463 }
464 }
465
466 /*package*/ void clearCookies(Context context) {
467 CookieManager.getInstance().removeAllCookie();
468 }
469
470 /* package */void clearHistory(Context context) {
471 ContentResolver resolver = context.getContentResolver();
472 Browser.clearHistory(resolver);
473 Browser.clearSearches(resolver);
474 }
475
476 /* package */ void clearFormData(Context context) {
477 WebViewDatabase.getInstance(context).clearFormData();
478 if (mTabControl != null) {
479 mTabControl.getCurrentTopWebView().clearFormData();
480 }
481 }
482
483 /*package*/ void clearPasswords(Context context) {
484 WebViewDatabase db = WebViewDatabase.getInstance(context);
485 db.clearUsernamePassword();
486 db.clearHttpAuthUsernamePassword();
487 }
488
Nicolas Roard78a98e42009-05-11 13:34:17 +0100489 /*package*/ void clearDatabases(Context context) {
490 WebStorage.getInstance().deleteAllDatabases();
491 // Remove all listed databases from the preferences
492 PreferenceActivity activity = (PreferenceActivity) context;
493 PreferenceScreen screen = (PreferenceScreen)
494 activity.findPreference(BrowserSettings.PREF_WEBSTORAGE_SETTINGS);
495 screen.removeAll();
Nicolas Roard6f480422009-05-12 16:31:33 +0100496 screen.setEnabled(false);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100497 }
498
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400499 /*package*/ void resetDefaultPreferences(Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800500 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400501 PreferenceManager.getDefaultSharedPreferences(ctx);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800502 p.edit().clear().commit();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400503 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800504 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700505 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700506 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700507 }
508
509 private String getFactoryResetHomeUrl(Context context) {
510 String url = context.getResources().getString(R.string.homepage_base);
511 if (url.indexOf("{CID}") != -1) {
512 url = url.replace("{CID}", Partner.getString(context
Ramanan Rajeswaran9768ac52009-06-03 19:34:58 -0700513 .getContentResolver(), Partner.CLIENT_ID, "android-google"));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700514 }
515 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800516 }
517
518 // Private constructor that does nothing.
519 private BrowserSettings() {
520 }
521}