blob: 2023ee61eab335a491e0fcac7200622ebab83d5e [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;
John Reckf57c0292011-07-21 18:15:39 -070025import android.os.Looper;
Ben Murdoch23da30e2010-10-26 15:18:44 +010026import android.os.Message;
Ben Murdoch0cb81892010-10-08 12:41:33 +010027import android.preference.PreferenceManager;
Ben Murdoch0cb81892010-10-08 12:41:33 +010028import android.provider.Browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010030import android.webkit.GeolocationPermissions;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.webkit.WebIconDatabase;
32import android.webkit.WebSettings;
Ben Murdoch0cb81892010-10-08 12:41:33 +010033import android.webkit.WebSettings.AutoFillProfile;
John Reck35e9dd62011-04-25 09:01:54 -070034import android.webkit.WebSettings.LayoutAlgorithm;
35import android.webkit.WebSettings.PluginState;
36import android.webkit.WebSettings.TextSize;
37import android.webkit.WebSettings.ZoomDensity;
Nicolas Roard78a98e42009-05-11 13:34:17 +010038import android.webkit.WebStorage;
John Reck812d2d62011-01-18 14:16:15 -080039import android.webkit.WebView;
40import android.webkit.WebViewDatabase;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041
John Reck46500332011-06-07 14:36:10 -070042import com.android.browser.homepages.HomeProvider;
43import com.android.browser.provider.BrowserProvider;
44import com.android.browser.search.SearchEngine;
45import com.android.browser.search.SearchEngines;
46
John Reck35e9dd62011-04-25 09:01:54 -070047import java.lang.ref.WeakReference;
48import java.util.Iterator;
49import java.util.LinkedList;
John Reckb8b2af82011-05-20 15:58:33 -070050import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051
John Reck35e9dd62011-04-25 09:01:54 -070052/**
53 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080054 */
John Reck35e9dd62011-04-25 09:01:54 -070055public class BrowserSettings implements OnSharedPreferenceChangeListener,
56 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010057
John Reck35e9dd62011-04-25 09:01:54 -070058 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070059 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
60 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
61 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080062
63 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070064 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
65 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070066
67 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070068 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
69 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070070
71 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070072 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
73 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080074
John Reck7fd1e8f2011-04-27 18:22:57 -070075 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
76 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
77 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
78
John Reck35e9dd62011-04-25 09:01:54 -070079 private static final String USER_AGENTS[] = { null,
80 DESKTOP_USERAGENT,
81 IPHONE_USERAGENT,
82 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070083 FROYO_USERAGENT,
84 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070085 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080086
John Reck8fc22a12011-06-16 14:57:41 -070087 // The minimum min font size
88 // Aka, the lower bounds for the min font size range
89 // which is 1:5..24
90 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070091 // The initial value in the text zoom range
92 // This is what represents 100% in the SeekBarPreference range
93 private static final int TEXT_ZOOM_START_VAL = 10;
94 // The size of a single step in the text zoom range, in percent
95 private static final int TEXT_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -070096
John Reck35e9dd62011-04-25 09:01:54 -070097 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -070098
John Reck35e9dd62011-04-25 09:01:54 -070099 private Context mContext;
100 private SharedPreferences mPrefs;
101 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700102 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700103 private WebStorageSizeManager mWebStorageSizeManager;
104 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700105 private WeakHashMap<WebSettings, String> mCustomUserAgents;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100106 private static boolean sInitialized = false;
John Reckf57c0292011-07-21 18:15:39 -0700107 // Looper shared between some lightweight background operations
108 // Specifically, this is created on the thread that initializes browser settings
109 // and is then reused by CrashRecoveryHandler
110 private Looper mBackgroundLooper;
John Reck78a6a1d2011-07-21 13:54:03 -0700111
112 // Cached values
113 private int mPageCacheCapacity = 1;
114 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800115
John Reck35e9dd62011-04-25 09:01:54 -0700116 // Cached settings
117 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118
Ben Murdochaaa1f372011-07-25 15:40:58 +0100119 private static String sFactoryResetUrl;
120
John Reck35e9dd62011-04-25 09:01:54 -0700121 public static void initialize(final Context context) {
122 sInstance = new BrowserSettings(context);
123 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124
John Reck35e9dd62011-04-25 09:01:54 -0700125 public static BrowserSettings getInstance() {
126 return sInstance;
127 }
Ben Murdochef671652010-11-25 17:17:58 +0000128
John Reck35e9dd62011-04-25 09:01:54 -0700129 private BrowserSettings(Context context) {
130 mContext = context;
131 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
132 mAutofillHandler = new AutofillHandler(mContext);
133 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700134 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reck35e9dd62011-04-25 09:01:54 -0700135 mPrefs.registerOnSharedPreferenceChangeListener(this);
136 mAutofillHandler.asyncLoadFromDb();
John Reckf57c0292011-07-21 18:15:39 -0700137 new Thread(mSetupAndLoop, "BackgroundLooper").start();
John Reck35e9dd62011-04-25 09:01:54 -0700138 }
139
140 public void setController(Controller controller) {
141 mController = controller;
142 syncSharedSettings();
143
144 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
145 ((InstantSearchEngine) mSearchEngine).setController(mController);
Ben Murdochef671652010-11-25 17:17:58 +0000146 }
147 }
148
John Reckf57c0292011-07-21 18:15:39 -0700149 public Looper getBackgroundLooper() {
150 requireInitialization();
151 return mBackgroundLooper;
152 }
153
John Reck35e9dd62011-04-25 09:01:54 -0700154 public void startManagingSettings(WebSettings settings) {
155 synchronized (mManagedSettings) {
156 syncStaticSettings(settings);
157 syncSetting(settings);
158 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800159 }
160 }
161
John Reckf57c0292011-07-21 18:15:39 -0700162 private Runnable mSetupAndLoop = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700163
164 @Override
165 public void run() {
166 // the cost of one cached page is ~3M (measured using nytimes.com). For
167 // low end devices, we only cache one page. For high end devices, we try
168 // to cache more pages, currently choose 5.
169 if (ActivityManager.staticGetMemoryClass() > 16) {
170 mPageCacheCapacity = 5;
171 }
172 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
173 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
174 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
175 if (Build.VERSION.CODENAME.equals("REL")) {
176 // This is a release build, always startup with debug disabled
177 setDebugEnabled(false);
178 }
179 if (mPrefs.contains(PREF_TEXT_SIZE)) {
180 /*
181 * Update from TextSize enum to zoom percent
182 * SMALLEST is 50%
183 * SMALLER is 75%
184 * NORMAL is 100%
185 * LARGER is 150%
186 * LARGEST is 200%
187 */
188 switch (getTextSize()) {
189 case SMALLEST:
190 setTextZoom(50);
191 break;
192 case SMALLER:
193 setTextZoom(75);
194 break;
195 case LARGER:
196 setTextZoom(150);
197 break;
198 case LARGEST:
199 setTextZoom(200);
200 break;
201 }
202 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
203 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100204
205 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
206 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
207 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
208 BrowserProvider.getClientId(mContext.getContentResolver()));
209 }
210
John Reckf57c0292011-07-21 18:15:39 -0700211 Looper.prepare();
212 mBackgroundLooper = Looper.myLooper();
Ben Murdochaaa1f372011-07-25 15:40:58 +0100213 synchronized (BrowserSettings.class) {
214 sInitialized = true;
215 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700216 }
John Reckf57c0292011-07-21 18:15:39 -0700217 Looper.loop();
John Reck78a6a1d2011-07-21 13:54:03 -0700218 }
219 };
220
Ben Murdochaaa1f372011-07-25 15:40:58 +0100221 private static void requireInitialization() {
222 synchronized (BrowserSettings.class) {
223 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700224 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100225 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700226 } catch (InterruptedException e) {
227 }
228 }
229 }
230 }
231
The Android Open Source Project0c908882009-03-03 19:32:16 -0800232 /**
John Reck35e9dd62011-04-25 09:01:54 -0700233 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800234 */
John Reck35e9dd62011-04-25 09:01:54 -0700235 private void syncSetting(WebSettings settings) {
236 settings.setGeolocationEnabled(enableGeolocation());
237 settings.setJavaScriptEnabled(enableJavascript());
238 settings.setLightTouchEnabled(enableLightTouch());
239 settings.setNavDump(enableNavDump());
240 settings.setShowVisualIndicator(enableVisualIndicator());
241 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
242 settings.setDefaultZoom(getDefaultZoom());
243 settings.setMinimumFontSize(getMinimumFontSize());
244 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck92f25f82011-04-26 16:57:10 -0700245 settings.setForceUserScalable(forceEnableUserScalable());
John Reck35e9dd62011-04-25 09:01:54 -0700246 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700247 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700248 settings.setAutoFillEnabled(isAutofillEnabled());
249 settings.setLayoutAlgorithm(getLayoutAlgorithm());
250 settings.setJavaScriptCanOpenWindowsAutomatically(blockPopupWindows());
251 settings.setLoadsImagesAutomatically(loadImages());
252 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
253 settings.setSavePassword(rememberPasswords());
254 settings.setSaveFormData(saveFormdata());
255 settings.setUseWideViewPort(isWideViewport());
256 settings.setAutoFillProfile(getAutoFillProfile());
John Reckb8b2af82011-05-20 15:58:33 -0700257
258 String ua = mCustomUserAgents.get(settings);
John Reck46500332011-06-07 14:36:10 -0700259 if (ua != null) {
John Reckb8b2af82011-05-20 15:58:33 -0700260 settings.setUserAgentString(ua);
261 } else {
262 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
263 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700264
265 settings.setProperty("gfxInvertedScreen",
266 useInvertedRendering() ? "true" : "false");
Ben Murdochef671652010-11-25 17:17:58 +0000267 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800268
John Reck35e9dd62011-04-25 09:01:54 -0700269 /**
270 * Syncs all the settings that have no UI
271 * These cannot change, so we only need to set them once per WebSettings
272 */
273 private void syncStaticSettings(WebSettings settings) {
274 settings.setDefaultFontSize(16);
275 settings.setDefaultFixedFontSize(13);
276 settings.setPageCacheCapacity(getPageCacheCapacity());
Ben Murdochef671652010-11-25 17:17:58 +0000277
John Reck35e9dd62011-04-25 09:01:54 -0700278 // WebView inside Browser doesn't want initial focus to be set.
279 settings.setNeedInitialFocus(false);
280 // Browser supports multiple windows
281 settings.setSupportMultipleWindows(true);
282 // enable smooth transition for better performance during panning or
283 // zooming
284 settings.setEnableSmoothTransition(true);
285 // disable content url access
286 settings.setAllowContentAccess(false);
287
288 // HTML5 API flags
289 settings.setAppCacheEnabled(true);
290 settings.setDatabaseEnabled(true);
291 settings.setDomStorageEnabled(true);
292 settings.setWorkersEnabled(true); // This only affects V8.
293
294 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700295 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700296 settings.setAppCachePath(getAppCachePath());
297 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
298 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
299 }
300
301 private void syncSharedSettings() {
302 CookieManager.getInstance().setAcceptCookie(acceptCookies());
303 if (mController != null) {
304 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700305 }
John Reck35e9dd62011-04-25 09:01:54 -0700306 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700307
John Reck35e9dd62011-04-25 09:01:54 -0700308 private void syncManagedSettings() {
309 syncSharedSettings();
310 synchronized (mManagedSettings) {
311 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
312 while (iter.hasNext()) {
313 WeakReference<WebSettings> ref = iter.next();
314 WebSettings settings = ref.get();
315 if (settings == null) {
316 iter.remove();
317 continue;
318 }
319 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000320 }
John Reck35e9dd62011-04-25 09:01:54 -0700321 }
322 }
Ben Murdochef671652010-11-25 17:17:58 +0000323
John Reck35e9dd62011-04-25 09:01:54 -0700324 @Override
325 public void onSharedPreferenceChanged(
326 SharedPreferences sharedPreferences, String key) {
327 syncManagedSettings();
328 if (PREF_SEARCH_ENGINE.equals(key)) {
329 updateSearchEngine(false);
330 }
331 if (PREF_USE_INSTANT_SEARCH.equals(key)) {
332 updateSearchEngine(true);
333 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700334 if (PREF_FULLSCREEN.equals(key)) {
335 if (mController.getUi() != null) {
336 mController.getUi().setFullscreen(useFullscreen());
337 }
Michael Kolb0241e752011-07-07 14:58:50 -0700338 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
339 if (mController.getUi() != null) {
340 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
341 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700342 }
John Reck35e9dd62011-04-25 09:01:54 -0700343 }
344
John Reck961d35d2011-06-23 09:45:54 -0700345 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100346 requireInitialization();
347 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700348 }
349
350 public LayoutAlgorithm getLayoutAlgorithm() {
351 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
352 if (autofitPages()) {
353 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
354 }
355 if (isDebugEnabled()) {
356 if (isSmallScreen()) {
357 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000358 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700359 if (isNormalLayout()) {
360 layoutAlgorithm = LayoutAlgorithm.NORMAL;
361 } else {
362 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
363 }
Ben Murdochef671652010-11-25 17:17:58 +0000364 }
John Reck35e9dd62011-04-25 09:01:54 -0700365 }
366 return layoutAlgorithm;
367 }
Ben Murdochef671652010-11-25 17:17:58 +0000368
John Reck35e9dd62011-04-25 09:01:54 -0700369 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700370 requireInitialization();
371 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800372 }
373
John Reck35e9dd62011-04-25 09:01:54 -0700374 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700375 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700376 return mWebStorageSizeManager;
377 }
378
John Reck35e9dd62011-04-25 09:01:54 -0700379 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700380 if (mAppCachePath == null) {
381 mAppCachePath = mContext.getDir("appcache", 0).getPath();
382 }
383 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700384 }
385
386 private void updateSearchEngine(boolean force) {
387 String searchEngineName = getSearchEngineName();
388 if (force || mSearchEngine == null ||
389 !mSearchEngine.getName().equals(searchEngineName)) {
390 if (mSearchEngine != null) {
391 if (mSearchEngine.supportsVoiceSearch()) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000392 // One or more tabs could have been in voice search mode.
393 // Clear it, since the new SearchEngine may not support
394 // it, or may handle it differently.
395 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
396 mController.getTabControl().getTab(i).revertVoiceSearchMode();
397 }
398 }
John Reck35e9dd62011-04-25 09:01:54 -0700399 mSearchEngine.close();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000400 }
John Reck35e9dd62011-04-25 09:01:54 -0700401 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000402
John Reck35e9dd62011-04-25 09:01:54 -0700403 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
404 ((InstantSearchEngine) mSearchEngine).setController(mController);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000405 }
406 }
407 }
408
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100409 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700410 if (mSearchEngine == null) {
411 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100412 }
John Reck35e9dd62011-04-25 09:01:54 -0700413 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100414 }
415
John Reck35e9dd62011-04-25 09:01:54 -0700416 public boolean isDebugEnabled() {
John Reck78a6a1d2011-07-21 13:54:03 -0700417 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700418 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100419 }
420
John Reck35e9dd62011-04-25 09:01:54 -0700421 public void setDebugEnabled(boolean value) {
422 mPrefs.edit().putBoolean(PREF_DEBUG_MENU, value).apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100423 }
424
John Reck35e9dd62011-04-25 09:01:54 -0700425 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800426 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700427 if (mController != null) {
428 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800429 if (current != null) {
430 current.clearCache(true);
431 }
432 }
433 }
434
John Reck35e9dd62011-04-25 09:01:54 -0700435 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800436 CookieManager.getInstance().removeAllCookie();
437 }
438
John Reck35e9dd62011-04-25 09:01:54 -0700439 public void clearHistory() {
440 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800441 Browser.clearHistory(resolver);
442 Browser.clearSearches(resolver);
443 }
444
John Reck35e9dd62011-04-25 09:01:54 -0700445 public void clearFormData() {
446 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700447 if (mController!= null) {
448 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100449 if (currentTopView != null) {
450 currentTopView.clearFormData();
451 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800452 }
453 }
454
John Reck35e9dd62011-04-25 09:01:54 -0700455 public void clearPasswords() {
456 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800457 db.clearUsernamePassword();
458 db.clearHttpAuthUsernamePassword();
459 }
460
John Reck35e9dd62011-04-25 09:01:54 -0700461 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100462 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100463 }
464
John Reck35e9dd62011-04-25 09:01:54 -0700465 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100466 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100467 }
468
John Reck35e9dd62011-04-25 09:01:54 -0700469 public void resetDefaultPreferences() {
470 mPrefs.edit().clear().apply();
471 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700472 }
473
John Reck35e9dd62011-04-25 09:01:54 -0700474 public AutoFillProfile getAutoFillProfile() {
475 mAutofillHandler.waitForLoad();
476 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800477 }
478
John Reck35e9dd62011-04-25 09:01:54 -0700479 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
480 mAutofillHandler.waitForLoad();
481 mAutofillHandler.setAutoFillProfile(profile, msg);
Ben Murdoch273330a2011-07-08 18:37:22 +0100482 // Auto-fill will reuse the same profile ID when making edits to the profile,
483 // so we need to force a settings sync (otherwise the SharedPreferences
484 // manager will optimise out the call to onSharedPreferenceChanged(), as
485 // it thinks nothing has changed).
486 syncManagedSettings();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800487 }
488
John Reck35e9dd62011-04-25 09:01:54 -0700489 public void toggleDebugSettings() {
490 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800491 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100492
John Reckb8b2af82011-05-20 15:58:33 -0700493 public boolean hasDesktopUseragent(WebView view) {
494 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
495 }
496
497 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700498 if (view == null) {
499 return;
500 }
John Reckb8b2af82011-05-20 15:58:33 -0700501 WebSettings settings = view.getSettings();
502 if (mCustomUserAgents.get(settings) != null) {
503 mCustomUserAgents.remove(settings);
504 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
505 } else {
506 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
507 settings.setUserAgentString(DESKTOP_USERAGENT);
508 }
509 }
510
John Reck7dc444b2011-06-16 17:44:29 -0700511 public static int getAdjustedMinimumFontSize(int rawValue) {
512 rawValue++; // Preference starts at 0, min font at 1
513 if (rawValue > 1) {
514 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
515 }
516 return rawValue;
517 }
518
519 public static int getAdjustedTextZoom(int rawValue) {
520 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
521 return rawValue + 100;
522 }
523
524 static int getRawTextZoom(int percent) {
525 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
526 }
527
John Reck35e9dd62011-04-25 09:01:54 -0700528 // -----------------------------
529 // getter/setters for accessibility_preferences.xml
530 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100531
John Reck7dc444b2011-06-16 17:44:29 -0700532 @Deprecated
533 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700534 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
535 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100536 }
537
John Reck35e9dd62011-04-25 09:01:54 -0700538 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700539 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700540 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100541 }
542
John Reck92f25f82011-04-26 16:57:10 -0700543 public boolean forceEnableUserScalable() {
544 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
545 }
546
John Reck7dc444b2011-06-16 17:44:29 -0700547 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700548 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700549 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
550 return getAdjustedTextZoom(textZoom);
551 }
552
553 public void setTextZoom(int percent) {
554 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
555 }
556
John Reck35e9dd62011-04-25 09:01:54 -0700557 // -----------------------------
558 // getter/setters for advanced_preferences.xml
559 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100560
John Reck35e9dd62011-04-25 09:01:54 -0700561 public String getSearchEngineName() {
562 return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100563 }
John Reck63bb6872010-12-01 19:29:32 -0800564
John Reck35e9dd62011-04-25 09:01:54 -0700565 public boolean openInBackground() {
566 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800567 }
John Reck35e9dd62011-04-25 09:01:54 -0700568
569 public boolean enableJavascript() {
570 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
571 }
572
573 // TODO: Cache
574 public PluginState getPluginState() {
575 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
576 return PluginState.valueOf(state);
577 }
578
579 // TODO: Cache
580 public ZoomDensity getDefaultZoom() {
581 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
582 return ZoomDensity.valueOf(zoom);
583 }
584
585 public boolean loadPageInOverviewMode() {
586 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
587 }
588
589 public boolean autofitPages() {
590 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
591 }
592
593 public boolean blockPopupWindows() {
594 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
595 }
596
597 public boolean loadImages() {
598 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
599 }
600
601 public String getDefaultTextEncoding() {
602 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
603 }
604
605 // -----------------------------
606 // getter/setters for general_preferences.xml
607 // -----------------------------
608
609 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700610 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
611 }
612
613 public void setHomePage(String value) {
614 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
615 }
616
617 public boolean isAutofillEnabled() {
618 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
619 }
620
621 public void setAutofillEnabled(boolean value) {
622 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
623 }
624
625 // -----------------------------
626 // getter/setters for debug_preferences.xml
627 // -----------------------------
628
629 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700630 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700631 return true;
632 }
633 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
634 }
635
636 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700637 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700638 return 0;
639 }
640 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
641 }
642
643 // -----------------------------
644 // getter/setters for hidden_debug_preferences.xml
645 // -----------------------------
646
647 public boolean enableVisualIndicator() {
648 if (!isDebugEnabled()) {
649 return false;
650 }
651 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
652 }
653
654 public boolean enableJavascriptConsole() {
655 if (!isDebugEnabled()) {
656 return false;
657 }
658 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
659 }
660
661 public boolean isSmallScreen() {
662 if (!isDebugEnabled()) {
663 return false;
664 }
665 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
666 }
667
668 public boolean isWideViewport() {
669 if (!isDebugEnabled()) {
670 return true;
671 }
672 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
673 }
674
675 public boolean isNormalLayout() {
676 if (!isDebugEnabled()) {
677 return false;
678 }
679 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
680 }
681
682 public boolean isTracing() {
683 if (!isDebugEnabled()) {
684 return false;
685 }
686 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
687 }
688
689 public boolean enableLightTouch() {
690 if (!isDebugEnabled()) {
691 return false;
692 }
693 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
694 }
695
696 public boolean enableNavDump() {
697 if (!isDebugEnabled()) {
698 return false;
699 }
700 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
701 }
702
703 public String getJsEngineFlags() {
704 if (!isDebugEnabled()) {
705 return "";
706 }
707 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
708 }
709
710 // -----------------------------
711 // getter/setters for lab_preferences.xml
712 // -----------------------------
713
714 public boolean useQuickControls() {
715 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
716 }
717
718 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700719 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700720 }
721
722 public boolean useInstantSearch() {
723 return mPrefs.getBoolean(PREF_USE_INSTANT_SEARCH, false);
724 }
725
Michael Kolbc38c6042011-04-27 10:46:06 -0700726 public boolean useFullscreen() {
727 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
728 }
729
John Reck2fd9d0e2011-07-15 11:13:48 -0700730 public boolean useInvertedRendering() {
731 return mPrefs.getBoolean(PREF_INVERTED, false);
732 }
733
John Reck35e9dd62011-04-25 09:01:54 -0700734 // -----------------------------
735 // getter/setters for privacy_security_preferences.xml
736 // -----------------------------
737
738 public boolean showSecurityWarnings() {
739 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
740 }
741
742 public boolean acceptCookies() {
743 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
744 }
745
746 public boolean saveFormdata() {
747 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
748 }
749
750 public boolean enableGeolocation() {
751 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
752 }
753
754 public boolean rememberPasswords() {
755 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
756 }
757
Michael Kolb14612442011-06-24 13:06:29 -0700758 // -----------------------------
759 // getter/setters for bandwidth_preferences.xml
760 // -----------------------------
761
762 public boolean isPreloadEnabled() {
763 return mPrefs.getBoolean(PREF_DATA_PRELOAD, false);
764 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800765}