blob: e2a521ae8509e2b3d5e4559fd7038624e585a980 [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;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -070098 // The initial value in the double tap zoom range
99 // This is what represents 100% in the SeekBarPreference range
100 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
101 // The size of a single step in the double tap zoom range, in percent
102 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -0700103
John Reck35e9dd62011-04-25 09:01:54 -0700104 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -0700105
John Reck35e9dd62011-04-25 09:01:54 -0700106 private Context mContext;
107 private SharedPreferences mPrefs;
108 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700109 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700110 private WebStorageSizeManager mWebStorageSizeManager;
111 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700112 private WeakHashMap<WebSettings, String> mCustomUserAgents;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100113 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -0700114 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -0700115 private float mFontSizeMult = 1.0f;
John Reck78a6a1d2011-07-21 13:54:03 -0700116
117 // Cached values
118 private int mPageCacheCapacity = 1;
119 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800120
John Reck35e9dd62011-04-25 09:01:54 -0700121 // Cached settings
122 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123
Ben Murdochaaa1f372011-07-25 15:40:58 +0100124 private static String sFactoryResetUrl;
125
John Reck35e9dd62011-04-25 09:01:54 -0700126 public static void initialize(final Context context) {
127 sInstance = new BrowserSettings(context);
128 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800129
John Reck35e9dd62011-04-25 09:01:54 -0700130 public static BrowserSettings getInstance() {
131 return sInstance;
132 }
Ben Murdochef671652010-11-25 17:17:58 +0000133
John Reck35e9dd62011-04-25 09:01:54 -0700134 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100135 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700136 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
137 mAutofillHandler = new AutofillHandler(mContext);
138 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700139 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reck35e9dd62011-04-25 09:01:54 -0700140 mAutofillHandler.asyncLoadFromDb();
John Reckcadae722011-07-25 11:36:17 -0700141 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700142 }
143
144 public void setController(Controller controller) {
145 mController = controller;
John Reckc477e712011-08-17 11:27:15 -0700146 if (sInitialized) {
147 syncSharedSettings();
148 }
John Reck35e9dd62011-04-25 09:01:54 -0700149
150 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
151 ((InstantSearchEngine) mSearchEngine).setController(mController);
Ben Murdochef671652010-11-25 17:17:58 +0000152 }
153 }
154
John Reck35e9dd62011-04-25 09:01:54 -0700155 public void startManagingSettings(WebSettings settings) {
John Reckc477e712011-08-17 11:27:15 -0700156 if (mNeedsSharedSync) {
157 syncSharedSettings();
158 }
John Reck35e9dd62011-04-25 09:01:54 -0700159 synchronized (mManagedSettings) {
160 syncStaticSettings(settings);
161 syncSetting(settings);
162 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800163 }
164 }
165
John Reckcadae722011-07-25 11:36:17 -0700166 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700167
168 @Override
169 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700170 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
171 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700172 // the cost of one cached page is ~3M (measured using nytimes.com). For
173 // low end devices, we only cache one page. For high end devices, we try
174 // to cache more pages, currently choose 5.
175 if (ActivityManager.staticGetMemoryClass() > 16) {
176 mPageCacheCapacity = 5;
177 }
178 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
179 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
180 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700181 // Workaround b/5253777
182 CookieManager.getInstance().acceptCookie();
183 // Workaround b/5254577
184 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700185 if (Build.VERSION.CODENAME.equals("REL")) {
186 // This is a release build, always startup with debug disabled
187 setDebugEnabled(false);
188 }
189 if (mPrefs.contains(PREF_TEXT_SIZE)) {
190 /*
191 * Update from TextSize enum to zoom percent
192 * SMALLEST is 50%
193 * SMALLER is 75%
194 * NORMAL is 100%
195 * LARGER is 150%
196 * LARGEST is 200%
197 */
198 switch (getTextSize()) {
199 case SMALLEST:
200 setTextZoom(50);
201 break;
202 case SMALLER:
203 setTextZoom(75);
204 break;
205 case LARGER:
206 setTextZoom(150);
207 break;
208 case LARGEST:
209 setTextZoom(200);
210 break;
211 }
212 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
213 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100214
215 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
216 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
217 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
218 BrowserProvider.getClientId(mContext.getContentResolver()));
219 }
220
Ben Murdochaaa1f372011-07-25 15:40:58 +0100221 synchronized (BrowserSettings.class) {
222 sInitialized = true;
223 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700224 }
225 }
226 };
227
Ben Murdochaaa1f372011-07-25 15:40:58 +0100228 private static void requireInitialization() {
229 synchronized (BrowserSettings.class) {
230 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700231 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100232 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700233 } catch (InterruptedException e) {
234 }
235 }
236 }
237 }
238
The Android Open Source Project0c908882009-03-03 19:32:16 -0800239 /**
John Reck35e9dd62011-04-25 09:01:54 -0700240 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800241 */
John Reck35e9dd62011-04-25 09:01:54 -0700242 private void syncSetting(WebSettings settings) {
243 settings.setGeolocationEnabled(enableGeolocation());
244 settings.setJavaScriptEnabled(enableJavascript());
245 settings.setLightTouchEnabled(enableLightTouch());
246 settings.setNavDump(enableNavDump());
Derek Sollenberger31adf672011-07-08 11:31:30 -0400247 settings.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
John Reck35e9dd62011-04-25 09:01:54 -0700248 settings.setShowVisualIndicator(enableVisualIndicator());
249 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
250 settings.setDefaultZoom(getDefaultZoom());
251 settings.setMinimumFontSize(getMinimumFontSize());
252 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck92f25f82011-04-26 16:57:10 -0700253 settings.setForceUserScalable(forceEnableUserScalable());
John Reck35e9dd62011-04-25 09:01:54 -0700254 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700255 settings.setTextZoom(getTextZoom());
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700256 settings.setDoubleTapZoom(getDoubleTapZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700257 settings.setAutoFillEnabled(isAutofillEnabled());
258 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100259 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700260 settings.setLoadsImagesAutomatically(loadImages());
261 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
262 settings.setSavePassword(rememberPasswords());
263 settings.setSaveFormData(saveFormdata());
264 settings.setUseWideViewPort(isWideViewport());
265 settings.setAutoFillProfile(getAutoFillProfile());
John Reckb8b2af82011-05-20 15:58:33 -0700266
267 String ua = mCustomUserAgents.get(settings);
John Reck46500332011-06-07 14:36:10 -0700268 if (ua != null) {
John Reckb8b2af82011-05-20 15:58:33 -0700269 settings.setUserAgentString(ua);
270 } else {
271 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
272 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700273
John Reckea17e782011-10-27 17:15:59 -0700274 boolean useInverted = useInvertedRendering();
John Reckeabe5da2011-08-08 13:24:13 -0700275 settings.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700276 useInverted ? "true" : "false");
277 if (useInverted) {
278 settings.setProperty(WebViewProperties.gfxInvertedScreenContrast,
279 Float.toString(getInvertedContrast()));
280 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700281
John Reckea17e782011-10-27 17:15:59 -0700282 if (isDebugEnabled()) {
283 settings.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
284 enableCpuUploadPath() ? "true" : "false");
285 }
Ben Murdochef671652010-11-25 17:17:58 +0000286 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800287
John Reck35e9dd62011-04-25 09:01:54 -0700288 /**
289 * Syncs all the settings that have no UI
290 * These cannot change, so we only need to set them once per WebSettings
291 */
292 private void syncStaticSettings(WebSettings settings) {
293 settings.setDefaultFontSize(16);
294 settings.setDefaultFixedFontSize(13);
295 settings.setPageCacheCapacity(getPageCacheCapacity());
Ben Murdochef671652010-11-25 17:17:58 +0000296
John Reck35e9dd62011-04-25 09:01:54 -0700297 // WebView inside Browser doesn't want initial focus to be set.
298 settings.setNeedInitialFocus(false);
299 // Browser supports multiple windows
300 settings.setSupportMultipleWindows(true);
301 // enable smooth transition for better performance during panning or
302 // zooming
303 settings.setEnableSmoothTransition(true);
304 // disable content url access
305 settings.setAllowContentAccess(false);
306
307 // HTML5 API flags
308 settings.setAppCacheEnabled(true);
309 settings.setDatabaseEnabled(true);
310 settings.setDomStorageEnabled(true);
311 settings.setWorkersEnabled(true); // This only affects V8.
312
313 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700314 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700315 settings.setAppCachePath(getAppCachePath());
316 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
317 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
318 }
319
320 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700321 mNeedsSharedSync = false;
John Reck35e9dd62011-04-25 09:01:54 -0700322 CookieManager.getInstance().setAcceptCookie(acceptCookies());
323 if (mController != null) {
324 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700325 }
John Reck35e9dd62011-04-25 09:01:54 -0700326 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700327
John Reck35e9dd62011-04-25 09:01:54 -0700328 private void syncManagedSettings() {
329 syncSharedSettings();
330 synchronized (mManagedSettings) {
331 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
332 while (iter.hasNext()) {
333 WeakReference<WebSettings> ref = iter.next();
334 WebSettings settings = ref.get();
335 if (settings == null) {
336 iter.remove();
337 continue;
338 }
339 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000340 }
John Reck35e9dd62011-04-25 09:01:54 -0700341 }
342 }
Ben Murdochef671652010-11-25 17:17:58 +0000343
John Reck35e9dd62011-04-25 09:01:54 -0700344 @Override
345 public void onSharedPreferenceChanged(
346 SharedPreferences sharedPreferences, String key) {
347 syncManagedSettings();
348 if (PREF_SEARCH_ENGINE.equals(key)) {
349 updateSearchEngine(false);
350 }
351 if (PREF_USE_INSTANT_SEARCH.equals(key)) {
352 updateSearchEngine(true);
353 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700354 if (PREF_FULLSCREEN.equals(key)) {
355 if (mController.getUi() != null) {
356 mController.getUi().setFullscreen(useFullscreen());
357 }
Michael Kolb0241e752011-07-07 14:58:50 -0700358 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
359 if (mController.getUi() != null) {
360 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
361 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700362 }
John Reck35e9dd62011-04-25 09:01:54 -0700363 }
364
John Reck961d35d2011-06-23 09:45:54 -0700365 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100366 requireInitialization();
367 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700368 }
369
370 public LayoutAlgorithm getLayoutAlgorithm() {
371 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
372 if (autofitPages()) {
373 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
374 }
375 if (isDebugEnabled()) {
376 if (isSmallScreen()) {
377 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000378 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700379 if (isNormalLayout()) {
380 layoutAlgorithm = LayoutAlgorithm.NORMAL;
381 } else {
382 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
383 }
Ben Murdochef671652010-11-25 17:17:58 +0000384 }
John Reck35e9dd62011-04-25 09:01:54 -0700385 }
386 return layoutAlgorithm;
387 }
Ben Murdochef671652010-11-25 17:17:58 +0000388
John Reck35e9dd62011-04-25 09:01:54 -0700389 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700390 requireInitialization();
391 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800392 }
393
John Reck35e9dd62011-04-25 09:01:54 -0700394 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700395 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700396 return mWebStorageSizeManager;
397 }
398
John Reck35e9dd62011-04-25 09:01:54 -0700399 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700400 if (mAppCachePath == null) {
401 mAppCachePath = mContext.getDir("appcache", 0).getPath();
402 }
403 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700404 }
405
406 private void updateSearchEngine(boolean force) {
407 String searchEngineName = getSearchEngineName();
408 if (force || mSearchEngine == null ||
409 !mSearchEngine.getName().equals(searchEngineName)) {
410 if (mSearchEngine != null) {
411 if (mSearchEngine.supportsVoiceSearch()) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000412 // One or more tabs could have been in voice search mode.
413 // Clear it, since the new SearchEngine may not support
414 // it, or may handle it differently.
415 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
416 mController.getTabControl().getTab(i).revertVoiceSearchMode();
417 }
418 }
John Reck35e9dd62011-04-25 09:01:54 -0700419 mSearchEngine.close();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000420 }
John Reck35e9dd62011-04-25 09:01:54 -0700421 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000422
John Reck35e9dd62011-04-25 09:01:54 -0700423 if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) {
424 ((InstantSearchEngine) mSearchEngine).setController(mController);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000425 }
426 }
427 }
428
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100429 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700430 if (mSearchEngine == null) {
431 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100432 }
John Reck35e9dd62011-04-25 09:01:54 -0700433 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100434 }
435
John Reck35e9dd62011-04-25 09:01:54 -0700436 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700437 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700438 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100439 }
440
John Reck35e9dd62011-04-25 09:01:54 -0700441 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700442 Editor edit = mPrefs.edit();
443 edit.putBoolean(PREF_DEBUG_MENU, value);
444 if (!value) {
445 // Reset to "safe" value
446 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
447 }
448 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100449 }
450
John Reck35e9dd62011-04-25 09:01:54 -0700451 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800452 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700453 if (mController != null) {
454 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800455 if (current != null) {
456 current.clearCache(true);
457 }
458 }
459 }
460
John Reck35e9dd62011-04-25 09:01:54 -0700461 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800462 CookieManager.getInstance().removeAllCookie();
463 }
464
John Reck35e9dd62011-04-25 09:01:54 -0700465 public void clearHistory() {
466 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800467 Browser.clearHistory(resolver);
468 Browser.clearSearches(resolver);
469 }
470
John Reck35e9dd62011-04-25 09:01:54 -0700471 public void clearFormData() {
472 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700473 if (mController!= null) {
474 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100475 if (currentTopView != null) {
476 currentTopView.clearFormData();
477 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800478 }
479 }
480
John Reck35e9dd62011-04-25 09:01:54 -0700481 public void clearPasswords() {
482 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800483 db.clearUsernamePassword();
484 db.clearHttpAuthUsernamePassword();
485 }
486
John Reck35e9dd62011-04-25 09:01:54 -0700487 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100488 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100489 }
490
John Reck35e9dd62011-04-25 09:01:54 -0700491 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100492 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100493 }
494
John Reck35e9dd62011-04-25 09:01:54 -0700495 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700496 // Preserve autologin setting
497 long gal = mPrefs.getLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, -1);
498 mPrefs.edit()
499 .clear()
500 .putLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, gal)
501 .apply();
John Reck35e9dd62011-04-25 09:01:54 -0700502 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700503 }
504
John Reck35e9dd62011-04-25 09:01:54 -0700505 public AutoFillProfile getAutoFillProfile() {
506 mAutofillHandler.waitForLoad();
507 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800508 }
509
John Reck35e9dd62011-04-25 09:01:54 -0700510 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
511 mAutofillHandler.waitForLoad();
512 mAutofillHandler.setAutoFillProfile(profile, msg);
Ben Murdoch273330a2011-07-08 18:37:22 +0100513 // Auto-fill will reuse the same profile ID when making edits to the profile,
514 // so we need to force a settings sync (otherwise the SharedPreferences
515 // manager will optimise out the call to onSharedPreferenceChanged(), as
516 // it thinks nothing has changed).
517 syncManagedSettings();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800518 }
519
John Reck35e9dd62011-04-25 09:01:54 -0700520 public void toggleDebugSettings() {
521 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800522 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100523
John Reckb8b2af82011-05-20 15:58:33 -0700524 public boolean hasDesktopUseragent(WebView view) {
525 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
526 }
527
528 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700529 if (view == null) {
530 return;
531 }
John Reckb8b2af82011-05-20 15:58:33 -0700532 WebSettings settings = view.getSettings();
533 if (mCustomUserAgents.get(settings) != null) {
534 mCustomUserAgents.remove(settings);
535 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
536 } else {
537 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
538 settings.setUserAgentString(DESKTOP_USERAGENT);
539 }
540 }
541
John Reck7dc444b2011-06-16 17:44:29 -0700542 public static int getAdjustedMinimumFontSize(int rawValue) {
543 rawValue++; // Preference starts at 0, min font at 1
544 if (rawValue > 1) {
545 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
546 }
547 return rawValue;
548 }
549
John Reck5ba3c762011-09-14 15:00:15 -0700550 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700551 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700552 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700553 }
554
555 static int getRawTextZoom(int percent) {
556 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
557 }
558
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700559 public int getAdjustedDoubleTapZoom(int rawValue) {
560 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
561 return (int) ((rawValue + 100) * mFontSizeMult);
562 }
563
564 static int getRawDoubleTapZoom(int percent) {
565 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
566 }
567
John Reckcadae722011-07-25 11:36:17 -0700568 public SharedPreferences getPreferences() {
569 return mPrefs;
570 }
571
John Reck35e9dd62011-04-25 09:01:54 -0700572 // -----------------------------
573 // getter/setters for accessibility_preferences.xml
574 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100575
John Reck7dc444b2011-06-16 17:44:29 -0700576 @Deprecated
577 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700578 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
579 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100580 }
581
John Reck35e9dd62011-04-25 09:01:54 -0700582 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700583 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700584 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100585 }
586
John Reck92f25f82011-04-26 16:57:10 -0700587 public boolean forceEnableUserScalable() {
588 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
589 }
590
John Reck7dc444b2011-06-16 17:44:29 -0700591 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700592 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700593 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
594 return getAdjustedTextZoom(textZoom);
595 }
596
597 public void setTextZoom(int percent) {
598 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
599 }
600
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700601 public int getDoubleTapZoom() {
602 requireInitialization();
603 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
604 return getAdjustedDoubleTapZoom(doubleTapZoom);
605 }
606
607 public void setDoubleTapZoom(int percent) {
608 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
609 }
610
John Reck35e9dd62011-04-25 09:01:54 -0700611 // -----------------------------
612 // getter/setters for advanced_preferences.xml
613 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100614
John Reck35e9dd62011-04-25 09:01:54 -0700615 public String getSearchEngineName() {
616 return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100617 }
John Reck63bb6872010-12-01 19:29:32 -0800618
John Reck35e9dd62011-04-25 09:01:54 -0700619 public boolean openInBackground() {
620 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800621 }
John Reck35e9dd62011-04-25 09:01:54 -0700622
623 public boolean enableJavascript() {
624 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
625 }
626
627 // TODO: Cache
628 public PluginState getPluginState() {
629 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
630 return PluginState.valueOf(state);
631 }
632
633 // TODO: Cache
634 public ZoomDensity getDefaultZoom() {
635 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
636 return ZoomDensity.valueOf(zoom);
637 }
638
639 public boolean loadPageInOverviewMode() {
640 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
641 }
642
643 public boolean autofitPages() {
644 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
645 }
646
647 public boolean blockPopupWindows() {
648 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
649 }
650
651 public boolean loadImages() {
652 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
653 }
654
655 public String getDefaultTextEncoding() {
656 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
657 }
658
659 // -----------------------------
660 // getter/setters for general_preferences.xml
661 // -----------------------------
662
663 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700664 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
665 }
666
667 public void setHomePage(String value) {
668 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
669 }
670
671 public boolean isAutofillEnabled() {
672 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
673 }
674
675 public void setAutofillEnabled(boolean value) {
676 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
677 }
678
679 // -----------------------------
680 // getter/setters for debug_preferences.xml
681 // -----------------------------
682
683 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700684 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700685 return true;
686 }
687 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
688 }
689
Derek Sollenberger31adf672011-07-08 11:31:30 -0400690 public boolean isSkiaHardwareAccelerated() {
691 if (!isDebugEnabled()) {
692 return false;
693 }
694 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
695 }
696
John Reck35e9dd62011-04-25 09:01:54 -0700697 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700698 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700699 return 0;
700 }
701 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
702 }
703
704 // -----------------------------
705 // getter/setters for hidden_debug_preferences.xml
706 // -----------------------------
707
708 public boolean enableVisualIndicator() {
709 if (!isDebugEnabled()) {
710 return false;
711 }
712 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
713 }
714
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700715 public boolean enableCpuUploadPath() {
716 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700717 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700718 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700719 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700720 }
721
John Reck35e9dd62011-04-25 09:01:54 -0700722 public boolean enableJavascriptConsole() {
723 if (!isDebugEnabled()) {
724 return false;
725 }
726 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
727 }
728
729 public boolean isSmallScreen() {
730 if (!isDebugEnabled()) {
731 return false;
732 }
733 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
734 }
735
736 public boolean isWideViewport() {
737 if (!isDebugEnabled()) {
738 return true;
739 }
740 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
741 }
742
743 public boolean isNormalLayout() {
744 if (!isDebugEnabled()) {
745 return false;
746 }
747 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
748 }
749
750 public boolean isTracing() {
751 if (!isDebugEnabled()) {
752 return false;
753 }
754 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
755 }
756
757 public boolean enableLightTouch() {
758 if (!isDebugEnabled()) {
759 return false;
760 }
761 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
762 }
763
764 public boolean enableNavDump() {
765 if (!isDebugEnabled()) {
766 return false;
767 }
768 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
769 }
770
771 public String getJsEngineFlags() {
772 if (!isDebugEnabled()) {
773 return "";
774 }
775 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
776 }
777
778 // -----------------------------
779 // getter/setters for lab_preferences.xml
780 // -----------------------------
781
782 public boolean useQuickControls() {
783 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
784 }
785
786 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700787 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700788 }
789
790 public boolean useInstantSearch() {
791 return mPrefs.getBoolean(PREF_USE_INSTANT_SEARCH, false);
792 }
793
Michael Kolbc38c6042011-04-27 10:46:06 -0700794 public boolean useFullscreen() {
795 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
796 }
797
John Reck2fd9d0e2011-07-15 11:13:48 -0700798 public boolean useInvertedRendering() {
799 return mPrefs.getBoolean(PREF_INVERTED, false);
800 }
801
Nicolas Roard5d513102011-08-03 15:35:34 -0700802 public float getInvertedContrast() {
803 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
804 }
805
John Reck35e9dd62011-04-25 09:01:54 -0700806 // -----------------------------
807 // getter/setters for privacy_security_preferences.xml
808 // -----------------------------
809
810 public boolean showSecurityWarnings() {
811 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
812 }
813
814 public boolean acceptCookies() {
815 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
816 }
817
818 public boolean saveFormdata() {
819 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
820 }
821
822 public boolean enableGeolocation() {
823 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
824 }
825
826 public boolean rememberPasswords() {
827 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
828 }
829
Michael Kolb14612442011-06-24 13:06:29 -0700830 // -----------------------------
831 // getter/setters for bandwidth_preferences.xml
832 // -----------------------------
833
Mathew Inwood467813f2011-09-02 15:43:17 +0100834 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
835 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -0700836 }
Mathew Inwood467813f2011-09-02 15:43:17 +0100837
838 public static String getPreloadAlwaysPreferenceString(Context context) {
839 return context.getResources().getString(R.string.pref_data_preload_value_always);
840 }
841
Mathew Inwood825fba72011-10-04 14:52:52 +0100842 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
843 "browser_default_preload_setting";
844
845 public String getDefaultPreloadSetting() {
846 String preload = Settings.Secure.getString(mContext.getContentResolver(),
847 DEAULT_PRELOAD_SECURE_SETTING_KEY);
848 if (preload == null) {
849 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
850 }
851 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +0100852 }
853
854 public String getPreloadEnabled() {
855 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
856 }
857
The Android Open Source Project0c908882009-03-03 19:32:16 -0800858}