blob: 11316ffcbc0544a4501c5e7a19e7a866c74119ec [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 Reckc477e712011-08-17 11:27:15 -0700106 private boolean mNeedsSharedSync = true;
John Reck78a6a1d2011-07-21 13:54:03 -0700107
108 // Cached values
109 private int mPageCacheCapacity = 1;
110 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800111
John Reck35e9dd62011-04-25 09:01:54 -0700112 // Cached settings
113 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114
Ben Murdochaaa1f372011-07-25 15:40:58 +0100115 private static String sFactoryResetUrl;
116
John Reck35e9dd62011-04-25 09:01:54 -0700117 public static void initialize(final Context context) {
118 sInstance = new BrowserSettings(context);
119 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800120
John Reck35e9dd62011-04-25 09:01:54 -0700121 public static BrowserSettings getInstance() {
122 return sInstance;
123 }
Ben Murdochef671652010-11-25 17:17:58 +0000124
John Reck35e9dd62011-04-25 09:01:54 -0700125 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100126 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700127 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
128 mAutofillHandler = new AutofillHandler(mContext);
129 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700130 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reck35e9dd62011-04-25 09:01:54 -0700131 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;
John Reckc477e712011-08-17 11:27:15 -0700137 if (sInitialized) {
138 syncSharedSettings();
139 }
John Reck35e9dd62011-04-25 09:01:54 -0700140
141 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
142 ((InstantSearchEngine) mSearchEngine).setController(mController);
Ben Murdochef671652010-11-25 17:17:58 +0000143 }
144 }
145
John Reck35e9dd62011-04-25 09:01:54 -0700146 public void startManagingSettings(WebSettings settings) {
John Reckc477e712011-08-17 11:27:15 -0700147 if (mNeedsSharedSync) {
148 syncSharedSettings();
149 }
John Reck35e9dd62011-04-25 09:01:54 -0700150 synchronized (mManagedSettings) {
151 syncStaticSettings(settings);
152 syncSetting(settings);
153 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800154 }
155 }
156
John Reckcadae722011-07-25 11:36:17 -0700157 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700158
159 @Override
160 public void run() {
161 // the cost of one cached page is ~3M (measured using nytimes.com). For
162 // low end devices, we only cache one page. For high end devices, we try
163 // to cache more pages, currently choose 5.
164 if (ActivityManager.staticGetMemoryClass() > 16) {
165 mPageCacheCapacity = 5;
166 }
167 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
168 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
169 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700170 // Workaround b/5253777
171 CookieManager.getInstance().acceptCookie();
172 // Workaround b/5254577
173 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700174 if (Build.VERSION.CODENAME.equals("REL")) {
175 // This is a release build, always startup with debug disabled
176 setDebugEnabled(false);
177 }
178 if (mPrefs.contains(PREF_TEXT_SIZE)) {
179 /*
180 * Update from TextSize enum to zoom percent
181 * SMALLEST is 50%
182 * SMALLER is 75%
183 * NORMAL is 100%
184 * LARGER is 150%
185 * LARGEST is 200%
186 */
187 switch (getTextSize()) {
188 case SMALLEST:
189 setTextZoom(50);
190 break;
191 case SMALLER:
192 setTextZoom(75);
193 break;
194 case LARGER:
195 setTextZoom(150);
196 break;
197 case LARGEST:
198 setTextZoom(200);
199 break;
200 }
201 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
202 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100203
204 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
205 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
206 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
207 BrowserProvider.getClientId(mContext.getContentResolver()));
208 }
209
Ben Murdochaaa1f372011-07-25 15:40:58 +0100210 synchronized (BrowserSettings.class) {
211 sInitialized = true;
212 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700213 }
214 }
215 };
216
Ben Murdochaaa1f372011-07-25 15:40:58 +0100217 private static void requireInitialization() {
218 synchronized (BrowserSettings.class) {
219 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700220 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100221 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700222 } catch (InterruptedException e) {
223 }
224 }
225 }
226 }
227
The Android Open Source Project0c908882009-03-03 19:32:16 -0800228 /**
John Reck35e9dd62011-04-25 09:01:54 -0700229 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800230 */
John Reck35e9dd62011-04-25 09:01:54 -0700231 private void syncSetting(WebSettings settings) {
232 settings.setGeolocationEnabled(enableGeolocation());
233 settings.setJavaScriptEnabled(enableJavascript());
234 settings.setLightTouchEnabled(enableLightTouch());
235 settings.setNavDump(enableNavDump());
Derek Sollenberger31adf672011-07-08 11:31:30 -0400236 settings.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
John Reck35e9dd62011-04-25 09:01:54 -0700237 settings.setShowVisualIndicator(enableVisualIndicator());
238 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
239 settings.setDefaultZoom(getDefaultZoom());
240 settings.setMinimumFontSize(getMinimumFontSize());
241 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck92f25f82011-04-26 16:57:10 -0700242 settings.setForceUserScalable(forceEnableUserScalable());
John Reck35e9dd62011-04-25 09:01:54 -0700243 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700244 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700245 settings.setAutoFillEnabled(isAutofillEnabled());
246 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100247 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700248 settings.setLoadsImagesAutomatically(loadImages());
249 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
250 settings.setSavePassword(rememberPasswords());
251 settings.setSaveFormData(saveFormdata());
252 settings.setUseWideViewPort(isWideViewport());
253 settings.setAutoFillProfile(getAutoFillProfile());
John Reckb8b2af82011-05-20 15:58:33 -0700254
255 String ua = mCustomUserAgents.get(settings);
John Reck46500332011-06-07 14:36:10 -0700256 if (ua != null) {
John Reckb8b2af82011-05-20 15:58:33 -0700257 settings.setUserAgentString(ua);
258 } else {
259 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
260 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700261
John Reckeabe5da2011-08-08 13:24:13 -0700262 settings.setProperty(WebViewProperties.gfxInvertedScreen,
John Reck2fd9d0e2011-07-15 11:13:48 -0700263 useInvertedRendering() ? "true" : "false");
Nicolas Roard5d513102011-08-03 15:35:34 -0700264
John Reckeabe5da2011-08-08 13:24:13 -0700265 settings.setProperty(WebViewProperties.gfxInvertedScreenContrast,
Nicolas Roard5d513102011-08-03 15:35:34 -0700266 Float.toString(getInvertedContrast()));
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() {
John Reckc477e712011-08-17 11:27:15 -0700302 mNeedsSharedSync = false;
John Reck35e9dd62011-04-25 09:01:54 -0700303 CookieManager.getInstance().setAcceptCookie(acceptCookies());
304 if (mController != null) {
305 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700306 }
John Reck35e9dd62011-04-25 09:01:54 -0700307 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700308
John Reck35e9dd62011-04-25 09:01:54 -0700309 private void syncManagedSettings() {
310 syncSharedSettings();
311 synchronized (mManagedSettings) {
312 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
313 while (iter.hasNext()) {
314 WeakReference<WebSettings> ref = iter.next();
315 WebSettings settings = ref.get();
316 if (settings == null) {
317 iter.remove();
318 continue;
319 }
320 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000321 }
John Reck35e9dd62011-04-25 09:01:54 -0700322 }
323 }
Ben Murdochef671652010-11-25 17:17:58 +0000324
John Reck35e9dd62011-04-25 09:01:54 -0700325 @Override
326 public void onSharedPreferenceChanged(
327 SharedPreferences sharedPreferences, String key) {
328 syncManagedSettings();
329 if (PREF_SEARCH_ENGINE.equals(key)) {
330 updateSearchEngine(false);
331 }
332 if (PREF_USE_INSTANT_SEARCH.equals(key)) {
333 updateSearchEngine(true);
334 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700335 if (PREF_FULLSCREEN.equals(key)) {
336 if (mController.getUi() != null) {
337 mController.getUi().setFullscreen(useFullscreen());
338 }
Michael Kolb0241e752011-07-07 14:58:50 -0700339 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
340 if (mController.getUi() != null) {
341 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
342 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700343 }
John Reck35e9dd62011-04-25 09:01:54 -0700344 }
345
John Reck961d35d2011-06-23 09:45:54 -0700346 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100347 requireInitialization();
348 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700349 }
350
351 public LayoutAlgorithm getLayoutAlgorithm() {
352 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
353 if (autofitPages()) {
354 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
355 }
356 if (isDebugEnabled()) {
357 if (isSmallScreen()) {
358 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000359 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700360 if (isNormalLayout()) {
361 layoutAlgorithm = LayoutAlgorithm.NORMAL;
362 } else {
363 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
364 }
Ben Murdochef671652010-11-25 17:17:58 +0000365 }
John Reck35e9dd62011-04-25 09:01:54 -0700366 }
367 return layoutAlgorithm;
368 }
Ben Murdochef671652010-11-25 17:17:58 +0000369
John Reck35e9dd62011-04-25 09:01:54 -0700370 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700371 requireInitialization();
372 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800373 }
374
John Reck35e9dd62011-04-25 09:01:54 -0700375 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700376 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700377 return mWebStorageSizeManager;
378 }
379
John Reck35e9dd62011-04-25 09:01:54 -0700380 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700381 if (mAppCachePath == null) {
382 mAppCachePath = mContext.getDir("appcache", 0).getPath();
383 }
384 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700385 }
386
387 private void updateSearchEngine(boolean force) {
388 String searchEngineName = getSearchEngineName();
389 if (force || mSearchEngine == null ||
390 !mSearchEngine.getName().equals(searchEngineName)) {
391 if (mSearchEngine != null) {
392 if (mSearchEngine.supportsVoiceSearch()) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000393 // One or more tabs could have been in voice search mode.
394 // Clear it, since the new SearchEngine may not support
395 // it, or may handle it differently.
396 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
397 mController.getTabControl().getTab(i).revertVoiceSearchMode();
398 }
399 }
John Reck35e9dd62011-04-25 09:01:54 -0700400 mSearchEngine.close();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000401 }
John Reck35e9dd62011-04-25 09:01:54 -0700402 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000403
John Reck35e9dd62011-04-25 09:01:54 -0700404 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
405 ((InstantSearchEngine) mSearchEngine).setController(mController);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000406 }
407 }
408 }
409
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100410 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700411 if (mSearchEngine == null) {
412 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100413 }
John Reck35e9dd62011-04-25 09:01:54 -0700414 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100415 }
416
John Reck35e9dd62011-04-25 09:01:54 -0700417 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700418 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700419 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100420 }
421
John Reck35e9dd62011-04-25 09:01:54 -0700422 public void setDebugEnabled(boolean value) {
423 mPrefs.edit().putBoolean(PREF_DEBUG_MENU, value).apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100424 }
425
John Reck35e9dd62011-04-25 09:01:54 -0700426 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800427 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700428 if (mController != null) {
429 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800430 if (current != null) {
431 current.clearCache(true);
432 }
433 }
434 }
435
John Reck35e9dd62011-04-25 09:01:54 -0700436 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800437 CookieManager.getInstance().removeAllCookie();
438 }
439
John Reck35e9dd62011-04-25 09:01:54 -0700440 public void clearHistory() {
441 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800442 Browser.clearHistory(resolver);
443 Browser.clearSearches(resolver);
444 }
445
John Reck35e9dd62011-04-25 09:01:54 -0700446 public void clearFormData() {
447 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700448 if (mController!= null) {
449 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100450 if (currentTopView != null) {
451 currentTopView.clearFormData();
452 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800453 }
454 }
455
John Reck35e9dd62011-04-25 09:01:54 -0700456 public void clearPasswords() {
457 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800458 db.clearUsernamePassword();
459 db.clearHttpAuthUsernamePassword();
460 }
461
John Reck35e9dd62011-04-25 09:01:54 -0700462 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100463 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100464 }
465
John Reck35e9dd62011-04-25 09:01:54 -0700466 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100467 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100468 }
469
John Reck35e9dd62011-04-25 09:01:54 -0700470 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700471 // Preserve autologin setting
472 long gal = mPrefs.getLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, -1);
473 mPrefs.edit()
474 .clear()
475 .putLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, gal)
476 .apply();
John Reck35e9dd62011-04-25 09:01:54 -0700477 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700478 }
479
John Reck35e9dd62011-04-25 09:01:54 -0700480 public AutoFillProfile getAutoFillProfile() {
481 mAutofillHandler.waitForLoad();
482 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800483 }
484
John Reck35e9dd62011-04-25 09:01:54 -0700485 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
486 mAutofillHandler.waitForLoad();
487 mAutofillHandler.setAutoFillProfile(profile, msg);
Ben Murdoch273330a2011-07-08 18:37:22 +0100488 // Auto-fill will reuse the same profile ID when making edits to the profile,
489 // so we need to force a settings sync (otherwise the SharedPreferences
490 // manager will optimise out the call to onSharedPreferenceChanged(), as
491 // it thinks nothing has changed).
492 syncManagedSettings();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800493 }
494
John Reck35e9dd62011-04-25 09:01:54 -0700495 public void toggleDebugSettings() {
496 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800497 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100498
John Reckb8b2af82011-05-20 15:58:33 -0700499 public boolean hasDesktopUseragent(WebView view) {
500 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
501 }
502
503 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700504 if (view == null) {
505 return;
506 }
John Reckb8b2af82011-05-20 15:58:33 -0700507 WebSettings settings = view.getSettings();
508 if (mCustomUserAgents.get(settings) != null) {
509 mCustomUserAgents.remove(settings);
510 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
511 } else {
512 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
513 settings.setUserAgentString(DESKTOP_USERAGENT);
514 }
515 }
516
John Reck7dc444b2011-06-16 17:44:29 -0700517 public static int getAdjustedMinimumFontSize(int rawValue) {
518 rawValue++; // Preference starts at 0, min font at 1
519 if (rawValue > 1) {
520 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
521 }
522 return rawValue;
523 }
524
525 public static int getAdjustedTextZoom(int rawValue) {
526 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
527 return rawValue + 100;
528 }
529
530 static int getRawTextZoom(int percent) {
531 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
532 }
533
John Reckcadae722011-07-25 11:36:17 -0700534 public SharedPreferences getPreferences() {
535 return mPrefs;
536 }
537
John Reck35e9dd62011-04-25 09:01:54 -0700538 // -----------------------------
539 // getter/setters for accessibility_preferences.xml
540 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100541
John Reck7dc444b2011-06-16 17:44:29 -0700542 @Deprecated
543 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700544 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
545 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100546 }
547
John Reck35e9dd62011-04-25 09:01:54 -0700548 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700549 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700550 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100551 }
552
John Reck92f25f82011-04-26 16:57:10 -0700553 public boolean forceEnableUserScalable() {
554 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
555 }
556
John Reck7dc444b2011-06-16 17:44:29 -0700557 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700558 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700559 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
560 return getAdjustedTextZoom(textZoom);
561 }
562
563 public void setTextZoom(int percent) {
564 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
565 }
566
John Reck35e9dd62011-04-25 09:01:54 -0700567 // -----------------------------
568 // getter/setters for advanced_preferences.xml
569 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100570
John Reck35e9dd62011-04-25 09:01:54 -0700571 public String getSearchEngineName() {
572 return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100573 }
John Reck63bb6872010-12-01 19:29:32 -0800574
John Reck35e9dd62011-04-25 09:01:54 -0700575 public boolean openInBackground() {
576 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800577 }
John Reck35e9dd62011-04-25 09:01:54 -0700578
579 public boolean enableJavascript() {
580 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
581 }
582
583 // TODO: Cache
584 public PluginState getPluginState() {
585 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
586 return PluginState.valueOf(state);
587 }
588
589 // TODO: Cache
590 public ZoomDensity getDefaultZoom() {
591 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
592 return ZoomDensity.valueOf(zoom);
593 }
594
595 public boolean loadPageInOverviewMode() {
596 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
597 }
598
599 public boolean autofitPages() {
600 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
601 }
602
603 public boolean blockPopupWindows() {
604 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
605 }
606
607 public boolean loadImages() {
608 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
609 }
610
611 public String getDefaultTextEncoding() {
612 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
613 }
614
615 // -----------------------------
616 // getter/setters for general_preferences.xml
617 // -----------------------------
618
619 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700620 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
621 }
622
623 public void setHomePage(String value) {
624 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
625 }
626
627 public boolean isAutofillEnabled() {
628 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
629 }
630
631 public void setAutofillEnabled(boolean value) {
632 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
633 }
634
635 // -----------------------------
636 // getter/setters for debug_preferences.xml
637 // -----------------------------
638
639 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700640 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700641 return true;
642 }
643 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
644 }
645
Derek Sollenberger31adf672011-07-08 11:31:30 -0400646 public boolean isSkiaHardwareAccelerated() {
647 if (!isDebugEnabled()) {
648 return false;
649 }
650 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
651 }
652
John Reck35e9dd62011-04-25 09:01:54 -0700653 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700654 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700655 return 0;
656 }
657 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
658 }
659
660 // -----------------------------
661 // getter/setters for hidden_debug_preferences.xml
662 // -----------------------------
663
664 public boolean enableVisualIndicator() {
665 if (!isDebugEnabled()) {
666 return false;
667 }
668 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
669 }
670
671 public boolean enableJavascriptConsole() {
672 if (!isDebugEnabled()) {
673 return false;
674 }
675 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
676 }
677
678 public boolean isSmallScreen() {
679 if (!isDebugEnabled()) {
680 return false;
681 }
682 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
683 }
684
685 public boolean isWideViewport() {
686 if (!isDebugEnabled()) {
687 return true;
688 }
689 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
690 }
691
692 public boolean isNormalLayout() {
693 if (!isDebugEnabled()) {
694 return false;
695 }
696 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
697 }
698
699 public boolean isTracing() {
700 if (!isDebugEnabled()) {
701 return false;
702 }
703 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
704 }
705
706 public boolean enableLightTouch() {
707 if (!isDebugEnabled()) {
708 return false;
709 }
710 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
711 }
712
713 public boolean enableNavDump() {
714 if (!isDebugEnabled()) {
715 return false;
716 }
717 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
718 }
719
720 public String getJsEngineFlags() {
721 if (!isDebugEnabled()) {
722 return "";
723 }
724 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
725 }
726
727 // -----------------------------
728 // getter/setters for lab_preferences.xml
729 // -----------------------------
730
731 public boolean useQuickControls() {
732 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
733 }
734
735 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700736 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700737 }
738
739 public boolean useInstantSearch() {
740 return mPrefs.getBoolean(PREF_USE_INSTANT_SEARCH, false);
741 }
742
Michael Kolbc38c6042011-04-27 10:46:06 -0700743 public boolean useFullscreen() {
744 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
745 }
746
John Reck2fd9d0e2011-07-15 11:13:48 -0700747 public boolean useInvertedRendering() {
748 return mPrefs.getBoolean(PREF_INVERTED, false);
749 }
750
Nicolas Roard5d513102011-08-03 15:35:34 -0700751 public float getInvertedContrast() {
752 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
753 }
754
John Reck35e9dd62011-04-25 09:01:54 -0700755 // -----------------------------
756 // getter/setters for privacy_security_preferences.xml
757 // -----------------------------
758
759 public boolean showSecurityWarnings() {
760 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
761 }
762
763 public boolean acceptCookies() {
764 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
765 }
766
767 public boolean saveFormdata() {
768 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
769 }
770
771 public boolean enableGeolocation() {
772 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
773 }
774
775 public boolean rememberPasswords() {
776 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
777 }
778
Michael Kolb14612442011-06-24 13:06:29 -0700779 // -----------------------------
780 // getter/setters for bandwidth_preferences.xml
781 // -----------------------------
782
783 public boolean isPreloadEnabled() {
784 return mPrefs.getBoolean(PREF_DATA_PRELOAD, false);
785 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800786}