blob: 60f13d50ae4f1913e5123524fa39a9b3d2347d12 [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);
158 int perm = info.getPermission(
159 PermissionsServiceFactory.PermissionType.WEBREFINER);
160 if (perm == PermissionsServiceFactory.Permission.ALLOW) {
161 allowList.add(origin);
162 } else if (perm == PermissionsServiceFactory.Permission.BLOCK) {
163 blockList.add(origin);
164 }
165 }
166 if (!allowList.isEmpty()) {
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700167 refiner.setPermissionForOrigins(
168 allowList.toArray(new String[allowList.size()]), true);
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700169 }
170
171 if (!blockList.isEmpty()) {
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700172 refiner.setPermissionForOrigins(
173 blockList.toArray(new String[blockList.size()]), false);
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700174 }
175 }
176 }
177 );
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700178 } else {
179 mPrefs.edit().putBoolean(PREF_WEB_REFINER, false).apply();
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700180 }
181
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700182 mAutofillHandler = new AutofillHandler(mContext);
183 if (mSyncManagedSettings) {
184 syncManagedSettings();
185 }
186 if (mNeedsSharedSync) {
John Reckc477e712011-08-17 11:27:15 -0700187 syncSharedSettings();
188 }
Dave Tharp4a2c8942015-07-10 14:10:51 -0700189
190 // Instantiate all MDM Restriction Singletons.
191 AutoFillRestriction.getInstance();
192 DevToolsRestriction.getInstance();
193 DoNotTrackRestriction.getInstance();
Dave Tharp13f3f7c2015-07-16 14:58:51 -0700194 DownloadDirRestriction.getInstance();
Dave Tharp4a2c8942015-07-10 14:10:51 -0700195 EditBookmarksRestriction.getInstance();
196 IncognitoRestriction.getInstance();
197 ManagedBookmarksRestriction.getInstance();
Panos Thomasa9ff2c72014-12-17 22:15:23 -0800198 ProxyRestriction.getInstance();
Dave Tharp4a2c8942015-07-10 14:10:51 -0700199 SearchEngineRestriction.getInstance();
200 ThirdPartyCookiesRestriction.getInstance();
201 URLFilterRestriction.getInstance();
Ben Murdochef671652010-11-25 17:17:58 +0000202 }
203
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800204 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800205
John Reckc477e712011-08-17 11:27:15 -0700206 if (mNeedsSharedSync) {
207 syncSharedSettings();
208 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800209
John Reck35e9dd62011-04-25 09:01:54 -0700210 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800211 syncStaticSettings(settings);
212 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700213 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800214 }
215 }
216
John Reckd1d87312012-03-08 13:25:00 -0800217 public void stopManagingSettings(WebSettings settings) {
218 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
219 while (iter.hasNext()) {
220 WeakReference<WebSettings> ref = iter.next();
221 if (ref.get() == settings) {
222 iter.remove();
223 return;
224 }
225 }
226 }
227
John Reckcadae722011-07-25 11:36:17 -0700228 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700229
230 @Override
231 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700232 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
233 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700234 // the cost of one cached page is ~3M (measured using nytimes.com). For
235 // low end devices, we only cache one page. For high end devices, we try
236 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800237
238 // SWE_TODO : assume a high-memory device
239 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700240 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800241 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700242 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
243 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
244 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700245 // Workaround b/5254577
246 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700247 if (Build.VERSION.CODENAME.equals("REL")) {
248 // This is a release build, always startup with debug disabled
249 setDebugEnabled(false);
250 }
251 if (mPrefs.contains(PREF_TEXT_SIZE)) {
252 /*
253 * Update from TextSize enum to zoom percent
254 * SMALLEST is 50%
255 * SMALLER is 75%
256 * NORMAL is 100%
257 * LARGER is 150%
258 * LARGEST is 200%
259 */
260 switch (getTextSize()) {
261 case SMALLEST:
262 setTextZoom(50);
263 break;
264 case SMALLER:
265 setTextZoom(75);
266 break;
267 case LARGER:
268 setTextZoom(150);
269 break;
270 case LARGEST:
271 setTextZoom(200);
272 break;
273 }
274 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
275 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100276
qqzhou8c5b0a32013-07-22 15:31:03 +0800277 // add for carrier homepage feature
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700278 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
qqzhou8c5b0a32013-07-22 15:31:03 +0800279
kaiyizbf086ea2013-08-02 11:03:58 +0800280 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
Tarun Nainani95b79682014-12-09 09:38:04 -0800281 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING, "auto").apply();
kaiyizbf086ea2013-08-02 11:03:58 +0800282 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700283
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700284 if (!mPrefs.contains(PREF_EDGE_SWIPE)) {
285 mPrefs.edit().putString(PREF_EDGE_SWIPE,
286 mContext.getResources().getString(
287 R.string.value_unknown_edge_swipe)).apply();
288 }
289
Ben Murdochaaa1f372011-07-25 15:40:58 +0100290 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
291 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
292 BrowserProvider.getClientId(mContext.getContentResolver()));
293 }
294
Ben Murdochaaa1f372011-07-25 15:40:58 +0100295 synchronized (BrowserSettings.class) {
296 sInitialized = true;
297 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700298 }
299 }
300 };
301
Ben Murdochaaa1f372011-07-25 15:40:58 +0100302 private static void requireInitialization() {
303 synchronized (BrowserSettings.class) {
304 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700305 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100306 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700307 } catch (InterruptedException e) {
308 }
309 }
310 }
311 }
312
The Android Open Source Project0c908882009-03-03 19:32:16 -0800313 /**
John Reck35e9dd62011-04-25 09:01:54 -0700314 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800315 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800316 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700317 settings.setGeolocationEnabled(enableGeolocation());
318 settings.setJavaScriptEnabled(enableJavascript());
319 settings.setLightTouchEnabled(enableLightTouch());
320 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700321 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
John Reck35e9dd62011-04-25 09:01:54 -0700322 settings.setMinimumFontSize(getMinimumFontSize());
323 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck7dc444b2011-06-16 17:44:29 -0700324 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700325 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100326 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700327 settings.setLoadsImagesAutomatically(loadImages());
328 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
329 settings.setSavePassword(rememberPasswords());
330 settings.setSaveFormData(saveFormdata());
331 settings.setUseWideViewPort(isWideViewport());
Panos Thomasb10bbda2014-06-23 10:18:36 -0700332 settings.setDoNotTrack(doNotTrack());
Yida Wang078425c2015-06-02 14:14:06 -0400333 settings.setNightModeEnabled(isNightModeEnabled());
Tarun Nainani8eb00912014-07-17 12:28:32 -0700334 settings.setMediaPlaybackRequiresUserGesture(false);
John Reck2fd9d0e2011-07-15 11:13:48 -0700335
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800336 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800337 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
338 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
339 settingsClassic.setForceUserScalable(forceEnableUserScalable());
340 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
341 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800342
John Reckea17e782011-10-27 17:15:59 -0700343 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800344 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700345 useInverted ? "true" : "false");
346 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800347 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700348 Float.toString(getInvertedContrast()));
349 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700350
John Reckea17e782011-10-27 17:15:59 -0700351 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800352 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700353 enableCpuUploadPath() ? "true" : "false");
354 }
Victoria Lease96497832012-03-23 14:19:56 -0700355
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800356 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000357 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800358
Tarun Nainani8eb00912014-07-17 12:28:32 -0700359
John Reck35e9dd62011-04-25 09:01:54 -0700360 /**
361 * Syncs all the settings that have no UI
362 * These cannot change, so we only need to set them once per WebSettings
363 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800364 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700365 settings.setDefaultFontSize(16);
366 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000367
John Reck35e9dd62011-04-25 09:01:54 -0700368 // WebView inside Browser doesn't want initial focus to be set.
369 settings.setNeedInitialFocus(false);
370 // Browser supports multiple windows
371 settings.setSupportMultipleWindows(true);
372 // enable smooth transition for better performance during panning or
373 // zooming
374 settings.setEnableSmoothTransition(true);
375 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700376 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700377
378 // HTML5 API flags
379 settings.setAppCacheEnabled(true);
380 settings.setDatabaseEnabled(true);
381 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700382
383 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700384 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700385 settings.setAppCachePath(getAppCachePath());
386 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
387 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700388 // origin policy for file access
389 settings.setAllowUniversalAccessFromFileURLs(false);
390 settings.setAllowFileAccessFromFileURLs(false);
Sudheer Koganti24766882014-10-02 10:58:09 -0700391 settings.setFullscreenSupported(true);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800392
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800393 //if (!(settings instanceof WebSettingsClassic)) return;
394 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800395
396 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
397 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
398 // WebView should be preserving the memory as much as possible.
399 // However, apps like browser wish to turn on the performance mode which
400 // would require more memory.
401 // TODO: We need to dynamically allocate/deallocate temporary memory for
402 // apps which are trying to use minimal memory. Currently, double
403 // buffering is always turned on, which is unnecessary.
404 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
405 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800406 */
John Reck35e9dd62011-04-25 09:01:54 -0700407 }
408
409 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700410 mNeedsSharedSync = false;
Axesh R. Ajmera579c70c2014-04-17 13:24:56 -0700411 CookieManager.getInstance().setAcceptCookie(acceptCookies());
Vivek Sekhared791da2015-02-22 12:39:05 -0800412
John Reck35e9dd62011-04-25 09:01:54 -0700413 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700414
John Reck35e9dd62011-04-25 09:01:54 -0700415 private void syncManagedSettings() {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700416 if (!mEngineInitialized) {
417 mSyncManagedSettings = true;
418 return;
419 }
420 mSyncManagedSettings = false;
John Reck35e9dd62011-04-25 09:01:54 -0700421 syncSharedSettings();
422 synchronized (mManagedSettings) {
423 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
424 while (iter.hasNext()) {
425 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800426 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700427 if (settings == null) {
428 iter.remove();
429 continue;
430 }
431 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000432 }
John Reck35e9dd62011-04-25 09:01:54 -0700433 }
434 }
Ben Murdochef671652010-11-25 17:17:58 +0000435
John Reck35e9dd62011-04-25 09:01:54 -0700436 @Override
437 public void onSharedPreferenceChanged(
438 SharedPreferences sharedPreferences, String key) {
439 syncManagedSettings();
440 if (PREF_SEARCH_ENGINE.equals(key)) {
441 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700442 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200443 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700444 mController.getUi().setFullscreen(useFullscreen());
445 }
Victoria Lease96497832012-03-23 14:19:56 -0700446 } else if (PREF_LINK_PREFETCH.equals(key)) {
447 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700448 }
John Reck35e9dd62011-04-25 09:01:54 -0700449 }
450
John Reck961d35d2011-06-23 09:45:54 -0700451 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100452 requireInitialization();
453 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700454 }
455
456 public LayoutAlgorithm getLayoutAlgorithm() {
457 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
458 if (autofitPages()) {
Tarun Nainani003bec52014-07-02 02:20:34 -0700459 layoutAlgorithm = LayoutAlgorithm.TEXT_AUTOSIZING;
John Reck35e9dd62011-04-25 09:01:54 -0700460 }
461 if (isDebugEnabled()) {
462 if (isSmallScreen()) {
463 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000464 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700465 if (isNormalLayout()) {
466 layoutAlgorithm = LayoutAlgorithm.NORMAL;
467 } else {
468 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
469 }
Ben Murdochef671652010-11-25 17:17:58 +0000470 }
John Reck35e9dd62011-04-25 09:01:54 -0700471 }
472 return layoutAlgorithm;
473 }
Ben Murdochef671652010-11-25 17:17:58 +0000474
John Reck35e9dd62011-04-25 09:01:54 -0700475 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700476 requireInitialization();
477 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800478 }
479
John Reck35e9dd62011-04-25 09:01:54 -0700480 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700481 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700482 return mWebStorageSizeManager;
483 }
484
John Reck35e9dd62011-04-25 09:01:54 -0700485 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700486 if (mAppCachePath == null) {
487 mAppCachePath = mContext.getDir("appcache", 0).getPath();
488 }
489 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700490 }
491
492 private void updateSearchEngine(boolean force) {
493 String searchEngineName = getSearchEngineName();
494 if (force || mSearchEngine == null ||
495 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700496 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000497 }
498 }
499
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100500 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700501 if (mSearchEngine == null) {
502 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100503 }
John Reck35e9dd62011-04-25 09:01:54 -0700504 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100505 }
506
John Reck35e9dd62011-04-25 09:01:54 -0700507 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700508 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700509 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100510 }
511
John Reck35e9dd62011-04-25 09:01:54 -0700512 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700513 Editor edit = mPrefs.edit();
514 edit.putBoolean(PREF_DEBUG_MENU, value);
515 if (!value) {
516 // Reset to "safe" value
517 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
518 }
519 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100520 }
521
John Reck35e9dd62011-04-25 09:01:54 -0700522 public void clearCache() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700523 if (mController != null) {
524 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800525 if (current != null) {
526 current.clearCache(true);
527 }
528 }
529 }
530
John Reck35e9dd62011-04-25 09:01:54 -0700531 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800532 CookieManager.getInstance().removeAllCookie();
533 }
534
John Reck35e9dd62011-04-25 09:01:54 -0700535 public void clearHistory() {
536 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800537 Browser.clearHistory(resolver);
538 Browser.clearSearches(resolver);
539 }
540
John Reck35e9dd62011-04-25 09:01:54 -0700541 public void clearFormData() {
542 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700543 if (mController!= null) {
544 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100545 if (currentTopView != null) {
546 currentTopView.clearFormData();
547 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800548 }
549 }
550
Bijan Amirzada3bbb3a42014-06-17 12:06:18 -0700551 public WebView getTopWebView(){
552 if (mController!= null)
553 return mController.getCurrentTopWebView();
554
555 return null;
556 }
557
John Reck35e9dd62011-04-25 09:01:54 -0700558 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700559 // Clear password store maintained by SWE engine
560 WebSettings settings = null;
561 // find a valid settings object
562 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
563 while (iter.hasNext()) {
564 WeakReference<WebSettings> ref = iter.next();
565 settings = (WebSettings)ref.get();
566 if (settings != null) {
567 break;
568 }
569 }
570 if (settings != null) {
Panos Thomasdbaea4e2014-05-22 06:48:47 -0700571 settings.clearPasswords();
Panos Thomasfa948b82014-03-09 03:42:42 -0700572 }
Vivek Sekhare8a8ec22014-07-10 14:52:43 -0700573
574 // Clear passwords in WebView database
575 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
576 db.clearHttpAuthUsernamePassword();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800577 }
578
John Reck35e9dd62011-04-25 09:01:54 -0700579 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100580 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100581 }
582
John Reck35e9dd62011-04-25 09:01:54 -0700583 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100584 GeolocationPermissions.getInstance().clearAll();
Panos Thomasb298aad2014-10-22 12:24:21 -0700585 if (GeolocationPermissions.isIncognitoCreated()) {
586 GeolocationPermissions.getIncognitoInstance().clearAll();
587 }
Nicolas Roard78a98e42009-05-11 13:34:17 +0100588 }
589
John Reck35e9dd62011-04-25 09:01:54 -0700590 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700591 mPrefs.edit()
592 .clear()
John Reckbd315192011-07-29 10:05:47 -0700593 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200594 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700595 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700596 }
597
Björn Isakssonc885a242012-06-05 17:19:04 +0200598 private void resetCachedValues() {
599 updateSearchEngine(false);
600 }
601
John Reck35e9dd62011-04-25 09:01:54 -0700602 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800603 // query the profile from components autofill database 524
604 if (mAutofillHandler.mAutoFillProfile == null &&
605 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
606 WebSettings settings = null;
607 // find a valid settings object
608 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
609 while (iter.hasNext()) {
610 WeakReference<WebSettings> ref = iter.next();
611 settings = (WebSettings)ref.get();
612 if (settings != null) {
613 break;
614 }
615 }
616 if (settings != null) {
617 AutoFillProfile profile =
618 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
619 mAutofillHandler.setAutoFillProfile(profile);
620 }
621 }
John Reck35e9dd62011-04-25 09:01:54 -0700622 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800623 }
624
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800625 public String getAutoFillProfileId() {
626 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800627 }
628
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800629 public void updateAutoFillProfile(AutoFillProfile profile) {
630 syncAutoFillProfile(profile);
631 }
632
633 private void syncAutoFillProfile(AutoFillProfile profile) {
634 synchronized (mManagedSettings) {
635 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
636 while (iter.hasNext()) {
637 WeakReference<WebSettings> ref = iter.next();
638 WebSettings settings = (WebSettings)ref.get();
639 if (settings == null) {
640 iter.remove();
641 continue;
642 }
643 // update the profile only once.
644 settings.setAutoFillProfile(profile);
645 // Now we should have the guid
646 mAutofillHandler.setAutoFillProfile(profile);
647 break;
648 }
649 }
650 }
John Reck35e9dd62011-04-25 09:01:54 -0700651 public void toggleDebugSettings() {
652 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800653 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100654
John Reckb8b2af82011-05-20 15:58:33 -0700655 public boolean hasDesktopUseragent(WebView view) {
Vivek Sekhar7b3d2da2014-11-13 16:20:11 -0800656 return view != null && view.getUseDesktopUserAgent();
John Reckb8b2af82011-05-20 15:58:33 -0700657 }
658
659 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700660 if (view == null) {
661 return;
662 }
Vivek Sekhar7b3d2da2014-11-13 16:20:11 -0800663 if (hasDesktopUseragent(view))
664 view.setUseDesktopUserAgent(false, true);
665 else
666 view.setUseDesktopUserAgent(true, true);
John Reckb8b2af82011-05-20 15:58:33 -0700667 }
668
John Reck7dc444b2011-06-16 17:44:29 -0700669 public static int getAdjustedMinimumFontSize(int rawValue) {
670 rawValue++; // Preference starts at 0, min font at 1
671 if (rawValue > 1) {
672 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
673 }
674 return rawValue;
675 }
676
John Reck5ba3c762011-09-14 15:00:15 -0700677 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700678 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700679 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700680 }
681
682 static int getRawTextZoom(int percent) {
683 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
684 }
685
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700686 public int getAdjustedDoubleTapZoom(int rawValue) {
687 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
688 return (int) ((rawValue + 100) * mFontSizeMult);
689 }
690
691 static int getRawDoubleTapZoom(int percent) {
692 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
693 }
694
John Reckcadae722011-07-25 11:36:17 -0700695 public SharedPreferences getPreferences() {
696 return mPrefs;
697 }
698
Victoria Lease96497832012-03-23 14:19:56 -0700699 // update connectivity-dependent options
700 public void updateConnectionType() {
701 ConnectivityManager cm = (ConnectivityManager)
702 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
703 String linkPrefetchPreference = getLinkPrefetchEnabled();
704 boolean linkPrefetchAllowed = linkPrefetchPreference.
705 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
706 NetworkInfo ni = cm.getActiveNetworkInfo();
707 if (ni != null) {
708 switch (ni.getType()) {
709 case ConnectivityManager.TYPE_WIFI:
710 case ConnectivityManager.TYPE_ETHERNET:
711 case ConnectivityManager.TYPE_BLUETOOTH:
712 linkPrefetchAllowed |= linkPrefetchPreference.
713 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
714 break;
715 case ConnectivityManager.TYPE_MOBILE:
716 case ConnectivityManager.TYPE_MOBILE_DUN:
717 case ConnectivityManager.TYPE_MOBILE_MMS:
718 case ConnectivityManager.TYPE_MOBILE_SUPL:
719 case ConnectivityManager.TYPE_WIMAX:
720 default:
721 break;
722 }
723 }
724 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
725 mLinkPrefetchAllowed = linkPrefetchAllowed;
726 syncManagedSettings();
727 }
728 }
729
luxiaol62677b02013-07-22 07:54:49 +0800730 public String getDownloadPath() {
731 return mPrefs.getString(PREF_DOWNLOAD_PATH,
732 DownloadHandler.getDefaultDownloadPath(mContext));
733 }
John Reck35e9dd62011-04-25 09:01:54 -0700734 // -----------------------------
735 // getter/setters for accessibility_preferences.xml
736 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100737
John Reck7dc444b2011-06-16 17:44:29 -0700738 @Deprecated
739 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700740 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
741 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100742 }
743
John Reck35e9dd62011-04-25 09:01:54 -0700744 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700745 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700746 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100747 }
748
John Reck92f25f82011-04-26 16:57:10 -0700749 public boolean forceEnableUserScalable() {
750 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
751 }
752
John Reck7dc444b2011-06-16 17:44:29 -0700753 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700754 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700755 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
756 return getAdjustedTextZoom(textZoom);
757 }
758
759 public void setTextZoom(int percent) {
760 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
761 }
762
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700763 public int getDoubleTapZoom() {
764 requireInitialization();
765 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
766 return getAdjustedDoubleTapZoom(doubleTapZoom);
767 }
768
769 public void setDoubleTapZoom(int percent) {
770 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
771 }
772
John Reck35e9dd62011-04-25 09:01:54 -0700773 // -----------------------------
774 // getter/setters for advanced_preferences.xml
775 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100776
John Reck35e9dd62011-04-25 09:01:54 -0700777 public String getSearchEngineName() {
Panos Thomas64c77e02014-12-17 22:15:23 -0800778 // The following is a NOP if the SEARCH_ENGINE restriction has already been created. Otherwise,
779 // it creates the restriction and if enabled it sets the <default_search_engine_value>.
780 SearchEngineRestriction.getInstance();
781
luxiaol221b3932013-07-19 15:27:57 +0800782 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
783 if (defaultSearchEngineValue == null) {
784 defaultSearchEngineValue = SearchEngine.GOOGLE;
785 }
786 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100787 }
John Reck63bb6872010-12-01 19:29:32 -0800788
Michael Kolb8d772b02012-01-19 13:56:00 -0800789 public boolean allowAppTabs() {
790 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
791 }
792
John Reck35e9dd62011-04-25 09:01:54 -0700793 public boolean openInBackground() {
794 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800795 }
John Reck35e9dd62011-04-25 09:01:54 -0700796
797 public boolean enableJavascript() {
798 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
799 }
800
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800801 public boolean enableMemoryMonitor() {
802 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
803 }
804
Tarun Nainani8eb00912014-07-17 12:28:32 -0700805
John Reck35e9dd62011-04-25 09:01:54 -0700806 public boolean loadPageInOverviewMode() {
807 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
808 }
809
810 public boolean autofitPages() {
811 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
812 }
813
814 public boolean blockPopupWindows() {
Pankaj Garg11015212015-07-06 14:38:43 -0700815 return !PermissionsServiceFactory.getDefaultPermissions(
816 PermissionsServiceFactory.PermissionType.POPUP);
John Reck35e9dd62011-04-25 09:01:54 -0700817 }
818
819 public boolean loadImages() {
820 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
821 }
822
823 public String getDefaultTextEncoding() {
Tarun Nainani88381422015-04-02 19:47:21 -0700824 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, "auto");
John Reck35e9dd62011-04-25 09:01:54 -0700825 }
826
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700827 public String getEdgeSwipeAction() {
828 return mPrefs.getString(PREF_EDGE_SWIPE,
829 mContext.getResources().getString(R.string.value_unknown_edge_swipe));
830 }
831
832 public void setEdgeSwipeTemporal() {
833 mPrefs.edit().putString(PREF_EDGE_SWIPE,
834 mContext.getResources().getString(R.string.value_temporal_edge_swipe)).apply();
835 }
836
837 public void setEdgeSwipeSpatial() {
838 mPrefs.edit().putString(PREF_EDGE_SWIPE,
839 mContext.getResources().getString(R.string.value_spatial_edge_swipe)).apply();
840 }
841
842 public void setEdgeSwipeDisabled() {
843 mPrefs.edit().putString(PREF_EDGE_SWIPE,
844 mContext.getResources().getString(R.string.value_disable_edge_swipe)).apply();
845 }
846
John Reck35e9dd62011-04-25 09:01:54 -0700847 // -----------------------------
848 // getter/setters for general_preferences.xml
849 // -----------------------------
850
851 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700852 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
853 }
854
855 public void setHomePage(String value) {
856 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
857 }
858
859 public boolean isAutofillEnabled() {
860 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
861 }
862
863 public void setAutofillEnabled(boolean value) {
864 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
865 }
866
Site Maoabb7bd32015-03-20 15:34:02 -0700867 public boolean isPowerSaveModeEnabled() {
868 return mPrefs.getBoolean(PREF_POWERSAVE_ENABLED, false);
869 }
870
871 public void setPowerSaveModeEnabled(boolean value) {
872 mPrefs.edit().putBoolean(PREF_POWERSAVE_ENABLED, value).apply();
873 }
874
Yida Wang078425c2015-06-02 14:14:06 -0400875 public boolean isNightModeEnabled() {
876 return mPrefs.getBoolean(PREF_NIGHTMODE_ENABLED, false);
877 }
878
879 public void setNightModeEnabled(boolean value) {
880 mPrefs.edit().putBoolean(PREF_NIGHTMODE_ENABLED, value).apply();
881 }
882
John Reck35e9dd62011-04-25 09:01:54 -0700883 // -----------------------------
884 // getter/setters for debug_preferences.xml
885 // -----------------------------
886
887 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700888 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700889 return true;
890 }
891 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
892 }
893
Derek Sollenberger31adf672011-07-08 11:31:30 -0400894 public boolean isSkiaHardwareAccelerated() {
895 if (!isDebugEnabled()) {
896 return false;
897 }
898 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
899 }
900
John Reck35e9dd62011-04-25 09:01:54 -0700901 // -----------------------------
902 // getter/setters for hidden_debug_preferences.xml
903 // -----------------------------
904
905 public boolean enableVisualIndicator() {
906 if (!isDebugEnabled()) {
907 return false;
908 }
909 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
910 }
911
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700912 public boolean enableCpuUploadPath() {
913 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700914 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700915 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700916 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700917 }
918
John Reck35e9dd62011-04-25 09:01:54 -0700919 public boolean isSmallScreen() {
920 if (!isDebugEnabled()) {
921 return false;
922 }
923 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
924 }
925
926 public boolean isWideViewport() {
927 if (!isDebugEnabled()) {
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800928 return true;
John Reck35e9dd62011-04-25 09:01:54 -0700929 }
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800930 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
John Reck35e9dd62011-04-25 09:01:54 -0700931 }
932
933 public boolean isNormalLayout() {
934 if (!isDebugEnabled()) {
935 return false;
936 }
937 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
938 }
939
940 public boolean isTracing() {
941 if (!isDebugEnabled()) {
942 return false;
943 }
944 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
945 }
946
947 public boolean enableLightTouch() {
948 if (!isDebugEnabled()) {
949 return false;
950 }
951 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
952 }
953
954 public boolean enableNavDump() {
955 if (!isDebugEnabled()) {
956 return false;
957 }
958 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
959 }
960
961 public String getJsEngineFlags() {
962 if (!isDebugEnabled()) {
963 return "";
964 }
965 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
966 }
967
968 // -----------------------------
969 // getter/setters for lab_preferences.xml
970 // -----------------------------
971
John Reck35e9dd62011-04-25 09:01:54 -0700972 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700973 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700974 }
975
Michael Kolbc38c6042011-04-27 10:46:06 -0700976 public boolean useFullscreen() {
977 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
978 }
979
John Reck2fd9d0e2011-07-15 11:13:48 -0700980 public boolean useInvertedRendering() {
981 return mPrefs.getBoolean(PREF_INVERTED, false);
982 }
983
Nicolas Roard5d513102011-08-03 15:35:34 -0700984 public float getInvertedContrast() {
985 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
986 }
987
John Reck35e9dd62011-04-25 09:01:54 -0700988 // -----------------------------
989 // getter/setters for privacy_security_preferences.xml
990 // -----------------------------
991
992 public boolean showSecurityWarnings() {
993 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
994 }
995
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700996 public boolean doNotTrack() {
Dave Tharpb487d5d2015-05-15 15:54:08 -0700997 boolean dntVal;
998 if (DoNotTrackRestriction.getInstance().isEnabled()) {
999 dntVal = DoNotTrackRestriction.getInstance().getValue();
1000 }
1001 else {
1002 dntVal = mPrefs.getBoolean(PREF_DO_NOT_TRACK, true);
1003 }
1004 return dntVal;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001005 }
1006
John Reck35e9dd62011-04-25 09:01:54 -07001007 public boolean acceptCookies() {
Pankaj Garg32e1b942015-06-03 18:13:24 -07001008 return PermissionsServiceFactory.getDefaultPermissions(
1009 PermissionsServiceFactory.PermissionType.COOKIE);
John Reck35e9dd62011-04-25 09:01:54 -07001010 }
1011
1012 public boolean saveFormdata() {
1013 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
1014 }
1015
1016 public boolean enableGeolocation() {
1017 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
1018 }
1019
1020 public boolean rememberPasswords() {
1021 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
1022 }
1023
Michael Kolb14612442011-06-24 13:06:29 -07001024 // -----------------------------
1025 // getter/setters for bandwidth_preferences.xml
1026 // -----------------------------
1027
Mathew Inwood467813f2011-09-02 15:43:17 +01001028 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
1029 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -07001030 }
Mathew Inwood467813f2011-09-02 15:43:17 +01001031
1032 public static String getPreloadAlwaysPreferenceString(Context context) {
1033 return context.getResources().getString(R.string.pref_data_preload_value_always);
1034 }
1035
Mathew Inwood825fba72011-10-04 14:52:52 +01001036 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1037 "browser_default_preload_setting";
1038
1039 public String getDefaultPreloadSetting() {
1040 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1041 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1042 if (preload == null) {
1043 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1044 }
1045 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001046 }
1047
1048 public String getPreloadEnabled() {
1049 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1050 }
1051
Victoria Lease96497832012-03-23 14:19:56 -07001052 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1053 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1054 }
1055
1056 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1057 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1058 }
1059
1060 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1061 "browser_default_link_prefetch_setting";
1062
1063 public String getDefaultLinkPrefetchSetting() {
1064 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1065 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1066 if (preload == null) {
1067 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1068 }
1069 return preload;
1070 }
1071
1072 public String getLinkPrefetchEnabled() {
1073 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1074 }
1075
George Mount3636d0a2011-11-21 09:08:21 -08001076 // -----------------------------
1077 // getter/setters for browser recovery
1078 // -----------------------------
1079 /**
1080 * The last time browser was started.
1081 * @return The last browser start time as System.currentTimeMillis. This
1082 * can be 0 if this is the first time or the last tab was closed.
1083 */
1084 public long getLastRecovered() {
1085 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1086 }
1087
1088 /**
1089 * Sets the last browser start time.
1090 * @param time The last time as System.currentTimeMillis that the browser
1091 * was started. This should be set to 0 if the last tab is closed.
1092 */
1093 public void setLastRecovered(long time) {
1094 mPrefs.edit()
1095 .putLong(KEY_LAST_RECOVERED, time)
1096 .apply();
1097 }
1098
1099 /**
1100 * Used to determine whether or not the previous browser run crashed. Once
1101 * the previous state has been determined, the value will be set to false
1102 * until a pause is received.
1103 * @return true if the last browser run was paused or false if it crashed.
1104 */
1105 public boolean wasLastRunPaused() {
1106 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1107 }
1108
1109 /**
1110 * Sets whether or not the last run was a pause or crash.
1111 * @param isPaused Set to true When a pause is received or false after
1112 * resuming.
1113 */
1114 public void setLastRunPaused(boolean isPaused) {
1115 mPrefs.edit()
1116 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1117 .apply();
1118 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001119}