blob: 9de584d562cbc2c6e965ad28adbcf42716912c1f [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) {
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700143 mPrefs.edit().putBoolean(PREF_WEB_REFINER, true).apply();
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700144 refiner.setDefaultPermission(PermissionsServiceFactory.getDefaultPermissions(
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700145 PermissionsServiceFactory.PermissionType.WEBREFINER));
146
147 PermissionsServiceFactory.getPermissionsService(
148 new ValueCallback<PermissionsServiceFactory.PermissionsService>() {
149 @Override
150 public void onReceiveValue(
151 PermissionsServiceFactory.PermissionsService value) {
152 Set<String> origins = value.getOrigins();
153 ArrayList<String> allowList = new ArrayList<>();
154 ArrayList<String> blockList = new ArrayList<>();
155 for (String origin : origins) {
156 PermissionsServiceFactory.PermissionsService.OriginInfo
157 info = value.getOriginInfo(origin);
Axesh R. Ajmera1ad7c572015-07-13 17:10:56 -0700158 if (info == null) {
159 continue;
160 }
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700161 int perm = info.getPermission(
162 PermissionsServiceFactory.PermissionType.WEBREFINER);
163 if (perm == PermissionsServiceFactory.Permission.ALLOW) {
164 allowList.add(origin);
165 } else if (perm == PermissionsServiceFactory.Permission.BLOCK) {
166 blockList.add(origin);
167 }
168 }
169 if (!allowList.isEmpty()) {
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700170 refiner.setPermissionForOrigins(
171 allowList.toArray(new String[allowList.size()]), true);
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700172 }
173
174 if (!blockList.isEmpty()) {
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700175 refiner.setPermissionForOrigins(
176 blockList.toArray(new String[blockList.size()]), false);
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700177 }
178 }
179 }
180 );
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700181 } else {
182 mPrefs.edit().putBoolean(PREF_WEB_REFINER, false).apply();
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700183 }
184
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700185 mAutofillHandler = new AutofillHandler(mContext);
186 if (mSyncManagedSettings) {
187 syncManagedSettings();
188 }
189 if (mNeedsSharedSync) {
John Reckc477e712011-08-17 11:27:15 -0700190 syncSharedSettings();
191 }
Dave Tharp4a2c8942015-07-10 14:10:51 -0700192
193 // Instantiate all MDM Restriction Singletons.
194 AutoFillRestriction.getInstance();
195 DevToolsRestriction.getInstance();
196 DoNotTrackRestriction.getInstance();
Dave Tharp13f3f7c2015-07-16 14:58:51 -0700197 DownloadDirRestriction.getInstance();
Dave Tharp4a2c8942015-07-10 14:10:51 -0700198 EditBookmarksRestriction.getInstance();
199 IncognitoRestriction.getInstance();
200 ManagedBookmarksRestriction.getInstance();
Panos Thomasa9ff2c72014-12-17 22:15:23 -0800201 ProxyRestriction.getInstance();
Dave Tharp4a2c8942015-07-10 14:10:51 -0700202 SearchEngineRestriction.getInstance();
203 ThirdPartyCookiesRestriction.getInstance();
204 URLFilterRestriction.getInstance();
Ben Murdochef671652010-11-25 17:17:58 +0000205 }
206
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800207 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800208
John Reckc477e712011-08-17 11:27:15 -0700209 if (mNeedsSharedSync) {
210 syncSharedSettings();
211 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800212
John Reck35e9dd62011-04-25 09:01:54 -0700213 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800214 syncStaticSettings(settings);
215 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700216 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800217 }
218 }
219
John Reckd1d87312012-03-08 13:25:00 -0800220 public void stopManagingSettings(WebSettings settings) {
221 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
222 while (iter.hasNext()) {
223 WeakReference<WebSettings> ref = iter.next();
224 if (ref.get() == settings) {
225 iter.remove();
226 return;
227 }
228 }
229 }
230
John Reckcadae722011-07-25 11:36:17 -0700231 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700232
233 @Override
234 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700235 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
236 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700237 // the cost of one cached page is ~3M (measured using nytimes.com). For
238 // low end devices, we only cache one page. For high end devices, we try
239 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800240
241 // SWE_TODO : assume a high-memory device
242 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700243 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800244 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700245 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
246 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
247 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700248 // Workaround b/5254577
249 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700250 if (Build.VERSION.CODENAME.equals("REL")) {
251 // This is a release build, always startup with debug disabled
252 setDebugEnabled(false);
253 }
254 if (mPrefs.contains(PREF_TEXT_SIZE)) {
255 /*
256 * Update from TextSize enum to zoom percent
257 * SMALLEST is 50%
258 * SMALLER is 75%
259 * NORMAL is 100%
260 * LARGER is 150%
261 * LARGEST is 200%
262 */
263 switch (getTextSize()) {
264 case SMALLEST:
265 setTextZoom(50);
266 break;
267 case SMALLER:
268 setTextZoom(75);
269 break;
270 case LARGER:
271 setTextZoom(150);
272 break;
273 case LARGEST:
274 setTextZoom(200);
275 break;
276 }
277 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
278 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100279
qqzhou8c5b0a32013-07-22 15:31:03 +0800280 // add for carrier homepage feature
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700281 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
qqzhou8c5b0a32013-07-22 15:31:03 +0800282
kaiyizbf086ea2013-08-02 11:03:58 +0800283 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
Tarun Nainani95b79682014-12-09 09:38:04 -0800284 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING, "auto").apply();
kaiyizbf086ea2013-08-02 11:03:58 +0800285 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700286
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700287 if (!mPrefs.contains(PREF_EDGE_SWIPE)) {
288 mPrefs.edit().putString(PREF_EDGE_SWIPE,
289 mContext.getResources().getString(
290 R.string.value_unknown_edge_swipe)).apply();
291 }
292
Ben Murdochaaa1f372011-07-25 15:40:58 +0100293 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
294 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
295 BrowserProvider.getClientId(mContext.getContentResolver()));
296 }
297
Ben Murdochaaa1f372011-07-25 15:40:58 +0100298 synchronized (BrowserSettings.class) {
299 sInitialized = true;
300 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700301 }
302 }
303 };
304
Ben Murdochaaa1f372011-07-25 15:40:58 +0100305 private static void requireInitialization() {
306 synchronized (BrowserSettings.class) {
307 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700308 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100309 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700310 } catch (InterruptedException e) {
311 }
312 }
313 }
314 }
315
The Android Open Source Project0c908882009-03-03 19:32:16 -0800316 /**
John Reck35e9dd62011-04-25 09:01:54 -0700317 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800318 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800319 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700320 settings.setGeolocationEnabled(enableGeolocation());
321 settings.setJavaScriptEnabled(enableJavascript());
322 settings.setLightTouchEnabled(enableLightTouch());
323 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700324 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
John Reck35e9dd62011-04-25 09:01:54 -0700325 settings.setMinimumFontSize(getMinimumFontSize());
326 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck7dc444b2011-06-16 17:44:29 -0700327 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700328 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100329 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700330 settings.setLoadsImagesAutomatically(loadImages());
331 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
332 settings.setSavePassword(rememberPasswords());
333 settings.setSaveFormData(saveFormdata());
334 settings.setUseWideViewPort(isWideViewport());
Panos Thomasb10bbda2014-06-23 10:18:36 -0700335 settings.setDoNotTrack(doNotTrack());
Yida Wang078425c2015-06-02 14:14:06 -0400336 settings.setNightModeEnabled(isNightModeEnabled());
Tarun Nainani8eb00912014-07-17 12:28:32 -0700337 settings.setMediaPlaybackRequiresUserGesture(false);
John Reck2fd9d0e2011-07-15 11:13:48 -0700338
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800339 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800340 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
341 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
342 settingsClassic.setForceUserScalable(forceEnableUserScalable());
343 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
344 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800345
John Reckea17e782011-10-27 17:15:59 -0700346 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800347 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700348 useInverted ? "true" : "false");
349 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800350 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700351 Float.toString(getInvertedContrast()));
352 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700353
John Reckea17e782011-10-27 17:15:59 -0700354 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800355 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700356 enableCpuUploadPath() ? "true" : "false");
357 }
Victoria Lease96497832012-03-23 14:19:56 -0700358
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800359 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000360 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800361
Tarun Nainani8eb00912014-07-17 12:28:32 -0700362
John Reck35e9dd62011-04-25 09:01:54 -0700363 /**
364 * Syncs all the settings that have no UI
365 * These cannot change, so we only need to set them once per WebSettings
366 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800367 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700368 settings.setDefaultFontSize(16);
369 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000370
John Reck35e9dd62011-04-25 09:01:54 -0700371 // WebView inside Browser doesn't want initial focus to be set.
372 settings.setNeedInitialFocus(false);
373 // Browser supports multiple windows
374 settings.setSupportMultipleWindows(true);
375 // enable smooth transition for better performance during panning or
376 // zooming
377 settings.setEnableSmoothTransition(true);
378 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700379 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700380
381 // HTML5 API flags
382 settings.setAppCacheEnabled(true);
383 settings.setDatabaseEnabled(true);
384 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700385
386 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700387 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700388 settings.setAppCachePath(getAppCachePath());
389 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
390 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700391 // origin policy for file access
392 settings.setAllowUniversalAccessFromFileURLs(false);
393 settings.setAllowFileAccessFromFileURLs(false);
Sudheer Koganti24766882014-10-02 10:58:09 -0700394 settings.setFullscreenSupported(true);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800395
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800396 //if (!(settings instanceof WebSettingsClassic)) return;
397 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800398
399 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
400 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
401 // WebView should be preserving the memory as much as possible.
402 // However, apps like browser wish to turn on the performance mode which
403 // would require more memory.
404 // TODO: We need to dynamically allocate/deallocate temporary memory for
405 // apps which are trying to use minimal memory. Currently, double
406 // buffering is always turned on, which is unnecessary.
407 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
408 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800409 */
John Reck35e9dd62011-04-25 09:01:54 -0700410 }
411
412 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700413 mNeedsSharedSync = false;
Axesh R. Ajmera579c70c2014-04-17 13:24:56 -0700414 CookieManager.getInstance().setAcceptCookie(acceptCookies());
Vivek Sekhared791da2015-02-22 12:39:05 -0800415
John Reck35e9dd62011-04-25 09:01:54 -0700416 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700417
John Reck35e9dd62011-04-25 09:01:54 -0700418 private void syncManagedSettings() {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700419 if (!mEngineInitialized) {
420 mSyncManagedSettings = true;
421 return;
422 }
423 mSyncManagedSettings = false;
John Reck35e9dd62011-04-25 09:01:54 -0700424 syncSharedSettings();
425 synchronized (mManagedSettings) {
426 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
427 while (iter.hasNext()) {
428 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800429 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700430 if (settings == null) {
431 iter.remove();
432 continue;
433 }
434 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000435 }
John Reck35e9dd62011-04-25 09:01:54 -0700436 }
437 }
Ben Murdochef671652010-11-25 17:17:58 +0000438
John Reck35e9dd62011-04-25 09:01:54 -0700439 @Override
440 public void onSharedPreferenceChanged(
441 SharedPreferences sharedPreferences, String key) {
442 syncManagedSettings();
443 if (PREF_SEARCH_ENGINE.equals(key)) {
444 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700445 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200446 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700447 mController.getUi().setFullscreen(useFullscreen());
448 }
Victoria Lease96497832012-03-23 14:19:56 -0700449 } else if (PREF_LINK_PREFETCH.equals(key)) {
450 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700451 }
John Reck35e9dd62011-04-25 09:01:54 -0700452 }
453
John Reck961d35d2011-06-23 09:45:54 -0700454 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100455 requireInitialization();
456 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700457 }
458
459 public LayoutAlgorithm getLayoutAlgorithm() {
460 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
461 if (autofitPages()) {
Tarun Nainani003bec52014-07-02 02:20:34 -0700462 layoutAlgorithm = LayoutAlgorithm.TEXT_AUTOSIZING;
John Reck35e9dd62011-04-25 09:01:54 -0700463 }
464 if (isDebugEnabled()) {
465 if (isSmallScreen()) {
466 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000467 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700468 if (isNormalLayout()) {
469 layoutAlgorithm = LayoutAlgorithm.NORMAL;
470 } else {
471 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
472 }
Ben Murdochef671652010-11-25 17:17:58 +0000473 }
John Reck35e9dd62011-04-25 09:01:54 -0700474 }
475 return layoutAlgorithm;
476 }
Ben Murdochef671652010-11-25 17:17:58 +0000477
John Reck35e9dd62011-04-25 09:01:54 -0700478 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700479 requireInitialization();
480 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800481 }
482
John Reck35e9dd62011-04-25 09:01:54 -0700483 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700484 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700485 return mWebStorageSizeManager;
486 }
487
John Reck35e9dd62011-04-25 09:01:54 -0700488 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700489 if (mAppCachePath == null) {
490 mAppCachePath = mContext.getDir("appcache", 0).getPath();
491 }
492 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700493 }
494
495 private void updateSearchEngine(boolean force) {
496 String searchEngineName = getSearchEngineName();
497 if (force || mSearchEngine == null ||
498 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700499 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000500 }
501 }
502
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100503 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700504 if (mSearchEngine == null) {
505 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100506 }
John Reck35e9dd62011-04-25 09:01:54 -0700507 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100508 }
509
John Reck35e9dd62011-04-25 09:01:54 -0700510 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700511 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700512 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100513 }
514
John Reck35e9dd62011-04-25 09:01:54 -0700515 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700516 Editor edit = mPrefs.edit();
517 edit.putBoolean(PREF_DEBUG_MENU, value);
518 if (!value) {
519 // Reset to "safe" value
520 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
521 }
522 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100523 }
524
John Reck35e9dd62011-04-25 09:01:54 -0700525 public void clearCache() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700526 if (mController != null) {
527 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800528 if (current != null) {
529 current.clearCache(true);
530 }
531 }
532 }
533
John Reck35e9dd62011-04-25 09:01:54 -0700534 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800535 CookieManager.getInstance().removeAllCookie();
536 }
537
John Reck35e9dd62011-04-25 09:01:54 -0700538 public void clearHistory() {
539 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800540 Browser.clearHistory(resolver);
541 Browser.clearSearches(resolver);
542 }
543
John Reck35e9dd62011-04-25 09:01:54 -0700544 public void clearFormData() {
545 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 if (mController!= null) {
547 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100548 if (currentTopView != null) {
549 currentTopView.clearFormData();
550 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800551 }
552 }
553
Bijan Amirzada3bbb3a42014-06-17 12:06:18 -0700554 public WebView getTopWebView(){
555 if (mController!= null)
556 return mController.getCurrentTopWebView();
557
558 return null;
559 }
560
John Reck35e9dd62011-04-25 09:01:54 -0700561 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700562 // Clear password store maintained by SWE engine
563 WebSettings settings = null;
564 // find a valid settings object
565 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
566 while (iter.hasNext()) {
567 WeakReference<WebSettings> ref = iter.next();
568 settings = (WebSettings)ref.get();
569 if (settings != null) {
570 break;
571 }
572 }
573 if (settings != null) {
Panos Thomasdbaea4e2014-05-22 06:48:47 -0700574 settings.clearPasswords();
Panos Thomasfa948b82014-03-09 03:42:42 -0700575 }
Vivek Sekhare8a8ec22014-07-10 14:52:43 -0700576
577 // Clear passwords in WebView database
578 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
579 db.clearHttpAuthUsernamePassword();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800580 }
581
John Reck35e9dd62011-04-25 09:01:54 -0700582 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100583 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100584 }
585
John Reck35e9dd62011-04-25 09:01:54 -0700586 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100587 GeolocationPermissions.getInstance().clearAll();
Panos Thomasb298aad2014-10-22 12:24:21 -0700588 if (GeolocationPermissions.isIncognitoCreated()) {
589 GeolocationPermissions.getIncognitoInstance().clearAll();
590 }
Nicolas Roard78a98e42009-05-11 13:34:17 +0100591 }
592
John Reck35e9dd62011-04-25 09:01:54 -0700593 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700594 mPrefs.edit()
595 .clear()
John Reckbd315192011-07-29 10:05:47 -0700596 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200597 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700598 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700599 }
600
Björn Isakssonc885a242012-06-05 17:19:04 +0200601 private void resetCachedValues() {
602 updateSearchEngine(false);
603 }
604
John Reck35e9dd62011-04-25 09:01:54 -0700605 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800606 // query the profile from components autofill database 524
607 if (mAutofillHandler.mAutoFillProfile == null &&
608 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
609 WebSettings settings = null;
610 // find a valid settings object
611 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
612 while (iter.hasNext()) {
613 WeakReference<WebSettings> ref = iter.next();
614 settings = (WebSettings)ref.get();
615 if (settings != null) {
616 break;
617 }
618 }
619 if (settings != null) {
620 AutoFillProfile profile =
621 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
622 mAutofillHandler.setAutoFillProfile(profile);
623 }
624 }
John Reck35e9dd62011-04-25 09:01:54 -0700625 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800626 }
627
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800628 public String getAutoFillProfileId() {
629 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800630 }
631
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800632 public void updateAutoFillProfile(AutoFillProfile profile) {
633 syncAutoFillProfile(profile);
634 }
635
636 private void syncAutoFillProfile(AutoFillProfile profile) {
637 synchronized (mManagedSettings) {
638 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
639 while (iter.hasNext()) {
640 WeakReference<WebSettings> ref = iter.next();
641 WebSettings settings = (WebSettings)ref.get();
642 if (settings == null) {
643 iter.remove();
644 continue;
645 }
646 // update the profile only once.
647 settings.setAutoFillProfile(profile);
648 // Now we should have the guid
649 mAutofillHandler.setAutoFillProfile(profile);
650 break;
651 }
652 }
653 }
John Reck35e9dd62011-04-25 09:01:54 -0700654 public void toggleDebugSettings() {
655 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800656 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100657
John Reckb8b2af82011-05-20 15:58:33 -0700658 public boolean hasDesktopUseragent(WebView view) {
Vivek Sekhar7b3d2da2014-11-13 16:20:11 -0800659 return view != null && view.getUseDesktopUserAgent();
John Reckb8b2af82011-05-20 15:58:33 -0700660 }
661
662 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700663 if (view == null) {
664 return;
665 }
Vivek Sekhar7b3d2da2014-11-13 16:20:11 -0800666 if (hasDesktopUseragent(view))
667 view.setUseDesktopUserAgent(false, true);
668 else
669 view.setUseDesktopUserAgent(true, true);
John Reckb8b2af82011-05-20 15:58:33 -0700670 }
671
John Reck7dc444b2011-06-16 17:44:29 -0700672 public static int getAdjustedMinimumFontSize(int rawValue) {
673 rawValue++; // Preference starts at 0, min font at 1
674 if (rawValue > 1) {
675 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
676 }
677 return rawValue;
678 }
679
John Reck5ba3c762011-09-14 15:00:15 -0700680 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700681 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700682 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700683 }
684
685 static int getRawTextZoom(int percent) {
686 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
687 }
688
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700689 public int getAdjustedDoubleTapZoom(int rawValue) {
690 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
691 return (int) ((rawValue + 100) * mFontSizeMult);
692 }
693
694 static int getRawDoubleTapZoom(int percent) {
695 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
696 }
697
John Reckcadae722011-07-25 11:36:17 -0700698 public SharedPreferences getPreferences() {
699 return mPrefs;
700 }
701
Victoria Lease96497832012-03-23 14:19:56 -0700702 // update connectivity-dependent options
703 public void updateConnectionType() {
704 ConnectivityManager cm = (ConnectivityManager)
705 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
706 String linkPrefetchPreference = getLinkPrefetchEnabled();
707 boolean linkPrefetchAllowed = linkPrefetchPreference.
708 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
709 NetworkInfo ni = cm.getActiveNetworkInfo();
710 if (ni != null) {
711 switch (ni.getType()) {
712 case ConnectivityManager.TYPE_WIFI:
713 case ConnectivityManager.TYPE_ETHERNET:
714 case ConnectivityManager.TYPE_BLUETOOTH:
715 linkPrefetchAllowed |= linkPrefetchPreference.
716 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
717 break;
718 case ConnectivityManager.TYPE_MOBILE:
719 case ConnectivityManager.TYPE_MOBILE_DUN:
720 case ConnectivityManager.TYPE_MOBILE_MMS:
721 case ConnectivityManager.TYPE_MOBILE_SUPL:
722 case ConnectivityManager.TYPE_WIMAX:
723 default:
724 break;
725 }
726 }
727 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
728 mLinkPrefetchAllowed = linkPrefetchAllowed;
729 syncManagedSettings();
730 }
731 }
732
luxiaol62677b02013-07-22 07:54:49 +0800733 public String getDownloadPath() {
734 return mPrefs.getString(PREF_DOWNLOAD_PATH,
735 DownloadHandler.getDefaultDownloadPath(mContext));
736 }
John Reck35e9dd62011-04-25 09:01:54 -0700737 // -----------------------------
738 // getter/setters for accessibility_preferences.xml
739 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100740
John Reck7dc444b2011-06-16 17:44:29 -0700741 @Deprecated
742 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700743 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
744 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100745 }
746
John Reck35e9dd62011-04-25 09:01:54 -0700747 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700748 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700749 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100750 }
751
John Reck92f25f82011-04-26 16:57:10 -0700752 public boolean forceEnableUserScalable() {
753 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
754 }
755
John Reck7dc444b2011-06-16 17:44:29 -0700756 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700757 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700758 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
759 return getAdjustedTextZoom(textZoom);
760 }
761
762 public void setTextZoom(int percent) {
763 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
764 }
765
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700766 public int getDoubleTapZoom() {
767 requireInitialization();
768 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
769 return getAdjustedDoubleTapZoom(doubleTapZoom);
770 }
771
772 public void setDoubleTapZoom(int percent) {
773 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
774 }
775
John Reck35e9dd62011-04-25 09:01:54 -0700776 // -----------------------------
777 // getter/setters for advanced_preferences.xml
778 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100779
John Reck35e9dd62011-04-25 09:01:54 -0700780 public String getSearchEngineName() {
Panos Thomas64c77e02014-12-17 22:15:23 -0800781 // The following is a NOP if the SEARCH_ENGINE restriction has already been created. Otherwise,
782 // it creates the restriction and if enabled it sets the <default_search_engine_value>.
783 SearchEngineRestriction.getInstance();
784
luxiaol221b3932013-07-19 15:27:57 +0800785 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
786 if (defaultSearchEngineValue == null) {
787 defaultSearchEngineValue = SearchEngine.GOOGLE;
788 }
789 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100790 }
John Reck63bb6872010-12-01 19:29:32 -0800791
Michael Kolb8d772b02012-01-19 13:56:00 -0800792 public boolean allowAppTabs() {
793 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
794 }
795
John Reck35e9dd62011-04-25 09:01:54 -0700796 public boolean openInBackground() {
797 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800798 }
John Reck35e9dd62011-04-25 09:01:54 -0700799
800 public boolean enableJavascript() {
801 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
802 }
803
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800804 public boolean enableMemoryMonitor() {
805 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
806 }
807
Tarun Nainani8eb00912014-07-17 12:28:32 -0700808
John Reck35e9dd62011-04-25 09:01:54 -0700809 public boolean loadPageInOverviewMode() {
810 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
811 }
812
813 public boolean autofitPages() {
814 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
815 }
816
817 public boolean blockPopupWindows() {
Pankaj Garg11015212015-07-06 14:38:43 -0700818 return !PermissionsServiceFactory.getDefaultPermissions(
819 PermissionsServiceFactory.PermissionType.POPUP);
John Reck35e9dd62011-04-25 09:01:54 -0700820 }
821
822 public boolean loadImages() {
823 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
824 }
825
826 public String getDefaultTextEncoding() {
Tarun Nainani88381422015-04-02 19:47:21 -0700827 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, "auto");
John Reck35e9dd62011-04-25 09:01:54 -0700828 }
829
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700830 public String getEdgeSwipeAction() {
831 return mPrefs.getString(PREF_EDGE_SWIPE,
832 mContext.getResources().getString(R.string.value_unknown_edge_swipe));
833 }
834
835 public void setEdgeSwipeTemporal() {
836 mPrefs.edit().putString(PREF_EDGE_SWIPE,
837 mContext.getResources().getString(R.string.value_temporal_edge_swipe)).apply();
838 }
839
840 public void setEdgeSwipeSpatial() {
841 mPrefs.edit().putString(PREF_EDGE_SWIPE,
842 mContext.getResources().getString(R.string.value_spatial_edge_swipe)).apply();
843 }
844
845 public void setEdgeSwipeDisabled() {
846 mPrefs.edit().putString(PREF_EDGE_SWIPE,
847 mContext.getResources().getString(R.string.value_disable_edge_swipe)).apply();
848 }
849
John Reck35e9dd62011-04-25 09:01:54 -0700850 // -----------------------------
851 // getter/setters for general_preferences.xml
852 // -----------------------------
853
854 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700855 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
856 }
857
858 public void setHomePage(String value) {
859 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
860 }
861
862 public boolean isAutofillEnabled() {
863 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
864 }
865
866 public void setAutofillEnabled(boolean value) {
867 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
868 }
869
Site Maoabb7bd32015-03-20 15:34:02 -0700870 public boolean isPowerSaveModeEnabled() {
871 return mPrefs.getBoolean(PREF_POWERSAVE_ENABLED, false);
872 }
873
874 public void setPowerSaveModeEnabled(boolean value) {
875 mPrefs.edit().putBoolean(PREF_POWERSAVE_ENABLED, value).apply();
876 }
877
Yida Wang078425c2015-06-02 14:14:06 -0400878 public boolean isNightModeEnabled() {
879 return mPrefs.getBoolean(PREF_NIGHTMODE_ENABLED, false);
880 }
881
882 public void setNightModeEnabled(boolean value) {
883 mPrefs.edit().putBoolean(PREF_NIGHTMODE_ENABLED, value).apply();
884 }
885
John Reck35e9dd62011-04-25 09:01:54 -0700886 // -----------------------------
887 // getter/setters for debug_preferences.xml
888 // -----------------------------
889
890 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700891 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700892 return true;
893 }
894 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
895 }
896
Derek Sollenberger31adf672011-07-08 11:31:30 -0400897 public boolean isSkiaHardwareAccelerated() {
898 if (!isDebugEnabled()) {
899 return false;
900 }
901 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
902 }
903
John Reck35e9dd62011-04-25 09:01:54 -0700904 // -----------------------------
905 // getter/setters for hidden_debug_preferences.xml
906 // -----------------------------
907
908 public boolean enableVisualIndicator() {
909 if (!isDebugEnabled()) {
910 return false;
911 }
912 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
913 }
914
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700915 public boolean enableCpuUploadPath() {
916 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700917 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700918 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700919 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700920 }
921
John Reck35e9dd62011-04-25 09:01:54 -0700922 public boolean isSmallScreen() {
923 if (!isDebugEnabled()) {
924 return false;
925 }
926 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
927 }
928
929 public boolean isWideViewport() {
930 if (!isDebugEnabled()) {
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800931 return true;
John Reck35e9dd62011-04-25 09:01:54 -0700932 }
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800933 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
John Reck35e9dd62011-04-25 09:01:54 -0700934 }
935
936 public boolean isNormalLayout() {
937 if (!isDebugEnabled()) {
938 return false;
939 }
940 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
941 }
942
943 public boolean isTracing() {
944 if (!isDebugEnabled()) {
945 return false;
946 }
947 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
948 }
949
950 public boolean enableLightTouch() {
951 if (!isDebugEnabled()) {
952 return false;
953 }
954 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
955 }
956
957 public boolean enableNavDump() {
958 if (!isDebugEnabled()) {
959 return false;
960 }
961 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
962 }
963
964 public String getJsEngineFlags() {
965 if (!isDebugEnabled()) {
966 return "";
967 }
968 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
969 }
970
971 // -----------------------------
972 // getter/setters for lab_preferences.xml
973 // -----------------------------
974
John Reck35e9dd62011-04-25 09:01:54 -0700975 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700976 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700977 }
978
Michael Kolbc38c6042011-04-27 10:46:06 -0700979 public boolean useFullscreen() {
980 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
981 }
982
John Reck2fd9d0e2011-07-15 11:13:48 -0700983 public boolean useInvertedRendering() {
984 return mPrefs.getBoolean(PREF_INVERTED, false);
985 }
986
Nicolas Roard5d513102011-08-03 15:35:34 -0700987 public float getInvertedContrast() {
988 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
989 }
990
John Reck35e9dd62011-04-25 09:01:54 -0700991 // -----------------------------
992 // getter/setters for privacy_security_preferences.xml
993 // -----------------------------
994
995 public boolean showSecurityWarnings() {
996 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
997 }
998
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700999 public boolean doNotTrack() {
Dave Tharpb487d5d2015-05-15 15:54:08 -07001000 boolean dntVal;
1001 if (DoNotTrackRestriction.getInstance().isEnabled()) {
1002 dntVal = DoNotTrackRestriction.getInstance().getValue();
1003 }
1004 else {
1005 dntVal = mPrefs.getBoolean(PREF_DO_NOT_TRACK, true);
1006 }
1007 return dntVal;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001008 }
1009
John Reck35e9dd62011-04-25 09:01:54 -07001010 public boolean acceptCookies() {
Pankaj Garg32e1b942015-06-03 18:13:24 -07001011 return PermissionsServiceFactory.getDefaultPermissions(
1012 PermissionsServiceFactory.PermissionType.COOKIE);
John Reck35e9dd62011-04-25 09:01:54 -07001013 }
1014
1015 public boolean saveFormdata() {
1016 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
1017 }
1018
1019 public boolean enableGeolocation() {
1020 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
1021 }
1022
1023 public boolean rememberPasswords() {
1024 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
1025 }
1026
Michael Kolb14612442011-06-24 13:06:29 -07001027 // -----------------------------
1028 // getter/setters for bandwidth_preferences.xml
1029 // -----------------------------
1030
Mathew Inwood467813f2011-09-02 15:43:17 +01001031 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
1032 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -07001033 }
Mathew Inwood467813f2011-09-02 15:43:17 +01001034
1035 public static String getPreloadAlwaysPreferenceString(Context context) {
1036 return context.getResources().getString(R.string.pref_data_preload_value_always);
1037 }
1038
Mathew Inwood825fba72011-10-04 14:52:52 +01001039 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1040 "browser_default_preload_setting";
1041
1042 public String getDefaultPreloadSetting() {
1043 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1044 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1045 if (preload == null) {
1046 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1047 }
1048 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001049 }
1050
1051 public String getPreloadEnabled() {
1052 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1053 }
1054
Victoria Lease96497832012-03-23 14:19:56 -07001055 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1056 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1057 }
1058
1059 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1060 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1061 }
1062
1063 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1064 "browser_default_link_prefetch_setting";
1065
1066 public String getDefaultLinkPrefetchSetting() {
1067 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1068 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1069 if (preload == null) {
1070 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1071 }
1072 return preload;
1073 }
1074
1075 public String getLinkPrefetchEnabled() {
1076 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1077 }
1078
George Mount3636d0a2011-11-21 09:08:21 -08001079 // -----------------------------
1080 // getter/setters for browser recovery
1081 // -----------------------------
1082 /**
1083 * The last time browser was started.
1084 * @return The last browser start time as System.currentTimeMillis. This
1085 * can be 0 if this is the first time or the last tab was closed.
1086 */
1087 public long getLastRecovered() {
1088 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1089 }
1090
1091 /**
1092 * Sets the last browser start time.
1093 * @param time The last time as System.currentTimeMillis that the browser
1094 * was started. This should be set to 0 if the last tab is closed.
1095 */
1096 public void setLastRecovered(long time) {
1097 mPrefs.edit()
1098 .putLong(KEY_LAST_RECOVERED, time)
1099 .apply();
1100 }
1101
1102 /**
1103 * Used to determine whether or not the previous browser run crashed. Once
1104 * the previous state has been determined, the value will be set to false
1105 * until a pause is received.
1106 * @return true if the last browser run was paused or false if it crashed.
1107 */
1108 public boolean wasLastRunPaused() {
1109 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1110 }
1111
1112 /**
1113 * Sets whether or not the last run was a pause or crash.
1114 * @param isPaused Set to true When a pause is received or false after
1115 * resuming.
1116 */
1117 public void setLastRunPaused(boolean isPaused) {
1118 mPrefs.edit()
1119 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1120 .apply();
1121 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001122}