blob: e3f31c0c4d10316161500eff470f33a4075014f5 [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;
John Reck78a6a1d2011-07-21 13:54:03 -0700124
Victoria Lease96497832012-03-23 14:19:56 -0700125 // Current state of network-dependent settings
126 private boolean mLinkPrefetchAllowed = true;
127
John Reck78a6a1d2011-07-21 13:54:03 -0700128 // Cached values
129 private int mPageCacheCapacity = 1;
130 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800131
John Reck35e9dd62011-04-25 09:01:54 -0700132 // Cached settings
133 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800134
Ben Murdochaaa1f372011-07-25 15:40:58 +0100135 private static String sFactoryResetUrl;
136
qqzhou8c5b0a32013-07-22 15:31:03 +0800137 // add for carrier feature
138 private static Context sResPackageCtx;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800139
140 //Determine if WebView is Initialized or not
141 private boolean mWebViewInitialized;
qqzhou8c5b0a32013-07-22 15:31:03 +0800142
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700143 public static void initialize(final Context context) {
John Reck35e9dd62011-04-25 09:01:54 -0700144 sInstance = new BrowserSettings(context);
145 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700147 public static BrowserSettings getInstance() {
John Reck35e9dd62011-04-25 09:01:54 -0700148 return sInstance;
149 }
Ben Murdochef671652010-11-25 17:17:58 +0000150
John Reck35e9dd62011-04-25 09:01:54 -0700151 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100152 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700153 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
154 mAutofillHandler = new AutofillHandler(mContext);
155 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700156 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
qqzhou8c5b0a32013-07-22 15:31:03 +0800157
158 // add for carrier feature
159 try {
160 sResPackageCtx = context.createPackageContext(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700161 "com.android.browser.res",
qqzhou8c5b0a32013-07-22 15:31:03 +0800162 Context.CONTEXT_IGNORE_SECURITY);
163 } catch (Exception e) {
164 Log.e("Res_Update", "Create Res Apk Failed");
165 }
John Reckcadae722011-07-25 11:36:17 -0700166 BackgroundHandler.execute(mSetup);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800167 mWebViewInitialized = false;
John Reck35e9dd62011-04-25 09:01:54 -0700168 }
169
170 public void setController(Controller controller) {
171 mController = controller;
John Reckc477e712011-08-17 11:27:15 -0700172 if (sInitialized) {
173 syncSharedSettings();
174 }
Ben Murdochef671652010-11-25 17:17:58 +0000175 }
176
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800177 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800178
John Reckc477e712011-08-17 11:27:15 -0700179 if (mNeedsSharedSync) {
180 syncSharedSettings();
181 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800182
John Reck35e9dd62011-04-25 09:01:54 -0700183 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800184 syncStaticSettings(settings);
185 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700186 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800187 }
188 }
189
John Reckd1d87312012-03-08 13:25:00 -0800190 public void stopManagingSettings(WebSettings settings) {
191 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
192 while (iter.hasNext()) {
193 WeakReference<WebSettings> ref = iter.next();
194 if (ref.get() == settings) {
195 iter.remove();
196 return;
197 }
198 }
199 }
200
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800201 public void initializeCookieSettings() {
202 CookieManager.getInstance().setAcceptCookie(acceptCookies());
203 mWebViewInitialized = true;
204 }
John Reckcadae722011-07-25 11:36:17 -0700205 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700206
207 @Override
208 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700209 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
210 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700211 // the cost of one cached page is ~3M (measured using nytimes.com). For
212 // low end devices, we only cache one page. For high end devices, we try
213 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800214
215 // SWE_TODO : assume a high-memory device
216 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700217 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800218 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700219 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
220 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
221 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700222 // Workaround b/5254577
223 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700224 if (Build.VERSION.CODENAME.equals("REL")) {
225 // This is a release build, always startup with debug disabled
226 setDebugEnabled(false);
227 }
228 if (mPrefs.contains(PREF_TEXT_SIZE)) {
229 /*
230 * Update from TextSize enum to zoom percent
231 * SMALLEST is 50%
232 * SMALLER is 75%
233 * NORMAL is 100%
234 * LARGER is 150%
235 * LARGEST is 200%
236 */
237 switch (getTextSize()) {
238 case SMALLEST:
239 setTextZoom(50);
240 break;
241 case SMALLER:
242 setTextZoom(75);
243 break;
244 case LARGER:
245 setTextZoom(150);
246 break;
247 case LARGEST:
248 setTextZoom(200);
249 break;
250 }
251 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
252 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100253
qqzhou8c5b0a32013-07-22 15:31:03 +0800254 // add for carrier homepage feature
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700255 String browserRes = mContext.getResources().getString(R.string.config_carrier_resource);
kaiyiz08f9e2b2013-08-16 09:15:48 +0800256 if ("cu".equals(browserRes) || "cmcc".equals(browserRes)) {
qqzhou8c5b0a32013-07-22 15:31:03 +0800257 int resID = sResPackageCtx.getResources().getIdentifier(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700258 "homepage_base", "string", "com.android.browser.res");
qqzhou8c5b0a32013-07-22 15:31:03 +0800259 sFactoryResetUrl = sResPackageCtx.getResources().getString(resID);
260 } else if ("ct".equals(browserRes)) {
261 int resID = sResPackageCtx.getResources().getIdentifier(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700262 "homepage_base", "string", "com.android.browser.res");
qqzhou8c5b0a32013-07-22 15:31:03 +0800263 sFactoryResetUrl = sResPackageCtx.getResources().getString(resID);
264
265 int pathID = sResPackageCtx.getResources().getIdentifier(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700266 "homepage_path", "string", "com.android.browser.res");
qqzhou8c5b0a32013-07-22 15:31:03 +0800267 String path = sResPackageCtx.getResources().getString(pathID);
268 Locale locale = Locale.getDefault();
269 path = path.replace("%y", locale.getLanguage().toLowerCase());
270 path = path.replace("%z", '_'+locale.getCountry().toLowerCase());
271 boolean useCountry = true;
272 boolean useLanguage = true;
273 InputStream is = null;
274 AssetManager am = mContext.getAssets();
275 try {
276 is = am.open(path);
277 } catch (Exception ignored) {
278 useCountry = false;
279 path = sResPackageCtx.getResources().getString(pathID);
280 path = path.replace("%y", locale.getLanguage().toLowerCase());
281 path = path.replace("%z", "");
282 try {
283 is = am.open(path);
284 } catch (Exception ignoredlanguage) {
285 useLanguage = false;
286 }
287 } finally {
288 if (is != null) {
289 try {
290 is.close();
291 } catch (Exception ignored) {}
292 }
293 }
294
295 if (!useCountry && !useLanguage) {
296 sFactoryResetUrl = sFactoryResetUrl.replace("%y%z", "en");
297 } else {
298 sFactoryResetUrl = sFactoryResetUrl.replace("%y",
299 locale.getLanguage().toLowerCase());
300 sFactoryResetUrl = sFactoryResetUrl.replace("%z", useCountry ?
301 '_' + locale.getCountry().toLowerCase() : "");
302 }
303 } else {
304 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
305 }
306
kaiyizbf086ea2013-08-02 11:03:58 +0800307 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
308 if (!"default".equals(browserRes)) {
309 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING,
Vivek Sekhar22bd57b2014-04-05 22:19:43 -0700310 "UTF-8").apply();
kaiyizbf086ea2013-08-02 11:03:58 +0800311 }
312 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100313 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
314 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
315 BrowserProvider.getClientId(mContext.getContentResolver()));
316 }
317
Ben Murdochaaa1f372011-07-25 15:40:58 +0100318 synchronized (BrowserSettings.class) {
319 sInitialized = true;
320 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700321 }
322 }
323 };
324
Ben Murdochaaa1f372011-07-25 15:40:58 +0100325 private static void requireInitialization() {
326 synchronized (BrowserSettings.class) {
327 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700328 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100329 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700330 } catch (InterruptedException e) {
331 }
332 }
333 }
334 }
335
The Android Open Source Project0c908882009-03-03 19:32:16 -0800336 /**
John Reck35e9dd62011-04-25 09:01:54 -0700337 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800338 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800339 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700340 settings.setGeolocationEnabled(enableGeolocation());
341 settings.setJavaScriptEnabled(enableJavascript());
342 settings.setLightTouchEnabled(enableLightTouch());
343 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700344 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
345 settings.setDefaultZoom(getDefaultZoom());
346 settings.setMinimumFontSize(getMinimumFontSize());
347 settings.setMinimumLogicalFontSize(getMinimumFontSize());
348 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700349 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700350 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100351 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700352 settings.setLoadsImagesAutomatically(loadImages());
353 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
354 settings.setSavePassword(rememberPasswords());
355 settings.setSaveFormData(saveFormdata());
356 settings.setUseWideViewPort(isWideViewport());
John Reckb8b2af82011-05-20 15:58:33 -0700357
qqzhoue6ff8b42013-07-23 17:28:48 +0800358 // add for carrier useragent feature
359 String ua = null;
Bijan Amirzada58383e72014-04-01 14:45:22 -0700360 Object objUserAgentHandler = ReflectHelper.newObject(
361 "com.qrd.useragent.UserAgentHandler", null, null);
362 Object[] params = {mContext};
363 Class[] type = new Class[] {Context.class};
364 ua = (String) ReflectHelper.invokeMethod(objUserAgentHandler,"getUAString",
365 type, params);
366 if (ua == null)
qqzhoue6ff8b42013-07-23 17:28:48 +0800367 ua = mCustomUserAgents.get(settings);
Bijan Amirzada58383e72014-04-01 14:45:22 -0700368
369 if (ua != null){
John Reckb8b2af82011-05-20 15:58:33 -0700370 settings.setUserAgentString(ua);
371 } else {
372 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
373 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700374
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800375 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800376 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
377 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
378 settingsClassic.setForceUserScalable(forceEnableUserScalable());
379 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
380 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800381
John Reckea17e782011-10-27 17:15:59 -0700382 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800383 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700384 useInverted ? "true" : "false");
385 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800386 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700387 Float.toString(getInvertedContrast()));
388 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700389
John Reckea17e782011-10-27 17:15:59 -0700390 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800391 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700392 enableCpuUploadPath() ? "true" : "false");
393 }
Victoria Lease96497832012-03-23 14:19:56 -0700394
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800395 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000396 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800397
John Reck35e9dd62011-04-25 09:01:54 -0700398 /**
399 * Syncs all the settings that have no UI
400 * These cannot change, so we only need to set them once per WebSettings
401 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800402 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700403 settings.setDefaultFontSize(16);
404 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000405
John Reck35e9dd62011-04-25 09:01:54 -0700406 // WebView inside Browser doesn't want initial focus to be set.
407 settings.setNeedInitialFocus(false);
408 // Browser supports multiple windows
409 settings.setSupportMultipleWindows(true);
410 // enable smooth transition for better performance during panning or
411 // zooming
412 settings.setEnableSmoothTransition(true);
413 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700414 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700415
416 // HTML5 API flags
417 settings.setAppCacheEnabled(true);
418 settings.setDatabaseEnabled(true);
419 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700420
421 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700422 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700423 settings.setAppCachePath(getAppCachePath());
424 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
425 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700426 // origin policy for file access
427 settings.setAllowUniversalAccessFromFileURLs(false);
428 settings.setAllowFileAccessFromFileURLs(false);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800429
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800430 //if (!(settings instanceof WebSettingsClassic)) return;
431 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800432
433 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
434 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
435 // WebView should be preserving the memory as much as possible.
436 // However, apps like browser wish to turn on the performance mode which
437 // would require more memory.
438 // TODO: We need to dynamically allocate/deallocate temporary memory for
439 // apps which are trying to use minimal memory. Currently, double
440 // buffering is always turned on, which is unnecessary.
441 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
442 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800443 */
John Reck35e9dd62011-04-25 09:01:54 -0700444 }
445
446 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700447 mNeedsSharedSync = false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800448 if (mWebViewInitialized) {
449 CookieManager.getInstance().setAcceptCookie(acceptCookies());
450 }
John Reck35e9dd62011-04-25 09:01:54 -0700451 if (mController != null) {
452 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700453 }
John Reck35e9dd62011-04-25 09:01:54 -0700454 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700455
John Reck35e9dd62011-04-25 09:01:54 -0700456 private void syncManagedSettings() {
457 syncSharedSettings();
458 synchronized (mManagedSettings) {
459 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
460 while (iter.hasNext()) {
461 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800462 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700463 if (settings == null) {
464 iter.remove();
465 continue;
466 }
467 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000468 }
John Reck35e9dd62011-04-25 09:01:54 -0700469 }
470 }
Ben Murdochef671652010-11-25 17:17:58 +0000471
John Reck35e9dd62011-04-25 09:01:54 -0700472 @Override
473 public void onSharedPreferenceChanged(
474 SharedPreferences sharedPreferences, String key) {
475 syncManagedSettings();
476 if (PREF_SEARCH_ENGINE.equals(key)) {
477 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700478 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200479 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700480 mController.getUi().setFullscreen(useFullscreen());
481 }
Michael Kolb0241e752011-07-07 14:58:50 -0700482 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200483 if (mController != null && mController.getUi() != null) {
Michael Kolb0241e752011-07-07 14:58:50 -0700484 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
485 }
Victoria Lease96497832012-03-23 14:19:56 -0700486 } else if (PREF_LINK_PREFETCH.equals(key)) {
487 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700488 }
John Reck35e9dd62011-04-25 09:01:54 -0700489 }
490
John Reck961d35d2011-06-23 09:45:54 -0700491 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100492 requireInitialization();
493 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700494 }
495
496 public LayoutAlgorithm getLayoutAlgorithm() {
497 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
498 if (autofitPages()) {
499 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
500 }
501 if (isDebugEnabled()) {
502 if (isSmallScreen()) {
503 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000504 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700505 if (isNormalLayout()) {
506 layoutAlgorithm = LayoutAlgorithm.NORMAL;
507 } else {
508 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
509 }
Ben Murdochef671652010-11-25 17:17:58 +0000510 }
John Reck35e9dd62011-04-25 09:01:54 -0700511 }
512 return layoutAlgorithm;
513 }
Ben Murdochef671652010-11-25 17:17:58 +0000514
John Reck35e9dd62011-04-25 09:01:54 -0700515 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700516 requireInitialization();
517 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800518 }
519
John Reck35e9dd62011-04-25 09:01:54 -0700520 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700521 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700522 return mWebStorageSizeManager;
523 }
524
John Reck35e9dd62011-04-25 09:01:54 -0700525 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700526 if (mAppCachePath == null) {
527 mAppCachePath = mContext.getDir("appcache", 0).getPath();
528 }
529 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700530 }
531
532 private void updateSearchEngine(boolean force) {
533 String searchEngineName = getSearchEngineName();
534 if (force || mSearchEngine == null ||
535 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700536 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000537 }
538 }
539
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100540 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700541 if (mSearchEngine == null) {
542 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100543 }
John Reck35e9dd62011-04-25 09:01:54 -0700544 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100545 }
546
John Reck35e9dd62011-04-25 09:01:54 -0700547 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700548 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700549 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100550 }
551
John Reck35e9dd62011-04-25 09:01:54 -0700552 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700553 Editor edit = mPrefs.edit();
554 edit.putBoolean(PREF_DEBUG_MENU, value);
555 if (!value) {
556 // Reset to "safe" value
557 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
558 }
559 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100560 }
561
John Reck35e9dd62011-04-25 09:01:54 -0700562 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800563 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700564 if (mController != null) {
565 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800566 if (current != null) {
567 current.clearCache(true);
568 }
569 }
570 }
571
John Reck35e9dd62011-04-25 09:01:54 -0700572 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800573 CookieManager.getInstance().removeAllCookie();
574 }
575
John Reck35e9dd62011-04-25 09:01:54 -0700576 public void clearHistory() {
577 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800578 Browser.clearHistory(resolver);
579 Browser.clearSearches(resolver);
580 }
581
John Reck35e9dd62011-04-25 09:01:54 -0700582 public void clearFormData() {
583 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700584 if (mController!= null) {
585 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100586 if (currentTopView != null) {
587 currentTopView.clearFormData();
588 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800589 }
590 }
591
John Reck35e9dd62011-04-25 09:01:54 -0700592 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700593 // Clear password store maintained by SWE engine
594 WebSettings settings = null;
595 // find a valid settings object
596 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
597 while (iter.hasNext()) {
598 WeakReference<WebSettings> ref = iter.next();
599 settings = (WebSettings)ref.get();
600 if (settings != null) {
601 break;
602 }
603 }
604 if (settings != null) {
605 settings.clearPasswords();
606 }
607
608 // Clear passwords in WebView database
John Reck35e9dd62011-04-25 09:01:54 -0700609 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800610 db.clearUsernamePassword();
611 db.clearHttpAuthUsernamePassword();
612 }
613
John Reck35e9dd62011-04-25 09:01:54 -0700614 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100615 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100616 }
617
John Reck35e9dd62011-04-25 09:01:54 -0700618 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100619 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100620 }
621
John Reck35e9dd62011-04-25 09:01:54 -0700622 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700623 mPrefs.edit()
624 .clear()
John Reckbd315192011-07-29 10:05:47 -0700625 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200626 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700627 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700628 }
629
Björn Isakssonc885a242012-06-05 17:19:04 +0200630 private void resetCachedValues() {
631 updateSearchEngine(false);
632 }
633
John Reck35e9dd62011-04-25 09:01:54 -0700634 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800635 // query the profile from components autofill database 524
636 if (mAutofillHandler.mAutoFillProfile == null &&
637 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
638 WebSettings settings = null;
639 // find a valid settings object
640 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
641 while (iter.hasNext()) {
642 WeakReference<WebSettings> ref = iter.next();
643 settings = (WebSettings)ref.get();
644 if (settings != null) {
645 break;
646 }
647 }
648 if (settings != null) {
649 AutoFillProfile profile =
650 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
651 mAutofillHandler.setAutoFillProfile(profile);
652 }
653 }
John Reck35e9dd62011-04-25 09:01:54 -0700654 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800655 }
656
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800657 public String getAutoFillProfileId() {
658 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800659 }
660
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800661 public void updateAutoFillProfile(AutoFillProfile profile) {
662 syncAutoFillProfile(profile);
663 }
664
665 private void syncAutoFillProfile(AutoFillProfile profile) {
666 synchronized (mManagedSettings) {
667 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
668 while (iter.hasNext()) {
669 WeakReference<WebSettings> ref = iter.next();
670 WebSettings settings = (WebSettings)ref.get();
671 if (settings == null) {
672 iter.remove();
673 continue;
674 }
675 // update the profile only once.
676 settings.setAutoFillProfile(profile);
677 // Now we should have the guid
678 mAutofillHandler.setAutoFillProfile(profile);
679 break;
680 }
681 }
682 }
John Reck35e9dd62011-04-25 09:01:54 -0700683 public void toggleDebugSettings() {
684 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800685 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100686
John Reckb8b2af82011-05-20 15:58:33 -0700687 public boolean hasDesktopUseragent(WebView view) {
688 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
689 }
690
691 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700692 if (view == null) {
693 return;
694 }
John Reckb8b2af82011-05-20 15:58:33 -0700695 WebSettings settings = view.getSettings();
696 if (mCustomUserAgents.get(settings) != null) {
697 mCustomUserAgents.remove(settings);
698 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
699 } else {
700 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
701 settings.setUserAgentString(DESKTOP_USERAGENT);
702 }
703 }
704
John Reck7dc444b2011-06-16 17:44:29 -0700705 public static int getAdjustedMinimumFontSize(int rawValue) {
706 rawValue++; // Preference starts at 0, min font at 1
707 if (rawValue > 1) {
708 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
709 }
710 return rawValue;
711 }
712
John Reck5ba3c762011-09-14 15:00:15 -0700713 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700714 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700715 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700716 }
717
718 static int getRawTextZoom(int percent) {
719 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
720 }
721
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700722 public int getAdjustedDoubleTapZoom(int rawValue) {
723 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
724 return (int) ((rawValue + 100) * mFontSizeMult);
725 }
726
727 static int getRawDoubleTapZoom(int percent) {
728 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
729 }
730
John Reckcadae722011-07-25 11:36:17 -0700731 public SharedPreferences getPreferences() {
732 return mPrefs;
733 }
734
Victoria Lease96497832012-03-23 14:19:56 -0700735 // update connectivity-dependent options
736 public void updateConnectionType() {
737 ConnectivityManager cm = (ConnectivityManager)
738 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
739 String linkPrefetchPreference = getLinkPrefetchEnabled();
740 boolean linkPrefetchAllowed = linkPrefetchPreference.
741 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
742 NetworkInfo ni = cm.getActiveNetworkInfo();
743 if (ni != null) {
744 switch (ni.getType()) {
745 case ConnectivityManager.TYPE_WIFI:
746 case ConnectivityManager.TYPE_ETHERNET:
747 case ConnectivityManager.TYPE_BLUETOOTH:
748 linkPrefetchAllowed |= linkPrefetchPreference.
749 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
750 break;
751 case ConnectivityManager.TYPE_MOBILE:
752 case ConnectivityManager.TYPE_MOBILE_DUN:
753 case ConnectivityManager.TYPE_MOBILE_MMS:
754 case ConnectivityManager.TYPE_MOBILE_SUPL:
755 case ConnectivityManager.TYPE_WIMAX:
756 default:
757 break;
758 }
759 }
760 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
761 mLinkPrefetchAllowed = linkPrefetchAllowed;
762 syncManagedSettings();
763 }
764 }
765
luxiaol62677b02013-07-22 07:54:49 +0800766 public String getDownloadPath() {
767 return mPrefs.getString(PREF_DOWNLOAD_PATH,
768 DownloadHandler.getDefaultDownloadPath(mContext));
769 }
John Reck35e9dd62011-04-25 09:01:54 -0700770 // -----------------------------
771 // getter/setters for accessibility_preferences.xml
772 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100773
John Reck7dc444b2011-06-16 17:44:29 -0700774 @Deprecated
775 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700776 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
777 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100778 }
779
John Reck35e9dd62011-04-25 09:01:54 -0700780 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700781 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700782 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100783 }
784
John Reck92f25f82011-04-26 16:57:10 -0700785 public boolean forceEnableUserScalable() {
786 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
787 }
788
John Reck7dc444b2011-06-16 17:44:29 -0700789 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700790 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700791 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
792 return getAdjustedTextZoom(textZoom);
793 }
794
795 public void setTextZoom(int percent) {
796 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
797 }
798
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700799 public int getDoubleTapZoom() {
800 requireInitialization();
801 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
802 return getAdjustedDoubleTapZoom(doubleTapZoom);
803 }
804
805 public void setDoubleTapZoom(int percent) {
806 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
807 }
808
John Reck35e9dd62011-04-25 09:01:54 -0700809 // -----------------------------
810 // getter/setters for advanced_preferences.xml
811 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100812
John Reck35e9dd62011-04-25 09:01:54 -0700813 public String getSearchEngineName() {
luxiaol221b3932013-07-19 15:27:57 +0800814 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
815 if (defaultSearchEngineValue == null) {
816 defaultSearchEngineValue = SearchEngine.GOOGLE;
817 }
818 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100819 }
John Reck63bb6872010-12-01 19:29:32 -0800820
Michael Kolb8d772b02012-01-19 13:56:00 -0800821 public boolean allowAppTabs() {
822 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
823 }
824
John Reck35e9dd62011-04-25 09:01:54 -0700825 public boolean openInBackground() {
826 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800827 }
John Reck35e9dd62011-04-25 09:01:54 -0700828
829 public boolean enableJavascript() {
830 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
831 }
832
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800833 public boolean enableMemoryMonitor() {
834 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
835 }
836
John Reck35e9dd62011-04-25 09:01:54 -0700837 // TODO: Cache
838 public PluginState getPluginState() {
839 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
840 return PluginState.valueOf(state);
841 }
842
843 // TODO: Cache
844 public ZoomDensity getDefaultZoom() {
845 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
846 return ZoomDensity.valueOf(zoom);
847 }
848
849 public boolean loadPageInOverviewMode() {
850 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
851 }
852
853 public boolean autofitPages() {
854 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
855 }
856
857 public boolean blockPopupWindows() {
858 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
859 }
860
861 public boolean loadImages() {
862 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
863 }
864
865 public String getDefaultTextEncoding() {
866 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
867 }
868
869 // -----------------------------
870 // getter/setters for general_preferences.xml
871 // -----------------------------
872
873 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700874 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
875 }
876
877 public void setHomePage(String value) {
878 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
879 }
880
881 public boolean isAutofillEnabled() {
882 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
883 }
884
885 public void setAutofillEnabled(boolean value) {
886 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
887 }
888
889 // -----------------------------
890 // getter/setters for debug_preferences.xml
891 // -----------------------------
892
893 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700894 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700895 return true;
896 }
897 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
898 }
899
Derek Sollenberger31adf672011-07-08 11:31:30 -0400900 public boolean isSkiaHardwareAccelerated() {
901 if (!isDebugEnabled()) {
902 return false;
903 }
904 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
905 }
906
John Reck35e9dd62011-04-25 09:01:54 -0700907 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700908 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700909 return 0;
910 }
911 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
912 }
913
914 // -----------------------------
915 // getter/setters for hidden_debug_preferences.xml
916 // -----------------------------
917
918 public boolean enableVisualIndicator() {
919 if (!isDebugEnabled()) {
920 return false;
921 }
922 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
923 }
924
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700925 public boolean enableCpuUploadPath() {
926 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700927 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700928 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700929 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700930 }
931
John Reck35e9dd62011-04-25 09:01:54 -0700932 public boolean enableJavascriptConsole() {
933 if (!isDebugEnabled()) {
934 return false;
935 }
936 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
937 }
938
939 public boolean isSmallScreen() {
940 if (!isDebugEnabled()) {
941 return false;
942 }
943 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
944 }
945
946 public boolean isWideViewport() {
947 if (!isDebugEnabled()) {
948 return true;
949 }
950 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
951 }
952
953 public boolean isNormalLayout() {
954 if (!isDebugEnabled()) {
955 return false;
956 }
957 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
958 }
959
960 public boolean isTracing() {
961 if (!isDebugEnabled()) {
962 return false;
963 }
964 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
965 }
966
967 public boolean enableLightTouch() {
968 if (!isDebugEnabled()) {
969 return false;
970 }
971 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
972 }
973
974 public boolean enableNavDump() {
975 if (!isDebugEnabled()) {
976 return false;
977 }
978 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
979 }
980
981 public String getJsEngineFlags() {
982 if (!isDebugEnabled()) {
983 return "";
984 }
985 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
986 }
987
988 // -----------------------------
989 // getter/setters for lab_preferences.xml
990 // -----------------------------
991
992 public boolean useQuickControls() {
993 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
994 }
995
996 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700997 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700998 }
999
Michael Kolbc38c6042011-04-27 10:46:06 -07001000 public boolean useFullscreen() {
1001 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
1002 }
1003
John Reck2fd9d0e2011-07-15 11:13:48 -07001004 public boolean useInvertedRendering() {
1005 return mPrefs.getBoolean(PREF_INVERTED, false);
1006 }
1007
Nicolas Roard5d513102011-08-03 15:35:34 -07001008 public float getInvertedContrast() {
1009 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
1010 }
1011
John Reck35e9dd62011-04-25 09:01:54 -07001012 // -----------------------------
1013 // getter/setters for privacy_security_preferences.xml
1014 // -----------------------------
1015
1016 public boolean showSecurityWarnings() {
1017 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
1018 }
1019
1020 public boolean acceptCookies() {
1021 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
1022 }
1023
1024 public boolean saveFormdata() {
1025 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
1026 }
1027
1028 public boolean enableGeolocation() {
1029 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
1030 }
1031
1032 public boolean rememberPasswords() {
1033 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
1034 }
1035
Michael Kolb14612442011-06-24 13:06:29 -07001036 // -----------------------------
1037 // getter/setters for bandwidth_preferences.xml
1038 // -----------------------------
1039
Mathew Inwood467813f2011-09-02 15:43:17 +01001040 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
1041 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -07001042 }
Mathew Inwood467813f2011-09-02 15:43:17 +01001043
1044 public static String getPreloadAlwaysPreferenceString(Context context) {
1045 return context.getResources().getString(R.string.pref_data_preload_value_always);
1046 }
1047
Mathew Inwood825fba72011-10-04 14:52:52 +01001048 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1049 "browser_default_preload_setting";
1050
1051 public String getDefaultPreloadSetting() {
1052 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1053 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1054 if (preload == null) {
1055 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1056 }
1057 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001058 }
1059
1060 public String getPreloadEnabled() {
1061 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1062 }
1063
Victoria Lease96497832012-03-23 14:19:56 -07001064 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1065 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1066 }
1067
1068 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1069 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1070 }
1071
1072 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1073 "browser_default_link_prefetch_setting";
1074
1075 public String getDefaultLinkPrefetchSetting() {
1076 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1077 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1078 if (preload == null) {
1079 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1080 }
1081 return preload;
1082 }
1083
1084 public String getLinkPrefetchEnabled() {
1085 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1086 }
1087
George Mount3636d0a2011-11-21 09:08:21 -08001088 // -----------------------------
1089 // getter/setters for browser recovery
1090 // -----------------------------
1091 /**
1092 * The last time browser was started.
1093 * @return The last browser start time as System.currentTimeMillis. This
1094 * can be 0 if this is the first time or the last tab was closed.
1095 */
1096 public long getLastRecovered() {
1097 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1098 }
1099
1100 /**
1101 * Sets the last browser start time.
1102 * @param time The last time as System.currentTimeMillis that the browser
1103 * was started. This should be set to 0 if the last tab is closed.
1104 */
1105 public void setLastRecovered(long time) {
1106 mPrefs.edit()
1107 .putLong(KEY_LAST_RECOVERED, time)
1108 .apply();
1109 }
1110
1111 /**
1112 * Used to determine whether or not the previous browser run crashed. Once
1113 * the previous state has been determined, the value will be set to false
1114 * until a pause is received.
1115 * @return true if the last browser run was paused or false if it crashed.
1116 */
1117 public boolean wasLastRunPaused() {
1118 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1119 }
1120
1121 /**
1122 * Sets whether or not the last run was a pause or crash.
1123 * @param isPaused Set to true When a pause is received or false after
1124 * resuming.
1125 */
1126 public void setLastRunPaused(boolean isPaused) {
1127 mPrefs.edit()
1128 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1129 .apply();
1130 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001131}