blob: 145178b2f59147da5a7fc300be2a949b472a1a5b [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
John Reck35e9dd62011-04-25 09:01:54 -07002 * Copyright (C) 2011 The Android Open Source Project
The Android Open Source Project0c908882009-03-03 19:32:16 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Grace Kloba9804c432009-12-02 11:07:40 -080019import android.app.ActivityManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080020import android.content.ContentResolver;
21import android.content.Context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.content.SharedPreferences;
John Reck1da81882011-10-04 17:21:10 -070023import android.content.SharedPreferences.Editor;
John Reck812d2d62011-01-18 14:16:15 -080024import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
John Reckf48314f2011-04-27 17:52:17 -070025import android.os.Build;
Ben Murdoch23da30e2010-10-26 15:18:44 +010026import android.os.Message;
Ben Murdoch0cb81892010-10-08 12:41:33 +010027import android.preference.PreferenceManager;
Ben Murdoch0cb81892010-10-08 12:41:33 +010028import android.provider.Browser;
Mathew Inwood825fba72011-10-04 14:52:52 +010029import android.provider.Settings;
John Reck5ba3c762011-09-14 15:00:15 -070030import android.util.DisplayMetrics;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010032import android.webkit.GeolocationPermissions;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.webkit.WebIconDatabase;
34import android.webkit.WebSettings;
Ben Murdoch0cb81892010-10-08 12:41:33 +010035import android.webkit.WebSettings.AutoFillProfile;
John Reck35e9dd62011-04-25 09:01:54 -070036import android.webkit.WebSettings.LayoutAlgorithm;
37import android.webkit.WebSettings.PluginState;
38import android.webkit.WebSettings.TextSize;
39import android.webkit.WebSettings.ZoomDensity;
Nicolas Roard78a98e42009-05-11 13:34:17 +010040import android.webkit.WebStorage;
John Reck812d2d62011-01-18 14:16:15 -080041import android.webkit.WebView;
42import android.webkit.WebViewDatabase;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043
John Reck46500332011-06-07 14:36:10 -070044import com.android.browser.homepages.HomeProvider;
45import com.android.browser.provider.BrowserProvider;
46import com.android.browser.search.SearchEngine;
47import com.android.browser.search.SearchEngines;
48
John Reck35e9dd62011-04-25 09:01:54 -070049import java.lang.ref.WeakReference;
50import java.util.Iterator;
51import java.util.LinkedList;
John Reckb8b2af82011-05-20 15:58:33 -070052import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080053
John Reck35e9dd62011-04-25 09:01:54 -070054/**
55 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080056 */
John Reck35e9dd62011-04-25 09:01:54 -070057public class BrowserSettings implements OnSharedPreferenceChangeListener,
58 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010059
John Reck35e9dd62011-04-25 09:01:54 -070060 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070061 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
62 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
63 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080064
65 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070066 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
67 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070068
69 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070070 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
71 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070072
73 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070074 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
75 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080076
John Reck7fd1e8f2011-04-27 18:22:57 -070077 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
78 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
79 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
80
John Reck35e9dd62011-04-25 09:01:54 -070081 private static final String USER_AGENTS[] = { null,
82 DESKTOP_USERAGENT,
83 IPHONE_USERAGENT,
84 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070085 FROYO_USERAGENT,
86 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070087 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080088
John Reck8fc22a12011-06-16 14:57:41 -070089 // The minimum min font size
90 // Aka, the lower bounds for the min font size range
91 // which is 1:5..24
92 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070093 // The initial value in the text zoom range
94 // This is what represents 100% in the SeekBarPreference range
95 private static final int TEXT_ZOOM_START_VAL = 10;
96 // The size of a single step in the text zoom range, in percent
97 private static final int TEXT_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -070098
John Reck35e9dd62011-04-25 09:01:54 -070099 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -0700100
John Reck35e9dd62011-04-25 09:01:54 -0700101 private Context mContext;
102 private SharedPreferences mPrefs;
103 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700104 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700105 private WebStorageSizeManager mWebStorageSizeManager;
106 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700107 private WeakHashMap<WebSettings, String> mCustomUserAgents;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100108 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -0700109 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -0700110 private float mFontSizeMult = 1.0f;
John Reck78a6a1d2011-07-21 13:54:03 -0700111
112 // Cached values
113 private int mPageCacheCapacity = 1;
114 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800115
John Reck35e9dd62011-04-25 09:01:54 -0700116 // Cached settings
117 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118
Ben Murdochaaa1f372011-07-25 15:40:58 +0100119 private static String sFactoryResetUrl;
120
John Reck35e9dd62011-04-25 09:01:54 -0700121 public static void initialize(final Context context) {
122 sInstance = new BrowserSettings(context);
123 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124
John Reck35e9dd62011-04-25 09:01:54 -0700125 public static BrowserSettings getInstance() {
126 return sInstance;
127 }
Ben Murdochef671652010-11-25 17:17:58 +0000128
John Reck35e9dd62011-04-25 09:01:54 -0700129 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100130 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700131 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
132 mAutofillHandler = new AutofillHandler(mContext);
133 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700134 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reck35e9dd62011-04-25 09:01:54 -0700135 mAutofillHandler.asyncLoadFromDb();
John Reckcadae722011-07-25 11:36:17 -0700136 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700137 }
138
139 public void setController(Controller controller) {
140 mController = controller;
John Reckc477e712011-08-17 11:27:15 -0700141 if (sInitialized) {
142 syncSharedSettings();
143 }
John Reck35e9dd62011-04-25 09:01:54 -0700144
145 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
146 ((InstantSearchEngine) mSearchEngine).setController(mController);
Ben Murdochef671652010-11-25 17:17:58 +0000147 }
148 }
149
John Reck35e9dd62011-04-25 09:01:54 -0700150 public void startManagingSettings(WebSettings settings) {
John Reckc477e712011-08-17 11:27:15 -0700151 if (mNeedsSharedSync) {
152 syncSharedSettings();
153 }
John Reck35e9dd62011-04-25 09:01:54 -0700154 synchronized (mManagedSettings) {
155 syncStaticSettings(settings);
156 syncSetting(settings);
157 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800158 }
159 }
160
John Reckcadae722011-07-25 11:36:17 -0700161 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700162
163 @Override
164 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700165 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
166 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700167 // the cost of one cached page is ~3M (measured using nytimes.com). For
168 // low end devices, we only cache one page. For high end devices, we try
169 // to cache more pages, currently choose 5.
170 if (ActivityManager.staticGetMemoryClass() > 16) {
171 mPageCacheCapacity = 5;
172 }
173 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
174 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
175 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700176 // Workaround b/5253777
177 CookieManager.getInstance().acceptCookie();
178 // Workaround b/5254577
179 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700180 if (Build.VERSION.CODENAME.equals("REL")) {
181 // This is a release build, always startup with debug disabled
182 setDebugEnabled(false);
183 }
184 if (mPrefs.contains(PREF_TEXT_SIZE)) {
185 /*
186 * Update from TextSize enum to zoom percent
187 * SMALLEST is 50%
188 * SMALLER is 75%
189 * NORMAL is 100%
190 * LARGER is 150%
191 * LARGEST is 200%
192 */
193 switch (getTextSize()) {
194 case SMALLEST:
195 setTextZoom(50);
196 break;
197 case SMALLER:
198 setTextZoom(75);
199 break;
200 case LARGER:
201 setTextZoom(150);
202 break;
203 case LARGEST:
204 setTextZoom(200);
205 break;
206 }
207 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
208 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100209
210 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
211 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
212 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
213 BrowserProvider.getClientId(mContext.getContentResolver()));
214 }
215
Ben Murdochaaa1f372011-07-25 15:40:58 +0100216 synchronized (BrowserSettings.class) {
217 sInitialized = true;
218 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700219 }
220 }
221 };
222
Ben Murdochaaa1f372011-07-25 15:40:58 +0100223 private static void requireInitialization() {
224 synchronized (BrowserSettings.class) {
225 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700226 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100227 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700228 } catch (InterruptedException e) {
229 }
230 }
231 }
232 }
233
The Android Open Source Project0c908882009-03-03 19:32:16 -0800234 /**
John Reck35e9dd62011-04-25 09:01:54 -0700235 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800236 */
John Reck35e9dd62011-04-25 09:01:54 -0700237 private void syncSetting(WebSettings settings) {
238 settings.setGeolocationEnabled(enableGeolocation());
239 settings.setJavaScriptEnabled(enableJavascript());
240 settings.setLightTouchEnabled(enableLightTouch());
241 settings.setNavDump(enableNavDump());
Derek Sollenberger31adf672011-07-08 11:31:30 -0400242 settings.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
John Reck35e9dd62011-04-25 09:01:54 -0700243 settings.setShowVisualIndicator(enableVisualIndicator());
244 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
245 settings.setDefaultZoom(getDefaultZoom());
246 settings.setMinimumFontSize(getMinimumFontSize());
247 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck92f25f82011-04-26 16:57:10 -0700248 settings.setForceUserScalable(forceEnableUserScalable());
John Reck35e9dd62011-04-25 09:01:54 -0700249 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700250 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700251 settings.setAutoFillEnabled(isAutofillEnabled());
252 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100253 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700254 settings.setLoadsImagesAutomatically(loadImages());
255 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
256 settings.setSavePassword(rememberPasswords());
257 settings.setSaveFormData(saveFormdata());
258 settings.setUseWideViewPort(isWideViewport());
259 settings.setAutoFillProfile(getAutoFillProfile());
John Reckb8b2af82011-05-20 15:58:33 -0700260
261 String ua = mCustomUserAgents.get(settings);
John Reck46500332011-06-07 14:36:10 -0700262 if (ua != null) {
John Reckb8b2af82011-05-20 15:58:33 -0700263 settings.setUserAgentString(ua);
264 } else {
265 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
266 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700267
John Reckeabe5da2011-08-08 13:24:13 -0700268 settings.setProperty(WebViewProperties.gfxInvertedScreen,
John Reck2fd9d0e2011-07-15 11:13:48 -0700269 useInvertedRendering() ? "true" : "false");
Nicolas Roard5d513102011-08-03 15:35:34 -0700270
John Reckeabe5da2011-08-08 13:24:13 -0700271 settings.setProperty(WebViewProperties.gfxInvertedScreenContrast,
Nicolas Roard5d513102011-08-03 15:35:34 -0700272 Float.toString(getInvertedContrast()));
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700273
274 settings.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
275 enableCpuUploadPath() ? "true" : "false");
Ben Murdochef671652010-11-25 17:17:58 +0000276 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800277
John Reck35e9dd62011-04-25 09:01:54 -0700278 /**
279 * Syncs all the settings that have no UI
280 * These cannot change, so we only need to set them once per WebSettings
281 */
282 private void syncStaticSettings(WebSettings settings) {
283 settings.setDefaultFontSize(16);
284 settings.setDefaultFixedFontSize(13);
285 settings.setPageCacheCapacity(getPageCacheCapacity());
Ben Murdochef671652010-11-25 17:17:58 +0000286
John Reck35e9dd62011-04-25 09:01:54 -0700287 // WebView inside Browser doesn't want initial focus to be set.
288 settings.setNeedInitialFocus(false);
289 // Browser supports multiple windows
290 settings.setSupportMultipleWindows(true);
291 // enable smooth transition for better performance during panning or
292 // zooming
293 settings.setEnableSmoothTransition(true);
294 // disable content url access
295 settings.setAllowContentAccess(false);
296
297 // HTML5 API flags
298 settings.setAppCacheEnabled(true);
299 settings.setDatabaseEnabled(true);
300 settings.setDomStorageEnabled(true);
301 settings.setWorkersEnabled(true); // This only affects V8.
302
303 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700304 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700305 settings.setAppCachePath(getAppCachePath());
306 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
307 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
308 }
309
310 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700311 mNeedsSharedSync = false;
John Reck35e9dd62011-04-25 09:01:54 -0700312 CookieManager.getInstance().setAcceptCookie(acceptCookies());
313 if (mController != null) {
314 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700315 }
John Reck35e9dd62011-04-25 09:01:54 -0700316 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700317
John Reck35e9dd62011-04-25 09:01:54 -0700318 private void syncManagedSettings() {
319 syncSharedSettings();
320 synchronized (mManagedSettings) {
321 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
322 while (iter.hasNext()) {
323 WeakReference<WebSettings> ref = iter.next();
324 WebSettings settings = ref.get();
325 if (settings == null) {
326 iter.remove();
327 continue;
328 }
329 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000330 }
John Reck35e9dd62011-04-25 09:01:54 -0700331 }
332 }
Ben Murdochef671652010-11-25 17:17:58 +0000333
John Reck35e9dd62011-04-25 09:01:54 -0700334 @Override
335 public void onSharedPreferenceChanged(
336 SharedPreferences sharedPreferences, String key) {
337 syncManagedSettings();
338 if (PREF_SEARCH_ENGINE.equals(key)) {
339 updateSearchEngine(false);
340 }
341 if (PREF_USE_INSTANT_SEARCH.equals(key)) {
342 updateSearchEngine(true);
343 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700344 if (PREF_FULLSCREEN.equals(key)) {
345 if (mController.getUi() != null) {
346 mController.getUi().setFullscreen(useFullscreen());
347 }
Michael Kolb0241e752011-07-07 14:58:50 -0700348 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
349 if (mController.getUi() != null) {
350 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
351 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700352 }
John Reck35e9dd62011-04-25 09:01:54 -0700353 }
354
John Reck961d35d2011-06-23 09:45:54 -0700355 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100356 requireInitialization();
357 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700358 }
359
360 public LayoutAlgorithm getLayoutAlgorithm() {
361 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
362 if (autofitPages()) {
363 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
364 }
365 if (isDebugEnabled()) {
366 if (isSmallScreen()) {
367 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000368 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700369 if (isNormalLayout()) {
370 layoutAlgorithm = LayoutAlgorithm.NORMAL;
371 } else {
372 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
373 }
Ben Murdochef671652010-11-25 17:17:58 +0000374 }
John Reck35e9dd62011-04-25 09:01:54 -0700375 }
376 return layoutAlgorithm;
377 }
Ben Murdochef671652010-11-25 17:17:58 +0000378
John Reck35e9dd62011-04-25 09:01:54 -0700379 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700380 requireInitialization();
381 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800382 }
383
John Reck35e9dd62011-04-25 09:01:54 -0700384 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700385 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700386 return mWebStorageSizeManager;
387 }
388
John Reck35e9dd62011-04-25 09:01:54 -0700389 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700390 if (mAppCachePath == null) {
391 mAppCachePath = mContext.getDir("appcache", 0).getPath();
392 }
393 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700394 }
395
396 private void updateSearchEngine(boolean force) {
397 String searchEngineName = getSearchEngineName();
398 if (force || mSearchEngine == null ||
399 !mSearchEngine.getName().equals(searchEngineName)) {
400 if (mSearchEngine != null) {
401 if (mSearchEngine.supportsVoiceSearch()) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000402 // One or more tabs could have been in voice search mode.
403 // Clear it, since the new SearchEngine may not support
404 // it, or may handle it differently.
405 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
406 mController.getTabControl().getTab(i).revertVoiceSearchMode();
407 }
408 }
John Reck35e9dd62011-04-25 09:01:54 -0700409 mSearchEngine.close();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000410 }
John Reck35e9dd62011-04-25 09:01:54 -0700411 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000412
John Reck35e9dd62011-04-25 09:01:54 -0700413 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
414 ((InstantSearchEngine) mSearchEngine).setController(mController);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000415 }
416 }
417 }
418
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100419 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700420 if (mSearchEngine == null) {
421 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100422 }
John Reck35e9dd62011-04-25 09:01:54 -0700423 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100424 }
425
John Reck35e9dd62011-04-25 09:01:54 -0700426 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700427 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700428 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100429 }
430
John Reck35e9dd62011-04-25 09:01:54 -0700431 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700432 Editor edit = mPrefs.edit();
433 edit.putBoolean(PREF_DEBUG_MENU, value);
434 if (!value) {
435 // Reset to "safe" value
436 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
437 }
438 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100439 }
440
John Reck35e9dd62011-04-25 09:01:54 -0700441 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800442 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700443 if (mController != null) {
444 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800445 if (current != null) {
446 current.clearCache(true);
447 }
448 }
449 }
450
John Reck35e9dd62011-04-25 09:01:54 -0700451 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800452 CookieManager.getInstance().removeAllCookie();
453 }
454
John Reck35e9dd62011-04-25 09:01:54 -0700455 public void clearHistory() {
456 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800457 Browser.clearHistory(resolver);
458 Browser.clearSearches(resolver);
459 }
460
John Reck35e9dd62011-04-25 09:01:54 -0700461 public void clearFormData() {
462 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700463 if (mController!= null) {
464 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100465 if (currentTopView != null) {
466 currentTopView.clearFormData();
467 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800468 }
469 }
470
John Reck35e9dd62011-04-25 09:01:54 -0700471 public void clearPasswords() {
472 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800473 db.clearUsernamePassword();
474 db.clearHttpAuthUsernamePassword();
475 }
476
John Reck35e9dd62011-04-25 09:01:54 -0700477 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100478 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100479 }
480
John Reck35e9dd62011-04-25 09:01:54 -0700481 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100482 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100483 }
484
John Reck35e9dd62011-04-25 09:01:54 -0700485 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700486 // Preserve autologin setting
487 long gal = mPrefs.getLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, -1);
488 mPrefs.edit()
489 .clear()
490 .putLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, gal)
491 .apply();
John Reck35e9dd62011-04-25 09:01:54 -0700492 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700493 }
494
John Reck35e9dd62011-04-25 09:01:54 -0700495 public AutoFillProfile getAutoFillProfile() {
496 mAutofillHandler.waitForLoad();
497 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800498 }
499
John Reck35e9dd62011-04-25 09:01:54 -0700500 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
501 mAutofillHandler.waitForLoad();
502 mAutofillHandler.setAutoFillProfile(profile, msg);
Ben Murdoch273330a2011-07-08 18:37:22 +0100503 // Auto-fill will reuse the same profile ID when making edits to the profile,
504 // so we need to force a settings sync (otherwise the SharedPreferences
505 // manager will optimise out the call to onSharedPreferenceChanged(), as
506 // it thinks nothing has changed).
507 syncManagedSettings();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800508 }
509
John Reck35e9dd62011-04-25 09:01:54 -0700510 public void toggleDebugSettings() {
511 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800512 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100513
John Reckb8b2af82011-05-20 15:58:33 -0700514 public boolean hasDesktopUseragent(WebView view) {
515 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
516 }
517
518 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700519 if (view == null) {
520 return;
521 }
John Reckb8b2af82011-05-20 15:58:33 -0700522 WebSettings settings = view.getSettings();
523 if (mCustomUserAgents.get(settings) != null) {
524 mCustomUserAgents.remove(settings);
525 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
526 } else {
527 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
528 settings.setUserAgentString(DESKTOP_USERAGENT);
529 }
530 }
531
John Reck7dc444b2011-06-16 17:44:29 -0700532 public static int getAdjustedMinimumFontSize(int rawValue) {
533 rawValue++; // Preference starts at 0, min font at 1
534 if (rawValue > 1) {
535 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
536 }
537 return rawValue;
538 }
539
John Reck5ba3c762011-09-14 15:00:15 -0700540 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700541 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700542 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700543 }
544
545 static int getRawTextZoom(int percent) {
546 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
547 }
548
John Reckcadae722011-07-25 11:36:17 -0700549 public SharedPreferences getPreferences() {
550 return mPrefs;
551 }
552
John Reck35e9dd62011-04-25 09:01:54 -0700553 // -----------------------------
554 // getter/setters for accessibility_preferences.xml
555 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100556
John Reck7dc444b2011-06-16 17:44:29 -0700557 @Deprecated
558 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700559 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
560 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100561 }
562
John Reck35e9dd62011-04-25 09:01:54 -0700563 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700564 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700565 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100566 }
567
John Reck92f25f82011-04-26 16:57:10 -0700568 public boolean forceEnableUserScalable() {
569 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
570 }
571
John Reck7dc444b2011-06-16 17:44:29 -0700572 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700573 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700574 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
575 return getAdjustedTextZoom(textZoom);
576 }
577
578 public void setTextZoom(int percent) {
579 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
580 }
581
John Reck35e9dd62011-04-25 09:01:54 -0700582 // -----------------------------
583 // getter/setters for advanced_preferences.xml
584 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100585
John Reck35e9dd62011-04-25 09:01:54 -0700586 public String getSearchEngineName() {
587 return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100588 }
John Reck63bb6872010-12-01 19:29:32 -0800589
John Reck35e9dd62011-04-25 09:01:54 -0700590 public boolean openInBackground() {
591 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800592 }
John Reck35e9dd62011-04-25 09:01:54 -0700593
594 public boolean enableJavascript() {
595 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
596 }
597
598 // TODO: Cache
599 public PluginState getPluginState() {
600 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
601 return PluginState.valueOf(state);
602 }
603
604 // TODO: Cache
605 public ZoomDensity getDefaultZoom() {
606 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
607 return ZoomDensity.valueOf(zoom);
608 }
609
610 public boolean loadPageInOverviewMode() {
611 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
612 }
613
614 public boolean autofitPages() {
615 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
616 }
617
618 public boolean blockPopupWindows() {
619 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
620 }
621
622 public boolean loadImages() {
623 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
624 }
625
626 public String getDefaultTextEncoding() {
627 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
628 }
629
630 // -----------------------------
631 // getter/setters for general_preferences.xml
632 // -----------------------------
633
634 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700635 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
636 }
637
638 public void setHomePage(String value) {
639 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
640 }
641
642 public boolean isAutofillEnabled() {
643 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
644 }
645
646 public void setAutofillEnabled(boolean value) {
647 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
648 }
649
650 // -----------------------------
651 // getter/setters for debug_preferences.xml
652 // -----------------------------
653
654 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700655 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700656 return true;
657 }
658 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
659 }
660
Derek Sollenberger31adf672011-07-08 11:31:30 -0400661 public boolean isSkiaHardwareAccelerated() {
662 if (!isDebugEnabled()) {
663 return false;
664 }
665 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
666 }
667
John Reck35e9dd62011-04-25 09:01:54 -0700668 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700669 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700670 return 0;
671 }
672 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
673 }
674
675 // -----------------------------
676 // getter/setters for hidden_debug_preferences.xml
677 // -----------------------------
678
679 public boolean enableVisualIndicator() {
680 if (!isDebugEnabled()) {
681 return false;
682 }
683 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
684 }
685
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700686 public boolean enableCpuUploadPath() {
687 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700688 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700689 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700690 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700691 }
692
John Reck35e9dd62011-04-25 09:01:54 -0700693 public boolean enableJavascriptConsole() {
694 if (!isDebugEnabled()) {
695 return false;
696 }
697 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
698 }
699
700 public boolean isSmallScreen() {
701 if (!isDebugEnabled()) {
702 return false;
703 }
704 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
705 }
706
707 public boolean isWideViewport() {
708 if (!isDebugEnabled()) {
709 return true;
710 }
711 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
712 }
713
714 public boolean isNormalLayout() {
715 if (!isDebugEnabled()) {
716 return false;
717 }
718 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
719 }
720
721 public boolean isTracing() {
722 if (!isDebugEnabled()) {
723 return false;
724 }
725 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
726 }
727
728 public boolean enableLightTouch() {
729 if (!isDebugEnabled()) {
730 return false;
731 }
732 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
733 }
734
735 public boolean enableNavDump() {
736 if (!isDebugEnabled()) {
737 return false;
738 }
739 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
740 }
741
742 public String getJsEngineFlags() {
743 if (!isDebugEnabled()) {
744 return "";
745 }
746 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
747 }
748
749 // -----------------------------
750 // getter/setters for lab_preferences.xml
751 // -----------------------------
752
753 public boolean useQuickControls() {
754 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
755 }
756
757 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700758 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700759 }
760
761 public boolean useInstantSearch() {
762 return mPrefs.getBoolean(PREF_USE_INSTANT_SEARCH, false);
763 }
764
Michael Kolbc38c6042011-04-27 10:46:06 -0700765 public boolean useFullscreen() {
766 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
767 }
768
John Reck2fd9d0e2011-07-15 11:13:48 -0700769 public boolean useInvertedRendering() {
770 return mPrefs.getBoolean(PREF_INVERTED, false);
771 }
772
Nicolas Roard5d513102011-08-03 15:35:34 -0700773 public float getInvertedContrast() {
774 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
775 }
776
John Reck35e9dd62011-04-25 09:01:54 -0700777 // -----------------------------
778 // getter/setters for privacy_security_preferences.xml
779 // -----------------------------
780
781 public boolean showSecurityWarnings() {
782 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
783 }
784
785 public boolean acceptCookies() {
786 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
787 }
788
789 public boolean saveFormdata() {
790 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
791 }
792
793 public boolean enableGeolocation() {
794 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
795 }
796
797 public boolean rememberPasswords() {
798 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
799 }
800
Michael Kolb14612442011-06-24 13:06:29 -0700801 // -----------------------------
802 // getter/setters for bandwidth_preferences.xml
803 // -----------------------------
804
Mathew Inwood467813f2011-09-02 15:43:17 +0100805 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
806 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -0700807 }
Mathew Inwood467813f2011-09-02 15:43:17 +0100808
809 public static String getPreloadAlwaysPreferenceString(Context context) {
810 return context.getResources().getString(R.string.pref_data_preload_value_always);
811 }
812
Mathew Inwood825fba72011-10-04 14:52:52 +0100813 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
814 "browser_default_preload_setting";
815
816 public String getDefaultPreloadSetting() {
817 String preload = Settings.Secure.getString(mContext.getContentResolver(),
818 DEAULT_PRELOAD_SECURE_SETTING_KEY);
819 if (preload == null) {
820 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
821 }
822 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +0100823 }
824
825 public String getPreloadEnabled() {
826 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
827 }
828
The Android Open Source Project0c908882009-03-03 19:32:16 -0800829}