blob: bc1b83e449e9d6cd9f501cc6329a83d90379e7e3 [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;
Victoria Lease96497832012-03-23 14:19:56 -070024import android.net.ConnectivityManager;
25import android.net.NetworkInfo;
John Reckf48314f2011-04-27 17:52:17 -070026import android.os.Build;
Vivek Sekharce94c2a2015-10-14 13:24:24 -070027import android.preference.ListPreference;
Ben Murdoch0cb81892010-10-08 12:41:33 +010028import android.preference.PreferenceManager;
Mathew Inwood825fba72011-10-04 14:52:52 +010029import android.provider.Settings;
John Reck5ba3c762011-09-14 15:00:15 -070030import android.util.DisplayMetrics;
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -070031import android.webkit.ValueCallback;
Nicolas Roard78a98e42009-05-11 13:34:17 +010032import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033
Bijan Amirzada41242f22014-03-21 12:12:18 -070034import com.android.browser.homepages.HomeProvider;
Dave Tharp4a2c8942015-07-10 14:10:51 -070035import com.android.browser.mdm.AutoFillRestriction;
36import com.android.browser.mdm.DevToolsRestriction;
Dave Tharpb487d5d2015-05-15 15:54:08 -070037import com.android.browser.mdm.DoNotTrackRestriction;
Dave Tharp13f3f7c2015-07-16 14:58:51 -070038import com.android.browser.mdm.DownloadDirRestriction;
Dave Tharp4a2c8942015-07-10 14:10:51 -070039import com.android.browser.mdm.EditBookmarksRestriction;
40import com.android.browser.mdm.IncognitoRestriction;
41import com.android.browser.mdm.ManagedBookmarksRestriction;
Panos Thomasa9ff2c72014-12-17 22:15:23 -080042import com.android.browser.mdm.ProxyRestriction;
Panos Thomas64c77e02014-12-17 22:15:23 -080043import com.android.browser.mdm.SearchEngineRestriction;
Dave Tharp4a2c8942015-07-10 14:10:51 -070044import com.android.browser.mdm.ThirdPartyCookiesRestriction;
45import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070046import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070047import com.android.browser.provider.BrowserProvider;
Bijan Amirzada41242f22014-03-21 12:12:18 -070048import com.android.browser.search.SearchEngine;
49import com.android.browser.search.SearchEngines;
John Reck46500332011-06-07 14:36:10 -070050
John Reck35e9dd62011-04-25 09:01:54 -070051import java.lang.ref.WeakReference;
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -070052import java.util.ArrayList;
John Reck35e9dd62011-04-25 09:01:54 -070053import java.util.Iterator;
54import java.util.LinkedList;
Pankaj Gargf320d6e2015-08-10 09:56:57 -070055import java.util.List;
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -070056import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080057
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080058import org.codeaurora.swe.AutoFillProfile;
59import org.codeaurora.swe.CookieManager;
60import org.codeaurora.swe.GeolocationPermissions;
Pankaj Garg32e1b942015-06-03 18:13:24 -070061import org.codeaurora.swe.PermissionsServiceFactory;
Kulanthaivel Palanichamyf36e1db2015-04-08 16:11:06 -070062import org.codeaurora.swe.WebRefiner;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080063import org.codeaurora.swe.WebSettings.LayoutAlgorithm;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080064import org.codeaurora.swe.WebSettings.TextSize;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080065import org.codeaurora.swe.WebSettings;
66import org.codeaurora.swe.WebView;
Vivek Sekhare8a8ec22014-07-10 14:52:43 -070067import org.codeaurora.swe.WebViewDatabase;
Panos Thomas3f35d8d2014-11-08 22:40:23 -080068
John Reck35e9dd62011-04-25 09:01:54 -070069/**
70 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080071 */
John Reck35e9dd62011-04-25 09:01:54 -070072public class BrowserSettings implements OnSharedPreferenceChangeListener,
73 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010074
qqzhoue6ff8b42013-07-23 17:28:48 +080075 private static final String TAG = "BrowserSettings";
John Reck8fc22a12011-06-16 14:57:41 -070076 // The minimum min font size
77 // Aka, the lower bounds for the min font size range
78 // which is 1:5..24
79 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070080 // The initial value in the text zoom range
81 // This is what represents 100% in the SeekBarPreference range
82 private static final int TEXT_ZOOM_START_VAL = 10;
83 // The size of a single step in the text zoom range, in percent
84 private static final int TEXT_ZOOM_STEP = 5;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -070085 // The initial value in the double tap zoom range
86 // This is what represents 100% in the SeekBarPreference range
87 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
88 // The size of a single step in the double tap zoom range, in percent
89 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -070090
John Reck35e9dd62011-04-25 09:01:54 -070091 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -070092
John Reck35e9dd62011-04-25 09:01:54 -070093 private Context mContext;
94 private SharedPreferences mPrefs;
95 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -070096 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -070097 private WebStorageSizeManager mWebStorageSizeManager;
98 private AutofillHandler mAutofillHandler;
Ben Murdochaaa1f372011-07-25 15:40:58 +010099 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -0700100 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -0700101 private float mFontSizeMult = 1.0f;
John Reck78a6a1d2011-07-21 13:54:03 -0700102
Victoria Lease96497832012-03-23 14:19:56 -0700103 // Current state of network-dependent settings
104 private boolean mLinkPrefetchAllowed = true;
105
John Reck78a6a1d2011-07-21 13:54:03 -0700106 // Cached values
107 private int mPageCacheCapacity = 1;
108 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109
John Reck35e9dd62011-04-25 09:01:54 -0700110 // Cached settings
111 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800112
Ben Murdochaaa1f372011-07-25 15:40:58 +0100113 private static String sFactoryResetUrl;
114
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700115 private boolean mEngineInitialized = false;
116 private boolean mSyncManagedSettings = false;
117
Bijan Amirzada3bb46302014-06-17 16:31:31 -0700118 public static synchronized void initialize(final Context context) {
119 if (sInstance == null)
120 sInstance = new BrowserSettings(context);
John Reck35e9dd62011-04-25 09:01:54 -0700121 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800122
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700123 public static BrowserSettings getInstance() {
John Reck35e9dd62011-04-25 09:01:54 -0700124 return sInstance;
125 }
Ben Murdochef671652010-11-25 17:17:58 +0000126
John Reck35e9dd62011-04-25 09:01:54 -0700127 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100128 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700129 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
John Reck35e9dd62011-04-25 09:01:54 -0700130 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckcadae722011-07-25 11:36:17 -0700131 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700132 }
133
134 public void setController(Controller controller) {
135 mController = controller;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700136 mNeedsSharedSync = true;
137 }
138
139 public void onEngineInitializationComplete() {
140 mEngineInitialized = true;
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700141
142 // Intialize Web Refiner only once
143 final WebRefiner refiner = WebRefiner.getInstance();
144 if (refiner != null) {
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700145 mPrefs.edit().putBoolean(PREF_WEB_REFINER, true).apply();
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700146 refiner.setDefaultPermission(PermissionsServiceFactory.getDefaultPermissions(
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700147 PermissionsServiceFactory.PermissionType.WEBREFINER));
148
149 PermissionsServiceFactory.getPermissionsService(
150 new ValueCallback<PermissionsServiceFactory.PermissionsService>() {
151 @Override
152 public void onReceiveValue(
153 PermissionsServiceFactory.PermissionsService value) {
154 Set<String> origins = value.getOrigins();
155 ArrayList<String> allowList = new ArrayList<>();
156 ArrayList<String> blockList = new ArrayList<>();
157 for (String origin : origins) {
158 PermissionsServiceFactory.PermissionsService.OriginInfo
159 info = value.getOriginInfo(origin);
Axesh R. Ajmera1ad7c572015-07-13 17:10:56 -0700160 if (info == null) {
161 continue;
162 }
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700163 int perm = info.getPermission(
164 PermissionsServiceFactory.PermissionType.WEBREFINER);
165 if (perm == PermissionsServiceFactory.Permission.ALLOW) {
166 allowList.add(origin);
167 } else if (perm == PermissionsServiceFactory.Permission.BLOCK) {
168 blockList.add(origin);
169 }
170 }
171 if (!allowList.isEmpty()) {
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700172 refiner.setPermissionForOrigins(
173 allowList.toArray(new String[allowList.size()]), true);
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700174 }
175
176 if (!blockList.isEmpty()) {
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700177 refiner.setPermissionForOrigins(
178 blockList.toArray(new String[blockList.size()]), false);
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700179 }
180 }
181 }
182 );
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700183 } else {
184 mPrefs.edit().putBoolean(PREF_WEB_REFINER, false).apply();
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700185 }
186
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700187 mAutofillHandler = new AutofillHandler(mContext);
188 if (mSyncManagedSettings) {
189 syncManagedSettings();
190 }
191 if (mNeedsSharedSync) {
John Reckc477e712011-08-17 11:27:15 -0700192 syncSharedSettings();
193 }
Dave Tharp4a2c8942015-07-10 14:10:51 -0700194
195 // Instantiate all MDM Restriction Singletons.
196 AutoFillRestriction.getInstance();
197 DevToolsRestriction.getInstance();
198 DoNotTrackRestriction.getInstance();
Dave Tharp13f3f7c2015-07-16 14:58:51 -0700199 DownloadDirRestriction.getInstance();
Dave Tharp4a2c8942015-07-10 14:10:51 -0700200 EditBookmarksRestriction.getInstance();
201 IncognitoRestriction.getInstance();
202 ManagedBookmarksRestriction.getInstance();
Panos Thomasa9ff2c72014-12-17 22:15:23 -0800203 ProxyRestriction.getInstance();
Dave Tharp4a2c8942015-07-10 14:10:51 -0700204 SearchEngineRestriction.getInstance();
205 ThirdPartyCookiesRestriction.getInstance();
206 URLFilterRestriction.getInstance();
Ben Murdochef671652010-11-25 17:17:58 +0000207 }
208
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800209 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800210
John Reckc477e712011-08-17 11:27:15 -0700211 if (mNeedsSharedSync) {
212 syncSharedSettings();
213 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800214
John Reck35e9dd62011-04-25 09:01:54 -0700215 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800216 syncStaticSettings(settings);
217 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700218 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800219 }
220 }
221
John Reckd1d87312012-03-08 13:25:00 -0800222 public void stopManagingSettings(WebSettings settings) {
223 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
224 while (iter.hasNext()) {
225 WeakReference<WebSettings> ref = iter.next();
226 if (ref.get() == settings) {
227 iter.remove();
228 return;
229 }
230 }
231 }
232
John Reckcadae722011-07-25 11:36:17 -0700233 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700234
235 @Override
236 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700237 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
238 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700239 // the cost of one cached page is ~3M (measured using nytimes.com). For
240 // low end devices, we only cache one page. For high end devices, we try
241 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800242
243 // SWE_TODO : assume a high-memory device
244 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700245 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800246 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700247 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
248 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
249 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700250 // Workaround b/5254577
251 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700252 if (Build.VERSION.CODENAME.equals("REL")) {
253 // This is a release build, always startup with debug disabled
254 setDebugEnabled(false);
255 }
256 if (mPrefs.contains(PREF_TEXT_SIZE)) {
257 /*
258 * Update from TextSize enum to zoom percent
259 * SMALLEST is 50%
260 * SMALLER is 75%
261 * NORMAL is 100%
262 * LARGER is 150%
263 * LARGEST is 200%
264 */
265 switch (getTextSize()) {
266 case SMALLEST:
267 setTextZoom(50);
268 break;
269 case SMALLER:
270 setTextZoom(75);
271 break;
272 case LARGER:
273 setTextZoom(150);
274 break;
275 case LARGEST:
276 setTextZoom(200);
277 break;
278 }
279 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
280 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100281
qqzhou8c5b0a32013-07-22 15:31:03 +0800282 // add for carrier homepage feature
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700283 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
qqzhou8c5b0a32013-07-22 15:31:03 +0800284
kaiyizbf086ea2013-08-02 11:03:58 +0800285 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
Tarun Nainani95b79682014-12-09 09:38:04 -0800286 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING, "auto").apply();
kaiyizbf086ea2013-08-02 11:03:58 +0800287 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700288
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700289 if (!mPrefs.contains(PREF_EDGE_SWIPE)) {
290 mPrefs.edit().putString(PREF_EDGE_SWIPE,
291 mContext.getResources().getString(
292 R.string.value_unknown_edge_swipe)).apply();
293 }
294
Ben Murdochaaa1f372011-07-25 15:40:58 +0100295 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
296 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
297 BrowserProvider.getClientId(mContext.getContentResolver()));
298 }
299
Ben Murdochaaa1f372011-07-25 15:40:58 +0100300 synchronized (BrowserSettings.class) {
301 sInitialized = true;
302 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700303 }
304 }
305 };
306
Ben Murdochaaa1f372011-07-25 15:40:58 +0100307 private static void requireInitialization() {
308 synchronized (BrowserSettings.class) {
309 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700310 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100311 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700312 } catch (InterruptedException e) {
313 }
314 }
315 }
316 }
317
The Android Open Source Project0c908882009-03-03 19:32:16 -0800318 /**
John Reck35e9dd62011-04-25 09:01:54 -0700319 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800320 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800321 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700322 settings.setGeolocationEnabled(enableGeolocation());
323 settings.setJavaScriptEnabled(enableJavascript());
324 settings.setLightTouchEnabled(enableLightTouch());
325 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700326 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
John Reck35e9dd62011-04-25 09:01:54 -0700327 settings.setMinimumFontSize(getMinimumFontSize());
328 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck7dc444b2011-06-16 17:44:29 -0700329 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700330 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100331 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700332 settings.setLoadsImagesAutomatically(loadImages());
333 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
334 settings.setSavePassword(rememberPasswords());
335 settings.setSaveFormData(saveFormdata());
336 settings.setUseWideViewPort(isWideViewport());
Panos Thomasb10bbda2014-06-23 10:18:36 -0700337 settings.setDoNotTrack(doNotTrack());
Yida Wang078425c2015-06-02 14:14:06 -0400338 settings.setNightModeEnabled(isNightModeEnabled());
Tarun Nainani8eb00912014-07-17 12:28:32 -0700339 settings.setMediaPlaybackRequiresUserGesture(false);
John Reck2fd9d0e2011-07-15 11:13:48 -0700340
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800341 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800342 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
343 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
344 settingsClassic.setForceUserScalable(forceEnableUserScalable());
345 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
346 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800347
John Reckea17e782011-10-27 17:15:59 -0700348 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800349 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700350 useInverted ? "true" : "false");
351 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800352 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700353 Float.toString(getInvertedContrast()));
354 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700355
John Reckea17e782011-10-27 17:15:59 -0700356 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800357 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700358 enableCpuUploadPath() ? "true" : "false");
359 }
Victoria Lease96497832012-03-23 14:19:56 -0700360
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800361 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000362 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800363
Tarun Nainani8eb00912014-07-17 12:28:32 -0700364
John Reck35e9dd62011-04-25 09:01:54 -0700365 /**
366 * Syncs all the settings that have no UI
367 * These cannot change, so we only need to set them once per WebSettings
368 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800369 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700370 settings.setDefaultFontSize(16);
371 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000372
John Reck35e9dd62011-04-25 09:01:54 -0700373 // WebView inside Browser doesn't want initial focus to be set.
374 settings.setNeedInitialFocus(false);
375 // Browser supports multiple windows
376 settings.setSupportMultipleWindows(true);
377 // enable smooth transition for better performance during panning or
378 // zooming
379 settings.setEnableSmoothTransition(true);
380 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700381 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700382
383 // HTML5 API flags
384 settings.setAppCacheEnabled(true);
385 settings.setDatabaseEnabled(true);
386 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700387
388 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700389 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700390 settings.setAppCachePath(getAppCachePath());
391 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
392 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700393 // origin policy for file access
394 settings.setAllowUniversalAccessFromFileURLs(false);
395 settings.setAllowFileAccessFromFileURLs(false);
Sudheer Koganti24766882014-10-02 10:58:09 -0700396 settings.setFullscreenSupported(true);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800397
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800398 //if (!(settings instanceof WebSettingsClassic)) return;
399 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800400
401 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
402 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
403 // WebView should be preserving the memory as much as possible.
404 // However, apps like browser wish to turn on the performance mode which
405 // would require more memory.
406 // TODO: We need to dynamically allocate/deallocate temporary memory for
407 // apps which are trying to use minimal memory. Currently, double
408 // buffering is always turned on, which is unnecessary.
409 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
410 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800411 */
John Reck35e9dd62011-04-25 09:01:54 -0700412 }
413
414 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700415 mNeedsSharedSync = false;
Axesh R. Ajmera579c70c2014-04-17 13:24:56 -0700416 CookieManager.getInstance().setAcceptCookie(acceptCookies());
Vivek Sekhared791da2015-02-22 12:39:05 -0800417
John Reck35e9dd62011-04-25 09:01:54 -0700418 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700419
John Reck35e9dd62011-04-25 09:01:54 -0700420 private void syncManagedSettings() {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700421 if (!mEngineInitialized) {
422 mSyncManagedSettings = true;
423 return;
424 }
425 mSyncManagedSettings = false;
John Reck35e9dd62011-04-25 09:01:54 -0700426 syncSharedSettings();
427 synchronized (mManagedSettings) {
428 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
429 while (iter.hasNext()) {
430 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800431 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700432 if (settings == null) {
433 iter.remove();
434 continue;
435 }
436 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000437 }
John Reck35e9dd62011-04-25 09:01:54 -0700438 }
439 }
Ben Murdochef671652010-11-25 17:17:58 +0000440
John Reck35e9dd62011-04-25 09:01:54 -0700441 @Override
442 public void onSharedPreferenceChanged(
443 SharedPreferences sharedPreferences, String key) {
444 syncManagedSettings();
445 if (PREF_SEARCH_ENGINE.equals(key)) {
446 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700447 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200448 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700449 mController.getUi().setFullscreen(useFullscreen());
450 }
Victoria Lease96497832012-03-23 14:19:56 -0700451 } else if (PREF_LINK_PREFETCH.equals(key)) {
452 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700453 }
John Reck35e9dd62011-04-25 09:01:54 -0700454 }
455
John Reck961d35d2011-06-23 09:45:54 -0700456 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100457 requireInitialization();
458 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700459 }
460
461 public LayoutAlgorithm getLayoutAlgorithm() {
462 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
463 if (autofitPages()) {
Tarun Nainani003bec52014-07-02 02:20:34 -0700464 layoutAlgorithm = LayoutAlgorithm.TEXT_AUTOSIZING;
John Reck35e9dd62011-04-25 09:01:54 -0700465 }
466 if (isDebugEnabled()) {
467 if (isSmallScreen()) {
468 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000469 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700470 if (isNormalLayout()) {
471 layoutAlgorithm = LayoutAlgorithm.NORMAL;
472 } else {
473 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
474 }
Ben Murdochef671652010-11-25 17:17:58 +0000475 }
John Reck35e9dd62011-04-25 09:01:54 -0700476 }
477 return layoutAlgorithm;
478 }
Ben Murdochef671652010-11-25 17:17:58 +0000479
John Reck35e9dd62011-04-25 09:01:54 -0700480 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700481 requireInitialization();
482 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800483 }
484
John Reck35e9dd62011-04-25 09:01:54 -0700485 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700486 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700487 return mWebStorageSizeManager;
488 }
489
John Reck35e9dd62011-04-25 09:01:54 -0700490 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700491 if (mAppCachePath == null) {
492 mAppCachePath = mContext.getDir("appcache", 0).getPath();
493 }
494 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700495 }
496
Vivek Sekharce94c2a2015-10-14 13:24:24 -0700497 protected void updateSearchEngine(boolean force) {
John Reck35e9dd62011-04-25 09:01:54 -0700498 String searchEngineName = getSearchEngineName();
499 if (force || mSearchEngine == null ||
500 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700501 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000502 }
503 }
504
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100505 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700506 if (mSearchEngine == null) {
507 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100508 }
John Reck35e9dd62011-04-25 09:01:54 -0700509 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100510 }
511
John Reck35e9dd62011-04-25 09:01:54 -0700512 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700513 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700514 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100515 }
516
John Reck35e9dd62011-04-25 09:01:54 -0700517 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700518 Editor edit = mPrefs.edit();
519 edit.putBoolean(PREF_DEBUG_MENU, value);
520 if (!value) {
521 // Reset to "safe" value
522 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
523 }
524 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100525 }
526
John Reck35e9dd62011-04-25 09:01:54 -0700527 public void clearCache() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700528 if (mController != null) {
529 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800530 if (current != null) {
531 current.clearCache(true);
532 }
533 }
534 }
535
John Reck35e9dd62011-04-25 09:01:54 -0700536 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800537 CookieManager.getInstance().removeAllCookie();
538 }
539
John Reck35e9dd62011-04-25 09:01:54 -0700540 public void clearHistory() {
541 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800542 Browser.clearHistory(resolver);
543 Browser.clearSearches(resolver);
544 }
545
John Reck35e9dd62011-04-25 09:01:54 -0700546 public void clearFormData() {
547 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700548 if (mController!= null) {
549 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100550 if (currentTopView != null) {
551 currentTopView.clearFormData();
552 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800553 }
554 }
555
Bijan Amirzada3bbb3a42014-06-17 12:06:18 -0700556 public WebView getTopWebView(){
557 if (mController!= null)
558 return mController.getCurrentTopWebView();
559
560 return null;
561 }
562
John Reck35e9dd62011-04-25 09:01:54 -0700563 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700564 // Clear password store maintained by SWE engine
565 WebSettings settings = null;
566 // find a valid settings object
567 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
568 while (iter.hasNext()) {
569 WeakReference<WebSettings> ref = iter.next();
570 settings = (WebSettings)ref.get();
571 if (settings != null) {
572 break;
573 }
574 }
575 if (settings != null) {
Panos Thomasdbaea4e2014-05-22 06:48:47 -0700576 settings.clearPasswords();
Panos Thomasfa948b82014-03-09 03:42:42 -0700577 }
Vivek Sekhare8a8ec22014-07-10 14:52:43 -0700578
579 // Clear passwords in WebView database
580 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
581 db.clearHttpAuthUsernamePassword();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800582 }
583
John Reck35e9dd62011-04-25 09:01:54 -0700584 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100585 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100586 }
587
John Reck35e9dd62011-04-25 09:01:54 -0700588 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100589 GeolocationPermissions.getInstance().clearAll();
Panos Thomasb298aad2014-10-22 12:24:21 -0700590 if (GeolocationPermissions.isIncognitoCreated()) {
591 GeolocationPermissions.getIncognitoInstance().clearAll();
592 }
Nicolas Roard78a98e42009-05-11 13:34:17 +0100593 }
594
John Reck35e9dd62011-04-25 09:01:54 -0700595 public void resetDefaultPreferences() {
Pankaj Gargf320d6e2015-08-10 09:56:57 -0700596 WebRefiner webRefiner = WebRefiner.getInstance();
597 if (webRefiner != null) {
598 List<String> webrefiner_list = PermissionsServiceFactory.getOriginsForPermission(
599 PermissionsServiceFactory.PermissionType.WEBREFINER);
600 if (!webrefiner_list.isEmpty()) {
601 String[] origins = webrefiner_list.toArray(new String[webrefiner_list.size()]);
602 webRefiner.useDefaultPermissionForOrigins(origins);
603 }
604 }
605
606 PermissionsServiceFactory.resetDefaultPermissions();
607 mPrefs.edit().clear().apply();
608
Björn Isakssonc885a242012-06-05 17:19:04 +0200609 resetCachedValues();
Pankaj Gargf320d6e2015-08-10 09:56:57 -0700610
611 if (webRefiner != null) {
Pankaj Garg1e967142015-08-07 09:05:10 -0700612 mPrefs.edit().putBoolean(PREF_WEB_REFINER, true).apply();
613 } else {
614 mPrefs.edit().putBoolean(PREF_WEB_REFINER, false).apply();
615 }
John Reck35e9dd62011-04-25 09:01:54 -0700616 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700617 }
618
Björn Isakssonc885a242012-06-05 17:19:04 +0200619 private void resetCachedValues() {
620 updateSearchEngine(false);
621 }
622
John Reck35e9dd62011-04-25 09:01:54 -0700623 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800624 // query the profile from components autofill database 524
625 if (mAutofillHandler.mAutoFillProfile == null &&
626 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
627 WebSettings settings = null;
628 // find a valid settings object
629 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
630 while (iter.hasNext()) {
631 WeakReference<WebSettings> ref = iter.next();
632 settings = (WebSettings)ref.get();
633 if (settings != null) {
634 break;
635 }
636 }
637 if (settings != null) {
638 AutoFillProfile profile =
639 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
640 mAutofillHandler.setAutoFillProfile(profile);
641 }
642 }
John Reck35e9dd62011-04-25 09:01:54 -0700643 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800644 }
645
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800646 public String getAutoFillProfileId() {
647 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800648 }
649
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800650 public void updateAutoFillProfile(AutoFillProfile profile) {
651 syncAutoFillProfile(profile);
652 }
653
654 private void syncAutoFillProfile(AutoFillProfile profile) {
655 synchronized (mManagedSettings) {
656 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
657 while (iter.hasNext()) {
658 WeakReference<WebSettings> ref = iter.next();
659 WebSettings settings = (WebSettings)ref.get();
660 if (settings == null) {
661 iter.remove();
662 continue;
663 }
664 // update the profile only once.
665 settings.setAutoFillProfile(profile);
666 // Now we should have the guid
667 mAutofillHandler.setAutoFillProfile(profile);
668 break;
669 }
670 }
671 }
John Reck35e9dd62011-04-25 09:01:54 -0700672 public void toggleDebugSettings() {
673 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100675
John Reckb8b2af82011-05-20 15:58:33 -0700676 public boolean hasDesktopUseragent(WebView view) {
Vivek Sekhar7b3d2da2014-11-13 16:20:11 -0800677 return view != null && view.getUseDesktopUserAgent();
John Reckb8b2af82011-05-20 15:58:33 -0700678 }
679
680 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700681 if (view == null) {
682 return;
683 }
Vivek Sekhar7b3d2da2014-11-13 16:20:11 -0800684 if (hasDesktopUseragent(view))
685 view.setUseDesktopUserAgent(false, true);
686 else
687 view.setUseDesktopUserAgent(true, true);
John Reckb8b2af82011-05-20 15:58:33 -0700688 }
689
John Reck7dc444b2011-06-16 17:44:29 -0700690 public static int getAdjustedMinimumFontSize(int rawValue) {
691 rawValue++; // Preference starts at 0, min font at 1
692 if (rawValue > 1) {
693 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
694 }
695 return rawValue;
696 }
697
John Reck5ba3c762011-09-14 15:00:15 -0700698 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700699 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700700 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700701 }
702
703 static int getRawTextZoom(int percent) {
704 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
705 }
706
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700707 public int getAdjustedDoubleTapZoom(int rawValue) {
708 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
709 return (int) ((rawValue + 100) * mFontSizeMult);
710 }
711
712 static int getRawDoubleTapZoom(int percent) {
713 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
714 }
715
John Reckcadae722011-07-25 11:36:17 -0700716 public SharedPreferences getPreferences() {
717 return mPrefs;
718 }
719
Victoria Lease96497832012-03-23 14:19:56 -0700720 // update connectivity-dependent options
721 public void updateConnectionType() {
722 ConnectivityManager cm = (ConnectivityManager)
723 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
724 String linkPrefetchPreference = getLinkPrefetchEnabled();
725 boolean linkPrefetchAllowed = linkPrefetchPreference.
726 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
727 NetworkInfo ni = cm.getActiveNetworkInfo();
728 if (ni != null) {
729 switch (ni.getType()) {
730 case ConnectivityManager.TYPE_WIFI:
731 case ConnectivityManager.TYPE_ETHERNET:
732 case ConnectivityManager.TYPE_BLUETOOTH:
733 linkPrefetchAllowed |= linkPrefetchPreference.
734 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
735 break;
736 case ConnectivityManager.TYPE_MOBILE:
737 case ConnectivityManager.TYPE_MOBILE_DUN:
738 case ConnectivityManager.TYPE_MOBILE_MMS:
739 case ConnectivityManager.TYPE_MOBILE_SUPL:
740 case ConnectivityManager.TYPE_WIMAX:
741 default:
742 break;
743 }
744 }
745 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
746 mLinkPrefetchAllowed = linkPrefetchAllowed;
747 syncManagedSettings();
748 }
749 }
750
luxiaol62677b02013-07-22 07:54:49 +0800751 public String getDownloadPath() {
752 return mPrefs.getString(PREF_DOWNLOAD_PATH,
753 DownloadHandler.getDefaultDownloadPath(mContext));
754 }
John Reck35e9dd62011-04-25 09:01:54 -0700755 // -----------------------------
756 // getter/setters for accessibility_preferences.xml
757 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100758
John Reck7dc444b2011-06-16 17:44:29 -0700759 @Deprecated
760 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700761 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
762 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100763 }
764
John Reck35e9dd62011-04-25 09:01:54 -0700765 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700766 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700767 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100768 }
769
John Reck92f25f82011-04-26 16:57:10 -0700770 public boolean forceEnableUserScalable() {
771 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
772 }
773
John Reck7dc444b2011-06-16 17:44:29 -0700774 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700775 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700776 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
777 return getAdjustedTextZoom(textZoom);
778 }
779
780 public void setTextZoom(int percent) {
781 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
782 }
783
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700784 public int getDoubleTapZoom() {
785 requireInitialization();
786 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
787 return getAdjustedDoubleTapZoom(doubleTapZoom);
788 }
789
790 public void setDoubleTapZoom(int percent) {
791 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
792 }
793
John Reck35e9dd62011-04-25 09:01:54 -0700794 // -----------------------------
795 // getter/setters for advanced_preferences.xml
796 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100797
John Reck35e9dd62011-04-25 09:01:54 -0700798 public String getSearchEngineName() {
Panos Thomas64c77e02014-12-17 22:15:23 -0800799 // The following is a NOP if the SEARCH_ENGINE restriction has already been created. Otherwise,
800 // it creates the restriction and if enabled it sets the <default_search_engine_value>.
801 SearchEngineRestriction.getInstance();
802
Vivek Sekharce94c2a2015-10-14 13:24:24 -0700803 String defaultSearchEngineValue = getUserSearchEngine();
luxiaol221b3932013-07-19 15:27:57 +0800804 if (defaultSearchEngineValue == null) {
805 defaultSearchEngineValue = SearchEngine.GOOGLE;
806 }
Vivek Sekharce94c2a2015-10-14 13:24:24 -0700807 return defaultSearchEngineValue;
Ben Murdoch23da30e2010-10-26 15:18:44 +0100808 }
John Reck63bb6872010-12-01 19:29:32 -0800809
Michael Kolb8d772b02012-01-19 13:56:00 -0800810 public boolean allowAppTabs() {
811 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
812 }
813
John Reck35e9dd62011-04-25 09:01:54 -0700814 public boolean openInBackground() {
815 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800816 }
John Reck35e9dd62011-04-25 09:01:54 -0700817
818 public boolean enableJavascript() {
819 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
820 }
821
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800822 public boolean enableMemoryMonitor() {
823 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
824 }
825
Tarun Nainani8eb00912014-07-17 12:28:32 -0700826
John Reck35e9dd62011-04-25 09:01:54 -0700827 public boolean loadPageInOverviewMode() {
828 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
829 }
830
831 public boolean autofitPages() {
832 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
833 }
834
835 public boolean blockPopupWindows() {
Pankaj Garg11015212015-07-06 14:38:43 -0700836 return !PermissionsServiceFactory.getDefaultPermissions(
837 PermissionsServiceFactory.PermissionType.POPUP);
John Reck35e9dd62011-04-25 09:01:54 -0700838 }
839
840 public boolean loadImages() {
841 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
842 }
843
844 public String getDefaultTextEncoding() {
Tarun Nainani88381422015-04-02 19:47:21 -0700845 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, "auto");
John Reck35e9dd62011-04-25 09:01:54 -0700846 }
847
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700848 public String getEdgeSwipeAction() {
849 return mPrefs.getString(PREF_EDGE_SWIPE,
850 mContext.getResources().getString(R.string.value_unknown_edge_swipe));
851 }
852
853 public void setEdgeSwipeTemporal() {
854 mPrefs.edit().putString(PREF_EDGE_SWIPE,
855 mContext.getResources().getString(R.string.value_temporal_edge_swipe)).apply();
856 }
857
Vivek Sekharce94c2a2015-10-14 13:24:24 -0700858 public void setUserSearchEngine(String engine) {
859 if (!engine.equals(mContext.getResources().getString(R.string.default_search_engine_value)))
860 mPrefs.edit().putString(
861 PREF_USER_SEARCH_ENGINE + mContext.getResources().getConfiguration().locale,
862 engine).apply();
863 // ensure changing back to default clears the search engine
864 else
865 mPrefs.edit().remove(PREF_USER_SEARCH_ENGINE
866 + mContext.getResources().getConfiguration().locale).apply();
867 }
868
869 public String getUserSearchEngine() {
870 return mPrefs.getString(
871 PREF_USER_SEARCH_ENGINE + mContext.getResources().getConfiguration().locale
872 , mContext.getResources().getString(R.string.default_search_engine_value));
873 }
874
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700875 public void setEdgeSwipeSpatial() {
876 mPrefs.edit().putString(PREF_EDGE_SWIPE,
877 mContext.getResources().getString(R.string.value_spatial_edge_swipe)).apply();
878 }
879
880 public void setEdgeSwipeDisabled() {
881 mPrefs.edit().putString(PREF_EDGE_SWIPE,
882 mContext.getResources().getString(R.string.value_disable_edge_swipe)).apply();
883 }
884
John Reck35e9dd62011-04-25 09:01:54 -0700885 // -----------------------------
886 // getter/setters for general_preferences.xml
887 // -----------------------------
888
889 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700890 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
891 }
892
893 public void setHomePage(String value) {
894 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
895 }
896
897 public boolean isAutofillEnabled() {
898 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
899 }
900
901 public void setAutofillEnabled(boolean value) {
902 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
903 }
904
jrizzolibe2afc52015-10-28 18:43:00 +0100905 public boolean isColoredSBEnabled() {
906 return mPrefs.getBoolean(PREF_COLOREDSB_ENABLED, false);
907 }
908
909 public void setColoredSBEnabled(boolean value) {
910 mPrefs.edit().putBoolean(PREF_COLOREDSB_ENABLED, value).apply();
911 }
912
Site Maoabb7bd32015-03-20 15:34:02 -0700913 public boolean isPowerSaveModeEnabled() {
914 return mPrefs.getBoolean(PREF_POWERSAVE_ENABLED, false);
915 }
916
917 public void setPowerSaveModeEnabled(boolean value) {
918 mPrefs.edit().putBoolean(PREF_POWERSAVE_ENABLED, value).apply();
919 }
920
Yida Wang078425c2015-06-02 14:14:06 -0400921 public boolean isNightModeEnabled() {
922 return mPrefs.getBoolean(PREF_NIGHTMODE_ENABLED, false);
923 }
924
925 public void setNightModeEnabled(boolean value) {
926 mPrefs.edit().putBoolean(PREF_NIGHTMODE_ENABLED, value).apply();
927 }
928
John Reck35e9dd62011-04-25 09:01:54 -0700929 // -----------------------------
930 // getter/setters for debug_preferences.xml
931 // -----------------------------
932
933 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700934 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700935 return true;
936 }
937 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
938 }
939
Derek Sollenberger31adf672011-07-08 11:31:30 -0400940 public boolean isSkiaHardwareAccelerated() {
941 if (!isDebugEnabled()) {
942 return false;
943 }
944 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
945 }
946
Site Mao7521b092015-07-20 18:27:20 -0700947 public boolean isDisablePerfFeatures() {
948 // This value is flipped in the prefs.
949 return !mPrefs.getBoolean(PREF_DISABLE_PERF, true);
950 }
951
John Reck35e9dd62011-04-25 09:01:54 -0700952 // -----------------------------
953 // getter/setters for hidden_debug_preferences.xml
954 // -----------------------------
955
956 public boolean enableVisualIndicator() {
957 if (!isDebugEnabled()) {
958 return false;
959 }
960 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
961 }
962
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700963 public boolean enableCpuUploadPath() {
964 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700965 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700966 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700967 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700968 }
969
John Reck35e9dd62011-04-25 09:01:54 -0700970 public boolean isSmallScreen() {
971 if (!isDebugEnabled()) {
972 return false;
973 }
974 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
975 }
976
977 public boolean isWideViewport() {
978 if (!isDebugEnabled()) {
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800979 return true;
John Reck35e9dd62011-04-25 09:01:54 -0700980 }
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800981 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
John Reck35e9dd62011-04-25 09:01:54 -0700982 }
983
984 public boolean isNormalLayout() {
985 if (!isDebugEnabled()) {
986 return false;
987 }
988 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
989 }
990
991 public boolean isTracing() {
992 if (!isDebugEnabled()) {
993 return false;
994 }
995 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
996 }
997
998 public boolean enableLightTouch() {
999 if (!isDebugEnabled()) {
1000 return false;
1001 }
1002 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
1003 }
1004
1005 public boolean enableNavDump() {
1006 if (!isDebugEnabled()) {
1007 return false;
1008 }
1009 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
1010 }
1011
1012 public String getJsEngineFlags() {
1013 if (!isDebugEnabled()) {
1014 return "";
1015 }
1016 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
1017 }
1018
1019 // -----------------------------
1020 // getter/setters for lab_preferences.xml
1021 // -----------------------------
1022
John Reck35e9dd62011-04-25 09:01:54 -07001023 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -07001024 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -07001025 }
1026
Michael Kolbc38c6042011-04-27 10:46:06 -07001027 public boolean useFullscreen() {
1028 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
1029 }
1030
John Reck2fd9d0e2011-07-15 11:13:48 -07001031 public boolean useInvertedRendering() {
1032 return mPrefs.getBoolean(PREF_INVERTED, false);
1033 }
1034
Nicolas Roard5d513102011-08-03 15:35:34 -07001035 public float getInvertedContrast() {
1036 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
1037 }
1038
John Reck35e9dd62011-04-25 09:01:54 -07001039 // -----------------------------
1040 // getter/setters for privacy_security_preferences.xml
1041 // -----------------------------
1042
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001043 public boolean doNotTrack() {
Dave Tharpb487d5d2015-05-15 15:54:08 -07001044 boolean dntVal;
1045 if (DoNotTrackRestriction.getInstance().isEnabled()) {
1046 dntVal = DoNotTrackRestriction.getInstance().getValue();
1047 }
1048 else {
1049 dntVal = mPrefs.getBoolean(PREF_DO_NOT_TRACK, true);
1050 }
1051 return dntVal;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001052 }
1053
John Reck35e9dd62011-04-25 09:01:54 -07001054 public boolean acceptCookies() {
Pankaj Garg32e1b942015-06-03 18:13:24 -07001055 return PermissionsServiceFactory.getDefaultPermissions(
1056 PermissionsServiceFactory.PermissionType.COOKIE);
John Reck35e9dd62011-04-25 09:01:54 -07001057 }
1058
1059 public boolean saveFormdata() {
1060 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
1061 }
1062
1063 public boolean enableGeolocation() {
1064 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
1065 }
1066
1067 public boolean rememberPasswords() {
1068 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
1069 }
1070
Michael Kolb14612442011-06-24 13:06:29 -07001071 // -----------------------------
1072 // getter/setters for bandwidth_preferences.xml
1073 // -----------------------------
1074
Mathew Inwood467813f2011-09-02 15:43:17 +01001075 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
1076 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -07001077 }
Mathew Inwood467813f2011-09-02 15:43:17 +01001078
1079 public static String getPreloadAlwaysPreferenceString(Context context) {
1080 return context.getResources().getString(R.string.pref_data_preload_value_always);
1081 }
1082
Mathew Inwood825fba72011-10-04 14:52:52 +01001083 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1084 "browser_default_preload_setting";
1085
1086 public String getDefaultPreloadSetting() {
1087 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1088 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1089 if (preload == null) {
1090 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1091 }
1092 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001093 }
1094
1095 public String getPreloadEnabled() {
1096 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1097 }
1098
Victoria Lease96497832012-03-23 14:19:56 -07001099 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1100 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1101 }
1102
1103 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1104 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1105 }
1106
1107 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1108 "browser_default_link_prefetch_setting";
1109
1110 public String getDefaultLinkPrefetchSetting() {
1111 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1112 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1113 if (preload == null) {
1114 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1115 }
1116 return preload;
1117 }
1118
1119 public String getLinkPrefetchEnabled() {
1120 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1121 }
1122
George Mount3636d0a2011-11-21 09:08:21 -08001123 // -----------------------------
1124 // getter/setters for browser recovery
1125 // -----------------------------
1126 /**
1127 * The last time browser was started.
1128 * @return The last browser start time as System.currentTimeMillis. This
1129 * can be 0 if this is the first time or the last tab was closed.
1130 */
1131 public long getLastRecovered() {
1132 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1133 }
1134
1135 /**
1136 * Sets the last browser start time.
1137 * @param time The last time as System.currentTimeMillis that the browser
1138 * was started. This should be set to 0 if the last tab is closed.
1139 */
1140 public void setLastRecovered(long time) {
1141 mPrefs.edit()
1142 .putLong(KEY_LAST_RECOVERED, time)
1143 .apply();
1144 }
1145
1146 /**
1147 * Used to determine whether or not the previous browser run crashed. Once
1148 * the previous state has been determined, the value will be set to false
1149 * until a pause is received.
1150 * @return true if the last browser run was paused or false if it crashed.
1151 */
1152 public boolean wasLastRunPaused() {
1153 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1154 }
1155
1156 /**
1157 * Sets whether or not the last run was a pause or crash.
1158 * @param isPaused Set to true When a pause is received or false after
1159 * resuming.
1160 */
1161 public void setLastRunPaused(boolean isPaused) {
1162 mPrefs.edit()
1163 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1164 .apply();
1165 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001166}