blob: c390ab66f5ad017c6214cf80746d8fe6490224a4 [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;
qqzhou8c5b0a32013-07-22 15:31:03 +080025import android.content.res.AssetManager;
Victoria Lease96497832012-03-23 14:19:56 -070026import android.net.ConnectivityManager;
27import android.net.NetworkInfo;
John Reckf48314f2011-04-27 17:52:17 -070028import android.os.Build;
Ben Murdoch23da30e2010-10-26 15:18:44 +010029import android.os.Message;
qqzhou8c5b0a32013-07-22 15:31:03 +080030import android.os.SystemProperties;
Ben Murdoch0cb81892010-10-08 12:41:33 +010031import android.preference.PreferenceManager;
Ben Murdoch0cb81892010-10-08 12:41:33 +010032import android.provider.Browser;
Mathew Inwood825fba72011-10-04 14:52:52 +010033import android.provider.Settings;
John Reck5ba3c762011-09-14 15:00:15 -070034import android.util.DisplayMetrics;
qqzhou8c5b0a32013-07-22 15:31:03 +080035import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010037import android.webkit.GeolocationPermissions;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.webkit.WebIconDatabase;
39import android.webkit.WebSettings;
John Reck35e9dd62011-04-25 09:01:54 -070040import android.webkit.WebSettings.LayoutAlgorithm;
41import android.webkit.WebSettings.PluginState;
42import android.webkit.WebSettings.TextSize;
43import android.webkit.WebSettings.ZoomDensity;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000044import android.webkit.WebSettingsClassic;
45import android.webkit.WebSettingsClassic.AutoFillProfile;
Nicolas Roard78a98e42009-05-11 13:34:17 +010046import android.webkit.WebStorage;
John Reck812d2d62011-01-18 14:16:15 -080047import android.webkit.WebView;
48import android.webkit.WebViewDatabase;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049
John Reck46500332011-06-07 14:36:10 -070050import com.android.browser.homepages.HomeProvider;
51import com.android.browser.provider.BrowserProvider;
52import com.android.browser.search.SearchEngine;
53import com.android.browser.search.SearchEngines;
54
qqzhou8c5b0a32013-07-22 15:31:03 +080055import java.io.InputStream;
John Reck35e9dd62011-04-25 09:01:54 -070056import java.lang.ref.WeakReference;
57import java.util.Iterator;
58import java.util.LinkedList;
qqzhou8c5b0a32013-07-22 15:31:03 +080059import java.util.Locale;
John Reckb8b2af82011-05-20 15:58:33 -070060import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080061
John Reck35e9dd62011-04-25 09:01:54 -070062/**
63 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080064 */
John Reck35e9dd62011-04-25 09:01:54 -070065public class BrowserSettings implements OnSharedPreferenceChangeListener,
66 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010067
John Reck35e9dd62011-04-25 09:01:54 -070068 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070069 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
70 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
71 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080072
73 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070074 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
75 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070076
77 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070078 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
79 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070080
81 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070082 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
83 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080084
John Reck7fd1e8f2011-04-27 18:22:57 -070085 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
86 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
87 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
88
John Reck35e9dd62011-04-25 09:01:54 -070089 private static final String USER_AGENTS[] = { null,
90 DESKTOP_USERAGENT,
91 IPHONE_USERAGENT,
92 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070093 FROYO_USERAGENT,
94 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070095 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080096
John Reck8fc22a12011-06-16 14:57:41 -070097 // The minimum min font size
98 // Aka, the lower bounds for the min font size range
99 // which is 1:5..24
100 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -0700101 // The initial value in the text zoom range
102 // This is what represents 100% in the SeekBarPreference range
103 private static final int TEXT_ZOOM_START_VAL = 10;
104 // The size of a single step in the text zoom range, in percent
105 private static final int TEXT_ZOOM_STEP = 5;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700106 // The initial value in the double tap zoom range
107 // This is what represents 100% in the SeekBarPreference range
108 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
109 // The size of a single step in the double tap zoom range, in percent
110 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -0700111
John Reck35e9dd62011-04-25 09:01:54 -0700112 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -0700113
John Reck35e9dd62011-04-25 09:01:54 -0700114 private Context mContext;
115 private SharedPreferences mPrefs;
116 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700117 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700118 private WebStorageSizeManager mWebStorageSizeManager;
119 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700120 private WeakHashMap<WebSettings, String> mCustomUserAgents;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100121 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -0700122 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -0700123 private float mFontSizeMult = 1.0f;
John Reck78a6a1d2011-07-21 13:54:03 -0700124
Victoria Lease96497832012-03-23 14:19:56 -0700125 // Current state of network-dependent settings
126 private boolean mLinkPrefetchAllowed = true;
127
John Reck78a6a1d2011-07-21 13:54:03 -0700128 // Cached values
129 private int mPageCacheCapacity = 1;
130 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800131
John Reck35e9dd62011-04-25 09:01:54 -0700132 // Cached settings
133 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800134
Ben Murdochaaa1f372011-07-25 15:40:58 +0100135 private static String sFactoryResetUrl;
136
qqzhou8c5b0a32013-07-22 15:31:03 +0800137 // add for carrier feature
138 private static Context sResPackageCtx;
139
John Reck35e9dd62011-04-25 09:01:54 -0700140 public static void initialize(final Context context) {
141 sInstance = new BrowserSettings(context);
142 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800143
John Reck35e9dd62011-04-25 09:01:54 -0700144 public static BrowserSettings getInstance() {
145 return sInstance;
146 }
Ben Murdochef671652010-11-25 17:17:58 +0000147
John Reck35e9dd62011-04-25 09:01:54 -0700148 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100149 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700150 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
151 mAutofillHandler = new AutofillHandler(mContext);
152 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700153 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reck35e9dd62011-04-25 09:01:54 -0700154 mAutofillHandler.asyncLoadFromDb();
qqzhou8c5b0a32013-07-22 15:31:03 +0800155
156 // add for carrier feature
157 try {
158 sResPackageCtx = context.createPackageContext(
159 "com.android.browser.res",
160 Context.CONTEXT_IGNORE_SECURITY);
161 } catch (Exception e) {
162 Log.e("Res_Update", "Create Res Apk Failed");
163 }
164
John Reckcadae722011-07-25 11:36:17 -0700165 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700166 }
167
168 public void setController(Controller controller) {
169 mController = controller;
John Reckc477e712011-08-17 11:27:15 -0700170 if (sInitialized) {
171 syncSharedSettings();
172 }
Ben Murdochef671652010-11-25 17:17:58 +0000173 }
174
John Reck35e9dd62011-04-25 09:01:54 -0700175 public void startManagingSettings(WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800176
John Reckc477e712011-08-17 11:27:15 -0700177 if (mNeedsSharedSync) {
178 syncSharedSettings();
179 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800180
John Reck35e9dd62011-04-25 09:01:54 -0700181 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800182 syncStaticSettings(settings);
183 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700184 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800185 }
186 }
187
John Reckd1d87312012-03-08 13:25:00 -0800188 public void stopManagingSettings(WebSettings settings) {
189 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
190 while (iter.hasNext()) {
191 WeakReference<WebSettings> ref = iter.next();
192 if (ref.get() == settings) {
193 iter.remove();
194 return;
195 }
196 }
197 }
198
John Reckcadae722011-07-25 11:36:17 -0700199 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700200
201 @Override
202 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700203 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
204 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700205 // the cost of one cached page is ~3M (measured using nytimes.com). For
206 // low end devices, we only cache one page. For high end devices, we try
207 // to cache more pages, currently choose 5.
208 if (ActivityManager.staticGetMemoryClass() > 16) {
209 mPageCacheCapacity = 5;
210 }
211 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
212 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
213 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700214 // Workaround b/5254577
215 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700216 if (Build.VERSION.CODENAME.equals("REL")) {
217 // This is a release build, always startup with debug disabled
218 setDebugEnabled(false);
219 }
220 if (mPrefs.contains(PREF_TEXT_SIZE)) {
221 /*
222 * Update from TextSize enum to zoom percent
223 * SMALLEST is 50%
224 * SMALLER is 75%
225 * NORMAL is 100%
226 * LARGER is 150%
227 * LARGEST is 200%
228 */
229 switch (getTextSize()) {
230 case SMALLEST:
231 setTextZoom(50);
232 break;
233 case SMALLER:
234 setTextZoom(75);
235 break;
236 case LARGER:
237 setTextZoom(150);
238 break;
239 case LARGEST:
240 setTextZoom(200);
241 break;
242 }
243 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
244 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100245
qqzhou8c5b0a32013-07-22 15:31:03 +0800246
247 // add for carrier homepage feature
248 String browserRes = SystemProperties.get("persist.env.c.browser.resource", "default");
249 if ("cu".equals(browserRes)) {
250 int resID = sResPackageCtx.getResources().getIdentifier(
251 "homepage_base", "string", "com.android.browser.res");
252 sFactoryResetUrl = sResPackageCtx.getResources().getString(resID);
253 } else if ("ct".equals(browserRes)) {
254 int resID = sResPackageCtx.getResources().getIdentifier(
255 "homepage_base", "string", "com.android.browser.res");
256 sFactoryResetUrl = sResPackageCtx.getResources().getString(resID);
257
258 int pathID = sResPackageCtx.getResources().getIdentifier(
259 "homepage_path", "string", "com.android.browser.res");
260 String path = sResPackageCtx.getResources().getString(pathID);
261 Locale locale = Locale.getDefault();
262 path = path.replace("%y", locale.getLanguage().toLowerCase());
263 path = path.replace("%z", '_'+locale.getCountry().toLowerCase());
264 boolean useCountry = true;
265 boolean useLanguage = true;
266 InputStream is = null;
267 AssetManager am = mContext.getAssets();
268 try {
269 is = am.open(path);
270 } catch (Exception ignored) {
271 useCountry = false;
272 path = sResPackageCtx.getResources().getString(pathID);
273 path = path.replace("%y", locale.getLanguage().toLowerCase());
274 path = path.replace("%z", "");
275 try {
276 is = am.open(path);
277 } catch (Exception ignoredlanguage) {
278 useLanguage = false;
279 }
280 } finally {
281 if (is != null) {
282 try {
283 is.close();
284 } catch (Exception ignored) {}
285 }
286 }
287
288 if (!useCountry && !useLanguage) {
289 sFactoryResetUrl = sFactoryResetUrl.replace("%y%z", "en");
290 } else {
291 sFactoryResetUrl = sFactoryResetUrl.replace("%y",
292 locale.getLanguage().toLowerCase());
293 sFactoryResetUrl = sFactoryResetUrl.replace("%z", useCountry ?
294 '_' + locale.getCountry().toLowerCase() : "");
295 }
296 } else {
297 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
298 }
299
Ben Murdochaaa1f372011-07-25 15:40:58 +0100300 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
301 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
302 BrowserProvider.getClientId(mContext.getContentResolver()));
303 }
304
Ben Murdochaaa1f372011-07-25 15:40:58 +0100305 synchronized (BrowserSettings.class) {
306 sInitialized = true;
307 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700308 }
309 }
310 };
311
Ben Murdochaaa1f372011-07-25 15:40:58 +0100312 private static void requireInitialization() {
313 synchronized (BrowserSettings.class) {
314 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700315 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100316 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700317 } catch (InterruptedException e) {
318 }
319 }
320 }
321 }
322
The Android Open Source Project0c908882009-03-03 19:32:16 -0800323 /**
John Reck35e9dd62011-04-25 09:01:54 -0700324 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800325 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800326 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700327 settings.setGeolocationEnabled(enableGeolocation());
328 settings.setJavaScriptEnabled(enableJavascript());
329 settings.setLightTouchEnabled(enableLightTouch());
330 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700331 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
332 settings.setDefaultZoom(getDefaultZoom());
333 settings.setMinimumFontSize(getMinimumFontSize());
334 settings.setMinimumLogicalFontSize(getMinimumFontSize());
335 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700336 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700337 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100338 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700339 settings.setLoadsImagesAutomatically(loadImages());
340 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
341 settings.setSavePassword(rememberPasswords());
342 settings.setSaveFormData(saveFormdata());
343 settings.setUseWideViewPort(isWideViewport());
John Reckb8b2af82011-05-20 15:58:33 -0700344
345 String ua = mCustomUserAgents.get(settings);
John Reck46500332011-06-07 14:36:10 -0700346 if (ua != null) {
John Reckb8b2af82011-05-20 15:58:33 -0700347 settings.setUserAgentString(ua);
348 } else {
349 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
350 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700351
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800352 if (!(settings instanceof WebSettingsClassic)) return;
353
354 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
355 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
356 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
357 settingsClassic.setForceUserScalable(forceEnableUserScalable());
358 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
359 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
360 settingsClassic.setAutoFillProfile(getAutoFillProfile());
361
John Reckea17e782011-10-27 17:15:59 -0700362 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800363 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700364 useInverted ? "true" : "false");
365 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800366 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700367 Float.toString(getInvertedContrast()));
368 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700369
John Reckea17e782011-10-27 17:15:59 -0700370 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800371 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700372 enableCpuUploadPath() ? "true" : "false");
373 }
Victoria Lease96497832012-03-23 14:19:56 -0700374
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800375 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000376 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800377
John Reck35e9dd62011-04-25 09:01:54 -0700378 /**
379 * Syncs all the settings that have no UI
380 * These cannot change, so we only need to set them once per WebSettings
381 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800382 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700383 settings.setDefaultFontSize(16);
384 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000385
John Reck35e9dd62011-04-25 09:01:54 -0700386 // WebView inside Browser doesn't want initial focus to be set.
387 settings.setNeedInitialFocus(false);
388 // Browser supports multiple windows
389 settings.setSupportMultipleWindows(true);
390 // enable smooth transition for better performance during panning or
391 // zooming
392 settings.setEnableSmoothTransition(true);
393 // disable content url access
394 settings.setAllowContentAccess(false);
395
396 // HTML5 API flags
397 settings.setAppCacheEnabled(true);
398 settings.setDatabaseEnabled(true);
399 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700400
401 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700402 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700403 settings.setAppCachePath(getAppCachePath());
404 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
405 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700406 // origin policy for file access
407 settings.setAllowUniversalAccessFromFileURLs(false);
408 settings.setAllowFileAccessFromFileURLs(false);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800409
410 if (!(settings instanceof WebSettingsClassic)) return;
411
412 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
413 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
414 // WebView should be preserving the memory as much as possible.
415 // However, apps like browser wish to turn on the performance mode which
416 // would require more memory.
417 // TODO: We need to dynamically allocate/deallocate temporary memory for
418 // apps which are trying to use minimal memory. Currently, double
419 // buffering is always turned on, which is unnecessary.
420 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
421 settingsClassic.setWorkersEnabled(true); // This only affects V8.
John Reck35e9dd62011-04-25 09:01:54 -0700422 }
423
424 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700425 mNeedsSharedSync = false;
John Reck35e9dd62011-04-25 09:01:54 -0700426 CookieManager.getInstance().setAcceptCookie(acceptCookies());
427 if (mController != null) {
428 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700429 }
John Reck35e9dd62011-04-25 09:01:54 -0700430 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700431
John Reck35e9dd62011-04-25 09:01:54 -0700432 private void syncManagedSettings() {
433 syncSharedSettings();
434 synchronized (mManagedSettings) {
435 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
436 while (iter.hasNext()) {
437 WeakReference<WebSettings> ref = iter.next();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800438 WebSettings settings = ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700439 if (settings == null) {
440 iter.remove();
441 continue;
442 }
443 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000444 }
John Reck35e9dd62011-04-25 09:01:54 -0700445 }
446 }
Ben Murdochef671652010-11-25 17:17:58 +0000447
John Reck35e9dd62011-04-25 09:01:54 -0700448 @Override
449 public void onSharedPreferenceChanged(
450 SharedPreferences sharedPreferences, String key) {
451 syncManagedSettings();
452 if (PREF_SEARCH_ENGINE.equals(key)) {
453 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700454 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200455 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700456 mController.getUi().setFullscreen(useFullscreen());
457 }
Michael Kolb0241e752011-07-07 14:58:50 -0700458 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200459 if (mController != null && mController.getUi() != null) {
Michael Kolb0241e752011-07-07 14:58:50 -0700460 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
461 }
Victoria Lease96497832012-03-23 14:19:56 -0700462 } else if (PREF_LINK_PREFETCH.equals(key)) {
463 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700464 }
John Reck35e9dd62011-04-25 09:01:54 -0700465 }
466
John Reck961d35d2011-06-23 09:45:54 -0700467 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100468 requireInitialization();
469 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700470 }
471
472 public LayoutAlgorithm getLayoutAlgorithm() {
473 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
474 if (autofitPages()) {
475 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
476 }
477 if (isDebugEnabled()) {
478 if (isSmallScreen()) {
479 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000480 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700481 if (isNormalLayout()) {
482 layoutAlgorithm = LayoutAlgorithm.NORMAL;
483 } else {
484 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
485 }
Ben Murdochef671652010-11-25 17:17:58 +0000486 }
John Reck35e9dd62011-04-25 09:01:54 -0700487 }
488 return layoutAlgorithm;
489 }
Ben Murdochef671652010-11-25 17:17:58 +0000490
John Reck35e9dd62011-04-25 09:01:54 -0700491 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700492 requireInitialization();
493 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800494 }
495
John Reck35e9dd62011-04-25 09:01:54 -0700496 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700497 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700498 return mWebStorageSizeManager;
499 }
500
John Reck35e9dd62011-04-25 09:01:54 -0700501 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700502 if (mAppCachePath == null) {
503 mAppCachePath = mContext.getDir("appcache", 0).getPath();
504 }
505 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700506 }
507
508 private void updateSearchEngine(boolean force) {
509 String searchEngineName = getSearchEngineName();
510 if (force || mSearchEngine == null ||
511 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700512 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000513 }
514 }
515
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100516 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700517 if (mSearchEngine == null) {
518 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100519 }
John Reck35e9dd62011-04-25 09:01:54 -0700520 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100521 }
522
John Reck35e9dd62011-04-25 09:01:54 -0700523 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700524 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700525 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100526 }
527
John Reck35e9dd62011-04-25 09:01:54 -0700528 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700529 Editor edit = mPrefs.edit();
530 edit.putBoolean(PREF_DEBUG_MENU, value);
531 if (!value) {
532 // Reset to "safe" value
533 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
534 }
535 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100536 }
537
John Reck35e9dd62011-04-25 09:01:54 -0700538 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800539 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700540 if (mController != null) {
541 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800542 if (current != null) {
543 current.clearCache(true);
544 }
545 }
546 }
547
John Reck35e9dd62011-04-25 09:01:54 -0700548 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800549 CookieManager.getInstance().removeAllCookie();
550 }
551
John Reck35e9dd62011-04-25 09:01:54 -0700552 public void clearHistory() {
553 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800554 Browser.clearHistory(resolver);
555 Browser.clearSearches(resolver);
556 }
557
John Reck35e9dd62011-04-25 09:01:54 -0700558 public void clearFormData() {
559 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700560 if (mController!= null) {
561 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100562 if (currentTopView != null) {
563 currentTopView.clearFormData();
564 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800565 }
566 }
567
John Reck35e9dd62011-04-25 09:01:54 -0700568 public void clearPasswords() {
569 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800570 db.clearUsernamePassword();
571 db.clearHttpAuthUsernamePassword();
572 }
573
John Reck35e9dd62011-04-25 09:01:54 -0700574 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100575 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100576 }
577
John Reck35e9dd62011-04-25 09:01:54 -0700578 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100579 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100580 }
581
John Reck35e9dd62011-04-25 09:01:54 -0700582 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700583 // Preserve autologin setting
584 long gal = mPrefs.getLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, -1);
585 mPrefs.edit()
586 .clear()
587 .putLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, gal)
588 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200589 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700590 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700591 }
592
Björn Isakssonc885a242012-06-05 17:19:04 +0200593 private void resetCachedValues() {
594 updateSearchEngine(false);
595 }
596
John Reck35e9dd62011-04-25 09:01:54 -0700597 public AutoFillProfile getAutoFillProfile() {
John Reck35e9dd62011-04-25 09:01:54 -0700598 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800599 }
600
John Reck35e9dd62011-04-25 09:01:54 -0700601 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
John Reck35e9dd62011-04-25 09:01:54 -0700602 mAutofillHandler.setAutoFillProfile(profile, msg);
Ben Murdoch273330a2011-07-08 18:37:22 +0100603 // Auto-fill will reuse the same profile ID when making edits to the profile,
604 // so we need to force a settings sync (otherwise the SharedPreferences
605 // manager will optimise out the call to onSharedPreferenceChanged(), as
606 // it thinks nothing has changed).
607 syncManagedSettings();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800608 }
609
John Reck35e9dd62011-04-25 09:01:54 -0700610 public void toggleDebugSettings() {
611 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800612 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100613
John Reckb8b2af82011-05-20 15:58:33 -0700614 public boolean hasDesktopUseragent(WebView view) {
615 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
616 }
617
618 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700619 if (view == null) {
620 return;
621 }
John Reckb8b2af82011-05-20 15:58:33 -0700622 WebSettings settings = view.getSettings();
623 if (mCustomUserAgents.get(settings) != null) {
624 mCustomUserAgents.remove(settings);
625 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
626 } else {
627 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
628 settings.setUserAgentString(DESKTOP_USERAGENT);
629 }
630 }
631
John Reck7dc444b2011-06-16 17:44:29 -0700632 public static int getAdjustedMinimumFontSize(int rawValue) {
633 rawValue++; // Preference starts at 0, min font at 1
634 if (rawValue > 1) {
635 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
636 }
637 return rawValue;
638 }
639
John Reck5ba3c762011-09-14 15:00:15 -0700640 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700641 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700642 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700643 }
644
645 static int getRawTextZoom(int percent) {
646 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
647 }
648
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700649 public int getAdjustedDoubleTapZoom(int rawValue) {
650 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
651 return (int) ((rawValue + 100) * mFontSizeMult);
652 }
653
654 static int getRawDoubleTapZoom(int percent) {
655 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
656 }
657
John Reckcadae722011-07-25 11:36:17 -0700658 public SharedPreferences getPreferences() {
659 return mPrefs;
660 }
661
Victoria Lease96497832012-03-23 14:19:56 -0700662 // update connectivity-dependent options
663 public void updateConnectionType() {
664 ConnectivityManager cm = (ConnectivityManager)
665 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
666 String linkPrefetchPreference = getLinkPrefetchEnabled();
667 boolean linkPrefetchAllowed = linkPrefetchPreference.
668 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
669 NetworkInfo ni = cm.getActiveNetworkInfo();
670 if (ni != null) {
671 switch (ni.getType()) {
672 case ConnectivityManager.TYPE_WIFI:
673 case ConnectivityManager.TYPE_ETHERNET:
674 case ConnectivityManager.TYPE_BLUETOOTH:
675 linkPrefetchAllowed |= linkPrefetchPreference.
676 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
677 break;
678 case ConnectivityManager.TYPE_MOBILE:
679 case ConnectivityManager.TYPE_MOBILE_DUN:
680 case ConnectivityManager.TYPE_MOBILE_MMS:
681 case ConnectivityManager.TYPE_MOBILE_SUPL:
682 case ConnectivityManager.TYPE_WIMAX:
683 default:
684 break;
685 }
686 }
687 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
688 mLinkPrefetchAllowed = linkPrefetchAllowed;
689 syncManagedSettings();
690 }
691 }
692
John Reck35e9dd62011-04-25 09:01:54 -0700693 // -----------------------------
694 // getter/setters for accessibility_preferences.xml
695 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100696
John Reck7dc444b2011-06-16 17:44:29 -0700697 @Deprecated
698 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700699 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
700 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100701 }
702
John Reck35e9dd62011-04-25 09:01:54 -0700703 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700704 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700705 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100706 }
707
John Reck92f25f82011-04-26 16:57:10 -0700708 public boolean forceEnableUserScalable() {
709 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
710 }
711
John Reck7dc444b2011-06-16 17:44:29 -0700712 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700713 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700714 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
715 return getAdjustedTextZoom(textZoom);
716 }
717
718 public void setTextZoom(int percent) {
719 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
720 }
721
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700722 public int getDoubleTapZoom() {
723 requireInitialization();
724 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
725 return getAdjustedDoubleTapZoom(doubleTapZoom);
726 }
727
728 public void setDoubleTapZoom(int percent) {
729 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
730 }
731
John Reck35e9dd62011-04-25 09:01:54 -0700732 // -----------------------------
733 // getter/setters for advanced_preferences.xml
734 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100735
John Reck35e9dd62011-04-25 09:01:54 -0700736 public String getSearchEngineName() {
737 return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100738 }
John Reck63bb6872010-12-01 19:29:32 -0800739
Michael Kolb8d772b02012-01-19 13:56:00 -0800740 public boolean allowAppTabs() {
741 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
742 }
743
John Reck35e9dd62011-04-25 09:01:54 -0700744 public boolean openInBackground() {
745 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800746 }
John Reck35e9dd62011-04-25 09:01:54 -0700747
748 public boolean enableJavascript() {
749 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
750 }
751
752 // TODO: Cache
753 public PluginState getPluginState() {
754 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
755 return PluginState.valueOf(state);
756 }
757
758 // TODO: Cache
759 public ZoomDensity getDefaultZoom() {
760 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
761 return ZoomDensity.valueOf(zoom);
762 }
763
764 public boolean loadPageInOverviewMode() {
765 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
766 }
767
768 public boolean autofitPages() {
769 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
770 }
771
772 public boolean blockPopupWindows() {
773 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
774 }
775
776 public boolean loadImages() {
777 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
778 }
779
780 public String getDefaultTextEncoding() {
781 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
782 }
783
784 // -----------------------------
785 // getter/setters for general_preferences.xml
786 // -----------------------------
787
788 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700789 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
790 }
791
792 public void setHomePage(String value) {
793 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
794 }
795
796 public boolean isAutofillEnabled() {
797 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
798 }
799
800 public void setAutofillEnabled(boolean value) {
801 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
802 }
803
804 // -----------------------------
805 // getter/setters for debug_preferences.xml
806 // -----------------------------
807
808 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700809 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700810 return true;
811 }
812 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
813 }
814
Derek Sollenberger31adf672011-07-08 11:31:30 -0400815 public boolean isSkiaHardwareAccelerated() {
816 if (!isDebugEnabled()) {
817 return false;
818 }
819 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
820 }
821
John Reck35e9dd62011-04-25 09:01:54 -0700822 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700823 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700824 return 0;
825 }
826 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
827 }
828
829 // -----------------------------
830 // getter/setters for hidden_debug_preferences.xml
831 // -----------------------------
832
833 public boolean enableVisualIndicator() {
834 if (!isDebugEnabled()) {
835 return false;
836 }
837 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
838 }
839
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700840 public boolean enableCpuUploadPath() {
841 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700842 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700843 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700844 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700845 }
846
John Reck35e9dd62011-04-25 09:01:54 -0700847 public boolean enableJavascriptConsole() {
848 if (!isDebugEnabled()) {
849 return false;
850 }
851 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
852 }
853
854 public boolean isSmallScreen() {
855 if (!isDebugEnabled()) {
856 return false;
857 }
858 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
859 }
860
861 public boolean isWideViewport() {
862 if (!isDebugEnabled()) {
863 return true;
864 }
865 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
866 }
867
868 public boolean isNormalLayout() {
869 if (!isDebugEnabled()) {
870 return false;
871 }
872 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
873 }
874
875 public boolean isTracing() {
876 if (!isDebugEnabled()) {
877 return false;
878 }
879 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
880 }
881
882 public boolean enableLightTouch() {
883 if (!isDebugEnabled()) {
884 return false;
885 }
886 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
887 }
888
889 public boolean enableNavDump() {
890 if (!isDebugEnabled()) {
891 return false;
892 }
893 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
894 }
895
896 public String getJsEngineFlags() {
897 if (!isDebugEnabled()) {
898 return "";
899 }
900 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
901 }
902
903 // -----------------------------
904 // getter/setters for lab_preferences.xml
905 // -----------------------------
906
907 public boolean useQuickControls() {
908 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
909 }
910
911 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700912 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700913 }
914
Michael Kolbc38c6042011-04-27 10:46:06 -0700915 public boolean useFullscreen() {
916 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
917 }
918
John Reck2fd9d0e2011-07-15 11:13:48 -0700919 public boolean useInvertedRendering() {
920 return mPrefs.getBoolean(PREF_INVERTED, false);
921 }
922
Nicolas Roard5d513102011-08-03 15:35:34 -0700923 public float getInvertedContrast() {
924 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
925 }
926
John Reck35e9dd62011-04-25 09:01:54 -0700927 // -----------------------------
928 // getter/setters for privacy_security_preferences.xml
929 // -----------------------------
930
931 public boolean showSecurityWarnings() {
932 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
933 }
934
935 public boolean acceptCookies() {
936 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
937 }
938
939 public boolean saveFormdata() {
940 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
941 }
942
943 public boolean enableGeolocation() {
944 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
945 }
946
947 public boolean rememberPasswords() {
948 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
949 }
950
Michael Kolb14612442011-06-24 13:06:29 -0700951 // -----------------------------
952 // getter/setters for bandwidth_preferences.xml
953 // -----------------------------
954
Mathew Inwood467813f2011-09-02 15:43:17 +0100955 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
956 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -0700957 }
Mathew Inwood467813f2011-09-02 15:43:17 +0100958
959 public static String getPreloadAlwaysPreferenceString(Context context) {
960 return context.getResources().getString(R.string.pref_data_preload_value_always);
961 }
962
Mathew Inwood825fba72011-10-04 14:52:52 +0100963 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
964 "browser_default_preload_setting";
965
966 public String getDefaultPreloadSetting() {
967 String preload = Settings.Secure.getString(mContext.getContentResolver(),
968 DEAULT_PRELOAD_SECURE_SETTING_KEY);
969 if (preload == null) {
970 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
971 }
972 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +0100973 }
974
975 public String getPreloadEnabled() {
976 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
977 }
978
Victoria Lease96497832012-03-23 14:19:56 -0700979 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
980 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
981 }
982
983 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
984 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
985 }
986
987 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
988 "browser_default_link_prefetch_setting";
989
990 public String getDefaultLinkPrefetchSetting() {
991 String preload = Settings.Secure.getString(mContext.getContentResolver(),
992 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
993 if (preload == null) {
994 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
995 }
996 return preload;
997 }
998
999 public String getLinkPrefetchEnabled() {
1000 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1001 }
1002
George Mount3636d0a2011-11-21 09:08:21 -08001003 // -----------------------------
1004 // getter/setters for browser recovery
1005 // -----------------------------
1006 /**
1007 * The last time browser was started.
1008 * @return The last browser start time as System.currentTimeMillis. This
1009 * can be 0 if this is the first time or the last tab was closed.
1010 */
1011 public long getLastRecovered() {
1012 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1013 }
1014
1015 /**
1016 * Sets the last browser start time.
1017 * @param time The last time as System.currentTimeMillis that the browser
1018 * was started. This should be set to 0 if the last tab is closed.
1019 */
1020 public void setLastRecovered(long time) {
1021 mPrefs.edit()
1022 .putLong(KEY_LAST_RECOVERED, time)
1023 .apply();
1024 }
1025
1026 /**
1027 * Used to determine whether or not the previous browser run crashed. Once
1028 * the previous state has been determined, the value will be set to false
1029 * until a pause is received.
1030 * @return true if the last browser run was paused or false if it crashed.
1031 */
1032 public boolean wasLastRunPaused() {
1033 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1034 }
1035
1036 /**
1037 * Sets whether or not the last run was a pause or crash.
1038 * @param isPaused Set to true When a pause is received or false after
1039 * resuming.
1040 */
1041 public void setLastRunPaused(boolean isPaused) {
1042 mPrefs.edit()
1043 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1044 .apply();
1045 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001046}