blob: 2c0b2ec750afabfb477bd9b89a32bec1afc74cea [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;
John Reck5ba3c762011-09-14 15:00:15 -070028import android.util.DisplayMetrics;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010030import android.webkit.GeolocationPermissions;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.webkit.WebIconDatabase;
32import android.webkit.WebSettings;
Ben Murdoch0cb81892010-10-08 12:41:33 +010033import android.webkit.WebSettings.AutoFillProfile;
John Reck35e9dd62011-04-25 09:01:54 -070034import android.webkit.WebSettings.LayoutAlgorithm;
35import android.webkit.WebSettings.PluginState;
36import android.webkit.WebSettings.TextSize;
37import android.webkit.WebSettings.ZoomDensity;
Nicolas Roard78a98e42009-05-11 13:34:17 +010038import android.webkit.WebStorage;
John Reck812d2d62011-01-18 14:16:15 -080039import android.webkit.WebView;
40import android.webkit.WebViewDatabase;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041
John Reck46500332011-06-07 14:36:10 -070042import com.android.browser.homepages.HomeProvider;
43import com.android.browser.provider.BrowserProvider;
44import com.android.browser.search.SearchEngine;
45import com.android.browser.search.SearchEngines;
46
John Reck35e9dd62011-04-25 09:01:54 -070047import java.lang.ref.WeakReference;
48import java.util.Iterator;
49import java.util.LinkedList;
John Reckb8b2af82011-05-20 15:58:33 -070050import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051
John Reck35e9dd62011-04-25 09:01:54 -070052/**
53 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080054 */
John Reck35e9dd62011-04-25 09:01:54 -070055public class BrowserSettings implements OnSharedPreferenceChangeListener,
56 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010057
John Reck35e9dd62011-04-25 09:01:54 -070058 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070059 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
60 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
61 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080062
63 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070064 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
65 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070066
67 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070068 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
69 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070070
71 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070072 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
73 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080074
John Reck7fd1e8f2011-04-27 18:22:57 -070075 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
76 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
77 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
78
John Reck35e9dd62011-04-25 09:01:54 -070079 private static final String USER_AGENTS[] = { null,
80 DESKTOP_USERAGENT,
81 IPHONE_USERAGENT,
82 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070083 FROYO_USERAGENT,
84 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070085 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080086
John Reck8fc22a12011-06-16 14:57:41 -070087 // The minimum min font size
88 // Aka, the lower bounds for the min font size range
89 // which is 1:5..24
90 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070091 // The initial value in the text zoom range
92 // This is what represents 100% in the SeekBarPreference range
93 private static final int TEXT_ZOOM_START_VAL = 10;
94 // The size of a single step in the text zoom range, in percent
95 private static final int TEXT_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -070096
John Reck35e9dd62011-04-25 09:01:54 -070097 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -070098
John Reck35e9dd62011-04-25 09:01:54 -070099 private Context mContext;
100 private SharedPreferences mPrefs;
101 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700102 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700103 private WebStorageSizeManager mWebStorageSizeManager;
104 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700105 private WeakHashMap<WebSettings, String> mCustomUserAgents;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100106 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -0700107 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -0700108 private float mFontSizeMult = 1.0f;
John Reck78a6a1d2011-07-21 13:54:03 -0700109
110 // Cached values
111 private int mPageCacheCapacity = 1;
112 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800113
John Reck35e9dd62011-04-25 09:01:54 -0700114 // Cached settings
115 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800116
Ben Murdochaaa1f372011-07-25 15:40:58 +0100117 private static String sFactoryResetUrl;
118
John Reck35e9dd62011-04-25 09:01:54 -0700119 public static void initialize(final Context context) {
120 sInstance = new BrowserSettings(context);
121 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800122
John Reck35e9dd62011-04-25 09:01:54 -0700123 public static BrowserSettings getInstance() {
124 return sInstance;
125 }
Ben Murdochef671652010-11-25 17:17:58 +0000126
John Reck35e9dd62011-04-25 09:01:54 -0700127 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100128 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700129 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
130 mAutofillHandler = new AutofillHandler(mContext);
131 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700132 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reck35e9dd62011-04-25 09:01:54 -0700133 mAutofillHandler.asyncLoadFromDb();
John Reckcadae722011-07-25 11:36:17 -0700134 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700135 }
136
137 public void setController(Controller controller) {
138 mController = controller;
John Reckc477e712011-08-17 11:27:15 -0700139 if (sInitialized) {
140 syncSharedSettings();
141 }
John Reck35e9dd62011-04-25 09:01:54 -0700142
143 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
144 ((InstantSearchEngine) mSearchEngine).setController(mController);
Ben Murdochef671652010-11-25 17:17:58 +0000145 }
146 }
147
John Reck35e9dd62011-04-25 09:01:54 -0700148 public void startManagingSettings(WebSettings settings) {
John Reckc477e712011-08-17 11:27:15 -0700149 if (mNeedsSharedSync) {
150 syncSharedSettings();
151 }
John Reck35e9dd62011-04-25 09:01:54 -0700152 synchronized (mManagedSettings) {
153 syncStaticSettings(settings);
154 syncSetting(settings);
155 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800156 }
157 }
158
John Reckcadae722011-07-25 11:36:17 -0700159 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700160
161 @Override
162 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700163 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
164 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700165 // the cost of one cached page is ~3M (measured using nytimes.com). For
166 // low end devices, we only cache one page. For high end devices, we try
167 // to cache more pages, currently choose 5.
168 if (ActivityManager.staticGetMemoryClass() > 16) {
169 mPageCacheCapacity = 5;
170 }
171 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
172 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
173 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700174 // Workaround b/5253777
175 CookieManager.getInstance().acceptCookie();
176 // Workaround b/5254577
177 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700178 if (Build.VERSION.CODENAME.equals("REL")) {
179 // This is a release build, always startup with debug disabled
180 setDebugEnabled(false);
181 }
182 if (mPrefs.contains(PREF_TEXT_SIZE)) {
183 /*
184 * Update from TextSize enum to zoom percent
185 * SMALLEST is 50%
186 * SMALLER is 75%
187 * NORMAL is 100%
188 * LARGER is 150%
189 * LARGEST is 200%
190 */
191 switch (getTextSize()) {
192 case SMALLEST:
193 setTextZoom(50);
194 break;
195 case SMALLER:
196 setTextZoom(75);
197 break;
198 case LARGER:
199 setTextZoom(150);
200 break;
201 case LARGEST:
202 setTextZoom(200);
203 break;
204 }
205 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
206 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100207
208 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
209 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
210 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
211 BrowserProvider.getClientId(mContext.getContentResolver()));
212 }
213
Ben Murdochaaa1f372011-07-25 15:40:58 +0100214 synchronized (BrowserSettings.class) {
215 sInitialized = true;
216 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700217 }
218 }
219 };
220
Ben Murdochaaa1f372011-07-25 15:40:58 +0100221 private static void requireInitialization() {
222 synchronized (BrowserSettings.class) {
223 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700224 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100225 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700226 } catch (InterruptedException e) {
227 }
228 }
229 }
230 }
231
The Android Open Source Project0c908882009-03-03 19:32:16 -0800232 /**
John Reck35e9dd62011-04-25 09:01:54 -0700233 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800234 */
John Reck35e9dd62011-04-25 09:01:54 -0700235 private void syncSetting(WebSettings settings) {
236 settings.setGeolocationEnabled(enableGeolocation());
237 settings.setJavaScriptEnabled(enableJavascript());
238 settings.setLightTouchEnabled(enableLightTouch());
239 settings.setNavDump(enableNavDump());
Derek Sollenberger31adf672011-07-08 11:31:30 -0400240 settings.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
John Reck35e9dd62011-04-25 09:01:54 -0700241 settings.setShowVisualIndicator(enableVisualIndicator());
242 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
243 settings.setDefaultZoom(getDefaultZoom());
244 settings.setMinimumFontSize(getMinimumFontSize());
245 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck92f25f82011-04-26 16:57:10 -0700246 settings.setForceUserScalable(forceEnableUserScalable());
John Reck35e9dd62011-04-25 09:01:54 -0700247 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700248 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700249 settings.setAutoFillEnabled(isAutofillEnabled());
250 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100251 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700252 settings.setLoadsImagesAutomatically(loadImages());
253 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
254 settings.setSavePassword(rememberPasswords());
255 settings.setSaveFormData(saveFormdata());
256 settings.setUseWideViewPort(isWideViewport());
257 settings.setAutoFillProfile(getAutoFillProfile());
John Reckb8b2af82011-05-20 15:58:33 -0700258
259 String ua = mCustomUserAgents.get(settings);
John Reck46500332011-06-07 14:36:10 -0700260 if (ua != null) {
John Reckb8b2af82011-05-20 15:58:33 -0700261 settings.setUserAgentString(ua);
262 } else {
263 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
264 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700265
John Reckeabe5da2011-08-08 13:24:13 -0700266 settings.setProperty(WebViewProperties.gfxInvertedScreen,
John Reck2fd9d0e2011-07-15 11:13:48 -0700267 useInvertedRendering() ? "true" : "false");
Nicolas Roard5d513102011-08-03 15:35:34 -0700268
John Reckeabe5da2011-08-08 13:24:13 -0700269 settings.setProperty(WebViewProperties.gfxInvertedScreenContrast,
Nicolas Roard5d513102011-08-03 15:35:34 -0700270 Float.toString(getInvertedContrast()));
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700271
272 settings.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
273 enableCpuUploadPath() ? "true" : "false");
Ben Murdochef671652010-11-25 17:17:58 +0000274 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800275
John Reck35e9dd62011-04-25 09:01:54 -0700276 /**
277 * Syncs all the settings that have no UI
278 * These cannot change, so we only need to set them once per WebSettings
279 */
280 private void syncStaticSettings(WebSettings settings) {
281 settings.setDefaultFontSize(16);
282 settings.setDefaultFixedFontSize(13);
283 settings.setPageCacheCapacity(getPageCacheCapacity());
Ben Murdochef671652010-11-25 17:17:58 +0000284
John Reck35e9dd62011-04-25 09:01:54 -0700285 // WebView inside Browser doesn't want initial focus to be set.
286 settings.setNeedInitialFocus(false);
287 // Browser supports multiple windows
288 settings.setSupportMultipleWindows(true);
289 // enable smooth transition for better performance during panning or
290 // zooming
291 settings.setEnableSmoothTransition(true);
292 // disable content url access
293 settings.setAllowContentAccess(false);
294
295 // HTML5 API flags
296 settings.setAppCacheEnabled(true);
297 settings.setDatabaseEnabled(true);
298 settings.setDomStorageEnabled(true);
299 settings.setWorkersEnabled(true); // This only affects V8.
300
301 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700302 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700303 settings.setAppCachePath(getAppCachePath());
304 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
305 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
306 }
307
308 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700309 mNeedsSharedSync = false;
John Reck35e9dd62011-04-25 09:01:54 -0700310 CookieManager.getInstance().setAcceptCookie(acceptCookies());
311 if (mController != null) {
312 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700313 }
John Reck35e9dd62011-04-25 09:01:54 -0700314 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700315
John Reck35e9dd62011-04-25 09:01:54 -0700316 private void syncManagedSettings() {
317 syncSharedSettings();
318 synchronized (mManagedSettings) {
319 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
320 while (iter.hasNext()) {
321 WeakReference<WebSettings> ref = iter.next();
322 WebSettings settings = ref.get();
323 if (settings == null) {
324 iter.remove();
325 continue;
326 }
327 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000328 }
John Reck35e9dd62011-04-25 09:01:54 -0700329 }
330 }
Ben Murdochef671652010-11-25 17:17:58 +0000331
John Reck35e9dd62011-04-25 09:01:54 -0700332 @Override
333 public void onSharedPreferenceChanged(
334 SharedPreferences sharedPreferences, String key) {
335 syncManagedSettings();
336 if (PREF_SEARCH_ENGINE.equals(key)) {
337 updateSearchEngine(false);
338 }
339 if (PREF_USE_INSTANT_SEARCH.equals(key)) {
340 updateSearchEngine(true);
341 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700342 if (PREF_FULLSCREEN.equals(key)) {
343 if (mController.getUi() != null) {
344 mController.getUi().setFullscreen(useFullscreen());
345 }
Michael Kolb0241e752011-07-07 14:58:50 -0700346 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
347 if (mController.getUi() != null) {
348 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
349 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700350 }
John Reck35e9dd62011-04-25 09:01:54 -0700351 }
352
John Reck961d35d2011-06-23 09:45:54 -0700353 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100354 requireInitialization();
355 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700356 }
357
358 public LayoutAlgorithm getLayoutAlgorithm() {
359 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
360 if (autofitPages()) {
361 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
362 }
363 if (isDebugEnabled()) {
364 if (isSmallScreen()) {
365 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000366 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700367 if (isNormalLayout()) {
368 layoutAlgorithm = LayoutAlgorithm.NORMAL;
369 } else {
370 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
371 }
Ben Murdochef671652010-11-25 17:17:58 +0000372 }
John Reck35e9dd62011-04-25 09:01:54 -0700373 }
374 return layoutAlgorithm;
375 }
Ben Murdochef671652010-11-25 17:17:58 +0000376
John Reck35e9dd62011-04-25 09:01:54 -0700377 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700378 requireInitialization();
379 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800380 }
381
John Reck35e9dd62011-04-25 09:01:54 -0700382 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700383 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700384 return mWebStorageSizeManager;
385 }
386
John Reck35e9dd62011-04-25 09:01:54 -0700387 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700388 if (mAppCachePath == null) {
389 mAppCachePath = mContext.getDir("appcache", 0).getPath();
390 }
391 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700392 }
393
394 private void updateSearchEngine(boolean force) {
395 String searchEngineName = getSearchEngineName();
396 if (force || mSearchEngine == null ||
397 !mSearchEngine.getName().equals(searchEngineName)) {
398 if (mSearchEngine != null) {
399 if (mSearchEngine.supportsVoiceSearch()) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000400 // One or more tabs could have been in voice search mode.
401 // Clear it, since the new SearchEngine may not support
402 // it, or may handle it differently.
403 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
404 mController.getTabControl().getTab(i).revertVoiceSearchMode();
405 }
406 }
John Reck35e9dd62011-04-25 09:01:54 -0700407 mSearchEngine.close();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000408 }
John Reck35e9dd62011-04-25 09:01:54 -0700409 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000410
John Reck35e9dd62011-04-25 09:01:54 -0700411 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
412 ((InstantSearchEngine) mSearchEngine).setController(mController);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000413 }
414 }
415 }
416
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100417 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700418 if (mSearchEngine == null) {
419 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100420 }
John Reck35e9dd62011-04-25 09:01:54 -0700421 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100422 }
423
John Reck35e9dd62011-04-25 09:01:54 -0700424 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700425 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700426 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100427 }
428
John Reck35e9dd62011-04-25 09:01:54 -0700429 public void setDebugEnabled(boolean value) {
430 mPrefs.edit().putBoolean(PREF_DEBUG_MENU, value).apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100431 }
432
John Reck35e9dd62011-04-25 09:01:54 -0700433 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800434 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700435 if (mController != null) {
436 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800437 if (current != null) {
438 current.clearCache(true);
439 }
440 }
441 }
442
John Reck35e9dd62011-04-25 09:01:54 -0700443 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800444 CookieManager.getInstance().removeAllCookie();
445 }
446
John Reck35e9dd62011-04-25 09:01:54 -0700447 public void clearHistory() {
448 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800449 Browser.clearHistory(resolver);
450 Browser.clearSearches(resolver);
451 }
452
John Reck35e9dd62011-04-25 09:01:54 -0700453 public void clearFormData() {
454 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700455 if (mController!= null) {
456 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100457 if (currentTopView != null) {
458 currentTopView.clearFormData();
459 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800460 }
461 }
462
John Reck35e9dd62011-04-25 09:01:54 -0700463 public void clearPasswords() {
464 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800465 db.clearUsernamePassword();
466 db.clearHttpAuthUsernamePassword();
467 }
468
John Reck35e9dd62011-04-25 09:01:54 -0700469 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100470 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100471 }
472
John Reck35e9dd62011-04-25 09:01:54 -0700473 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100474 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100475 }
476
John Reck35e9dd62011-04-25 09:01:54 -0700477 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700478 // Preserve autologin setting
479 long gal = mPrefs.getLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, -1);
480 mPrefs.edit()
481 .clear()
482 .putLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, gal)
483 .apply();
John Reck35e9dd62011-04-25 09:01:54 -0700484 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700485 }
486
John Reck35e9dd62011-04-25 09:01:54 -0700487 public AutoFillProfile getAutoFillProfile() {
488 mAutofillHandler.waitForLoad();
489 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800490 }
491
John Reck35e9dd62011-04-25 09:01:54 -0700492 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
493 mAutofillHandler.waitForLoad();
494 mAutofillHandler.setAutoFillProfile(profile, msg);
Ben Murdoch273330a2011-07-08 18:37:22 +0100495 // Auto-fill will reuse the same profile ID when making edits to the profile,
496 // so we need to force a settings sync (otherwise the SharedPreferences
497 // manager will optimise out the call to onSharedPreferenceChanged(), as
498 // it thinks nothing has changed).
499 syncManagedSettings();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800500 }
501
John Reck35e9dd62011-04-25 09:01:54 -0700502 public void toggleDebugSettings() {
503 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800504 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100505
John Reckb8b2af82011-05-20 15:58:33 -0700506 public boolean hasDesktopUseragent(WebView view) {
507 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
508 }
509
510 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700511 if (view == null) {
512 return;
513 }
John Reckb8b2af82011-05-20 15:58:33 -0700514 WebSettings settings = view.getSettings();
515 if (mCustomUserAgents.get(settings) != null) {
516 mCustomUserAgents.remove(settings);
517 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
518 } else {
519 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
520 settings.setUserAgentString(DESKTOP_USERAGENT);
521 }
522 }
523
John Reck7dc444b2011-06-16 17:44:29 -0700524 public static int getAdjustedMinimumFontSize(int rawValue) {
525 rawValue++; // Preference starts at 0, min font at 1
526 if (rawValue > 1) {
527 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
528 }
529 return rawValue;
530 }
531
John Reck5ba3c762011-09-14 15:00:15 -0700532 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700533 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700534 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700535 }
536
537 static int getRawTextZoom(int percent) {
538 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
539 }
540
John Reckcadae722011-07-25 11:36:17 -0700541 public SharedPreferences getPreferences() {
542 return mPrefs;
543 }
544
John Reck35e9dd62011-04-25 09:01:54 -0700545 // -----------------------------
546 // getter/setters for accessibility_preferences.xml
547 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100548
John Reck7dc444b2011-06-16 17:44:29 -0700549 @Deprecated
550 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700551 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
552 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100553 }
554
John Reck35e9dd62011-04-25 09:01:54 -0700555 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700556 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700557 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100558 }
559
John Reck92f25f82011-04-26 16:57:10 -0700560 public boolean forceEnableUserScalable() {
561 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
562 }
563
John Reck7dc444b2011-06-16 17:44:29 -0700564 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700565 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700566 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
567 return getAdjustedTextZoom(textZoom);
568 }
569
570 public void setTextZoom(int percent) {
571 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
572 }
573
John Reck35e9dd62011-04-25 09:01:54 -0700574 // -----------------------------
575 // getter/setters for advanced_preferences.xml
576 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100577
John Reck35e9dd62011-04-25 09:01:54 -0700578 public String getSearchEngineName() {
579 return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100580 }
John Reck63bb6872010-12-01 19:29:32 -0800581
John Reck35e9dd62011-04-25 09:01:54 -0700582 public boolean openInBackground() {
583 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800584 }
John Reck35e9dd62011-04-25 09:01:54 -0700585
586 public boolean enableJavascript() {
587 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
588 }
589
590 // TODO: Cache
591 public PluginState getPluginState() {
592 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
593 return PluginState.valueOf(state);
594 }
595
596 // TODO: Cache
597 public ZoomDensity getDefaultZoom() {
598 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
599 return ZoomDensity.valueOf(zoom);
600 }
601
602 public boolean loadPageInOverviewMode() {
603 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
604 }
605
606 public boolean autofitPages() {
607 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
608 }
609
610 public boolean blockPopupWindows() {
611 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
612 }
613
614 public boolean loadImages() {
615 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
616 }
617
618 public String getDefaultTextEncoding() {
619 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
620 }
621
622 // -----------------------------
623 // getter/setters for general_preferences.xml
624 // -----------------------------
625
626 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700627 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
628 }
629
630 public void setHomePage(String value) {
631 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
632 }
633
634 public boolean isAutofillEnabled() {
635 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
636 }
637
638 public void setAutofillEnabled(boolean value) {
639 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
640 }
641
642 // -----------------------------
643 // getter/setters for debug_preferences.xml
644 // -----------------------------
645
646 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700647 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700648 return true;
649 }
650 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
651 }
652
Derek Sollenberger31adf672011-07-08 11:31:30 -0400653 public boolean isSkiaHardwareAccelerated() {
654 if (!isDebugEnabled()) {
655 return false;
656 }
657 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
658 }
659
John Reck35e9dd62011-04-25 09:01:54 -0700660 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700661 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700662 return 0;
663 }
664 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
665 }
666
667 // -----------------------------
668 // getter/setters for hidden_debug_preferences.xml
669 // -----------------------------
670
671 public boolean enableVisualIndicator() {
672 if (!isDebugEnabled()) {
673 return false;
674 }
675 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
676 }
677
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700678 public boolean enableCpuUploadPath() {
679 if (!isDebugEnabled()) {
680 return true;
681 }
682 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, true);
683 }
684
John Reck35e9dd62011-04-25 09:01:54 -0700685 public boolean enableJavascriptConsole() {
686 if (!isDebugEnabled()) {
687 return false;
688 }
689 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
690 }
691
692 public boolean isSmallScreen() {
693 if (!isDebugEnabled()) {
694 return false;
695 }
696 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
697 }
698
699 public boolean isWideViewport() {
700 if (!isDebugEnabled()) {
701 return true;
702 }
703 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
704 }
705
706 public boolean isNormalLayout() {
707 if (!isDebugEnabled()) {
708 return false;
709 }
710 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
711 }
712
713 public boolean isTracing() {
714 if (!isDebugEnabled()) {
715 return false;
716 }
717 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
718 }
719
720 public boolean enableLightTouch() {
721 if (!isDebugEnabled()) {
722 return false;
723 }
724 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
725 }
726
727 public boolean enableNavDump() {
728 if (!isDebugEnabled()) {
729 return false;
730 }
731 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
732 }
733
734 public String getJsEngineFlags() {
735 if (!isDebugEnabled()) {
736 return "";
737 }
738 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
739 }
740
741 // -----------------------------
742 // getter/setters for lab_preferences.xml
743 // -----------------------------
744
745 public boolean useQuickControls() {
746 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
747 }
748
749 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700750 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700751 }
752
753 public boolean useInstantSearch() {
754 return mPrefs.getBoolean(PREF_USE_INSTANT_SEARCH, false);
755 }
756
Michael Kolbc38c6042011-04-27 10:46:06 -0700757 public boolean useFullscreen() {
758 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
759 }
760
John Reck2fd9d0e2011-07-15 11:13:48 -0700761 public boolean useInvertedRendering() {
762 return mPrefs.getBoolean(PREF_INVERTED, false);
763 }
764
Nicolas Roard5d513102011-08-03 15:35:34 -0700765 public float getInvertedContrast() {
766 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
767 }
768
John Reck35e9dd62011-04-25 09:01:54 -0700769 // -----------------------------
770 // getter/setters for privacy_security_preferences.xml
771 // -----------------------------
772
773 public boolean showSecurityWarnings() {
774 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
775 }
776
777 public boolean acceptCookies() {
778 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
779 }
780
781 public boolean saveFormdata() {
782 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
783 }
784
785 public boolean enableGeolocation() {
786 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
787 }
788
789 public boolean rememberPasswords() {
790 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
791 }
792
Michael Kolb14612442011-06-24 13:06:29 -0700793 // -----------------------------
794 // getter/setters for bandwidth_preferences.xml
795 // -----------------------------
796
Mathew Inwood467813f2011-09-02 15:43:17 +0100797 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
798 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -0700799 }
Mathew Inwood467813f2011-09-02 15:43:17 +0100800
801 public static String getPreloadAlwaysPreferenceString(Context context) {
802 return context.getResources().getString(R.string.pref_data_preload_value_always);
803 }
804
805 private String getDefaultPreloadSetting() {
Mathew Inwood75905982011-09-07 14:24:06 +0100806 return mContext.getResources().getString(R.string.pref_data_preload_default_value);
Mathew Inwood467813f2011-09-02 15:43:17 +0100807 }
808
809 public String getPreloadEnabled() {
810 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
811 }
812
The Android Open Source Project0c908882009-03-03 19:32:16 -0800813}