blob: d2371cdbed9e8d15e3a6f25086e7bc5438d945e9 [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.content.ContentResolver;
22import android.content.Context;
23import android.content.pm.ActivityInfo;
24import android.content.SharedPreferences;
25import android.content.SharedPreferences.Editor;
Nicolas Roard78a98e42009-05-11 13:34:17 +010026import android.preference.PreferenceActivity;
27import android.preference.PreferenceScreen;
Andrei Popescu20634ce2009-07-22 16:46:55 +010028import android.os.StatFs;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.webkit.CookieManager;
30import android.webkit.WebView;
31import android.webkit.WebViewDatabase;
32import android.webkit.WebIconDatabase;
33import android.webkit.WebSettings;
Nicolas Roard78a98e42009-05-11 13:34:17 +010034import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.preference.PreferenceManager;
36import android.provider.Browser;
37
38import java.util.HashMap;
39import java.util.Observable;
40
41/*
42 * Package level class for storing various WebView and Browser settings. To use
43 * this class:
44 * BrowserSettings s = BrowserSettings.getInstance();
45 * s.addObserver(webView.getSettings());
46 * s.loadFromDb(context); // Only needed on app startup
47 * s.javaScriptEnabled = true;
48 * ... // set any other settings
49 * s.update(); // this will update all the observers
50 *
51 * To remove an observer:
52 * s.deleteObserver(webView.getSettings());
53 */
54class BrowserSettings extends Observable {
55
Leon Scroggins9ac587d2009-04-20 12:53:46 -040056 // Private variables for settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080057 // NOTE: these defaults need to be kept in sync with the XML
58 // until the performance of PreferenceManager.setDefaultValues()
59 // is improved.
60 private boolean loadsImagesAutomatically = true;
61 private boolean javaScriptEnabled = true;
62 private boolean pluginsEnabled = true;
63 private String pluginsPath; // default value set in loadFromDb().
64 private boolean javaScriptCanOpenWindowsAutomatically = false;
65 private boolean showSecurityWarnings = true;
66 private boolean rememberPasswords = true;
67 private boolean saveFormData = true;
68 private boolean openInBackground = false;
69 private String defaultTextEncodingName;
Grace Klobaf2c5c1b2009-05-26 10:48:31 -070070 private String homeUrl = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -080071 private boolean loginInitialized = false;
72 private boolean autoFitPage = true;
Leon Scrogginsb0cd0722009-03-31 14:31:22 -070073 private boolean landscapeOnly = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -080074 private boolean showDebugSettings = false;
Ben Murdoch092dd5d2009-04-22 12:34:12 +010075 private String databasePath; // default value set in loadFromDb()
76 private boolean databaseEnabled = true;
Nicolas Roard78a98e42009-05-11 13:34:17 +010077 private long webStorageDefaultQuota = 5 * 1024 * 1024;
Andrei Popescu5151ac12009-04-17 10:43:07 +010078 // The Browser always enables Application Caches.
79 private boolean appCacheEnabled = true;
80 private String appCachePath; // default value set in loadFromDb().
Andrei Popescu20634ce2009-07-22 16:46:55 +010081 private long appCacheMaxSize = Long.MAX_VALUE;
Ben Murdoch734a0662009-06-02 19:11:52 +010082 private boolean domStorageEnabled = true;
Feng Qianb3c02da2009-06-29 15:58:08 -070083 private String jsFlags = "";
The Android Open Source Project0c908882009-03-03 19:32:16 -080084
Nicolas Roard78a98e42009-05-11 13:34:17 +010085 private final static String TAG = "BrowserSettings";
86
The Android Open Source Project0c908882009-03-03 19:32:16 -080087 // Development settings
88 public WebSettings.LayoutAlgorithm layoutAlgorithm =
89 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
90 private boolean useWideViewPort = true;
91 private int userAgent = 0;
92 private boolean tracing = false;
93 private boolean lightTouch = false;
94 private boolean navDump = false;
Feng Qianb3c02da2009-06-29 15:58:08 -070095
Ben Murdochbff2d602009-07-01 20:19:05 +010096 // By default the error console is shown once the user navigates to about:debug.
97 // The setting can be then toggled from the settings menu.
98 private boolean showConsole = true;
99
The Android Open Source Project0c908882009-03-03 19:32:16 -0800100 // Browser only settings
101 private boolean doFlick = false;
102
103 // Private preconfigured values
104 private static int minimumFontSize = 8;
105 private static int minimumLogicalFontSize = 8;
106 private static int defaultFontSize = 16;
107 private static int defaultFixedFontSize = 13;
108 private static WebSettings.TextSize textSize =
109 WebSettings.TextSize.NORMAL;
Grace Kloba2f830682009-06-25 11:08:53 -0700110 private static WebSettings.ZoomDensity zoomDensity =
111 WebSettings.ZoomDensity.MEDIUM;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800112
113 // Preference keys that are used outside this class
114 public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
115 public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
116 public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
117 public final static String PREF_HOMEPAGE = "homepage";
118 public final static String PREF_CLEAR_FORM_DATA =
119 "privacy_clear_form_data";
120 public final static String PREF_CLEAR_PASSWORDS =
121 "privacy_clear_passwords";
Nicolas Roard78a98e42009-05-11 13:34:17 +0100122 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 Roarde46990e2009-06-19 16:27:49 +0100128 public final static String PREF_WEBSITE_SETTINGS = "website_settings";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800129 public final static String PREF_TEXT_SIZE = "text_size";
Grace Kloba2f830682009-06-25 11:08:53 -0700130 public final static String PREF_DEFAULT_ZOOM = "default_zoom";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800131 public final static String PREF_DEFAULT_TEXT_ENCODING =
132 "default_text_encoding";
133
134 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
Grace Klobaa4fa6ee2009-06-26 00:34:46 -0700135 "U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, " +
136 "like Gecko) Version/4.0 Safari/530.17";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800137
138 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
Grace Klobaa4fa6ee2009-06-26 00:34:46 -0700139 "CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 " +
140 "(KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800141
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);
Grace Kloba2f830682009-06-25 11:08:53 -0700194 s.setDefaultZoom(b.zoomDensity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800195 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);
Andrei Popescu20634ce2009-07-22 16:46:55 +0100214 s.setAppCacheMaxSize(b.appCacheMaxSize);
Ben Murdochbff2d602009-07-01 20:19:05 +0100215
216 // Enable/Disable the error console.
217 b.mTabControl.getBrowserActivity().setShouldShowErrorConsole(
218 b.showDebugSettings && b.showConsole);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800219 }
220 }
221
222 /**
223 * Load settings from the browser app's database.
224 * NOTE: Strings used for the preferences must match those specified
225 * in the browser_preferences.xml
226 * @param ctx A Context object used to query the browser's settings
227 * database. If the database exists, the saved settings will be
228 * stored in this BrowserSettings object. This will update all
229 * observers of this object.
230 */
231 public void loadFromDb(Context ctx) {
232 SharedPreferences p =
233 PreferenceManager.getDefaultSharedPreferences(ctx);
234
235 // Set the default value for the plugins path to the application's
236 // local directory.
237 pluginsPath = ctx.getDir("plugins", 0).getPath();
Andrei Popescu5151ac12009-04-17 10:43:07 +0100238 // Set the default value for the Application Caches path.
239 appCachePath = ctx.getDir("appcache", 0).getPath();
Andrei Popescu20634ce2009-07-22 16:46:55 +0100240 // Determine the maximum size of the application cache.
241 appCacheMaxSize = getAppCacheMaxSize();
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100242 // Set the default value for the Database path.
243 databasePath = ctx.getDir("databases", 0).getPath();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800244
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700245 homeUrl = getFactoryResetHomeUrl(ctx);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700246
The Android Open Source Project0c908882009-03-03 19:32:16 -0800247 // Load the defaults from the xml
248 // This call is TOO SLOW, need to manually keep the defaults
249 // in sync
250 //PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
251 syncSharedPreferences(p);
252 }
253
254 /* package */ void syncSharedPreferences(SharedPreferences p) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700255
The Android Open Source Project0c908882009-03-03 19:32:16 -0800256 homeUrl =
257 p.getString(PREF_HOMEPAGE, homeUrl);
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700258
The Android Open Source Project0c908882009-03-03 19:32:16 -0800259 loadsImagesAutomatically = p.getBoolean("load_images",
260 loadsImagesAutomatically);
261 javaScriptEnabled = p.getBoolean("enable_javascript",
262 javaScriptEnabled);
263 pluginsEnabled = p.getBoolean("enable_plugins",
264 pluginsEnabled);
265 pluginsPath = p.getString("plugins_path", pluginsPath);
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100266 databasePath = p.getString("database_path", databasePath);
267 databaseEnabled = p.getBoolean("enable_database", databaseEnabled);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100268 webStorageDefaultQuota = Long.parseLong(p.getString(PREF_DEFAULT_QUOTA,
269 String.valueOf(webStorageDefaultQuota)));
Andrei Popescu5151ac12009-04-17 10:43:07 +0100270 appCacheEnabled = p.getBoolean("enable_appcache",
Ben Murdoch734a0662009-06-02 19:11:52 +0100271 appCacheEnabled);
272 domStorageEnabled = p.getBoolean("enable_domstorage",
273 domStorageEnabled);
Andrei Popescu5151ac12009-04-17 10:43:07 +0100274 appCachePath = p.getString("appcache_path", appCachePath);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800275 javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
276 "block_popup_windows",
277 !javaScriptCanOpenWindowsAutomatically);
278 showSecurityWarnings = p.getBoolean("show_security_warnings",
279 showSecurityWarnings);
280 rememberPasswords = p.getBoolean("remember_passwords",
281 rememberPasswords);
282 saveFormData = p.getBoolean("save_formdata",
283 saveFormData);
284 boolean accept_cookies = p.getBoolean("accept_cookies",
285 CookieManager.getInstance().acceptCookie());
286 CookieManager.getInstance().setAcceptCookie(accept_cookies);
287 openInBackground = p.getBoolean("open_in_background", openInBackground);
288 loginInitialized = p.getBoolean("login_initialized", loginInitialized);
289 textSize = WebSettings.TextSize.valueOf(
290 p.getString(PREF_TEXT_SIZE, textSize.name()));
Grace Kloba2f830682009-06-25 11:08:53 -0700291 zoomDensity = WebSettings.ZoomDensity.valueOf(
292 p.getString(PREF_DEFAULT_ZOOM, zoomDensity.name()));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800293 autoFitPage = p.getBoolean("autofit_pages", autoFitPage);
Leon Scrogginsb0cd0722009-03-31 14:31:22 -0700294 boolean landscapeOnlyTemp =
295 p.getBoolean("landscape_only", landscapeOnly);
296 if (landscapeOnlyTemp != landscapeOnly) {
297 landscapeOnly = landscapeOnlyTemp;
298 mTabControl.getBrowserActivity().setRequestedOrientation(
299 landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
300 : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
301 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800302 useWideViewPort = true; // use wide view port for either setting
303 if (autoFitPage) {
304 layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
305 } else {
306 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
307 }
308 defaultTextEncodingName =
309 p.getString(PREF_DEFAULT_TEXT_ENCODING,
310 defaultTextEncodingName);
311
312 showDebugSettings =
313 p.getBoolean(PREF_DEBUG_SETTINGS, showDebugSettings);
314 // Debug menu items have precidence if the menu is visible
315 if (showDebugSettings) {
316 boolean small_screen = p.getBoolean("small_screen",
317 layoutAlgorithm ==
318 WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
319 if (small_screen) {
320 layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
321 } else {
322 boolean normal_layout = p.getBoolean("normal_layout",
323 layoutAlgorithm == WebSettings.LayoutAlgorithm.NORMAL);
324 if (normal_layout) {
325 layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL;
326 } else {
327 layoutAlgorithm =
328 WebSettings.LayoutAlgorithm.NARROW_COLUMNS;
329 }
330 }
331 useWideViewPort = p.getBoolean("wide_viewport", useWideViewPort);
332 tracing = p.getBoolean("enable_tracing", tracing);
333 lightTouch = p.getBoolean("enable_light_touch", lightTouch);
334 navDump = p.getBoolean("enable_nav_dump", navDump);
335 doFlick = p.getBoolean("enable_flick", doFlick);
336 userAgent = Integer.parseInt(p.getString("user_agent", "0"));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800337 }
Feng Qianb3c02da2009-06-29 15:58:08 -0700338 // JS flags is loaded from DB even if showDebugSettings is false,
339 // so that it can be set once and be effective all the time.
340 jsFlags = p.getString("js_engine_flags", "");
Ben Murdochbff2d602009-07-01 20:19:05 +0100341
342 // Read the setting for showing/hiding the JS Console always so that should the
343 // user enable debug settings, we already know if we should show the console.
344 // The user will never see the console unless they navigate to about:debug,
345 // regardless of the setting we read here. This setting is only used after debug
346 // is enabled.
347 showConsole = p.getBoolean("javascript_console", showConsole);
348 mTabControl.getBrowserActivity().setShouldShowErrorConsole(
349 showDebugSettings && showConsole);
Grace Klobad9ee1392009-07-20 11:53:33 -0700350
351 update();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800352 }
353
354 public String getPluginsPath() {
355 return pluginsPath;
356 }
357
358 public String getHomePage() {
359 return homeUrl;
360 }
361
Feng Qianb3c02da2009-06-29 15:58:08 -0700362 public String getJsFlags() {
363 return jsFlags;
364 }
365
The Android Open Source Project0c908882009-03-03 19:32:16 -0800366 public void setHomePage(Context context, String url) {
367 Editor ed = PreferenceManager.
368 getDefaultSharedPreferences(context).edit();
369 ed.putString(PREF_HOMEPAGE, url);
370 ed.commit();
371 homeUrl = url;
372 }
373
374 public boolean isLoginInitialized() {
375 return loginInitialized;
376 }
377
378 public void setLoginInitialized(Context context) {
379 loginInitialized = true;
380 Editor ed = PreferenceManager.
381 getDefaultSharedPreferences(context).edit();
382 ed.putBoolean("login_initialized", loginInitialized);
383 ed.commit();
384 }
385
386 public WebSettings.TextSize getTextSize() {
387 return textSize;
388 }
389
Grace Kloba2f830682009-06-25 11:08:53 -0700390 public WebSettings.ZoomDensity getDefaultZoom() {
391 return zoomDensity;
392 }
393
The Android Open Source Project0c908882009-03-03 19:32:16 -0800394 public boolean openInBackground() {
395 return openInBackground;
396 }
397
398 public boolean showSecurityWarnings() {
399 return showSecurityWarnings;
400 }
401
402 public boolean isTracing() {
403 return tracing;
404 }
405
406 public boolean isLightTouch() {
407 return lightTouch;
408 }
409
410 public boolean isNavDump() {
411 return navDump;
412 }
413
414 public boolean doFlick() {
415 return doFlick;
416 }
417
418 public boolean showDebugSettings() {
419 return showDebugSettings;
420 }
421
422 public void toggleDebugSettings() {
423 showDebugSettings = !showDebugSettings;
424 navDump = showDebugSettings;
425 update();
426 }
427
428 /**
429 * Add a WebSettings object to the list of observers that will be updated
430 * when update() is called.
431 *
432 * @param s A WebSettings object that is strictly tied to the life of a
433 * WebView.
434 */
435 public Observer addObserver(WebSettings s) {
436 Observer old = mWebSettingsToObservers.get(s);
437 if (old != null) {
438 super.deleteObserver(old);
439 }
440 Observer o = new Observer(s);
441 mWebSettingsToObservers.put(s, o);
442 super.addObserver(o);
443 return o;
444 }
445
446 /**
447 * Delete the given WebSettings observer from the list of observers.
448 * @param s The WebSettings object to be deleted.
449 */
450 public void deleteObserver(WebSettings s) {
451 Observer o = mWebSettingsToObservers.get(s);
452 if (o != null) {
453 mWebSettingsToObservers.remove(s);
454 super.deleteObserver(o);
455 }
456 }
457
458 /*
459 * Package level method for obtaining a single app instance of the
460 * BrowserSettings.
461 */
462 /*package*/ static BrowserSettings getInstance() {
463 if (sSingleton == null ) {
464 sSingleton = new BrowserSettings();
465 }
466 return sSingleton;
467 }
468
469 /*
470 * Package level method for associating the BrowserSettings with TabControl
471 */
472 /* package */void setTabControl(TabControl tabControl) {
473 mTabControl = tabControl;
474 }
475
476 /*
477 * Update all the observers of the object.
478 */
479 /*package*/ void update() {
480 setChanged();
481 notifyObservers();
482 }
483
484 /*package*/ void clearCache(Context context) {
485 WebIconDatabase.getInstance().removeAllIcons();
486 if (mTabControl != null) {
487 WebView current = mTabControl.getCurrentWebView();
488 if (current != null) {
489 current.clearCache(true);
490 }
491 }
492 }
493
494 /*package*/ void clearCookies(Context context) {
495 CookieManager.getInstance().removeAllCookie();
496 }
497
498 /* package */void clearHistory(Context context) {
499 ContentResolver resolver = context.getContentResolver();
500 Browser.clearHistory(resolver);
501 Browser.clearSearches(resolver);
502 }
503
504 /* package */ void clearFormData(Context context) {
505 WebViewDatabase.getInstance(context).clearFormData();
506 if (mTabControl != null) {
507 mTabControl.getCurrentTopWebView().clearFormData();
508 }
509 }
510
511 /*package*/ void clearPasswords(Context context) {
512 WebViewDatabase db = WebViewDatabase.getInstance(context);
513 db.clearUsernamePassword();
514 db.clearHttpAuthUsernamePassword();
515 }
516
Nicolas Roard78a98e42009-05-11 13:34:17 +0100517 /*package*/ void clearDatabases(Context context) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100518 WebStorage.getInstance().deleteAllData();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100519 // Remove all listed databases from the preferences
520 PreferenceActivity activity = (PreferenceActivity) context;
521 PreferenceScreen screen = (PreferenceScreen)
Nicolas Roarde46990e2009-06-19 16:27:49 +0100522 activity.findPreference(BrowserSettings.PREF_WEBSITE_SETTINGS);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100523 screen.removeAll();
Nicolas Roard6f480422009-05-12 16:31:33 +0100524 screen.setEnabled(false);
Nicolas Roard78a98e42009-05-11 13:34:17 +0100525 }
526
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400527 /*package*/ void resetDefaultPreferences(Context ctx) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800528 SharedPreferences p =
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400529 PreferenceManager.getDefaultSharedPreferences(ctx);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800530 p.edit().clear().commit();
Leon Scroggins9ac587d2009-04-20 12:53:46 -0400531 PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800532 true);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700533 // reset homeUrl
Grace Klobaa3f90f02009-05-26 11:32:53 -0700534 setHomePage(ctx, getFactoryResetHomeUrl(ctx));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700535 }
536
537 private String getFactoryResetHomeUrl(Context context) {
538 String url = context.getResources().getString(R.string.homepage_base);
539 if (url.indexOf("{CID}") != -1) {
540 url = url.replace("{CID}", Partner.getString(context
Ramanan Rajeswaran9768ac52009-06-03 19:34:58 -0700541 .getContentResolver(), Partner.CLIENT_ID, "android-google"));
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700542 }
543 return url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800544 }
545
Andrei Popescu20634ce2009-07-22 16:46:55 +0100546 private long getAppCacheMaxSize() {
547 StatFs dataPartition = new StatFs(appCachePath);
548 long freeSpace = dataPartition.getAvailableBlocks()
549 * dataPartition.getBlockSize();
550 long fileSystemSize = dataPartition.getBlockCount()
551 * dataPartition.getBlockSize();
552 return calculateAppCacheMaxSize(fileSystemSize, freeSpace);
553 }
554
555 /*package*/ static long calculateAppCacheMaxSize(long fileSystemSizeBytes,
556 long freeSpaceBytes) {
557 if (fileSystemSizeBytes <= 0
558 || freeSpaceBytes <= 0
559 || freeSpaceBytes > fileSystemSizeBytes) {
560 return 0;
561 }
562
563 long fileSystemSizeRatio =
564 4 << ((int) Math.floor(Math.log10(fileSystemSizeBytes / (1024 * 1024))));
565 long maxSizeBytes = (long) Math.min(Math.floor(fileSystemSizeBytes / fileSystemSizeRatio),
566 Math.floor(freeSpaceBytes / 4));
567 // Round maxSizeBytes up to a multiple of 512KB (except when freeSpaceBytes < 1MB).
568 long maxSizeStepBytes = 512 * 1024;
569 if (freeSpaceBytes < maxSizeStepBytes * 2) {
570 return 0;
571 }
572 return (maxSizeStepBytes * ((maxSizeBytes / maxSizeStepBytes) + 1));
573 }
574
The Android Open Source Project0c908882009-03-03 19:32:16 -0800575 // Private constructor that does nothing.
576 private BrowserSettings() {
577 }
578}