blob: d4f1ba33471ed8ac4a9dda779c19c05197bfa502 [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;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100105 private static boolean sInitialized = false;
John Reck78a6a1d2011-07-21 13:54:03 -0700106
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
Ben Murdochaaa1f372011-07-25 15:40:58 +0100114 private static String sFactoryResetUrl;
115
John Reck35e9dd62011-04-25 09:01:54 -0700116 public static void initialize(final Context context) {
117 sInstance = new BrowserSettings(context);
118 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119
John Reck35e9dd62011-04-25 09:01:54 -0700120 public static BrowserSettings getInstance() {
121 return sInstance;
122 }
Ben Murdochef671652010-11-25 17:17:58 +0000123
John Reck35e9dd62011-04-25 09:01:54 -0700124 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100125 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700126 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
127 mAutofillHandler = new AutofillHandler(mContext);
128 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700129 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reck35e9dd62011-04-25 09:01:54 -0700130 mPrefs.registerOnSharedPreferenceChangeListener(this);
131 mAutofillHandler.asyncLoadFromDb();
John Reckcadae722011-07-25 11:36:17 -0700132 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700133 }
134
135 public void setController(Controller controller) {
136 mController = controller;
137 syncSharedSettings();
138
139 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
140 ((InstantSearchEngine) mSearchEngine).setController(mController);
Ben Murdochef671652010-11-25 17:17:58 +0000141 }
142 }
143
John Reck35e9dd62011-04-25 09:01:54 -0700144 public void startManagingSettings(WebSettings settings) {
145 synchronized (mManagedSettings) {
146 syncStaticSettings(settings);
147 syncSetting(settings);
148 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800149 }
150 }
151
John Reckcadae722011-07-25 11:36:17 -0700152 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700153
154 @Override
155 public void run() {
156 // the cost of one cached page is ~3M (measured using nytimes.com). For
157 // low end devices, we only cache one page. For high end devices, we try
158 // to cache more pages, currently choose 5.
159 if (ActivityManager.staticGetMemoryClass() > 16) {
160 mPageCacheCapacity = 5;
161 }
162 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
163 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
164 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
165 if (Build.VERSION.CODENAME.equals("REL")) {
166 // This is a release build, always startup with debug disabled
167 setDebugEnabled(false);
168 }
169 if (mPrefs.contains(PREF_TEXT_SIZE)) {
170 /*
171 * Update from TextSize enum to zoom percent
172 * SMALLEST is 50%
173 * SMALLER is 75%
174 * NORMAL is 100%
175 * LARGER is 150%
176 * LARGEST is 200%
177 */
178 switch (getTextSize()) {
179 case SMALLEST:
180 setTextZoom(50);
181 break;
182 case SMALLER:
183 setTextZoom(75);
184 break;
185 case LARGER:
186 setTextZoom(150);
187 break;
188 case LARGEST:
189 setTextZoom(200);
190 break;
191 }
192 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
193 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100194
195 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
196 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
197 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
198 BrowserProvider.getClientId(mContext.getContentResolver()));
199 }
200
Ben Murdochaaa1f372011-07-25 15:40:58 +0100201 synchronized (BrowserSettings.class) {
202 sInitialized = true;
203 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700204 }
205 }
206 };
207
Ben Murdochaaa1f372011-07-25 15:40:58 +0100208 private static void requireInitialization() {
209 synchronized (BrowserSettings.class) {
210 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700211 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100212 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700213 } catch (InterruptedException e) {
214 }
215 }
216 }
217 }
218
The Android Open Source Project0c908882009-03-03 19:32:16 -0800219 /**
John Reck35e9dd62011-04-25 09:01:54 -0700220 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800221 */
John Reck35e9dd62011-04-25 09:01:54 -0700222 private void syncSetting(WebSettings settings) {
223 settings.setGeolocationEnabled(enableGeolocation());
224 settings.setJavaScriptEnabled(enableJavascript());
225 settings.setLightTouchEnabled(enableLightTouch());
226 settings.setNavDump(enableNavDump());
Derek Sollenberger31adf672011-07-08 11:31:30 -0400227 settings.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
John Reck35e9dd62011-04-25 09:01:54 -0700228 settings.setShowVisualIndicator(enableVisualIndicator());
229 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
230 settings.setDefaultZoom(getDefaultZoom());
231 settings.setMinimumFontSize(getMinimumFontSize());
232 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck92f25f82011-04-26 16:57:10 -0700233 settings.setForceUserScalable(forceEnableUserScalable());
John Reck35e9dd62011-04-25 09:01:54 -0700234 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700235 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700236 settings.setAutoFillEnabled(isAutofillEnabled());
237 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100238 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700239 settings.setLoadsImagesAutomatically(loadImages());
240 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
241 settings.setSavePassword(rememberPasswords());
242 settings.setSaveFormData(saveFormdata());
243 settings.setUseWideViewPort(isWideViewport());
244 settings.setAutoFillProfile(getAutoFillProfile());
John Reckb8b2af82011-05-20 15:58:33 -0700245
246 String ua = mCustomUserAgents.get(settings);
John Reck46500332011-06-07 14:36:10 -0700247 if (ua != null) {
John Reckb8b2af82011-05-20 15:58:33 -0700248 settings.setUserAgentString(ua);
249 } else {
250 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
251 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700252
John Reckeabe5da2011-08-08 13:24:13 -0700253 settings.setProperty(WebViewProperties.gfxInvertedScreen,
John Reck2fd9d0e2011-07-15 11:13:48 -0700254 useInvertedRendering() ? "true" : "false");
Nicolas Roard5d513102011-08-03 15:35:34 -0700255
John Reckeabe5da2011-08-08 13:24:13 -0700256 settings.setProperty(WebViewProperties.gfxInvertedScreenContrast,
Nicolas Roard5d513102011-08-03 15:35:34 -0700257 Float.toString(getInvertedContrast()));
Ben Murdochef671652010-11-25 17:17:58 +0000258 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800259
John Reck35e9dd62011-04-25 09:01:54 -0700260 /**
261 * Syncs all the settings that have no UI
262 * These cannot change, so we only need to set them once per WebSettings
263 */
264 private void syncStaticSettings(WebSettings settings) {
265 settings.setDefaultFontSize(16);
266 settings.setDefaultFixedFontSize(13);
267 settings.setPageCacheCapacity(getPageCacheCapacity());
Ben Murdochef671652010-11-25 17:17:58 +0000268
John Reck35e9dd62011-04-25 09:01:54 -0700269 // WebView inside Browser doesn't want initial focus to be set.
270 settings.setNeedInitialFocus(false);
271 // Browser supports multiple windows
272 settings.setSupportMultipleWindows(true);
273 // enable smooth transition for better performance during panning or
274 // zooming
275 settings.setEnableSmoothTransition(true);
276 // disable content url access
277 settings.setAllowContentAccess(false);
278
279 // HTML5 API flags
280 settings.setAppCacheEnabled(true);
281 settings.setDatabaseEnabled(true);
282 settings.setDomStorageEnabled(true);
283 settings.setWorkersEnabled(true); // This only affects V8.
284
285 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700286 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700287 settings.setAppCachePath(getAppCachePath());
288 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
289 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
290 }
291
292 private void syncSharedSettings() {
293 CookieManager.getInstance().setAcceptCookie(acceptCookies());
294 if (mController != null) {
295 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700296 }
John Reck35e9dd62011-04-25 09:01:54 -0700297 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700298
John Reck35e9dd62011-04-25 09:01:54 -0700299 private void syncManagedSettings() {
300 syncSharedSettings();
301 synchronized (mManagedSettings) {
302 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
303 while (iter.hasNext()) {
304 WeakReference<WebSettings> ref = iter.next();
305 WebSettings settings = ref.get();
306 if (settings == null) {
307 iter.remove();
308 continue;
309 }
310 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000311 }
John Reck35e9dd62011-04-25 09:01:54 -0700312 }
313 }
Ben Murdochef671652010-11-25 17:17:58 +0000314
John Reck35e9dd62011-04-25 09:01:54 -0700315 @Override
316 public void onSharedPreferenceChanged(
317 SharedPreferences sharedPreferences, String key) {
318 syncManagedSettings();
319 if (PREF_SEARCH_ENGINE.equals(key)) {
320 updateSearchEngine(false);
321 }
322 if (PREF_USE_INSTANT_SEARCH.equals(key)) {
323 updateSearchEngine(true);
324 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700325 if (PREF_FULLSCREEN.equals(key)) {
326 if (mController.getUi() != null) {
327 mController.getUi().setFullscreen(useFullscreen());
328 }
Michael Kolb0241e752011-07-07 14:58:50 -0700329 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
330 if (mController.getUi() != null) {
331 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
332 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700333 }
John Reck35e9dd62011-04-25 09:01:54 -0700334 }
335
John Reck961d35d2011-06-23 09:45:54 -0700336 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100337 requireInitialization();
338 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700339 }
340
341 public LayoutAlgorithm getLayoutAlgorithm() {
342 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
343 if (autofitPages()) {
344 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
345 }
346 if (isDebugEnabled()) {
347 if (isSmallScreen()) {
348 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000349 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700350 if (isNormalLayout()) {
351 layoutAlgorithm = LayoutAlgorithm.NORMAL;
352 } else {
353 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
354 }
Ben Murdochef671652010-11-25 17:17:58 +0000355 }
John Reck35e9dd62011-04-25 09:01:54 -0700356 }
357 return layoutAlgorithm;
358 }
Ben Murdochef671652010-11-25 17:17:58 +0000359
John Reck35e9dd62011-04-25 09:01:54 -0700360 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700361 requireInitialization();
362 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800363 }
364
John Reck35e9dd62011-04-25 09:01:54 -0700365 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700366 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700367 return mWebStorageSizeManager;
368 }
369
John Reck35e9dd62011-04-25 09:01:54 -0700370 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700371 if (mAppCachePath == null) {
372 mAppCachePath = mContext.getDir("appcache", 0).getPath();
373 }
374 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700375 }
376
377 private void updateSearchEngine(boolean force) {
378 String searchEngineName = getSearchEngineName();
379 if (force || mSearchEngine == null ||
380 !mSearchEngine.getName().equals(searchEngineName)) {
381 if (mSearchEngine != null) {
382 if (mSearchEngine.supportsVoiceSearch()) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000383 // One or more tabs could have been in voice search mode.
384 // Clear it, since the new SearchEngine may not support
385 // it, or may handle it differently.
386 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
387 mController.getTabControl().getTab(i).revertVoiceSearchMode();
388 }
389 }
John Reck35e9dd62011-04-25 09:01:54 -0700390 mSearchEngine.close();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000391 }
John Reck35e9dd62011-04-25 09:01:54 -0700392 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000393
John Reck35e9dd62011-04-25 09:01:54 -0700394 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
395 ((InstantSearchEngine) mSearchEngine).setController(mController);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000396 }
397 }
398 }
399
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100400 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700401 if (mSearchEngine == null) {
402 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100403 }
John Reck35e9dd62011-04-25 09:01:54 -0700404 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100405 }
406
John Reck35e9dd62011-04-25 09:01:54 -0700407 public boolean isDebugEnabled() {
408 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100409 }
410
John Reck35e9dd62011-04-25 09:01:54 -0700411 public void setDebugEnabled(boolean value) {
412 mPrefs.edit().putBoolean(PREF_DEBUG_MENU, value).apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100413 }
414
John Reck35e9dd62011-04-25 09:01:54 -0700415 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800416 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700417 if (mController != null) {
418 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800419 if (current != null) {
420 current.clearCache(true);
421 }
422 }
423 }
424
John Reck35e9dd62011-04-25 09:01:54 -0700425 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800426 CookieManager.getInstance().removeAllCookie();
427 }
428
John Reck35e9dd62011-04-25 09:01:54 -0700429 public void clearHistory() {
430 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800431 Browser.clearHistory(resolver);
432 Browser.clearSearches(resolver);
433 }
434
John Reck35e9dd62011-04-25 09:01:54 -0700435 public void clearFormData() {
436 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700437 if (mController!= null) {
438 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100439 if (currentTopView != null) {
440 currentTopView.clearFormData();
441 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800442 }
443 }
444
John Reck35e9dd62011-04-25 09:01:54 -0700445 public void clearPasswords() {
446 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800447 db.clearUsernamePassword();
448 db.clearHttpAuthUsernamePassword();
449 }
450
John Reck35e9dd62011-04-25 09:01:54 -0700451 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100452 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100453 }
454
John Reck35e9dd62011-04-25 09:01:54 -0700455 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100456 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100457 }
458
John Reck35e9dd62011-04-25 09:01:54 -0700459 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700460 // Preserve autologin setting
461 long gal = mPrefs.getLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, -1);
462 mPrefs.edit()
463 .clear()
464 .putLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, gal)
465 .apply();
John Reck35e9dd62011-04-25 09:01:54 -0700466 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700467 }
468
John Reck35e9dd62011-04-25 09:01:54 -0700469 public AutoFillProfile getAutoFillProfile() {
470 mAutofillHandler.waitForLoad();
471 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800472 }
473
John Reck35e9dd62011-04-25 09:01:54 -0700474 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
475 mAutofillHandler.waitForLoad();
476 mAutofillHandler.setAutoFillProfile(profile, msg);
Ben Murdoch273330a2011-07-08 18:37:22 +0100477 // Auto-fill will reuse the same profile ID when making edits to the profile,
478 // so we need to force a settings sync (otherwise the SharedPreferences
479 // manager will optimise out the call to onSharedPreferenceChanged(), as
480 // it thinks nothing has changed).
481 syncManagedSettings();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800482 }
483
John Reck35e9dd62011-04-25 09:01:54 -0700484 public void toggleDebugSettings() {
485 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800486 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100487
John Reckb8b2af82011-05-20 15:58:33 -0700488 public boolean hasDesktopUseragent(WebView view) {
489 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
490 }
491
492 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700493 if (view == null) {
494 return;
495 }
John Reckb8b2af82011-05-20 15:58:33 -0700496 WebSettings settings = view.getSettings();
497 if (mCustomUserAgents.get(settings) != null) {
498 mCustomUserAgents.remove(settings);
499 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
500 } else {
501 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
502 settings.setUserAgentString(DESKTOP_USERAGENT);
503 }
504 }
505
John Reck7dc444b2011-06-16 17:44:29 -0700506 public static int getAdjustedMinimumFontSize(int rawValue) {
507 rawValue++; // Preference starts at 0, min font at 1
508 if (rawValue > 1) {
509 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
510 }
511 return rawValue;
512 }
513
514 public static int getAdjustedTextZoom(int rawValue) {
515 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
516 return rawValue + 100;
517 }
518
519 static int getRawTextZoom(int percent) {
520 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
521 }
522
John Reckcadae722011-07-25 11:36:17 -0700523 public SharedPreferences getPreferences() {
524 return mPrefs;
525 }
526
John Reck35e9dd62011-04-25 09:01:54 -0700527 // -----------------------------
528 // getter/setters for accessibility_preferences.xml
529 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100530
John Reck7dc444b2011-06-16 17:44:29 -0700531 @Deprecated
532 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700533 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
534 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100535 }
536
John Reck35e9dd62011-04-25 09:01:54 -0700537 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700538 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700539 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100540 }
541
John Reck92f25f82011-04-26 16:57:10 -0700542 public boolean forceEnableUserScalable() {
543 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
544 }
545
John Reck7dc444b2011-06-16 17:44:29 -0700546 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700547 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700548 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
549 return getAdjustedTextZoom(textZoom);
550 }
551
552 public void setTextZoom(int percent) {
553 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
554 }
555
John Reck35e9dd62011-04-25 09:01:54 -0700556 // -----------------------------
557 // getter/setters for advanced_preferences.xml
558 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100559
John Reck35e9dd62011-04-25 09:01:54 -0700560 public String getSearchEngineName() {
561 return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100562 }
John Reck63bb6872010-12-01 19:29:32 -0800563
John Reck35e9dd62011-04-25 09:01:54 -0700564 public boolean openInBackground() {
565 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800566 }
John Reck35e9dd62011-04-25 09:01:54 -0700567
568 public boolean enableJavascript() {
569 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
570 }
571
572 // TODO: Cache
573 public PluginState getPluginState() {
574 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
575 return PluginState.valueOf(state);
576 }
577
578 // TODO: Cache
579 public ZoomDensity getDefaultZoom() {
580 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
581 return ZoomDensity.valueOf(zoom);
582 }
583
584 public boolean loadPageInOverviewMode() {
585 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
586 }
587
588 public boolean autofitPages() {
589 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
590 }
591
592 public boolean blockPopupWindows() {
593 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
594 }
595
596 public boolean loadImages() {
597 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
598 }
599
600 public String getDefaultTextEncoding() {
601 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
602 }
603
604 // -----------------------------
605 // getter/setters for general_preferences.xml
606 // -----------------------------
607
608 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700609 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
610 }
611
612 public void setHomePage(String value) {
613 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
614 }
615
616 public boolean isAutofillEnabled() {
617 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
618 }
619
620 public void setAutofillEnabled(boolean value) {
621 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
622 }
623
624 // -----------------------------
625 // getter/setters for debug_preferences.xml
626 // -----------------------------
627
628 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700629 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700630 return true;
631 }
632 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
633 }
634
Derek Sollenberger31adf672011-07-08 11:31:30 -0400635 public boolean isSkiaHardwareAccelerated() {
636 if (!isDebugEnabled()) {
637 return false;
638 }
639 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
640 }
641
John Reck35e9dd62011-04-25 09:01:54 -0700642 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700643 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700644 return 0;
645 }
646 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
647 }
648
649 // -----------------------------
650 // getter/setters for hidden_debug_preferences.xml
651 // -----------------------------
652
653 public boolean enableVisualIndicator() {
654 if (!isDebugEnabled()) {
655 return false;
656 }
657 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
658 }
659
660 public boolean enableJavascriptConsole() {
661 if (!isDebugEnabled()) {
662 return false;
663 }
664 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
665 }
666
667 public boolean isSmallScreen() {
668 if (!isDebugEnabled()) {
669 return false;
670 }
671 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
672 }
673
674 public boolean isWideViewport() {
675 if (!isDebugEnabled()) {
676 return true;
677 }
678 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
679 }
680
681 public boolean isNormalLayout() {
682 if (!isDebugEnabled()) {
683 return false;
684 }
685 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
686 }
687
688 public boolean isTracing() {
689 if (!isDebugEnabled()) {
690 return false;
691 }
692 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
693 }
694
695 public boolean enableLightTouch() {
696 if (!isDebugEnabled()) {
697 return false;
698 }
699 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
700 }
701
702 public boolean enableNavDump() {
703 if (!isDebugEnabled()) {
704 return false;
705 }
706 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
707 }
708
709 public String getJsEngineFlags() {
710 if (!isDebugEnabled()) {
711 return "";
712 }
713 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
714 }
715
716 // -----------------------------
717 // getter/setters for lab_preferences.xml
718 // -----------------------------
719
720 public boolean useQuickControls() {
721 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
722 }
723
724 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700725 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700726 }
727
728 public boolean useInstantSearch() {
729 return mPrefs.getBoolean(PREF_USE_INSTANT_SEARCH, false);
730 }
731
Michael Kolbc38c6042011-04-27 10:46:06 -0700732 public boolean useFullscreen() {
733 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
734 }
735
John Reck2fd9d0e2011-07-15 11:13:48 -0700736 public boolean useInvertedRendering() {
737 return mPrefs.getBoolean(PREF_INVERTED, false);
738 }
739
Nicolas Roard5d513102011-08-03 15:35:34 -0700740 public float getInvertedContrast() {
741 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
742 }
743
John Reck35e9dd62011-04-25 09:01:54 -0700744 // -----------------------------
745 // getter/setters for privacy_security_preferences.xml
746 // -----------------------------
747
748 public boolean showSecurityWarnings() {
749 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
750 }
751
752 public boolean acceptCookies() {
753 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
754 }
755
756 public boolean saveFormdata() {
757 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
758 }
759
760 public boolean enableGeolocation() {
761 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
762 }
763
764 public boolean rememberPasswords() {
765 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
766 }
767
Michael Kolb14612442011-06-24 13:06:29 -0700768 // -----------------------------
769 // getter/setters for bandwidth_preferences.xml
770 // -----------------------------
771
772 public boolean isPreloadEnabled() {
773 return mPrefs.getBoolean(PREF_DATA_PRELOAD, false);
774 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800775}