blob: fef0b4462e9a2a80488a1919dcf56c92c201a67c [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;
Ben Murdoch0cb81892010-10-08 12:41:33 +010027import android.preference.PreferenceManager;
Mathew Inwood825fba72011-10-04 14:52:52 +010028import android.provider.Settings;
John Reck5ba3c762011-09-14 15:00:15 -070029import android.util.DisplayMetrics;
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -070030import android.webkit.ValueCallback;
Nicolas Roard78a98e42009-05-11 13:34:17 +010031import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032
Bijan Amirzada41242f22014-03-21 12:12:18 -070033import com.android.browser.homepages.HomeProvider;
Dave Tharp4a2c8942015-07-10 14:10:51 -070034import com.android.browser.mdm.AutoFillRestriction;
35import com.android.browser.mdm.DevToolsRestriction;
Dave Tharpb487d5d2015-05-15 15:54:08 -070036import com.android.browser.mdm.DoNotTrackRestriction;
Dave Tharp13f3f7c2015-07-16 14:58:51 -070037import com.android.browser.mdm.DownloadDirRestriction;
Dave Tharp4a2c8942015-07-10 14:10:51 -070038import com.android.browser.mdm.EditBookmarksRestriction;
39import com.android.browser.mdm.IncognitoRestriction;
40import com.android.browser.mdm.ManagedBookmarksRestriction;
Panos Thomasa9ff2c72014-12-17 22:15:23 -080041import com.android.browser.mdm.ProxyRestriction;
Panos Thomas64c77e02014-12-17 22:15:23 -080042import com.android.browser.mdm.SearchEngineRestriction;
Dave Tharp4a2c8942015-07-10 14:10:51 -070043import com.android.browser.mdm.ThirdPartyCookiesRestriction;
44import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070045import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070046import com.android.browser.provider.BrowserProvider;
Bijan Amirzada41242f22014-03-21 12:12:18 -070047import com.android.browser.search.SearchEngine;
48import com.android.browser.search.SearchEngines;
John Reck46500332011-06-07 14:36:10 -070049
John Reck35e9dd62011-04-25 09:01:54 -070050import java.lang.ref.WeakReference;
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -070051import java.util.ArrayList;
John Reck35e9dd62011-04-25 09:01:54 -070052import java.util.Iterator;
53import java.util.LinkedList;
Pankaj Gargf320d6e2015-08-10 09:56:57 -070054import java.util.List;
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -070055import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080056
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080057import org.codeaurora.swe.AutoFillProfile;
58import org.codeaurora.swe.CookieManager;
59import org.codeaurora.swe.GeolocationPermissions;
Pankaj Garg32e1b942015-06-03 18:13:24 -070060import org.codeaurora.swe.PermissionsServiceFactory;
Kulanthaivel Palanichamyf36e1db2015-04-08 16:11:06 -070061import org.codeaurora.swe.WebRefiner;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080062import org.codeaurora.swe.WebSettings.LayoutAlgorithm;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080063import org.codeaurora.swe.WebSettings.TextSize;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080064import org.codeaurora.swe.WebSettings;
65import org.codeaurora.swe.WebView;
Vivek Sekhare8a8ec22014-07-10 14:52:43 -070066import org.codeaurora.swe.WebViewDatabase;
Panos Thomas3f35d8d2014-11-08 22:40:23 -080067
John Reck35e9dd62011-04-25 09:01:54 -070068/**
69 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080070 */
John Reck35e9dd62011-04-25 09:01:54 -070071public class BrowserSettings implements OnSharedPreferenceChangeListener,
72 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010073
qqzhoue6ff8b42013-07-23 17:28:48 +080074 private static final String TAG = "BrowserSettings";
John Reck8fc22a12011-06-16 14:57:41 -070075 // The minimum min font size
76 // Aka, the lower bounds for the min font size range
77 // which is 1:5..24
78 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070079 // The initial value in the text zoom range
80 // This is what represents 100% in the SeekBarPreference range
81 private static final int TEXT_ZOOM_START_VAL = 10;
82 // The size of a single step in the text zoom range, in percent
83 private static final int TEXT_ZOOM_STEP = 5;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -070084 // The initial value in the double tap zoom range
85 // This is what represents 100% in the SeekBarPreference range
86 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
87 // The size of a single step in the double tap zoom range, in percent
88 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -070089
John Reck35e9dd62011-04-25 09:01:54 -070090 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -070091
John Reck35e9dd62011-04-25 09:01:54 -070092 private Context mContext;
93 private SharedPreferences mPrefs;
94 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -070095 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -070096 private WebStorageSizeManager mWebStorageSizeManager;
97 private AutofillHandler mAutofillHandler;
Ben Murdochaaa1f372011-07-25 15:40:58 +010098 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -070099 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -0700100 private float mFontSizeMult = 1.0f;
John Reck78a6a1d2011-07-21 13:54:03 -0700101
Victoria Lease96497832012-03-23 14:19:56 -0700102 // Current state of network-dependent settings
103 private boolean mLinkPrefetchAllowed = true;
104
John Reck78a6a1d2011-07-21 13:54:03 -0700105 // Cached values
106 private int mPageCacheCapacity = 1;
107 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800108
John Reck35e9dd62011-04-25 09:01:54 -0700109 // Cached settings
110 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800111
Ben Murdochaaa1f372011-07-25 15:40:58 +0100112 private static String sFactoryResetUrl;
113
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700114 private boolean mEngineInitialized = false;
115 private boolean mSyncManagedSettings = false;
116
Bijan Amirzada3bb46302014-06-17 16:31:31 -0700117 public static synchronized void initialize(final Context context) {
118 if (sInstance == null)
119 sInstance = new BrowserSettings(context);
John Reck35e9dd62011-04-25 09:01:54 -0700120 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700122 public static BrowserSettings getInstance() {
John Reck35e9dd62011-04-25 09:01:54 -0700123 return sInstance;
124 }
Ben Murdochef671652010-11-25 17:17:58 +0000125
John Reck35e9dd62011-04-25 09:01:54 -0700126 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100127 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700128 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
John Reck35e9dd62011-04-25 09:01:54 -0700129 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckcadae722011-07-25 11:36:17 -0700130 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700131 }
132
133 public void setController(Controller controller) {
134 mController = controller;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700135 mNeedsSharedSync = true;
136 }
137
138 public void onEngineInitializationComplete() {
139 mEngineInitialized = true;
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700140
141 // Intialize Web Refiner only once
142 final WebRefiner refiner = WebRefiner.getInstance();
143 if (refiner != null) {
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700144 mPrefs.edit().putBoolean(PREF_WEB_REFINER, true).apply();
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700145 refiner.setDefaultPermission(PermissionsServiceFactory.getDefaultPermissions(
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700146 PermissionsServiceFactory.PermissionType.WEBREFINER));
147
148 PermissionsServiceFactory.getPermissionsService(
149 new ValueCallback<PermissionsServiceFactory.PermissionsService>() {
150 @Override
151 public void onReceiveValue(
152 PermissionsServiceFactory.PermissionsService value) {
153 Set<String> origins = value.getOrigins();
154 ArrayList<String> allowList = new ArrayList<>();
155 ArrayList<String> blockList = new ArrayList<>();
156 for (String origin : origins) {
157 PermissionsServiceFactory.PermissionsService.OriginInfo
158 info = value.getOriginInfo(origin);
Axesh R. Ajmera1ad7c572015-07-13 17:10:56 -0700159 if (info == null) {
160 continue;
161 }
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700162 int perm = info.getPermission(
163 PermissionsServiceFactory.PermissionType.WEBREFINER);
164 if (perm == PermissionsServiceFactory.Permission.ALLOW) {
165 allowList.add(origin);
166 } else if (perm == PermissionsServiceFactory.Permission.BLOCK) {
167 blockList.add(origin);
168 }
169 }
170 if (!allowList.isEmpty()) {
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700171 refiner.setPermissionForOrigins(
172 allowList.toArray(new String[allowList.size()]), true);
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700173 }
174
175 if (!blockList.isEmpty()) {
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700176 refiner.setPermissionForOrigins(
177 blockList.toArray(new String[blockList.size()]), false);
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700178 }
179 }
180 }
181 );
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700182 } else {
183 mPrefs.edit().putBoolean(PREF_WEB_REFINER, false).apply();
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700184 }
185
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700186 mAutofillHandler = new AutofillHandler(mContext);
187 if (mSyncManagedSettings) {
188 syncManagedSettings();
189 }
190 if (mNeedsSharedSync) {
John Reckc477e712011-08-17 11:27:15 -0700191 syncSharedSettings();
192 }
Dave Tharp4a2c8942015-07-10 14:10:51 -0700193
194 // Instantiate all MDM Restriction Singletons.
195 AutoFillRestriction.getInstance();
196 DevToolsRestriction.getInstance();
197 DoNotTrackRestriction.getInstance();
Dave Tharp13f3f7c2015-07-16 14:58:51 -0700198 DownloadDirRestriction.getInstance();
Dave Tharp4a2c8942015-07-10 14:10:51 -0700199 EditBookmarksRestriction.getInstance();
200 IncognitoRestriction.getInstance();
201 ManagedBookmarksRestriction.getInstance();
Panos Thomasa9ff2c72014-12-17 22:15:23 -0800202 ProxyRestriction.getInstance();
Dave Tharp4a2c8942015-07-10 14:10:51 -0700203 SearchEngineRestriction.getInstance();
204 ThirdPartyCookiesRestriction.getInstance();
205 URLFilterRestriction.getInstance();
Ben Murdochef671652010-11-25 17:17:58 +0000206 }
207
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800208 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800209
John Reckc477e712011-08-17 11:27:15 -0700210 if (mNeedsSharedSync) {
211 syncSharedSettings();
212 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800213
John Reck35e9dd62011-04-25 09:01:54 -0700214 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800215 syncStaticSettings(settings);
216 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700217 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800218 }
219 }
220
John Reckd1d87312012-03-08 13:25:00 -0800221 public void stopManagingSettings(WebSettings settings) {
222 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
223 while (iter.hasNext()) {
224 WeakReference<WebSettings> ref = iter.next();
225 if (ref.get() == settings) {
226 iter.remove();
227 return;
228 }
229 }
230 }
231
John Reckcadae722011-07-25 11:36:17 -0700232 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700233
234 @Override
235 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700236 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
237 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700238 // the cost of one cached page is ~3M (measured using nytimes.com). For
239 // low end devices, we only cache one page. For high end devices, we try
240 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800241
242 // SWE_TODO : assume a high-memory device
243 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700244 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800245 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700246 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
247 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
248 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700249 // Workaround b/5254577
250 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700251 if (Build.VERSION.CODENAME.equals("REL")) {
252 // This is a release build, always startup with debug disabled
253 setDebugEnabled(false);
254 }
255 if (mPrefs.contains(PREF_TEXT_SIZE)) {
256 /*
257 * Update from TextSize enum to zoom percent
258 * SMALLEST is 50%
259 * SMALLER is 75%
260 * NORMAL is 100%
261 * LARGER is 150%
262 * LARGEST is 200%
263 */
264 switch (getTextSize()) {
265 case SMALLEST:
266 setTextZoom(50);
267 break;
268 case SMALLER:
269 setTextZoom(75);
270 break;
271 case LARGER:
272 setTextZoom(150);
273 break;
274 case LARGEST:
275 setTextZoom(200);
276 break;
277 }
278 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
279 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100280
qqzhou8c5b0a32013-07-22 15:31:03 +0800281 // add for carrier homepage feature
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700282 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
qqzhou8c5b0a32013-07-22 15:31:03 +0800283
kaiyizbf086ea2013-08-02 11:03:58 +0800284 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
Tarun Nainani95b79682014-12-09 09:38:04 -0800285 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING, "auto").apply();
kaiyizbf086ea2013-08-02 11:03:58 +0800286 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700287
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700288 if (!mPrefs.contains(PREF_EDGE_SWIPE)) {
289 mPrefs.edit().putString(PREF_EDGE_SWIPE,
290 mContext.getResources().getString(
291 R.string.value_unknown_edge_swipe)).apply();
292 }
293
Ben Murdochaaa1f372011-07-25 15:40:58 +0100294 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
295 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
296 BrowserProvider.getClientId(mContext.getContentResolver()));
297 }
298
Ben Murdochaaa1f372011-07-25 15:40:58 +0100299 synchronized (BrowserSettings.class) {
300 sInitialized = true;
301 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700302 }
303 }
304 };
305
Ben Murdochaaa1f372011-07-25 15:40:58 +0100306 private static void requireInitialization() {
307 synchronized (BrowserSettings.class) {
308 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700309 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100310 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700311 } catch (InterruptedException e) {
312 }
313 }
314 }
315 }
316
The Android Open Source Project0c908882009-03-03 19:32:16 -0800317 /**
John Reck35e9dd62011-04-25 09:01:54 -0700318 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800319 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800320 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700321 settings.setGeolocationEnabled(enableGeolocation());
322 settings.setJavaScriptEnabled(enableJavascript());
323 settings.setLightTouchEnabled(enableLightTouch());
324 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700325 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
John Reck35e9dd62011-04-25 09:01:54 -0700326 settings.setMinimumFontSize(getMinimumFontSize());
327 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck7dc444b2011-06-16 17:44:29 -0700328 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700329 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100330 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700331 settings.setLoadsImagesAutomatically(loadImages());
332 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
333 settings.setSavePassword(rememberPasswords());
334 settings.setSaveFormData(saveFormdata());
335 settings.setUseWideViewPort(isWideViewport());
Panos Thomasb10bbda2014-06-23 10:18:36 -0700336 settings.setDoNotTrack(doNotTrack());
Yida Wang078425c2015-06-02 14:14:06 -0400337 settings.setNightModeEnabled(isNightModeEnabled());
Tarun Nainani8eb00912014-07-17 12:28:32 -0700338 settings.setMediaPlaybackRequiresUserGesture(false);
John Reck2fd9d0e2011-07-15 11:13:48 -0700339
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800340 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800341 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
342 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
343 settingsClassic.setForceUserScalable(forceEnableUserScalable());
344 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
345 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800346
John Reckea17e782011-10-27 17:15:59 -0700347 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800348 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700349 useInverted ? "true" : "false");
350 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800351 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700352 Float.toString(getInvertedContrast()));
353 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700354
John Reckea17e782011-10-27 17:15:59 -0700355 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800356 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700357 enableCpuUploadPath() ? "true" : "false");
358 }
Victoria Lease96497832012-03-23 14:19:56 -0700359
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800360 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000361 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800362
Tarun Nainani8eb00912014-07-17 12:28:32 -0700363
John Reck35e9dd62011-04-25 09:01:54 -0700364 /**
365 * Syncs all the settings that have no UI
366 * These cannot change, so we only need to set them once per WebSettings
367 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800368 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700369 settings.setDefaultFontSize(16);
370 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000371
John Reck35e9dd62011-04-25 09:01:54 -0700372 // WebView inside Browser doesn't want initial focus to be set.
373 settings.setNeedInitialFocus(false);
374 // Browser supports multiple windows
375 settings.setSupportMultipleWindows(true);
376 // enable smooth transition for better performance during panning or
377 // zooming
378 settings.setEnableSmoothTransition(true);
379 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700380 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700381
382 // HTML5 API flags
383 settings.setAppCacheEnabled(true);
384 settings.setDatabaseEnabled(true);
385 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700386
387 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700388 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700389 settings.setAppCachePath(getAppCachePath());
390 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
391 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700392 // origin policy for file access
393 settings.setAllowUniversalAccessFromFileURLs(false);
394 settings.setAllowFileAccessFromFileURLs(false);
Sudheer Koganti24766882014-10-02 10:58:09 -0700395 settings.setFullscreenSupported(true);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800396
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800397 //if (!(settings instanceof WebSettingsClassic)) return;
398 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800399
400 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
401 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
402 // WebView should be preserving the memory as much as possible.
403 // However, apps like browser wish to turn on the performance mode which
404 // would require more memory.
405 // TODO: We need to dynamically allocate/deallocate temporary memory for
406 // apps which are trying to use minimal memory. Currently, double
407 // buffering is always turned on, which is unnecessary.
408 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
409 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800410 */
John Reck35e9dd62011-04-25 09:01:54 -0700411 }
412
413 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700414 mNeedsSharedSync = false;
Axesh R. Ajmera579c70c2014-04-17 13:24:56 -0700415 CookieManager.getInstance().setAcceptCookie(acceptCookies());
Vivek Sekhared791da2015-02-22 12:39:05 -0800416
John Reck35e9dd62011-04-25 09:01:54 -0700417 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700418
John Reck35e9dd62011-04-25 09:01:54 -0700419 private void syncManagedSettings() {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700420 if (!mEngineInitialized) {
421 mSyncManagedSettings = true;
422 return;
423 }
424 mSyncManagedSettings = false;
John Reck35e9dd62011-04-25 09:01:54 -0700425 syncSharedSettings();
426 synchronized (mManagedSettings) {
427 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
428 while (iter.hasNext()) {
429 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800430 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700431 if (settings == null) {
432 iter.remove();
433 continue;
434 }
435 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000436 }
John Reck35e9dd62011-04-25 09:01:54 -0700437 }
438 }
Ben Murdochef671652010-11-25 17:17:58 +0000439
John Reck35e9dd62011-04-25 09:01:54 -0700440 @Override
441 public void onSharedPreferenceChanged(
442 SharedPreferences sharedPreferences, String key) {
443 syncManagedSettings();
444 if (PREF_SEARCH_ENGINE.equals(key)) {
445 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700446 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200447 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700448 mController.getUi().setFullscreen(useFullscreen());
449 }
Victoria Lease96497832012-03-23 14:19:56 -0700450 } else if (PREF_LINK_PREFETCH.equals(key)) {
451 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700452 }
John Reck35e9dd62011-04-25 09:01:54 -0700453 }
454
John Reck961d35d2011-06-23 09:45:54 -0700455 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100456 requireInitialization();
457 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700458 }
459
460 public LayoutAlgorithm getLayoutAlgorithm() {
461 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
462 if (autofitPages()) {
Tarun Nainani003bec52014-07-02 02:20:34 -0700463 layoutAlgorithm = LayoutAlgorithm.TEXT_AUTOSIZING;
John Reck35e9dd62011-04-25 09:01:54 -0700464 }
465 if (isDebugEnabled()) {
466 if (isSmallScreen()) {
467 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000468 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700469 if (isNormalLayout()) {
470 layoutAlgorithm = LayoutAlgorithm.NORMAL;
471 } else {
472 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
473 }
Ben Murdochef671652010-11-25 17:17:58 +0000474 }
John Reck35e9dd62011-04-25 09:01:54 -0700475 }
476 return layoutAlgorithm;
477 }
Ben Murdochef671652010-11-25 17:17:58 +0000478
John Reck35e9dd62011-04-25 09:01:54 -0700479 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700480 requireInitialization();
481 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800482 }
483
John Reck35e9dd62011-04-25 09:01:54 -0700484 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700485 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700486 return mWebStorageSizeManager;
487 }
488
John Reck35e9dd62011-04-25 09:01:54 -0700489 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700490 if (mAppCachePath == null) {
491 mAppCachePath = mContext.getDir("appcache", 0).getPath();
492 }
493 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700494 }
495
496 private void updateSearchEngine(boolean force) {
497 String searchEngineName = getSearchEngineName();
498 if (force || mSearchEngine == null ||
499 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700500 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000501 }
502 }
503
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100504 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700505 if (mSearchEngine == null) {
506 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100507 }
John Reck35e9dd62011-04-25 09:01:54 -0700508 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100509 }
510
John Reck35e9dd62011-04-25 09:01:54 -0700511 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700512 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700513 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100514 }
515
John Reck35e9dd62011-04-25 09:01:54 -0700516 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700517 Editor edit = mPrefs.edit();
518 edit.putBoolean(PREF_DEBUG_MENU, value);
519 if (!value) {
520 // Reset to "safe" value
521 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
522 }
523 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100524 }
525
John Reck35e9dd62011-04-25 09:01:54 -0700526 public void clearCache() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700527 if (mController != null) {
528 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800529 if (current != null) {
530 current.clearCache(true);
531 }
532 }
533 }
534
John Reck35e9dd62011-04-25 09:01:54 -0700535 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800536 CookieManager.getInstance().removeAllCookie();
537 }
538
John Reck35e9dd62011-04-25 09:01:54 -0700539 public void clearHistory() {
540 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800541 Browser.clearHistory(resolver);
542 Browser.clearSearches(resolver);
543 }
544
John Reck35e9dd62011-04-25 09:01:54 -0700545 public void clearFormData() {
546 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700547 if (mController!= null) {
548 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100549 if (currentTopView != null) {
550 currentTopView.clearFormData();
551 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800552 }
553 }
554
Bijan Amirzada3bbb3a42014-06-17 12:06:18 -0700555 public WebView getTopWebView(){
556 if (mController!= null)
557 return mController.getCurrentTopWebView();
558
559 return null;
560 }
561
John Reck35e9dd62011-04-25 09:01:54 -0700562 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700563 // Clear password store maintained by SWE engine
564 WebSettings settings = null;
565 // find a valid settings object
566 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
567 while (iter.hasNext()) {
568 WeakReference<WebSettings> ref = iter.next();
569 settings = (WebSettings)ref.get();
570 if (settings != null) {
571 break;
572 }
573 }
574 if (settings != null) {
Panos Thomasdbaea4e2014-05-22 06:48:47 -0700575 settings.clearPasswords();
Panos Thomasfa948b82014-03-09 03:42:42 -0700576 }
Vivek Sekhare8a8ec22014-07-10 14:52:43 -0700577
578 // Clear passwords in WebView database
579 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
580 db.clearHttpAuthUsernamePassword();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800581 }
582
John Reck35e9dd62011-04-25 09:01:54 -0700583 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100584 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100585 }
586
John Reck35e9dd62011-04-25 09:01:54 -0700587 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100588 GeolocationPermissions.getInstance().clearAll();
Panos Thomasb298aad2014-10-22 12:24:21 -0700589 if (GeolocationPermissions.isIncognitoCreated()) {
590 GeolocationPermissions.getIncognitoInstance().clearAll();
591 }
Nicolas Roard78a98e42009-05-11 13:34:17 +0100592 }
593
John Reck35e9dd62011-04-25 09:01:54 -0700594 public void resetDefaultPreferences() {
Pankaj Gargf320d6e2015-08-10 09:56:57 -0700595 WebRefiner webRefiner = WebRefiner.getInstance();
596 if (webRefiner != null) {
597 List<String> webrefiner_list = PermissionsServiceFactory.getOriginsForPermission(
598 PermissionsServiceFactory.PermissionType.WEBREFINER);
599 if (!webrefiner_list.isEmpty()) {
600 String[] origins = webrefiner_list.toArray(new String[webrefiner_list.size()]);
601 webRefiner.useDefaultPermissionForOrigins(origins);
602 }
603 }
604
605 PermissionsServiceFactory.resetDefaultPermissions();
606 mPrefs.edit().clear().apply();
607
Björn Isakssonc885a242012-06-05 17:19:04 +0200608 resetCachedValues();
Pankaj Gargf320d6e2015-08-10 09:56:57 -0700609
610 if (webRefiner != null) {
Pankaj Garg1e967142015-08-07 09:05:10 -0700611 mPrefs.edit().putBoolean(PREF_WEB_REFINER, true).apply();
612 } else {
613 mPrefs.edit().putBoolean(PREF_WEB_REFINER, false).apply();
614 }
John Reck35e9dd62011-04-25 09:01:54 -0700615 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700616 }
617
Björn Isakssonc885a242012-06-05 17:19:04 +0200618 private void resetCachedValues() {
619 updateSearchEngine(false);
620 }
621
John Reck35e9dd62011-04-25 09:01:54 -0700622 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800623 // query the profile from components autofill database 524
624 if (mAutofillHandler.mAutoFillProfile == null &&
625 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
626 WebSettings settings = null;
627 // find a valid settings object
628 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
629 while (iter.hasNext()) {
630 WeakReference<WebSettings> ref = iter.next();
631 settings = (WebSettings)ref.get();
632 if (settings != null) {
633 break;
634 }
635 }
636 if (settings != null) {
637 AutoFillProfile profile =
638 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
639 mAutofillHandler.setAutoFillProfile(profile);
640 }
641 }
John Reck35e9dd62011-04-25 09:01:54 -0700642 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800643 }
644
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800645 public String getAutoFillProfileId() {
646 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800647 }
648
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800649 public void updateAutoFillProfile(AutoFillProfile profile) {
650 syncAutoFillProfile(profile);
651 }
652
653 private void syncAutoFillProfile(AutoFillProfile profile) {
654 synchronized (mManagedSettings) {
655 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
656 while (iter.hasNext()) {
657 WeakReference<WebSettings> ref = iter.next();
658 WebSettings settings = (WebSettings)ref.get();
659 if (settings == null) {
660 iter.remove();
661 continue;
662 }
663 // update the profile only once.
664 settings.setAutoFillProfile(profile);
665 // Now we should have the guid
666 mAutofillHandler.setAutoFillProfile(profile);
667 break;
668 }
669 }
670 }
John Reck35e9dd62011-04-25 09:01:54 -0700671 public void toggleDebugSettings() {
672 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800673 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100674
John Reckb8b2af82011-05-20 15:58:33 -0700675 public boolean hasDesktopUseragent(WebView view) {
Vivek Sekhar7b3d2da2014-11-13 16:20:11 -0800676 return view != null && view.getUseDesktopUserAgent();
John Reckb8b2af82011-05-20 15:58:33 -0700677 }
678
679 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700680 if (view == null) {
681 return;
682 }
Vivek Sekhar7b3d2da2014-11-13 16:20:11 -0800683 if (hasDesktopUseragent(view))
684 view.setUseDesktopUserAgent(false, true);
685 else
686 view.setUseDesktopUserAgent(true, true);
John Reckb8b2af82011-05-20 15:58:33 -0700687 }
688
John Reck7dc444b2011-06-16 17:44:29 -0700689 public static int getAdjustedMinimumFontSize(int rawValue) {
690 rawValue++; // Preference starts at 0, min font at 1
691 if (rawValue > 1) {
692 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
693 }
694 return rawValue;
695 }
696
John Reck5ba3c762011-09-14 15:00:15 -0700697 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700698 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700699 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700700 }
701
702 static int getRawTextZoom(int percent) {
703 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
704 }
705
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700706 public int getAdjustedDoubleTapZoom(int rawValue) {
707 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
708 return (int) ((rawValue + 100) * mFontSizeMult);
709 }
710
711 static int getRawDoubleTapZoom(int percent) {
712 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
713 }
714
John Reckcadae722011-07-25 11:36:17 -0700715 public SharedPreferences getPreferences() {
716 return mPrefs;
717 }
718
Victoria Lease96497832012-03-23 14:19:56 -0700719 // update connectivity-dependent options
720 public void updateConnectionType() {
721 ConnectivityManager cm = (ConnectivityManager)
722 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
723 String linkPrefetchPreference = getLinkPrefetchEnabled();
724 boolean linkPrefetchAllowed = linkPrefetchPreference.
725 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
726 NetworkInfo ni = cm.getActiveNetworkInfo();
727 if (ni != null) {
728 switch (ni.getType()) {
729 case ConnectivityManager.TYPE_WIFI:
730 case ConnectivityManager.TYPE_ETHERNET:
731 case ConnectivityManager.TYPE_BLUETOOTH:
732 linkPrefetchAllowed |= linkPrefetchPreference.
733 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
734 break;
735 case ConnectivityManager.TYPE_MOBILE:
736 case ConnectivityManager.TYPE_MOBILE_DUN:
737 case ConnectivityManager.TYPE_MOBILE_MMS:
738 case ConnectivityManager.TYPE_MOBILE_SUPL:
739 case ConnectivityManager.TYPE_WIMAX:
740 default:
741 break;
742 }
743 }
744 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
745 mLinkPrefetchAllowed = linkPrefetchAllowed;
746 syncManagedSettings();
747 }
748 }
749
luxiaol62677b02013-07-22 07:54:49 +0800750 public String getDownloadPath() {
751 return mPrefs.getString(PREF_DOWNLOAD_PATH,
752 DownloadHandler.getDefaultDownloadPath(mContext));
753 }
John Reck35e9dd62011-04-25 09:01:54 -0700754 // -----------------------------
755 // getter/setters for accessibility_preferences.xml
756 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100757
John Reck7dc444b2011-06-16 17:44:29 -0700758 @Deprecated
759 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700760 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
761 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100762 }
763
John Reck35e9dd62011-04-25 09:01:54 -0700764 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700765 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700766 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100767 }
768
John Reck92f25f82011-04-26 16:57:10 -0700769 public boolean forceEnableUserScalable() {
770 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
771 }
772
John Reck7dc444b2011-06-16 17:44:29 -0700773 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700774 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700775 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
776 return getAdjustedTextZoom(textZoom);
777 }
778
779 public void setTextZoom(int percent) {
780 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
781 }
782
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700783 public int getDoubleTapZoom() {
784 requireInitialization();
785 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
786 return getAdjustedDoubleTapZoom(doubleTapZoom);
787 }
788
789 public void setDoubleTapZoom(int percent) {
790 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
791 }
792
John Reck35e9dd62011-04-25 09:01:54 -0700793 // -----------------------------
794 // getter/setters for advanced_preferences.xml
795 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100796
John Reck35e9dd62011-04-25 09:01:54 -0700797 public String getSearchEngineName() {
Panos Thomas64c77e02014-12-17 22:15:23 -0800798 // The following is a NOP if the SEARCH_ENGINE restriction has already been created. Otherwise,
799 // it creates the restriction and if enabled it sets the <default_search_engine_value>.
800 SearchEngineRestriction.getInstance();
801
luxiaol221b3932013-07-19 15:27:57 +0800802 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
803 if (defaultSearchEngineValue == null) {
804 defaultSearchEngineValue = SearchEngine.GOOGLE;
805 }
806 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100807 }
John Reck63bb6872010-12-01 19:29:32 -0800808
Michael Kolb8d772b02012-01-19 13:56:00 -0800809 public boolean allowAppTabs() {
810 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
811 }
812
John Reck35e9dd62011-04-25 09:01:54 -0700813 public boolean openInBackground() {
814 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800815 }
John Reck35e9dd62011-04-25 09:01:54 -0700816
817 public boolean enableJavascript() {
818 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
819 }
820
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800821 public boolean enableMemoryMonitor() {
822 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
823 }
824
Tarun Nainani8eb00912014-07-17 12:28:32 -0700825
John Reck35e9dd62011-04-25 09:01:54 -0700826 public boolean loadPageInOverviewMode() {
827 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
828 }
829
830 public boolean autofitPages() {
831 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
832 }
833
834 public boolean blockPopupWindows() {
Pankaj Garg11015212015-07-06 14:38:43 -0700835 return !PermissionsServiceFactory.getDefaultPermissions(
836 PermissionsServiceFactory.PermissionType.POPUP);
John Reck35e9dd62011-04-25 09:01:54 -0700837 }
838
839 public boolean loadImages() {
840 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
841 }
842
843 public String getDefaultTextEncoding() {
Tarun Nainani88381422015-04-02 19:47:21 -0700844 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, "auto");
John Reck35e9dd62011-04-25 09:01:54 -0700845 }
846
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700847 public String getEdgeSwipeAction() {
848 return mPrefs.getString(PREF_EDGE_SWIPE,
849 mContext.getResources().getString(R.string.value_unknown_edge_swipe));
850 }
851
852 public void setEdgeSwipeTemporal() {
853 mPrefs.edit().putString(PREF_EDGE_SWIPE,
854 mContext.getResources().getString(R.string.value_temporal_edge_swipe)).apply();
855 }
856
857 public void setEdgeSwipeSpatial() {
858 mPrefs.edit().putString(PREF_EDGE_SWIPE,
859 mContext.getResources().getString(R.string.value_spatial_edge_swipe)).apply();
860 }
861
862 public void setEdgeSwipeDisabled() {
863 mPrefs.edit().putString(PREF_EDGE_SWIPE,
864 mContext.getResources().getString(R.string.value_disable_edge_swipe)).apply();
865 }
866
John Reck35e9dd62011-04-25 09:01:54 -0700867 // -----------------------------
868 // getter/setters for general_preferences.xml
869 // -----------------------------
870
871 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700872 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
873 }
874
875 public void setHomePage(String value) {
876 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
877 }
878
879 public boolean isAutofillEnabled() {
880 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
881 }
882
883 public void setAutofillEnabled(boolean value) {
884 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
885 }
886
Site Maoabb7bd32015-03-20 15:34:02 -0700887 public boolean isPowerSaveModeEnabled() {
888 return mPrefs.getBoolean(PREF_POWERSAVE_ENABLED, false);
889 }
890
891 public void setPowerSaveModeEnabled(boolean value) {
892 mPrefs.edit().putBoolean(PREF_POWERSAVE_ENABLED, value).apply();
893 }
894
Yida Wang078425c2015-06-02 14:14:06 -0400895 public boolean isNightModeEnabled() {
896 return mPrefs.getBoolean(PREF_NIGHTMODE_ENABLED, false);
897 }
898
899 public void setNightModeEnabled(boolean value) {
900 mPrefs.edit().putBoolean(PREF_NIGHTMODE_ENABLED, value).apply();
901 }
902
John Reck35e9dd62011-04-25 09:01:54 -0700903 // -----------------------------
904 // getter/setters for debug_preferences.xml
905 // -----------------------------
906
907 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700908 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700909 return true;
910 }
911 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
912 }
913
Derek Sollenberger31adf672011-07-08 11:31:30 -0400914 public boolean isSkiaHardwareAccelerated() {
915 if (!isDebugEnabled()) {
916 return false;
917 }
918 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
919 }
920
Site Mao7521b092015-07-20 18:27:20 -0700921 public boolean isDisablePerfFeatures() {
922 // This value is flipped in the prefs.
923 return !mPrefs.getBoolean(PREF_DISABLE_PERF, true);
924 }
925
John Reck35e9dd62011-04-25 09:01:54 -0700926 // -----------------------------
927 // getter/setters for hidden_debug_preferences.xml
928 // -----------------------------
929
930 public boolean enableVisualIndicator() {
931 if (!isDebugEnabled()) {
932 return false;
933 }
934 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
935 }
936
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700937 public boolean enableCpuUploadPath() {
938 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700939 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700940 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700941 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700942 }
943
John Reck35e9dd62011-04-25 09:01:54 -0700944 public boolean isSmallScreen() {
945 if (!isDebugEnabled()) {
946 return false;
947 }
948 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
949 }
950
951 public boolean isWideViewport() {
952 if (!isDebugEnabled()) {
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800953 return true;
John Reck35e9dd62011-04-25 09:01:54 -0700954 }
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800955 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
John Reck35e9dd62011-04-25 09:01:54 -0700956 }
957
958 public boolean isNormalLayout() {
959 if (!isDebugEnabled()) {
960 return false;
961 }
962 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
963 }
964
965 public boolean isTracing() {
966 if (!isDebugEnabled()) {
967 return false;
968 }
969 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
970 }
971
972 public boolean enableLightTouch() {
973 if (!isDebugEnabled()) {
974 return false;
975 }
976 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
977 }
978
979 public boolean enableNavDump() {
980 if (!isDebugEnabled()) {
981 return false;
982 }
983 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
984 }
985
986 public String getJsEngineFlags() {
987 if (!isDebugEnabled()) {
988 return "";
989 }
990 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
991 }
992
993 // -----------------------------
994 // getter/setters for lab_preferences.xml
995 // -----------------------------
996
John Reck35e9dd62011-04-25 09:01:54 -0700997 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700998 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700999 }
1000
Michael Kolbc38c6042011-04-27 10:46:06 -07001001 public boolean useFullscreen() {
1002 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
1003 }
1004
John Reck2fd9d0e2011-07-15 11:13:48 -07001005 public boolean useInvertedRendering() {
1006 return mPrefs.getBoolean(PREF_INVERTED, false);
1007 }
1008
Nicolas Roard5d513102011-08-03 15:35:34 -07001009 public float getInvertedContrast() {
1010 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
1011 }
1012
John Reck35e9dd62011-04-25 09:01:54 -07001013 // -----------------------------
1014 // getter/setters for privacy_security_preferences.xml
1015 // -----------------------------
1016
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001017 public boolean doNotTrack() {
Dave Tharpb487d5d2015-05-15 15:54:08 -07001018 boolean dntVal;
1019 if (DoNotTrackRestriction.getInstance().isEnabled()) {
1020 dntVal = DoNotTrackRestriction.getInstance().getValue();
1021 }
1022 else {
1023 dntVal = mPrefs.getBoolean(PREF_DO_NOT_TRACK, true);
1024 }
1025 return dntVal;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001026 }
1027
John Reck35e9dd62011-04-25 09:01:54 -07001028 public boolean acceptCookies() {
Pankaj Garg32e1b942015-06-03 18:13:24 -07001029 return PermissionsServiceFactory.getDefaultPermissions(
1030 PermissionsServiceFactory.PermissionType.COOKIE);
John Reck35e9dd62011-04-25 09:01:54 -07001031 }
1032
1033 public boolean saveFormdata() {
1034 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
1035 }
1036
1037 public boolean enableGeolocation() {
1038 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
1039 }
1040
1041 public boolean rememberPasswords() {
1042 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
1043 }
1044
Michael Kolb14612442011-06-24 13:06:29 -07001045 // -----------------------------
1046 // getter/setters for bandwidth_preferences.xml
1047 // -----------------------------
1048
Mathew Inwood467813f2011-09-02 15:43:17 +01001049 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
1050 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -07001051 }
Mathew Inwood467813f2011-09-02 15:43:17 +01001052
1053 public static String getPreloadAlwaysPreferenceString(Context context) {
1054 return context.getResources().getString(R.string.pref_data_preload_value_always);
1055 }
1056
Mathew Inwood825fba72011-10-04 14:52:52 +01001057 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1058 "browser_default_preload_setting";
1059
1060 public String getDefaultPreloadSetting() {
1061 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1062 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1063 if (preload == null) {
1064 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1065 }
1066 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001067 }
1068
1069 public String getPreloadEnabled() {
1070 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1071 }
1072
Victoria Lease96497832012-03-23 14:19:56 -07001073 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1074 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1075 }
1076
1077 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1078 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1079 }
1080
1081 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1082 "browser_default_link_prefetch_setting";
1083
1084 public String getDefaultLinkPrefetchSetting() {
1085 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1086 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1087 if (preload == null) {
1088 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1089 }
1090 return preload;
1091 }
1092
1093 public String getLinkPrefetchEnabled() {
1094 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1095 }
1096
George Mount3636d0a2011-11-21 09:08:21 -08001097 // -----------------------------
1098 // getter/setters for browser recovery
1099 // -----------------------------
1100 /**
1101 * The last time browser was started.
1102 * @return The last browser start time as System.currentTimeMillis. This
1103 * can be 0 if this is the first time or the last tab was closed.
1104 */
1105 public long getLastRecovered() {
1106 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1107 }
1108
1109 /**
1110 * Sets the last browser start time.
1111 * @param time The last time as System.currentTimeMillis that the browser
1112 * was started. This should be set to 0 if the last tab is closed.
1113 */
1114 public void setLastRecovered(long time) {
1115 mPrefs.edit()
1116 .putLong(KEY_LAST_RECOVERED, time)
1117 .apply();
1118 }
1119
1120 /**
1121 * Used to determine whether or not the previous browser run crashed. Once
1122 * the previous state has been determined, the value will be set to false
1123 * until a pause is received.
1124 * @return true if the last browser run was paused or false if it crashed.
1125 */
1126 public boolean wasLastRunPaused() {
1127 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1128 }
1129
1130 /**
1131 * Sets whether or not the last run was a pause or crash.
1132 * @param isPaused Set to true When a pause is received or false after
1133 * resuming.
1134 */
1135 public void setLastRunPaused(boolean isPaused) {
1136 mPrefs.edit()
1137 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1138 .apply();
1139 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001140}