blob: 90dcc4f2efe31fbfde2024ac7d8e71e9b83d0640 [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080018
The Android Open Source Project0c908882009-03-03 19:32:16 -080019import android.content.ContentResolver;
20import android.content.Context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.content.SharedPreferences;
John Reck1da81882011-10-04 17:21:10 -070022import android.content.SharedPreferences.Editor;
John Reck812d2d62011-01-18 14:16:15 -080023import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
qqzhou8c5b0a32013-07-22 15:31:03 +080024import android.content.res.AssetManager;
Victoria Lease96497832012-03-23 14:19:56 -070025import android.net.ConnectivityManager;
26import android.net.NetworkInfo;
John Reckf48314f2011-04-27 17:52:17 -070027import android.os.Build;
Ben Murdoch0cb81892010-10-08 12:41:33 +010028import android.preference.PreferenceManager;
Ben Murdoch0cb81892010-10-08 12:41:33 +010029import android.provider.Browser;
Mathew Inwood825fba72011-10-04 14:52:52 +010030import android.provider.Settings;
John Reck5ba3c762011-09-14 15:00:15 -070031import android.util.DisplayMetrics;
qqzhou8c5b0a32013-07-22 15:31:03 +080032import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.webkit.WebIconDatabase;
Nicolas Roard78a98e42009-05-11 13:34:17 +010034import android.webkit.WebStorage;
John Reck812d2d62011-01-18 14:16:15 -080035import android.webkit.WebViewDatabase;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036
Bijan Amirzada41242f22014-03-21 12:12:18 -070037import com.android.browser.R;
38import com.android.browser.homepages.HomeProvider;
39import com.android.browser.provider.BrowserProvider;
40import com.android.browser.reflect.ReflectHelper;
41import com.android.browser.search.SearchEngine;
42import com.android.browser.search.SearchEngines;
John Reck46500332011-06-07 14:36:10 -070043
qqzhou8c5b0a32013-07-22 15:31:03 +080044import java.io.InputStream;
John Reck35e9dd62011-04-25 09:01:54 -070045import java.lang.ref.WeakReference;
qqzhoue6ff8b42013-07-23 17:28:48 +080046import java.lang.reflect.Method;
John Reck35e9dd62011-04-25 09:01:54 -070047import java.util.Iterator;
48import java.util.LinkedList;
qqzhou8c5b0a32013-07-22 15:31:03 +080049import java.util.Locale;
John Reckb8b2af82011-05-20 15:58:33 -070050import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080052import org.codeaurora.swe.AutoFillProfile;
53import org.codeaurora.swe.CookieManager;
54import org.codeaurora.swe.GeolocationPermissions;
55import org.codeaurora.swe.WebSettings.LayoutAlgorithm;
56import org.codeaurora.swe.WebSettings.PluginState;
57import org.codeaurora.swe.WebSettings.TextSize;
58import org.codeaurora.swe.WebSettings.ZoomDensity;
59import org.codeaurora.swe.WebSettings;
60import org.codeaurora.swe.WebView;
61
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
qqzhoue6ff8b42013-07-23 17:28:48 +080097 private static final String TAG = "BrowserSettings";
John Reck8fc22a12011-06-16 14:57:41 -070098 // The minimum min font size
99 // Aka, the lower bounds for the min font size range
100 // which is 1:5..24
101 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -0700102 // The initial value in the text zoom range
103 // This is what represents 100% in the SeekBarPreference range
104 private static final int TEXT_ZOOM_START_VAL = 10;
105 // The size of a single step in the text zoom range, in percent
106 private static final int TEXT_ZOOM_STEP = 5;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700107 // The initial value in the double tap zoom range
108 // This is what represents 100% in the SeekBarPreference range
109 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
110 // The size of a single step in the double tap zoom range, in percent
111 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -0700112
John Reck35e9dd62011-04-25 09:01:54 -0700113 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -0700114
John Reck35e9dd62011-04-25 09:01:54 -0700115 private Context mContext;
116 private SharedPreferences mPrefs;
117 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700118 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700119 private WebStorageSizeManager mWebStorageSizeManager;
120 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700121 private WeakHashMap<WebSettings, String> mCustomUserAgents;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100122 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -0700123 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -0700124 private float mFontSizeMult = 1.0f;
John Reck78a6a1d2011-07-21 13:54:03 -0700125
Victoria Lease96497832012-03-23 14:19:56 -0700126 // Current state of network-dependent settings
127 private boolean mLinkPrefetchAllowed = true;
128
John Reck78a6a1d2011-07-21 13:54:03 -0700129 // Cached values
130 private int mPageCacheCapacity = 1;
131 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800132
John Reck35e9dd62011-04-25 09:01:54 -0700133 // Cached settings
134 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800135
Ben Murdochaaa1f372011-07-25 15:40:58 +0100136 private static String sFactoryResetUrl;
137
qqzhou8c5b0a32013-07-22 15:31:03 +0800138 // add for carrier feature
139 private static Context sResPackageCtx;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800140 private android.os.CountDownTimer mCountDownTimer;
141
142 //Determine if WebView is Initialized or not
143 private boolean mWebViewInitialized;
qqzhou8c5b0a32013-07-22 15:31:03 +0800144
Bijan Amirzada41242f22014-03-21 12:12:18 -0700145 public static void initialize(final Context context) {
John Reck35e9dd62011-04-25 09:01:54 -0700146 sInstance = new BrowserSettings(context);
147 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800148
Bijan Amirzada41242f22014-03-21 12:12:18 -0700149 public static BrowserSettings getInstance() {
John Reck35e9dd62011-04-25 09:01:54 -0700150 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>();
qqzhou8c5b0a32013-07-22 15:31:03 +0800159
160 // add for carrier feature
161 try {
162 sResPackageCtx = context.createPackageContext(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700163 "com.android.browser.res",
qqzhou8c5b0a32013-07-22 15:31:03 +0800164 Context.CONTEXT_IGNORE_SECURITY);
165 } catch (Exception e) {
166 Log.e("Res_Update", "Create Res Apk Failed");
167 }
John Reckcadae722011-07-25 11:36:17 -0700168 BackgroundHandler.execute(mSetup);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800169 mWebViewInitialized = false;
John Reck35e9dd62011-04-25 09:01:54 -0700170 }
171
172 public void setController(Controller controller) {
173 mController = controller;
John Reckc477e712011-08-17 11:27:15 -0700174 if (sInitialized) {
175 syncSharedSettings();
176 }
Ben Murdochef671652010-11-25 17:17:58 +0000177 }
178
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800179 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800180
John Reckc477e712011-08-17 11:27:15 -0700181 if (mNeedsSharedSync) {
182 syncSharedSettings();
183 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800184
John Reck35e9dd62011-04-25 09:01:54 -0700185 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800186 syncStaticSettings(settings);
187 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700188 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800189 }
190 }
191
John Reckd1d87312012-03-08 13:25:00 -0800192 public void stopManagingSettings(WebSettings settings) {
193 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
194 while (iter.hasNext()) {
195 WeakReference<WebSettings> ref = iter.next();
196 if (ref.get() == settings) {
197 iter.remove();
198 return;
199 }
200 }
201 }
202
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800203 public void initializeCookieSettings() {
204 CookieManager.getInstance().setAcceptCookie(acceptCookies());
205 mWebViewInitialized = true;
206 }
John Reckcadae722011-07-25 11:36:17 -0700207 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700208
209 @Override
210 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700211 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
212 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700213 // the cost of one cached page is ~3M (measured using nytimes.com). For
214 // low end devices, we only cache one page. For high end devices, we try
215 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800216
217 // SWE_TODO : assume a high-memory device
218 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700219 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800220 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700221 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
222 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
223 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700224 // Workaround b/5254577
225 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700226 if (Build.VERSION.CODENAME.equals("REL")) {
227 // This is a release build, always startup with debug disabled
228 setDebugEnabled(false);
229 }
230 if (mPrefs.contains(PREF_TEXT_SIZE)) {
231 /*
232 * Update from TextSize enum to zoom percent
233 * SMALLEST is 50%
234 * SMALLER is 75%
235 * NORMAL is 100%
236 * LARGER is 150%
237 * LARGEST is 200%
238 */
239 switch (getTextSize()) {
240 case SMALLEST:
241 setTextZoom(50);
242 break;
243 case SMALLER:
244 setTextZoom(75);
245 break;
246 case LARGER:
247 setTextZoom(150);
248 break;
249 case LARGEST:
250 setTextZoom(200);
251 break;
252 }
253 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
254 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100255
qqzhou8c5b0a32013-07-22 15:31:03 +0800256 // add for carrier homepage feature
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800257 Object[] params = { new String("persist.env.c.browser.resource"),
258 new String("default")};
259 Class[] type = new Class[] {String.class, String.class};
260 String browserRes = (String)ReflectHelper.invokeStaticMethod(
261 "android.os.SystemProperties","get",type, params);
kaiyiz08f9e2b2013-08-16 09:15:48 +0800262 if ("cu".equals(browserRes) || "cmcc".equals(browserRes)) {
qqzhou8c5b0a32013-07-22 15:31:03 +0800263 int resID = sResPackageCtx.getResources().getIdentifier(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700264 "homepage_base", "string", "com.android.browser.res");
qqzhou8c5b0a32013-07-22 15:31:03 +0800265 sFactoryResetUrl = sResPackageCtx.getResources().getString(resID);
266 } else if ("ct".equals(browserRes)) {
267 int resID = sResPackageCtx.getResources().getIdentifier(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700268 "homepage_base", "string", "com.android.browser.res");
qqzhou8c5b0a32013-07-22 15:31:03 +0800269 sFactoryResetUrl = sResPackageCtx.getResources().getString(resID);
270
271 int pathID = sResPackageCtx.getResources().getIdentifier(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700272 "homepage_path", "string", "com.android.browser.res");
qqzhou8c5b0a32013-07-22 15:31:03 +0800273 String path = sResPackageCtx.getResources().getString(pathID);
274 Locale locale = Locale.getDefault();
275 path = path.replace("%y", locale.getLanguage().toLowerCase());
276 path = path.replace("%z", '_'+locale.getCountry().toLowerCase());
277 boolean useCountry = true;
278 boolean useLanguage = true;
279 InputStream is = null;
280 AssetManager am = mContext.getAssets();
281 try {
282 is = am.open(path);
283 } catch (Exception ignored) {
284 useCountry = false;
285 path = sResPackageCtx.getResources().getString(pathID);
286 path = path.replace("%y", locale.getLanguage().toLowerCase());
287 path = path.replace("%z", "");
288 try {
289 is = am.open(path);
290 } catch (Exception ignoredlanguage) {
291 useLanguage = false;
292 }
293 } finally {
294 if (is != null) {
295 try {
296 is.close();
297 } catch (Exception ignored) {}
298 }
299 }
300
301 if (!useCountry && !useLanguage) {
302 sFactoryResetUrl = sFactoryResetUrl.replace("%y%z", "en");
303 } else {
304 sFactoryResetUrl = sFactoryResetUrl.replace("%y",
305 locale.getLanguage().toLowerCase());
306 sFactoryResetUrl = sFactoryResetUrl.replace("%z", useCountry ?
307 '_' + locale.getCountry().toLowerCase() : "");
308 }
309 } else {
310 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
311 }
312
kaiyizbf086ea2013-08-02 11:03:58 +0800313 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
314 if (!"default".equals(browserRes)) {
315 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING,
316 "GBK").apply();
317 }
318 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100319 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
320 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
321 BrowserProvider.getClientId(mContext.getContentResolver()));
322 }
323
Ben Murdochaaa1f372011-07-25 15:40:58 +0100324 synchronized (BrowserSettings.class) {
325 sInitialized = true;
326 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700327 }
328 }
329 };
330
Ben Murdochaaa1f372011-07-25 15:40:58 +0100331 private static void requireInitialization() {
332 synchronized (BrowserSettings.class) {
333 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700334 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100335 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700336 } catch (InterruptedException e) {
337 }
338 }
339 }
340 }
341
The Android Open Source Project0c908882009-03-03 19:32:16 -0800342 /**
John Reck35e9dd62011-04-25 09:01:54 -0700343 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800344 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800345 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700346 settings.setGeolocationEnabled(enableGeolocation());
347 settings.setJavaScriptEnabled(enableJavascript());
348 settings.setLightTouchEnabled(enableLightTouch());
349 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700350 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
351 settings.setDefaultZoom(getDefaultZoom());
352 settings.setMinimumFontSize(getMinimumFontSize());
353 settings.setMinimumLogicalFontSize(getMinimumFontSize());
354 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700355 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700356 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100357 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700358 settings.setLoadsImagesAutomatically(loadImages());
359 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
360 settings.setSavePassword(rememberPasswords());
361 settings.setSaveFormData(saveFormdata());
362 settings.setUseWideViewPort(isWideViewport());
John Reckb8b2af82011-05-20 15:58:33 -0700363
qqzhoue6ff8b42013-07-23 17:28:48 +0800364 // add for carrier useragent feature
365 String ua = null;
366 try {
367 Class c = Class.forName("com.qrd.useragent.UserAgentHandler");
368 Object cObj = c.newInstance();
369 Method m = c.getDeclaredMethod("getUAString", Context.class);
370 ua = (String)m.invoke(cObj, mContext);
371 } catch (Exception e) {
372 Log.e(TAG, "plug in Load failed, err " + e);
373 ua = mCustomUserAgents.get(settings);
374 }
John Reck46500332011-06-07 14:36:10 -0700375 if (ua != null) {
John Reckb8b2af82011-05-20 15:58:33 -0700376 settings.setUserAgentString(ua);
377 } else {
378 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
379 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700380
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800381 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800382 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
383 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
384 settingsClassic.setForceUserScalable(forceEnableUserScalable());
385 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
386 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800387
John Reckea17e782011-10-27 17:15:59 -0700388 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800389 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700390 useInverted ? "true" : "false");
391 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800392 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700393 Float.toString(getInvertedContrast()));
394 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700395
John Reckea17e782011-10-27 17:15:59 -0700396 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800397 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700398 enableCpuUploadPath() ? "true" : "false");
399 }
Victoria Lease96497832012-03-23 14:19:56 -0700400
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800401 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000402 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800403
John Reck35e9dd62011-04-25 09:01:54 -0700404 /**
405 * Syncs all the settings that have no UI
406 * These cannot change, so we only need to set them once per WebSettings
407 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800408 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700409 settings.setDefaultFontSize(16);
410 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000411
John Reck35e9dd62011-04-25 09:01:54 -0700412 // WebView inside Browser doesn't want initial focus to be set.
413 settings.setNeedInitialFocus(false);
414 // Browser supports multiple windows
415 settings.setSupportMultipleWindows(true);
416 // enable smooth transition for better performance during panning or
417 // zooming
418 settings.setEnableSmoothTransition(true);
419 // disable content url access
420 settings.setAllowContentAccess(false);
421
422 // HTML5 API flags
423 settings.setAppCacheEnabled(true);
424 settings.setDatabaseEnabled(true);
425 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700426
427 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700428 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700429 settings.setAppCachePath(getAppCachePath());
430 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
431 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700432 // origin policy for file access
433 settings.setAllowUniversalAccessFromFileURLs(false);
434 settings.setAllowFileAccessFromFileURLs(false);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800435
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800436 //if (!(settings instanceof WebSettingsClassic)) return;
437 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800438
439 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
440 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
441 // WebView should be preserving the memory as much as possible.
442 // However, apps like browser wish to turn on the performance mode which
443 // would require more memory.
444 // TODO: We need to dynamically allocate/deallocate temporary memory for
445 // apps which are trying to use minimal memory. Currently, double
446 // buffering is always turned on, which is unnecessary.
447 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
448 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800449 */
John Reck35e9dd62011-04-25 09:01:54 -0700450 }
451
452 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700453 mNeedsSharedSync = false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800454 if (mWebViewInitialized) {
455 CookieManager.getInstance().setAcceptCookie(acceptCookies());
456 }
John Reck35e9dd62011-04-25 09:01:54 -0700457 if (mController != null) {
458 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700459 }
John Reck35e9dd62011-04-25 09:01:54 -0700460 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700461
John Reck35e9dd62011-04-25 09:01:54 -0700462 private void syncManagedSettings() {
463 syncSharedSettings();
464 synchronized (mManagedSettings) {
465 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
466 while (iter.hasNext()) {
467 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800468 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700469 if (settings == null) {
470 iter.remove();
471 continue;
472 }
473 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000474 }
John Reck35e9dd62011-04-25 09:01:54 -0700475 }
476 }
Ben Murdochef671652010-11-25 17:17:58 +0000477
John Reck35e9dd62011-04-25 09:01:54 -0700478 @Override
479 public void onSharedPreferenceChanged(
480 SharedPreferences sharedPreferences, String key) {
481 syncManagedSettings();
482 if (PREF_SEARCH_ENGINE.equals(key)) {
483 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700484 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200485 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700486 mController.getUi().setFullscreen(useFullscreen());
487 }
Michael Kolb0241e752011-07-07 14:58:50 -0700488 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200489 if (mController != null && mController.getUi() != null) {
Michael Kolb0241e752011-07-07 14:58:50 -0700490 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
491 }
Victoria Lease96497832012-03-23 14:19:56 -0700492 } else if (PREF_LINK_PREFETCH.equals(key)) {
493 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700494 }
John Reck35e9dd62011-04-25 09:01:54 -0700495 }
496
John Reck961d35d2011-06-23 09:45:54 -0700497 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100498 requireInitialization();
499 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700500 }
501
502 public LayoutAlgorithm getLayoutAlgorithm() {
503 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
504 if (autofitPages()) {
505 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
506 }
507 if (isDebugEnabled()) {
508 if (isSmallScreen()) {
509 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000510 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700511 if (isNormalLayout()) {
512 layoutAlgorithm = LayoutAlgorithm.NORMAL;
513 } else {
514 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
515 }
Ben Murdochef671652010-11-25 17:17:58 +0000516 }
John Reck35e9dd62011-04-25 09:01:54 -0700517 }
518 return layoutAlgorithm;
519 }
Ben Murdochef671652010-11-25 17:17:58 +0000520
John Reck35e9dd62011-04-25 09:01:54 -0700521 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700522 requireInitialization();
523 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800524 }
525
John Reck35e9dd62011-04-25 09:01:54 -0700526 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700527 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700528 return mWebStorageSizeManager;
529 }
530
John Reck35e9dd62011-04-25 09:01:54 -0700531 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700532 if (mAppCachePath == null) {
533 mAppCachePath = mContext.getDir("appcache", 0).getPath();
534 }
535 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700536 }
537
538 private void updateSearchEngine(boolean force) {
539 String searchEngineName = getSearchEngineName();
540 if (force || mSearchEngine == null ||
541 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700542 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000543 }
544 }
545
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100546 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700547 if (mSearchEngine == null) {
548 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100549 }
John Reck35e9dd62011-04-25 09:01:54 -0700550 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100551 }
552
John Reck35e9dd62011-04-25 09:01:54 -0700553 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700554 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700555 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100556 }
557
John Reck35e9dd62011-04-25 09:01:54 -0700558 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700559 Editor edit = mPrefs.edit();
560 edit.putBoolean(PREF_DEBUG_MENU, value);
561 if (!value) {
562 // Reset to "safe" value
563 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
564 }
565 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100566 }
567
John Reck35e9dd62011-04-25 09:01:54 -0700568 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800569 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700570 if (mController != null) {
571 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800572 if (current != null) {
573 current.clearCache(true);
574 }
575 }
576 }
577
John Reck35e9dd62011-04-25 09:01:54 -0700578 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800579 CookieManager.getInstance().removeAllCookie();
580 }
581
John Reck35e9dd62011-04-25 09:01:54 -0700582 public void clearHistory() {
583 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800584 Browser.clearHistory(resolver);
585 Browser.clearSearches(resolver);
586 }
587
John Reck35e9dd62011-04-25 09:01:54 -0700588 public void clearFormData() {
589 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700590 if (mController!= null) {
591 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100592 if (currentTopView != null) {
593 currentTopView.clearFormData();
594 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800595 }
596 }
597
John Reck35e9dd62011-04-25 09:01:54 -0700598 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700599 // Clear password store maintained by SWE engine
600 WebSettings settings = null;
601 // find a valid settings object
602 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
603 while (iter.hasNext()) {
604 WeakReference<WebSettings> ref = iter.next();
605 settings = (WebSettings)ref.get();
606 if (settings != null) {
607 break;
608 }
609 }
610 if (settings != null) {
611 settings.clearPasswords();
612 }
613
614 // Clear passwords in WebView database
John Reck35e9dd62011-04-25 09:01:54 -0700615 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800616 db.clearUsernamePassword();
617 db.clearHttpAuthUsernamePassword();
618 }
619
John Reck35e9dd62011-04-25 09:01:54 -0700620 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100621 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100622 }
623
John Reck35e9dd62011-04-25 09:01:54 -0700624 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100625 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100626 }
627
John Reck35e9dd62011-04-25 09:01:54 -0700628 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700629 // Preserve autologin setting
630 long gal = mPrefs.getLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, -1);
631 mPrefs.edit()
632 .clear()
633 .putLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, gal)
634 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200635 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700636 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700637 }
638
Björn Isakssonc885a242012-06-05 17:19:04 +0200639 private void resetCachedValues() {
640 updateSearchEngine(false);
641 }
642
John Reck35e9dd62011-04-25 09:01:54 -0700643 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800644 // query the profile from components autofill database 524
645 if (mAutofillHandler.mAutoFillProfile == null &&
646 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
647 WebSettings settings = null;
648 // find a valid settings object
649 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
650 while (iter.hasNext()) {
651 WeakReference<WebSettings> ref = iter.next();
652 settings = (WebSettings)ref.get();
653 if (settings != null) {
654 break;
655 }
656 }
657 if (settings != null) {
658 AutoFillProfile profile =
659 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
660 mAutofillHandler.setAutoFillProfile(profile);
661 }
662 }
John Reck35e9dd62011-04-25 09:01:54 -0700663 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800664 }
665
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800666 public String getAutoFillProfileId() {
667 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800668 }
669
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800670 public void updateAutoFillProfile(AutoFillProfile profile) {
671 syncAutoFillProfile(profile);
672 }
673
674 private void syncAutoFillProfile(AutoFillProfile profile) {
675 synchronized (mManagedSettings) {
676 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
677 while (iter.hasNext()) {
678 WeakReference<WebSettings> ref = iter.next();
679 WebSettings settings = (WebSettings)ref.get();
680 if (settings == null) {
681 iter.remove();
682 continue;
683 }
684 // update the profile only once.
685 settings.setAutoFillProfile(profile);
686 // Now we should have the guid
687 mAutofillHandler.setAutoFillProfile(profile);
688 break;
689 }
690 }
691 }
John Reck35e9dd62011-04-25 09:01:54 -0700692 public void toggleDebugSettings() {
693 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800694 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100695
John Reckb8b2af82011-05-20 15:58:33 -0700696 public boolean hasDesktopUseragent(WebView view) {
697 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
698 }
699
700 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700701 if (view == null) {
702 return;
703 }
John Reckb8b2af82011-05-20 15:58:33 -0700704 WebSettings settings = view.getSettings();
705 if (mCustomUserAgents.get(settings) != null) {
706 mCustomUserAgents.remove(settings);
707 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
708 } else {
709 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
710 settings.setUserAgentString(DESKTOP_USERAGENT);
711 }
712 }
713
John Reck7dc444b2011-06-16 17:44:29 -0700714 public static int getAdjustedMinimumFontSize(int rawValue) {
715 rawValue++; // Preference starts at 0, min font at 1
716 if (rawValue > 1) {
717 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
718 }
719 return rawValue;
720 }
721
John Reck5ba3c762011-09-14 15:00:15 -0700722 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700723 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700724 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700725 }
726
727 static int getRawTextZoom(int percent) {
728 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
729 }
730
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700731 public int getAdjustedDoubleTapZoom(int rawValue) {
732 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
733 return (int) ((rawValue + 100) * mFontSizeMult);
734 }
735
736 static int getRawDoubleTapZoom(int percent) {
737 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
738 }
739
John Reckcadae722011-07-25 11:36:17 -0700740 public SharedPreferences getPreferences() {
741 return mPrefs;
742 }
743
Victoria Lease96497832012-03-23 14:19:56 -0700744 // update connectivity-dependent options
745 public void updateConnectionType() {
746 ConnectivityManager cm = (ConnectivityManager)
747 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
748 String linkPrefetchPreference = getLinkPrefetchEnabled();
749 boolean linkPrefetchAllowed = linkPrefetchPreference.
750 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
751 NetworkInfo ni = cm.getActiveNetworkInfo();
752 if (ni != null) {
753 switch (ni.getType()) {
754 case ConnectivityManager.TYPE_WIFI:
755 case ConnectivityManager.TYPE_ETHERNET:
756 case ConnectivityManager.TYPE_BLUETOOTH:
757 linkPrefetchAllowed |= linkPrefetchPreference.
758 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
759 break;
760 case ConnectivityManager.TYPE_MOBILE:
761 case ConnectivityManager.TYPE_MOBILE_DUN:
762 case ConnectivityManager.TYPE_MOBILE_MMS:
763 case ConnectivityManager.TYPE_MOBILE_SUPL:
764 case ConnectivityManager.TYPE_WIMAX:
765 default:
766 break;
767 }
768 }
769 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
770 mLinkPrefetchAllowed = linkPrefetchAllowed;
771 syncManagedSettings();
772 }
773 }
774
luxiaol62677b02013-07-22 07:54:49 +0800775 public String getDownloadPath() {
776 return mPrefs.getString(PREF_DOWNLOAD_PATH,
777 DownloadHandler.getDefaultDownloadPath(mContext));
778 }
John Reck35e9dd62011-04-25 09:01:54 -0700779 // -----------------------------
780 // getter/setters for accessibility_preferences.xml
781 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100782
John Reck7dc444b2011-06-16 17:44:29 -0700783 @Deprecated
784 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700785 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
786 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100787 }
788
John Reck35e9dd62011-04-25 09:01:54 -0700789 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700790 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700791 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100792 }
793
John Reck92f25f82011-04-26 16:57:10 -0700794 public boolean forceEnableUserScalable() {
795 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
796 }
797
John Reck7dc444b2011-06-16 17:44:29 -0700798 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700799 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700800 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
801 return getAdjustedTextZoom(textZoom);
802 }
803
804 public void setTextZoom(int percent) {
805 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
806 }
807
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700808 public int getDoubleTapZoom() {
809 requireInitialization();
810 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
811 return getAdjustedDoubleTapZoom(doubleTapZoom);
812 }
813
814 public void setDoubleTapZoom(int percent) {
815 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
816 }
817
John Reck35e9dd62011-04-25 09:01:54 -0700818 // -----------------------------
819 // getter/setters for advanced_preferences.xml
820 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100821
John Reck35e9dd62011-04-25 09:01:54 -0700822 public String getSearchEngineName() {
luxiaol221b3932013-07-19 15:27:57 +0800823 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
824 if (defaultSearchEngineValue == null) {
825 defaultSearchEngineValue = SearchEngine.GOOGLE;
826 }
827 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100828 }
John Reck63bb6872010-12-01 19:29:32 -0800829
Michael Kolb8d772b02012-01-19 13:56:00 -0800830 public boolean allowAppTabs() {
831 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
832 }
833
John Reck35e9dd62011-04-25 09:01:54 -0700834 public boolean openInBackground() {
835 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800836 }
John Reck35e9dd62011-04-25 09:01:54 -0700837
838 public boolean enableJavascript() {
839 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
840 }
841
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800842 public boolean enableMemoryMonitor() {
843 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
844 }
845
John Reck35e9dd62011-04-25 09:01:54 -0700846 // TODO: Cache
847 public PluginState getPluginState() {
848 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
849 return PluginState.valueOf(state);
850 }
851
852 // TODO: Cache
853 public ZoomDensity getDefaultZoom() {
854 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
855 return ZoomDensity.valueOf(zoom);
856 }
857
858 public boolean loadPageInOverviewMode() {
859 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
860 }
861
862 public boolean autofitPages() {
863 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
864 }
865
866 public boolean blockPopupWindows() {
867 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
868 }
869
870 public boolean loadImages() {
871 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
872 }
873
874 public String getDefaultTextEncoding() {
875 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
876 }
877
878 // -----------------------------
879 // getter/setters for general_preferences.xml
880 // -----------------------------
881
882 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700883 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
884 }
885
886 public void setHomePage(String value) {
887 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
888 }
889
890 public boolean isAutofillEnabled() {
891 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
892 }
893
894 public void setAutofillEnabled(boolean value) {
895 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
896 }
897
898 // -----------------------------
899 // getter/setters for debug_preferences.xml
900 // -----------------------------
901
902 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700903 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700904 return true;
905 }
906 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
907 }
908
Derek Sollenberger31adf672011-07-08 11:31:30 -0400909 public boolean isSkiaHardwareAccelerated() {
910 if (!isDebugEnabled()) {
911 return false;
912 }
913 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
914 }
915
John Reck35e9dd62011-04-25 09:01:54 -0700916 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700917 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700918 return 0;
919 }
920 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
921 }
922
923 // -----------------------------
924 // getter/setters for hidden_debug_preferences.xml
925 // -----------------------------
926
927 public boolean enableVisualIndicator() {
928 if (!isDebugEnabled()) {
929 return false;
930 }
931 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
932 }
933
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700934 public boolean enableCpuUploadPath() {
935 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700936 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700937 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700938 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700939 }
940
John Reck35e9dd62011-04-25 09:01:54 -0700941 public boolean enableJavascriptConsole() {
942 if (!isDebugEnabled()) {
943 return false;
944 }
945 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
946 }
947
948 public boolean isSmallScreen() {
949 if (!isDebugEnabled()) {
950 return false;
951 }
952 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
953 }
954
955 public boolean isWideViewport() {
956 if (!isDebugEnabled()) {
957 return true;
958 }
959 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
960 }
961
962 public boolean isNormalLayout() {
963 if (!isDebugEnabled()) {
964 return false;
965 }
966 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
967 }
968
969 public boolean isTracing() {
970 if (!isDebugEnabled()) {
971 return false;
972 }
973 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
974 }
975
976 public boolean enableLightTouch() {
977 if (!isDebugEnabled()) {
978 return false;
979 }
980 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
981 }
982
983 public boolean enableNavDump() {
984 if (!isDebugEnabled()) {
985 return false;
986 }
987 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
988 }
989
990 public String getJsEngineFlags() {
991 if (!isDebugEnabled()) {
992 return "";
993 }
994 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
995 }
996
997 // -----------------------------
998 // getter/setters for lab_preferences.xml
999 // -----------------------------
1000
1001 public boolean useQuickControls() {
1002 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
1003 }
1004
1005 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -07001006 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -07001007 }
1008
Michael Kolbc38c6042011-04-27 10:46:06 -07001009 public boolean useFullscreen() {
1010 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
1011 }
1012
John Reck2fd9d0e2011-07-15 11:13:48 -07001013 public boolean useInvertedRendering() {
1014 return mPrefs.getBoolean(PREF_INVERTED, false);
1015 }
1016
Nicolas Roard5d513102011-08-03 15:35:34 -07001017 public float getInvertedContrast() {
1018 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
1019 }
1020
John Reck35e9dd62011-04-25 09:01:54 -07001021 // -----------------------------
1022 // getter/setters for privacy_security_preferences.xml
1023 // -----------------------------
1024
1025 public boolean showSecurityWarnings() {
1026 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
1027 }
1028
1029 public boolean acceptCookies() {
1030 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
1031 }
1032
1033 public boolean saveFormdata() {
1034 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
1035 }
1036
1037 public boolean enableGeolocation() {
1038 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
1039 }
1040
1041 public boolean rememberPasswords() {
1042 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
1043 }
1044
Michael Kolb14612442011-06-24 13:06:29 -07001045 // -----------------------------
1046 // getter/setters for bandwidth_preferences.xml
1047 // -----------------------------
1048
Mathew Inwood467813f2011-09-02 15:43:17 +01001049 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
1050 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -07001051 }
Mathew Inwood467813f2011-09-02 15:43:17 +01001052
1053 public static String getPreloadAlwaysPreferenceString(Context context) {
1054 return context.getResources().getString(R.string.pref_data_preload_value_always);
1055 }
1056
Mathew Inwood825fba72011-10-04 14:52:52 +01001057 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1058 "browser_default_preload_setting";
1059
1060 public String getDefaultPreloadSetting() {
1061 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1062 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1063 if (preload == null) {
1064 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1065 }
1066 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001067 }
1068
1069 public String getPreloadEnabled() {
1070 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1071 }
1072
Victoria Lease96497832012-03-23 14:19:56 -07001073 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1074 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1075 }
1076
1077 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1078 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1079 }
1080
1081 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1082 "browser_default_link_prefetch_setting";
1083
1084 public String getDefaultLinkPrefetchSetting() {
1085 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1086 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1087 if (preload == null) {
1088 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1089 }
1090 return preload;
1091 }
1092
1093 public String getLinkPrefetchEnabled() {
1094 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1095 }
1096
George Mount3636d0a2011-11-21 09:08:21 -08001097 // -----------------------------
1098 // getter/setters for browser recovery
1099 // -----------------------------
1100 /**
1101 * The last time browser was started.
1102 * @return The last browser start time as System.currentTimeMillis. This
1103 * can be 0 if this is the first time or the last tab was closed.
1104 */
1105 public long getLastRecovered() {
1106 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1107 }
1108
1109 /**
1110 * Sets the last browser start time.
1111 * @param time The last time as System.currentTimeMillis that the browser
1112 * was started. This should be set to 0 if the last tab is closed.
1113 */
1114 public void setLastRecovered(long time) {
1115 mPrefs.edit()
1116 .putLong(KEY_LAST_RECOVERED, time)
1117 .apply();
1118 }
1119
1120 /**
1121 * Used to determine whether or not the previous browser run crashed. Once
1122 * the previous state has been determined, the value will be set to false
1123 * until a pause is received.
1124 * @return true if the last browser run was paused or false if it crashed.
1125 */
1126 public boolean wasLastRunPaused() {
1127 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1128 }
1129
1130 /**
1131 * Sets whether or not the last run was a pause or crash.
1132 * @param isPaused Set to true When a pause is received or false after
1133 * resuming.
1134 */
1135 public void setLastRunPaused(boolean isPaused) {
1136 mPrefs.edit()
1137 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1138 .apply();
1139 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001140}