blob: 5920e584a1467a69cb27208dfc9936dcd7074199 [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 Reck1da81882011-10-04 17:21:10 -070023import android.content.SharedPreferences.Editor;
John Reck812d2d62011-01-18 14:16:15 -080024import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
John Reckf48314f2011-04-27 17:52:17 -070025import android.os.Build;
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;
John Reck5ba3c762011-09-14 15:00:15 -070029import android.util.DisplayMetrics;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010031import android.webkit.GeolocationPermissions;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.webkit.WebIconDatabase;
33import android.webkit.WebSettings;
Ben Murdoch0cb81892010-10-08 12:41:33 +010034import android.webkit.WebSettings.AutoFillProfile;
John Reck35e9dd62011-04-25 09:01:54 -070035import android.webkit.WebSettings.LayoutAlgorithm;
36import android.webkit.WebSettings.PluginState;
37import android.webkit.WebSettings.TextSize;
38import android.webkit.WebSettings.ZoomDensity;
Nicolas Roard78a98e42009-05-11 13:34:17 +010039import android.webkit.WebStorage;
John Reck812d2d62011-01-18 14:16:15 -080040import android.webkit.WebView;
41import android.webkit.WebViewDatabase;
The Android Open Source Project0c908882009-03-03 19:32:16 -080042
John Reck46500332011-06-07 14:36:10 -070043import com.android.browser.homepages.HomeProvider;
44import com.android.browser.provider.BrowserProvider;
45import com.android.browser.search.SearchEngine;
46import com.android.browser.search.SearchEngines;
47
John Reck35e9dd62011-04-25 09:01:54 -070048import java.lang.ref.WeakReference;
49import java.util.Iterator;
50import java.util.LinkedList;
John Reckb8b2af82011-05-20 15:58:33 -070051import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080052
John Reck35e9dd62011-04-25 09:01:54 -070053/**
54 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080055 */
John Reck35e9dd62011-04-25 09:01:54 -070056public class BrowserSettings implements OnSharedPreferenceChangeListener,
57 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010058
John Reck35e9dd62011-04-25 09:01:54 -070059 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070060 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
61 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
62 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080063
64 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070065 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
66 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070067
68 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070069 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
70 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070071
72 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070073 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
74 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080075
John Reck7fd1e8f2011-04-27 18:22:57 -070076 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
77 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
78 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
79
John Reck35e9dd62011-04-25 09:01:54 -070080 private static final String USER_AGENTS[] = { null,
81 DESKTOP_USERAGENT,
82 IPHONE_USERAGENT,
83 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070084 FROYO_USERAGENT,
85 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070086 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080087
John Reck8fc22a12011-06-16 14:57:41 -070088 // The minimum min font size
89 // Aka, the lower bounds for the min font size range
90 // which is 1:5..24
91 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070092 // The initial value in the text zoom range
93 // This is what represents 100% in the SeekBarPreference range
94 private static final int TEXT_ZOOM_START_VAL = 10;
95 // The size of a single step in the text zoom range, in percent
96 private static final int TEXT_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -070097
John Reck35e9dd62011-04-25 09:01:54 -070098 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -070099
John Reck35e9dd62011-04-25 09:01:54 -0700100 private Context mContext;
101 private SharedPreferences mPrefs;
102 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700103 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700104 private WebStorageSizeManager mWebStorageSizeManager;
105 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700106 private WeakHashMap<WebSettings, String> mCustomUserAgents;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100107 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -0700108 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -0700109 private float mFontSizeMult = 1.0f;
John Reck78a6a1d2011-07-21 13:54:03 -0700110
111 // Cached values
112 private int mPageCacheCapacity = 1;
113 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114
John Reck35e9dd62011-04-25 09:01:54 -0700115 // Cached settings
116 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800117
Ben Murdochaaa1f372011-07-25 15:40:58 +0100118 private static String sFactoryResetUrl;
119
John Reck35e9dd62011-04-25 09:01:54 -0700120 public static void initialize(final Context context) {
121 sInstance = new BrowserSettings(context);
122 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123
John Reck35e9dd62011-04-25 09:01:54 -0700124 public static BrowserSettings getInstance() {
125 return sInstance;
126 }
Ben Murdochef671652010-11-25 17:17:58 +0000127
John Reck35e9dd62011-04-25 09:01:54 -0700128 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100129 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700130 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
131 mAutofillHandler = new AutofillHandler(mContext);
132 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700133 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reck35e9dd62011-04-25 09:01:54 -0700134 mAutofillHandler.asyncLoadFromDb();
John Reckcadae722011-07-25 11:36:17 -0700135 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700136 }
137
138 public void setController(Controller controller) {
139 mController = controller;
John Reckc477e712011-08-17 11:27:15 -0700140 if (sInitialized) {
141 syncSharedSettings();
142 }
John Reck35e9dd62011-04-25 09:01:54 -0700143
144 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
145 ((InstantSearchEngine) mSearchEngine).setController(mController);
Ben Murdochef671652010-11-25 17:17:58 +0000146 }
147 }
148
John Reck35e9dd62011-04-25 09:01:54 -0700149 public void startManagingSettings(WebSettings settings) {
John Reckc477e712011-08-17 11:27:15 -0700150 if (mNeedsSharedSync) {
151 syncSharedSettings();
152 }
John Reck35e9dd62011-04-25 09:01:54 -0700153 synchronized (mManagedSettings) {
154 syncStaticSettings(settings);
155 syncSetting(settings);
156 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800157 }
158 }
159
John Reckcadae722011-07-25 11:36:17 -0700160 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700161
162 @Override
163 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700164 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
165 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700166 // 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()));
John Reck276b1352011-09-02 15:47:33 -0700175 // Workaround b/5253777
176 CookieManager.getInstance().acceptCookie();
177 // Workaround b/5254577
178 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700179 if (Build.VERSION.CODENAME.equals("REL")) {
180 // This is a release build, always startup with debug disabled
181 setDebugEnabled(false);
182 }
183 if (mPrefs.contains(PREF_TEXT_SIZE)) {
184 /*
185 * Update from TextSize enum to zoom percent
186 * SMALLEST is 50%
187 * SMALLER is 75%
188 * NORMAL is 100%
189 * LARGER is 150%
190 * LARGEST is 200%
191 */
192 switch (getTextSize()) {
193 case SMALLEST:
194 setTextZoom(50);
195 break;
196 case SMALLER:
197 setTextZoom(75);
198 break;
199 case LARGER:
200 setTextZoom(150);
201 break;
202 case LARGEST:
203 setTextZoom(200);
204 break;
205 }
206 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
207 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100208
209 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
210 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
211 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
212 BrowserProvider.getClientId(mContext.getContentResolver()));
213 }
214
Ben Murdochaaa1f372011-07-25 15:40:58 +0100215 synchronized (BrowserSettings.class) {
216 sInitialized = true;
217 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700218 }
219 }
220 };
221
Ben Murdochaaa1f372011-07-25 15:40:58 +0100222 private static void requireInitialization() {
223 synchronized (BrowserSettings.class) {
224 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700225 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100226 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700227 } catch (InterruptedException e) {
228 }
229 }
230 }
231 }
232
The Android Open Source Project0c908882009-03-03 19:32:16 -0800233 /**
John Reck35e9dd62011-04-25 09:01:54 -0700234 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800235 */
John Reck35e9dd62011-04-25 09:01:54 -0700236 private void syncSetting(WebSettings settings) {
237 settings.setGeolocationEnabled(enableGeolocation());
238 settings.setJavaScriptEnabled(enableJavascript());
239 settings.setLightTouchEnabled(enableLightTouch());
240 settings.setNavDump(enableNavDump());
Derek Sollenberger31adf672011-07-08 11:31:30 -0400241 settings.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
John Reck35e9dd62011-04-25 09:01:54 -0700242 settings.setShowVisualIndicator(enableVisualIndicator());
243 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
244 settings.setDefaultZoom(getDefaultZoom());
245 settings.setMinimumFontSize(getMinimumFontSize());
246 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck92f25f82011-04-26 16:57:10 -0700247 settings.setForceUserScalable(forceEnableUserScalable());
John Reck35e9dd62011-04-25 09:01:54 -0700248 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700249 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700250 settings.setAutoFillEnabled(isAutofillEnabled());
251 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100252 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700253 settings.setLoadsImagesAutomatically(loadImages());
254 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
255 settings.setSavePassword(rememberPasswords());
256 settings.setSaveFormData(saveFormdata());
257 settings.setUseWideViewPort(isWideViewport());
258 settings.setAutoFillProfile(getAutoFillProfile());
John Reckb8b2af82011-05-20 15:58:33 -0700259
260 String ua = mCustomUserAgents.get(settings);
John Reck46500332011-06-07 14:36:10 -0700261 if (ua != null) {
John Reckb8b2af82011-05-20 15:58:33 -0700262 settings.setUserAgentString(ua);
263 } else {
264 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
265 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700266
John Reckeabe5da2011-08-08 13:24:13 -0700267 settings.setProperty(WebViewProperties.gfxInvertedScreen,
John Reck2fd9d0e2011-07-15 11:13:48 -0700268 useInvertedRendering() ? "true" : "false");
Nicolas Roard5d513102011-08-03 15:35:34 -0700269
John Reckeabe5da2011-08-08 13:24:13 -0700270 settings.setProperty(WebViewProperties.gfxInvertedScreenContrast,
Nicolas Roard5d513102011-08-03 15:35:34 -0700271 Float.toString(getInvertedContrast()));
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700272
273 settings.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
274 enableCpuUploadPath() ? "true" : "false");
Ben Murdochef671652010-11-25 17:17:58 +0000275 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800276
John Reck35e9dd62011-04-25 09:01:54 -0700277 /**
278 * Syncs all the settings that have no UI
279 * These cannot change, so we only need to set them once per WebSettings
280 */
281 private void syncStaticSettings(WebSettings settings) {
282 settings.setDefaultFontSize(16);
283 settings.setDefaultFixedFontSize(13);
284 settings.setPageCacheCapacity(getPageCacheCapacity());
Ben Murdochef671652010-11-25 17:17:58 +0000285
John Reck35e9dd62011-04-25 09:01:54 -0700286 // WebView inside Browser doesn't want initial focus to be set.
287 settings.setNeedInitialFocus(false);
288 // Browser supports multiple windows
289 settings.setSupportMultipleWindows(true);
290 // enable smooth transition for better performance during panning or
291 // zooming
292 settings.setEnableSmoothTransition(true);
293 // disable content url access
294 settings.setAllowContentAccess(false);
295
296 // HTML5 API flags
297 settings.setAppCacheEnabled(true);
298 settings.setDatabaseEnabled(true);
299 settings.setDomStorageEnabled(true);
300 settings.setWorkersEnabled(true); // This only affects V8.
301
302 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700303 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700304 settings.setAppCachePath(getAppCachePath());
305 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
306 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
307 }
308
309 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700310 mNeedsSharedSync = false;
John Reck35e9dd62011-04-25 09:01:54 -0700311 CookieManager.getInstance().setAcceptCookie(acceptCookies());
312 if (mController != null) {
313 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700314 }
John Reck35e9dd62011-04-25 09:01:54 -0700315 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700316
John Reck35e9dd62011-04-25 09:01:54 -0700317 private void syncManagedSettings() {
318 syncSharedSettings();
319 synchronized (mManagedSettings) {
320 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
321 while (iter.hasNext()) {
322 WeakReference<WebSettings> ref = iter.next();
323 WebSettings settings = ref.get();
324 if (settings == null) {
325 iter.remove();
326 continue;
327 }
328 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000329 }
John Reck35e9dd62011-04-25 09:01:54 -0700330 }
331 }
Ben Murdochef671652010-11-25 17:17:58 +0000332
John Reck35e9dd62011-04-25 09:01:54 -0700333 @Override
334 public void onSharedPreferenceChanged(
335 SharedPreferences sharedPreferences, String key) {
336 syncManagedSettings();
337 if (PREF_SEARCH_ENGINE.equals(key)) {
338 updateSearchEngine(false);
339 }
340 if (PREF_USE_INSTANT_SEARCH.equals(key)) {
341 updateSearchEngine(true);
342 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700343 if (PREF_FULLSCREEN.equals(key)) {
344 if (mController.getUi() != null) {
345 mController.getUi().setFullscreen(useFullscreen());
346 }
Michael Kolb0241e752011-07-07 14:58:50 -0700347 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
348 if (mController.getUi() != null) {
349 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
350 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700351 }
John Reck35e9dd62011-04-25 09:01:54 -0700352 }
353
John Reck961d35d2011-06-23 09:45:54 -0700354 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100355 requireInitialization();
356 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700357 }
358
359 public LayoutAlgorithm getLayoutAlgorithm() {
360 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
361 if (autofitPages()) {
362 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
363 }
364 if (isDebugEnabled()) {
365 if (isSmallScreen()) {
366 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000367 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700368 if (isNormalLayout()) {
369 layoutAlgorithm = LayoutAlgorithm.NORMAL;
370 } else {
371 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
372 }
Ben Murdochef671652010-11-25 17:17:58 +0000373 }
John Reck35e9dd62011-04-25 09:01:54 -0700374 }
375 return layoutAlgorithm;
376 }
Ben Murdochef671652010-11-25 17:17:58 +0000377
John Reck35e9dd62011-04-25 09:01:54 -0700378 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700379 requireInitialization();
380 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800381 }
382
John Reck35e9dd62011-04-25 09:01:54 -0700383 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700384 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700385 return mWebStorageSizeManager;
386 }
387
John Reck35e9dd62011-04-25 09:01:54 -0700388 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700389 if (mAppCachePath == null) {
390 mAppCachePath = mContext.getDir("appcache", 0).getPath();
391 }
392 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700393 }
394
395 private void updateSearchEngine(boolean force) {
396 String searchEngineName = getSearchEngineName();
397 if (force || mSearchEngine == null ||
398 !mSearchEngine.getName().equals(searchEngineName)) {
399 if (mSearchEngine != null) {
400 if (mSearchEngine.supportsVoiceSearch()) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000401 // One or more tabs could have been in voice search mode.
402 // Clear it, since the new SearchEngine may not support
403 // it, or may handle it differently.
404 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
405 mController.getTabControl().getTab(i).revertVoiceSearchMode();
406 }
407 }
John Reck35e9dd62011-04-25 09:01:54 -0700408 mSearchEngine.close();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000409 }
John Reck35e9dd62011-04-25 09:01:54 -0700410 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000411
John Reck35e9dd62011-04-25 09:01:54 -0700412 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
413 ((InstantSearchEngine) mSearchEngine).setController(mController);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000414 }
415 }
416 }
417
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100418 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700419 if (mSearchEngine == null) {
420 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100421 }
John Reck35e9dd62011-04-25 09:01:54 -0700422 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100423 }
424
John Reck35e9dd62011-04-25 09:01:54 -0700425 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700426 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700427 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100428 }
429
John Reck35e9dd62011-04-25 09:01:54 -0700430 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700431 Editor edit = mPrefs.edit();
432 edit.putBoolean(PREF_DEBUG_MENU, value);
433 if (!value) {
434 // Reset to "safe" value
435 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
436 }
437 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100438 }
439
John Reck35e9dd62011-04-25 09:01:54 -0700440 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800441 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700442 if (mController != null) {
443 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800444 if (current != null) {
445 current.clearCache(true);
446 }
447 }
448 }
449
John Reck35e9dd62011-04-25 09:01:54 -0700450 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800451 CookieManager.getInstance().removeAllCookie();
452 }
453
John Reck35e9dd62011-04-25 09:01:54 -0700454 public void clearHistory() {
455 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800456 Browser.clearHistory(resolver);
457 Browser.clearSearches(resolver);
458 }
459
John Reck35e9dd62011-04-25 09:01:54 -0700460 public void clearFormData() {
461 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700462 if (mController!= null) {
463 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100464 if (currentTopView != null) {
465 currentTopView.clearFormData();
466 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800467 }
468 }
469
John Reck35e9dd62011-04-25 09:01:54 -0700470 public void clearPasswords() {
471 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800472 db.clearUsernamePassword();
473 db.clearHttpAuthUsernamePassword();
474 }
475
John Reck35e9dd62011-04-25 09:01:54 -0700476 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100477 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100478 }
479
John Reck35e9dd62011-04-25 09:01:54 -0700480 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100481 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100482 }
483
John Reck35e9dd62011-04-25 09:01:54 -0700484 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700485 // Preserve autologin setting
486 long gal = mPrefs.getLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, -1);
487 mPrefs.edit()
488 .clear()
489 .putLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, gal)
490 .apply();
John Reck35e9dd62011-04-25 09:01:54 -0700491 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700492 }
493
John Reck35e9dd62011-04-25 09:01:54 -0700494 public AutoFillProfile getAutoFillProfile() {
495 mAutofillHandler.waitForLoad();
496 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800497 }
498
John Reck35e9dd62011-04-25 09:01:54 -0700499 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
500 mAutofillHandler.waitForLoad();
501 mAutofillHandler.setAutoFillProfile(profile, msg);
Ben Murdoch273330a2011-07-08 18:37:22 +0100502 // Auto-fill will reuse the same profile ID when making edits to the profile,
503 // so we need to force a settings sync (otherwise the SharedPreferences
504 // manager will optimise out the call to onSharedPreferenceChanged(), as
505 // it thinks nothing has changed).
506 syncManagedSettings();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800507 }
508
John Reck35e9dd62011-04-25 09:01:54 -0700509 public void toggleDebugSettings() {
510 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800511 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100512
John Reckb8b2af82011-05-20 15:58:33 -0700513 public boolean hasDesktopUseragent(WebView view) {
514 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
515 }
516
517 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700518 if (view == null) {
519 return;
520 }
John Reckb8b2af82011-05-20 15:58:33 -0700521 WebSettings settings = view.getSettings();
522 if (mCustomUserAgents.get(settings) != null) {
523 mCustomUserAgents.remove(settings);
524 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
525 } else {
526 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
527 settings.setUserAgentString(DESKTOP_USERAGENT);
528 }
529 }
530
John Reck7dc444b2011-06-16 17:44:29 -0700531 public static int getAdjustedMinimumFontSize(int rawValue) {
532 rawValue++; // Preference starts at 0, min font at 1
533 if (rawValue > 1) {
534 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
535 }
536 return rawValue;
537 }
538
John Reck5ba3c762011-09-14 15:00:15 -0700539 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700540 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700541 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700542 }
543
544 static int getRawTextZoom(int percent) {
545 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
546 }
547
John Reckcadae722011-07-25 11:36:17 -0700548 public SharedPreferences getPreferences() {
549 return mPrefs;
550 }
551
John Reck35e9dd62011-04-25 09:01:54 -0700552 // -----------------------------
553 // getter/setters for accessibility_preferences.xml
554 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100555
John Reck7dc444b2011-06-16 17:44:29 -0700556 @Deprecated
557 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700558 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
559 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100560 }
561
John Reck35e9dd62011-04-25 09:01:54 -0700562 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700563 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700564 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100565 }
566
John Reck92f25f82011-04-26 16:57:10 -0700567 public boolean forceEnableUserScalable() {
568 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
569 }
570
John Reck7dc444b2011-06-16 17:44:29 -0700571 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700572 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700573 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
574 return getAdjustedTextZoom(textZoom);
575 }
576
577 public void setTextZoom(int percent) {
578 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
579 }
580
John Reck35e9dd62011-04-25 09:01:54 -0700581 // -----------------------------
582 // getter/setters for advanced_preferences.xml
583 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100584
John Reck35e9dd62011-04-25 09:01:54 -0700585 public String getSearchEngineName() {
586 return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100587 }
John Reck63bb6872010-12-01 19:29:32 -0800588
John Reck35e9dd62011-04-25 09:01:54 -0700589 public boolean openInBackground() {
590 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800591 }
John Reck35e9dd62011-04-25 09:01:54 -0700592
593 public boolean enableJavascript() {
594 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
595 }
596
597 // TODO: Cache
598 public PluginState getPluginState() {
599 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
600 return PluginState.valueOf(state);
601 }
602
603 // TODO: Cache
604 public ZoomDensity getDefaultZoom() {
605 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
606 return ZoomDensity.valueOf(zoom);
607 }
608
609 public boolean loadPageInOverviewMode() {
610 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
611 }
612
613 public boolean autofitPages() {
614 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
615 }
616
617 public boolean blockPopupWindows() {
618 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
619 }
620
621 public boolean loadImages() {
622 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
623 }
624
625 public String getDefaultTextEncoding() {
626 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
627 }
628
629 // -----------------------------
630 // getter/setters for general_preferences.xml
631 // -----------------------------
632
633 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700634 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
635 }
636
637 public void setHomePage(String value) {
638 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
639 }
640
641 public boolean isAutofillEnabled() {
642 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
643 }
644
645 public void setAutofillEnabled(boolean value) {
646 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
647 }
648
649 // -----------------------------
650 // getter/setters for debug_preferences.xml
651 // -----------------------------
652
653 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700654 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700655 return true;
656 }
657 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
658 }
659
Derek Sollenberger31adf672011-07-08 11:31:30 -0400660 public boolean isSkiaHardwareAccelerated() {
661 if (!isDebugEnabled()) {
662 return false;
663 }
664 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
665 }
666
John Reck35e9dd62011-04-25 09:01:54 -0700667 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700668 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700669 return 0;
670 }
671 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
672 }
673
674 // -----------------------------
675 // getter/setters for hidden_debug_preferences.xml
676 // -----------------------------
677
678 public boolean enableVisualIndicator() {
679 if (!isDebugEnabled()) {
680 return false;
681 }
682 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
683 }
684
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700685 public boolean enableCpuUploadPath() {
686 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700687 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700688 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700689 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700690 }
691
John Reck35e9dd62011-04-25 09:01:54 -0700692 public boolean enableJavascriptConsole() {
693 if (!isDebugEnabled()) {
694 return false;
695 }
696 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
697 }
698
699 public boolean isSmallScreen() {
700 if (!isDebugEnabled()) {
701 return false;
702 }
703 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
704 }
705
706 public boolean isWideViewport() {
707 if (!isDebugEnabled()) {
708 return true;
709 }
710 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
711 }
712
713 public boolean isNormalLayout() {
714 if (!isDebugEnabled()) {
715 return false;
716 }
717 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
718 }
719
720 public boolean isTracing() {
721 if (!isDebugEnabled()) {
722 return false;
723 }
724 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
725 }
726
727 public boolean enableLightTouch() {
728 if (!isDebugEnabled()) {
729 return false;
730 }
731 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
732 }
733
734 public boolean enableNavDump() {
735 if (!isDebugEnabled()) {
736 return false;
737 }
738 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
739 }
740
741 public String getJsEngineFlags() {
742 if (!isDebugEnabled()) {
743 return "";
744 }
745 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
746 }
747
748 // -----------------------------
749 // getter/setters for lab_preferences.xml
750 // -----------------------------
751
752 public boolean useQuickControls() {
753 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
754 }
755
756 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700757 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700758 }
759
760 public boolean useInstantSearch() {
761 return mPrefs.getBoolean(PREF_USE_INSTANT_SEARCH, false);
762 }
763
Michael Kolbc38c6042011-04-27 10:46:06 -0700764 public boolean useFullscreen() {
765 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
766 }
767
John Reck2fd9d0e2011-07-15 11:13:48 -0700768 public boolean useInvertedRendering() {
769 return mPrefs.getBoolean(PREF_INVERTED, false);
770 }
771
Nicolas Roard5d513102011-08-03 15:35:34 -0700772 public float getInvertedContrast() {
773 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
774 }
775
John Reck35e9dd62011-04-25 09:01:54 -0700776 // -----------------------------
777 // getter/setters for privacy_security_preferences.xml
778 // -----------------------------
779
780 public boolean showSecurityWarnings() {
781 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
782 }
783
784 public boolean acceptCookies() {
785 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
786 }
787
788 public boolean saveFormdata() {
789 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
790 }
791
792 public boolean enableGeolocation() {
793 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
794 }
795
796 public boolean rememberPasswords() {
797 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
798 }
799
Michael Kolb14612442011-06-24 13:06:29 -0700800 // -----------------------------
801 // getter/setters for bandwidth_preferences.xml
802 // -----------------------------
803
Mathew Inwood467813f2011-09-02 15:43:17 +0100804 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
805 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -0700806 }
Mathew Inwood467813f2011-09-02 15:43:17 +0100807
808 public static String getPreloadAlwaysPreferenceString(Context context) {
809 return context.getResources().getString(R.string.pref_data_preload_value_always);
810 }
811
812 private String getDefaultPreloadSetting() {
Mathew Inwood75905982011-09-07 14:24:06 +0100813 return mContext.getResources().getString(R.string.pref_data_preload_default_value);
Mathew Inwood467813f2011-09-02 15:43:17 +0100814 }
815
816 public String getPreloadEnabled() {
817 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
818 }
819
The Android Open Source Project0c908882009-03-03 19:32:16 -0800820}