blob: 04da119d8153b5ca30ff002a8228a48a55cc1628 [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 Tharp4a2c8942015-07-10 14:10:51 -070037import com.android.browser.mdm.EditBookmarksRestriction;
38import com.android.browser.mdm.IncognitoRestriction;
39import com.android.browser.mdm.ManagedBookmarksRestriction;
Panos Thomasa9ff2c72014-12-17 22:15:23 -080040import com.android.browser.mdm.ProxyRestriction;
Panos Thomas64c77e02014-12-17 22:15:23 -080041import com.android.browser.mdm.SearchEngineRestriction;
Dave Tharp4a2c8942015-07-10 14:10:51 -070042import com.android.browser.mdm.ThirdPartyCookiesRestriction;
43import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070044import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070045import com.android.browser.provider.BrowserProvider;
Bijan Amirzada41242f22014-03-21 12:12:18 -070046import com.android.browser.search.SearchEngine;
47import com.android.browser.search.SearchEngines;
John Reck46500332011-06-07 14:36:10 -070048
John Reck35e9dd62011-04-25 09:01:54 -070049import java.lang.ref.WeakReference;
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -070050import java.util.ArrayList;
John Reck35e9dd62011-04-25 09:01:54 -070051import java.util.Iterator;
52import java.util.LinkedList;
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -070053import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080054
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080055import org.codeaurora.swe.AutoFillProfile;
56import org.codeaurora.swe.CookieManager;
57import org.codeaurora.swe.GeolocationPermissions;
Pankaj Garg32e1b942015-06-03 18:13:24 -070058import org.codeaurora.swe.PermissionsServiceFactory;
Kulanthaivel Palanichamyf36e1db2015-04-08 16:11:06 -070059import org.codeaurora.swe.WebRefiner;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080060import org.codeaurora.swe.WebSettings.LayoutAlgorithm;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080061import org.codeaurora.swe.WebSettings.TextSize;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080062import org.codeaurora.swe.WebSettings;
63import org.codeaurora.swe.WebView;
Vivek Sekhare8a8ec22014-07-10 14:52:43 -070064import org.codeaurora.swe.WebViewDatabase;
Panos Thomas3f35d8d2014-11-08 22:40:23 -080065
John Reck35e9dd62011-04-25 09:01:54 -070066/**
67 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080068 */
John Reck35e9dd62011-04-25 09:01:54 -070069public class BrowserSettings implements OnSharedPreferenceChangeListener,
70 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010071
qqzhoue6ff8b42013-07-23 17:28:48 +080072 private static final String TAG = "BrowserSettings";
John Reck8fc22a12011-06-16 14:57:41 -070073 // The minimum min font size
74 // Aka, the lower bounds for the min font size range
75 // which is 1:5..24
76 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070077 // The initial value in the text zoom range
78 // This is what represents 100% in the SeekBarPreference range
79 private static final int TEXT_ZOOM_START_VAL = 10;
80 // The size of a single step in the text zoom range, in percent
81 private static final int TEXT_ZOOM_STEP = 5;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -070082 // The initial value in the double tap zoom range
83 // This is what represents 100% in the SeekBarPreference range
84 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
85 // The size of a single step in the double tap zoom range, in percent
86 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -070087
John Reck35e9dd62011-04-25 09:01:54 -070088 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -070089
John Reck35e9dd62011-04-25 09:01:54 -070090 private Context mContext;
91 private SharedPreferences mPrefs;
92 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -070093 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -070094 private WebStorageSizeManager mWebStorageSizeManager;
95 private AutofillHandler mAutofillHandler;
Ben Murdochaaa1f372011-07-25 15:40:58 +010096 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -070097 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -070098 private float mFontSizeMult = 1.0f;
John Reck78a6a1d2011-07-21 13:54:03 -070099
Victoria Lease96497832012-03-23 14:19:56 -0700100 // Current state of network-dependent settings
101 private boolean mLinkPrefetchAllowed = true;
102
John Reck78a6a1d2011-07-21 13:54:03 -0700103 // Cached values
104 private int mPageCacheCapacity = 1;
105 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800106
John Reck35e9dd62011-04-25 09:01:54 -0700107 // Cached settings
108 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109
Ben Murdochaaa1f372011-07-25 15:40:58 +0100110 private static String sFactoryResetUrl;
111
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700112 private boolean mEngineInitialized = false;
113 private boolean mSyncManagedSettings = false;
114
Bijan Amirzada3bb46302014-06-17 16:31:31 -0700115 public static synchronized void initialize(final Context context) {
116 if (sInstance == null)
117 sInstance = new BrowserSettings(context);
John Reck35e9dd62011-04-25 09:01:54 -0700118 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700120 public static BrowserSettings getInstance() {
John Reck35e9dd62011-04-25 09:01:54 -0700121 return sInstance;
122 }
Ben Murdochef671652010-11-25 17:17:58 +0000123
John Reck35e9dd62011-04-25 09:01:54 -0700124 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100125 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700126 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
John Reck35e9dd62011-04-25 09:01:54 -0700127 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckcadae722011-07-25 11:36:17 -0700128 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700129 }
130
131 public void setController(Controller controller) {
132 mController = controller;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700133 mNeedsSharedSync = true;
134 }
135
136 public void onEngineInitializationComplete() {
137 mEngineInitialized = true;
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700138
139 // Intialize Web Refiner only once
140 final WebRefiner refiner = WebRefiner.getInstance();
141 if (refiner != null) {
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700142 refiner.setDefaultPermission(PermissionsServiceFactory.getDefaultPermissions(
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700143 PermissionsServiceFactory.PermissionType.WEBREFINER));
144
145 PermissionsServiceFactory.getPermissionsService(
146 new ValueCallback<PermissionsServiceFactory.PermissionsService>() {
147 @Override
148 public void onReceiveValue(
149 PermissionsServiceFactory.PermissionsService value) {
150 Set<String> origins = value.getOrigins();
151 ArrayList<String> allowList = new ArrayList<>();
152 ArrayList<String> blockList = new ArrayList<>();
153 for (String origin : origins) {
154 PermissionsServiceFactory.PermissionsService.OriginInfo
155 info = value.getOriginInfo(origin);
156 int perm = info.getPermission(
157 PermissionsServiceFactory.PermissionType.WEBREFINER);
158 if (perm == PermissionsServiceFactory.Permission.ALLOW) {
159 allowList.add(origin);
160 } else if (perm == PermissionsServiceFactory.Permission.BLOCK) {
161 blockList.add(origin);
162 }
163 }
164 if (!allowList.isEmpty()) {
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700165 refiner.setPermissionForOrigins(
166 allowList.toArray(new String[allowList.size()]), true);
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700167 }
168
169 if (!blockList.isEmpty()) {
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700170 refiner.setPermissionForOrigins(
171 blockList.toArray(new String[blockList.size()]), false);
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700172 }
173 }
174 }
175 );
176 }
177
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700178 mAutofillHandler = new AutofillHandler(mContext);
179 if (mSyncManagedSettings) {
180 syncManagedSettings();
181 }
182 if (mNeedsSharedSync) {
John Reckc477e712011-08-17 11:27:15 -0700183 syncSharedSettings();
184 }
Dave Tharp4a2c8942015-07-10 14:10:51 -0700185
186 // Instantiate all MDM Restriction Singletons.
187 AutoFillRestriction.getInstance();
188 DevToolsRestriction.getInstance();
189 DoNotTrackRestriction.getInstance();
190 EditBookmarksRestriction.getInstance();
191 IncognitoRestriction.getInstance();
192 ManagedBookmarksRestriction.getInstance();
Panos Thomasa9ff2c72014-12-17 22:15:23 -0800193 ProxyRestriction.getInstance();
Dave Tharp4a2c8942015-07-10 14:10:51 -0700194 SearchEngineRestriction.getInstance();
195 ThirdPartyCookiesRestriction.getInstance();
196 URLFilterRestriction.getInstance();
Ben Murdochef671652010-11-25 17:17:58 +0000197 }
198
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800199 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800200
John Reckc477e712011-08-17 11:27:15 -0700201 if (mNeedsSharedSync) {
202 syncSharedSettings();
203 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800204
John Reck35e9dd62011-04-25 09:01:54 -0700205 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800206 syncStaticSettings(settings);
207 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700208 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800209 }
210 }
211
John Reckd1d87312012-03-08 13:25:00 -0800212 public void stopManagingSettings(WebSettings settings) {
213 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
214 while (iter.hasNext()) {
215 WeakReference<WebSettings> ref = iter.next();
216 if (ref.get() == settings) {
217 iter.remove();
218 return;
219 }
220 }
221 }
222
John Reckcadae722011-07-25 11:36:17 -0700223 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700224
225 @Override
226 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700227 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
228 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700229 // the cost of one cached page is ~3M (measured using nytimes.com). For
230 // low end devices, we only cache one page. For high end devices, we try
231 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800232
233 // SWE_TODO : assume a high-memory device
234 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700235 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800236 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700237 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
238 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
239 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700240 // Workaround b/5254577
241 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700242 if (Build.VERSION.CODENAME.equals("REL")) {
243 // This is a release build, always startup with debug disabled
244 setDebugEnabled(false);
245 }
246 if (mPrefs.contains(PREF_TEXT_SIZE)) {
247 /*
248 * Update from TextSize enum to zoom percent
249 * SMALLEST is 50%
250 * SMALLER is 75%
251 * NORMAL is 100%
252 * LARGER is 150%
253 * LARGEST is 200%
254 */
255 switch (getTextSize()) {
256 case SMALLEST:
257 setTextZoom(50);
258 break;
259 case SMALLER:
260 setTextZoom(75);
261 break;
262 case LARGER:
263 setTextZoom(150);
264 break;
265 case LARGEST:
266 setTextZoom(200);
267 break;
268 }
269 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
270 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100271
qqzhou8c5b0a32013-07-22 15:31:03 +0800272 // add for carrier homepage feature
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700273 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
qqzhou8c5b0a32013-07-22 15:31:03 +0800274
kaiyizbf086ea2013-08-02 11:03:58 +0800275 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
Tarun Nainani95b79682014-12-09 09:38:04 -0800276 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING, "auto").apply();
kaiyizbf086ea2013-08-02 11:03:58 +0800277 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700278
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700279 if (!mPrefs.contains(PREF_EDGE_SWIPE)) {
280 mPrefs.edit().putString(PREF_EDGE_SWIPE,
281 mContext.getResources().getString(
282 R.string.value_unknown_edge_swipe)).apply();
283 }
284
Ben Murdochaaa1f372011-07-25 15:40:58 +0100285 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
286 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
287 BrowserProvider.getClientId(mContext.getContentResolver()));
288 }
289
Ben Murdochaaa1f372011-07-25 15:40:58 +0100290 synchronized (BrowserSettings.class) {
291 sInitialized = true;
292 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700293 }
294 }
295 };
296
Ben Murdochaaa1f372011-07-25 15:40:58 +0100297 private static void requireInitialization() {
298 synchronized (BrowserSettings.class) {
299 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700300 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100301 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700302 } catch (InterruptedException e) {
303 }
304 }
305 }
306 }
307
The Android Open Source Project0c908882009-03-03 19:32:16 -0800308 /**
John Reck35e9dd62011-04-25 09:01:54 -0700309 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800310 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800311 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700312 settings.setGeolocationEnabled(enableGeolocation());
313 settings.setJavaScriptEnabled(enableJavascript());
314 settings.setLightTouchEnabled(enableLightTouch());
315 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700316 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
John Reck35e9dd62011-04-25 09:01:54 -0700317 settings.setMinimumFontSize(getMinimumFontSize());
318 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck7dc444b2011-06-16 17:44:29 -0700319 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700320 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100321 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700322 settings.setLoadsImagesAutomatically(loadImages());
323 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
324 settings.setSavePassword(rememberPasswords());
325 settings.setSaveFormData(saveFormdata());
326 settings.setUseWideViewPort(isWideViewport());
Panos Thomasb10bbda2014-06-23 10:18:36 -0700327 settings.setDoNotTrack(doNotTrack());
Yida Wang078425c2015-06-02 14:14:06 -0400328 settings.setNightModeEnabled(isNightModeEnabled());
Tarun Nainani8eb00912014-07-17 12:28:32 -0700329 settings.setMediaPlaybackRequiresUserGesture(false);
John Reck2fd9d0e2011-07-15 11:13:48 -0700330
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800331 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800332 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
333 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
334 settingsClassic.setForceUserScalable(forceEnableUserScalable());
335 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
336 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800337
John Reckea17e782011-10-27 17:15:59 -0700338 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800339 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700340 useInverted ? "true" : "false");
341 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800342 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700343 Float.toString(getInvertedContrast()));
344 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700345
John Reckea17e782011-10-27 17:15:59 -0700346 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800347 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700348 enableCpuUploadPath() ? "true" : "false");
349 }
Victoria Lease96497832012-03-23 14:19:56 -0700350
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800351 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000352 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800353
Tarun Nainani8eb00912014-07-17 12:28:32 -0700354
John Reck35e9dd62011-04-25 09:01:54 -0700355 /**
356 * Syncs all the settings that have no UI
357 * These cannot change, so we only need to set them once per WebSettings
358 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800359 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700360 settings.setDefaultFontSize(16);
361 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000362
John Reck35e9dd62011-04-25 09:01:54 -0700363 // WebView inside Browser doesn't want initial focus to be set.
364 settings.setNeedInitialFocus(false);
365 // Browser supports multiple windows
366 settings.setSupportMultipleWindows(true);
367 // enable smooth transition for better performance during panning or
368 // zooming
369 settings.setEnableSmoothTransition(true);
370 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700371 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700372
373 // HTML5 API flags
374 settings.setAppCacheEnabled(true);
375 settings.setDatabaseEnabled(true);
376 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700377
378 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700379 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700380 settings.setAppCachePath(getAppCachePath());
381 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
382 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700383 // origin policy for file access
384 settings.setAllowUniversalAccessFromFileURLs(false);
385 settings.setAllowFileAccessFromFileURLs(false);
Sudheer Koganti24766882014-10-02 10:58:09 -0700386 settings.setFullscreenSupported(true);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800387
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800388 //if (!(settings instanceof WebSettingsClassic)) return;
389 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800390
391 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
392 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
393 // WebView should be preserving the memory as much as possible.
394 // However, apps like browser wish to turn on the performance mode which
395 // would require more memory.
396 // TODO: We need to dynamically allocate/deallocate temporary memory for
397 // apps which are trying to use minimal memory. Currently, double
398 // buffering is always turned on, which is unnecessary.
399 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
400 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800401 */
John Reck35e9dd62011-04-25 09:01:54 -0700402 }
403
404 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700405 mNeedsSharedSync = false;
Axesh R. Ajmera579c70c2014-04-17 13:24:56 -0700406 CookieManager.getInstance().setAcceptCookie(acceptCookies());
Vivek Sekhared791da2015-02-22 12:39:05 -0800407
John Reck35e9dd62011-04-25 09:01:54 -0700408 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700409
John Reck35e9dd62011-04-25 09:01:54 -0700410 private void syncManagedSettings() {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700411 if (!mEngineInitialized) {
412 mSyncManagedSettings = true;
413 return;
414 }
415 mSyncManagedSettings = false;
John Reck35e9dd62011-04-25 09:01:54 -0700416 syncSharedSettings();
417 synchronized (mManagedSettings) {
418 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
419 while (iter.hasNext()) {
420 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800421 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700422 if (settings == null) {
423 iter.remove();
424 continue;
425 }
426 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000427 }
John Reck35e9dd62011-04-25 09:01:54 -0700428 }
429 }
Ben Murdochef671652010-11-25 17:17:58 +0000430
John Reck35e9dd62011-04-25 09:01:54 -0700431 @Override
432 public void onSharedPreferenceChanged(
433 SharedPreferences sharedPreferences, String key) {
434 syncManagedSettings();
435 if (PREF_SEARCH_ENGINE.equals(key)) {
436 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700437 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200438 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700439 mController.getUi().setFullscreen(useFullscreen());
440 }
Victoria Lease96497832012-03-23 14:19:56 -0700441 } else if (PREF_LINK_PREFETCH.equals(key)) {
442 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700443 }
John Reck35e9dd62011-04-25 09:01:54 -0700444 }
445
John Reck961d35d2011-06-23 09:45:54 -0700446 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100447 requireInitialization();
448 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700449 }
450
451 public LayoutAlgorithm getLayoutAlgorithm() {
452 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
453 if (autofitPages()) {
Tarun Nainani003bec52014-07-02 02:20:34 -0700454 layoutAlgorithm = LayoutAlgorithm.TEXT_AUTOSIZING;
John Reck35e9dd62011-04-25 09:01:54 -0700455 }
456 if (isDebugEnabled()) {
457 if (isSmallScreen()) {
458 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000459 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700460 if (isNormalLayout()) {
461 layoutAlgorithm = LayoutAlgorithm.NORMAL;
462 } else {
463 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
464 }
Ben Murdochef671652010-11-25 17:17:58 +0000465 }
John Reck35e9dd62011-04-25 09:01:54 -0700466 }
467 return layoutAlgorithm;
468 }
Ben Murdochef671652010-11-25 17:17:58 +0000469
John Reck35e9dd62011-04-25 09:01:54 -0700470 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700471 requireInitialization();
472 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800473 }
474
John Reck35e9dd62011-04-25 09:01:54 -0700475 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700476 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700477 return mWebStorageSizeManager;
478 }
479
John Reck35e9dd62011-04-25 09:01:54 -0700480 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700481 if (mAppCachePath == null) {
482 mAppCachePath = mContext.getDir("appcache", 0).getPath();
483 }
484 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700485 }
486
487 private void updateSearchEngine(boolean force) {
488 String searchEngineName = getSearchEngineName();
489 if (force || mSearchEngine == null ||
490 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700491 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000492 }
493 }
494
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100495 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700496 if (mSearchEngine == null) {
497 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100498 }
John Reck35e9dd62011-04-25 09:01:54 -0700499 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100500 }
501
John Reck35e9dd62011-04-25 09:01:54 -0700502 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700503 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700504 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100505 }
506
John Reck35e9dd62011-04-25 09:01:54 -0700507 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700508 Editor edit = mPrefs.edit();
509 edit.putBoolean(PREF_DEBUG_MENU, value);
510 if (!value) {
511 // Reset to "safe" value
512 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
513 }
514 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100515 }
516
John Reck35e9dd62011-04-25 09:01:54 -0700517 public void clearCache() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700518 if (mController != null) {
519 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800520 if (current != null) {
521 current.clearCache(true);
522 }
523 }
524 }
525
John Reck35e9dd62011-04-25 09:01:54 -0700526 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800527 CookieManager.getInstance().removeAllCookie();
528 }
529
John Reck35e9dd62011-04-25 09:01:54 -0700530 public void clearHistory() {
531 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800532 Browser.clearHistory(resolver);
533 Browser.clearSearches(resolver);
534 }
535
John Reck35e9dd62011-04-25 09:01:54 -0700536 public void clearFormData() {
537 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700538 if (mController!= null) {
539 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100540 if (currentTopView != null) {
541 currentTopView.clearFormData();
542 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800543 }
544 }
545
Bijan Amirzada3bbb3a42014-06-17 12:06:18 -0700546 public WebView getTopWebView(){
547 if (mController!= null)
548 return mController.getCurrentTopWebView();
549
550 return null;
551 }
552
John Reck35e9dd62011-04-25 09:01:54 -0700553 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700554 // Clear password store maintained by SWE engine
555 WebSettings settings = null;
556 // find a valid settings object
557 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
558 while (iter.hasNext()) {
559 WeakReference<WebSettings> ref = iter.next();
560 settings = (WebSettings)ref.get();
561 if (settings != null) {
562 break;
563 }
564 }
565 if (settings != null) {
Panos Thomasdbaea4e2014-05-22 06:48:47 -0700566 settings.clearPasswords();
Panos Thomasfa948b82014-03-09 03:42:42 -0700567 }
Vivek Sekhare8a8ec22014-07-10 14:52:43 -0700568
569 // Clear passwords in WebView database
570 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
571 db.clearHttpAuthUsernamePassword();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800572 }
573
John Reck35e9dd62011-04-25 09:01:54 -0700574 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100575 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100576 }
577
John Reck35e9dd62011-04-25 09:01:54 -0700578 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100579 GeolocationPermissions.getInstance().clearAll();
Panos Thomasb298aad2014-10-22 12:24:21 -0700580 if (GeolocationPermissions.isIncognitoCreated()) {
581 GeolocationPermissions.getIncognitoInstance().clearAll();
582 }
Nicolas Roard78a98e42009-05-11 13:34:17 +0100583 }
584
John Reck35e9dd62011-04-25 09:01:54 -0700585 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700586 mPrefs.edit()
587 .clear()
John Reckbd315192011-07-29 10:05:47 -0700588 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200589 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700590 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700591 }
592
Björn Isakssonc885a242012-06-05 17:19:04 +0200593 private void resetCachedValues() {
594 updateSearchEngine(false);
595 }
596
John Reck35e9dd62011-04-25 09:01:54 -0700597 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800598 // query the profile from components autofill database 524
599 if (mAutofillHandler.mAutoFillProfile == null &&
600 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
601 WebSettings settings = null;
602 // find a valid settings object
603 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
604 while (iter.hasNext()) {
605 WeakReference<WebSettings> ref = iter.next();
606 settings = (WebSettings)ref.get();
607 if (settings != null) {
608 break;
609 }
610 }
611 if (settings != null) {
612 AutoFillProfile profile =
613 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
614 mAutofillHandler.setAutoFillProfile(profile);
615 }
616 }
John Reck35e9dd62011-04-25 09:01:54 -0700617 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800618 }
619
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800620 public String getAutoFillProfileId() {
621 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800622 }
623
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800624 public void updateAutoFillProfile(AutoFillProfile profile) {
625 syncAutoFillProfile(profile);
626 }
627
628 private void syncAutoFillProfile(AutoFillProfile profile) {
629 synchronized (mManagedSettings) {
630 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
631 while (iter.hasNext()) {
632 WeakReference<WebSettings> ref = iter.next();
633 WebSettings settings = (WebSettings)ref.get();
634 if (settings == null) {
635 iter.remove();
636 continue;
637 }
638 // update the profile only once.
639 settings.setAutoFillProfile(profile);
640 // Now we should have the guid
641 mAutofillHandler.setAutoFillProfile(profile);
642 break;
643 }
644 }
645 }
John Reck35e9dd62011-04-25 09:01:54 -0700646 public void toggleDebugSettings() {
647 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800648 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100649
John Reckb8b2af82011-05-20 15:58:33 -0700650 public boolean hasDesktopUseragent(WebView view) {
Vivek Sekhar7b3d2da2014-11-13 16:20:11 -0800651 return view != null && view.getUseDesktopUserAgent();
John Reckb8b2af82011-05-20 15:58:33 -0700652 }
653
654 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700655 if (view == null) {
656 return;
657 }
Vivek Sekhar7b3d2da2014-11-13 16:20:11 -0800658 if (hasDesktopUseragent(view))
659 view.setUseDesktopUserAgent(false, true);
660 else
661 view.setUseDesktopUserAgent(true, true);
John Reckb8b2af82011-05-20 15:58:33 -0700662 }
663
John Reck7dc444b2011-06-16 17:44:29 -0700664 public static int getAdjustedMinimumFontSize(int rawValue) {
665 rawValue++; // Preference starts at 0, min font at 1
666 if (rawValue > 1) {
667 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
668 }
669 return rawValue;
670 }
671
John Reck5ba3c762011-09-14 15:00:15 -0700672 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700673 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700674 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700675 }
676
677 static int getRawTextZoom(int percent) {
678 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
679 }
680
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700681 public int getAdjustedDoubleTapZoom(int rawValue) {
682 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
683 return (int) ((rawValue + 100) * mFontSizeMult);
684 }
685
686 static int getRawDoubleTapZoom(int percent) {
687 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
688 }
689
John Reckcadae722011-07-25 11:36:17 -0700690 public SharedPreferences getPreferences() {
691 return mPrefs;
692 }
693
Victoria Lease96497832012-03-23 14:19:56 -0700694 // update connectivity-dependent options
695 public void updateConnectionType() {
696 ConnectivityManager cm = (ConnectivityManager)
697 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
698 String linkPrefetchPreference = getLinkPrefetchEnabled();
699 boolean linkPrefetchAllowed = linkPrefetchPreference.
700 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
701 NetworkInfo ni = cm.getActiveNetworkInfo();
702 if (ni != null) {
703 switch (ni.getType()) {
704 case ConnectivityManager.TYPE_WIFI:
705 case ConnectivityManager.TYPE_ETHERNET:
706 case ConnectivityManager.TYPE_BLUETOOTH:
707 linkPrefetchAllowed |= linkPrefetchPreference.
708 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
709 break;
710 case ConnectivityManager.TYPE_MOBILE:
711 case ConnectivityManager.TYPE_MOBILE_DUN:
712 case ConnectivityManager.TYPE_MOBILE_MMS:
713 case ConnectivityManager.TYPE_MOBILE_SUPL:
714 case ConnectivityManager.TYPE_WIMAX:
715 default:
716 break;
717 }
718 }
719 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
720 mLinkPrefetchAllowed = linkPrefetchAllowed;
721 syncManagedSettings();
722 }
723 }
724
luxiaol62677b02013-07-22 07:54:49 +0800725 public String getDownloadPath() {
726 return mPrefs.getString(PREF_DOWNLOAD_PATH,
727 DownloadHandler.getDefaultDownloadPath(mContext));
728 }
John Reck35e9dd62011-04-25 09:01:54 -0700729 // -----------------------------
730 // getter/setters for accessibility_preferences.xml
731 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100732
John Reck7dc444b2011-06-16 17:44:29 -0700733 @Deprecated
734 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700735 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
736 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100737 }
738
John Reck35e9dd62011-04-25 09:01:54 -0700739 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700740 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700741 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100742 }
743
John Reck92f25f82011-04-26 16:57:10 -0700744 public boolean forceEnableUserScalable() {
745 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
746 }
747
John Reck7dc444b2011-06-16 17:44:29 -0700748 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700749 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700750 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
751 return getAdjustedTextZoom(textZoom);
752 }
753
754 public void setTextZoom(int percent) {
755 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
756 }
757
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700758 public int getDoubleTapZoom() {
759 requireInitialization();
760 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
761 return getAdjustedDoubleTapZoom(doubleTapZoom);
762 }
763
764 public void setDoubleTapZoom(int percent) {
765 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
766 }
767
John Reck35e9dd62011-04-25 09:01:54 -0700768 // -----------------------------
769 // getter/setters for advanced_preferences.xml
770 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100771
John Reck35e9dd62011-04-25 09:01:54 -0700772 public String getSearchEngineName() {
Panos Thomas64c77e02014-12-17 22:15:23 -0800773 // The following is a NOP if the SEARCH_ENGINE restriction has already been created. Otherwise,
774 // it creates the restriction and if enabled it sets the <default_search_engine_value>.
775 SearchEngineRestriction.getInstance();
776
luxiaol221b3932013-07-19 15:27:57 +0800777 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
778 if (defaultSearchEngineValue == null) {
779 defaultSearchEngineValue = SearchEngine.GOOGLE;
780 }
781 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100782 }
John Reck63bb6872010-12-01 19:29:32 -0800783
Michael Kolb8d772b02012-01-19 13:56:00 -0800784 public boolean allowAppTabs() {
785 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
786 }
787
John Reck35e9dd62011-04-25 09:01:54 -0700788 public boolean openInBackground() {
789 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800790 }
John Reck35e9dd62011-04-25 09:01:54 -0700791
792 public boolean enableJavascript() {
793 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
794 }
795
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800796 public boolean enableMemoryMonitor() {
797 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
798 }
799
Tarun Nainani8eb00912014-07-17 12:28:32 -0700800
John Reck35e9dd62011-04-25 09:01:54 -0700801 public boolean loadPageInOverviewMode() {
802 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
803 }
804
805 public boolean autofitPages() {
806 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
807 }
808
809 public boolean blockPopupWindows() {
Pankaj Garg11015212015-07-06 14:38:43 -0700810 return !PermissionsServiceFactory.getDefaultPermissions(
811 PermissionsServiceFactory.PermissionType.POPUP);
John Reck35e9dd62011-04-25 09:01:54 -0700812 }
813
814 public boolean loadImages() {
815 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
816 }
817
818 public String getDefaultTextEncoding() {
Tarun Nainani88381422015-04-02 19:47:21 -0700819 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, "auto");
John Reck35e9dd62011-04-25 09:01:54 -0700820 }
821
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700822 public String getEdgeSwipeAction() {
823 return mPrefs.getString(PREF_EDGE_SWIPE,
824 mContext.getResources().getString(R.string.value_unknown_edge_swipe));
825 }
826
827 public void setEdgeSwipeTemporal() {
828 mPrefs.edit().putString(PREF_EDGE_SWIPE,
829 mContext.getResources().getString(R.string.value_temporal_edge_swipe)).apply();
830 }
831
832 public void setEdgeSwipeSpatial() {
833 mPrefs.edit().putString(PREF_EDGE_SWIPE,
834 mContext.getResources().getString(R.string.value_spatial_edge_swipe)).apply();
835 }
836
837 public void setEdgeSwipeDisabled() {
838 mPrefs.edit().putString(PREF_EDGE_SWIPE,
839 mContext.getResources().getString(R.string.value_disable_edge_swipe)).apply();
840 }
841
John Reck35e9dd62011-04-25 09:01:54 -0700842 // -----------------------------
843 // getter/setters for general_preferences.xml
844 // -----------------------------
845
846 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700847 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
848 }
849
850 public void setHomePage(String value) {
851 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
852 }
853
854 public boolean isAutofillEnabled() {
855 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
856 }
857
858 public void setAutofillEnabled(boolean value) {
859 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
860 }
861
Site Maoabb7bd32015-03-20 15:34:02 -0700862 public boolean isPowerSaveModeEnabled() {
863 return mPrefs.getBoolean(PREF_POWERSAVE_ENABLED, false);
864 }
865
866 public void setPowerSaveModeEnabled(boolean value) {
867 mPrefs.edit().putBoolean(PREF_POWERSAVE_ENABLED, value).apply();
868 }
869
Yida Wang078425c2015-06-02 14:14:06 -0400870 public boolean isNightModeEnabled() {
871 return mPrefs.getBoolean(PREF_NIGHTMODE_ENABLED, false);
872 }
873
874 public void setNightModeEnabled(boolean value) {
875 mPrefs.edit().putBoolean(PREF_NIGHTMODE_ENABLED, value).apply();
876 }
877
John Reck35e9dd62011-04-25 09:01:54 -0700878 // -----------------------------
879 // getter/setters for debug_preferences.xml
880 // -----------------------------
881
882 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700883 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700884 return true;
885 }
886 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
887 }
888
Derek Sollenberger31adf672011-07-08 11:31:30 -0400889 public boolean isSkiaHardwareAccelerated() {
890 if (!isDebugEnabled()) {
891 return false;
892 }
893 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
894 }
895
John Reck35e9dd62011-04-25 09:01:54 -0700896 // -----------------------------
897 // getter/setters for hidden_debug_preferences.xml
898 // -----------------------------
899
900 public boolean enableVisualIndicator() {
901 if (!isDebugEnabled()) {
902 return false;
903 }
904 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
905 }
906
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700907 public boolean enableCpuUploadPath() {
908 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700909 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700910 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700911 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700912 }
913
John Reck35e9dd62011-04-25 09:01:54 -0700914 public boolean isSmallScreen() {
915 if (!isDebugEnabled()) {
916 return false;
917 }
918 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
919 }
920
921 public boolean isWideViewport() {
922 if (!isDebugEnabled()) {
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800923 return true;
John Reck35e9dd62011-04-25 09:01:54 -0700924 }
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800925 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
John Reck35e9dd62011-04-25 09:01:54 -0700926 }
927
928 public boolean isNormalLayout() {
929 if (!isDebugEnabled()) {
930 return false;
931 }
932 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
933 }
934
935 public boolean isTracing() {
936 if (!isDebugEnabled()) {
937 return false;
938 }
939 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
940 }
941
942 public boolean enableLightTouch() {
943 if (!isDebugEnabled()) {
944 return false;
945 }
946 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
947 }
948
949 public boolean enableNavDump() {
950 if (!isDebugEnabled()) {
951 return false;
952 }
953 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
954 }
955
956 public String getJsEngineFlags() {
957 if (!isDebugEnabled()) {
958 return "";
959 }
960 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
961 }
962
963 // -----------------------------
964 // getter/setters for lab_preferences.xml
965 // -----------------------------
966
John Reck35e9dd62011-04-25 09:01:54 -0700967 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700968 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700969 }
970
Michael Kolbc38c6042011-04-27 10:46:06 -0700971 public boolean useFullscreen() {
972 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
973 }
974
John Reck2fd9d0e2011-07-15 11:13:48 -0700975 public boolean useInvertedRendering() {
976 return mPrefs.getBoolean(PREF_INVERTED, false);
977 }
978
Nicolas Roard5d513102011-08-03 15:35:34 -0700979 public float getInvertedContrast() {
980 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
981 }
982
John Reck35e9dd62011-04-25 09:01:54 -0700983 // -----------------------------
984 // getter/setters for privacy_security_preferences.xml
985 // -----------------------------
986
987 public boolean showSecurityWarnings() {
988 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
989 }
990
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700991 public boolean doNotTrack() {
Dave Tharpb487d5d2015-05-15 15:54:08 -0700992 boolean dntVal;
993 if (DoNotTrackRestriction.getInstance().isEnabled()) {
994 dntVal = DoNotTrackRestriction.getInstance().getValue();
995 }
996 else {
997 dntVal = mPrefs.getBoolean(PREF_DO_NOT_TRACK, true);
998 }
999 return dntVal;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001000 }
1001
John Reck35e9dd62011-04-25 09:01:54 -07001002 public boolean acceptCookies() {
Pankaj Garg32e1b942015-06-03 18:13:24 -07001003 return PermissionsServiceFactory.getDefaultPermissions(
1004 PermissionsServiceFactory.PermissionType.COOKIE);
John Reck35e9dd62011-04-25 09:01:54 -07001005 }
1006
1007 public boolean saveFormdata() {
1008 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
1009 }
1010
1011 public boolean enableGeolocation() {
1012 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
1013 }
1014
1015 public boolean rememberPasswords() {
1016 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
1017 }
1018
Michael Kolb14612442011-06-24 13:06:29 -07001019 // -----------------------------
1020 // getter/setters for bandwidth_preferences.xml
1021 // -----------------------------
1022
Mathew Inwood467813f2011-09-02 15:43:17 +01001023 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
1024 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -07001025 }
Mathew Inwood467813f2011-09-02 15:43:17 +01001026
1027 public static String getPreloadAlwaysPreferenceString(Context context) {
1028 return context.getResources().getString(R.string.pref_data_preload_value_always);
1029 }
1030
Mathew Inwood825fba72011-10-04 14:52:52 +01001031 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1032 "browser_default_preload_setting";
1033
1034 public String getDefaultPreloadSetting() {
1035 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1036 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1037 if (preload == null) {
1038 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1039 }
1040 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001041 }
1042
1043 public String getPreloadEnabled() {
1044 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1045 }
1046
Victoria Lease96497832012-03-23 14:19:56 -07001047 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1048 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1049 }
1050
1051 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1052 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1053 }
1054
1055 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1056 "browser_default_link_prefetch_setting";
1057
1058 public String getDefaultLinkPrefetchSetting() {
1059 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1060 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1061 if (preload == null) {
1062 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1063 }
1064 return preload;
1065 }
1066
1067 public String getLinkPrefetchEnabled() {
1068 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1069 }
1070
George Mount3636d0a2011-11-21 09:08:21 -08001071 // -----------------------------
1072 // getter/setters for browser recovery
1073 // -----------------------------
1074 /**
1075 * The last time browser was started.
1076 * @return The last browser start time as System.currentTimeMillis. This
1077 * can be 0 if this is the first time or the last tab was closed.
1078 */
1079 public long getLastRecovered() {
1080 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1081 }
1082
1083 /**
1084 * Sets the last browser start time.
1085 * @param time The last time as System.currentTimeMillis that the browser
1086 * was started. This should be set to 0 if the last tab is closed.
1087 */
1088 public void setLastRecovered(long time) {
1089 mPrefs.edit()
1090 .putLong(KEY_LAST_RECOVERED, time)
1091 .apply();
1092 }
1093
1094 /**
1095 * Used to determine whether or not the previous browser run crashed. Once
1096 * the previous state has been determined, the value will be set to false
1097 * until a pause is received.
1098 * @return true if the last browser run was paused or false if it crashed.
1099 */
1100 public boolean wasLastRunPaused() {
1101 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1102 }
1103
1104 /**
1105 * Sets whether or not the last run was a pause or crash.
1106 * @param isPaused Set to true When a pause is received or false after
1107 * resuming.
1108 */
1109 public void setLastRunPaused(boolean isPaused) {
1110 mPrefs.edit()
1111 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1112 .apply();
1113 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001114}