blob: 9be22ead43eee01692cc98d127fd5f5cc6df14a4 [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 static final String DEFAULT_HOME_URL =
61 "http://www.google.com/m?client=ms-";
62 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080063 // NOTE: these defaults need to be kept in sync with the XML
64 // until the performance of PreferenceManager.setDefaultValues()
65 // is improved.
66 private boolean loadsImagesAutomatically = true;
67 private boolean javaScriptEnabled = true;
68 private boolean pluginsEnabled = true;
69 private String pluginsPath; // default value set in loadFromDb().
70 private boolean javaScriptCanOpenWindowsAutomatically = false;
71 private boolean showSecurityWarnings = true;
72 private boolean rememberPasswords = true;
73 private boolean saveFormData = true;
74 private boolean openInBackground = false;
75 private String defaultTextEncodingName;
Leon Scroggins9ac587d2009-04-20 12:53:46 -040076 private String homeUrl;
The Android Open Source Project0c908882009-03-03 19:32:16 -080077 private boolean loginInitialized = false;
78 private boolean autoFitPage = true;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -070079 private boolean landscapeOnly = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -080080 private boolean showDebugSettings = false;
Ben Murdoch092dd5d2009-04-22 12:34:12 +010081 private String databasePath; // default value set in loadFromDb()
82 private boolean databaseEnabled = true;
Nicolas Roard78a98e42009-05-11 13:34:17 +010083 private long webStorageDefaultQuota = 5 * 1024 * 1024;
Andrei Popescu5151ac12009-04-17 10:43:07 +010084 // The Browser always enables Application Caches.
85 private boolean appCacheEnabled = true;
86 private String appCachePath; // default value set in loadFromDb().
The Android Open Source Project0c908882009-03-03 19:32:16 -080087
Nicolas Roard78a98e42009-05-11 13:34:17 +010088 private final static String TAG = "BrowserSettings";
89
The Android Open Source Project0c908882009-03-03 19:32:16 -080090 // Development settings
91 public WebSettings.LayoutAlgorithm layoutAlgorithm =
92 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
93 private boolean useWideViewPort = true;
94 private int userAgent = 0;
95 private boolean tracing = false;
96 private boolean lightTouch = false;
97 private boolean navDump = false;
98 // Browser only settings
99 private boolean doFlick = false;
100
101 // Private preconfigured values
102 private static int minimumFontSize = 8;
103 private static int minimumLogicalFontSize = 8;
104 private static int defaultFontSize = 16;
105 private static int defaultFixedFontSize = 13;
106 private static WebSettings.TextSize textSize =
107 WebSettings.TextSize.NORMAL;
108
109 // Preference keys that are used outside this class
110 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
111 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
112 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
113 public final static String PREF_HOMEPAGE = "homepage";
114 public final static String PREF_CLEAR_FORM_DATA =
115 "privacy_clear_form_data";
116 public final static String PREF_CLEAR_PASSWORDS =
117 "privacy_clear_passwords";
Nicolas Roard78a98e42009-05-11 13:34:17 +0100118 public final static String PREF_CLEAR_DATABASES =
119 "webstorage_clear_databases";
120 public final static String PREF_CLEAR_ALL_DATA =
121 "webstorage_clear_all_data";
122 public final static String PREF_MANAGE_QUOTA =
123 "webstorage_manage_quota";
124 public final static String PREF_DEFAULT_QUOTA =
125 "webstorage_default_quota";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800126 public final static String PREF_EXTRAS_RESET_DEFAULTS =
127 "reset_default_preferences";
128 public final static String PREF_DEBUG_SETTINGS = "debug_menu";
129 public final static String PREF_GEARS_SETTINGS = "gears_settings";
Nicolas Roard78a98e42009-05-11 13:34:17 +0100130 public final static String PREF_WEBSTORAGE_SETTINGS = "webstorage_manage_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);
186 s.setPluginsPath(b.pluginsPath);
187 s.setJavaScriptCanOpenWindowsAutomatically(
188 b.javaScriptCanOpenWindowsAutomatically);
189 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
190 s.setMinimumFontSize(b.minimumFontSize);
191 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
192 s.setDefaultFontSize(b.defaultFontSize);
193 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
194 s.setNavDump(b.navDump);
195 s.setTextSize(b.textSize);
196 s.setLightTouchEnabled(b.lightTouch);
197 s.setSaveFormData(b.saveFormData);
198 s.setSavePassword(b.rememberPasswords);
199
200 // WebView inside Browser doesn't want initial focus to be set.
201 s.setNeedInitialFocus(false);
202 // Browser supports multiple windows
203 s.setSupportMultipleWindows(true);
204 // Turn off file access
205 s.setAllowFileAccess(false);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100206
207 s.setDatabasePath(b.databasePath);
208 s.setDatabaseEnabled(b.databaseEnabled);
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
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400238 homeUrl = DEFAULT_HOME_URL +
239 Partner.getString(ctx.getContentResolver(), Partner.CLIENT_ID);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700240
The Android Open Source Project0c908882009-03-03 19:32:16 -0800241 // Load the defaults from the xml
242 // This call is TOO SLOW, need to manually keep the defaults
243 // in sync
244 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
245 syncSharedPreferences(p);
246 }
247
248 /* package */ void syncSharedPreferences(SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700249
The Android Open Source Project0c908882009-03-03 19:32:16 -0800250 homeUrl =
251 p.getString(PREF_HOMEPAGE, homeUrl);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700252
The Android Open Source Project0c908882009-03-03 19:32:16 -0800253 loadsImagesAutomatically = p.getBoolean("load_images",
254 loadsImagesAutomatically);
255 javaScriptEnabled = p.getBoolean("enable_javascript",
256 javaScriptEnabled);
257 pluginsEnabled = p.getBoolean("enable_plugins",
258 pluginsEnabled);
259 pluginsPath = p.getString("plugins_path", pluginsPath);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100260 databasePath = p.getString("database_path", databasePath);
261 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100262 webStorageDefaultQuota = Long.parseLong(p.getString(PREF_DEFAULT_QUOTA,
263 String.valueOf(webStorageDefaultQuota)));
Andrei Popescu5151ac12009-04-17 10:43:07 +0100264 appCacheEnabled = p.getBoolean("enable_appcache",
265 appCacheEnabled);
266 appCachePath = p.getString("appcache_path", appCachePath);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800267 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
268 "block_popup_windows",
269 !javaScriptCanOpenWindowsAutomatically);
270 showSecurityWarnings = p.getBoolean("show_security_warnings",
271 showSecurityWarnings);
272 rememberPasswords = p.getBoolean("remember_passwords",
273 rememberPasswords);
274 saveFormData = p.getBoolean("save_formdata",
275 saveFormData);
276 boolean accept_cookies = p.getBoolean("accept_cookies",
277 CookieManager.getInstance().acceptCookie());
278 CookieManager.getInstance().setAcceptCookie(accept_cookies);
279 openInBackground = p.getBoolean("open_in_background", openInBackground);
280 loginInitialized = p.getBoolean("login_initialized", loginInitialized);
281 textSize = WebSettings.TextSize.valueOf(
282 p.getString(PREF_TEXT_SIZE, textSize.name()));
283 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700284 boolean landscapeOnlyTemp =
285 p.getBoolean("landscape_only", landscapeOnly);
286 if (landscapeOnlyTemp != landscapeOnly) {
287 landscapeOnly = landscapeOnlyTemp;
288 mTabControl.getBrowserActivity().setRequestedOrientation(
289 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
290 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
291 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800292 useWideViewPort = true; // use wide view port for either setting
293 if (autoFitPage) {
294 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
295 } else {
296 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
297 }
298 defaultTextEncodingName =
299 p.getString(PREF_DEFAULT_TEXT_ENCODING,
300 defaultTextEncodingName);
301
302 showDebugSettings =
303 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
304 // Debug menu items have precidence if the menu is visible
305 if (showDebugSettings) {
306 boolean small_screen = p.getBoolean("small_screen",
307 layoutAlgorithm ==
308 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
309 if (small_screen) {
310 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
311 } else {
312 boolean normal_layout = p.getBoolean("normal_layout",
313 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
314 if (normal_layout) {
315 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
316 } else {
317 layoutAlgorithm =
318 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
319 }
320 }
321 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
322 tracing = p.getBoolean("enable_tracing", tracing);
323 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
324 navDump = p.getBoolean("enable_nav_dump", navDump);
325 doFlick = p.getBoolean("enable_flick", doFlick);
326 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
327 mTabControl.getBrowserActivity().setBaseSearchUrl(
328 p.getString("search_url", ""));
329 }
330 update();
331 }
332
333 public String getPluginsPath() {
334 return pluginsPath;
335 }
336
337 public String getHomePage() {
338 return homeUrl;
339 }
340
341 public void setHomePage(Context context, String url) {
342 Editor ed = PreferenceManager.
343 getDefaultSharedPreferences(context).edit();
344 ed.putString(PREF_HOMEPAGE, url);
345 ed.commit();
346 homeUrl = url;
347 }
348
349 public boolean isLoginInitialized() {
350 return loginInitialized;
351 }
352
353 public void setLoginInitialized(Context context) {
354 loginInitialized = true;
355 Editor ed = PreferenceManager.
356 getDefaultSharedPreferences(context).edit();
357 ed.putBoolean("login_initialized", loginInitialized);
358 ed.commit();
359 }
360
361 public WebSettings.TextSize getTextSize() {
362 return textSize;
363 }
364
365 public boolean openInBackground() {
366 return openInBackground;
367 }
368
369 public boolean showSecurityWarnings() {
370 return showSecurityWarnings;
371 }
372
373 public boolean isTracing() {
374 return tracing;
375 }
376
377 public boolean isLightTouch() {
378 return lightTouch;
379 }
380
381 public boolean isNavDump() {
382 return navDump;
383 }
384
385 public boolean doFlick() {
386 return doFlick;
387 }
388
389 public boolean showDebugSettings() {
390 return showDebugSettings;
391 }
392
393 public void toggleDebugSettings() {
394 showDebugSettings = !showDebugSettings;
395 navDump = showDebugSettings;
396 update();
397 }
398
399 /**
400 * Add a WebSettings object to the list of observers that will be updated
401 * when update() is called.
402 *
403 * @param s A WebSettings object that is strictly tied to the life of a
404 * WebView.
405 */
406 public Observer addObserver(WebSettings s) {
407 Observer old = mWebSettingsToObservers.get(s);
408 if (old != null) {
409 super.deleteObserver(old);
410 }
411 Observer o = new Observer(s);
412 mWebSettingsToObservers.put(s, o);
413 super.addObserver(o);
414 return o;
415 }
416
417 /**
418 * Delete the given WebSettings observer from the list of observers.
419 * @param s The WebSettings object to be deleted.
420 */
421 public void deleteObserver(WebSettings s) {
422 Observer o = mWebSettingsToObservers.get(s);
423 if (o != null) {
424 mWebSettingsToObservers.remove(s);
425 super.deleteObserver(o);
426 }
427 }
428
429 /*
430 * Package level method for obtaining a single app instance of the
431 * BrowserSettings.
432 */
433 /*package*/ static BrowserSettings getInstance() {
434 if (sSingleton == null ) {
435 sSingleton = new BrowserSettings();
436 }
437 return sSingleton;
438 }
439
440 /*
441 * Package level method for associating the BrowserSettings with TabControl
442 */
443 /* package */void setTabControl(TabControl tabControl) {
444 mTabControl = tabControl;
445 }
446
447 /*
448 * Update all the observers of the object.
449 */
450 /*package*/ void update() {
451 setChanged();
452 notifyObservers();
453 }
454
455 /*package*/ void clearCache(Context context) {
456 WebIconDatabase.getInstance().removeAllIcons();
457 if (mTabControl != null) {
458 WebView current = mTabControl.getCurrentWebView();
459 if (current != null) {
460 current.clearCache(true);
461 }
462 }
463 }
464
465 /*package*/ void clearCookies(Context context) {
466 CookieManager.getInstance().removeAllCookie();
467 }
468
469 /* package */void clearHistory(Context context) {
470 ContentResolver resolver = context.getContentResolver();
471 Browser.clearHistory(resolver);
472 Browser.clearSearches(resolver);
473 }
474
475 /* package */ void clearFormData(Context context) {
476 WebViewDatabase.getInstance(context).clearFormData();
477 if (mTabControl != null) {
478 mTabControl.getCurrentTopWebView().clearFormData();
479 }
480 }
481
482 /*package*/ void clearPasswords(Context context) {
483 WebViewDatabase db = WebViewDatabase.getInstance(context);
484 db.clearUsernamePassword();
485 db.clearHttpAuthUsernamePassword();
486 }
487
Nicolas Roard78a98e42009-05-11 13:34:17 +0100488 /*package*/ void clearDatabases(Context context) {
489 WebStorage.getInstance().deleteAllDatabases();
490 // Remove all listed databases from the preferences
491 PreferenceActivity activity = (PreferenceActivity) context;
492 PreferenceScreen screen = (PreferenceScreen)
493 activity.findPreference(BrowserSettings.PREF_WEBSTORAGE_SETTINGS);
494 screen.removeAll();
495 }
496
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400497 /*package*/ void resetDefaultPreferences(Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800498 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400499 PreferenceManager.getDefaultSharedPreferences(ctx);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800500 p.edit().clear().commit();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400501 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800502 true);
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400503 setHomePage(ctx, DEFAULT_HOME_URL +
504 Partner.getString(ctx.getContentResolver(), Partner.CLIENT_ID));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800505 }
506
507 // Private constructor that does nothing.
508 private BrowserSettings() {
509 }
510}