blob: 6ee8e298f0bfe691522133af222ad8b82519975e [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;
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -070054import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080055
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080056import org.codeaurora.swe.AutoFillProfile;
57import org.codeaurora.swe.CookieManager;
58import org.codeaurora.swe.GeolocationPermissions;
Pankaj Garg32e1b942015-06-03 18:13:24 -070059import org.codeaurora.swe.PermissionsServiceFactory;
Kulanthaivel Palanichamyf36e1db2015-04-08 16:11:06 -070060import org.codeaurora.swe.WebRefiner;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080061import org.codeaurora.swe.WebSettings.LayoutAlgorithm;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080062import org.codeaurora.swe.WebSettings.TextSize;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080063import org.codeaurora.swe.WebSettings;
64import org.codeaurora.swe.WebView;
Vivek Sekhare8a8ec22014-07-10 14:52:43 -070065import org.codeaurora.swe.WebViewDatabase;
Panos Thomas3f35d8d2014-11-08 22:40:23 -080066
John Reck35e9dd62011-04-25 09:01:54 -070067/**
68 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080069 */
John Reck35e9dd62011-04-25 09:01:54 -070070public class BrowserSettings implements OnSharedPreferenceChangeListener,
71 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010072
qqzhoue6ff8b42013-07-23 17:28:48 +080073 private static final String TAG = "BrowserSettings";
John Reck8fc22a12011-06-16 14:57:41 -070074 // The minimum min font size
75 // Aka, the lower bounds for the min font size range
76 // which is 1:5..24
77 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070078 // The initial value in the text zoom range
79 // This is what represents 100% in the SeekBarPreference range
80 private static final int TEXT_ZOOM_START_VAL = 10;
81 // The size of a single step in the text zoom range, in percent
82 private static final int TEXT_ZOOM_STEP = 5;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -070083 // The initial value in the double tap zoom range
84 // This is what represents 100% in the SeekBarPreference range
85 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
86 // The size of a single step in the double tap zoom range, in percent
87 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -070088
John Reck35e9dd62011-04-25 09:01:54 -070089 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -070090
John Reck35e9dd62011-04-25 09:01:54 -070091 private Context mContext;
92 private SharedPreferences mPrefs;
93 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -070094 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -070095 private WebStorageSizeManager mWebStorageSizeManager;
96 private AutofillHandler mAutofillHandler;
Ben Murdochaaa1f372011-07-25 15:40:58 +010097 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -070098 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -070099 private float mFontSizeMult = 1.0f;
John Reck78a6a1d2011-07-21 13:54:03 -0700100
Victoria Lease96497832012-03-23 14:19:56 -0700101 // Current state of network-dependent settings
102 private boolean mLinkPrefetchAllowed = true;
103
John Reck78a6a1d2011-07-21 13:54:03 -0700104 // Cached values
105 private int mPageCacheCapacity = 1;
106 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800107
John Reck35e9dd62011-04-25 09:01:54 -0700108 // Cached settings
109 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110
Ben Murdochaaa1f372011-07-25 15:40:58 +0100111 private static String sFactoryResetUrl;
112
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700113 private boolean mEngineInitialized = false;
114 private boolean mSyncManagedSettings = false;
115
Bijan Amirzada3bb46302014-06-17 16:31:31 -0700116 public static synchronized void initialize(final Context context) {
117 if (sInstance == null)
118 sInstance = new BrowserSettings(context);
John Reck35e9dd62011-04-25 09:01:54 -0700119 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800120
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700121 public static BrowserSettings getInstance() {
John Reck35e9dd62011-04-25 09:01:54 -0700122 return sInstance;
123 }
Ben Murdochef671652010-11-25 17:17:58 +0000124
John Reck35e9dd62011-04-25 09:01:54 -0700125 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100126 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700127 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
John Reck35e9dd62011-04-25 09:01:54 -0700128 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckcadae722011-07-25 11:36:17 -0700129 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700130 }
131
132 public void setController(Controller controller) {
133 mController = controller;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700134 mNeedsSharedSync = true;
135 }
136
137 public void onEngineInitializationComplete() {
138 mEngineInitialized = true;
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700139
140 // Intialize Web Refiner only once
141 final WebRefiner refiner = WebRefiner.getInstance();
142 if (refiner != null) {
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700143 refiner.setDefaultPermission(PermissionsServiceFactory.getDefaultPermissions(
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700144 PermissionsServiceFactory.PermissionType.WEBREFINER));
145
146 PermissionsServiceFactory.getPermissionsService(
147 new ValueCallback<PermissionsServiceFactory.PermissionsService>() {
148 @Override
149 public void onReceiveValue(
150 PermissionsServiceFactory.PermissionsService value) {
151 Set<String> origins = value.getOrigins();
152 ArrayList<String> allowList = new ArrayList<>();
153 ArrayList<String> blockList = new ArrayList<>();
154 for (String origin : origins) {
155 PermissionsServiceFactory.PermissionsService.OriginInfo
156 info = value.getOriginInfo(origin);
157 int perm = info.getPermission(
158 PermissionsServiceFactory.PermissionType.WEBREFINER);
159 if (perm == PermissionsServiceFactory.Permission.ALLOW) {
160 allowList.add(origin);
161 } else if (perm == PermissionsServiceFactory.Permission.BLOCK) {
162 blockList.add(origin);
163 }
164 }
165 if (!allowList.isEmpty()) {
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700166 refiner.setPermissionForOrigins(
167 allowList.toArray(new String[allowList.size()]), true);
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700168 }
169
170 if (!blockList.isEmpty()) {
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700171 refiner.setPermissionForOrigins(
172 blockList.toArray(new String[blockList.size()]), false);
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700173 }
174 }
175 }
176 );
177 }
178
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700179 mAutofillHandler = new AutofillHandler(mContext);
180 if (mSyncManagedSettings) {
181 syncManagedSettings();
182 }
183 if (mNeedsSharedSync) {
John Reckc477e712011-08-17 11:27:15 -0700184 syncSharedSettings();
185 }
Dave Tharp4a2c8942015-07-10 14:10:51 -0700186
187 // Instantiate all MDM Restriction Singletons.
188 AutoFillRestriction.getInstance();
189 DevToolsRestriction.getInstance();
190 DoNotTrackRestriction.getInstance();
Dave Tharp13f3f7c2015-07-16 14:58:51 -0700191 DownloadDirRestriction.getInstance();
Dave Tharp4a2c8942015-07-10 14:10:51 -0700192 EditBookmarksRestriction.getInstance();
193 IncognitoRestriction.getInstance();
194 ManagedBookmarksRestriction.getInstance();
Panos Thomasa9ff2c72014-12-17 22:15:23 -0800195 ProxyRestriction.getInstance();
Dave Tharp4a2c8942015-07-10 14:10:51 -0700196 SearchEngineRestriction.getInstance();
197 ThirdPartyCookiesRestriction.getInstance();
198 URLFilterRestriction.getInstance();
Ben Murdochef671652010-11-25 17:17:58 +0000199 }
200
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800201 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800202
John Reckc477e712011-08-17 11:27:15 -0700203 if (mNeedsSharedSync) {
204 syncSharedSettings();
205 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800206
John Reck35e9dd62011-04-25 09:01:54 -0700207 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800208 syncStaticSettings(settings);
209 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700210 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800211 }
212 }
213
John Reckd1d87312012-03-08 13:25:00 -0800214 public void stopManagingSettings(WebSettings settings) {
215 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
216 while (iter.hasNext()) {
217 WeakReference<WebSettings> ref = iter.next();
218 if (ref.get() == settings) {
219 iter.remove();
220 return;
221 }
222 }
223 }
224
John Reckcadae722011-07-25 11:36:17 -0700225 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700226
227 @Override
228 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700229 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
230 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700231 // the cost of one cached page is ~3M (measured using nytimes.com). For
232 // low end devices, we only cache one page. For high end devices, we try
233 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800234
235 // SWE_TODO : assume a high-memory device
236 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700237 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800238 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700239 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
240 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
241 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700242 // Workaround b/5254577
243 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700244 if (Build.VERSION.CODENAME.equals("REL")) {
245 // This is a release build, always startup with debug disabled
246 setDebugEnabled(false);
247 }
248 if (mPrefs.contains(PREF_TEXT_SIZE)) {
249 /*
250 * Update from TextSize enum to zoom percent
251 * SMALLEST is 50%
252 * SMALLER is 75%
253 * NORMAL is 100%
254 * LARGER is 150%
255 * LARGEST is 200%
256 */
257 switch (getTextSize()) {
258 case SMALLEST:
259 setTextZoom(50);
260 break;
261 case SMALLER:
262 setTextZoom(75);
263 break;
264 case LARGER:
265 setTextZoom(150);
266 break;
267 case LARGEST:
268 setTextZoom(200);
269 break;
270 }
271 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
272 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100273
qqzhou8c5b0a32013-07-22 15:31:03 +0800274 // add for carrier homepage feature
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700275 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
qqzhou8c5b0a32013-07-22 15:31:03 +0800276
kaiyizbf086ea2013-08-02 11:03:58 +0800277 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
Tarun Nainani95b79682014-12-09 09:38:04 -0800278 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING, "auto").apply();
kaiyizbf086ea2013-08-02 11:03:58 +0800279 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700280
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700281 if (!mPrefs.contains(PREF_EDGE_SWIPE)) {
282 mPrefs.edit().putString(PREF_EDGE_SWIPE,
283 mContext.getResources().getString(
284 R.string.value_unknown_edge_swipe)).apply();
285 }
286
Ben Murdochaaa1f372011-07-25 15:40:58 +0100287 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
288 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
289 BrowserProvider.getClientId(mContext.getContentResolver()));
290 }
291
Ben Murdochaaa1f372011-07-25 15:40:58 +0100292 synchronized (BrowserSettings.class) {
293 sInitialized = true;
294 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700295 }
296 }
297 };
298
Ben Murdochaaa1f372011-07-25 15:40:58 +0100299 private static void requireInitialization() {
300 synchronized (BrowserSettings.class) {
301 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700302 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100303 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700304 } catch (InterruptedException e) {
305 }
306 }
307 }
308 }
309
The Android Open Source Project0c908882009-03-03 19:32:16 -0800310 /**
John Reck35e9dd62011-04-25 09:01:54 -0700311 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800312 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800313 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700314 settings.setGeolocationEnabled(enableGeolocation());
315 settings.setJavaScriptEnabled(enableJavascript());
316 settings.setLightTouchEnabled(enableLightTouch());
317 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700318 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
John Reck35e9dd62011-04-25 09:01:54 -0700319 settings.setMinimumFontSize(getMinimumFontSize());
320 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck7dc444b2011-06-16 17:44:29 -0700321 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700322 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100323 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700324 settings.setLoadsImagesAutomatically(loadImages());
325 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
326 settings.setSavePassword(rememberPasswords());
327 settings.setSaveFormData(saveFormdata());
328 settings.setUseWideViewPort(isWideViewport());
Panos Thomasb10bbda2014-06-23 10:18:36 -0700329 settings.setDoNotTrack(doNotTrack());
Yida Wang078425c2015-06-02 14:14:06 -0400330 settings.setNightModeEnabled(isNightModeEnabled());
Tarun Nainani8eb00912014-07-17 12:28:32 -0700331 settings.setMediaPlaybackRequiresUserGesture(false);
John Reck2fd9d0e2011-07-15 11:13:48 -0700332
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800333 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800334 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
335 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
336 settingsClassic.setForceUserScalable(forceEnableUserScalable());
337 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
338 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800339
John Reckea17e782011-10-27 17:15:59 -0700340 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800341 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700342 useInverted ? "true" : "false");
343 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800344 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700345 Float.toString(getInvertedContrast()));
346 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700347
John Reckea17e782011-10-27 17:15:59 -0700348 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800349 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700350 enableCpuUploadPath() ? "true" : "false");
351 }
Victoria Lease96497832012-03-23 14:19:56 -0700352
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800353 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000354 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800355
Tarun Nainani8eb00912014-07-17 12:28:32 -0700356
John Reck35e9dd62011-04-25 09:01:54 -0700357 /**
358 * Syncs all the settings that have no UI
359 * These cannot change, so we only need to set them once per WebSettings
360 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800361 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700362 settings.setDefaultFontSize(16);
363 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000364
John Reck35e9dd62011-04-25 09:01:54 -0700365 // WebView inside Browser doesn't want initial focus to be set.
366 settings.setNeedInitialFocus(false);
367 // Browser supports multiple windows
368 settings.setSupportMultipleWindows(true);
369 // enable smooth transition for better performance during panning or
370 // zooming
371 settings.setEnableSmoothTransition(true);
372 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700373 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700374
375 // HTML5 API flags
376 settings.setAppCacheEnabled(true);
377 settings.setDatabaseEnabled(true);
378 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700379
380 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700381 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700382 settings.setAppCachePath(getAppCachePath());
383 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
384 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700385 // origin policy for file access
386 settings.setAllowUniversalAccessFromFileURLs(false);
387 settings.setAllowFileAccessFromFileURLs(false);
Sudheer Koganti24766882014-10-02 10:58:09 -0700388 settings.setFullscreenSupported(true);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800389
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800390 //if (!(settings instanceof WebSettingsClassic)) return;
391 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800392
393 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
394 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
395 // WebView should be preserving the memory as much as possible.
396 // However, apps like browser wish to turn on the performance mode which
397 // would require more memory.
398 // TODO: We need to dynamically allocate/deallocate temporary memory for
399 // apps which are trying to use minimal memory. Currently, double
400 // buffering is always turned on, which is unnecessary.
401 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
402 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800403 */
John Reck35e9dd62011-04-25 09:01:54 -0700404 }
405
406 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700407 mNeedsSharedSync = false;
Axesh R. Ajmera579c70c2014-04-17 13:24:56 -0700408 CookieManager.getInstance().setAcceptCookie(acceptCookies());
Vivek Sekhared791da2015-02-22 12:39:05 -0800409
John Reck35e9dd62011-04-25 09:01:54 -0700410 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700411
John Reck35e9dd62011-04-25 09:01:54 -0700412 private void syncManagedSettings() {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700413 if (!mEngineInitialized) {
414 mSyncManagedSettings = true;
415 return;
416 }
417 mSyncManagedSettings = false;
John Reck35e9dd62011-04-25 09:01:54 -0700418 syncSharedSettings();
419 synchronized (mManagedSettings) {
420 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
421 while (iter.hasNext()) {
422 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800423 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700424 if (settings == null) {
425 iter.remove();
426 continue;
427 }
428 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000429 }
John Reck35e9dd62011-04-25 09:01:54 -0700430 }
431 }
Ben Murdochef671652010-11-25 17:17:58 +0000432
John Reck35e9dd62011-04-25 09:01:54 -0700433 @Override
434 public void onSharedPreferenceChanged(
435 SharedPreferences sharedPreferences, String key) {
436 syncManagedSettings();
437 if (PREF_SEARCH_ENGINE.equals(key)) {
438 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700439 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200440 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700441 mController.getUi().setFullscreen(useFullscreen());
442 }
Victoria Lease96497832012-03-23 14:19:56 -0700443 } else if (PREF_LINK_PREFETCH.equals(key)) {
444 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700445 }
John Reck35e9dd62011-04-25 09:01:54 -0700446 }
447
John Reck961d35d2011-06-23 09:45:54 -0700448 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100449 requireInitialization();
450 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700451 }
452
453 public LayoutAlgorithm getLayoutAlgorithm() {
454 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
455 if (autofitPages()) {
Tarun Nainani003bec52014-07-02 02:20:34 -0700456 layoutAlgorithm = LayoutAlgorithm.TEXT_AUTOSIZING;
John Reck35e9dd62011-04-25 09:01:54 -0700457 }
458 if (isDebugEnabled()) {
459 if (isSmallScreen()) {
460 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000461 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700462 if (isNormalLayout()) {
463 layoutAlgorithm = LayoutAlgorithm.NORMAL;
464 } else {
465 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
466 }
Ben Murdochef671652010-11-25 17:17:58 +0000467 }
John Reck35e9dd62011-04-25 09:01:54 -0700468 }
469 return layoutAlgorithm;
470 }
Ben Murdochef671652010-11-25 17:17:58 +0000471
John Reck35e9dd62011-04-25 09:01:54 -0700472 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700473 requireInitialization();
474 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800475 }
476
John Reck35e9dd62011-04-25 09:01:54 -0700477 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700478 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700479 return mWebStorageSizeManager;
480 }
481
John Reck35e9dd62011-04-25 09:01:54 -0700482 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700483 if (mAppCachePath == null) {
484 mAppCachePath = mContext.getDir("appcache", 0).getPath();
485 }
486 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700487 }
488
489 private void updateSearchEngine(boolean force) {
490 String searchEngineName = getSearchEngineName();
491 if (force || mSearchEngine == null ||
492 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700493 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000494 }
495 }
496
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100497 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700498 if (mSearchEngine == null) {
499 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100500 }
John Reck35e9dd62011-04-25 09:01:54 -0700501 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100502 }
503
John Reck35e9dd62011-04-25 09:01:54 -0700504 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700505 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700506 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100507 }
508
John Reck35e9dd62011-04-25 09:01:54 -0700509 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700510 Editor edit = mPrefs.edit();
511 edit.putBoolean(PREF_DEBUG_MENU, value);
512 if (!value) {
513 // Reset to "safe" value
514 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
515 }
516 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100517 }
518
John Reck35e9dd62011-04-25 09:01:54 -0700519 public void clearCache() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700520 if (mController != null) {
521 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800522 if (current != null) {
523 current.clearCache(true);
524 }
525 }
526 }
527
John Reck35e9dd62011-04-25 09:01:54 -0700528 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800529 CookieManager.getInstance().removeAllCookie();
530 }
531
John Reck35e9dd62011-04-25 09:01:54 -0700532 public void clearHistory() {
533 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800534 Browser.clearHistory(resolver);
535 Browser.clearSearches(resolver);
536 }
537
John Reck35e9dd62011-04-25 09:01:54 -0700538 public void clearFormData() {
539 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700540 if (mController!= null) {
541 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100542 if (currentTopView != null) {
543 currentTopView.clearFormData();
544 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800545 }
546 }
547
Bijan Amirzada3bbb3a42014-06-17 12:06:18 -0700548 public WebView getTopWebView(){
549 if (mController!= null)
550 return mController.getCurrentTopWebView();
551
552 return null;
553 }
554
John Reck35e9dd62011-04-25 09:01:54 -0700555 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700556 // Clear password store maintained by SWE engine
557 WebSettings settings = null;
558 // find a valid settings object
559 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
560 while (iter.hasNext()) {
561 WeakReference<WebSettings> ref = iter.next();
562 settings = (WebSettings)ref.get();
563 if (settings != null) {
564 break;
565 }
566 }
567 if (settings != null) {
Panos Thomasdbaea4e2014-05-22 06:48:47 -0700568 settings.clearPasswords();
Panos Thomasfa948b82014-03-09 03:42:42 -0700569 }
Vivek Sekhare8a8ec22014-07-10 14:52:43 -0700570
571 // Clear passwords in WebView database
572 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
573 db.clearHttpAuthUsernamePassword();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800574 }
575
John Reck35e9dd62011-04-25 09:01:54 -0700576 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100577 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100578 }
579
John Reck35e9dd62011-04-25 09:01:54 -0700580 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100581 GeolocationPermissions.getInstance().clearAll();
Panos Thomasb298aad2014-10-22 12:24:21 -0700582 if (GeolocationPermissions.isIncognitoCreated()) {
583 GeolocationPermissions.getIncognitoInstance().clearAll();
584 }
Nicolas Roard78a98e42009-05-11 13:34:17 +0100585 }
586
John Reck35e9dd62011-04-25 09:01:54 -0700587 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700588 mPrefs.edit()
589 .clear()
John Reckbd315192011-07-29 10:05:47 -0700590 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200591 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700592 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700593 }
594
Björn Isakssonc885a242012-06-05 17:19:04 +0200595 private void resetCachedValues() {
596 updateSearchEngine(false);
597 }
598
John Reck35e9dd62011-04-25 09:01:54 -0700599 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800600 // query the profile from components autofill database 524
601 if (mAutofillHandler.mAutoFillProfile == null &&
602 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
603 WebSettings settings = null;
604 // find a valid settings object
605 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
606 while (iter.hasNext()) {
607 WeakReference<WebSettings> ref = iter.next();
608 settings = (WebSettings)ref.get();
609 if (settings != null) {
610 break;
611 }
612 }
613 if (settings != null) {
614 AutoFillProfile profile =
615 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
616 mAutofillHandler.setAutoFillProfile(profile);
617 }
618 }
John Reck35e9dd62011-04-25 09:01:54 -0700619 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800620 }
621
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800622 public String getAutoFillProfileId() {
623 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800624 }
625
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800626 public void updateAutoFillProfile(AutoFillProfile profile) {
627 syncAutoFillProfile(profile);
628 }
629
630 private void syncAutoFillProfile(AutoFillProfile profile) {
631 synchronized (mManagedSettings) {
632 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
633 while (iter.hasNext()) {
634 WeakReference<WebSettings> ref = iter.next();
635 WebSettings settings = (WebSettings)ref.get();
636 if (settings == null) {
637 iter.remove();
638 continue;
639 }
640 // update the profile only once.
641 settings.setAutoFillProfile(profile);
642 // Now we should have the guid
643 mAutofillHandler.setAutoFillProfile(profile);
644 break;
645 }
646 }
647 }
John Reck35e9dd62011-04-25 09:01:54 -0700648 public void toggleDebugSettings() {
649 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800650 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100651
John Reckb8b2af82011-05-20 15:58:33 -0700652 public boolean hasDesktopUseragent(WebView view) {
Vivek Sekhar7b3d2da2014-11-13 16:20:11 -0800653 return view != null && view.getUseDesktopUserAgent();
John Reckb8b2af82011-05-20 15:58:33 -0700654 }
655
656 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700657 if (view == null) {
658 return;
659 }
Vivek Sekhar7b3d2da2014-11-13 16:20:11 -0800660 if (hasDesktopUseragent(view))
661 view.setUseDesktopUserAgent(false, true);
662 else
663 view.setUseDesktopUserAgent(true, true);
John Reckb8b2af82011-05-20 15:58:33 -0700664 }
665
John Reck7dc444b2011-06-16 17:44:29 -0700666 public static int getAdjustedMinimumFontSize(int rawValue) {
667 rawValue++; // Preference starts at 0, min font at 1
668 if (rawValue > 1) {
669 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
670 }
671 return rawValue;
672 }
673
John Reck5ba3c762011-09-14 15:00:15 -0700674 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700675 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700676 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700677 }
678
679 static int getRawTextZoom(int percent) {
680 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
681 }
682
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700683 public int getAdjustedDoubleTapZoom(int rawValue) {
684 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
685 return (int) ((rawValue + 100) * mFontSizeMult);
686 }
687
688 static int getRawDoubleTapZoom(int percent) {
689 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
690 }
691
John Reckcadae722011-07-25 11:36:17 -0700692 public SharedPreferences getPreferences() {
693 return mPrefs;
694 }
695
Victoria Lease96497832012-03-23 14:19:56 -0700696 // update connectivity-dependent options
697 public void updateConnectionType() {
698 ConnectivityManager cm = (ConnectivityManager)
699 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
700 String linkPrefetchPreference = getLinkPrefetchEnabled();
701 boolean linkPrefetchAllowed = linkPrefetchPreference.
702 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
703 NetworkInfo ni = cm.getActiveNetworkInfo();
704 if (ni != null) {
705 switch (ni.getType()) {
706 case ConnectivityManager.TYPE_WIFI:
707 case ConnectivityManager.TYPE_ETHERNET:
708 case ConnectivityManager.TYPE_BLUETOOTH:
709 linkPrefetchAllowed |= linkPrefetchPreference.
710 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
711 break;
712 case ConnectivityManager.TYPE_MOBILE:
713 case ConnectivityManager.TYPE_MOBILE_DUN:
714 case ConnectivityManager.TYPE_MOBILE_MMS:
715 case ConnectivityManager.TYPE_MOBILE_SUPL:
716 case ConnectivityManager.TYPE_WIMAX:
717 default:
718 break;
719 }
720 }
721 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
722 mLinkPrefetchAllowed = linkPrefetchAllowed;
723 syncManagedSettings();
724 }
725 }
726
luxiaol62677b02013-07-22 07:54:49 +0800727 public String getDownloadPath() {
728 return mPrefs.getString(PREF_DOWNLOAD_PATH,
729 DownloadHandler.getDefaultDownloadPath(mContext));
730 }
John Reck35e9dd62011-04-25 09:01:54 -0700731 // -----------------------------
732 // getter/setters for accessibility_preferences.xml
733 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100734
John Reck7dc444b2011-06-16 17:44:29 -0700735 @Deprecated
736 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700737 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
738 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100739 }
740
John Reck35e9dd62011-04-25 09:01:54 -0700741 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700742 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700743 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100744 }
745
John Reck92f25f82011-04-26 16:57:10 -0700746 public boolean forceEnableUserScalable() {
747 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
748 }
749
John Reck7dc444b2011-06-16 17:44:29 -0700750 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700751 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700752 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
753 return getAdjustedTextZoom(textZoom);
754 }
755
756 public void setTextZoom(int percent) {
757 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
758 }
759
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700760 public int getDoubleTapZoom() {
761 requireInitialization();
762 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
763 return getAdjustedDoubleTapZoom(doubleTapZoom);
764 }
765
766 public void setDoubleTapZoom(int percent) {
767 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
768 }
769
John Reck35e9dd62011-04-25 09:01:54 -0700770 // -----------------------------
771 // getter/setters for advanced_preferences.xml
772 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100773
John Reck35e9dd62011-04-25 09:01:54 -0700774 public String getSearchEngineName() {
Panos Thomas64c77e02014-12-17 22:15:23 -0800775 // The following is a NOP if the SEARCH_ENGINE restriction has already been created. Otherwise,
776 // it creates the restriction and if enabled it sets the <default_search_engine_value>.
777 SearchEngineRestriction.getInstance();
778
luxiaol221b3932013-07-19 15:27:57 +0800779 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
780 if (defaultSearchEngineValue == null) {
781 defaultSearchEngineValue = SearchEngine.GOOGLE;
782 }
783 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100784 }
John Reck63bb6872010-12-01 19:29:32 -0800785
Michael Kolb8d772b02012-01-19 13:56:00 -0800786 public boolean allowAppTabs() {
787 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
788 }
789
John Reck35e9dd62011-04-25 09:01:54 -0700790 public boolean openInBackground() {
791 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800792 }
John Reck35e9dd62011-04-25 09:01:54 -0700793
794 public boolean enableJavascript() {
795 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
796 }
797
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800798 public boolean enableMemoryMonitor() {
799 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
800 }
801
Tarun Nainani8eb00912014-07-17 12:28:32 -0700802
John Reck35e9dd62011-04-25 09:01:54 -0700803 public boolean loadPageInOverviewMode() {
804 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
805 }
806
807 public boolean autofitPages() {
808 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
809 }
810
811 public boolean blockPopupWindows() {
Pankaj Garg11015212015-07-06 14:38:43 -0700812 return !PermissionsServiceFactory.getDefaultPermissions(
813 PermissionsServiceFactory.PermissionType.POPUP);
John Reck35e9dd62011-04-25 09:01:54 -0700814 }
815
816 public boolean loadImages() {
817 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
818 }
819
820 public String getDefaultTextEncoding() {
Tarun Nainani88381422015-04-02 19:47:21 -0700821 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, "auto");
John Reck35e9dd62011-04-25 09:01:54 -0700822 }
823
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700824 public String getEdgeSwipeAction() {
825 return mPrefs.getString(PREF_EDGE_SWIPE,
826 mContext.getResources().getString(R.string.value_unknown_edge_swipe));
827 }
828
829 public void setEdgeSwipeTemporal() {
830 mPrefs.edit().putString(PREF_EDGE_SWIPE,
831 mContext.getResources().getString(R.string.value_temporal_edge_swipe)).apply();
832 }
833
834 public void setEdgeSwipeSpatial() {
835 mPrefs.edit().putString(PREF_EDGE_SWIPE,
836 mContext.getResources().getString(R.string.value_spatial_edge_swipe)).apply();
837 }
838
839 public void setEdgeSwipeDisabled() {
840 mPrefs.edit().putString(PREF_EDGE_SWIPE,
841 mContext.getResources().getString(R.string.value_disable_edge_swipe)).apply();
842 }
843
John Reck35e9dd62011-04-25 09:01:54 -0700844 // -----------------------------
845 // getter/setters for general_preferences.xml
846 // -----------------------------
847
848 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700849 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
850 }
851
852 public void setHomePage(String value) {
853 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
854 }
855
856 public boolean isAutofillEnabled() {
857 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
858 }
859
860 public void setAutofillEnabled(boolean value) {
861 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
862 }
863
Site Maoabb7bd32015-03-20 15:34:02 -0700864 public boolean isPowerSaveModeEnabled() {
865 return mPrefs.getBoolean(PREF_POWERSAVE_ENABLED, false);
866 }
867
868 public void setPowerSaveModeEnabled(boolean value) {
869 mPrefs.edit().putBoolean(PREF_POWERSAVE_ENABLED, value).apply();
870 }
871
Yida Wang078425c2015-06-02 14:14:06 -0400872 public boolean isNightModeEnabled() {
873 return mPrefs.getBoolean(PREF_NIGHTMODE_ENABLED, false);
874 }
875
876 public void setNightModeEnabled(boolean value) {
877 mPrefs.edit().putBoolean(PREF_NIGHTMODE_ENABLED, value).apply();
878 }
879
John Reck35e9dd62011-04-25 09:01:54 -0700880 // -----------------------------
881 // getter/setters for debug_preferences.xml
882 // -----------------------------
883
884 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700885 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700886 return true;
887 }
888 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
889 }
890
Derek Sollenberger31adf672011-07-08 11:31:30 -0400891 public boolean isSkiaHardwareAccelerated() {
892 if (!isDebugEnabled()) {
893 return false;
894 }
895 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
896 }
897
John Reck35e9dd62011-04-25 09:01:54 -0700898 // -----------------------------
899 // getter/setters for hidden_debug_preferences.xml
900 // -----------------------------
901
902 public boolean enableVisualIndicator() {
903 if (!isDebugEnabled()) {
904 return false;
905 }
906 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
907 }
908
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700909 public boolean enableCpuUploadPath() {
910 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700911 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700912 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700913 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700914 }
915
John Reck35e9dd62011-04-25 09:01:54 -0700916 public boolean isSmallScreen() {
917 if (!isDebugEnabled()) {
918 return false;
919 }
920 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
921 }
922
923 public boolean isWideViewport() {
924 if (!isDebugEnabled()) {
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800925 return true;
John Reck35e9dd62011-04-25 09:01:54 -0700926 }
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800927 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
John Reck35e9dd62011-04-25 09:01:54 -0700928 }
929
930 public boolean isNormalLayout() {
931 if (!isDebugEnabled()) {
932 return false;
933 }
934 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
935 }
936
937 public boolean isTracing() {
938 if (!isDebugEnabled()) {
939 return false;
940 }
941 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
942 }
943
944 public boolean enableLightTouch() {
945 if (!isDebugEnabled()) {
946 return false;
947 }
948 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
949 }
950
951 public boolean enableNavDump() {
952 if (!isDebugEnabled()) {
953 return false;
954 }
955 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
956 }
957
958 public String getJsEngineFlags() {
959 if (!isDebugEnabled()) {
960 return "";
961 }
962 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
963 }
964
965 // -----------------------------
966 // getter/setters for lab_preferences.xml
967 // -----------------------------
968
John Reck35e9dd62011-04-25 09:01:54 -0700969 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700970 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700971 }
972
Michael Kolbc38c6042011-04-27 10:46:06 -0700973 public boolean useFullscreen() {
974 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
975 }
976
John Reck2fd9d0e2011-07-15 11:13:48 -0700977 public boolean useInvertedRendering() {
978 return mPrefs.getBoolean(PREF_INVERTED, false);
979 }
980
Nicolas Roard5d513102011-08-03 15:35:34 -0700981 public float getInvertedContrast() {
982 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
983 }
984
John Reck35e9dd62011-04-25 09:01:54 -0700985 // -----------------------------
986 // getter/setters for privacy_security_preferences.xml
987 // -----------------------------
988
989 public boolean showSecurityWarnings() {
990 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
991 }
992
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700993 public boolean doNotTrack() {
Dave Tharpb487d5d2015-05-15 15:54:08 -0700994 boolean dntVal;
995 if (DoNotTrackRestriction.getInstance().isEnabled()) {
996 dntVal = DoNotTrackRestriction.getInstance().getValue();
997 }
998 else {
999 dntVal = mPrefs.getBoolean(PREF_DO_NOT_TRACK, true);
1000 }
1001 return dntVal;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001002 }
1003
John Reck35e9dd62011-04-25 09:01:54 -07001004 public boolean acceptCookies() {
Pankaj Garg32e1b942015-06-03 18:13:24 -07001005 return PermissionsServiceFactory.getDefaultPermissions(
1006 PermissionsServiceFactory.PermissionType.COOKIE);
John Reck35e9dd62011-04-25 09:01:54 -07001007 }
1008
1009 public boolean saveFormdata() {
1010 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
1011 }
1012
1013 public boolean enableGeolocation() {
1014 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
1015 }
1016
1017 public boolean rememberPasswords() {
1018 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
1019 }
1020
Michael Kolb14612442011-06-24 13:06:29 -07001021 // -----------------------------
1022 // getter/setters for bandwidth_preferences.xml
1023 // -----------------------------
1024
Mathew Inwood467813f2011-09-02 15:43:17 +01001025 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
1026 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -07001027 }
Mathew Inwood467813f2011-09-02 15:43:17 +01001028
1029 public static String getPreloadAlwaysPreferenceString(Context context) {
1030 return context.getResources().getString(R.string.pref_data_preload_value_always);
1031 }
1032
Mathew Inwood825fba72011-10-04 14:52:52 +01001033 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1034 "browser_default_preload_setting";
1035
1036 public String getDefaultPreloadSetting() {
1037 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1038 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1039 if (preload == null) {
1040 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1041 }
1042 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001043 }
1044
1045 public String getPreloadEnabled() {
1046 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1047 }
1048
Victoria Lease96497832012-03-23 14:19:56 -07001049 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1050 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1051 }
1052
1053 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1054 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1055 }
1056
1057 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1058 "browser_default_link_prefetch_setting";
1059
1060 public String getDefaultLinkPrefetchSetting() {
1061 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1062 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1063 if (preload == null) {
1064 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1065 }
1066 return preload;
1067 }
1068
1069 public String getLinkPrefetchEnabled() {
1070 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1071 }
1072
George Mount3636d0a2011-11-21 09:08:21 -08001073 // -----------------------------
1074 // getter/setters for browser recovery
1075 // -----------------------------
1076 /**
1077 * The last time browser was started.
1078 * @return The last browser start time as System.currentTimeMillis. This
1079 * can be 0 if this is the first time or the last tab was closed.
1080 */
1081 public long getLastRecovered() {
1082 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1083 }
1084
1085 /**
1086 * Sets the last browser start time.
1087 * @param time The last time as System.currentTimeMillis that the browser
1088 * was started. This should be set to 0 if the last tab is closed.
1089 */
1090 public void setLastRecovered(long time) {
1091 mPrefs.edit()
1092 .putLong(KEY_LAST_RECOVERED, time)
1093 .apply();
1094 }
1095
1096 /**
1097 * Used to determine whether or not the previous browser run crashed. Once
1098 * the previous state has been determined, the value will be set to false
1099 * until a pause is received.
1100 * @return true if the last browser run was paused or false if it crashed.
1101 */
1102 public boolean wasLastRunPaused() {
1103 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1104 }
1105
1106 /**
1107 * Sets whether or not the last run was a pause or crash.
1108 * @param isPaused Set to true When a pause is received or false after
1109 * resuming.
1110 */
1111 public void setLastRunPaused(boolean isPaused) {
1112 mPrefs.edit()
1113 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1114 .apply();
1115 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001116}