blob: 0e4fba68aa418272699ac522f77a14c56f750a80 [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;
46import java.util.Iterator;
47import java.util.LinkedList;
qqzhou8c5b0a32013-07-22 15:31:03 +080048import java.util.Locale;
John Reckb8b2af82011-05-20 15:58:33 -070049import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080051import org.codeaurora.swe.AutoFillProfile;
52import org.codeaurora.swe.CookieManager;
53import org.codeaurora.swe.GeolocationPermissions;
54import org.codeaurora.swe.WebSettings.LayoutAlgorithm;
55import org.codeaurora.swe.WebSettings.PluginState;
56import org.codeaurora.swe.WebSettings.TextSize;
57import org.codeaurora.swe.WebSettings.ZoomDensity;
58import org.codeaurora.swe.WebSettings;
59import org.codeaurora.swe.WebView;
60
John Reck35e9dd62011-04-25 09:01:54 -070061/**
62 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080063 */
John Reck35e9dd62011-04-25 09:01:54 -070064public class BrowserSettings implements OnSharedPreferenceChangeListener,
65 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010066
John Reck35e9dd62011-04-25 09:01:54 -070067 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070068 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
69 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
70 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080071
72 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070073 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
74 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070075
76 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070077 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
78 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070079
80 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070081 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
82 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080083
John Reck7fd1e8f2011-04-27 18:22:57 -070084 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
85 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
86 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
87
John Reck35e9dd62011-04-25 09:01:54 -070088 private static final String USER_AGENTS[] = { null,
89 DESKTOP_USERAGENT,
90 IPHONE_USERAGENT,
91 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070092 FROYO_USERAGENT,
93 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070094 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080095
qqzhoue6ff8b42013-07-23 17:28:48 +080096 private static final String TAG = "BrowserSettings";
John Reck8fc22a12011-06-16 14:57:41 -070097 // The minimum min font size
98 // Aka, the lower bounds for the min font size range
99 // which is 1:5..24
100 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -0700101 // The initial value in the text zoom range
102 // This is what represents 100% in the SeekBarPreference range
103 private static final int TEXT_ZOOM_START_VAL = 10;
104 // The size of a single step in the text zoom range, in percent
105 private static final int TEXT_ZOOM_STEP = 5;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700106 // The initial value in the double tap zoom range
107 // This is what represents 100% in the SeekBarPreference range
108 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
109 // The size of a single step in the double tap zoom range, in percent
110 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -0700111
John Reck35e9dd62011-04-25 09:01:54 -0700112 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -0700113
John Reck35e9dd62011-04-25 09:01:54 -0700114 private Context mContext;
115 private SharedPreferences mPrefs;
116 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700117 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700118 private WebStorageSizeManager mWebStorageSizeManager;
119 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700120 private WeakHashMap<WebSettings, String> mCustomUserAgents;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100121 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -0700122 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -0700123 private float mFontSizeMult = 1.0f;
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700124 private boolean enableWideViewport = true;
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
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700141 public static void initialize(final Context context) {
John Reck35e9dd62011-04-25 09:01:54 -0700142 sInstance = new BrowserSettings(context);
143 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800144
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700145 public static BrowserSettings getInstance() {
John Reck35e9dd62011-04-25 09:01:54 -0700146 return sInstance;
147 }
Ben Murdochef671652010-11-25 17:17:58 +0000148
John Reck35e9dd62011-04-25 09:01:54 -0700149 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100150 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700151 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
152 mAutofillHandler = new AutofillHandler(mContext);
153 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700154 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
qqzhou8c5b0a32013-07-22 15:31:03 +0800155
156 // add for carrier feature
157 try {
158 sResPackageCtx = context.createPackageContext(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700159 "com.android.browser.res",
qqzhou8c5b0a32013-07-22 15:31:03 +0800160 Context.CONTEXT_IGNORE_SECURITY);
161 } catch (Exception e) {
162 Log.e("Res_Update", "Create Res Apk Failed");
163 }
John Reckcadae722011-07-25 11:36:17 -0700164 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700165 }
166
167 public void setController(Controller controller) {
168 mController = controller;
John Reckc477e712011-08-17 11:27:15 -0700169 if (sInitialized) {
170 syncSharedSettings();
171 }
Ben Murdochef671652010-11-25 17:17:58 +0000172 }
173
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800174 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800175
John Reckc477e712011-08-17 11:27:15 -0700176 if (mNeedsSharedSync) {
177 syncSharedSettings();
178 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800179
John Reck35e9dd62011-04-25 09:01:54 -0700180 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800181 syncStaticSettings(settings);
182 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700183 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800184 }
185 }
186
John Reckd1d87312012-03-08 13:25:00 -0800187 public void stopManagingSettings(WebSettings settings) {
188 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
189 while (iter.hasNext()) {
190 WeakReference<WebSettings> ref = iter.next();
191 if (ref.get() == settings) {
192 iter.remove();
193 return;
194 }
195 }
196 }
197
John Reckcadae722011-07-25 11:36:17 -0700198 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700199
200 @Override
201 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700202 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
203 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700204 // the cost of one cached page is ~3M (measured using nytimes.com). For
205 // low end devices, we only cache one page. For high end devices, we try
206 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800207
208 // SWE_TODO : assume a high-memory device
209 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700210 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800211 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700212 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
213 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
214 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700215 // Workaround b/5254577
216 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700217 if (Build.VERSION.CODENAME.equals("REL")) {
218 // This is a release build, always startup with debug disabled
219 setDebugEnabled(false);
220 }
221 if (mPrefs.contains(PREF_TEXT_SIZE)) {
222 /*
223 * Update from TextSize enum to zoom percent
224 * SMALLEST is 50%
225 * SMALLER is 75%
226 * NORMAL is 100%
227 * LARGER is 150%
228 * LARGEST is 200%
229 */
230 switch (getTextSize()) {
231 case SMALLEST:
232 setTextZoom(50);
233 break;
234 case SMALLER:
235 setTextZoom(75);
236 break;
237 case LARGER:
238 setTextZoom(150);
239 break;
240 case LARGEST:
241 setTextZoom(200);
242 break;
243 }
244 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
245 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100246
qqzhou8c5b0a32013-07-22 15:31:03 +0800247 // add for carrier homepage feature
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700248 String browserRes = mContext.getResources().getString(R.string.config_carrier_resource);
kaiyiz08f9e2b2013-08-16 09:15:48 +0800249 if ("cu".equals(browserRes) || "cmcc".equals(browserRes)) {
qqzhou8c5b0a32013-07-22 15:31:03 +0800250 int resID = sResPackageCtx.getResources().getIdentifier(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700251 "homepage_base", "string", "com.android.browser.res");
qqzhou8c5b0a32013-07-22 15:31:03 +0800252 sFactoryResetUrl = sResPackageCtx.getResources().getString(resID);
253 } else if ("ct".equals(browserRes)) {
254 int resID = sResPackageCtx.getResources().getIdentifier(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700255 "homepage_base", "string", "com.android.browser.res");
qqzhou8c5b0a32013-07-22 15:31:03 +0800256 sFactoryResetUrl = sResPackageCtx.getResources().getString(resID);
257
258 int pathID = sResPackageCtx.getResources().getIdentifier(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700259 "homepage_path", "string", "com.android.browser.res");
qqzhou8c5b0a32013-07-22 15:31:03 +0800260 String path = sResPackageCtx.getResources().getString(pathID);
261 Locale locale = Locale.getDefault();
262 path = path.replace("%y", locale.getLanguage().toLowerCase());
263 path = path.replace("%z", '_'+locale.getCountry().toLowerCase());
264 boolean useCountry = true;
265 boolean useLanguage = true;
266 InputStream is = null;
267 AssetManager am = mContext.getAssets();
268 try {
269 is = am.open(path);
270 } catch (Exception ignored) {
271 useCountry = false;
272 path = sResPackageCtx.getResources().getString(pathID);
273 path = path.replace("%y", locale.getLanguage().toLowerCase());
274 path = path.replace("%z", "");
275 try {
276 is = am.open(path);
277 } catch (Exception ignoredlanguage) {
278 useLanguage = false;
279 }
280 } finally {
281 if (is != null) {
282 try {
283 is.close();
284 } catch (Exception ignored) {}
285 }
286 }
287
288 if (!useCountry && !useLanguage) {
289 sFactoryResetUrl = sFactoryResetUrl.replace("%y%z", "en");
290 } else {
291 sFactoryResetUrl = sFactoryResetUrl.replace("%y",
292 locale.getLanguage().toLowerCase());
293 sFactoryResetUrl = sFactoryResetUrl.replace("%z", useCountry ?
294 '_' + locale.getCountry().toLowerCase() : "");
295 }
296 } else {
297 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
298 }
299
kaiyizbf086ea2013-08-02 11:03:58 +0800300 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
301 if (!"default".equals(browserRes)) {
302 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING,
Vivek Sekhar22bd57b2014-04-05 22:19:43 -0700303 "UTF-8").apply();
kaiyizbf086ea2013-08-02 11:03:58 +0800304 }
305 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100306 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
307 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
308 BrowserProvider.getClientId(mContext.getContentResolver()));
309 }
310
Ben Murdochaaa1f372011-07-25 15:40:58 +0100311 synchronized (BrowserSettings.class) {
312 sInitialized = true;
313 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700314 }
315 }
316 };
317
Ben Murdochaaa1f372011-07-25 15:40:58 +0100318 private static void requireInitialization() {
319 synchronized (BrowserSettings.class) {
320 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700321 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100322 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700323 } catch (InterruptedException e) {
324 }
325 }
326 }
327 }
328
The Android Open Source Project0c908882009-03-03 19:32:16 -0800329 /**
John Reck35e9dd62011-04-25 09:01:54 -0700330 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800331 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800332 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700333 settings.setGeolocationEnabled(enableGeolocation());
334 settings.setJavaScriptEnabled(enableJavascript());
335 settings.setLightTouchEnabled(enableLightTouch());
336 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700337 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
338 settings.setDefaultZoom(getDefaultZoom());
339 settings.setMinimumFontSize(getMinimumFontSize());
340 settings.setMinimumLogicalFontSize(getMinimumFontSize());
341 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700342 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700343 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100344 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700345 settings.setLoadsImagesAutomatically(loadImages());
346 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
347 settings.setSavePassword(rememberPasswords());
348 settings.setSaveFormData(saveFormdata());
349 settings.setUseWideViewPort(isWideViewport());
John Reckb8b2af82011-05-20 15:58:33 -0700350
qqzhoue6ff8b42013-07-23 17:28:48 +0800351 // add for carrier useragent feature
352 String ua = null;
Bijan Amirzada58383e72014-04-01 14:45:22 -0700353 Object objUserAgentHandler = ReflectHelper.newObject(
354 "com.qrd.useragent.UserAgentHandler", null, null);
355 Object[] params = {mContext};
356 Class[] type = new Class[] {Context.class};
357 ua = (String) ReflectHelper.invokeMethod(objUserAgentHandler,"getUAString",
358 type, params);
359 if (ua == null)
qqzhoue6ff8b42013-07-23 17:28:48 +0800360 ua = mCustomUserAgents.get(settings);
Bijan Amirzada58383e72014-04-01 14:45:22 -0700361
362 if (ua != null){
John Reckb8b2af82011-05-20 15:58:33 -0700363 settings.setUserAgentString(ua);
364 } else {
365 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
366 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700367
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800368 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800369 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
370 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
371 settingsClassic.setForceUserScalable(forceEnableUserScalable());
372 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
373 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800374
John Reckea17e782011-10-27 17:15:59 -0700375 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800376 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700377 useInverted ? "true" : "false");
378 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800379 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700380 Float.toString(getInvertedContrast()));
381 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700382
John Reckea17e782011-10-27 17:15:59 -0700383 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800384 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700385 enableCpuUploadPath() ? "true" : "false");
386 }
Victoria Lease96497832012-03-23 14:19:56 -0700387
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800388 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000389 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800390
John Reck35e9dd62011-04-25 09:01:54 -0700391 /**
392 * Syncs all the settings that have no UI
393 * These cannot change, so we only need to set them once per WebSettings
394 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800395 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700396 settings.setDefaultFontSize(16);
397 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000398
John Reck35e9dd62011-04-25 09:01:54 -0700399 // WebView inside Browser doesn't want initial focus to be set.
400 settings.setNeedInitialFocus(false);
401 // Browser supports multiple windows
402 settings.setSupportMultipleWindows(true);
403 // enable smooth transition for better performance during panning or
404 // zooming
405 settings.setEnableSmoothTransition(true);
406 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700407 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700408
409 // HTML5 API flags
410 settings.setAppCacheEnabled(true);
411 settings.setDatabaseEnabled(true);
412 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700413
414 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700415 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700416 settings.setAppCachePath(getAppCachePath());
417 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
418 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700419 // origin policy for file access
420 settings.setAllowUniversalAccessFromFileURLs(false);
421 settings.setAllowFileAccessFromFileURLs(false);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800422
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800423 //if (!(settings instanceof WebSettingsClassic)) return;
424 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800425
426 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
427 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
428 // WebView should be preserving the memory as much as possible.
429 // However, apps like browser wish to turn on the performance mode which
430 // would require more memory.
431 // TODO: We need to dynamically allocate/deallocate temporary memory for
432 // apps which are trying to use minimal memory. Currently, double
433 // buffering is always turned on, which is unnecessary.
434 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
435 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800436 */
John Reck35e9dd62011-04-25 09:01:54 -0700437 }
438
439 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700440 mNeedsSharedSync = false;
Axesh R. Ajmera579c70c2014-04-17 13:24:56 -0700441 CookieManager.getInstance().setAcceptCookie(acceptCookies());
442
John Reck35e9dd62011-04-25 09:01:54 -0700443 if (mController != null) {
444 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700445 }
John Reck35e9dd62011-04-25 09:01:54 -0700446 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700447
John Reck35e9dd62011-04-25 09:01:54 -0700448 private void syncManagedSettings() {
449 syncSharedSettings();
450 synchronized (mManagedSettings) {
451 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
452 while (iter.hasNext()) {
453 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800454 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700455 if (settings == null) {
456 iter.remove();
457 continue;
458 }
459 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000460 }
John Reck35e9dd62011-04-25 09:01:54 -0700461 }
462 }
Ben Murdochef671652010-11-25 17:17:58 +0000463
John Reck35e9dd62011-04-25 09:01:54 -0700464 @Override
465 public void onSharedPreferenceChanged(
466 SharedPreferences sharedPreferences, String key) {
467 syncManagedSettings();
468 if (PREF_SEARCH_ENGINE.equals(key)) {
469 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700470 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200471 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700472 mController.getUi().setFullscreen(useFullscreen());
473 }
Michael Kolb0241e752011-07-07 14:58:50 -0700474 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200475 if (mController != null && mController.getUi() != null) {
Michael Kolb0241e752011-07-07 14:58:50 -0700476 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
477 }
Victoria Lease96497832012-03-23 14:19:56 -0700478 } else if (PREF_LINK_PREFETCH.equals(key)) {
479 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700480 }
John Reck35e9dd62011-04-25 09:01:54 -0700481 }
482
John Reck961d35d2011-06-23 09:45:54 -0700483 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100484 requireInitialization();
485 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700486 }
487
488 public LayoutAlgorithm getLayoutAlgorithm() {
489 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
490 if (autofitPages()) {
491 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
492 }
493 if (isDebugEnabled()) {
494 if (isSmallScreen()) {
495 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000496 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700497 if (isNormalLayout()) {
498 layoutAlgorithm = LayoutAlgorithm.NORMAL;
499 } else {
500 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
501 }
Ben Murdochef671652010-11-25 17:17:58 +0000502 }
John Reck35e9dd62011-04-25 09:01:54 -0700503 }
504 return layoutAlgorithm;
505 }
Ben Murdochef671652010-11-25 17:17:58 +0000506
John Reck35e9dd62011-04-25 09:01:54 -0700507 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700508 requireInitialization();
509 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800510 }
511
John Reck35e9dd62011-04-25 09:01:54 -0700512 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700513 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700514 return mWebStorageSizeManager;
515 }
516
John Reck35e9dd62011-04-25 09:01:54 -0700517 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700518 if (mAppCachePath == null) {
519 mAppCachePath = mContext.getDir("appcache", 0).getPath();
520 }
521 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700522 }
523
524 private void updateSearchEngine(boolean force) {
525 String searchEngineName = getSearchEngineName();
526 if (force || mSearchEngine == null ||
527 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700528 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000529 }
530 }
531
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100532 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700533 if (mSearchEngine == null) {
534 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100535 }
John Reck35e9dd62011-04-25 09:01:54 -0700536 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100537 }
538
John Reck35e9dd62011-04-25 09:01:54 -0700539 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700540 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700541 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100542 }
543
John Reck35e9dd62011-04-25 09:01:54 -0700544 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700545 Editor edit = mPrefs.edit();
546 edit.putBoolean(PREF_DEBUG_MENU, value);
547 if (!value) {
548 // Reset to "safe" value
549 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
550 }
551 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100552 }
553
John Reck35e9dd62011-04-25 09:01:54 -0700554 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800555 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700556 if (mController != null) {
557 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800558 if (current != null) {
559 current.clearCache(true);
560 }
561 }
562 }
563
John Reck35e9dd62011-04-25 09:01:54 -0700564 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800565 CookieManager.getInstance().removeAllCookie();
566 }
567
John Reck35e9dd62011-04-25 09:01:54 -0700568 public void clearHistory() {
569 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800570 Browser.clearHistory(resolver);
571 Browser.clearSearches(resolver);
572 }
573
John Reck35e9dd62011-04-25 09:01:54 -0700574 public void clearFormData() {
575 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700576 if (mController!= null) {
577 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100578 if (currentTopView != null) {
579 currentTopView.clearFormData();
580 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800581 }
582 }
583
John Reck35e9dd62011-04-25 09:01:54 -0700584 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700585 // Clear password store maintained by SWE engine
586 WebSettings settings = null;
587 // find a valid settings object
588 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
589 while (iter.hasNext()) {
590 WeakReference<WebSettings> ref = iter.next();
591 settings = (WebSettings)ref.get();
592 if (settings != null) {
593 break;
594 }
595 }
596 if (settings != null) {
597 settings.clearPasswords();
598 }
599
600 // Clear passwords in WebView database
John Reck35e9dd62011-04-25 09:01:54 -0700601 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800602 db.clearUsernamePassword();
603 db.clearHttpAuthUsernamePassword();
604 }
605
John Reck35e9dd62011-04-25 09:01:54 -0700606 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100607 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100608 }
609
John Reck35e9dd62011-04-25 09:01:54 -0700610 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100611 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100612 }
613
John Reck35e9dd62011-04-25 09:01:54 -0700614 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700615 mPrefs.edit()
616 .clear()
John Reckbd315192011-07-29 10:05:47 -0700617 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200618 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700619 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700620 }
621
Björn Isakssonc885a242012-06-05 17:19:04 +0200622 private void resetCachedValues() {
623 updateSearchEngine(false);
624 }
625
John Reck35e9dd62011-04-25 09:01:54 -0700626 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800627 // query the profile from components autofill database 524
628 if (mAutofillHandler.mAutoFillProfile == null &&
629 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
630 WebSettings settings = null;
631 // find a valid settings object
632 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
633 while (iter.hasNext()) {
634 WeakReference<WebSettings> ref = iter.next();
635 settings = (WebSettings)ref.get();
636 if (settings != null) {
637 break;
638 }
639 }
640 if (settings != null) {
641 AutoFillProfile profile =
642 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
643 mAutofillHandler.setAutoFillProfile(profile);
644 }
645 }
John Reck35e9dd62011-04-25 09:01:54 -0700646 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800647 }
648
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800649 public String getAutoFillProfileId() {
650 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800651 }
652
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800653 public void updateAutoFillProfile(AutoFillProfile profile) {
654 syncAutoFillProfile(profile);
655 }
656
657 private void syncAutoFillProfile(AutoFillProfile profile) {
658 synchronized (mManagedSettings) {
659 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
660 while (iter.hasNext()) {
661 WeakReference<WebSettings> ref = iter.next();
662 WebSettings settings = (WebSettings)ref.get();
663 if (settings == null) {
664 iter.remove();
665 continue;
666 }
667 // update the profile only once.
668 settings.setAutoFillProfile(profile);
669 // Now we should have the guid
670 mAutofillHandler.setAutoFillProfile(profile);
671 break;
672 }
673 }
674 }
John Reck35e9dd62011-04-25 09:01:54 -0700675 public void toggleDebugSettings() {
676 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800677 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100678
John Reckb8b2af82011-05-20 15:58:33 -0700679 public boolean hasDesktopUseragent(WebView view) {
680 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
681 }
682
683 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700684 if (view == null) {
685 return;
686 }
John Reckb8b2af82011-05-20 15:58:33 -0700687 WebSettings settings = view.getSettings();
688 if (mCustomUserAgents.get(settings) != null) {
689 mCustomUserAgents.remove(settings);
690 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
691 } else {
692 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
693 settings.setUserAgentString(DESKTOP_USERAGENT);
694 }
695 }
696
John Reck7dc444b2011-06-16 17:44:29 -0700697 public static int getAdjustedMinimumFontSize(int rawValue) {
698 rawValue++; // Preference starts at 0, min font at 1
699 if (rawValue > 1) {
700 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
701 }
702 return rawValue;
703 }
704
John Reck5ba3c762011-09-14 15:00:15 -0700705 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700706 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700707 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700708 }
709
710 static int getRawTextZoom(int percent) {
711 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
712 }
713
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700714 public int getAdjustedDoubleTapZoom(int rawValue) {
715 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
716 return (int) ((rawValue + 100) * mFontSizeMult);
717 }
718
719 static int getRawDoubleTapZoom(int percent) {
720 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
721 }
722
John Reckcadae722011-07-25 11:36:17 -0700723 public SharedPreferences getPreferences() {
724 return mPrefs;
725 }
726
Victoria Lease96497832012-03-23 14:19:56 -0700727 // update connectivity-dependent options
728 public void updateConnectionType() {
729 ConnectivityManager cm = (ConnectivityManager)
730 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
731 String linkPrefetchPreference = getLinkPrefetchEnabled();
732 boolean linkPrefetchAllowed = linkPrefetchPreference.
733 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
734 NetworkInfo ni = cm.getActiveNetworkInfo();
735 if (ni != null) {
736 switch (ni.getType()) {
737 case ConnectivityManager.TYPE_WIFI:
738 case ConnectivityManager.TYPE_ETHERNET:
739 case ConnectivityManager.TYPE_BLUETOOTH:
740 linkPrefetchAllowed |= linkPrefetchPreference.
741 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
742 break;
743 case ConnectivityManager.TYPE_MOBILE:
744 case ConnectivityManager.TYPE_MOBILE_DUN:
745 case ConnectivityManager.TYPE_MOBILE_MMS:
746 case ConnectivityManager.TYPE_MOBILE_SUPL:
747 case ConnectivityManager.TYPE_WIMAX:
748 default:
749 break;
750 }
751 }
752 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
753 mLinkPrefetchAllowed = linkPrefetchAllowed;
754 syncManagedSettings();
755 }
756 }
757
luxiaol62677b02013-07-22 07:54:49 +0800758 public String getDownloadPath() {
759 return mPrefs.getString(PREF_DOWNLOAD_PATH,
760 DownloadHandler.getDefaultDownloadPath(mContext));
761 }
John Reck35e9dd62011-04-25 09:01:54 -0700762 // -----------------------------
763 // getter/setters for accessibility_preferences.xml
764 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100765
John Reck7dc444b2011-06-16 17:44:29 -0700766 @Deprecated
767 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700768 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
769 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100770 }
771
John Reck35e9dd62011-04-25 09:01:54 -0700772 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700773 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700774 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100775 }
776
John Reck92f25f82011-04-26 16:57:10 -0700777 public boolean forceEnableUserScalable() {
778 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
779 }
780
John Reck7dc444b2011-06-16 17:44:29 -0700781 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700782 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700783 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
784 return getAdjustedTextZoom(textZoom);
785 }
786
787 public void setTextZoom(int percent) {
788 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
789 }
790
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700791 public int getDoubleTapZoom() {
792 requireInitialization();
793 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
794 return getAdjustedDoubleTapZoom(doubleTapZoom);
795 }
796
797 public void setDoubleTapZoom(int percent) {
798 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
799 }
800
John Reck35e9dd62011-04-25 09:01:54 -0700801 // -----------------------------
802 // getter/setters for advanced_preferences.xml
803 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100804
John Reck35e9dd62011-04-25 09:01:54 -0700805 public String getSearchEngineName() {
luxiaol221b3932013-07-19 15:27:57 +0800806 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
807 if (defaultSearchEngineValue == null) {
808 defaultSearchEngineValue = SearchEngine.GOOGLE;
809 }
810 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100811 }
John Reck63bb6872010-12-01 19:29:32 -0800812
Michael Kolb8d772b02012-01-19 13:56:00 -0800813 public boolean allowAppTabs() {
814 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
815 }
816
John Reck35e9dd62011-04-25 09:01:54 -0700817 public boolean openInBackground() {
818 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800819 }
John Reck35e9dd62011-04-25 09:01:54 -0700820
821 public boolean enableJavascript() {
822 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
823 }
824
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800825 public boolean enableMemoryMonitor() {
826 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
827 }
828
John Reck35e9dd62011-04-25 09:01:54 -0700829 // TODO: Cache
830 public PluginState getPluginState() {
831 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
832 return PluginState.valueOf(state);
833 }
834
835 // TODO: Cache
836 public ZoomDensity getDefaultZoom() {
837 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700838 ZoomDensity zoomDensity = ZoomDensity.valueOf(zoom);
839 //SWE: If zoomDensity is not MEDIUM, set enableWideViewport to false.
840 if(zoomDensity != ZoomDensity.MEDIUM)
841 enableWideViewport = false;
842 else
843 enableWideViewport = true;
844 return zoomDensity;
John Reck35e9dd62011-04-25 09:01:54 -0700845 }
846
847 public boolean loadPageInOverviewMode() {
848 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
849 }
850
851 public boolean autofitPages() {
852 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
853 }
854
855 public boolean blockPopupWindows() {
856 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
857 }
858
859 public boolean loadImages() {
860 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
861 }
862
863 public String getDefaultTextEncoding() {
864 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
865 }
866
867 // -----------------------------
868 // getter/setters for general_preferences.xml
869 // -----------------------------
870
871 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700872 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
873 }
874
875 public void setHomePage(String value) {
876 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
877 }
878
879 public boolean isAutofillEnabled() {
880 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
881 }
882
883 public void setAutofillEnabled(boolean value) {
884 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
885 }
886
887 // -----------------------------
888 // getter/setters for debug_preferences.xml
889 // -----------------------------
890
891 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700892 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700893 return true;
894 }
895 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
896 }
897
Derek Sollenberger31adf672011-07-08 11:31:30 -0400898 public boolean isSkiaHardwareAccelerated() {
899 if (!isDebugEnabled()) {
900 return false;
901 }
902 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
903 }
904
John Reck35e9dd62011-04-25 09:01:54 -0700905 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700906 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700907 return 0;
908 }
909 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
910 }
911
912 // -----------------------------
913 // getter/setters for hidden_debug_preferences.xml
914 // -----------------------------
915
916 public boolean enableVisualIndicator() {
917 if (!isDebugEnabled()) {
918 return false;
919 }
920 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
921 }
922
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700923 public boolean enableCpuUploadPath() {
924 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700925 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700926 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700927 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700928 }
929
John Reck35e9dd62011-04-25 09:01:54 -0700930 public boolean enableJavascriptConsole() {
931 if (!isDebugEnabled()) {
932 return false;
933 }
934 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
935 }
936
937 public boolean isSmallScreen() {
938 if (!isDebugEnabled()) {
939 return false;
940 }
941 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
942 }
943
944 public boolean isWideViewport() {
945 if (!isDebugEnabled()) {
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700946 return enableWideViewport;
John Reck35e9dd62011-04-25 09:01:54 -0700947 }
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700948 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, enableWideViewport);
John Reck35e9dd62011-04-25 09:01:54 -0700949 }
950
951 public boolean isNormalLayout() {
952 if (!isDebugEnabled()) {
953 return false;
954 }
955 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
956 }
957
958 public boolean isTracing() {
959 if (!isDebugEnabled()) {
960 return false;
961 }
962 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
963 }
964
965 public boolean enableLightTouch() {
966 if (!isDebugEnabled()) {
967 return false;
968 }
969 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
970 }
971
972 public boolean enableNavDump() {
973 if (!isDebugEnabled()) {
974 return false;
975 }
976 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
977 }
978
979 public String getJsEngineFlags() {
980 if (!isDebugEnabled()) {
981 return "";
982 }
983 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
984 }
985
986 // -----------------------------
987 // getter/setters for lab_preferences.xml
988 // -----------------------------
989
990 public boolean useQuickControls() {
991 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
992 }
993
994 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700995 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700996 }
997
Michael Kolbc38c6042011-04-27 10:46:06 -0700998 public boolean useFullscreen() {
999 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
1000 }
1001
John Reck2fd9d0e2011-07-15 11:13:48 -07001002 public boolean useInvertedRendering() {
1003 return mPrefs.getBoolean(PREF_INVERTED, false);
1004 }
1005
Nicolas Roard5d513102011-08-03 15:35:34 -07001006 public float getInvertedContrast() {
1007 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
1008 }
1009
John Reck35e9dd62011-04-25 09:01:54 -07001010 // -----------------------------
1011 // getter/setters for privacy_security_preferences.xml
1012 // -----------------------------
1013
1014 public boolean showSecurityWarnings() {
1015 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
1016 }
1017
1018 public boolean acceptCookies() {
1019 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
1020 }
1021
1022 public boolean saveFormdata() {
1023 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
1024 }
1025
1026 public boolean enableGeolocation() {
1027 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
1028 }
1029
1030 public boolean rememberPasswords() {
1031 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
1032 }
1033
Michael Kolb14612442011-06-24 13:06:29 -07001034 // -----------------------------
1035 // getter/setters for bandwidth_preferences.xml
1036 // -----------------------------
1037
Mathew Inwood467813f2011-09-02 15:43:17 +01001038 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
1039 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -07001040 }
Mathew Inwood467813f2011-09-02 15:43:17 +01001041
1042 public static String getPreloadAlwaysPreferenceString(Context context) {
1043 return context.getResources().getString(R.string.pref_data_preload_value_always);
1044 }
1045
Mathew Inwood825fba72011-10-04 14:52:52 +01001046 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1047 "browser_default_preload_setting";
1048
1049 public String getDefaultPreloadSetting() {
1050 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1051 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1052 if (preload == null) {
1053 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1054 }
1055 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001056 }
1057
1058 public String getPreloadEnabled() {
1059 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1060 }
1061
Victoria Lease96497832012-03-23 14:19:56 -07001062 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1063 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1064 }
1065
1066 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1067 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1068 }
1069
1070 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1071 "browser_default_link_prefetch_setting";
1072
1073 public String getDefaultLinkPrefetchSetting() {
1074 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1075 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1076 if (preload == null) {
1077 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1078 }
1079 return preload;
1080 }
1081
1082 public String getLinkPrefetchEnabled() {
1083 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1084 }
1085
George Mount3636d0a2011-11-21 09:08:21 -08001086 // -----------------------------
1087 // getter/setters for browser recovery
1088 // -----------------------------
1089 /**
1090 * The last time browser was started.
1091 * @return The last browser start time as System.currentTimeMillis. This
1092 * can be 0 if this is the first time or the last tab was closed.
1093 */
1094 public long getLastRecovered() {
1095 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1096 }
1097
1098 /**
1099 * Sets the last browser start time.
1100 * @param time The last time as System.currentTimeMillis that the browser
1101 * was started. This should be set to 0 if the last tab is closed.
1102 */
1103 public void setLastRecovered(long time) {
1104 mPrefs.edit()
1105 .putLong(KEY_LAST_RECOVERED, time)
1106 .apply();
1107 }
1108
1109 /**
1110 * Used to determine whether or not the previous browser run crashed. Once
1111 * the previous state has been determined, the value will be set to false
1112 * until a pause is received.
1113 * @return true if the last browser run was paused or false if it crashed.
1114 */
1115 public boolean wasLastRunPaused() {
1116 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1117 }
1118
1119 /**
1120 * Sets whether or not the last run was a pause or crash.
1121 * @param isPaused Set to true When a pause is received or false after
1122 * resuming.
1123 */
1124 public void setLastRunPaused(boolean isPaused) {
1125 mPrefs.edit()
1126 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1127 .apply();
1128 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001129}