blob: 36a9d3f57e4093870e2888df63de407c79a25788 [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";
Nicolas Roard6f480422009-05-12 16:31:33 +0100131 public final static String PREF_WEBSTORAGE_CLEAR_ALL = "webstorage_clear_databases";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800132 public final static String PREF_TEXT_SIZE = "text_size";
133 public final static String PREF_DEFAULT_TEXT_ENCODING =
134 "default_text_encoding";
135
136 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
137 "U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/525.18 (KHTML, " +
138 "like Gecko) Version/3.1.2 Safari/525.20.1";
139
140 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
141 "CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 " +
142 "(KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20";
143
144 // Value to truncate strings when adding them to a TextView within
145 // a ListView
146 public final static int MAX_TEXTVIEW_LEN = 80;
147
148 private TabControl mTabControl;
149
150 // Single instance of the BrowserSettings for use in the Browser app.
151 private static BrowserSettings sSingleton;
152
153 // Private map of WebSettings to Observer objects used when deleting an
154 // observer.
155 private HashMap<WebSettings,Observer> mWebSettingsToObservers =
156 new HashMap<WebSettings,Observer>();
157
158 /*
159 * An observer wrapper for updating a WebSettings object with the new
160 * settings after a call to BrowserSettings.update().
161 */
162 static class Observer implements java.util.Observer {
163 // Private WebSettings object that will be updated.
164 private WebSettings mSettings;
165
166 Observer(WebSettings w) {
167 mSettings = w;
168 }
169
170 public void update(Observable o, Object arg) {
171 BrowserSettings b = (BrowserSettings)o;
172 WebSettings s = mSettings;
173
174 s.setLayoutAlgorithm(b.layoutAlgorithm);
175 if (b.userAgent == 0) {
176 // use the default ua string
177 s.setUserAgentString(null);
178 } else if (b.userAgent == 1) {
179 s.setUserAgentString(DESKTOP_USERAGENT);
180 } else if (b.userAgent == 2) {
181 s.setUserAgentString(IPHONE_USERAGENT);
182 }
183 s.setUseWideViewPort(b.useWideViewPort);
184 s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
185 s.setJavaScriptEnabled(b.javaScriptEnabled);
186 s.setPluginsEnabled(b.pluginsEnabled);
187 s.setPluginsPath(b.pluginsPath);
188 s.setJavaScriptCanOpenWindowsAutomatically(
189 b.javaScriptCanOpenWindowsAutomatically);
190 s.setDefaultTextEncodingName(b.defaultTextEncodingName);
191 s.setMinimumFontSize(b.minimumFontSize);
192 s.setMinimumLogicalFontSize(b.minimumLogicalFontSize);
193 s.setDefaultFontSize(b.defaultFontSize);
194 s.setDefaultFixedFontSize(b.defaultFixedFontSize);
195 s.setNavDump(b.navDump);
196 s.setTextSize(b.textSize);
197 s.setLightTouchEnabled(b.lightTouch);
198 s.setSaveFormData(b.saveFormData);
199 s.setSavePassword(b.rememberPasswords);
200
201 // WebView inside Browser doesn't want initial focus to be set.
202 s.setNeedInitialFocus(false);
203 // Browser supports multiple windows
204 s.setSupportMultipleWindows(true);
205 // Turn off file access
206 s.setAllowFileAccess(false);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100207
208 s.setDatabasePath(b.databasePath);
209 s.setDatabaseEnabled(b.databaseEnabled);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100210 s.setWebStorageDefaultQuota(b.webStorageDefaultQuota);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100211
Andrei Popescu5151ac12009-04-17 10:43:07 +0100212 // Turn on Application Caches.
213 s.setAppCachePath(b.appCachePath);
214 s.setAppCacheEnabled(b.appCacheEnabled);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800215 }
216 }
217
218 /**
219 * Load settings from the browser app's database.
220 * NOTE: Strings used for the preferences must match those specified
221 * in the browser_preferences.xml
222 * @param ctx A Context object used to query the browser's settings
223 * database. If the database exists, the saved settings will be
224 * stored in this BrowserSettings object. This will update all
225 * observers of this object.
226 */
227 public void loadFromDb(Context ctx) {
228 SharedPreferences p =
229 PreferenceManager.getDefaultSharedPreferences(ctx);
230
231 // Set the default value for the plugins path to the application's
232 // local directory.
233 pluginsPath = ctx.getDir("plugins", 0).getPath();
Andrei Popescu5151ac12009-04-17 10:43:07 +0100234 // Set the default value for the Application Caches path.
235 appCachePath = ctx.getDir("appcache", 0).getPath();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100236 // Set the default value for the Database path.
237 databasePath = ctx.getDir("databases", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800238
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400239 homeUrl = DEFAULT_HOME_URL +
240 Partner.getString(ctx.getContentResolver(), Partner.CLIENT_ID);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700241
The Android Open Source Project0c908882009-03-03 19:32:16 -0800242 // Load the defaults from the xml
243 // This call is TOO SLOW, need to manually keep the defaults
244 // in sync
245 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
246 syncSharedPreferences(p);
247 }
248
249 /* package */ void syncSharedPreferences(SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700250
The Android Open Source Project0c908882009-03-03 19:32:16 -0800251 homeUrl =
252 p.getString(PREF_HOMEPAGE, homeUrl);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700253
The Android Open Source Project0c908882009-03-03 19:32:16 -0800254 loadsImagesAutomatically = p.getBoolean("load_images",
255 loadsImagesAutomatically);
256 javaScriptEnabled = p.getBoolean("enable_javascript",
257 javaScriptEnabled);
258 pluginsEnabled = p.getBoolean("enable_plugins",
259 pluginsEnabled);
260 pluginsPath = p.getString("plugins_path", pluginsPath);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100261 databasePath = p.getString("database_path", databasePath);
262 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100263 webStorageDefaultQuota = Long.parseLong(p.getString(PREF_DEFAULT_QUOTA,
264 String.valueOf(webStorageDefaultQuota)));
Andrei Popescu5151ac12009-04-17 10:43:07 +0100265 appCacheEnabled = p.getBoolean("enable_appcache",
266 appCacheEnabled);
267 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);
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400505 setHomePage(ctx, DEFAULT_HOME_URL +
506 Partner.getString(ctx.getContentResolver(), Partner.CLIENT_ID));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800507 }
508
509 // Private constructor that does nothing.
510 private BrowserSettings() {
511 }
512}