blob: 09c72007f0f979c56a165aa0fe1d28b989bec0f1 [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;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010029import android.webkit.GeolocationPermissions;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.webkit.WebIconDatabase;
31import android.webkit.WebSettings;
Ben Murdoch0cb81892010-10-08 12:41:33 +010032import android.webkit.WebSettings.AutoFillProfile;
John Reck35e9dd62011-04-25 09:01:54 -070033import android.webkit.WebSettings.LayoutAlgorithm;
34import android.webkit.WebSettings.PluginState;
35import android.webkit.WebSettings.TextSize;
36import android.webkit.WebSettings.ZoomDensity;
Nicolas Roard78a98e42009-05-11 13:34:17 +010037import android.webkit.WebStorage;
John Reck812d2d62011-01-18 14:16:15 -080038import android.webkit.WebView;
39import android.webkit.WebViewDatabase;
The Android Open Source Project0c908882009-03-03 19:32:16 -080040
John Reck46500332011-06-07 14:36:10 -070041import com.android.browser.homepages.HomeProvider;
42import com.android.browser.provider.BrowserProvider;
43import com.android.browser.search.SearchEngine;
44import com.android.browser.search.SearchEngines;
45
John Reck35e9dd62011-04-25 09:01:54 -070046import java.lang.ref.WeakReference;
47import java.util.Iterator;
48import java.util.LinkedList;
John Reckb8b2af82011-05-20 15:58:33 -070049import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050
John Reck35e9dd62011-04-25 09:01:54 -070051/**
52 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080053 */
John Reck35e9dd62011-04-25 09:01:54 -070054public class BrowserSettings implements OnSharedPreferenceChangeListener,
55 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010056
John Reck35e9dd62011-04-25 09:01:54 -070057 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070058 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
59 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
60 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080061
62 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070063 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
64 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070065
66 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070067 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
68 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070069
70 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070071 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
72 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080073
John Reck7fd1e8f2011-04-27 18:22:57 -070074 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
75 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
76 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
77
John Reck35e9dd62011-04-25 09:01:54 -070078 private static final String USER_AGENTS[] = { null,
79 DESKTOP_USERAGENT,
80 IPHONE_USERAGENT,
81 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070082 FROYO_USERAGENT,
83 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070084 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080085
John Reck8fc22a12011-06-16 14:57:41 -070086 // The minimum min font size
87 // Aka, the lower bounds for the min font size range
88 // which is 1:5..24
89 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070090 // The initial value in the text zoom range
91 // This is what represents 100% in the SeekBarPreference range
92 private static final int TEXT_ZOOM_START_VAL = 10;
93 // The size of a single step in the text zoom range, in percent
94 private static final int TEXT_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -070095
John Reck35e9dd62011-04-25 09:01:54 -070096 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -070097
John Reck35e9dd62011-04-25 09:01:54 -070098 private Context mContext;
99 private SharedPreferences mPrefs;
100 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700101 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700102 private WebStorageSizeManager mWebStorageSizeManager;
103 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700104 private WeakHashMap<WebSettings, String> mCustomUserAgents;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100105 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -0700106 private boolean mNeedsSharedSync = true;
John Reck78a6a1d2011-07-21 13:54:03 -0700107
108 // Cached values
109 private int mPageCacheCapacity = 1;
110 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800111
John Reck35e9dd62011-04-25 09:01:54 -0700112 // Cached settings
113 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114
Ben Murdochaaa1f372011-07-25 15:40:58 +0100115 private static String sFactoryResetUrl;
116
John Reck35e9dd62011-04-25 09:01:54 -0700117 public static void initialize(final Context context) {
118 sInstance = new BrowserSettings(context);
119 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800120
John Reck35e9dd62011-04-25 09:01:54 -0700121 public static BrowserSettings getInstance() {
122 return sInstance;
123 }
Ben Murdochef671652010-11-25 17:17:58 +0000124
John Reck35e9dd62011-04-25 09:01:54 -0700125 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100126 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700127 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
128 mAutofillHandler = new AutofillHandler(mContext);
129 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700130 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reck35e9dd62011-04-25 09:01:54 -0700131 mPrefs.registerOnSharedPreferenceChangeListener(this);
132 mAutofillHandler.asyncLoadFromDb();
John Reckcadae722011-07-25 11:36:17 -0700133 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700134 }
135
136 public void setController(Controller controller) {
137 mController = controller;
John Reckc477e712011-08-17 11:27:15 -0700138 if (sInitialized) {
139 syncSharedSettings();
140 }
John Reck35e9dd62011-04-25 09:01:54 -0700141
142 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
143 ((InstantSearchEngine) mSearchEngine).setController(mController);
Ben Murdochef671652010-11-25 17:17:58 +0000144 }
145 }
146
John Reck35e9dd62011-04-25 09:01:54 -0700147 public void startManagingSettings(WebSettings settings) {
John Reckc477e712011-08-17 11:27:15 -0700148 if (mNeedsSharedSync) {
149 syncSharedSettings();
150 }
John Reck35e9dd62011-04-25 09:01:54 -0700151 synchronized (mManagedSettings) {
152 syncStaticSettings(settings);
153 syncSetting(settings);
154 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800155 }
156 }
157
John Reckcadae722011-07-25 11:36:17 -0700158 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700159
160 @Override
161 public void run() {
162 // the cost of one cached page is ~3M (measured using nytimes.com). For
163 // low end devices, we only cache one page. For high end devices, we try
164 // to cache more pages, currently choose 5.
165 if (ActivityManager.staticGetMemoryClass() > 16) {
166 mPageCacheCapacity = 5;
167 }
168 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
169 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
170 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
171 if (Build.VERSION.CODENAME.equals("REL")) {
172 // This is a release build, always startup with debug disabled
173 setDebugEnabled(false);
174 }
175 if (mPrefs.contains(PREF_TEXT_SIZE)) {
176 /*
177 * Update from TextSize enum to zoom percent
178 * SMALLEST is 50%
179 * SMALLER is 75%
180 * NORMAL is 100%
181 * LARGER is 150%
182 * LARGEST is 200%
183 */
184 switch (getTextSize()) {
185 case SMALLEST:
186 setTextZoom(50);
187 break;
188 case SMALLER:
189 setTextZoom(75);
190 break;
191 case LARGER:
192 setTextZoom(150);
193 break;
194 case LARGEST:
195 setTextZoom(200);
196 break;
197 }
198 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
199 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100200
201 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
202 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
203 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
204 BrowserProvider.getClientId(mContext.getContentResolver()));
205 }
206
Ben Murdochaaa1f372011-07-25 15:40:58 +0100207 synchronized (BrowserSettings.class) {
208 sInitialized = true;
209 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700210 }
211 }
212 };
213
Ben Murdochaaa1f372011-07-25 15:40:58 +0100214 private static void requireInitialization() {
215 synchronized (BrowserSettings.class) {
216 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700217 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100218 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700219 } catch (InterruptedException e) {
220 }
221 }
222 }
223 }
224
The Android Open Source Project0c908882009-03-03 19:32:16 -0800225 /**
John Reck35e9dd62011-04-25 09:01:54 -0700226 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800227 */
John Reck35e9dd62011-04-25 09:01:54 -0700228 private void syncSetting(WebSettings settings) {
229 settings.setGeolocationEnabled(enableGeolocation());
230 settings.setJavaScriptEnabled(enableJavascript());
231 settings.setLightTouchEnabled(enableLightTouch());
232 settings.setNavDump(enableNavDump());
Derek Sollenberger31adf672011-07-08 11:31:30 -0400233 settings.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
John Reck35e9dd62011-04-25 09:01:54 -0700234 settings.setShowVisualIndicator(enableVisualIndicator());
235 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
236 settings.setDefaultZoom(getDefaultZoom());
237 settings.setMinimumFontSize(getMinimumFontSize());
238 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck92f25f82011-04-26 16:57:10 -0700239 settings.setForceUserScalable(forceEnableUserScalable());
John Reck35e9dd62011-04-25 09:01:54 -0700240 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700241 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700242 settings.setAutoFillEnabled(isAutofillEnabled());
243 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100244 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700245 settings.setLoadsImagesAutomatically(loadImages());
246 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
247 settings.setSavePassword(rememberPasswords());
248 settings.setSaveFormData(saveFormdata());
249 settings.setUseWideViewPort(isWideViewport());
250 settings.setAutoFillProfile(getAutoFillProfile());
John Reckb8b2af82011-05-20 15:58:33 -0700251
252 String ua = mCustomUserAgents.get(settings);
John Reck46500332011-06-07 14:36:10 -0700253 if (ua != null) {
John Reckb8b2af82011-05-20 15:58:33 -0700254 settings.setUserAgentString(ua);
255 } else {
256 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
257 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700258
John Reckeabe5da2011-08-08 13:24:13 -0700259 settings.setProperty(WebViewProperties.gfxInvertedScreen,
John Reck2fd9d0e2011-07-15 11:13:48 -0700260 useInvertedRendering() ? "true" : "false");
Nicolas Roard5d513102011-08-03 15:35:34 -0700261
John Reckeabe5da2011-08-08 13:24:13 -0700262 settings.setProperty(WebViewProperties.gfxInvertedScreenContrast,
Nicolas Roard5d513102011-08-03 15:35:34 -0700263 Float.toString(getInvertedContrast()));
Ben Murdochef671652010-11-25 17:17:58 +0000264 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800265
John Reck35e9dd62011-04-25 09:01:54 -0700266 /**
267 * Syncs all the settings that have no UI
268 * These cannot change, so we only need to set them once per WebSettings
269 */
270 private void syncStaticSettings(WebSettings settings) {
271 settings.setDefaultFontSize(16);
272 settings.setDefaultFixedFontSize(13);
273 settings.setPageCacheCapacity(getPageCacheCapacity());
Ben Murdochef671652010-11-25 17:17:58 +0000274
John Reck35e9dd62011-04-25 09:01:54 -0700275 // WebView inside Browser doesn't want initial focus to be set.
276 settings.setNeedInitialFocus(false);
277 // Browser supports multiple windows
278 settings.setSupportMultipleWindows(true);
279 // enable smooth transition for better performance during panning or
280 // zooming
281 settings.setEnableSmoothTransition(true);
282 // disable content url access
283 settings.setAllowContentAccess(false);
284
285 // HTML5 API flags
286 settings.setAppCacheEnabled(true);
287 settings.setDatabaseEnabled(true);
288 settings.setDomStorageEnabled(true);
289 settings.setWorkersEnabled(true); // This only affects V8.
290
291 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700292 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700293 settings.setAppCachePath(getAppCachePath());
294 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
295 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
296 }
297
298 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700299 mNeedsSharedSync = false;
John Reck35e9dd62011-04-25 09:01:54 -0700300 CookieManager.getInstance().setAcceptCookie(acceptCookies());
301 if (mController != null) {
302 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700303 }
John Reck35e9dd62011-04-25 09:01:54 -0700304 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700305
John Reck35e9dd62011-04-25 09:01:54 -0700306 private void syncManagedSettings() {
307 syncSharedSettings();
308 synchronized (mManagedSettings) {
309 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
310 while (iter.hasNext()) {
311 WeakReference<WebSettings> ref = iter.next();
312 WebSettings settings = ref.get();
313 if (settings == null) {
314 iter.remove();
315 continue;
316 }
317 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000318 }
John Reck35e9dd62011-04-25 09:01:54 -0700319 }
320 }
Ben Murdochef671652010-11-25 17:17:58 +0000321
John Reck35e9dd62011-04-25 09:01:54 -0700322 @Override
323 public void onSharedPreferenceChanged(
324 SharedPreferences sharedPreferences, String key) {
325 syncManagedSettings();
326 if (PREF_SEARCH_ENGINE.equals(key)) {
327 updateSearchEngine(false);
328 }
329 if (PREF_USE_INSTANT_SEARCH.equals(key)) {
330 updateSearchEngine(true);
331 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700332 if (PREF_FULLSCREEN.equals(key)) {
333 if (mController.getUi() != null) {
334 mController.getUi().setFullscreen(useFullscreen());
335 }
Michael Kolb0241e752011-07-07 14:58:50 -0700336 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
337 if (mController.getUi() != null) {
338 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
339 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700340 }
John Reck35e9dd62011-04-25 09:01:54 -0700341 }
342
John Reck961d35d2011-06-23 09:45:54 -0700343 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100344 requireInitialization();
345 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700346 }
347
348 public LayoutAlgorithm getLayoutAlgorithm() {
349 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
350 if (autofitPages()) {
351 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
352 }
353 if (isDebugEnabled()) {
354 if (isSmallScreen()) {
355 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000356 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700357 if (isNormalLayout()) {
358 layoutAlgorithm = LayoutAlgorithm.NORMAL;
359 } else {
360 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
361 }
Ben Murdochef671652010-11-25 17:17:58 +0000362 }
John Reck35e9dd62011-04-25 09:01:54 -0700363 }
364 return layoutAlgorithm;
365 }
Ben Murdochef671652010-11-25 17:17:58 +0000366
John Reck35e9dd62011-04-25 09:01:54 -0700367 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700368 requireInitialization();
369 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800370 }
371
John Reck35e9dd62011-04-25 09:01:54 -0700372 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700373 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700374 return mWebStorageSizeManager;
375 }
376
John Reck35e9dd62011-04-25 09:01:54 -0700377 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700378 if (mAppCachePath == null) {
379 mAppCachePath = mContext.getDir("appcache", 0).getPath();
380 }
381 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700382 }
383
384 private void updateSearchEngine(boolean force) {
385 String searchEngineName = getSearchEngineName();
386 if (force || mSearchEngine == null ||
387 !mSearchEngine.getName().equals(searchEngineName)) {
388 if (mSearchEngine != null) {
389 if (mSearchEngine.supportsVoiceSearch()) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000390 // One or more tabs could have been in voice search mode.
391 // Clear it, since the new SearchEngine may not support
392 // it, or may handle it differently.
393 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
394 mController.getTabControl().getTab(i).revertVoiceSearchMode();
395 }
396 }
John Reck35e9dd62011-04-25 09:01:54 -0700397 mSearchEngine.close();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000398 }
John Reck35e9dd62011-04-25 09:01:54 -0700399 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000400
John Reck35e9dd62011-04-25 09:01:54 -0700401 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
402 ((InstantSearchEngine) mSearchEngine).setController(mController);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000403 }
404 }
405 }
406
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100407 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700408 if (mSearchEngine == null) {
409 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100410 }
John Reck35e9dd62011-04-25 09:01:54 -0700411 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100412 }
413
John Reck35e9dd62011-04-25 09:01:54 -0700414 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700415 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700416 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100417 }
418
John Reck35e9dd62011-04-25 09:01:54 -0700419 public void setDebugEnabled(boolean value) {
420 mPrefs.edit().putBoolean(PREF_DEBUG_MENU, value).apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100421 }
422
John Reck35e9dd62011-04-25 09:01:54 -0700423 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800424 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700425 if (mController != null) {
426 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800427 if (current != null) {
428 current.clearCache(true);
429 }
430 }
431 }
432
John Reck35e9dd62011-04-25 09:01:54 -0700433 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800434 CookieManager.getInstance().removeAllCookie();
435 }
436
John Reck35e9dd62011-04-25 09:01:54 -0700437 public void clearHistory() {
438 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800439 Browser.clearHistory(resolver);
440 Browser.clearSearches(resolver);
441 }
442
John Reck35e9dd62011-04-25 09:01:54 -0700443 public void clearFormData() {
444 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700445 if (mController!= null) {
446 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100447 if (currentTopView != null) {
448 currentTopView.clearFormData();
449 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800450 }
451 }
452
John Reck35e9dd62011-04-25 09:01:54 -0700453 public void clearPasswords() {
454 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800455 db.clearUsernamePassword();
456 db.clearHttpAuthUsernamePassword();
457 }
458
John Reck35e9dd62011-04-25 09:01:54 -0700459 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100460 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100461 }
462
John Reck35e9dd62011-04-25 09:01:54 -0700463 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100464 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100465 }
466
John Reck35e9dd62011-04-25 09:01:54 -0700467 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700468 // Preserve autologin setting
469 long gal = mPrefs.getLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, -1);
470 mPrefs.edit()
471 .clear()
472 .putLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, gal)
473 .apply();
John Reck35e9dd62011-04-25 09:01:54 -0700474 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700475 }
476
John Reck35e9dd62011-04-25 09:01:54 -0700477 public AutoFillProfile getAutoFillProfile() {
478 mAutofillHandler.waitForLoad();
479 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800480 }
481
John Reck35e9dd62011-04-25 09:01:54 -0700482 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
483 mAutofillHandler.waitForLoad();
484 mAutofillHandler.setAutoFillProfile(profile, msg);
Ben Murdoch273330a2011-07-08 18:37:22 +0100485 // Auto-fill will reuse the same profile ID when making edits to the profile,
486 // so we need to force a settings sync (otherwise the SharedPreferences
487 // manager will optimise out the call to onSharedPreferenceChanged(), as
488 // it thinks nothing has changed).
489 syncManagedSettings();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800490 }
491
John Reck35e9dd62011-04-25 09:01:54 -0700492 public void toggleDebugSettings() {
493 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800494 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100495
John Reckb8b2af82011-05-20 15:58:33 -0700496 public boolean hasDesktopUseragent(WebView view) {
497 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
498 }
499
500 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700501 if (view == null) {
502 return;
503 }
John Reckb8b2af82011-05-20 15:58:33 -0700504 WebSettings settings = view.getSettings();
505 if (mCustomUserAgents.get(settings) != null) {
506 mCustomUserAgents.remove(settings);
507 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
508 } else {
509 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
510 settings.setUserAgentString(DESKTOP_USERAGENT);
511 }
512 }
513
John Reck7dc444b2011-06-16 17:44:29 -0700514 public static int getAdjustedMinimumFontSize(int rawValue) {
515 rawValue++; // Preference starts at 0, min font at 1
516 if (rawValue > 1) {
517 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
518 }
519 return rawValue;
520 }
521
522 public static int getAdjustedTextZoom(int rawValue) {
523 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
524 return rawValue + 100;
525 }
526
527 static int getRawTextZoom(int percent) {
528 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
529 }
530
John Reckcadae722011-07-25 11:36:17 -0700531 public SharedPreferences getPreferences() {
532 return mPrefs;
533 }
534
John Reck35e9dd62011-04-25 09:01:54 -0700535 // -----------------------------
536 // getter/setters for accessibility_preferences.xml
537 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100538
John Reck7dc444b2011-06-16 17:44:29 -0700539 @Deprecated
540 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700541 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
542 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100543 }
544
John Reck35e9dd62011-04-25 09:01:54 -0700545 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700546 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700547 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100548 }
549
John Reck92f25f82011-04-26 16:57:10 -0700550 public boolean forceEnableUserScalable() {
551 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
552 }
553
John Reck7dc444b2011-06-16 17:44:29 -0700554 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700555 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700556 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
557 return getAdjustedTextZoom(textZoom);
558 }
559
560 public void setTextZoom(int percent) {
561 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
562 }
563
John Reck35e9dd62011-04-25 09:01:54 -0700564 // -----------------------------
565 // getter/setters for advanced_preferences.xml
566 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100567
John Reck35e9dd62011-04-25 09:01:54 -0700568 public String getSearchEngineName() {
569 return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100570 }
John Reck63bb6872010-12-01 19:29:32 -0800571
John Reck35e9dd62011-04-25 09:01:54 -0700572 public boolean openInBackground() {
573 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800574 }
John Reck35e9dd62011-04-25 09:01:54 -0700575
576 public boolean enableJavascript() {
577 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
578 }
579
580 // TODO: Cache
581 public PluginState getPluginState() {
582 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
583 return PluginState.valueOf(state);
584 }
585
586 // TODO: Cache
587 public ZoomDensity getDefaultZoom() {
588 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
589 return ZoomDensity.valueOf(zoom);
590 }
591
592 public boolean loadPageInOverviewMode() {
593 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
594 }
595
596 public boolean autofitPages() {
597 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
598 }
599
600 public boolean blockPopupWindows() {
601 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
602 }
603
604 public boolean loadImages() {
605 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
606 }
607
608 public String getDefaultTextEncoding() {
609 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
610 }
611
612 // -----------------------------
613 // getter/setters for general_preferences.xml
614 // -----------------------------
615
616 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700617 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
618 }
619
620 public void setHomePage(String value) {
621 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
622 }
623
624 public boolean isAutofillEnabled() {
625 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
626 }
627
628 public void setAutofillEnabled(boolean value) {
629 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
630 }
631
632 // -----------------------------
633 // getter/setters for debug_preferences.xml
634 // -----------------------------
635
636 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700637 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700638 return true;
639 }
640 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
641 }
642
Derek Sollenberger31adf672011-07-08 11:31:30 -0400643 public boolean isSkiaHardwareAccelerated() {
644 if (!isDebugEnabled()) {
645 return false;
646 }
647 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
648 }
649
John Reck35e9dd62011-04-25 09:01:54 -0700650 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700651 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700652 return 0;
653 }
654 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
655 }
656
657 // -----------------------------
658 // getter/setters for hidden_debug_preferences.xml
659 // -----------------------------
660
661 public boolean enableVisualIndicator() {
662 if (!isDebugEnabled()) {
663 return false;
664 }
665 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
666 }
667
668 public boolean enableJavascriptConsole() {
669 if (!isDebugEnabled()) {
670 return false;
671 }
672 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
673 }
674
675 public boolean isSmallScreen() {
676 if (!isDebugEnabled()) {
677 return false;
678 }
679 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
680 }
681
682 public boolean isWideViewport() {
683 if (!isDebugEnabled()) {
684 return true;
685 }
686 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
687 }
688
689 public boolean isNormalLayout() {
690 if (!isDebugEnabled()) {
691 return false;
692 }
693 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
694 }
695
696 public boolean isTracing() {
697 if (!isDebugEnabled()) {
698 return false;
699 }
700 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
701 }
702
703 public boolean enableLightTouch() {
704 if (!isDebugEnabled()) {
705 return false;
706 }
707 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
708 }
709
710 public boolean enableNavDump() {
711 if (!isDebugEnabled()) {
712 return false;
713 }
714 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
715 }
716
717 public String getJsEngineFlags() {
718 if (!isDebugEnabled()) {
719 return "";
720 }
721 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
722 }
723
724 // -----------------------------
725 // getter/setters for lab_preferences.xml
726 // -----------------------------
727
728 public boolean useQuickControls() {
729 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
730 }
731
732 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700733 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700734 }
735
736 public boolean useInstantSearch() {
737 return mPrefs.getBoolean(PREF_USE_INSTANT_SEARCH, false);
738 }
739
Michael Kolbc38c6042011-04-27 10:46:06 -0700740 public boolean useFullscreen() {
741 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
742 }
743
John Reck2fd9d0e2011-07-15 11:13:48 -0700744 public boolean useInvertedRendering() {
745 return mPrefs.getBoolean(PREF_INVERTED, false);
746 }
747
Nicolas Roard5d513102011-08-03 15:35:34 -0700748 public float getInvertedContrast() {
749 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
750 }
751
John Reck35e9dd62011-04-25 09:01:54 -0700752 // -----------------------------
753 // getter/setters for privacy_security_preferences.xml
754 // -----------------------------
755
756 public boolean showSecurityWarnings() {
757 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
758 }
759
760 public boolean acceptCookies() {
761 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
762 }
763
764 public boolean saveFormdata() {
765 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
766 }
767
768 public boolean enableGeolocation() {
769 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
770 }
771
772 public boolean rememberPasswords() {
773 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
774 }
775
Michael Kolb14612442011-06-24 13:06:29 -0700776 // -----------------------------
777 // getter/setters for bandwidth_preferences.xml
778 // -----------------------------
779
780 public boolean isPreloadEnabled() {
781 return mPrefs.getBoolean(PREF_DATA_PRELOAD, false);
782 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800783}