blob: 8bbe7b951fcdc6da82349aa8848c4d5bc2775d5e [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,
311 "GBK").apply();
312 }
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
415 settings.setAllowContentAccess(false);
416
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 // Preserve autologin setting
625 long gal = mPrefs.getLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, -1);
626 mPrefs.edit()
627 .clear()
628 .putLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, gal)
629 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200630 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700631 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700632 }
633
Björn Isakssonc885a242012-06-05 17:19:04 +0200634 private void resetCachedValues() {
635 updateSearchEngine(false);
636 }
637
John Reck35e9dd62011-04-25 09:01:54 -0700638 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800639 // query the profile from components autofill database 524
640 if (mAutofillHandler.mAutoFillProfile == null &&
641 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
642 WebSettings settings = null;
643 // find a valid settings object
644 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
645 while (iter.hasNext()) {
646 WeakReference<WebSettings> ref = iter.next();
647 settings = (WebSettings)ref.get();
648 if (settings != null) {
649 break;
650 }
651 }
652 if (settings != null) {
653 AutoFillProfile profile =
654 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
655 mAutofillHandler.setAutoFillProfile(profile);
656 }
657 }
John Reck35e9dd62011-04-25 09:01:54 -0700658 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800659 }
660
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800661 public String getAutoFillProfileId() {
662 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800663 }
664
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800665 public void updateAutoFillProfile(AutoFillProfile profile) {
666 syncAutoFillProfile(profile);
667 }
668
669 private void syncAutoFillProfile(AutoFillProfile profile) {
670 synchronized (mManagedSettings) {
671 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
672 while (iter.hasNext()) {
673 WeakReference<WebSettings> ref = iter.next();
674 WebSettings settings = (WebSettings)ref.get();
675 if (settings == null) {
676 iter.remove();
677 continue;
678 }
679 // update the profile only once.
680 settings.setAutoFillProfile(profile);
681 // Now we should have the guid
682 mAutofillHandler.setAutoFillProfile(profile);
683 break;
684 }
685 }
686 }
John Reck35e9dd62011-04-25 09:01:54 -0700687 public void toggleDebugSettings() {
688 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800689 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100690
John Reckb8b2af82011-05-20 15:58:33 -0700691 public boolean hasDesktopUseragent(WebView view) {
692 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
693 }
694
695 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700696 if (view == null) {
697 return;
698 }
John Reckb8b2af82011-05-20 15:58:33 -0700699 WebSettings settings = view.getSettings();
700 if (mCustomUserAgents.get(settings) != null) {
701 mCustomUserAgents.remove(settings);
702 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
703 } else {
704 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
705 settings.setUserAgentString(DESKTOP_USERAGENT);
706 }
707 }
708
John Reck7dc444b2011-06-16 17:44:29 -0700709 public static int getAdjustedMinimumFontSize(int rawValue) {
710 rawValue++; // Preference starts at 0, min font at 1
711 if (rawValue > 1) {
712 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
713 }
714 return rawValue;
715 }
716
John Reck5ba3c762011-09-14 15:00:15 -0700717 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700718 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700719 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700720 }
721
722 static int getRawTextZoom(int percent) {
723 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
724 }
725
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700726 public int getAdjustedDoubleTapZoom(int rawValue) {
727 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
728 return (int) ((rawValue + 100) * mFontSizeMult);
729 }
730
731 static int getRawDoubleTapZoom(int percent) {
732 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
733 }
734
John Reckcadae722011-07-25 11:36:17 -0700735 public SharedPreferences getPreferences() {
736 return mPrefs;
737 }
738
Victoria Lease96497832012-03-23 14:19:56 -0700739 // update connectivity-dependent options
740 public void updateConnectionType() {
741 ConnectivityManager cm = (ConnectivityManager)
742 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
743 String linkPrefetchPreference = getLinkPrefetchEnabled();
744 boolean linkPrefetchAllowed = linkPrefetchPreference.
745 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
746 NetworkInfo ni = cm.getActiveNetworkInfo();
747 if (ni != null) {
748 switch (ni.getType()) {
749 case ConnectivityManager.TYPE_WIFI:
750 case ConnectivityManager.TYPE_ETHERNET:
751 case ConnectivityManager.TYPE_BLUETOOTH:
752 linkPrefetchAllowed |= linkPrefetchPreference.
753 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
754 break;
755 case ConnectivityManager.TYPE_MOBILE:
756 case ConnectivityManager.TYPE_MOBILE_DUN:
757 case ConnectivityManager.TYPE_MOBILE_MMS:
758 case ConnectivityManager.TYPE_MOBILE_SUPL:
759 case ConnectivityManager.TYPE_WIMAX:
760 default:
761 break;
762 }
763 }
764 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
765 mLinkPrefetchAllowed = linkPrefetchAllowed;
766 syncManagedSettings();
767 }
768 }
769
luxiaol62677b02013-07-22 07:54:49 +0800770 public String getDownloadPath() {
771 return mPrefs.getString(PREF_DOWNLOAD_PATH,
772 DownloadHandler.getDefaultDownloadPath(mContext));
773 }
John Reck35e9dd62011-04-25 09:01:54 -0700774 // -----------------------------
775 // getter/setters for accessibility_preferences.xml
776 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100777
John Reck7dc444b2011-06-16 17:44:29 -0700778 @Deprecated
779 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700780 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
781 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100782 }
783
John Reck35e9dd62011-04-25 09:01:54 -0700784 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700785 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700786 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100787 }
788
John Reck92f25f82011-04-26 16:57:10 -0700789 public boolean forceEnableUserScalable() {
790 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
791 }
792
John Reck7dc444b2011-06-16 17:44:29 -0700793 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700794 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700795 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
796 return getAdjustedTextZoom(textZoom);
797 }
798
799 public void setTextZoom(int percent) {
800 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
801 }
802
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700803 public int getDoubleTapZoom() {
804 requireInitialization();
805 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
806 return getAdjustedDoubleTapZoom(doubleTapZoom);
807 }
808
809 public void setDoubleTapZoom(int percent) {
810 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
811 }
812
John Reck35e9dd62011-04-25 09:01:54 -0700813 // -----------------------------
814 // getter/setters for advanced_preferences.xml
815 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100816
John Reck35e9dd62011-04-25 09:01:54 -0700817 public String getSearchEngineName() {
luxiaol221b3932013-07-19 15:27:57 +0800818 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
819 if (defaultSearchEngineValue == null) {
820 defaultSearchEngineValue = SearchEngine.GOOGLE;
821 }
822 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100823 }
John Reck63bb6872010-12-01 19:29:32 -0800824
Michael Kolb8d772b02012-01-19 13:56:00 -0800825 public boolean allowAppTabs() {
826 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
827 }
828
John Reck35e9dd62011-04-25 09:01:54 -0700829 public boolean openInBackground() {
830 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800831 }
John Reck35e9dd62011-04-25 09:01:54 -0700832
833 public boolean enableJavascript() {
834 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
835 }
836
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800837 public boolean enableMemoryMonitor() {
838 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
839 }
840
John Reck35e9dd62011-04-25 09:01:54 -0700841 // TODO: Cache
842 public PluginState getPluginState() {
843 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
844 return PluginState.valueOf(state);
845 }
846
847 // TODO: Cache
848 public ZoomDensity getDefaultZoom() {
849 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
850 return ZoomDensity.valueOf(zoom);
851 }
852
853 public boolean loadPageInOverviewMode() {
854 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
855 }
856
857 public boolean autofitPages() {
858 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
859 }
860
861 public boolean blockPopupWindows() {
862 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
863 }
864
865 public boolean loadImages() {
866 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
867 }
868
869 public String getDefaultTextEncoding() {
870 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
871 }
872
873 // -----------------------------
874 // getter/setters for general_preferences.xml
875 // -----------------------------
876
877 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700878 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
879 }
880
881 public void setHomePage(String value) {
882 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
883 }
884
885 public boolean isAutofillEnabled() {
886 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
887 }
888
889 public void setAutofillEnabled(boolean value) {
890 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
891 }
892
893 // -----------------------------
894 // getter/setters for debug_preferences.xml
895 // -----------------------------
896
897 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700898 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700899 return true;
900 }
901 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
902 }
903
Derek Sollenberger31adf672011-07-08 11:31:30 -0400904 public boolean isSkiaHardwareAccelerated() {
905 if (!isDebugEnabled()) {
906 return false;
907 }
908 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
909 }
910
John Reck35e9dd62011-04-25 09:01:54 -0700911 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700912 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700913 return 0;
914 }
915 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
916 }
917
918 // -----------------------------
919 // getter/setters for hidden_debug_preferences.xml
920 // -----------------------------
921
922 public boolean enableVisualIndicator() {
923 if (!isDebugEnabled()) {
924 return false;
925 }
926 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
927 }
928
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700929 public boolean enableCpuUploadPath() {
930 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700931 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700932 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700933 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700934 }
935
John Reck35e9dd62011-04-25 09:01:54 -0700936 public boolean enableJavascriptConsole() {
937 if (!isDebugEnabled()) {
938 return false;
939 }
940 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
941 }
942
943 public boolean isSmallScreen() {
944 if (!isDebugEnabled()) {
945 return false;
946 }
947 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
948 }
949
950 public boolean isWideViewport() {
951 if (!isDebugEnabled()) {
952 return true;
953 }
954 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
955 }
956
957 public boolean isNormalLayout() {
958 if (!isDebugEnabled()) {
959 return false;
960 }
961 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
962 }
963
964 public boolean isTracing() {
965 if (!isDebugEnabled()) {
966 return false;
967 }
968 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
969 }
970
971 public boolean enableLightTouch() {
972 if (!isDebugEnabled()) {
973 return false;
974 }
975 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
976 }
977
978 public boolean enableNavDump() {
979 if (!isDebugEnabled()) {
980 return false;
981 }
982 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
983 }
984
985 public String getJsEngineFlags() {
986 if (!isDebugEnabled()) {
987 return "";
988 }
989 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
990 }
991
992 // -----------------------------
993 // getter/setters for lab_preferences.xml
994 // -----------------------------
995
996 public boolean useQuickControls() {
997 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
998 }
999
1000 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -07001001 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -07001002 }
1003
Michael Kolbc38c6042011-04-27 10:46:06 -07001004 public boolean useFullscreen() {
1005 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
1006 }
1007
John Reck2fd9d0e2011-07-15 11:13:48 -07001008 public boolean useInvertedRendering() {
1009 return mPrefs.getBoolean(PREF_INVERTED, false);
1010 }
1011
Nicolas Roard5d513102011-08-03 15:35:34 -07001012 public float getInvertedContrast() {
1013 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
1014 }
1015
John Reck35e9dd62011-04-25 09:01:54 -07001016 // -----------------------------
1017 // getter/setters for privacy_security_preferences.xml
1018 // -----------------------------
1019
1020 public boolean showSecurityWarnings() {
1021 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
1022 }
1023
1024 public boolean acceptCookies() {
1025 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
1026 }
1027
1028 public boolean saveFormdata() {
1029 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
1030 }
1031
1032 public boolean enableGeolocation() {
1033 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
1034 }
1035
1036 public boolean rememberPasswords() {
1037 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
1038 }
1039
Michael Kolb14612442011-06-24 13:06:29 -07001040 // -----------------------------
1041 // getter/setters for bandwidth_preferences.xml
1042 // -----------------------------
1043
Mathew Inwood467813f2011-09-02 15:43:17 +01001044 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
1045 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -07001046 }
Mathew Inwood467813f2011-09-02 15:43:17 +01001047
1048 public static String getPreloadAlwaysPreferenceString(Context context) {
1049 return context.getResources().getString(R.string.pref_data_preload_value_always);
1050 }
1051
Mathew Inwood825fba72011-10-04 14:52:52 +01001052 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1053 "browser_default_preload_setting";
1054
1055 public String getDefaultPreloadSetting() {
1056 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1057 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1058 if (preload == null) {
1059 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1060 }
1061 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001062 }
1063
1064 public String getPreloadEnabled() {
1065 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1066 }
1067
Victoria Lease96497832012-03-23 14:19:56 -07001068 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1069 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1070 }
1071
1072 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1073 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1074 }
1075
1076 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1077 "browser_default_link_prefetch_setting";
1078
1079 public String getDefaultLinkPrefetchSetting() {
1080 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1081 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1082 if (preload == null) {
1083 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1084 }
1085 return preload;
1086 }
1087
1088 public String getLinkPrefetchEnabled() {
1089 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1090 }
1091
George Mount3636d0a2011-11-21 09:08:21 -08001092 // -----------------------------
1093 // getter/setters for browser recovery
1094 // -----------------------------
1095 /**
1096 * The last time browser was started.
1097 * @return The last browser start time as System.currentTimeMillis. This
1098 * can be 0 if this is the first time or the last tab was closed.
1099 */
1100 public long getLastRecovered() {
1101 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1102 }
1103
1104 /**
1105 * Sets the last browser start time.
1106 * @param time The last time as System.currentTimeMillis that the browser
1107 * was started. This should be set to 0 if the last tab is closed.
1108 */
1109 public void setLastRecovered(long time) {
1110 mPrefs.edit()
1111 .putLong(KEY_LAST_RECOVERED, time)
1112 .apply();
1113 }
1114
1115 /**
1116 * Used to determine whether or not the previous browser run crashed. Once
1117 * the previous state has been determined, the value will be set to false
1118 * until a pause is received.
1119 * @return true if the last browser run was paused or false if it crashed.
1120 */
1121 public boolean wasLastRunPaused() {
1122 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1123 }
1124
1125 /**
1126 * Sets whether or not the last run was a pause or crash.
1127 * @param isPaused Set to true When a pause is received or false after
1128 * resuming.
1129 */
1130 public void setLastRunPaused(boolean isPaused) {
1131 mPrefs.edit()
1132 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1133 .apply();
1134 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001135}