blob: 9e0971d53d9889a4bfdbc96ae340a5d7e9dfad29 [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;
qqzhoue6ff8b42013-07-23 17:28:48 +080036import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010038import android.webkit.GeolocationPermissions;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.webkit.WebIconDatabase;
40import android.webkit.WebSettings;
John Reck35e9dd62011-04-25 09:01:54 -070041import android.webkit.WebSettings.LayoutAlgorithm;
42import android.webkit.WebSettings.PluginState;
43import android.webkit.WebSettings.TextSize;
44import android.webkit.WebSettings.ZoomDensity;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000045import android.webkit.WebSettingsClassic;
46import android.webkit.WebSettingsClassic.AutoFillProfile;
Nicolas Roard78a98e42009-05-11 13:34:17 +010047import android.webkit.WebStorage;
John Reck812d2d62011-01-18 14:16:15 -080048import android.webkit.WebView;
49import android.webkit.WebViewDatabase;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050
John Reck46500332011-06-07 14:36:10 -070051import com.android.browser.homepages.HomeProvider;
52import com.android.browser.provider.BrowserProvider;
53import com.android.browser.search.SearchEngine;
54import com.android.browser.search.SearchEngines;
55
qqzhou8c5b0a32013-07-22 15:31:03 +080056import java.io.InputStream;
qqzhoue6ff8b42013-07-23 17:28:48 +080057import java.lang.Class;
John Reck35e9dd62011-04-25 09:01:54 -070058import java.lang.ref.WeakReference;
qqzhoue6ff8b42013-07-23 17:28:48 +080059import java.lang.reflect.Method;
John Reck35e9dd62011-04-25 09:01:54 -070060import java.util.Iterator;
61import java.util.LinkedList;
qqzhou8c5b0a32013-07-22 15:31:03 +080062import java.util.Locale;
John Reckb8b2af82011-05-20 15:58:33 -070063import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080064
John Reck35e9dd62011-04-25 09:01:54 -070065/**
66 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080067 */
John Reck35e9dd62011-04-25 09:01:54 -070068public class BrowserSettings implements OnSharedPreferenceChangeListener,
69 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010070
John Reck35e9dd62011-04-25 09:01:54 -070071 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070072 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
73 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
74 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080075
76 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070077 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
78 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070079
80 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070081 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
82 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070083
84 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070085 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
86 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080087
John Reck7fd1e8f2011-04-27 18:22:57 -070088 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
89 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
90 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
91
John Reck35e9dd62011-04-25 09:01:54 -070092 private static final String USER_AGENTS[] = { null,
93 DESKTOP_USERAGENT,
94 IPHONE_USERAGENT,
95 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070096 FROYO_USERAGENT,
97 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070098 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080099
qqzhoue6ff8b42013-07-23 17:28:48 +0800100 private static final String TAG = "BrowserSettings";
101
John Reck8fc22a12011-06-16 14:57:41 -0700102 // The minimum min font size
103 // Aka, the lower bounds for the min font size range
104 // which is 1:5..24
105 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -0700106 // The initial value in the text zoom range
107 // This is what represents 100% in the SeekBarPreference range
108 private static final int TEXT_ZOOM_START_VAL = 10;
109 // The size of a single step in the text zoom range, in percent
110 private static final int TEXT_ZOOM_STEP = 5;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700111 // The initial value in the double tap zoom range
112 // This is what represents 100% in the SeekBarPreference range
113 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
114 // The size of a single step in the double tap zoom range, in percent
115 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -0700116
John Reck35e9dd62011-04-25 09:01:54 -0700117 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -0700118
John Reck35e9dd62011-04-25 09:01:54 -0700119 private Context mContext;
120 private SharedPreferences mPrefs;
121 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700122 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700123 private WebStorageSizeManager mWebStorageSizeManager;
124 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700125 private WeakHashMap<WebSettings, String> mCustomUserAgents;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100126 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -0700127 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -0700128 private float mFontSizeMult = 1.0f;
John Reck78a6a1d2011-07-21 13:54:03 -0700129
Victoria Lease96497832012-03-23 14:19:56 -0700130 // Current state of network-dependent settings
131 private boolean mLinkPrefetchAllowed = true;
132
John Reck78a6a1d2011-07-21 13:54:03 -0700133 // Cached values
134 private int mPageCacheCapacity = 1;
135 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800136
John Reck35e9dd62011-04-25 09:01:54 -0700137 // Cached settings
138 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800139
Ben Murdochaaa1f372011-07-25 15:40:58 +0100140 private static String sFactoryResetUrl;
141
qqzhou8c5b0a32013-07-22 15:31:03 +0800142 // add for carrier feature
143 private static Context sResPackageCtx;
144
John Reck35e9dd62011-04-25 09:01:54 -0700145 public static void initialize(final Context context) {
146 sInstance = new BrowserSettings(context);
147 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800148
John Reck35e9dd62011-04-25 09:01:54 -0700149 public static BrowserSettings getInstance() {
150 return sInstance;
151 }
Ben Murdochef671652010-11-25 17:17:58 +0000152
John Reck35e9dd62011-04-25 09:01:54 -0700153 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100154 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700155 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
156 mAutofillHandler = new AutofillHandler(mContext);
157 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700158 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reck35e9dd62011-04-25 09:01:54 -0700159 mAutofillHandler.asyncLoadFromDb();
qqzhou8c5b0a32013-07-22 15:31:03 +0800160
161 // add for carrier feature
162 try {
163 sResPackageCtx = context.createPackageContext(
164 "com.android.browser.res",
165 Context.CONTEXT_IGNORE_SECURITY);
166 } catch (Exception e) {
167 Log.e("Res_Update", "Create Res Apk Failed");
168 }
169
John Reckcadae722011-07-25 11:36:17 -0700170 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700171 }
172
173 public void setController(Controller controller) {
174 mController = controller;
John Reckc477e712011-08-17 11:27:15 -0700175 if (sInitialized) {
176 syncSharedSettings();
177 }
Ben Murdochef671652010-11-25 17:17:58 +0000178 }
179
John Reck35e9dd62011-04-25 09:01:54 -0700180 public void startManagingSettings(WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800181
John Reckc477e712011-08-17 11:27:15 -0700182 if (mNeedsSharedSync) {
183 syncSharedSettings();
184 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800185
John Reck35e9dd62011-04-25 09:01:54 -0700186 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800187 syncStaticSettings(settings);
188 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700189 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 }
191 }
192
John Reckd1d87312012-03-08 13:25:00 -0800193 public void stopManagingSettings(WebSettings settings) {
194 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
195 while (iter.hasNext()) {
196 WeakReference<WebSettings> ref = iter.next();
197 if (ref.get() == settings) {
198 iter.remove();
199 return;
200 }
201 }
202 }
203
John Reckcadae722011-07-25 11:36:17 -0700204 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700205
206 @Override
207 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700208 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
209 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700210 // the cost of one cached page is ~3M (measured using nytimes.com). For
211 // low end devices, we only cache one page. For high end devices, we try
212 // to cache more pages, currently choose 5.
213 if (ActivityManager.staticGetMemoryClass() > 16) {
214 mPageCacheCapacity = 5;
215 }
216 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
217 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
218 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700219 // Workaround b/5254577
220 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700221 if (Build.VERSION.CODENAME.equals("REL")) {
222 // This is a release build, always startup with debug disabled
223 setDebugEnabled(false);
224 }
225 if (mPrefs.contains(PREF_TEXT_SIZE)) {
226 /*
227 * Update from TextSize enum to zoom percent
228 * SMALLEST is 50%
229 * SMALLER is 75%
230 * NORMAL is 100%
231 * LARGER is 150%
232 * LARGEST is 200%
233 */
234 switch (getTextSize()) {
235 case SMALLEST:
236 setTextZoom(50);
237 break;
238 case SMALLER:
239 setTextZoom(75);
240 break;
241 case LARGER:
242 setTextZoom(150);
243 break;
244 case LARGEST:
245 setTextZoom(200);
246 break;
247 }
248 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
249 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100250
qqzhou8c5b0a32013-07-22 15:31:03 +0800251
252 // add for carrier homepage feature
253 String browserRes = SystemProperties.get("persist.env.c.browser.resource", "default");
254 if ("cu".equals(browserRes)) {
255 int resID = sResPackageCtx.getResources().getIdentifier(
256 "homepage_base", "string", "com.android.browser.res");
257 sFactoryResetUrl = sResPackageCtx.getResources().getString(resID);
258 } else if ("ct".equals(browserRes)) {
259 int resID = sResPackageCtx.getResources().getIdentifier(
260 "homepage_base", "string", "com.android.browser.res");
261 sFactoryResetUrl = sResPackageCtx.getResources().getString(resID);
262
263 int pathID = sResPackageCtx.getResources().getIdentifier(
264 "homepage_path", "string", "com.android.browser.res");
265 String path = sResPackageCtx.getResources().getString(pathID);
266 Locale locale = Locale.getDefault();
267 path = path.replace("%y", locale.getLanguage().toLowerCase());
268 path = path.replace("%z", '_'+locale.getCountry().toLowerCase());
269 boolean useCountry = true;
270 boolean useLanguage = true;
271 InputStream is = null;
272 AssetManager am = mContext.getAssets();
273 try {
274 is = am.open(path);
275 } catch (Exception ignored) {
276 useCountry = false;
277 path = sResPackageCtx.getResources().getString(pathID);
278 path = path.replace("%y", locale.getLanguage().toLowerCase());
279 path = path.replace("%z", "");
280 try {
281 is = am.open(path);
282 } catch (Exception ignoredlanguage) {
283 useLanguage = false;
284 }
285 } finally {
286 if (is != null) {
287 try {
288 is.close();
289 } catch (Exception ignored) {}
290 }
291 }
292
293 if (!useCountry && !useLanguage) {
294 sFactoryResetUrl = sFactoryResetUrl.replace("%y%z", "en");
295 } else {
296 sFactoryResetUrl = sFactoryResetUrl.replace("%y",
297 locale.getLanguage().toLowerCase());
298 sFactoryResetUrl = sFactoryResetUrl.replace("%z", useCountry ?
299 '_' + locale.getCountry().toLowerCase() : "");
300 }
301 } else {
302 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
303 }
304
Ben Murdochaaa1f372011-07-25 15:40:58 +0100305 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
306 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
307 BrowserProvider.getClientId(mContext.getContentResolver()));
308 }
309
Ben Murdochaaa1f372011-07-25 15:40:58 +0100310 synchronized (BrowserSettings.class) {
311 sInitialized = true;
312 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700313 }
314 }
315 };
316
Ben Murdochaaa1f372011-07-25 15:40:58 +0100317 private static void requireInitialization() {
318 synchronized (BrowserSettings.class) {
319 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700320 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100321 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700322 } catch (InterruptedException e) {
323 }
324 }
325 }
326 }
327
The Android Open Source Project0c908882009-03-03 19:32:16 -0800328 /**
John Reck35e9dd62011-04-25 09:01:54 -0700329 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800330 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800331 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700332 settings.setGeolocationEnabled(enableGeolocation());
333 settings.setJavaScriptEnabled(enableJavascript());
334 settings.setLightTouchEnabled(enableLightTouch());
335 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700336 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
337 settings.setDefaultZoom(getDefaultZoom());
338 settings.setMinimumFontSize(getMinimumFontSize());
339 settings.setMinimumLogicalFontSize(getMinimumFontSize());
340 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700341 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700342 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100343 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700344 settings.setLoadsImagesAutomatically(loadImages());
345 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
346 settings.setSavePassword(rememberPasswords());
347 settings.setSaveFormData(saveFormdata());
348 settings.setUseWideViewPort(isWideViewport());
John Reckb8b2af82011-05-20 15:58:33 -0700349
qqzhoue6ff8b42013-07-23 17:28:48 +0800350 // add for carrier useragent feature
351 String ua = null;
352 try {
353 Class c = Class.forName("com.qrd.useragent.UserAgentHandler");
354 Object cObj = c.newInstance();
355 Method m = c.getDeclaredMethod("getUAString", Context.class);
356 ua = (String)m.invoke(cObj, mContext);
357 } catch (Exception e) {
358 Log.e(TAG, "plug in Load failed, err " + e);
359 ua = mCustomUserAgents.get(settings);
360 }
361
John Reck46500332011-06-07 14:36:10 -0700362 if (ua != null) {
John Reckb8b2af82011-05-20 15:58:33 -0700363 settings.setUserAgentString(ua);
364 } else {
365 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
366 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700367
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800368 if (!(settings instanceof WebSettingsClassic)) return;
369
370 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
371 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
372 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
373 settingsClassic.setForceUserScalable(forceEnableUserScalable());
374 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
375 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
376 settingsClassic.setAutoFillProfile(getAutoFillProfile());
377
John Reckea17e782011-10-27 17:15:59 -0700378 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800379 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700380 useInverted ? "true" : "false");
381 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800382 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700383 Float.toString(getInvertedContrast()));
384 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700385
John Reckea17e782011-10-27 17:15:59 -0700386 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800387 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700388 enableCpuUploadPath() ? "true" : "false");
389 }
Victoria Lease96497832012-03-23 14:19:56 -0700390
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800391 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000392 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800393
John Reck35e9dd62011-04-25 09:01:54 -0700394 /**
395 * Syncs all the settings that have no UI
396 * These cannot change, so we only need to set them once per WebSettings
397 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800398 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700399 settings.setDefaultFontSize(16);
400 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000401
John Reck35e9dd62011-04-25 09:01:54 -0700402 // WebView inside Browser doesn't want initial focus to be set.
403 settings.setNeedInitialFocus(false);
404 // Browser supports multiple windows
405 settings.setSupportMultipleWindows(true);
406 // enable smooth transition for better performance during panning or
407 // zooming
408 settings.setEnableSmoothTransition(true);
409 // disable content url access
410 settings.setAllowContentAccess(false);
411
412 // HTML5 API flags
413 settings.setAppCacheEnabled(true);
414 settings.setDatabaseEnabled(true);
415 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700416
417 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700418 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700419 settings.setAppCachePath(getAppCachePath());
420 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
421 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700422 // origin policy for file access
423 settings.setAllowUniversalAccessFromFileURLs(false);
424 settings.setAllowFileAccessFromFileURLs(false);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800425
426 if (!(settings instanceof WebSettingsClassic)) return;
427
428 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
429 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
430 // WebView should be preserving the memory as much as possible.
431 // However, apps like browser wish to turn on the performance mode which
432 // would require more memory.
433 // TODO: We need to dynamically allocate/deallocate temporary memory for
434 // apps which are trying to use minimal memory. Currently, double
435 // buffering is always turned on, which is unnecessary.
436 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
437 settingsClassic.setWorkersEnabled(true); // This only affects V8.
John Reck35e9dd62011-04-25 09:01:54 -0700438 }
439
440 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700441 mNeedsSharedSync = false;
John Reck35e9dd62011-04-25 09:01:54 -0700442 CookieManager.getInstance().setAcceptCookie(acceptCookies());
443 if (mController != null) {
444 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700445 }
John Reck35e9dd62011-04-25 09:01:54 -0700446 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700447
John Reck35e9dd62011-04-25 09:01:54 -0700448 private void syncManagedSettings() {
449 syncSharedSettings();
450 synchronized (mManagedSettings) {
451 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
452 while (iter.hasNext()) {
453 WeakReference<WebSettings> ref = iter.next();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800454 WebSettings settings = ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700455 if (settings == null) {
456 iter.remove();
457 continue;
458 }
459 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000460 }
John Reck35e9dd62011-04-25 09:01:54 -0700461 }
462 }
Ben Murdochef671652010-11-25 17:17:58 +0000463
John Reck35e9dd62011-04-25 09:01:54 -0700464 @Override
465 public void onSharedPreferenceChanged(
466 SharedPreferences sharedPreferences, String key) {
467 syncManagedSettings();
468 if (PREF_SEARCH_ENGINE.equals(key)) {
469 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700470 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200471 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700472 mController.getUi().setFullscreen(useFullscreen());
473 }
Michael Kolb0241e752011-07-07 14:58:50 -0700474 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200475 if (mController != null && mController.getUi() != null) {
Michael Kolb0241e752011-07-07 14:58:50 -0700476 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
477 }
Victoria Lease96497832012-03-23 14:19:56 -0700478 } else if (PREF_LINK_PREFETCH.equals(key)) {
479 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700480 }
John Reck35e9dd62011-04-25 09:01:54 -0700481 }
482
John Reck961d35d2011-06-23 09:45:54 -0700483 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100484 requireInitialization();
485 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700486 }
487
488 public LayoutAlgorithm getLayoutAlgorithm() {
489 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
490 if (autofitPages()) {
491 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
492 }
493 if (isDebugEnabled()) {
494 if (isSmallScreen()) {
495 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000496 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700497 if (isNormalLayout()) {
498 layoutAlgorithm = LayoutAlgorithm.NORMAL;
499 } else {
500 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
501 }
Ben Murdochef671652010-11-25 17:17:58 +0000502 }
John Reck35e9dd62011-04-25 09:01:54 -0700503 }
504 return layoutAlgorithm;
505 }
Ben Murdochef671652010-11-25 17:17:58 +0000506
John Reck35e9dd62011-04-25 09:01:54 -0700507 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700508 requireInitialization();
509 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800510 }
511
John Reck35e9dd62011-04-25 09:01:54 -0700512 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700513 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700514 return mWebStorageSizeManager;
515 }
516
John Reck35e9dd62011-04-25 09:01:54 -0700517 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700518 if (mAppCachePath == null) {
519 mAppCachePath = mContext.getDir("appcache", 0).getPath();
520 }
521 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700522 }
523
524 private void updateSearchEngine(boolean force) {
525 String searchEngineName = getSearchEngineName();
526 if (force || mSearchEngine == null ||
527 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700528 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000529 }
530 }
531
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100532 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700533 if (mSearchEngine == null) {
534 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100535 }
John Reck35e9dd62011-04-25 09:01:54 -0700536 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100537 }
538
John Reck35e9dd62011-04-25 09:01:54 -0700539 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700540 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700541 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100542 }
543
John Reck35e9dd62011-04-25 09:01:54 -0700544 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700545 Editor edit = mPrefs.edit();
546 edit.putBoolean(PREF_DEBUG_MENU, value);
547 if (!value) {
548 // Reset to "safe" value
549 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
550 }
551 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100552 }
553
John Reck35e9dd62011-04-25 09:01:54 -0700554 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800555 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700556 if (mController != null) {
557 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800558 if (current != null) {
559 current.clearCache(true);
560 }
561 }
562 }
563
John Reck35e9dd62011-04-25 09:01:54 -0700564 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800565 CookieManager.getInstance().removeAllCookie();
566 }
567
John Reck35e9dd62011-04-25 09:01:54 -0700568 public void clearHistory() {
569 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800570 Browser.clearHistory(resolver);
571 Browser.clearSearches(resolver);
572 }
573
John Reck35e9dd62011-04-25 09:01:54 -0700574 public void clearFormData() {
575 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700576 if (mController!= null) {
577 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100578 if (currentTopView != null) {
579 currentTopView.clearFormData();
580 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800581 }
582 }
583
John Reck35e9dd62011-04-25 09:01:54 -0700584 public void clearPasswords() {
585 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800586 db.clearUsernamePassword();
587 db.clearHttpAuthUsernamePassword();
588 }
589
John Reck35e9dd62011-04-25 09:01:54 -0700590 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100591 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100592 }
593
John Reck35e9dd62011-04-25 09:01:54 -0700594 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100595 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100596 }
597
John Reck35e9dd62011-04-25 09:01:54 -0700598 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700599 // Preserve autologin setting
600 long gal = mPrefs.getLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, -1);
601 mPrefs.edit()
602 .clear()
603 .putLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, gal)
604 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200605 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700606 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700607 }
608
Björn Isakssonc885a242012-06-05 17:19:04 +0200609 private void resetCachedValues() {
610 updateSearchEngine(false);
611 }
612
John Reck35e9dd62011-04-25 09:01:54 -0700613 public AutoFillProfile getAutoFillProfile() {
John Reck35e9dd62011-04-25 09:01:54 -0700614 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800615 }
616
John Reck35e9dd62011-04-25 09:01:54 -0700617 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
John Reck35e9dd62011-04-25 09:01:54 -0700618 mAutofillHandler.setAutoFillProfile(profile, msg);
Ben Murdoch273330a2011-07-08 18:37:22 +0100619 // Auto-fill will reuse the same profile ID when making edits to the profile,
620 // so we need to force a settings sync (otherwise the SharedPreferences
621 // manager will optimise out the call to onSharedPreferenceChanged(), as
622 // it thinks nothing has changed).
623 syncManagedSettings();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800624 }
625
John Reck35e9dd62011-04-25 09:01:54 -0700626 public void toggleDebugSettings() {
627 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800628 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100629
John Reckb8b2af82011-05-20 15:58:33 -0700630 public boolean hasDesktopUseragent(WebView view) {
631 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
632 }
633
634 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700635 if (view == null) {
636 return;
637 }
John Reckb8b2af82011-05-20 15:58:33 -0700638 WebSettings settings = view.getSettings();
639 if (mCustomUserAgents.get(settings) != null) {
640 mCustomUserAgents.remove(settings);
641 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
642 } else {
643 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
644 settings.setUserAgentString(DESKTOP_USERAGENT);
645 }
646 }
647
John Reck7dc444b2011-06-16 17:44:29 -0700648 public static int getAdjustedMinimumFontSize(int rawValue) {
649 rawValue++; // Preference starts at 0, min font at 1
650 if (rawValue > 1) {
651 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
652 }
653 return rawValue;
654 }
655
John Reck5ba3c762011-09-14 15:00:15 -0700656 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700657 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700658 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700659 }
660
661 static int getRawTextZoom(int percent) {
662 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
663 }
664
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700665 public int getAdjustedDoubleTapZoom(int rawValue) {
666 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
667 return (int) ((rawValue + 100) * mFontSizeMult);
668 }
669
670 static int getRawDoubleTapZoom(int percent) {
671 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
672 }
673
John Reckcadae722011-07-25 11:36:17 -0700674 public SharedPreferences getPreferences() {
675 return mPrefs;
676 }
677
Victoria Lease96497832012-03-23 14:19:56 -0700678 // update connectivity-dependent options
679 public void updateConnectionType() {
680 ConnectivityManager cm = (ConnectivityManager)
681 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
682 String linkPrefetchPreference = getLinkPrefetchEnabled();
683 boolean linkPrefetchAllowed = linkPrefetchPreference.
684 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
685 NetworkInfo ni = cm.getActiveNetworkInfo();
686 if (ni != null) {
687 switch (ni.getType()) {
688 case ConnectivityManager.TYPE_WIFI:
689 case ConnectivityManager.TYPE_ETHERNET:
690 case ConnectivityManager.TYPE_BLUETOOTH:
691 linkPrefetchAllowed |= linkPrefetchPreference.
692 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
693 break;
694 case ConnectivityManager.TYPE_MOBILE:
695 case ConnectivityManager.TYPE_MOBILE_DUN:
696 case ConnectivityManager.TYPE_MOBILE_MMS:
697 case ConnectivityManager.TYPE_MOBILE_SUPL:
698 case ConnectivityManager.TYPE_WIMAX:
699 default:
700 break;
701 }
702 }
703 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
704 mLinkPrefetchAllowed = linkPrefetchAllowed;
705 syncManagedSettings();
706 }
707 }
708
John Reck35e9dd62011-04-25 09:01:54 -0700709 // -----------------------------
710 // getter/setters for accessibility_preferences.xml
711 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100712
John Reck7dc444b2011-06-16 17:44:29 -0700713 @Deprecated
714 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700715 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
716 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100717 }
718
John Reck35e9dd62011-04-25 09:01:54 -0700719 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700720 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700721 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100722 }
723
John Reck92f25f82011-04-26 16:57:10 -0700724 public boolean forceEnableUserScalable() {
725 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
726 }
727
John Reck7dc444b2011-06-16 17:44:29 -0700728 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700729 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700730 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
731 return getAdjustedTextZoom(textZoom);
732 }
733
734 public void setTextZoom(int percent) {
735 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
736 }
737
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700738 public int getDoubleTapZoom() {
739 requireInitialization();
740 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
741 return getAdjustedDoubleTapZoom(doubleTapZoom);
742 }
743
744 public void setDoubleTapZoom(int percent) {
745 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
746 }
747
John Reck35e9dd62011-04-25 09:01:54 -0700748 // -----------------------------
749 // getter/setters for advanced_preferences.xml
750 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100751
John Reck35e9dd62011-04-25 09:01:54 -0700752 public String getSearchEngineName() {
753 return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100754 }
John Reck63bb6872010-12-01 19:29:32 -0800755
Michael Kolb8d772b02012-01-19 13:56:00 -0800756 public boolean allowAppTabs() {
757 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
758 }
759
John Reck35e9dd62011-04-25 09:01:54 -0700760 public boolean openInBackground() {
761 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800762 }
John Reck35e9dd62011-04-25 09:01:54 -0700763
764 public boolean enableJavascript() {
765 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
766 }
767
768 // TODO: Cache
769 public PluginState getPluginState() {
770 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
771 return PluginState.valueOf(state);
772 }
773
774 // TODO: Cache
775 public ZoomDensity getDefaultZoom() {
776 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
777 return ZoomDensity.valueOf(zoom);
778 }
779
780 public boolean loadPageInOverviewMode() {
781 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
782 }
783
784 public boolean autofitPages() {
785 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
786 }
787
788 public boolean blockPopupWindows() {
789 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
790 }
791
792 public boolean loadImages() {
793 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
794 }
795
796 public String getDefaultTextEncoding() {
797 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
798 }
799
800 // -----------------------------
801 // getter/setters for general_preferences.xml
802 // -----------------------------
803
804 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700805 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
806 }
807
808 public void setHomePage(String value) {
809 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
810 }
811
812 public boolean isAutofillEnabled() {
813 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
814 }
815
816 public void setAutofillEnabled(boolean value) {
817 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
818 }
819
820 // -----------------------------
821 // getter/setters for debug_preferences.xml
822 // -----------------------------
823
824 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700825 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700826 return true;
827 }
828 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
829 }
830
Derek Sollenberger31adf672011-07-08 11:31:30 -0400831 public boolean isSkiaHardwareAccelerated() {
832 if (!isDebugEnabled()) {
833 return false;
834 }
835 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
836 }
837
John Reck35e9dd62011-04-25 09:01:54 -0700838 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700839 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700840 return 0;
841 }
842 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
843 }
844
845 // -----------------------------
846 // getter/setters for hidden_debug_preferences.xml
847 // -----------------------------
848
849 public boolean enableVisualIndicator() {
850 if (!isDebugEnabled()) {
851 return false;
852 }
853 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
854 }
855
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700856 public boolean enableCpuUploadPath() {
857 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700858 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700859 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700860 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700861 }
862
John Reck35e9dd62011-04-25 09:01:54 -0700863 public boolean enableJavascriptConsole() {
864 if (!isDebugEnabled()) {
865 return false;
866 }
867 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
868 }
869
870 public boolean isSmallScreen() {
871 if (!isDebugEnabled()) {
872 return false;
873 }
874 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
875 }
876
877 public boolean isWideViewport() {
878 if (!isDebugEnabled()) {
879 return true;
880 }
881 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
882 }
883
884 public boolean isNormalLayout() {
885 if (!isDebugEnabled()) {
886 return false;
887 }
888 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
889 }
890
891 public boolean isTracing() {
892 if (!isDebugEnabled()) {
893 return false;
894 }
895 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
896 }
897
898 public boolean enableLightTouch() {
899 if (!isDebugEnabled()) {
900 return false;
901 }
902 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
903 }
904
905 public boolean enableNavDump() {
906 if (!isDebugEnabled()) {
907 return false;
908 }
909 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
910 }
911
912 public String getJsEngineFlags() {
913 if (!isDebugEnabled()) {
914 return "";
915 }
916 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
917 }
918
919 // -----------------------------
920 // getter/setters for lab_preferences.xml
921 // -----------------------------
922
923 public boolean useQuickControls() {
924 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
925 }
926
927 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700928 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700929 }
930
Michael Kolbc38c6042011-04-27 10:46:06 -0700931 public boolean useFullscreen() {
932 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
933 }
934
John Reck2fd9d0e2011-07-15 11:13:48 -0700935 public boolean useInvertedRendering() {
936 return mPrefs.getBoolean(PREF_INVERTED, false);
937 }
938
Nicolas Roard5d513102011-08-03 15:35:34 -0700939 public float getInvertedContrast() {
940 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
941 }
942
John Reck35e9dd62011-04-25 09:01:54 -0700943 // -----------------------------
944 // getter/setters for privacy_security_preferences.xml
945 // -----------------------------
946
947 public boolean showSecurityWarnings() {
948 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
949 }
950
951 public boolean acceptCookies() {
952 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
953 }
954
955 public boolean saveFormdata() {
956 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
957 }
958
959 public boolean enableGeolocation() {
960 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
961 }
962
963 public boolean rememberPasswords() {
964 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
965 }
966
Michael Kolb14612442011-06-24 13:06:29 -0700967 // -----------------------------
968 // getter/setters for bandwidth_preferences.xml
969 // -----------------------------
970
Mathew Inwood467813f2011-09-02 15:43:17 +0100971 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
972 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -0700973 }
Mathew Inwood467813f2011-09-02 15:43:17 +0100974
975 public static String getPreloadAlwaysPreferenceString(Context context) {
976 return context.getResources().getString(R.string.pref_data_preload_value_always);
977 }
978
Mathew Inwood825fba72011-10-04 14:52:52 +0100979 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
980 "browser_default_preload_setting";
981
982 public String getDefaultPreloadSetting() {
983 String preload = Settings.Secure.getString(mContext.getContentResolver(),
984 DEAULT_PRELOAD_SECURE_SETTING_KEY);
985 if (preload == null) {
986 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
987 }
988 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +0100989 }
990
991 public String getPreloadEnabled() {
992 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
993 }
994
Victoria Lease96497832012-03-23 14:19:56 -0700995 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
996 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
997 }
998
999 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1000 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1001 }
1002
1003 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1004 "browser_default_link_prefetch_setting";
1005
1006 public String getDefaultLinkPrefetchSetting() {
1007 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1008 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1009 if (preload == null) {
1010 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1011 }
1012 return preload;
1013 }
1014
1015 public String getLinkPrefetchEnabled() {
1016 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1017 }
1018
George Mount3636d0a2011-11-21 09:08:21 -08001019 // -----------------------------
1020 // getter/setters for browser recovery
1021 // -----------------------------
1022 /**
1023 * The last time browser was started.
1024 * @return The last browser start time as System.currentTimeMillis. This
1025 * can be 0 if this is the first time or the last tab was closed.
1026 */
1027 public long getLastRecovered() {
1028 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1029 }
1030
1031 /**
1032 * Sets the last browser start time.
1033 * @param time The last time as System.currentTimeMillis that the browser
1034 * was started. This should be set to 0 if the last tab is closed.
1035 */
1036 public void setLastRecovered(long time) {
1037 mPrefs.edit()
1038 .putLong(KEY_LAST_RECOVERED, time)
1039 .apply();
1040 }
1041
1042 /**
1043 * Used to determine whether or not the previous browser run crashed. Once
1044 * the previous state has been determined, the value will be set to false
1045 * until a pause is received.
1046 * @return true if the last browser run was paused or false if it crashed.
1047 */
1048 public boolean wasLastRunPaused() {
1049 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1050 }
1051
1052 /**
1053 * Sets whether or not the last run was a pause or crash.
1054 * @param isPaused Set to true When a pause is received or false after
1055 * resuming.
1056 */
1057 public void setLastRunPaused(boolean isPaused) {
1058 mPrefs.edit()
1059 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1060 .apply();
1061 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001062}