blob: 34ba56ba0fe477ef5d5e891c516ae3c8b1168dba [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
141 //Determine if WebView is Initialized or not
142 private boolean mWebViewInitialized;
qqzhou8c5b0a32013-07-22 15:31:03 +0800143
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700144 public static void initialize(final Context context) {
John Reck35e9dd62011-04-25 09:01:54 -0700145 sInstance = new BrowserSettings(context);
146 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800147
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700148 public static BrowserSettings getInstance() {
John Reck35e9dd62011-04-25 09:01:54 -0700149 return sInstance;
150 }
Ben Murdochef671652010-11-25 17:17:58 +0000151
John Reck35e9dd62011-04-25 09:01:54 -0700152 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100153 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700154 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
155 mAutofillHandler = new AutofillHandler(mContext);
156 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700157 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
qqzhou8c5b0a32013-07-22 15:31:03 +0800158
159 // add for carrier feature
160 try {
161 sResPackageCtx = context.createPackageContext(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700162 "com.android.browser.res",
qqzhou8c5b0a32013-07-22 15:31:03 +0800163 Context.CONTEXT_IGNORE_SECURITY);
164 } catch (Exception e) {
165 Log.e("Res_Update", "Create Res Apk Failed");
166 }
John Reckcadae722011-07-25 11:36:17 -0700167 BackgroundHandler.execute(mSetup);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800168 mWebViewInitialized = false;
John Reck35e9dd62011-04-25 09:01:54 -0700169 }
170
171 public void setController(Controller controller) {
172 mController = controller;
John Reckc477e712011-08-17 11:27:15 -0700173 if (sInitialized) {
174 syncSharedSettings();
175 }
Ben Murdochef671652010-11-25 17:17:58 +0000176 }
177
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800178 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800179
John Reckc477e712011-08-17 11:27:15 -0700180 if (mNeedsSharedSync) {
181 syncSharedSettings();
182 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800183
John Reck35e9dd62011-04-25 09:01:54 -0700184 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800185 syncStaticSettings(settings);
186 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700187 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800188 }
189 }
190
John Reckd1d87312012-03-08 13:25:00 -0800191 public void stopManagingSettings(WebSettings settings) {
192 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
193 while (iter.hasNext()) {
194 WeakReference<WebSettings> ref = iter.next();
195 if (ref.get() == settings) {
196 iter.remove();
197 return;
198 }
199 }
200 }
201
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800202 public void initializeCookieSettings() {
203 CookieManager.getInstance().setAcceptCookie(acceptCookies());
204 mWebViewInitialized = true;
205 }
John Reckcadae722011-07-25 11:36:17 -0700206 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700207
208 @Override
209 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700210 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
211 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700212 // the cost of one cached page is ~3M (measured using nytimes.com). For
213 // low end devices, we only cache one page. For high end devices, we try
214 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800215
216 // SWE_TODO : assume a high-memory device
217 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700218 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800219 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700220 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
221 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
222 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700223 // Workaround b/5254577
224 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700225 if (Build.VERSION.CODENAME.equals("REL")) {
226 // This is a release build, always startup with debug disabled
227 setDebugEnabled(false);
228 }
229 if (mPrefs.contains(PREF_TEXT_SIZE)) {
230 /*
231 * Update from TextSize enum to zoom percent
232 * SMALLEST is 50%
233 * SMALLER is 75%
234 * NORMAL is 100%
235 * LARGER is 150%
236 * LARGEST is 200%
237 */
238 switch (getTextSize()) {
239 case SMALLEST:
240 setTextZoom(50);
241 break;
242 case SMALLER:
243 setTextZoom(75);
244 break;
245 case LARGER:
246 setTextZoom(150);
247 break;
248 case LARGEST:
249 setTextZoom(200);
250 break;
251 }
252 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
253 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100254
qqzhou8c5b0a32013-07-22 15:31:03 +0800255 // add for carrier homepage feature
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700256 String browserRes = mContext.getResources().getString(R.string.config_carrier_resource);
kaiyiz08f9e2b2013-08-16 09:15:48 +0800257 if ("cu".equals(browserRes) || "cmcc".equals(browserRes)) {
qqzhou8c5b0a32013-07-22 15:31:03 +0800258 int resID = sResPackageCtx.getResources().getIdentifier(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700259 "homepage_base", "string", "com.android.browser.res");
qqzhou8c5b0a32013-07-22 15:31:03 +0800260 sFactoryResetUrl = sResPackageCtx.getResources().getString(resID);
261 } else if ("ct".equals(browserRes)) {
262 int resID = sResPackageCtx.getResources().getIdentifier(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700263 "homepage_base", "string", "com.android.browser.res");
qqzhou8c5b0a32013-07-22 15:31:03 +0800264 sFactoryResetUrl = sResPackageCtx.getResources().getString(resID);
265
266 int pathID = sResPackageCtx.getResources().getIdentifier(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700267 "homepage_path", "string", "com.android.browser.res");
qqzhou8c5b0a32013-07-22 15:31:03 +0800268 String path = sResPackageCtx.getResources().getString(pathID);
269 Locale locale = Locale.getDefault();
270 path = path.replace("%y", locale.getLanguage().toLowerCase());
271 path = path.replace("%z", '_'+locale.getCountry().toLowerCase());
272 boolean useCountry = true;
273 boolean useLanguage = true;
274 InputStream is = null;
275 AssetManager am = mContext.getAssets();
276 try {
277 is = am.open(path);
278 } catch (Exception ignored) {
279 useCountry = false;
280 path = sResPackageCtx.getResources().getString(pathID);
281 path = path.replace("%y", locale.getLanguage().toLowerCase());
282 path = path.replace("%z", "");
283 try {
284 is = am.open(path);
285 } catch (Exception ignoredlanguage) {
286 useLanguage = false;
287 }
288 } finally {
289 if (is != null) {
290 try {
291 is.close();
292 } catch (Exception ignored) {}
293 }
294 }
295
296 if (!useCountry && !useLanguage) {
297 sFactoryResetUrl = sFactoryResetUrl.replace("%y%z", "en");
298 } else {
299 sFactoryResetUrl = sFactoryResetUrl.replace("%y",
300 locale.getLanguage().toLowerCase());
301 sFactoryResetUrl = sFactoryResetUrl.replace("%z", useCountry ?
302 '_' + locale.getCountry().toLowerCase() : "");
303 }
304 } else {
305 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
306 }
307
kaiyizbf086ea2013-08-02 11:03:58 +0800308 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
309 if (!"default".equals(browserRes)) {
310 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING,
Vivek Sekhar22bd57b2014-04-05 22:19:43 -0700311 "UTF-8").apply();
kaiyizbf086ea2013-08-02 11:03:58 +0800312 }
313 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100314 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
315 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
316 BrowserProvider.getClientId(mContext.getContentResolver()));
317 }
318
Ben Murdochaaa1f372011-07-25 15:40:58 +0100319 synchronized (BrowserSettings.class) {
320 sInitialized = true;
321 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700322 }
323 }
324 };
325
Ben Murdochaaa1f372011-07-25 15:40:58 +0100326 private static void requireInitialization() {
327 synchronized (BrowserSettings.class) {
328 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700329 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100330 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700331 } catch (InterruptedException e) {
332 }
333 }
334 }
335 }
336
The Android Open Source Project0c908882009-03-03 19:32:16 -0800337 /**
John Reck35e9dd62011-04-25 09:01:54 -0700338 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800339 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800340 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700341 settings.setGeolocationEnabled(enableGeolocation());
342 settings.setJavaScriptEnabled(enableJavascript());
343 settings.setLightTouchEnabled(enableLightTouch());
344 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700345 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
346 settings.setDefaultZoom(getDefaultZoom());
347 settings.setMinimumFontSize(getMinimumFontSize());
348 settings.setMinimumLogicalFontSize(getMinimumFontSize());
349 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700350 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700351 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100352 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700353 settings.setLoadsImagesAutomatically(loadImages());
354 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
355 settings.setSavePassword(rememberPasswords());
356 settings.setSaveFormData(saveFormdata());
357 settings.setUseWideViewPort(isWideViewport());
John Reckb8b2af82011-05-20 15:58:33 -0700358
qqzhoue6ff8b42013-07-23 17:28:48 +0800359 // add for carrier useragent feature
360 String ua = null;
361 try {
362 Class c = Class.forName("com.qrd.useragent.UserAgentHandler");
363 Object cObj = c.newInstance();
364 Method m = c.getDeclaredMethod("getUAString", Context.class);
365 ua = (String)m.invoke(cObj, mContext);
366 } catch (Exception e) {
367 Log.e(TAG, "plug in Load failed, err " + e);
368 ua = mCustomUserAgents.get(settings);
369 }
John Reck46500332011-06-07 14:36:10 -0700370 if (ua != null) {
John Reckb8b2af82011-05-20 15:58:33 -0700371 settings.setUserAgentString(ua);
372 } else {
373 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
374 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700375
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800376 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800377 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
378 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
379 settingsClassic.setForceUserScalable(forceEnableUserScalable());
380 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
381 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800382
John Reckea17e782011-10-27 17:15:59 -0700383 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800384 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700385 useInverted ? "true" : "false");
386 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800387 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700388 Float.toString(getInvertedContrast()));
389 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700390
John Reckea17e782011-10-27 17:15:59 -0700391 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800392 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700393 enableCpuUploadPath() ? "true" : "false");
394 }
Victoria Lease96497832012-03-23 14:19:56 -0700395
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800396 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000397 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800398
John Reck35e9dd62011-04-25 09:01:54 -0700399 /**
400 * Syncs all the settings that have no UI
401 * These cannot change, so we only need to set them once per WebSettings
402 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800403 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700404 settings.setDefaultFontSize(16);
405 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000406
John Reck35e9dd62011-04-25 09:01:54 -0700407 // WebView inside Browser doesn't want initial focus to be set.
408 settings.setNeedInitialFocus(false);
409 // Browser supports multiple windows
410 settings.setSupportMultipleWindows(true);
411 // enable smooth transition for better performance during panning or
412 // zooming
413 settings.setEnableSmoothTransition(true);
414 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700415 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700416
417 // HTML5 API flags
418 settings.setAppCacheEnabled(true);
419 settings.setDatabaseEnabled(true);
420 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700421
422 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700423 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700424 settings.setAppCachePath(getAppCachePath());
425 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
426 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700427 // origin policy for file access
428 settings.setAllowUniversalAccessFromFileURLs(false);
429 settings.setAllowFileAccessFromFileURLs(false);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800430
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800431 //if (!(settings instanceof WebSettingsClassic)) return;
432 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800433
434 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
435 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
436 // WebView should be preserving the memory as much as possible.
437 // However, apps like browser wish to turn on the performance mode which
438 // would require more memory.
439 // TODO: We need to dynamically allocate/deallocate temporary memory for
440 // apps which are trying to use minimal memory. Currently, double
441 // buffering is always turned on, which is unnecessary.
442 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
443 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800444 */
John Reck35e9dd62011-04-25 09:01:54 -0700445 }
446
447 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700448 mNeedsSharedSync = false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800449 if (mWebViewInitialized) {
450 CookieManager.getInstance().setAcceptCookie(acceptCookies());
451 }
John Reck35e9dd62011-04-25 09:01:54 -0700452 if (mController != null) {
453 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700454 }
John Reck35e9dd62011-04-25 09:01:54 -0700455 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700456
John Reck35e9dd62011-04-25 09:01:54 -0700457 private void syncManagedSettings() {
458 syncSharedSettings();
459 synchronized (mManagedSettings) {
460 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
461 while (iter.hasNext()) {
462 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800463 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700464 if (settings == null) {
465 iter.remove();
466 continue;
467 }
468 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000469 }
John Reck35e9dd62011-04-25 09:01:54 -0700470 }
471 }
Ben Murdochef671652010-11-25 17:17:58 +0000472
John Reck35e9dd62011-04-25 09:01:54 -0700473 @Override
474 public void onSharedPreferenceChanged(
475 SharedPreferences sharedPreferences, String key) {
476 syncManagedSettings();
477 if (PREF_SEARCH_ENGINE.equals(key)) {
478 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700479 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200480 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700481 mController.getUi().setFullscreen(useFullscreen());
482 }
Michael Kolb0241e752011-07-07 14:58:50 -0700483 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200484 if (mController != null && mController.getUi() != null) {
Michael Kolb0241e752011-07-07 14:58:50 -0700485 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
486 }
Victoria Lease96497832012-03-23 14:19:56 -0700487 } else if (PREF_LINK_PREFETCH.equals(key)) {
488 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700489 }
John Reck35e9dd62011-04-25 09:01:54 -0700490 }
491
John Reck961d35d2011-06-23 09:45:54 -0700492 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100493 requireInitialization();
494 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700495 }
496
497 public LayoutAlgorithm getLayoutAlgorithm() {
498 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
499 if (autofitPages()) {
500 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
501 }
502 if (isDebugEnabled()) {
503 if (isSmallScreen()) {
504 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000505 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700506 if (isNormalLayout()) {
507 layoutAlgorithm = LayoutAlgorithm.NORMAL;
508 } else {
509 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
510 }
Ben Murdochef671652010-11-25 17:17:58 +0000511 }
John Reck35e9dd62011-04-25 09:01:54 -0700512 }
513 return layoutAlgorithm;
514 }
Ben Murdochef671652010-11-25 17:17:58 +0000515
John Reck35e9dd62011-04-25 09:01:54 -0700516 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700517 requireInitialization();
518 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800519 }
520
John Reck35e9dd62011-04-25 09:01:54 -0700521 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700522 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700523 return mWebStorageSizeManager;
524 }
525
John Reck35e9dd62011-04-25 09:01:54 -0700526 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700527 if (mAppCachePath == null) {
528 mAppCachePath = mContext.getDir("appcache", 0).getPath();
529 }
530 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700531 }
532
533 private void updateSearchEngine(boolean force) {
534 String searchEngineName = getSearchEngineName();
535 if (force || mSearchEngine == null ||
536 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700537 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000538 }
539 }
540
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100541 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700542 if (mSearchEngine == null) {
543 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100544 }
John Reck35e9dd62011-04-25 09:01:54 -0700545 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100546 }
547
John Reck35e9dd62011-04-25 09:01:54 -0700548 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700549 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700550 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100551 }
552
John Reck35e9dd62011-04-25 09:01:54 -0700553 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700554 Editor edit = mPrefs.edit();
555 edit.putBoolean(PREF_DEBUG_MENU, value);
556 if (!value) {
557 // Reset to "safe" value
558 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
559 }
560 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100561 }
562
John Reck35e9dd62011-04-25 09:01:54 -0700563 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800564 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700565 if (mController != null) {
566 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800567 if (current != null) {
568 current.clearCache(true);
569 }
570 }
571 }
572
John Reck35e9dd62011-04-25 09:01:54 -0700573 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800574 CookieManager.getInstance().removeAllCookie();
575 }
576
John Reck35e9dd62011-04-25 09:01:54 -0700577 public void clearHistory() {
578 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800579 Browser.clearHistory(resolver);
580 Browser.clearSearches(resolver);
581 }
582
John Reck35e9dd62011-04-25 09:01:54 -0700583 public void clearFormData() {
584 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700585 if (mController!= null) {
586 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100587 if (currentTopView != null) {
588 currentTopView.clearFormData();
589 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800590 }
591 }
592
John Reck35e9dd62011-04-25 09:01:54 -0700593 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700594 // Clear password store maintained by SWE engine
595 WebSettings settings = null;
596 // find a valid settings object
597 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
598 while (iter.hasNext()) {
599 WeakReference<WebSettings> ref = iter.next();
600 settings = (WebSettings)ref.get();
601 if (settings != null) {
602 break;
603 }
604 }
605 if (settings != null) {
606 settings.clearPasswords();
607 }
608
609 // Clear passwords in WebView database
John Reck35e9dd62011-04-25 09:01:54 -0700610 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800611 db.clearUsernamePassword();
612 db.clearHttpAuthUsernamePassword();
613 }
614
John Reck35e9dd62011-04-25 09:01:54 -0700615 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100616 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100617 }
618
John Reck35e9dd62011-04-25 09:01:54 -0700619 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100620 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100621 }
622
John Reck35e9dd62011-04-25 09:01:54 -0700623 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700624 mPrefs.edit()
625 .clear()
John Reckbd315192011-07-29 10:05:47 -0700626 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200627 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700628 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700629 }
630
Björn Isakssonc885a242012-06-05 17:19:04 +0200631 private void resetCachedValues() {
632 updateSearchEngine(false);
633 }
634
John Reck35e9dd62011-04-25 09:01:54 -0700635 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800636 // query the profile from components autofill database 524
637 if (mAutofillHandler.mAutoFillProfile == null &&
638 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
639 WebSettings settings = null;
640 // find a valid settings object
641 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
642 while (iter.hasNext()) {
643 WeakReference<WebSettings> ref = iter.next();
644 settings = (WebSettings)ref.get();
645 if (settings != null) {
646 break;
647 }
648 }
649 if (settings != null) {
650 AutoFillProfile profile =
651 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
652 mAutofillHandler.setAutoFillProfile(profile);
653 }
654 }
John Reck35e9dd62011-04-25 09:01:54 -0700655 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800656 }
657
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800658 public String getAutoFillProfileId() {
659 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800660 }
661
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800662 public void updateAutoFillProfile(AutoFillProfile profile) {
663 syncAutoFillProfile(profile);
664 }
665
666 private void syncAutoFillProfile(AutoFillProfile profile) {
667 synchronized (mManagedSettings) {
668 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
669 while (iter.hasNext()) {
670 WeakReference<WebSettings> ref = iter.next();
671 WebSettings settings = (WebSettings)ref.get();
672 if (settings == null) {
673 iter.remove();
674 continue;
675 }
676 // update the profile only once.
677 settings.setAutoFillProfile(profile);
678 // Now we should have the guid
679 mAutofillHandler.setAutoFillProfile(profile);
680 break;
681 }
682 }
683 }
John Reck35e9dd62011-04-25 09:01:54 -0700684 public void toggleDebugSettings() {
685 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800686 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100687
John Reckb8b2af82011-05-20 15:58:33 -0700688 public boolean hasDesktopUseragent(WebView view) {
689 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
690 }
691
692 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700693 if (view == null) {
694 return;
695 }
John Reckb8b2af82011-05-20 15:58:33 -0700696 WebSettings settings = view.getSettings();
697 if (mCustomUserAgents.get(settings) != null) {
698 mCustomUserAgents.remove(settings);
699 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
700 } else {
701 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
702 settings.setUserAgentString(DESKTOP_USERAGENT);
703 }
704 }
705
John Reck7dc444b2011-06-16 17:44:29 -0700706 public static int getAdjustedMinimumFontSize(int rawValue) {
707 rawValue++; // Preference starts at 0, min font at 1
708 if (rawValue > 1) {
709 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
710 }
711 return rawValue;
712 }
713
John Reck5ba3c762011-09-14 15:00:15 -0700714 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700715 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700716 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700717 }
718
719 static int getRawTextZoom(int percent) {
720 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
721 }
722
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700723 public int getAdjustedDoubleTapZoom(int rawValue) {
724 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
725 return (int) ((rawValue + 100) * mFontSizeMult);
726 }
727
728 static int getRawDoubleTapZoom(int percent) {
729 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
730 }
731
John Reckcadae722011-07-25 11:36:17 -0700732 public SharedPreferences getPreferences() {
733 return mPrefs;
734 }
735
Victoria Lease96497832012-03-23 14:19:56 -0700736 // update connectivity-dependent options
737 public void updateConnectionType() {
738 ConnectivityManager cm = (ConnectivityManager)
739 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
740 String linkPrefetchPreference = getLinkPrefetchEnabled();
741 boolean linkPrefetchAllowed = linkPrefetchPreference.
742 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
743 NetworkInfo ni = cm.getActiveNetworkInfo();
744 if (ni != null) {
745 switch (ni.getType()) {
746 case ConnectivityManager.TYPE_WIFI:
747 case ConnectivityManager.TYPE_ETHERNET:
748 case ConnectivityManager.TYPE_BLUETOOTH:
749 linkPrefetchAllowed |= linkPrefetchPreference.
750 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
751 break;
752 case ConnectivityManager.TYPE_MOBILE:
753 case ConnectivityManager.TYPE_MOBILE_DUN:
754 case ConnectivityManager.TYPE_MOBILE_MMS:
755 case ConnectivityManager.TYPE_MOBILE_SUPL:
756 case ConnectivityManager.TYPE_WIMAX:
757 default:
758 break;
759 }
760 }
761 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
762 mLinkPrefetchAllowed = linkPrefetchAllowed;
763 syncManagedSettings();
764 }
765 }
766
luxiaol62677b02013-07-22 07:54:49 +0800767 public String getDownloadPath() {
768 return mPrefs.getString(PREF_DOWNLOAD_PATH,
769 DownloadHandler.getDefaultDownloadPath(mContext));
770 }
John Reck35e9dd62011-04-25 09:01:54 -0700771 // -----------------------------
772 // getter/setters for accessibility_preferences.xml
773 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100774
John Reck7dc444b2011-06-16 17:44:29 -0700775 @Deprecated
776 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700777 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
778 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100779 }
780
John Reck35e9dd62011-04-25 09:01:54 -0700781 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700782 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700783 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100784 }
785
John Reck92f25f82011-04-26 16:57:10 -0700786 public boolean forceEnableUserScalable() {
787 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
788 }
789
John Reck7dc444b2011-06-16 17:44:29 -0700790 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700791 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700792 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
793 return getAdjustedTextZoom(textZoom);
794 }
795
796 public void setTextZoom(int percent) {
797 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
798 }
799
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700800 public int getDoubleTapZoom() {
801 requireInitialization();
802 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
803 return getAdjustedDoubleTapZoom(doubleTapZoom);
804 }
805
806 public void setDoubleTapZoom(int percent) {
807 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
808 }
809
John Reck35e9dd62011-04-25 09:01:54 -0700810 // -----------------------------
811 // getter/setters for advanced_preferences.xml
812 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100813
John Reck35e9dd62011-04-25 09:01:54 -0700814 public String getSearchEngineName() {
luxiaol221b3932013-07-19 15:27:57 +0800815 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
816 if (defaultSearchEngineValue == null) {
817 defaultSearchEngineValue = SearchEngine.GOOGLE;
818 }
819 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100820 }
John Reck63bb6872010-12-01 19:29:32 -0800821
Michael Kolb8d772b02012-01-19 13:56:00 -0800822 public boolean allowAppTabs() {
823 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
824 }
825
John Reck35e9dd62011-04-25 09:01:54 -0700826 public boolean openInBackground() {
827 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800828 }
John Reck35e9dd62011-04-25 09:01:54 -0700829
830 public boolean enableJavascript() {
831 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
832 }
833
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800834 public boolean enableMemoryMonitor() {
835 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
836 }
837
John Reck35e9dd62011-04-25 09:01:54 -0700838 // TODO: Cache
839 public PluginState getPluginState() {
840 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
841 return PluginState.valueOf(state);
842 }
843
844 // TODO: Cache
845 public ZoomDensity getDefaultZoom() {
846 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
847 return ZoomDensity.valueOf(zoom);
848 }
849
850 public boolean loadPageInOverviewMode() {
851 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
852 }
853
854 public boolean autofitPages() {
855 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
856 }
857
858 public boolean blockPopupWindows() {
859 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
860 }
861
862 public boolean loadImages() {
863 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
864 }
865
866 public String getDefaultTextEncoding() {
867 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
868 }
869
870 // -----------------------------
871 // getter/setters for general_preferences.xml
872 // -----------------------------
873
874 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700875 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
876 }
877
878 public void setHomePage(String value) {
879 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
880 }
881
882 public boolean isAutofillEnabled() {
883 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
884 }
885
886 public void setAutofillEnabled(boolean value) {
887 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
888 }
889
890 // -----------------------------
891 // getter/setters for debug_preferences.xml
892 // -----------------------------
893
894 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700895 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700896 return true;
897 }
898 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
899 }
900
Derek Sollenberger31adf672011-07-08 11:31:30 -0400901 public boolean isSkiaHardwareAccelerated() {
902 if (!isDebugEnabled()) {
903 return false;
904 }
905 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
906 }
907
John Reck35e9dd62011-04-25 09:01:54 -0700908 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700909 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700910 return 0;
911 }
912 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
913 }
914
915 // -----------------------------
916 // getter/setters for hidden_debug_preferences.xml
917 // -----------------------------
918
919 public boolean enableVisualIndicator() {
920 if (!isDebugEnabled()) {
921 return false;
922 }
923 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
924 }
925
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700926 public boolean enableCpuUploadPath() {
927 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700928 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700929 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700930 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700931 }
932
John Reck35e9dd62011-04-25 09:01:54 -0700933 public boolean enableJavascriptConsole() {
934 if (!isDebugEnabled()) {
935 return false;
936 }
937 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
938 }
939
940 public boolean isSmallScreen() {
941 if (!isDebugEnabled()) {
942 return false;
943 }
944 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
945 }
946
947 public boolean isWideViewport() {
948 if (!isDebugEnabled()) {
949 return true;
950 }
951 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
952 }
953
954 public boolean isNormalLayout() {
955 if (!isDebugEnabled()) {
956 return false;
957 }
958 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
959 }
960
961 public boolean isTracing() {
962 if (!isDebugEnabled()) {
963 return false;
964 }
965 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
966 }
967
968 public boolean enableLightTouch() {
969 if (!isDebugEnabled()) {
970 return false;
971 }
972 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
973 }
974
975 public boolean enableNavDump() {
976 if (!isDebugEnabled()) {
977 return false;
978 }
979 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
980 }
981
982 public String getJsEngineFlags() {
983 if (!isDebugEnabled()) {
984 return "";
985 }
986 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
987 }
988
989 // -----------------------------
990 // getter/setters for lab_preferences.xml
991 // -----------------------------
992
993 public boolean useQuickControls() {
994 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
995 }
996
997 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700998 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700999 }
1000
Michael Kolbc38c6042011-04-27 10:46:06 -07001001 public boolean useFullscreen() {
1002 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
1003 }
1004
John Reck2fd9d0e2011-07-15 11:13:48 -07001005 public boolean useInvertedRendering() {
1006 return mPrefs.getBoolean(PREF_INVERTED, false);
1007 }
1008
Nicolas Roard5d513102011-08-03 15:35:34 -07001009 public float getInvertedContrast() {
1010 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
1011 }
1012
John Reck35e9dd62011-04-25 09:01:54 -07001013 // -----------------------------
1014 // getter/setters for privacy_security_preferences.xml
1015 // -----------------------------
1016
1017 public boolean showSecurityWarnings() {
1018 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
1019 }
1020
1021 public boolean acceptCookies() {
1022 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
1023 }
1024
1025 public boolean saveFormdata() {
1026 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
1027 }
1028
1029 public boolean enableGeolocation() {
1030 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
1031 }
1032
1033 public boolean rememberPasswords() {
1034 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
1035 }
1036
Michael Kolb14612442011-06-24 13:06:29 -07001037 // -----------------------------
1038 // getter/setters for bandwidth_preferences.xml
1039 // -----------------------------
1040
Mathew Inwood467813f2011-09-02 15:43:17 +01001041 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
1042 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -07001043 }
Mathew Inwood467813f2011-09-02 15:43:17 +01001044
1045 public static String getPreloadAlwaysPreferenceString(Context context) {
1046 return context.getResources().getString(R.string.pref_data_preload_value_always);
1047 }
1048
Mathew Inwood825fba72011-10-04 14:52:52 +01001049 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1050 "browser_default_preload_setting";
1051
1052 public String getDefaultPreloadSetting() {
1053 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1054 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1055 if (preload == null) {
1056 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1057 }
1058 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001059 }
1060
1061 public String getPreloadEnabled() {
1062 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1063 }
1064
Victoria Lease96497832012-03-23 14:19:56 -07001065 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1066 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1067 }
1068
1069 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1070 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1071 }
1072
1073 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1074 "browser_default_link_prefetch_setting";
1075
1076 public String getDefaultLinkPrefetchSetting() {
1077 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1078 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1079 if (preload == null) {
1080 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1081 }
1082 return preload;
1083 }
1084
1085 public String getLinkPrefetchEnabled() {
1086 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1087 }
1088
George Mount3636d0a2011-11-21 09:08:21 -08001089 // -----------------------------
1090 // getter/setters for browser recovery
1091 // -----------------------------
1092 /**
1093 * The last time browser was started.
1094 * @return The last browser start time as System.currentTimeMillis. This
1095 * can be 0 if this is the first time or the last tab was closed.
1096 */
1097 public long getLastRecovered() {
1098 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1099 }
1100
1101 /**
1102 * Sets the last browser start time.
1103 * @param time The last time as System.currentTimeMillis that the browser
1104 * was started. This should be set to 0 if the last tab is closed.
1105 */
1106 public void setLastRecovered(long time) {
1107 mPrefs.edit()
1108 .putLong(KEY_LAST_RECOVERED, time)
1109 .apply();
1110 }
1111
1112 /**
1113 * Used to determine whether or not the previous browser run crashed. Once
1114 * the previous state has been determined, the value will be set to false
1115 * until a pause is received.
1116 * @return true if the last browser run was paused or false if it crashed.
1117 */
1118 public boolean wasLastRunPaused() {
1119 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1120 }
1121
1122 /**
1123 * Sets whether or not the last run was a pause or crash.
1124 * @param isPaused Set to true When a pause is received or false after
1125 * resuming.
1126 */
1127 public void setLastRunPaused(boolean isPaused) {
1128 mPrefs.edit()
1129 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1130 .apply();
1131 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001132}