blob: cfaa18aa39b79cabd3037b2a6f78ab13d49960e1 [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()));
Ben Murdochef671652010-11-25 17:17:58 +0000271 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800272
John Reck35e9dd62011-04-25 09:01:54 -0700273 /**
274 * Syncs all the settings that have no UI
275 * These cannot change, so we only need to set them once per WebSettings
276 */
277 private void syncStaticSettings(WebSettings settings) {
278 settings.setDefaultFontSize(16);
279 settings.setDefaultFixedFontSize(13);
280 settings.setPageCacheCapacity(getPageCacheCapacity());
Ben Murdochef671652010-11-25 17:17:58 +0000281
John Reck35e9dd62011-04-25 09:01:54 -0700282 // WebView inside Browser doesn't want initial focus to be set.
283 settings.setNeedInitialFocus(false);
284 // Browser supports multiple windows
285 settings.setSupportMultipleWindows(true);
286 // enable smooth transition for better performance during panning or
287 // zooming
288 settings.setEnableSmoothTransition(true);
289 // disable content url access
290 settings.setAllowContentAccess(false);
291
292 // HTML5 API flags
293 settings.setAppCacheEnabled(true);
294 settings.setDatabaseEnabled(true);
295 settings.setDomStorageEnabled(true);
296 settings.setWorkersEnabled(true); // This only affects V8.
297
298 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700299 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700300 settings.setAppCachePath(getAppCachePath());
301 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
302 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
303 }
304
305 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700306 mNeedsSharedSync = false;
John Reck35e9dd62011-04-25 09:01:54 -0700307 CookieManager.getInstance().setAcceptCookie(acceptCookies());
308 if (mController != null) {
309 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700310 }
John Reck35e9dd62011-04-25 09:01:54 -0700311 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700312
John Reck35e9dd62011-04-25 09:01:54 -0700313 private void syncManagedSettings() {
314 syncSharedSettings();
315 synchronized (mManagedSettings) {
316 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
317 while (iter.hasNext()) {
318 WeakReference<WebSettings> ref = iter.next();
319 WebSettings settings = ref.get();
320 if (settings == null) {
321 iter.remove();
322 continue;
323 }
324 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000325 }
John Reck35e9dd62011-04-25 09:01:54 -0700326 }
327 }
Ben Murdochef671652010-11-25 17:17:58 +0000328
John Reck35e9dd62011-04-25 09:01:54 -0700329 @Override
330 public void onSharedPreferenceChanged(
331 SharedPreferences sharedPreferences, String key) {
332 syncManagedSettings();
333 if (PREF_SEARCH_ENGINE.equals(key)) {
334 updateSearchEngine(false);
335 }
336 if (PREF_USE_INSTANT_SEARCH.equals(key)) {
337 updateSearchEngine(true);
338 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700339 if (PREF_FULLSCREEN.equals(key)) {
340 if (mController.getUi() != null) {
341 mController.getUi().setFullscreen(useFullscreen());
342 }
Michael Kolb0241e752011-07-07 14:58:50 -0700343 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
344 if (mController.getUi() != null) {
345 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
346 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700347 }
John Reck35e9dd62011-04-25 09:01:54 -0700348 }
349
John Reck961d35d2011-06-23 09:45:54 -0700350 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100351 requireInitialization();
352 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700353 }
354
355 public LayoutAlgorithm getLayoutAlgorithm() {
356 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
357 if (autofitPages()) {
358 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
359 }
360 if (isDebugEnabled()) {
361 if (isSmallScreen()) {
362 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000363 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700364 if (isNormalLayout()) {
365 layoutAlgorithm = LayoutAlgorithm.NORMAL;
366 } else {
367 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
368 }
Ben Murdochef671652010-11-25 17:17:58 +0000369 }
John Reck35e9dd62011-04-25 09:01:54 -0700370 }
371 return layoutAlgorithm;
372 }
Ben Murdochef671652010-11-25 17:17:58 +0000373
John Reck35e9dd62011-04-25 09:01:54 -0700374 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700375 requireInitialization();
376 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800377 }
378
John Reck35e9dd62011-04-25 09:01:54 -0700379 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700380 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700381 return mWebStorageSizeManager;
382 }
383
John Reck35e9dd62011-04-25 09:01:54 -0700384 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700385 if (mAppCachePath == null) {
386 mAppCachePath = mContext.getDir("appcache", 0).getPath();
387 }
388 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700389 }
390
391 private void updateSearchEngine(boolean force) {
392 String searchEngineName = getSearchEngineName();
393 if (force || mSearchEngine == null ||
394 !mSearchEngine.getName().equals(searchEngineName)) {
395 if (mSearchEngine != null) {
396 if (mSearchEngine.supportsVoiceSearch()) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000397 // One or more tabs could have been in voice search mode.
398 // Clear it, since the new SearchEngine may not support
399 // it, or may handle it differently.
400 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
401 mController.getTabControl().getTab(i).revertVoiceSearchMode();
402 }
403 }
John Reck35e9dd62011-04-25 09:01:54 -0700404 mSearchEngine.close();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000405 }
John Reck35e9dd62011-04-25 09:01:54 -0700406 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000407
John Reck35e9dd62011-04-25 09:01:54 -0700408 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
409 ((InstantSearchEngine) mSearchEngine).setController(mController);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000410 }
411 }
412 }
413
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100414 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700415 if (mSearchEngine == null) {
416 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100417 }
John Reck35e9dd62011-04-25 09:01:54 -0700418 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100419 }
420
John Reck35e9dd62011-04-25 09:01:54 -0700421 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700422 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700423 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100424 }
425
John Reck35e9dd62011-04-25 09:01:54 -0700426 public void setDebugEnabled(boolean value) {
427 mPrefs.edit().putBoolean(PREF_DEBUG_MENU, value).apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100428 }
429
John Reck35e9dd62011-04-25 09:01:54 -0700430 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800431 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700432 if (mController != null) {
433 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800434 if (current != null) {
435 current.clearCache(true);
436 }
437 }
438 }
439
John Reck35e9dd62011-04-25 09:01:54 -0700440 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800441 CookieManager.getInstance().removeAllCookie();
442 }
443
John Reck35e9dd62011-04-25 09:01:54 -0700444 public void clearHistory() {
445 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800446 Browser.clearHistory(resolver);
447 Browser.clearSearches(resolver);
448 }
449
John Reck35e9dd62011-04-25 09:01:54 -0700450 public void clearFormData() {
451 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700452 if (mController!= null) {
453 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100454 if (currentTopView != null) {
455 currentTopView.clearFormData();
456 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800457 }
458 }
459
John Reck35e9dd62011-04-25 09:01:54 -0700460 public void clearPasswords() {
461 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800462 db.clearUsernamePassword();
463 db.clearHttpAuthUsernamePassword();
464 }
465
John Reck35e9dd62011-04-25 09:01:54 -0700466 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100467 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100468 }
469
John Reck35e9dd62011-04-25 09:01:54 -0700470 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100471 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100472 }
473
John Reck35e9dd62011-04-25 09:01:54 -0700474 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700475 // Preserve autologin setting
476 long gal = mPrefs.getLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, -1);
477 mPrefs.edit()
478 .clear()
479 .putLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, gal)
480 .apply();
John Reck35e9dd62011-04-25 09:01:54 -0700481 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700482 }
483
John Reck35e9dd62011-04-25 09:01:54 -0700484 public AutoFillProfile getAutoFillProfile() {
485 mAutofillHandler.waitForLoad();
486 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800487 }
488
John Reck35e9dd62011-04-25 09:01:54 -0700489 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
490 mAutofillHandler.waitForLoad();
491 mAutofillHandler.setAutoFillProfile(profile, msg);
Ben Murdoch273330a2011-07-08 18:37:22 +0100492 // Auto-fill will reuse the same profile ID when making edits to the profile,
493 // so we need to force a settings sync (otherwise the SharedPreferences
494 // manager will optimise out the call to onSharedPreferenceChanged(), as
495 // it thinks nothing has changed).
496 syncManagedSettings();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800497 }
498
John Reck35e9dd62011-04-25 09:01:54 -0700499 public void toggleDebugSettings() {
500 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800501 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100502
John Reckb8b2af82011-05-20 15:58:33 -0700503 public boolean hasDesktopUseragent(WebView view) {
504 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
505 }
506
507 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700508 if (view == null) {
509 return;
510 }
John Reckb8b2af82011-05-20 15:58:33 -0700511 WebSettings settings = view.getSettings();
512 if (mCustomUserAgents.get(settings) != null) {
513 mCustomUserAgents.remove(settings);
514 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
515 } else {
516 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
517 settings.setUserAgentString(DESKTOP_USERAGENT);
518 }
519 }
520
John Reck7dc444b2011-06-16 17:44:29 -0700521 public static int getAdjustedMinimumFontSize(int rawValue) {
522 rawValue++; // Preference starts at 0, min font at 1
523 if (rawValue > 1) {
524 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
525 }
526 return rawValue;
527 }
528
John Reck5ba3c762011-09-14 15:00:15 -0700529 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700530 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700531 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700532 }
533
534 static int getRawTextZoom(int percent) {
535 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
536 }
537
John Reckcadae722011-07-25 11:36:17 -0700538 public SharedPreferences getPreferences() {
539 return mPrefs;
540 }
541
John Reck35e9dd62011-04-25 09:01:54 -0700542 // -----------------------------
543 // getter/setters for accessibility_preferences.xml
544 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100545
John Reck7dc444b2011-06-16 17:44:29 -0700546 @Deprecated
547 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700548 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
549 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100550 }
551
John Reck35e9dd62011-04-25 09:01:54 -0700552 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700553 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700554 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100555 }
556
John Reck92f25f82011-04-26 16:57:10 -0700557 public boolean forceEnableUserScalable() {
558 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
559 }
560
John Reck7dc444b2011-06-16 17:44:29 -0700561 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700562 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700563 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
564 return getAdjustedTextZoom(textZoom);
565 }
566
567 public void setTextZoom(int percent) {
568 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
569 }
570
John Reck35e9dd62011-04-25 09:01:54 -0700571 // -----------------------------
572 // getter/setters for advanced_preferences.xml
573 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100574
John Reck35e9dd62011-04-25 09:01:54 -0700575 public String getSearchEngineName() {
576 return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100577 }
John Reck63bb6872010-12-01 19:29:32 -0800578
John Reck35e9dd62011-04-25 09:01:54 -0700579 public boolean openInBackground() {
580 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800581 }
John Reck35e9dd62011-04-25 09:01:54 -0700582
583 public boolean enableJavascript() {
584 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
585 }
586
587 // TODO: Cache
588 public PluginState getPluginState() {
589 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
590 return PluginState.valueOf(state);
591 }
592
593 // TODO: Cache
594 public ZoomDensity getDefaultZoom() {
595 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
596 return ZoomDensity.valueOf(zoom);
597 }
598
599 public boolean loadPageInOverviewMode() {
600 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
601 }
602
603 public boolean autofitPages() {
604 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
605 }
606
607 public boolean blockPopupWindows() {
608 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
609 }
610
611 public boolean loadImages() {
612 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
613 }
614
615 public String getDefaultTextEncoding() {
616 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
617 }
618
619 // -----------------------------
620 // getter/setters for general_preferences.xml
621 // -----------------------------
622
623 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700624 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
625 }
626
627 public void setHomePage(String value) {
628 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
629 }
630
631 public boolean isAutofillEnabled() {
632 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
633 }
634
635 public void setAutofillEnabled(boolean value) {
636 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
637 }
638
639 // -----------------------------
640 // getter/setters for debug_preferences.xml
641 // -----------------------------
642
643 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700644 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700645 return true;
646 }
647 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
648 }
649
Derek Sollenberger31adf672011-07-08 11:31:30 -0400650 public boolean isSkiaHardwareAccelerated() {
651 if (!isDebugEnabled()) {
652 return false;
653 }
654 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
655 }
656
John Reck35e9dd62011-04-25 09:01:54 -0700657 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700658 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700659 return 0;
660 }
661 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
662 }
663
664 // -----------------------------
665 // getter/setters for hidden_debug_preferences.xml
666 // -----------------------------
667
668 public boolean enableVisualIndicator() {
669 if (!isDebugEnabled()) {
670 return false;
671 }
672 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
673 }
674
675 public boolean enableJavascriptConsole() {
676 if (!isDebugEnabled()) {
677 return false;
678 }
679 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
680 }
681
682 public boolean isSmallScreen() {
683 if (!isDebugEnabled()) {
684 return false;
685 }
686 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
687 }
688
689 public boolean isWideViewport() {
690 if (!isDebugEnabled()) {
691 return true;
692 }
693 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
694 }
695
696 public boolean isNormalLayout() {
697 if (!isDebugEnabled()) {
698 return false;
699 }
700 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
701 }
702
703 public boolean isTracing() {
704 if (!isDebugEnabled()) {
705 return false;
706 }
707 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
708 }
709
710 public boolean enableLightTouch() {
711 if (!isDebugEnabled()) {
712 return false;
713 }
714 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
715 }
716
717 public boolean enableNavDump() {
718 if (!isDebugEnabled()) {
719 return false;
720 }
721 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
722 }
723
724 public String getJsEngineFlags() {
725 if (!isDebugEnabled()) {
726 return "";
727 }
728 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
729 }
730
731 // -----------------------------
732 // getter/setters for lab_preferences.xml
733 // -----------------------------
734
735 public boolean useQuickControls() {
736 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
737 }
738
739 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700740 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700741 }
742
743 public boolean useInstantSearch() {
744 return mPrefs.getBoolean(PREF_USE_INSTANT_SEARCH, false);
745 }
746
Michael Kolbc38c6042011-04-27 10:46:06 -0700747 public boolean useFullscreen() {
748 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
749 }
750
John Reck2fd9d0e2011-07-15 11:13:48 -0700751 public boolean useInvertedRendering() {
752 return mPrefs.getBoolean(PREF_INVERTED, false);
753 }
754
Nicolas Roard5d513102011-08-03 15:35:34 -0700755 public float getInvertedContrast() {
756 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
757 }
758
John Reck35e9dd62011-04-25 09:01:54 -0700759 // -----------------------------
760 // getter/setters for privacy_security_preferences.xml
761 // -----------------------------
762
763 public boolean showSecurityWarnings() {
764 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
765 }
766
767 public boolean acceptCookies() {
768 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
769 }
770
771 public boolean saveFormdata() {
772 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
773 }
774
775 public boolean enableGeolocation() {
776 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
777 }
778
779 public boolean rememberPasswords() {
780 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
781 }
782
Michael Kolb14612442011-06-24 13:06:29 -0700783 // -----------------------------
784 // getter/setters for bandwidth_preferences.xml
785 // -----------------------------
786
Mathew Inwood467813f2011-09-02 15:43:17 +0100787 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
788 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -0700789 }
Mathew Inwood467813f2011-09-02 15:43:17 +0100790
791 public static String getPreloadAlwaysPreferenceString(Context context) {
792 return context.getResources().getString(R.string.pref_data_preload_value_always);
793 }
794
795 private String getDefaultPreloadSetting() {
Mathew Inwood75905982011-09-07 14:24:06 +0100796 return mContext.getResources().getString(R.string.pref_data_preload_default_value);
Mathew Inwood467813f2011-09-02 15:43:17 +0100797 }
798
799 public String getPreloadEnabled() {
800 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
801 }
802
The Android Open Source Project0c908882009-03-03 19:32:16 -0800803}