blob: cfb8dd43579ef685f93b44519aa14c682fbf08b4 [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;
Mathew Inwood825fba72011-10-04 14:52:52 +010028import android.provider.Settings;
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) {
431 mPrefs.edit().putBoolean(PREF_DEBUG_MENU, value).apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100432 }
433
John Reck35e9dd62011-04-25 09:01:54 -0700434 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800435 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700436 if (mController != null) {
437 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800438 if (current != null) {
439 current.clearCache(true);
440 }
441 }
442 }
443
John Reck35e9dd62011-04-25 09:01:54 -0700444 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800445 CookieManager.getInstance().removeAllCookie();
446 }
447
John Reck35e9dd62011-04-25 09:01:54 -0700448 public void clearHistory() {
449 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800450 Browser.clearHistory(resolver);
451 Browser.clearSearches(resolver);
452 }
453
John Reck35e9dd62011-04-25 09:01:54 -0700454 public void clearFormData() {
455 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700456 if (mController!= null) {
457 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100458 if (currentTopView != null) {
459 currentTopView.clearFormData();
460 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800461 }
462 }
463
John Reck35e9dd62011-04-25 09:01:54 -0700464 public void clearPasswords() {
465 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800466 db.clearUsernamePassword();
467 db.clearHttpAuthUsernamePassword();
468 }
469
John Reck35e9dd62011-04-25 09:01:54 -0700470 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100471 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100472 }
473
John Reck35e9dd62011-04-25 09:01:54 -0700474 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100475 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100476 }
477
John Reck35e9dd62011-04-25 09:01:54 -0700478 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700479 // Preserve autologin setting
480 long gal = mPrefs.getLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, -1);
481 mPrefs.edit()
482 .clear()
483 .putLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, gal)
484 .apply();
John Reck35e9dd62011-04-25 09:01:54 -0700485 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700486 }
487
John Reck35e9dd62011-04-25 09:01:54 -0700488 public AutoFillProfile getAutoFillProfile() {
489 mAutofillHandler.waitForLoad();
490 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800491 }
492
John Reck35e9dd62011-04-25 09:01:54 -0700493 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
494 mAutofillHandler.waitForLoad();
495 mAutofillHandler.setAutoFillProfile(profile, msg);
Ben Murdoch273330a2011-07-08 18:37:22 +0100496 // Auto-fill will reuse the same profile ID when making edits to the profile,
497 // so we need to force a settings sync (otherwise the SharedPreferences
498 // manager will optimise out the call to onSharedPreferenceChanged(), as
499 // it thinks nothing has changed).
500 syncManagedSettings();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800501 }
502
John Reck35e9dd62011-04-25 09:01:54 -0700503 public void toggleDebugSettings() {
504 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800505 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100506
John Reckb8b2af82011-05-20 15:58:33 -0700507 public boolean hasDesktopUseragent(WebView view) {
508 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
509 }
510
511 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700512 if (view == null) {
513 return;
514 }
John Reckb8b2af82011-05-20 15:58:33 -0700515 WebSettings settings = view.getSettings();
516 if (mCustomUserAgents.get(settings) != null) {
517 mCustomUserAgents.remove(settings);
518 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
519 } else {
520 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
521 settings.setUserAgentString(DESKTOP_USERAGENT);
522 }
523 }
524
John Reck7dc444b2011-06-16 17:44:29 -0700525 public static int getAdjustedMinimumFontSize(int rawValue) {
526 rawValue++; // Preference starts at 0, min font at 1
527 if (rawValue > 1) {
528 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
529 }
530 return rawValue;
531 }
532
John Reck5ba3c762011-09-14 15:00:15 -0700533 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700534 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700535 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700536 }
537
538 static int getRawTextZoom(int percent) {
539 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
540 }
541
John Reckcadae722011-07-25 11:36:17 -0700542 public SharedPreferences getPreferences() {
543 return mPrefs;
544 }
545
John Reck35e9dd62011-04-25 09:01:54 -0700546 // -----------------------------
547 // getter/setters for accessibility_preferences.xml
548 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100549
John Reck7dc444b2011-06-16 17:44:29 -0700550 @Deprecated
551 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700552 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
553 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100554 }
555
John Reck35e9dd62011-04-25 09:01:54 -0700556 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700557 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700558 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100559 }
560
John Reck92f25f82011-04-26 16:57:10 -0700561 public boolean forceEnableUserScalable() {
562 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
563 }
564
John Reck7dc444b2011-06-16 17:44:29 -0700565 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700566 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700567 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
568 return getAdjustedTextZoom(textZoom);
569 }
570
571 public void setTextZoom(int percent) {
572 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
573 }
574
John Reck35e9dd62011-04-25 09:01:54 -0700575 // -----------------------------
576 // getter/setters for advanced_preferences.xml
577 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100578
John Reck35e9dd62011-04-25 09:01:54 -0700579 public String getSearchEngineName() {
580 return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100581 }
John Reck63bb6872010-12-01 19:29:32 -0800582
John Reck35e9dd62011-04-25 09:01:54 -0700583 public boolean openInBackground() {
584 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800585 }
John Reck35e9dd62011-04-25 09:01:54 -0700586
587 public boolean enableJavascript() {
588 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
589 }
590
591 // TODO: Cache
592 public PluginState getPluginState() {
593 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
594 return PluginState.valueOf(state);
595 }
596
597 // TODO: Cache
598 public ZoomDensity getDefaultZoom() {
599 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
600 return ZoomDensity.valueOf(zoom);
601 }
602
603 public boolean loadPageInOverviewMode() {
604 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
605 }
606
607 public boolean autofitPages() {
608 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
609 }
610
611 public boolean blockPopupWindows() {
612 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
613 }
614
615 public boolean loadImages() {
616 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
617 }
618
619 public String getDefaultTextEncoding() {
620 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
621 }
622
623 // -----------------------------
624 // getter/setters for general_preferences.xml
625 // -----------------------------
626
627 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700628 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
629 }
630
631 public void setHomePage(String value) {
632 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
633 }
634
635 public boolean isAutofillEnabled() {
636 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
637 }
638
639 public void setAutofillEnabled(boolean value) {
640 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
641 }
642
643 // -----------------------------
644 // getter/setters for debug_preferences.xml
645 // -----------------------------
646
647 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700648 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700649 return true;
650 }
651 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
652 }
653
Derek Sollenberger31adf672011-07-08 11:31:30 -0400654 public boolean isSkiaHardwareAccelerated() {
655 if (!isDebugEnabled()) {
656 return false;
657 }
658 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
659 }
660
John Reck35e9dd62011-04-25 09:01:54 -0700661 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700662 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700663 return 0;
664 }
665 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
666 }
667
668 // -----------------------------
669 // getter/setters for hidden_debug_preferences.xml
670 // -----------------------------
671
672 public boolean enableVisualIndicator() {
673 if (!isDebugEnabled()) {
674 return false;
675 }
676 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
677 }
678
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700679 public boolean enableCpuUploadPath() {
680 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700681 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700682 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700683 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700684 }
685
John Reck35e9dd62011-04-25 09:01:54 -0700686 public boolean enableJavascriptConsole() {
687 if (!isDebugEnabled()) {
688 return false;
689 }
690 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
691 }
692
693 public boolean isSmallScreen() {
694 if (!isDebugEnabled()) {
695 return false;
696 }
697 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
698 }
699
700 public boolean isWideViewport() {
701 if (!isDebugEnabled()) {
702 return true;
703 }
704 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
705 }
706
707 public boolean isNormalLayout() {
708 if (!isDebugEnabled()) {
709 return false;
710 }
711 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
712 }
713
714 public boolean isTracing() {
715 if (!isDebugEnabled()) {
716 return false;
717 }
718 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
719 }
720
721 public boolean enableLightTouch() {
722 if (!isDebugEnabled()) {
723 return false;
724 }
725 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
726 }
727
728 public boolean enableNavDump() {
729 if (!isDebugEnabled()) {
730 return false;
731 }
732 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
733 }
734
735 public String getJsEngineFlags() {
736 if (!isDebugEnabled()) {
737 return "";
738 }
739 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
740 }
741
742 // -----------------------------
743 // getter/setters for lab_preferences.xml
744 // -----------------------------
745
746 public boolean useQuickControls() {
747 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
748 }
749
750 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700751 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700752 }
753
754 public boolean useInstantSearch() {
755 return mPrefs.getBoolean(PREF_USE_INSTANT_SEARCH, false);
756 }
757
Michael Kolbc38c6042011-04-27 10:46:06 -0700758 public boolean useFullscreen() {
759 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
760 }
761
John Reck2fd9d0e2011-07-15 11:13:48 -0700762 public boolean useInvertedRendering() {
763 return mPrefs.getBoolean(PREF_INVERTED, false);
764 }
765
Nicolas Roard5d513102011-08-03 15:35:34 -0700766 public float getInvertedContrast() {
767 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
768 }
769
John Reck35e9dd62011-04-25 09:01:54 -0700770 // -----------------------------
771 // getter/setters for privacy_security_preferences.xml
772 // -----------------------------
773
774 public boolean showSecurityWarnings() {
775 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
776 }
777
778 public boolean acceptCookies() {
779 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
780 }
781
782 public boolean saveFormdata() {
783 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
784 }
785
786 public boolean enableGeolocation() {
787 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
788 }
789
790 public boolean rememberPasswords() {
791 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
792 }
793
Michael Kolb14612442011-06-24 13:06:29 -0700794 // -----------------------------
795 // getter/setters for bandwidth_preferences.xml
796 // -----------------------------
797
Mathew Inwood467813f2011-09-02 15:43:17 +0100798 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
799 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -0700800 }
Mathew Inwood467813f2011-09-02 15:43:17 +0100801
802 public static String getPreloadAlwaysPreferenceString(Context context) {
803 return context.getResources().getString(R.string.pref_data_preload_value_always);
804 }
805
Mathew Inwood825fba72011-10-04 14:52:52 +0100806 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
807 "browser_default_preload_setting";
808
809 public String getDefaultPreloadSetting() {
810 String preload = Settings.Secure.getString(mContext.getContentResolver(),
811 DEAULT_PRELOAD_SECURE_SETTING_KEY);
812 if (preload == null) {
813 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
814 }
815 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +0100816 }
817
818 public String getPreloadEnabled() {
819 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
820 }
821
The Android Open Source Project0c908882009-03-03 19:32:16 -0800822}