blob: 97769fce311d99396cc87ebd2f7030739fbf2e67 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
John Reck35e9dd62011-04-25 09:01:54 -07002 * Copyright (C) 2011 The Android Open Source Project
The Android Open Source Project0c908882009-03-03 19:32:16 -08003 *
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
Grace Kloba9804c432009-12-02 11:07:40 -080019import android.app.ActivityManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080020import android.content.ContentResolver;
21import android.content.Context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.content.SharedPreferences;
John Reck812d2d62011-01-18 14:16:15 -080023import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
John Reckf48314f2011-04-27 17:52:17 -070024import android.os.Build;
Ben Murdoch23da30e2010-10-26 15:18:44 +010025import android.os.Message;
Ben Murdoch0cb81892010-10-08 12:41:33 +010026import android.preference.PreferenceManager;
Ben Murdoch0cb81892010-10-08 12:41:33 +010027import android.provider.Browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010029import android.webkit.GeolocationPermissions;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.webkit.WebIconDatabase;
31import android.webkit.WebSettings;
Ben Murdoch0cb81892010-10-08 12:41:33 +010032import android.webkit.WebSettings.AutoFillProfile;
John Reck35e9dd62011-04-25 09:01:54 -070033import android.webkit.WebSettings.LayoutAlgorithm;
34import android.webkit.WebSettings.PluginState;
35import android.webkit.WebSettings.TextSize;
36import android.webkit.WebSettings.ZoomDensity;
Nicolas Roard78a98e42009-05-11 13:34:17 +010037import android.webkit.WebStorage;
John Reck812d2d62011-01-18 14:16:15 -080038import android.webkit.WebView;
39import android.webkit.WebViewDatabase;
The Android Open Source Project0c908882009-03-03 19:32:16 -080040
John Reck46500332011-06-07 14:36:10 -070041import com.android.browser.homepages.HomeProvider;
42import com.android.browser.provider.BrowserProvider;
43import com.android.browser.search.SearchEngine;
44import com.android.browser.search.SearchEngines;
45
John Reck35e9dd62011-04-25 09:01:54 -070046import java.lang.ref.WeakReference;
47import java.util.Iterator;
48import java.util.LinkedList;
John Reckb8b2af82011-05-20 15:58:33 -070049import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050
John Reck35e9dd62011-04-25 09:01:54 -070051/**
52 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080053 */
John Reck35e9dd62011-04-25 09:01:54 -070054public class BrowserSettings implements OnSharedPreferenceChangeListener,
55 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010056
John Reck35e9dd62011-04-25 09:01:54 -070057 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070058 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
59 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
60 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080061
62 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070063 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
64 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070065
66 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070067 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
68 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070069
70 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070071 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
72 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080073
John Reck7fd1e8f2011-04-27 18:22:57 -070074 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
75 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
76 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
77
John Reck35e9dd62011-04-25 09:01:54 -070078 private static final String USER_AGENTS[] = { null,
79 DESKTOP_USERAGENT,
80 IPHONE_USERAGENT,
81 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070082 FROYO_USERAGENT,
83 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070084 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080085
John Reck8fc22a12011-06-16 14:57:41 -070086 // The minimum min font size
87 // Aka, the lower bounds for the min font size range
88 // which is 1:5..24
89 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070090 // The initial value in the text zoom range
91 // This is what represents 100% in the SeekBarPreference range
92 private static final int TEXT_ZOOM_START_VAL = 10;
93 // The size of a single step in the text zoom range, in percent
94 private static final int TEXT_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -070095
John Reck35e9dd62011-04-25 09:01:54 -070096 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -070097
John Reck35e9dd62011-04-25 09:01:54 -070098 private Context mContext;
99 private SharedPreferences mPrefs;
100 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700101 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700102 private WebStorageSizeManager mWebStorageSizeManager;
103 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700104 private WeakHashMap<WebSettings, String> mCustomUserAgents;
John Reck78a6a1d2011-07-21 13:54:03 -0700105 private boolean mInitialized = false;
106
107 // Cached values
108 private int mPageCacheCapacity = 1;
109 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110
John Reck35e9dd62011-04-25 09:01:54 -0700111 // Cached settings
112 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800113
John Reck35e9dd62011-04-25 09:01:54 -0700114 public static void initialize(final Context context) {
115 sInstance = new BrowserSettings(context);
116 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800117
John Reck35e9dd62011-04-25 09:01:54 -0700118 public static BrowserSettings getInstance() {
119 return sInstance;
120 }
Ben Murdochef671652010-11-25 17:17:58 +0000121
John Reck35e9dd62011-04-25 09:01:54 -0700122 private BrowserSettings(Context context) {
123 mContext = context;
124 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
125 mAutofillHandler = new AutofillHandler(mContext);
126 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700127 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reck35e9dd62011-04-25 09:01:54 -0700128 mPrefs.registerOnSharedPreferenceChangeListener(this);
129 mAutofillHandler.asyncLoadFromDb();
John Reck78a6a1d2011-07-21 13:54:03 -0700130 new Thread(mInitialization, "BrowserSettingsInitialization").start();
John Reck35e9dd62011-04-25 09:01:54 -0700131 }
132
133 public void setController(Controller controller) {
134 mController = controller;
135 syncSharedSettings();
136
137 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
138 ((InstantSearchEngine) mSearchEngine).setController(mController);
Ben Murdochef671652010-11-25 17:17:58 +0000139 }
140 }
141
John Reck35e9dd62011-04-25 09:01:54 -0700142 public void startManagingSettings(WebSettings settings) {
143 synchronized (mManagedSettings) {
144 syncStaticSettings(settings);
145 syncSetting(settings);
146 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800147 }
148 }
149
John Reck78a6a1d2011-07-21 13:54:03 -0700150 private Runnable mInitialization = new Runnable() {
151
152 @Override
153 public void run() {
154 // the cost of one cached page is ~3M (measured using nytimes.com). For
155 // low end devices, we only cache one page. For high end devices, we try
156 // to cache more pages, currently choose 5.
157 if (ActivityManager.staticGetMemoryClass() > 16) {
158 mPageCacheCapacity = 5;
159 }
160 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
161 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
162 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
163 if (Build.VERSION.CODENAME.equals("REL")) {
164 // This is a release build, always startup with debug disabled
165 setDebugEnabled(false);
166 }
167 if (mPrefs.contains(PREF_TEXT_SIZE)) {
168 /*
169 * Update from TextSize enum to zoom percent
170 * SMALLEST is 50%
171 * SMALLER is 75%
172 * NORMAL is 100%
173 * LARGER is 150%
174 * LARGEST is 200%
175 */
176 switch (getTextSize()) {
177 case SMALLEST:
178 setTextZoom(50);
179 break;
180 case SMALLER:
181 setTextZoom(75);
182 break;
183 case LARGER:
184 setTextZoom(150);
185 break;
186 case LARGEST:
187 setTextZoom(200);
188 break;
189 }
190 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
191 }
192 synchronized (BrowserSettings.this) {
193 mInitialized = true;
194 BrowserSettings.this.notifyAll();
195 }
196 }
197 };
198
199 void requireInitialization() {
200 synchronized (BrowserSettings.this) {
201 while (!mInitialized) {
202 try {
203 BrowserSettings.this.wait();
204 } catch (InterruptedException e) {
205 }
206 }
207 }
208 }
209
The Android Open Source Project0c908882009-03-03 19:32:16 -0800210 /**
John Reck35e9dd62011-04-25 09:01:54 -0700211 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800212 */
John Reck35e9dd62011-04-25 09:01:54 -0700213 private void syncSetting(WebSettings settings) {
214 settings.setGeolocationEnabled(enableGeolocation());
215 settings.setJavaScriptEnabled(enableJavascript());
216 settings.setLightTouchEnabled(enableLightTouch());
217 settings.setNavDump(enableNavDump());
218 settings.setShowVisualIndicator(enableVisualIndicator());
219 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
220 settings.setDefaultZoom(getDefaultZoom());
221 settings.setMinimumFontSize(getMinimumFontSize());
222 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck92f25f82011-04-26 16:57:10 -0700223 settings.setForceUserScalable(forceEnableUserScalable());
John Reck35e9dd62011-04-25 09:01:54 -0700224 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700225 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700226 settings.setAutoFillEnabled(isAutofillEnabled());
227 settings.setLayoutAlgorithm(getLayoutAlgorithm());
228 settings.setJavaScriptCanOpenWindowsAutomatically(blockPopupWindows());
229 settings.setLoadsImagesAutomatically(loadImages());
230 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
231 settings.setSavePassword(rememberPasswords());
232 settings.setSaveFormData(saveFormdata());
233 settings.setUseWideViewPort(isWideViewport());
234 settings.setAutoFillProfile(getAutoFillProfile());
John Reckb8b2af82011-05-20 15:58:33 -0700235
236 String ua = mCustomUserAgents.get(settings);
John Reck46500332011-06-07 14:36:10 -0700237 if (ua != null) {
John Reckb8b2af82011-05-20 15:58:33 -0700238 settings.setUserAgentString(ua);
239 } else {
240 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
241 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700242
243 settings.setProperty("gfxInvertedScreen",
244 useInvertedRendering() ? "true" : "false");
Ben Murdochef671652010-11-25 17:17:58 +0000245 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800246
John Reck35e9dd62011-04-25 09:01:54 -0700247 /**
248 * Syncs all the settings that have no UI
249 * These cannot change, so we only need to set them once per WebSettings
250 */
251 private void syncStaticSettings(WebSettings settings) {
252 settings.setDefaultFontSize(16);
253 settings.setDefaultFixedFontSize(13);
254 settings.setPageCacheCapacity(getPageCacheCapacity());
Ben Murdochef671652010-11-25 17:17:58 +0000255
John Reck35e9dd62011-04-25 09:01:54 -0700256 // WebView inside Browser doesn't want initial focus to be set.
257 settings.setNeedInitialFocus(false);
258 // Browser supports multiple windows
259 settings.setSupportMultipleWindows(true);
260 // enable smooth transition for better performance during panning or
261 // zooming
262 settings.setEnableSmoothTransition(true);
263 // disable content url access
264 settings.setAllowContentAccess(false);
265
266 // HTML5 API flags
267 settings.setAppCacheEnabled(true);
268 settings.setDatabaseEnabled(true);
269 settings.setDomStorageEnabled(true);
270 settings.setWorkersEnabled(true); // This only affects V8.
271
272 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700273 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700274 settings.setAppCachePath(getAppCachePath());
275 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
276 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
277 }
278
279 private void syncSharedSettings() {
280 CookieManager.getInstance().setAcceptCookie(acceptCookies());
281 if (mController != null) {
282 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700283 }
John Reck35e9dd62011-04-25 09:01:54 -0700284 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700285
John Reck35e9dd62011-04-25 09:01:54 -0700286 private void syncManagedSettings() {
287 syncSharedSettings();
288 synchronized (mManagedSettings) {
289 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
290 while (iter.hasNext()) {
291 WeakReference<WebSettings> ref = iter.next();
292 WebSettings settings = ref.get();
293 if (settings == null) {
294 iter.remove();
295 continue;
296 }
297 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000298 }
John Reck35e9dd62011-04-25 09:01:54 -0700299 }
300 }
Ben Murdochef671652010-11-25 17:17:58 +0000301
John Reck35e9dd62011-04-25 09:01:54 -0700302 @Override
303 public void onSharedPreferenceChanged(
304 SharedPreferences sharedPreferences, String key) {
305 syncManagedSettings();
306 if (PREF_SEARCH_ENGINE.equals(key)) {
307 updateSearchEngine(false);
308 }
309 if (PREF_USE_INSTANT_SEARCH.equals(key)) {
310 updateSearchEngine(true);
311 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700312 if (PREF_FULLSCREEN.equals(key)) {
313 if (mController.getUi() != null) {
314 mController.getUi().setFullscreen(useFullscreen());
315 }
Michael Kolb0241e752011-07-07 14:58:50 -0700316 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
317 if (mController.getUi() != null) {
318 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
319 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700320 }
John Reck35e9dd62011-04-25 09:01:54 -0700321 }
322
John Reck961d35d2011-06-23 09:45:54 -0700323 public static String getFactoryResetHomeUrl(Context context) {
John Reck35e9dd62011-04-25 09:01:54 -0700324 String url = context.getResources().getString(R.string.homepage_base);
325 if (url.indexOf("{CID}") != -1) {
326 url = url.replace("{CID}",
327 BrowserProvider.getClientId(context.getContentResolver()));
328 }
329 return url;
330 }
331
332 public LayoutAlgorithm getLayoutAlgorithm() {
333 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
334 if (autofitPages()) {
335 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
336 }
337 if (isDebugEnabled()) {
338 if (isSmallScreen()) {
339 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000340 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700341 if (isNormalLayout()) {
342 layoutAlgorithm = LayoutAlgorithm.NORMAL;
343 } else {
344 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
345 }
Ben Murdochef671652010-11-25 17:17:58 +0000346 }
John Reck35e9dd62011-04-25 09:01:54 -0700347 }
348 return layoutAlgorithm;
349 }
Ben Murdochef671652010-11-25 17:17:58 +0000350
John Reck35e9dd62011-04-25 09:01:54 -0700351 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700352 requireInitialization();
353 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800354 }
355
John Reck35e9dd62011-04-25 09:01:54 -0700356 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700357 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700358 return mWebStorageSizeManager;
359 }
360
John Reck35e9dd62011-04-25 09:01:54 -0700361 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700362 if (mAppCachePath == null) {
363 mAppCachePath = mContext.getDir("appcache", 0).getPath();
364 }
365 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700366 }
367
368 private void updateSearchEngine(boolean force) {
369 String searchEngineName = getSearchEngineName();
370 if (force || mSearchEngine == null ||
371 !mSearchEngine.getName().equals(searchEngineName)) {
372 if (mSearchEngine != null) {
373 if (mSearchEngine.supportsVoiceSearch()) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000374 // One or more tabs could have been in voice search mode.
375 // Clear it, since the new SearchEngine may not support
376 // it, or may handle it differently.
377 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
378 mController.getTabControl().getTab(i).revertVoiceSearchMode();
379 }
380 }
John Reck35e9dd62011-04-25 09:01:54 -0700381 mSearchEngine.close();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000382 }
John Reck35e9dd62011-04-25 09:01:54 -0700383 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000384
John Reck35e9dd62011-04-25 09:01:54 -0700385 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
386 ((InstantSearchEngine) mSearchEngine).setController(mController);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000387 }
388 }
389 }
390
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100391 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700392 if (mSearchEngine == null) {
393 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100394 }
John Reck35e9dd62011-04-25 09:01:54 -0700395 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100396 }
397
John Reck35e9dd62011-04-25 09:01:54 -0700398 public boolean isDebugEnabled() {
John Reck78a6a1d2011-07-21 13:54:03 -0700399 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700400 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100401 }
402
John Reck35e9dd62011-04-25 09:01:54 -0700403 public void setDebugEnabled(boolean value) {
404 mPrefs.edit().putBoolean(PREF_DEBUG_MENU, value).apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100405 }
406
John Reck35e9dd62011-04-25 09:01:54 -0700407 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800408 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700409 if (mController != null) {
410 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800411 if (current != null) {
412 current.clearCache(true);
413 }
414 }
415 }
416
John Reck35e9dd62011-04-25 09:01:54 -0700417 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800418 CookieManager.getInstance().removeAllCookie();
419 }
420
John Reck35e9dd62011-04-25 09:01:54 -0700421 public void clearHistory() {
422 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800423 Browser.clearHistory(resolver);
424 Browser.clearSearches(resolver);
425 }
426
John Reck35e9dd62011-04-25 09:01:54 -0700427 public void clearFormData() {
428 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700429 if (mController!= null) {
430 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100431 if (currentTopView != null) {
432 currentTopView.clearFormData();
433 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800434 }
435 }
436
John Reck35e9dd62011-04-25 09:01:54 -0700437 public void clearPasswords() {
438 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800439 db.clearUsernamePassword();
440 db.clearHttpAuthUsernamePassword();
441 }
442
John Reck35e9dd62011-04-25 09:01:54 -0700443 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100444 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100445 }
446
John Reck35e9dd62011-04-25 09:01:54 -0700447 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100448 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100449 }
450
John Reck35e9dd62011-04-25 09:01:54 -0700451 public void resetDefaultPreferences() {
452 mPrefs.edit().clear().apply();
453 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700454 }
455
John Reck35e9dd62011-04-25 09:01:54 -0700456 public AutoFillProfile getAutoFillProfile() {
457 mAutofillHandler.waitForLoad();
458 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800459 }
460
John Reck35e9dd62011-04-25 09:01:54 -0700461 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
462 mAutofillHandler.waitForLoad();
463 mAutofillHandler.setAutoFillProfile(profile, msg);
Ben Murdoch273330a2011-07-08 18:37:22 +0100464 // Auto-fill will reuse the same profile ID when making edits to the profile,
465 // so we need to force a settings sync (otherwise the SharedPreferences
466 // manager will optimise out the call to onSharedPreferenceChanged(), as
467 // it thinks nothing has changed).
468 syncManagedSettings();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800469 }
470
John Reck35e9dd62011-04-25 09:01:54 -0700471 public void toggleDebugSettings() {
472 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800473 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100474
John Reckb8b2af82011-05-20 15:58:33 -0700475 public boolean hasDesktopUseragent(WebView view) {
476 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
477 }
478
479 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700480 if (view == null) {
481 return;
482 }
John Reckb8b2af82011-05-20 15:58:33 -0700483 WebSettings settings = view.getSettings();
484 if (mCustomUserAgents.get(settings) != null) {
485 mCustomUserAgents.remove(settings);
486 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
487 } else {
488 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
489 settings.setUserAgentString(DESKTOP_USERAGENT);
490 }
491 }
492
John Reck7dc444b2011-06-16 17:44:29 -0700493 public static int getAdjustedMinimumFontSize(int rawValue) {
494 rawValue++; // Preference starts at 0, min font at 1
495 if (rawValue > 1) {
496 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
497 }
498 return rawValue;
499 }
500
501 public static int getAdjustedTextZoom(int rawValue) {
502 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
503 return rawValue + 100;
504 }
505
506 static int getRawTextZoom(int percent) {
507 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
508 }
509
John Reck35e9dd62011-04-25 09:01:54 -0700510 // -----------------------------
511 // getter/setters for accessibility_preferences.xml
512 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100513
John Reck7dc444b2011-06-16 17:44:29 -0700514 @Deprecated
515 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700516 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
517 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100518 }
519
John Reck35e9dd62011-04-25 09:01:54 -0700520 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700521 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700522 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100523 }
524
John Reck92f25f82011-04-26 16:57:10 -0700525 public boolean forceEnableUserScalable() {
526 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
527 }
528
John Reck7dc444b2011-06-16 17:44:29 -0700529 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700530 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700531 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
532 return getAdjustedTextZoom(textZoom);
533 }
534
535 public void setTextZoom(int percent) {
536 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
537 }
538
John Reck35e9dd62011-04-25 09:01:54 -0700539 // -----------------------------
540 // getter/setters for advanced_preferences.xml
541 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100542
John Reck35e9dd62011-04-25 09:01:54 -0700543 public String getSearchEngineName() {
544 return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100545 }
John Reck63bb6872010-12-01 19:29:32 -0800546
John Reck35e9dd62011-04-25 09:01:54 -0700547 public boolean openInBackground() {
548 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800549 }
John Reck35e9dd62011-04-25 09:01:54 -0700550
551 public boolean enableJavascript() {
552 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
553 }
554
555 // TODO: Cache
556 public PluginState getPluginState() {
557 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
558 return PluginState.valueOf(state);
559 }
560
561 // TODO: Cache
562 public ZoomDensity getDefaultZoom() {
563 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
564 return ZoomDensity.valueOf(zoom);
565 }
566
567 public boolean loadPageInOverviewMode() {
568 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
569 }
570
571 public boolean autofitPages() {
572 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
573 }
574
575 public boolean blockPopupWindows() {
576 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
577 }
578
579 public boolean loadImages() {
580 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
581 }
582
583 public String getDefaultTextEncoding() {
584 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
585 }
586
587 // -----------------------------
588 // getter/setters for general_preferences.xml
589 // -----------------------------
590
591 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700592 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
593 }
594
595 public void setHomePage(String value) {
596 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
597 }
598
599 public boolean isAutofillEnabled() {
600 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
601 }
602
603 public void setAutofillEnabled(boolean value) {
604 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
605 }
606
607 // -----------------------------
608 // getter/setters for debug_preferences.xml
609 // -----------------------------
610
611 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700612 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700613 return true;
614 }
615 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
616 }
617
618 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700619 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700620 return 0;
621 }
622 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
623 }
624
625 // -----------------------------
626 // getter/setters for hidden_debug_preferences.xml
627 // -----------------------------
628
629 public boolean enableVisualIndicator() {
630 if (!isDebugEnabled()) {
631 return false;
632 }
633 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
634 }
635
636 public boolean enableJavascriptConsole() {
637 if (!isDebugEnabled()) {
638 return false;
639 }
640 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
641 }
642
643 public boolean isSmallScreen() {
644 if (!isDebugEnabled()) {
645 return false;
646 }
647 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
648 }
649
650 public boolean isWideViewport() {
651 if (!isDebugEnabled()) {
652 return true;
653 }
654 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
655 }
656
657 public boolean isNormalLayout() {
658 if (!isDebugEnabled()) {
659 return false;
660 }
661 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
662 }
663
664 public boolean isTracing() {
665 if (!isDebugEnabled()) {
666 return false;
667 }
668 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
669 }
670
671 public boolean enableLightTouch() {
672 if (!isDebugEnabled()) {
673 return false;
674 }
675 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
676 }
677
678 public boolean enableNavDump() {
679 if (!isDebugEnabled()) {
680 return false;
681 }
682 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
683 }
684
685 public String getJsEngineFlags() {
686 if (!isDebugEnabled()) {
687 return "";
688 }
689 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
690 }
691
692 // -----------------------------
693 // getter/setters for lab_preferences.xml
694 // -----------------------------
695
696 public boolean useQuickControls() {
697 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
698 }
699
700 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700701 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700702 }
703
704 public boolean useInstantSearch() {
705 return mPrefs.getBoolean(PREF_USE_INSTANT_SEARCH, false);
706 }
707
Michael Kolbc38c6042011-04-27 10:46:06 -0700708 public boolean useFullscreen() {
709 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
710 }
711
John Reck2fd9d0e2011-07-15 11:13:48 -0700712 public boolean useInvertedRendering() {
713 return mPrefs.getBoolean(PREF_INVERTED, false);
714 }
715
John Reck35e9dd62011-04-25 09:01:54 -0700716 // -----------------------------
717 // getter/setters for privacy_security_preferences.xml
718 // -----------------------------
719
720 public boolean showSecurityWarnings() {
721 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
722 }
723
724 public boolean acceptCookies() {
725 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
726 }
727
728 public boolean saveFormdata() {
729 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
730 }
731
732 public boolean enableGeolocation() {
733 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
734 }
735
736 public boolean rememberPasswords() {
737 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
738 }
739
Michael Kolb14612442011-06-24 13:06:29 -0700740 // -----------------------------
741 // getter/setters for bandwidth_preferences.xml
742 // -----------------------------
743
744 public boolean isPreloadEnabled() {
745 return mPrefs.getBoolean(PREF_DATA_PRELOAD, false);
746 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800747}