blob: 2d2d39b51bc1c368b435f2bd47f3ee0a0ab288bf [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_DEFAULT_QUOTA =
118 "webstorage_default_quota";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119 public final static String PREF_EXTRAS_RESET_DEFAULTS =
120 "reset_default_preferences";
121 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
122 public final static String PREF_GEARS_SETTINGS = "gears_settings";
Nicolas Roarde46990e2009-06-19 16:27:49 +0100123 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124 public final static String PREF_TEXT_SIZE = "text_size";
125 public final static String PREF_DEFAULT_TEXT_ENCODING =
126 "default_text_encoding";
127
128 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
129 "U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/525.18 (KHTML, " +
130 "like Gecko) Version/3.1.2 Safari/525.20.1";
131
132 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
133 "CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 " +
134 "(KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20";
135
136 // Value to truncate strings when adding them to a TextView within
137 // a ListView
138 public final static int MAX_TEXTVIEW_LEN = 80;
139
140 private TabControl mTabControl;
141
142 // Single instance of the BrowserSettings for use in the Browser app.
143 private static BrowserSettings sSingleton;
144
145 // Private map of WebSettings to Observer objects used when deleting an
146 // observer.
147 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
148 new HashMap<WebSettings,Observer>();
149
150 /*
151 * An observer wrapper for updating a WebSettings object with the new
152 * settings after a call to BrowserSettings.update().
153 */
154 static class Observer implements java.util.Observer {
155 // Private WebSettings object that will be updated.
156 private WebSettings mSettings;
157
158 Observer(WebSettings w) {
159 mSettings = w;
160 }
161
162 public void update(Observable o, Object arg) {
163 BrowserSettings b = (BrowserSettings)o;
164 WebSettings s = mSettings;
165
166 s.setLayoutAlgorithm(b.layoutAlgorithm);
167 if (b.userAgent == 0) {
168 // use the default ua string
169 s.setUserAgentString(null);
170 } else if (b.userAgent == 1) {
171 s.setUserAgentString(DESKTOP_USERAGENT);
172 } else if (b.userAgent == 2) {
173 s.setUserAgentString(IPHONE_USERAGENT);
174 }
175 s.setUseWideViewPort(b.useWideViewPort);
176 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
177 s.setJavaScriptEnabled(b.javaScriptEnabled);
178 s.setPluginsEnabled(b.pluginsEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800179 s.setJavaScriptCanOpenWindowsAutomatically(
180 b.javaScriptCanOpenWindowsAutomatically);
181 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
182 s.setMinimumFontSize(b.minimumFontSize);
183 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
184 s.setDefaultFontSize(b.defaultFontSize);
185 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
186 s.setNavDump(b.navDump);
187 s.setTextSize(b.textSize);
188 s.setLightTouchEnabled(b.lightTouch);
189 s.setSaveFormData(b.saveFormData);
190 s.setSavePassword(b.rememberPasswords);
191
192 // WebView inside Browser doesn't want initial focus to be set.
193 s.setNeedInitialFocus(false);
194 // Browser supports multiple windows
195 s.setSupportMultipleWindows(true);
196 // Turn off file access
197 s.setAllowFileAccess(false);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100198
199 s.setDatabasePath(b.databasePath);
200 s.setDatabaseEnabled(b.databaseEnabled);
Ben Murdoch734a0662009-06-02 19:11:52 +0100201 s.setDomStorageEnabled(b.domStorageEnabled);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100202 s.setWebStorageDefaultQuota(b.webStorageDefaultQuota);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100203
Andrei Popescu5151ac12009-04-17 10:43:07 +0100204 // Turn on Application Caches.
205 s.setAppCachePath(b.appCachePath);
206 s.setAppCacheEnabled(b.appCacheEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800207 }
208 }
209
210 /**
211 * Load settings from the browser app's database.
212 * NOTE: Strings used for the preferences must match those specified
213 * in the browser_preferences.xml
214 * @param ctx A Context object used to query the browser's settings
215 * database. If the database exists, the saved settings will be
216 * stored in this BrowserSettings object. This will update all
217 * observers of this object.
218 */
219 public void loadFromDb(Context ctx) {
220 SharedPreferences p =
221 PreferenceManager.getDefaultSharedPreferences(ctx);
222
223 // Set the default value for the plugins path to the application's
224 // local directory.
225 pluginsPath = ctx.getDir("plugins", 0).getPath();
Andrei Popescu5151ac12009-04-17 10:43:07 +0100226 // Set the default value for the Application Caches path.
227 appCachePath = ctx.getDir("appcache", 0).getPath();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100228 // Set the default value for the Database path.
229 databasePath = ctx.getDir("databases", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800230
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700231 homeUrl = getFactoryResetHomeUrl(ctx);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700232
The Android Open Source Project0c908882009-03-03 19:32:16 -0800233 // Load the defaults from the xml
234 // This call is TOO SLOW, need to manually keep the defaults
235 // in sync
236 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
237 syncSharedPreferences(p);
238 }
239
240 /* package */ void syncSharedPreferences(SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700241
The Android Open Source Project0c908882009-03-03 19:32:16 -0800242 homeUrl =
243 p.getString(PREF_HOMEPAGE, homeUrl);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700244
The Android Open Source Project0c908882009-03-03 19:32:16 -0800245 loadsImagesAutomatically = p.getBoolean("load_images",
246 loadsImagesAutomatically);
247 javaScriptEnabled = p.getBoolean("enable_javascript",
248 javaScriptEnabled);
249 pluginsEnabled = p.getBoolean("enable_plugins",
250 pluginsEnabled);
251 pluginsPath = p.getString("plugins_path", pluginsPath);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100252 databasePath = p.getString("database_path", databasePath);
253 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100254 webStorageDefaultQuota = Long.parseLong(p.getString(PREF_DEFAULT_QUOTA,
255 String.valueOf(webStorageDefaultQuota)));
Andrei Popescu5151ac12009-04-17 10:43:07 +0100256 appCacheEnabled = p.getBoolean("enable_appcache",
Ben Murdoch734a0662009-06-02 19:11:52 +0100257 appCacheEnabled);
258 domStorageEnabled = p.getBoolean("enable_domstorage",
259 domStorageEnabled);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100260 appCachePath = p.getString("appcache_path", appCachePath);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800261 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
262 "block_popup_windows",
263 !javaScriptCanOpenWindowsAutomatically);
264 showSecurityWarnings = p.getBoolean("show_security_warnings",
265 showSecurityWarnings);
266 rememberPasswords = p.getBoolean("remember_passwords",
267 rememberPasswords);
268 saveFormData = p.getBoolean("save_formdata",
269 saveFormData);
270 boolean accept_cookies = p.getBoolean("accept_cookies",
271 CookieManager.getInstance().acceptCookie());
272 CookieManager.getInstance().setAcceptCookie(accept_cookies);
273 openInBackground = p.getBoolean("open_in_background", openInBackground);
274 loginInitialized = p.getBoolean("login_initialized", loginInitialized);
275 textSize = WebSettings.TextSize.valueOf(
276 p.getString(PREF_TEXT_SIZE, textSize.name()));
277 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700278 boolean landscapeOnlyTemp =
279 p.getBoolean("landscape_only", landscapeOnly);
280 if (landscapeOnlyTemp != landscapeOnly) {
281 landscapeOnly = landscapeOnlyTemp;
282 mTabControl.getBrowserActivity().setRequestedOrientation(
283 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
284 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
285 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800286 useWideViewPort = true; // use wide view port for either setting
287 if (autoFitPage) {
288 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
289 } else {
290 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
291 }
292 defaultTextEncodingName =
293 p.getString(PREF_DEFAULT_TEXT_ENCODING,
294 defaultTextEncodingName);
295
296 showDebugSettings =
297 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
298 // Debug menu items have precidence if the menu is visible
299 if (showDebugSettings) {
300 boolean small_screen = p.getBoolean("small_screen",
301 layoutAlgorithm ==
302 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
303 if (small_screen) {
304 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
305 } else {
306 boolean normal_layout = p.getBoolean("normal_layout",
307 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
308 if (normal_layout) {
309 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
310 } else {
311 layoutAlgorithm =
312 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
313 }
314 }
315 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
316 tracing = p.getBoolean("enable_tracing", tracing);
317 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
318 navDump = p.getBoolean("enable_nav_dump", navDump);
319 doFlick = p.getBoolean("enable_flick", doFlick);
320 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
321 mTabControl.getBrowserActivity().setBaseSearchUrl(
322 p.getString("search_url", ""));
323 }
324 update();
325 }
326
327 public String getPluginsPath() {
328 return pluginsPath;
329 }
330
331 public String getHomePage() {
332 return homeUrl;
333 }
334
335 public void setHomePage(Context context, String url) {
336 Editor ed = PreferenceManager.
337 getDefaultSharedPreferences(context).edit();
338 ed.putString(PREF_HOMEPAGE, url);
339 ed.commit();
340 homeUrl = url;
341 }
342
343 public boolean isLoginInitialized() {
344 return loginInitialized;
345 }
346
347 public void setLoginInitialized(Context context) {
348 loginInitialized = true;
349 Editor ed = PreferenceManager.
350 getDefaultSharedPreferences(context).edit();
351 ed.putBoolean("login_initialized", loginInitialized);
352 ed.commit();
353 }
354
355 public WebSettings.TextSize getTextSize() {
356 return textSize;
357 }
358
359 public boolean openInBackground() {
360 return openInBackground;
361 }
362
363 public boolean showSecurityWarnings() {
364 return showSecurityWarnings;
365 }
366
367 public boolean isTracing() {
368 return tracing;
369 }
370
371 public boolean isLightTouch() {
372 return lightTouch;
373 }
374
375 public boolean isNavDump() {
376 return navDump;
377 }
378
379 public boolean doFlick() {
380 return doFlick;
381 }
382
383 public boolean showDebugSettings() {
384 return showDebugSettings;
385 }
386
387 public void toggleDebugSettings() {
388 showDebugSettings = !showDebugSettings;
389 navDump = showDebugSettings;
390 update();
391 }
392
393 /**
394 * Add a WebSettings object to the list of observers that will be updated
395 * when update() is called.
396 *
397 * @param s A WebSettings object that is strictly tied to the life of a
398 * WebView.
399 */
400 public Observer addObserver(WebSettings s) {
401 Observer old = mWebSettingsToObservers.get(s);
402 if (old != null) {
403 super.deleteObserver(old);
404 }
405 Observer o = new Observer(s);
406 mWebSettingsToObservers.put(s, o);
407 super.addObserver(o);
408 return o;
409 }
410
411 /**
412 * Delete the given WebSettings observer from the list of observers.
413 * @param s The WebSettings object to be deleted.
414 */
415 public void deleteObserver(WebSettings s) {
416 Observer o = mWebSettingsToObservers.get(s);
417 if (o != null) {
418 mWebSettingsToObservers.remove(s);
419 super.deleteObserver(o);
420 }
421 }
422
423 /*
424 * Package level method for obtaining a single app instance of the
425 * BrowserSettings.
426 */
427 /*package*/ static BrowserSettings getInstance() {
428 if (sSingleton == null ) {
429 sSingleton = new BrowserSettings();
430 }
431 return sSingleton;
432 }
433
434 /*
435 * Package level method for associating the BrowserSettings with TabControl
436 */
437 /* package */void setTabControl(TabControl tabControl) {
438 mTabControl = tabControl;
439 }
440
441 /*
442 * Update all the observers of the object.
443 */
444 /*package*/ void update() {
445 setChanged();
446 notifyObservers();
447 }
448
449 /*package*/ void clearCache(Context context) {
450 WebIconDatabase.getInstance().removeAllIcons();
451 if (mTabControl != null) {
452 WebView current = mTabControl.getCurrentWebView();
453 if (current != null) {
454 current.clearCache(true);
455 }
456 }
457 }
458
459 /*package*/ void clearCookies(Context context) {
460 CookieManager.getInstance().removeAllCookie();
461 }
462
463 /* package */void clearHistory(Context context) {
464 ContentResolver resolver = context.getContentResolver();
465 Browser.clearHistory(resolver);
466 Browser.clearSearches(resolver);
467 }
468
469 /* package */ void clearFormData(Context context) {
470 WebViewDatabase.getInstance(context).clearFormData();
471 if (mTabControl != null) {
472 mTabControl.getCurrentTopWebView().clearFormData();
473 }
474 }
475
476 /*package*/ void clearPasswords(Context context) {
477 WebViewDatabase db = WebViewDatabase.getInstance(context);
478 db.clearUsernamePassword();
479 db.clearHttpAuthUsernamePassword();
480 }
481
Nicolas Roard78a98e42009-05-11 13:34:17 +0100482 /*package*/ void clearDatabases(Context context) {
483 WebStorage.getInstance().deleteAllDatabases();
484 // Remove all listed databases from the preferences
485 PreferenceActivity activity = (PreferenceActivity) context;
486 PreferenceScreen screen = (PreferenceScreen)
Nicolas Roarde46990e2009-06-19 16:27:49 +0100487 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100488 screen.removeAll();
Nicolas Roard6f480422009-05-12 16:31:33 +0100489 screen.setEnabled(false);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100490 }
491
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400492 /*package*/ void resetDefaultPreferences(Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800493 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400494 PreferenceManager.getDefaultSharedPreferences(ctx);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800495 p.edit().clear().commit();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400496 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800497 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700498 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700499 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700500 }
501
502 private String getFactoryResetHomeUrl(Context context) {
503 String url = context.getResources().getString(R.string.homepage_base);
504 if (url.indexOf("{CID}") != -1) {
505 url = url.replace("{CID}", Partner.getString(context
Ramanan Rajeswaran9768ac52009-06-03 19:34:58 -0700506 .getContentResolver(), Partner.CLIENT_ID, "android-google"));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700507 }
508 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800509 }
510
511 // Private constructor that does nothing.
512 private BrowserSettings() {
513 }
514}