blob: 8042efd3ffbf132839c814242ec1f305840ae5da [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());
John Reck35e9dd62011-04-25 09:01:54 -0700335 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
Vivek Sekharce94c2a2015-10-14 13:24:24 -0700496 protected void updateSearchEngine(boolean force) {
John Reck35e9dd62011-04-25 09:01:54 -0700497 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,
Karthikeyan Periasamyd7f751b2016-02-15 21:00:55 +0100752 DownloadHandler.getDefaultDownloadPath(mContext, null));
luxiaol62677b02013-07-22 07:54:49 +0800753 }
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
Vivek Sekharce94c2a2015-10-14 13:24:24 -0700802 String defaultSearchEngineValue = getUserSearchEngine();
luxiaol221b3932013-07-19 15:27:57 +0800803 if (defaultSearchEngineValue == null) {
804 defaultSearchEngineValue = SearchEngine.GOOGLE;
805 }
Vivek Sekharce94c2a2015-10-14 13:24:24 -0700806 return 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
Vivek Sekharce94c2a2015-10-14 13:24:24 -0700857 public void setUserSearchEngine(String engine) {
858 if (!engine.equals(mContext.getResources().getString(R.string.default_search_engine_value)))
859 mPrefs.edit().putString(
860 PREF_USER_SEARCH_ENGINE + mContext.getResources().getConfiguration().locale,
861 engine).apply();
862 // ensure changing back to default clears the search engine
863 else
864 mPrefs.edit().remove(PREF_USER_SEARCH_ENGINE
865 + mContext.getResources().getConfiguration().locale).apply();
866 }
867
868 public String getUserSearchEngine() {
869 return mPrefs.getString(
870 PREF_USER_SEARCH_ENGINE + mContext.getResources().getConfiguration().locale
871 , mContext.getResources().getString(R.string.default_search_engine_value));
872 }
873
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700874 public void setEdgeSwipeSpatial() {
875 mPrefs.edit().putString(PREF_EDGE_SWIPE,
876 mContext.getResources().getString(R.string.value_spatial_edge_swipe)).apply();
877 }
878
879 public void setEdgeSwipeDisabled() {
880 mPrefs.edit().putString(PREF_EDGE_SWIPE,
881 mContext.getResources().getString(R.string.value_disable_edge_swipe)).apply();
882 }
883
John Reck35e9dd62011-04-25 09:01:54 -0700884 // -----------------------------
885 // getter/setters for general_preferences.xml
886 // -----------------------------
887
888 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700889 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
890 }
891
892 public void setHomePage(String value) {
893 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
894 }
895
896 public boolean isAutofillEnabled() {
897 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
898 }
899
900 public void setAutofillEnabled(boolean value) {
901 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
902 }
903
jrizzolibe2afc52015-10-28 18:43:00 +0100904 public boolean isColoredSBEnabled() {
905 return mPrefs.getBoolean(PREF_COLOREDSB_ENABLED, false);
906 }
907
908 public void setColoredSBEnabled(boolean value) {
909 mPrefs.edit().putBoolean(PREF_COLOREDSB_ENABLED, value).apply();
910 }
911
jrizzoli7924a0c2016-02-05 14:30:14 +0100912 public boolean isLookLockEnabled() {
913 return mPrefs.getBoolean(PREF_LOOKLOCK_ENABLED, false);
914 }
915
916 public void setLookLockEnabled(boolean value) {
917 mPrefs.edit().putBoolean(PREF_LOOKLOCK_ENABLED, value).apply();
918 }
919
Site Maoabb7bd32015-03-20 15:34:02 -0700920 public boolean isPowerSaveModeEnabled() {
921 return mPrefs.getBoolean(PREF_POWERSAVE_ENABLED, false);
922 }
923
924 public void setPowerSaveModeEnabled(boolean value) {
925 mPrefs.edit().putBoolean(PREF_POWERSAVE_ENABLED, value).apply();
926 }
927
Yida Wang078425c2015-06-02 14:14:06 -0400928 public boolean isNightModeEnabled() {
929 return mPrefs.getBoolean(PREF_NIGHTMODE_ENABLED, false);
930 }
931
932 public void setNightModeEnabled(boolean value) {
933 mPrefs.edit().putBoolean(PREF_NIGHTMODE_ENABLED, value).apply();
934 }
935
John Reck35e9dd62011-04-25 09:01:54 -0700936 // -----------------------------
937 // getter/setters for debug_preferences.xml
938 // -----------------------------
939
940 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700941 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700942 return true;
943 }
944 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
945 }
946
Derek Sollenberger31adf672011-07-08 11:31:30 -0400947 public boolean isSkiaHardwareAccelerated() {
948 if (!isDebugEnabled()) {
949 return false;
950 }
951 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
952 }
953
Site Mao7521b092015-07-20 18:27:20 -0700954 public boolean isDisablePerfFeatures() {
955 // This value is flipped in the prefs.
956 return !mPrefs.getBoolean(PREF_DISABLE_PERF, true);
957 }
958
John Reck35e9dd62011-04-25 09:01:54 -0700959 // -----------------------------
960 // getter/setters for hidden_debug_preferences.xml
961 // -----------------------------
962
963 public boolean enableVisualIndicator() {
964 if (!isDebugEnabled()) {
965 return false;
966 }
967 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
968 }
969
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700970 public boolean enableCpuUploadPath() {
971 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700972 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700973 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700974 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700975 }
976
John Reck35e9dd62011-04-25 09:01:54 -0700977 public boolean isSmallScreen() {
978 if (!isDebugEnabled()) {
979 return false;
980 }
981 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
982 }
983
984 public boolean isWideViewport() {
985 if (!isDebugEnabled()) {
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800986 return true;
John Reck35e9dd62011-04-25 09:01:54 -0700987 }
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800988 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
John Reck35e9dd62011-04-25 09:01:54 -0700989 }
990
991 public boolean isNormalLayout() {
992 if (!isDebugEnabled()) {
993 return false;
994 }
995 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
996 }
997
998 public boolean isTracing() {
999 if (!isDebugEnabled()) {
1000 return false;
1001 }
1002 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
1003 }
1004
1005 public boolean enableLightTouch() {
1006 if (!isDebugEnabled()) {
1007 return false;
1008 }
1009 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
1010 }
1011
1012 public boolean enableNavDump() {
1013 if (!isDebugEnabled()) {
1014 return false;
1015 }
1016 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
1017 }
1018
1019 public String getJsEngineFlags() {
1020 if (!isDebugEnabled()) {
1021 return "";
1022 }
1023 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
1024 }
1025
1026 // -----------------------------
1027 // getter/setters for lab_preferences.xml
1028 // -----------------------------
1029
John Reck35e9dd62011-04-25 09:01:54 -07001030 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -07001031 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -07001032 }
1033
Michael Kolbc38c6042011-04-27 10:46:06 -07001034 public boolean useFullscreen() {
1035 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
1036 }
1037
John Reck2fd9d0e2011-07-15 11:13:48 -07001038 public boolean useInvertedRendering() {
1039 return mPrefs.getBoolean(PREF_INVERTED, false);
1040 }
1041
Nicolas Roard5d513102011-08-03 15:35:34 -07001042 public float getInvertedContrast() {
1043 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
1044 }
1045
John Reck35e9dd62011-04-25 09:01:54 -07001046 // -----------------------------
1047 // getter/setters for privacy_security_preferences.xml
1048 // -----------------------------
1049
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001050 public boolean doNotTrack() {
Dave Tharpb487d5d2015-05-15 15:54:08 -07001051 boolean dntVal;
1052 if (DoNotTrackRestriction.getInstance().isEnabled()) {
1053 dntVal = DoNotTrackRestriction.getInstance().getValue();
1054 }
1055 else {
1056 dntVal = mPrefs.getBoolean(PREF_DO_NOT_TRACK, true);
1057 }
1058 return dntVal;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001059 }
1060
John Reck35e9dd62011-04-25 09:01:54 -07001061 public boolean acceptCookies() {
Pankaj Garg32e1b942015-06-03 18:13:24 -07001062 return PermissionsServiceFactory.getDefaultPermissions(
1063 PermissionsServiceFactory.PermissionType.COOKIE);
John Reck35e9dd62011-04-25 09:01:54 -07001064 }
1065
John Reck35e9dd62011-04-25 09:01:54 -07001066 public boolean enableGeolocation() {
1067 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
1068 }
1069
1070 public boolean rememberPasswords() {
1071 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
1072 }
1073
Michael Kolb14612442011-06-24 13:06:29 -07001074 // -----------------------------
1075 // getter/setters for bandwidth_preferences.xml
1076 // -----------------------------
1077
Mathew Inwood467813f2011-09-02 15:43:17 +01001078 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
1079 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -07001080 }
Mathew Inwood467813f2011-09-02 15:43:17 +01001081
1082 public static String getPreloadAlwaysPreferenceString(Context context) {
1083 return context.getResources().getString(R.string.pref_data_preload_value_always);
1084 }
1085
Mathew Inwood825fba72011-10-04 14:52:52 +01001086 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1087 "browser_default_preload_setting";
1088
1089 public String getDefaultPreloadSetting() {
1090 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1091 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1092 if (preload == null) {
1093 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1094 }
1095 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001096 }
1097
1098 public String getPreloadEnabled() {
1099 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1100 }
1101
Victoria Lease96497832012-03-23 14:19:56 -07001102 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1103 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1104 }
1105
1106 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1107 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1108 }
1109
1110 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1111 "browser_default_link_prefetch_setting";
1112
1113 public String getDefaultLinkPrefetchSetting() {
1114 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1115 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1116 if (preload == null) {
1117 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1118 }
1119 return preload;
1120 }
1121
1122 public String getLinkPrefetchEnabled() {
1123 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1124 }
1125
George Mount3636d0a2011-11-21 09:08:21 -08001126 // -----------------------------
1127 // getter/setters for browser recovery
1128 // -----------------------------
1129 /**
1130 * The last time browser was started.
1131 * @return The last browser start time as System.currentTimeMillis. This
1132 * can be 0 if this is the first time or the last tab was closed.
1133 */
1134 public long getLastRecovered() {
1135 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1136 }
1137
1138 /**
1139 * Sets the last browser start time.
1140 * @param time The last time as System.currentTimeMillis that the browser
1141 * was started. This should be set to 0 if the last tab is closed.
1142 */
1143 public void setLastRecovered(long time) {
1144 mPrefs.edit()
1145 .putLong(KEY_LAST_RECOVERED, time)
1146 .apply();
1147 }
1148
1149 /**
1150 * Used to determine whether or not the previous browser run crashed. Once
1151 * the previous state has been determined, the value will be set to false
1152 * until a pause is received.
1153 * @return true if the last browser run was paused or false if it crashed.
1154 */
1155 public boolean wasLastRunPaused() {
1156 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1157 }
1158
1159 /**
1160 * Sets whether or not the last run was a pause or crash.
1161 * @param isPaused Set to true When a pause is received or false after
1162 * resuming.
1163 */
1164 public void setLastRunPaused(boolean isPaused) {
1165 mPrefs.edit()
1166 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1167 .apply();
1168 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001169}