blob: dd7bb563b936c84fe484b879902f8c516629ae60 [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 }
Ben Murdochef671652010-11-25 17:17:58 +0000149 }
150
John Reck35e9dd62011-04-25 09:01:54 -0700151 public void startManagingSettings(WebSettings settings) {
John Reckc477e712011-08-17 11:27:15 -0700152 if (mNeedsSharedSync) {
153 syncSharedSettings();
154 }
John Reck35e9dd62011-04-25 09:01:54 -0700155 synchronized (mManagedSettings) {
156 syncStaticSettings(settings);
157 syncSetting(settings);
158 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800159 }
160 }
161
John Reckcadae722011-07-25 11:36:17 -0700162 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700163
164 @Override
165 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700166 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
167 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700168 // the cost of one cached page is ~3M (measured using nytimes.com). For
169 // low end devices, we only cache one page. For high end devices, we try
170 // to cache more pages, currently choose 5.
171 if (ActivityManager.staticGetMemoryClass() > 16) {
172 mPageCacheCapacity = 5;
173 }
174 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
175 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
176 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700177 // Workaround b/5254577
178 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700179 if (Build.VERSION.CODENAME.equals("REL")) {
180 // This is a release build, always startup with debug disabled
181 setDebugEnabled(false);
182 }
183 if (mPrefs.contains(PREF_TEXT_SIZE)) {
184 /*
185 * Update from TextSize enum to zoom percent
186 * SMALLEST is 50%
187 * SMALLER is 75%
188 * NORMAL is 100%
189 * LARGER is 150%
190 * LARGEST is 200%
191 */
192 switch (getTextSize()) {
193 case SMALLEST:
194 setTextZoom(50);
195 break;
196 case SMALLER:
197 setTextZoom(75);
198 break;
199 case LARGER:
200 setTextZoom(150);
201 break;
202 case LARGEST:
203 setTextZoom(200);
204 break;
205 }
206 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
207 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100208
209 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
210 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
211 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
212 BrowserProvider.getClientId(mContext.getContentResolver()));
213 }
214
Ben Murdochaaa1f372011-07-25 15:40:58 +0100215 synchronized (BrowserSettings.class) {
216 sInitialized = true;
217 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700218 }
219 }
220 };
221
Ben Murdochaaa1f372011-07-25 15:40:58 +0100222 private static void requireInitialization() {
223 synchronized (BrowserSettings.class) {
224 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700225 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100226 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700227 } catch (InterruptedException e) {
228 }
229 }
230 }
231 }
232
The Android Open Source Project0c908882009-03-03 19:32:16 -0800233 /**
John Reck35e9dd62011-04-25 09:01:54 -0700234 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800235 */
John Reck35e9dd62011-04-25 09:01:54 -0700236 private void syncSetting(WebSettings settings) {
237 settings.setGeolocationEnabled(enableGeolocation());
238 settings.setJavaScriptEnabled(enableJavascript());
239 settings.setLightTouchEnabled(enableLightTouch());
240 settings.setNavDump(enableNavDump());
Derek Sollenberger31adf672011-07-08 11:31:30 -0400241 settings.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
John Reck35e9dd62011-04-25 09:01:54 -0700242 settings.setShowVisualIndicator(enableVisualIndicator());
243 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
244 settings.setDefaultZoom(getDefaultZoom());
245 settings.setMinimumFontSize(getMinimumFontSize());
246 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck92f25f82011-04-26 16:57:10 -0700247 settings.setForceUserScalable(forceEnableUserScalable());
John Reck35e9dd62011-04-25 09:01:54 -0700248 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700249 settings.setTextZoom(getTextZoom());
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700250 settings.setDoubleTapZoom(getDoubleTapZoom());
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 Reckea17e782011-10-27 17:15:59 -0700268 boolean useInverted = useInvertedRendering();
John Reckeabe5da2011-08-08 13:24:13 -0700269 settings.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700270 useInverted ? "true" : "false");
271 if (useInverted) {
272 settings.setProperty(WebViewProperties.gfxInvertedScreenContrast,
273 Float.toString(getInvertedContrast()));
274 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700275
John Reckea17e782011-10-27 17:15:59 -0700276 if (isDebugEnabled()) {
277 settings.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
278 enableCpuUploadPath() ? "true" : "false");
279 }
Ben Murdochef671652010-11-25 17:17:58 +0000280 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800281
John Reck35e9dd62011-04-25 09:01:54 -0700282 /**
283 * Syncs all the settings that have no UI
284 * These cannot change, so we only need to set them once per WebSettings
285 */
286 private void syncStaticSettings(WebSettings settings) {
287 settings.setDefaultFontSize(16);
288 settings.setDefaultFixedFontSize(13);
289 settings.setPageCacheCapacity(getPageCacheCapacity());
Ben Murdochef671652010-11-25 17:17:58 +0000290
John Reck35e9dd62011-04-25 09:01:54 -0700291 // WebView inside Browser doesn't want initial focus to be set.
292 settings.setNeedInitialFocus(false);
293 // Browser supports multiple windows
294 settings.setSupportMultipleWindows(true);
295 // enable smooth transition for better performance during panning or
296 // zooming
297 settings.setEnableSmoothTransition(true);
Teng-Hui Zhu164f74e2011-11-07 12:54:43 -0800298 // WebView should be preserving the memory as much as possible.
299 // However, apps like browser wish to turn on the performance mode which
300 // would require more memory.
301 // TODO: We need to dynamically allocate/deallocate temporary memory for
302 // apps which are trying to use minimal memory. Currently, double
303 // buffering is always turned on, which is unnecessary.
304 settings.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
John Reck35e9dd62011-04-25 09:01:54 -0700305 // disable content url access
306 settings.setAllowContentAccess(false);
307
308 // HTML5 API flags
309 settings.setAppCacheEnabled(true);
310 settings.setDatabaseEnabled(true);
311 settings.setDomStorageEnabled(true);
312 settings.setWorkersEnabled(true); // This only affects V8.
313
314 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700315 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700316 settings.setAppCachePath(getAppCachePath());
317 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
318 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
319 }
320
321 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700322 mNeedsSharedSync = false;
John Reck35e9dd62011-04-25 09:01:54 -0700323 CookieManager.getInstance().setAcceptCookie(acceptCookies());
324 if (mController != null) {
325 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700326 }
John Reck35e9dd62011-04-25 09:01:54 -0700327 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700328
John Reck35e9dd62011-04-25 09:01:54 -0700329 private void syncManagedSettings() {
330 syncSharedSettings();
331 synchronized (mManagedSettings) {
332 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
333 while (iter.hasNext()) {
334 WeakReference<WebSettings> ref = iter.next();
335 WebSettings settings = ref.get();
336 if (settings == null) {
337 iter.remove();
338 continue;
339 }
340 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000341 }
John Reck35e9dd62011-04-25 09:01:54 -0700342 }
343 }
Ben Murdochef671652010-11-25 17:17:58 +0000344
John Reck35e9dd62011-04-25 09:01:54 -0700345 @Override
346 public void onSharedPreferenceChanged(
347 SharedPreferences sharedPreferences, String key) {
348 syncManagedSettings();
349 if (PREF_SEARCH_ENGINE.equals(key)) {
350 updateSearchEngine(false);
351 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700352 if (PREF_FULLSCREEN.equals(key)) {
353 if (mController.getUi() != null) {
354 mController.getUi().setFullscreen(useFullscreen());
355 }
Michael Kolb0241e752011-07-07 14:58:50 -0700356 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
357 if (mController.getUi() != null) {
358 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
359 }
Michael Kolbc38c6042011-04-27 10:46:06 -0700360 }
John Reck35e9dd62011-04-25 09:01:54 -0700361 }
362
John Reck961d35d2011-06-23 09:45:54 -0700363 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100364 requireInitialization();
365 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700366 }
367
368 public LayoutAlgorithm getLayoutAlgorithm() {
369 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
370 if (autofitPages()) {
371 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
372 }
373 if (isDebugEnabled()) {
374 if (isSmallScreen()) {
375 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000376 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700377 if (isNormalLayout()) {
378 layoutAlgorithm = LayoutAlgorithm.NORMAL;
379 } else {
380 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
381 }
Ben Murdochef671652010-11-25 17:17:58 +0000382 }
John Reck35e9dd62011-04-25 09:01:54 -0700383 }
384 return layoutAlgorithm;
385 }
Ben Murdochef671652010-11-25 17:17:58 +0000386
John Reck35e9dd62011-04-25 09:01:54 -0700387 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700388 requireInitialization();
389 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800390 }
391
John Reck35e9dd62011-04-25 09:01:54 -0700392 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700393 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700394 return mWebStorageSizeManager;
395 }
396
John Reck35e9dd62011-04-25 09:01:54 -0700397 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700398 if (mAppCachePath == null) {
399 mAppCachePath = mContext.getDir("appcache", 0).getPath();
400 }
401 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700402 }
403
404 private void updateSearchEngine(boolean force) {
405 String searchEngineName = getSearchEngineName();
406 if (force || mSearchEngine == null ||
407 !mSearchEngine.getName().equals(searchEngineName)) {
408 if (mSearchEngine != null) {
409 if (mSearchEngine.supportsVoiceSearch()) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000410 // One or more tabs could have been in voice search mode.
411 // Clear it, since the new SearchEngine may not support
412 // it, or may handle it differently.
413 for (int i = 0; i < mController.getTabControl().getTabCount(); i++) {
414 mController.getTabControl().getTab(i).revertVoiceSearchMode();
415 }
416 }
John Reck35e9dd62011-04-25 09:01:54 -0700417 mSearchEngine.close();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000418 }
John Reck35e9dd62011-04-25 09:01:54 -0700419 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000420 }
421 }
422
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100423 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700424 if (mSearchEngine == null) {
425 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100426 }
John Reck35e9dd62011-04-25 09:01:54 -0700427 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100428 }
429
John Reck35e9dd62011-04-25 09:01:54 -0700430 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700431 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700432 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100433 }
434
John Reck35e9dd62011-04-25 09:01:54 -0700435 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700436 Editor edit = mPrefs.edit();
437 edit.putBoolean(PREF_DEBUG_MENU, value);
438 if (!value) {
439 // Reset to "safe" value
440 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
441 }
442 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100443 }
444
John Reck35e9dd62011-04-25 09:01:54 -0700445 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800446 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700447 if (mController != null) {
448 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800449 if (current != null) {
450 current.clearCache(true);
451 }
452 }
453 }
454
John Reck35e9dd62011-04-25 09:01:54 -0700455 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800456 CookieManager.getInstance().removeAllCookie();
457 }
458
John Reck35e9dd62011-04-25 09:01:54 -0700459 public void clearHistory() {
460 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800461 Browser.clearHistory(resolver);
462 Browser.clearSearches(resolver);
463 }
464
John Reck35e9dd62011-04-25 09:01:54 -0700465 public void clearFormData() {
466 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700467 if (mController!= null) {
468 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100469 if (currentTopView != null) {
470 currentTopView.clearFormData();
471 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800472 }
473 }
474
John Reck35e9dd62011-04-25 09:01:54 -0700475 public void clearPasswords() {
476 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800477 db.clearUsernamePassword();
478 db.clearHttpAuthUsernamePassword();
479 }
480
John Reck35e9dd62011-04-25 09:01:54 -0700481 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100482 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100483 }
484
John Reck35e9dd62011-04-25 09:01:54 -0700485 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100486 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100487 }
488
John Reck35e9dd62011-04-25 09:01:54 -0700489 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700490 // Preserve autologin setting
491 long gal = mPrefs.getLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, -1);
492 mPrefs.edit()
493 .clear()
494 .putLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, gal)
495 .apply();
John Reck35e9dd62011-04-25 09:01:54 -0700496 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700497 }
498
John Reck35e9dd62011-04-25 09:01:54 -0700499 public AutoFillProfile getAutoFillProfile() {
500 mAutofillHandler.waitForLoad();
501 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800502 }
503
John Reck35e9dd62011-04-25 09:01:54 -0700504 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
505 mAutofillHandler.waitForLoad();
506 mAutofillHandler.setAutoFillProfile(profile, msg);
Ben Murdoch273330a2011-07-08 18:37:22 +0100507 // Auto-fill will reuse the same profile ID when making edits to the profile,
508 // so we need to force a settings sync (otherwise the SharedPreferences
509 // manager will optimise out the call to onSharedPreferenceChanged(), as
510 // it thinks nothing has changed).
511 syncManagedSettings();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800512 }
513
John Reck35e9dd62011-04-25 09:01:54 -0700514 public void toggleDebugSettings() {
515 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800516 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100517
John Reckb8b2af82011-05-20 15:58:33 -0700518 public boolean hasDesktopUseragent(WebView view) {
519 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
520 }
521
522 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700523 if (view == null) {
524 return;
525 }
John Reckb8b2af82011-05-20 15:58:33 -0700526 WebSettings settings = view.getSettings();
527 if (mCustomUserAgents.get(settings) != null) {
528 mCustomUserAgents.remove(settings);
529 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
530 } else {
531 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
532 settings.setUserAgentString(DESKTOP_USERAGENT);
533 }
534 }
535
John Reck7dc444b2011-06-16 17:44:29 -0700536 public static int getAdjustedMinimumFontSize(int rawValue) {
537 rawValue++; // Preference starts at 0, min font at 1
538 if (rawValue > 1) {
539 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
540 }
541 return rawValue;
542 }
543
John Reck5ba3c762011-09-14 15:00:15 -0700544 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700545 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700546 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700547 }
548
549 static int getRawTextZoom(int percent) {
550 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
551 }
552
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700553 public int getAdjustedDoubleTapZoom(int rawValue) {
554 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
555 return (int) ((rawValue + 100) * mFontSizeMult);
556 }
557
558 static int getRawDoubleTapZoom(int percent) {
559 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
560 }
561
John Reckcadae722011-07-25 11:36:17 -0700562 public SharedPreferences getPreferences() {
563 return mPrefs;
564 }
565
John Reck35e9dd62011-04-25 09:01:54 -0700566 // -----------------------------
567 // getter/setters for accessibility_preferences.xml
568 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100569
John Reck7dc444b2011-06-16 17:44:29 -0700570 @Deprecated
571 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700572 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
573 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100574 }
575
John Reck35e9dd62011-04-25 09:01:54 -0700576 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700577 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700578 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100579 }
580
John Reck92f25f82011-04-26 16:57:10 -0700581 public boolean forceEnableUserScalable() {
582 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
583 }
584
John Reck7dc444b2011-06-16 17:44:29 -0700585 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700586 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700587 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
588 return getAdjustedTextZoom(textZoom);
589 }
590
591 public void setTextZoom(int percent) {
592 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
593 }
594
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700595 public int getDoubleTapZoom() {
596 requireInitialization();
597 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
598 return getAdjustedDoubleTapZoom(doubleTapZoom);
599 }
600
601 public void setDoubleTapZoom(int percent) {
602 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
603 }
604
John Reck35e9dd62011-04-25 09:01:54 -0700605 // -----------------------------
606 // getter/setters for advanced_preferences.xml
607 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100608
John Reck35e9dd62011-04-25 09:01:54 -0700609 public String getSearchEngineName() {
610 return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100611 }
John Reck63bb6872010-12-01 19:29:32 -0800612
John Reck35e9dd62011-04-25 09:01:54 -0700613 public boolean openInBackground() {
614 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800615 }
John Reck35e9dd62011-04-25 09:01:54 -0700616
617 public boolean enableJavascript() {
618 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
619 }
620
621 // TODO: Cache
622 public PluginState getPluginState() {
623 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
624 return PluginState.valueOf(state);
625 }
626
627 // TODO: Cache
628 public ZoomDensity getDefaultZoom() {
629 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
630 return ZoomDensity.valueOf(zoom);
631 }
632
633 public boolean loadPageInOverviewMode() {
634 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
635 }
636
637 public boolean autofitPages() {
638 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
639 }
640
641 public boolean blockPopupWindows() {
642 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
643 }
644
645 public boolean loadImages() {
646 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
647 }
648
649 public String getDefaultTextEncoding() {
650 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
651 }
652
653 // -----------------------------
654 // getter/setters for general_preferences.xml
655 // -----------------------------
656
657 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700658 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
659 }
660
661 public void setHomePage(String value) {
662 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
663 }
664
665 public boolean isAutofillEnabled() {
666 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
667 }
668
669 public void setAutofillEnabled(boolean value) {
670 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
671 }
672
673 // -----------------------------
674 // getter/setters for debug_preferences.xml
675 // -----------------------------
676
677 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700678 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700679 return true;
680 }
681 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
682 }
683
Derek Sollenberger31adf672011-07-08 11:31:30 -0400684 public boolean isSkiaHardwareAccelerated() {
685 if (!isDebugEnabled()) {
686 return false;
687 }
688 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
689 }
690
John Reck35e9dd62011-04-25 09:01:54 -0700691 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700692 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700693 return 0;
694 }
695 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
696 }
697
698 // -----------------------------
699 // getter/setters for hidden_debug_preferences.xml
700 // -----------------------------
701
702 public boolean enableVisualIndicator() {
703 if (!isDebugEnabled()) {
704 return false;
705 }
706 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
707 }
708
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700709 public boolean enableCpuUploadPath() {
710 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700711 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700712 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700713 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700714 }
715
John Reck35e9dd62011-04-25 09:01:54 -0700716 public boolean enableJavascriptConsole() {
717 if (!isDebugEnabled()) {
718 return false;
719 }
720 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
721 }
722
723 public boolean isSmallScreen() {
724 if (!isDebugEnabled()) {
725 return false;
726 }
727 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
728 }
729
730 public boolean isWideViewport() {
731 if (!isDebugEnabled()) {
732 return true;
733 }
734 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
735 }
736
737 public boolean isNormalLayout() {
738 if (!isDebugEnabled()) {
739 return false;
740 }
741 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
742 }
743
744 public boolean isTracing() {
745 if (!isDebugEnabled()) {
746 return false;
747 }
748 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
749 }
750
751 public boolean enableLightTouch() {
752 if (!isDebugEnabled()) {
753 return false;
754 }
755 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
756 }
757
758 public boolean enableNavDump() {
759 if (!isDebugEnabled()) {
760 return false;
761 }
762 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
763 }
764
765 public String getJsEngineFlags() {
766 if (!isDebugEnabled()) {
767 return "";
768 }
769 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
770 }
771
772 // -----------------------------
773 // getter/setters for lab_preferences.xml
774 // -----------------------------
775
776 public boolean useQuickControls() {
777 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
778 }
779
780 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700781 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700782 }
783
Michael Kolbc38c6042011-04-27 10:46:06 -0700784 public boolean useFullscreen() {
785 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
786 }
787
John Reck2fd9d0e2011-07-15 11:13:48 -0700788 public boolean useInvertedRendering() {
789 return mPrefs.getBoolean(PREF_INVERTED, false);
790 }
791
Nicolas Roard5d513102011-08-03 15:35:34 -0700792 public float getInvertedContrast() {
793 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
794 }
795
John Reck35e9dd62011-04-25 09:01:54 -0700796 // -----------------------------
797 // getter/setters for privacy_security_preferences.xml
798 // -----------------------------
799
800 public boolean showSecurityWarnings() {
801 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
802 }
803
804 public boolean acceptCookies() {
805 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
806 }
807
808 public boolean saveFormdata() {
809 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
810 }
811
812 public boolean enableGeolocation() {
813 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
814 }
815
816 public boolean rememberPasswords() {
817 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
818 }
819
Michael Kolb14612442011-06-24 13:06:29 -0700820 // -----------------------------
821 // getter/setters for bandwidth_preferences.xml
822 // -----------------------------
823
Mathew Inwood467813f2011-09-02 15:43:17 +0100824 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
825 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -0700826 }
Mathew Inwood467813f2011-09-02 15:43:17 +0100827
828 public static String getPreloadAlwaysPreferenceString(Context context) {
829 return context.getResources().getString(R.string.pref_data_preload_value_always);
830 }
831
Mathew Inwood825fba72011-10-04 14:52:52 +0100832 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
833 "browser_default_preload_setting";
834
835 public String getDefaultPreloadSetting() {
836 String preload = Settings.Secure.getString(mContext.getContentResolver(),
837 DEAULT_PRELOAD_SECURE_SETTING_KEY);
838 if (preload == null) {
839 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
840 }
841 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +0100842 }
843
844 public String getPreloadEnabled() {
845 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
846 }
847
George Mount3636d0a2011-11-21 09:08:21 -0800848 // -----------------------------
849 // getter/setters for browser recovery
850 // -----------------------------
851 /**
852 * The last time browser was started.
853 * @return The last browser start time as System.currentTimeMillis. This
854 * can be 0 if this is the first time or the last tab was closed.
855 */
856 public long getLastRecovered() {
857 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
858 }
859
860 /**
861 * Sets the last browser start time.
862 * @param time The last time as System.currentTimeMillis that the browser
863 * was started. This should be set to 0 if the last tab is closed.
864 */
865 public void setLastRecovered(long time) {
866 mPrefs.edit()
867 .putLong(KEY_LAST_RECOVERED, time)
868 .apply();
869 }
870
871 /**
872 * Used to determine whether or not the previous browser run crashed. Once
873 * the previous state has been determined, the value will be set to false
874 * until a pause is received.
875 * @return true if the last browser run was paused or false if it crashed.
876 */
877 public boolean wasLastRunPaused() {
878 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
879 }
880
881 /**
882 * Sets whether or not the last run was a pause or crash.
883 * @param isPaused Set to true When a pause is received or false after
884 * resuming.
885 */
886 public void setLastRunPaused(boolean isPaused) {
887 mPrefs.edit()
888 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
889 .apply();
890 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800891}