blob: cde500c6beac35c45b1f426671bfb74a954bccd5 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
John Reck35e9dd62011-04-25 09:01:54 -07002 * Copyright (C) 2011 The Android Open Source Project
The Android Open Source Project0c908882009-03-03 19:32:16 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080018
The Android Open Source Project0c908882009-03-03 19:32:16 -080019import android.content.ContentResolver;
20import android.content.Context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.content.SharedPreferences;
John Reck1da81882011-10-04 17:21:10 -070022import android.content.SharedPreferences.Editor;
John Reck812d2d62011-01-18 14:16:15 -080023import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Victoria Lease96497832012-03-23 14:19:56 -070024import android.net.ConnectivityManager;
25import android.net.NetworkInfo;
John Reckf48314f2011-04-27 17:52:17 -070026import android.os.Build;
Ben Murdoch0cb81892010-10-08 12:41:33 +010027import android.preference.PreferenceManager;
Mathew Inwood825fba72011-10-04 14:52:52 +010028import android.provider.Settings;
John Reck5ba3c762011-09-14 15:00:15 -070029import android.util.DisplayMetrics;
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -070030import android.webkit.ValueCallback;
Nicolas Roard78a98e42009-05-11 13:34:17 +010031import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032
Bijan Amirzada41242f22014-03-21 12:12:18 -070033import com.android.browser.homepages.HomeProvider;
Dave Tharp4a2c8942015-07-10 14:10:51 -070034import com.android.browser.mdm.AutoFillRestriction;
35import com.android.browser.mdm.DevToolsRestriction;
Dave Tharpb487d5d2015-05-15 15:54:08 -070036import com.android.browser.mdm.DoNotTrackRestriction;
Dave Tharp13f3f7c2015-07-16 14:58:51 -070037import com.android.browser.mdm.DownloadDirRestriction;
Dave Tharp4a2c8942015-07-10 14:10:51 -070038import com.android.browser.mdm.EditBookmarksRestriction;
39import com.android.browser.mdm.IncognitoRestriction;
40import com.android.browser.mdm.ManagedBookmarksRestriction;
Panos Thomasa9ff2c72014-12-17 22:15:23 -080041import com.android.browser.mdm.ProxyRestriction;
Panos Thomas64c77e02014-12-17 22:15:23 -080042import com.android.browser.mdm.SearchEngineRestriction;
Dave Tharp4a2c8942015-07-10 14:10:51 -070043import com.android.browser.mdm.ThirdPartyCookiesRestriction;
44import com.android.browser.mdm.URLFilterRestriction;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070045import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070046import com.android.browser.provider.BrowserProvider;
Bijan Amirzada41242f22014-03-21 12:12:18 -070047import com.android.browser.search.SearchEngine;
48import com.android.browser.search.SearchEngines;
John Reck46500332011-06-07 14:36:10 -070049
John Reck35e9dd62011-04-25 09:01:54 -070050import java.lang.ref.WeakReference;
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -070051import java.util.ArrayList;
John Reck35e9dd62011-04-25 09:01:54 -070052import java.util.Iterator;
53import java.util.LinkedList;
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -070054import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -080055
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080056import org.codeaurora.swe.AutoFillProfile;
57import org.codeaurora.swe.CookieManager;
58import org.codeaurora.swe.GeolocationPermissions;
Pankaj Garg32e1b942015-06-03 18:13:24 -070059import org.codeaurora.swe.PermissionsServiceFactory;
Kulanthaivel Palanichamyf36e1db2015-04-08 16:11:06 -070060import org.codeaurora.swe.WebRefiner;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080061import org.codeaurora.swe.WebSettings.LayoutAlgorithm;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080062import org.codeaurora.swe.WebSettings.TextSize;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080063import org.codeaurora.swe.WebSettings;
64import org.codeaurora.swe.WebView;
Vivek Sekhare8a8ec22014-07-10 14:52:43 -070065import org.codeaurora.swe.WebViewDatabase;
Panos Thomas3f35d8d2014-11-08 22:40:23 -080066
John Reck35e9dd62011-04-25 09:01:54 -070067/**
68 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080069 */
John Reck35e9dd62011-04-25 09:01:54 -070070public class BrowserSettings implements OnSharedPreferenceChangeListener,
71 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010072
qqzhoue6ff8b42013-07-23 17:28:48 +080073 private static final String TAG = "BrowserSettings";
John Reck8fc22a12011-06-16 14:57:41 -070074 // The minimum min font size
75 // Aka, the lower bounds for the min font size range
76 // which is 1:5..24
77 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070078 // The initial value in the text zoom range
79 // This is what represents 100% in the SeekBarPreference range
80 private static final int TEXT_ZOOM_START_VAL = 10;
81 // The size of a single step in the text zoom range, in percent
82 private static final int TEXT_ZOOM_STEP = 5;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -070083 // The initial value in the double tap zoom range
84 // This is what represents 100% in the SeekBarPreference range
85 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
86 // The size of a single step in the double tap zoom range, in percent
87 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -070088
John Reck35e9dd62011-04-25 09:01:54 -070089 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -070090
John Reck35e9dd62011-04-25 09:01:54 -070091 private Context mContext;
92 private SharedPreferences mPrefs;
93 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -070094 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -070095 private WebStorageSizeManager mWebStorageSizeManager;
96 private AutofillHandler mAutofillHandler;
Ben Murdochaaa1f372011-07-25 15:40:58 +010097 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -070098 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -070099 private float mFontSizeMult = 1.0f;
John Reck78a6a1d2011-07-21 13:54:03 -0700100
Victoria Lease96497832012-03-23 14:19:56 -0700101 // Current state of network-dependent settings
102 private boolean mLinkPrefetchAllowed = true;
103
John Reck78a6a1d2011-07-21 13:54:03 -0700104 // Cached values
105 private int mPageCacheCapacity = 1;
106 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800107
John Reck35e9dd62011-04-25 09:01:54 -0700108 // Cached settings
109 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110
Ben Murdochaaa1f372011-07-25 15:40:58 +0100111 private static String sFactoryResetUrl;
112
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700113 private boolean mEngineInitialized = false;
114 private boolean mSyncManagedSettings = false;
115
Bijan Amirzada3bb46302014-06-17 16:31:31 -0700116 public static synchronized void initialize(final Context context) {
117 if (sInstance == null)
118 sInstance = new BrowserSettings(context);
John Reck35e9dd62011-04-25 09:01:54 -0700119 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800120
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700121 public static BrowserSettings getInstance() {
John Reck35e9dd62011-04-25 09:01:54 -0700122 return sInstance;
123 }
Ben Murdochef671652010-11-25 17:17:58 +0000124
John Reck35e9dd62011-04-25 09:01:54 -0700125 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100126 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700127 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
John Reck35e9dd62011-04-25 09:01:54 -0700128 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckcadae722011-07-25 11:36:17 -0700129 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700130 }
131
132 public void setController(Controller controller) {
133 mController = controller;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700134 mNeedsSharedSync = true;
135 }
136
137 public void onEngineInitializationComplete() {
138 mEngineInitialized = true;
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700139
140 // Intialize Web Refiner only once
141 final WebRefiner refiner = WebRefiner.getInstance();
142 if (refiner != null) {
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700143 mPrefs.edit().putBoolean(PREF_WEB_REFINER, true).apply();
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700144 refiner.setDefaultPermission(PermissionsServiceFactory.getDefaultPermissions(
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700145 PermissionsServiceFactory.PermissionType.WEBREFINER));
146
147 PermissionsServiceFactory.getPermissionsService(
148 new ValueCallback<PermissionsServiceFactory.PermissionsService>() {
149 @Override
150 public void onReceiveValue(
151 PermissionsServiceFactory.PermissionsService value) {
152 Set<String> origins = value.getOrigins();
153 ArrayList<String> allowList = new ArrayList<>();
154 ArrayList<String> blockList = new ArrayList<>();
155 for (String origin : origins) {
156 PermissionsServiceFactory.PermissionsService.OriginInfo
157 info = value.getOriginInfo(origin);
Axesh R. Ajmera1ad7c572015-07-13 17:10:56 -0700158 if (info == null) {
159 continue;
160 }
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700161 int perm = info.getPermission(
162 PermissionsServiceFactory.PermissionType.WEBREFINER);
163 if (perm == PermissionsServiceFactory.Permission.ALLOW) {
164 allowList.add(origin);
165 } else if (perm == PermissionsServiceFactory.Permission.BLOCK) {
166 blockList.add(origin);
167 }
168 }
169 if (!allowList.isEmpty()) {
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700170 refiner.setPermissionForOrigins(
171 allowList.toArray(new String[allowList.size()]), true);
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700172 }
173
174 if (!blockList.isEmpty()) {
Kulanthaivel Palanichamy4101f272015-07-09 19:52:55 -0700175 refiner.setPermissionForOrigins(
176 blockList.toArray(new String[blockList.size()]), false);
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700177 }
178 }
179 }
180 );
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700181 } else {
182 mPrefs.edit().putBoolean(PREF_WEB_REFINER, false).apply();
Axesh R. Ajmerab7b86b92015-06-25 20:25:24 -0700183 }
184
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700185 mAutofillHandler = new AutofillHandler(mContext);
186 if (mSyncManagedSettings) {
187 syncManagedSettings();
188 }
189 if (mNeedsSharedSync) {
John Reckc477e712011-08-17 11:27:15 -0700190 syncSharedSettings();
191 }
Dave Tharp4a2c8942015-07-10 14:10:51 -0700192
193 // Instantiate all MDM Restriction Singletons.
194 AutoFillRestriction.getInstance();
195 DevToolsRestriction.getInstance();
196 DoNotTrackRestriction.getInstance();
Dave Tharp13f3f7c2015-07-16 14:58:51 -0700197 DownloadDirRestriction.getInstance();
Dave Tharp4a2c8942015-07-10 14:10:51 -0700198 EditBookmarksRestriction.getInstance();
199 IncognitoRestriction.getInstance();
200 ManagedBookmarksRestriction.getInstance();
Panos Thomasa9ff2c72014-12-17 22:15:23 -0800201 ProxyRestriction.getInstance();
Dave Tharp4a2c8942015-07-10 14:10:51 -0700202 SearchEngineRestriction.getInstance();
203 ThirdPartyCookiesRestriction.getInstance();
204 URLFilterRestriction.getInstance();
Ben Murdochef671652010-11-25 17:17:58 +0000205 }
206
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800207 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800208
John Reckc477e712011-08-17 11:27:15 -0700209 if (mNeedsSharedSync) {
210 syncSharedSettings();
211 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800212
John Reck35e9dd62011-04-25 09:01:54 -0700213 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800214 syncStaticSettings(settings);
215 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700216 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800217 }
218 }
219
John Reckd1d87312012-03-08 13:25:00 -0800220 public void stopManagingSettings(WebSettings settings) {
221 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
222 while (iter.hasNext()) {
223 WeakReference<WebSettings> ref = iter.next();
224 if (ref.get() == settings) {
225 iter.remove();
226 return;
227 }
228 }
229 }
230
John Reckcadae722011-07-25 11:36:17 -0700231 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700232
233 @Override
234 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700235 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
236 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700237 // the cost of one cached page is ~3M (measured using nytimes.com). For
238 // low end devices, we only cache one page. For high end devices, we try
239 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800240
241 // SWE_TODO : assume a high-memory device
242 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700243 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800244 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700245 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
246 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
247 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700248 // Workaround b/5254577
249 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700250 if (Build.VERSION.CODENAME.equals("REL")) {
251 // This is a release build, always startup with debug disabled
252 setDebugEnabled(false);
253 }
254 if (mPrefs.contains(PREF_TEXT_SIZE)) {
255 /*
256 * Update from TextSize enum to zoom percent
257 * SMALLEST is 50%
258 * SMALLER is 75%
259 * NORMAL is 100%
260 * LARGER is 150%
261 * LARGEST is 200%
262 */
263 switch (getTextSize()) {
264 case SMALLEST:
265 setTextZoom(50);
266 break;
267 case SMALLER:
268 setTextZoom(75);
269 break;
270 case LARGER:
271 setTextZoom(150);
272 break;
273 case LARGEST:
274 setTextZoom(200);
275 break;
276 }
277 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
278 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100279
qqzhou8c5b0a32013-07-22 15:31:03 +0800280 // add for carrier homepage feature
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700281 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
qqzhou8c5b0a32013-07-22 15:31:03 +0800282
kaiyizbf086ea2013-08-02 11:03:58 +0800283 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
Tarun Nainani95b79682014-12-09 09:38:04 -0800284 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING, "auto").apply();
kaiyizbf086ea2013-08-02 11:03:58 +0800285 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700286
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700287 if (!mPrefs.contains(PREF_EDGE_SWIPE)) {
288 mPrefs.edit().putString(PREF_EDGE_SWIPE,
289 mContext.getResources().getString(
290 R.string.value_unknown_edge_swipe)).apply();
291 }
292
Ben Murdochaaa1f372011-07-25 15:40:58 +0100293 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
294 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
295 BrowserProvider.getClientId(mContext.getContentResolver()));
296 }
297
Ben Murdochaaa1f372011-07-25 15:40:58 +0100298 synchronized (BrowserSettings.class) {
299 sInitialized = true;
300 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700301 }
302 }
303 };
304
Ben Murdochaaa1f372011-07-25 15:40:58 +0100305 private static void requireInitialization() {
306 synchronized (BrowserSettings.class) {
307 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700308 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100309 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700310 } catch (InterruptedException e) {
311 }
312 }
313 }
314 }
315
The Android Open Source Project0c908882009-03-03 19:32:16 -0800316 /**
John Reck35e9dd62011-04-25 09:01:54 -0700317 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800318 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800319 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700320 settings.setGeolocationEnabled(enableGeolocation());
321 settings.setJavaScriptEnabled(enableJavascript());
322 settings.setLightTouchEnabled(enableLightTouch());
323 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700324 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
John Reck35e9dd62011-04-25 09:01:54 -0700325 settings.setMinimumFontSize(getMinimumFontSize());
326 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck7dc444b2011-06-16 17:44:29 -0700327 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700328 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100329 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700330 settings.setLoadsImagesAutomatically(loadImages());
331 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
332 settings.setSavePassword(rememberPasswords());
333 settings.setSaveFormData(saveFormdata());
334 settings.setUseWideViewPort(isWideViewport());
Panos Thomasb10bbda2014-06-23 10:18:36 -0700335 settings.setDoNotTrack(doNotTrack());
Yida Wang078425c2015-06-02 14:14:06 -0400336 settings.setNightModeEnabled(isNightModeEnabled());
Tarun Nainani8eb00912014-07-17 12:28:32 -0700337 settings.setMediaPlaybackRequiresUserGesture(false);
John Reck2fd9d0e2011-07-15 11:13:48 -0700338
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800339 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800340 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
341 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
342 settingsClassic.setForceUserScalable(forceEnableUserScalable());
343 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
344 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800345
John Reckea17e782011-10-27 17:15:59 -0700346 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800347 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700348 useInverted ? "true" : "false");
349 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800350 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700351 Float.toString(getInvertedContrast()));
352 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700353
John Reckea17e782011-10-27 17:15:59 -0700354 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800355 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700356 enableCpuUploadPath() ? "true" : "false");
357 }
Victoria Lease96497832012-03-23 14:19:56 -0700358
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800359 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000360 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800361
Tarun Nainani8eb00912014-07-17 12:28:32 -0700362
John Reck35e9dd62011-04-25 09:01:54 -0700363 /**
364 * Syncs all the settings that have no UI
365 * These cannot change, so we only need to set them once per WebSettings
366 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800367 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700368 settings.setDefaultFontSize(16);
369 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000370
John Reck35e9dd62011-04-25 09:01:54 -0700371 // WebView inside Browser doesn't want initial focus to be set.
372 settings.setNeedInitialFocus(false);
373 // Browser supports multiple windows
374 settings.setSupportMultipleWindows(true);
375 // enable smooth transition for better performance during panning or
376 // zooming
377 settings.setEnableSmoothTransition(true);
378 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700379 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700380
381 // HTML5 API flags
382 settings.setAppCacheEnabled(true);
383 settings.setDatabaseEnabled(true);
384 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700385
386 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700387 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700388 settings.setAppCachePath(getAppCachePath());
389 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
390 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700391 // origin policy for file access
392 settings.setAllowUniversalAccessFromFileURLs(false);
393 settings.setAllowFileAccessFromFileURLs(false);
Sudheer Koganti24766882014-10-02 10:58:09 -0700394 settings.setFullscreenSupported(true);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800395
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800396 //if (!(settings instanceof WebSettingsClassic)) return;
397 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800398
399 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
400 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
401 // WebView should be preserving the memory as much as possible.
402 // However, apps like browser wish to turn on the performance mode which
403 // would require more memory.
404 // TODO: We need to dynamically allocate/deallocate temporary memory for
405 // apps which are trying to use minimal memory. Currently, double
406 // buffering is always turned on, which is unnecessary.
407 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
408 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800409 */
John Reck35e9dd62011-04-25 09:01:54 -0700410 }
411
412 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700413 mNeedsSharedSync = false;
Axesh R. Ajmera579c70c2014-04-17 13:24:56 -0700414 CookieManager.getInstance().setAcceptCookie(acceptCookies());
Vivek Sekhared791da2015-02-22 12:39:05 -0800415
John Reck35e9dd62011-04-25 09:01:54 -0700416 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700417
John Reck35e9dd62011-04-25 09:01:54 -0700418 private void syncManagedSettings() {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700419 if (!mEngineInitialized) {
420 mSyncManagedSettings = true;
421 return;
422 }
423 mSyncManagedSettings = false;
John Reck35e9dd62011-04-25 09:01:54 -0700424 syncSharedSettings();
425 synchronized (mManagedSettings) {
426 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
427 while (iter.hasNext()) {
428 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800429 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700430 if (settings == null) {
431 iter.remove();
432 continue;
433 }
434 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000435 }
John Reck35e9dd62011-04-25 09:01:54 -0700436 }
437 }
Ben Murdochef671652010-11-25 17:17:58 +0000438
John Reck35e9dd62011-04-25 09:01:54 -0700439 @Override
440 public void onSharedPreferenceChanged(
441 SharedPreferences sharedPreferences, String key) {
442 syncManagedSettings();
443 if (PREF_SEARCH_ENGINE.equals(key)) {
444 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700445 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200446 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700447 mController.getUi().setFullscreen(useFullscreen());
448 }
Victoria Lease96497832012-03-23 14:19:56 -0700449 } else if (PREF_LINK_PREFETCH.equals(key)) {
450 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700451 }
John Reck35e9dd62011-04-25 09:01:54 -0700452 }
453
John Reck961d35d2011-06-23 09:45:54 -0700454 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100455 requireInitialization();
456 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700457 }
458
459 public LayoutAlgorithm getLayoutAlgorithm() {
460 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
461 if (autofitPages()) {
Tarun Nainani003bec52014-07-02 02:20:34 -0700462 layoutAlgorithm = LayoutAlgorithm.TEXT_AUTOSIZING;
John Reck35e9dd62011-04-25 09:01:54 -0700463 }
464 if (isDebugEnabled()) {
465 if (isSmallScreen()) {
466 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000467 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700468 if (isNormalLayout()) {
469 layoutAlgorithm = LayoutAlgorithm.NORMAL;
470 } else {
471 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
472 }
Ben Murdochef671652010-11-25 17:17:58 +0000473 }
John Reck35e9dd62011-04-25 09:01:54 -0700474 }
475 return layoutAlgorithm;
476 }
Ben Murdochef671652010-11-25 17:17:58 +0000477
John Reck35e9dd62011-04-25 09:01:54 -0700478 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700479 requireInitialization();
480 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800481 }
482
John Reck35e9dd62011-04-25 09:01:54 -0700483 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700484 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700485 return mWebStorageSizeManager;
486 }
487
John Reck35e9dd62011-04-25 09:01:54 -0700488 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700489 if (mAppCachePath == null) {
490 mAppCachePath = mContext.getDir("appcache", 0).getPath();
491 }
492 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700493 }
494
495 private void updateSearchEngine(boolean force) {
496 String searchEngineName = getSearchEngineName();
497 if (force || mSearchEngine == null ||
498 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700499 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000500 }
501 }
502
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100503 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700504 if (mSearchEngine == null) {
505 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100506 }
John Reck35e9dd62011-04-25 09:01:54 -0700507 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100508 }
509
John Reck35e9dd62011-04-25 09:01:54 -0700510 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700511 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700512 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100513 }
514
John Reck35e9dd62011-04-25 09:01:54 -0700515 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700516 Editor edit = mPrefs.edit();
517 edit.putBoolean(PREF_DEBUG_MENU, value);
518 if (!value) {
519 // Reset to "safe" value
520 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
521 }
522 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100523 }
524
John Reck35e9dd62011-04-25 09:01:54 -0700525 public void clearCache() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700526 if (mController != null) {
527 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800528 if (current != null) {
529 current.clearCache(true);
530 }
531 }
532 }
533
John Reck35e9dd62011-04-25 09:01:54 -0700534 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800535 CookieManager.getInstance().removeAllCookie();
536 }
537
John Reck35e9dd62011-04-25 09:01:54 -0700538 public void clearHistory() {
539 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800540 Browser.clearHistory(resolver);
541 Browser.clearSearches(resolver);
542 }
543
John Reck35e9dd62011-04-25 09:01:54 -0700544 public void clearFormData() {
545 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 if (mController!= null) {
547 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100548 if (currentTopView != null) {
549 currentTopView.clearFormData();
550 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800551 }
552 }
553
Bijan Amirzada3bbb3a42014-06-17 12:06:18 -0700554 public WebView getTopWebView(){
555 if (mController!= null)
556 return mController.getCurrentTopWebView();
557
558 return null;
559 }
560
John Reck35e9dd62011-04-25 09:01:54 -0700561 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700562 // Clear password store maintained by SWE engine
563 WebSettings settings = null;
564 // find a valid settings object
565 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
566 while (iter.hasNext()) {
567 WeakReference<WebSettings> ref = iter.next();
568 settings = (WebSettings)ref.get();
569 if (settings != null) {
570 break;
571 }
572 }
573 if (settings != null) {
Panos Thomasdbaea4e2014-05-22 06:48:47 -0700574 settings.clearPasswords();
Panos Thomasfa948b82014-03-09 03:42:42 -0700575 }
Vivek Sekhare8a8ec22014-07-10 14:52:43 -0700576
577 // Clear passwords in WebView database
578 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
579 db.clearHttpAuthUsernamePassword();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800580 }
581
John Reck35e9dd62011-04-25 09:01:54 -0700582 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100583 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100584 }
585
John Reck35e9dd62011-04-25 09:01:54 -0700586 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100587 GeolocationPermissions.getInstance().clearAll();
Panos Thomasb298aad2014-10-22 12:24:21 -0700588 if (GeolocationPermissions.isIncognitoCreated()) {
589 GeolocationPermissions.getIncognitoInstance().clearAll();
590 }
Nicolas Roard78a98e42009-05-11 13:34:17 +0100591 }
592
John Reck35e9dd62011-04-25 09:01:54 -0700593 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700594 mPrefs.edit()
595 .clear()
John Reckbd315192011-07-29 10:05:47 -0700596 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200597 resetCachedValues();
Pankaj Garg1e967142015-08-07 09:05:10 -0700598 if (WebRefiner.getInstance() != null) {
599 mPrefs.edit().putBoolean(PREF_WEB_REFINER, true).apply();
600 } else {
601 mPrefs.edit().putBoolean(PREF_WEB_REFINER, false).apply();
602 }
John Reck35e9dd62011-04-25 09:01:54 -0700603 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700604 }
605
Björn Isakssonc885a242012-06-05 17:19:04 +0200606 private void resetCachedValues() {
607 updateSearchEngine(false);
608 }
609
John Reck35e9dd62011-04-25 09:01:54 -0700610 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800611 // query the profile from components autofill database 524
612 if (mAutofillHandler.mAutoFillProfile == null &&
613 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
614 WebSettings settings = null;
615 // find a valid settings object
616 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
617 while (iter.hasNext()) {
618 WeakReference<WebSettings> ref = iter.next();
619 settings = (WebSettings)ref.get();
620 if (settings != null) {
621 break;
622 }
623 }
624 if (settings != null) {
625 AutoFillProfile profile =
626 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
627 mAutofillHandler.setAutoFillProfile(profile);
628 }
629 }
John Reck35e9dd62011-04-25 09:01:54 -0700630 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800631 }
632
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800633 public String getAutoFillProfileId() {
634 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800635 }
636
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800637 public void updateAutoFillProfile(AutoFillProfile profile) {
638 syncAutoFillProfile(profile);
639 }
640
641 private void syncAutoFillProfile(AutoFillProfile profile) {
642 synchronized (mManagedSettings) {
643 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
644 while (iter.hasNext()) {
645 WeakReference<WebSettings> ref = iter.next();
646 WebSettings settings = (WebSettings)ref.get();
647 if (settings == null) {
648 iter.remove();
649 continue;
650 }
651 // update the profile only once.
652 settings.setAutoFillProfile(profile);
653 // Now we should have the guid
654 mAutofillHandler.setAutoFillProfile(profile);
655 break;
656 }
657 }
658 }
John Reck35e9dd62011-04-25 09:01:54 -0700659 public void toggleDebugSettings() {
660 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800661 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100662
John Reckb8b2af82011-05-20 15:58:33 -0700663 public boolean hasDesktopUseragent(WebView view) {
Vivek Sekhar7b3d2da2014-11-13 16:20:11 -0800664 return view != null && view.getUseDesktopUserAgent();
John Reckb8b2af82011-05-20 15:58:33 -0700665 }
666
667 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700668 if (view == null) {
669 return;
670 }
Vivek Sekhar7b3d2da2014-11-13 16:20:11 -0800671 if (hasDesktopUseragent(view))
672 view.setUseDesktopUserAgent(false, true);
673 else
674 view.setUseDesktopUserAgent(true, true);
John Reckb8b2af82011-05-20 15:58:33 -0700675 }
676
John Reck7dc444b2011-06-16 17:44:29 -0700677 public static int getAdjustedMinimumFontSize(int rawValue) {
678 rawValue++; // Preference starts at 0, min font at 1
679 if (rawValue > 1) {
680 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
681 }
682 return rawValue;
683 }
684
John Reck5ba3c762011-09-14 15:00:15 -0700685 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700686 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700687 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700688 }
689
690 static int getRawTextZoom(int percent) {
691 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
692 }
693
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700694 public int getAdjustedDoubleTapZoom(int rawValue) {
695 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
696 return (int) ((rawValue + 100) * mFontSizeMult);
697 }
698
699 static int getRawDoubleTapZoom(int percent) {
700 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
701 }
702
John Reckcadae722011-07-25 11:36:17 -0700703 public SharedPreferences getPreferences() {
704 return mPrefs;
705 }
706
Victoria Lease96497832012-03-23 14:19:56 -0700707 // update connectivity-dependent options
708 public void updateConnectionType() {
709 ConnectivityManager cm = (ConnectivityManager)
710 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
711 String linkPrefetchPreference = getLinkPrefetchEnabled();
712 boolean linkPrefetchAllowed = linkPrefetchPreference.
713 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
714 NetworkInfo ni = cm.getActiveNetworkInfo();
715 if (ni != null) {
716 switch (ni.getType()) {
717 case ConnectivityManager.TYPE_WIFI:
718 case ConnectivityManager.TYPE_ETHERNET:
719 case ConnectivityManager.TYPE_BLUETOOTH:
720 linkPrefetchAllowed |= linkPrefetchPreference.
721 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
722 break;
723 case ConnectivityManager.TYPE_MOBILE:
724 case ConnectivityManager.TYPE_MOBILE_DUN:
725 case ConnectivityManager.TYPE_MOBILE_MMS:
726 case ConnectivityManager.TYPE_MOBILE_SUPL:
727 case ConnectivityManager.TYPE_WIMAX:
728 default:
729 break;
730 }
731 }
732 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
733 mLinkPrefetchAllowed = linkPrefetchAllowed;
734 syncManagedSettings();
735 }
736 }
737
luxiaol62677b02013-07-22 07:54:49 +0800738 public String getDownloadPath() {
739 return mPrefs.getString(PREF_DOWNLOAD_PATH,
740 DownloadHandler.getDefaultDownloadPath(mContext));
741 }
John Reck35e9dd62011-04-25 09:01:54 -0700742 // -----------------------------
743 // getter/setters for accessibility_preferences.xml
744 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100745
John Reck7dc444b2011-06-16 17:44:29 -0700746 @Deprecated
747 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700748 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
749 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100750 }
751
John Reck35e9dd62011-04-25 09:01:54 -0700752 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700753 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700754 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100755 }
756
John Reck92f25f82011-04-26 16:57:10 -0700757 public boolean forceEnableUserScalable() {
758 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
759 }
760
John Reck7dc444b2011-06-16 17:44:29 -0700761 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700762 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700763 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
764 return getAdjustedTextZoom(textZoom);
765 }
766
767 public void setTextZoom(int percent) {
768 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
769 }
770
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700771 public int getDoubleTapZoom() {
772 requireInitialization();
773 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
774 return getAdjustedDoubleTapZoom(doubleTapZoom);
775 }
776
777 public void setDoubleTapZoom(int percent) {
778 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
779 }
780
John Reck35e9dd62011-04-25 09:01:54 -0700781 // -----------------------------
782 // getter/setters for advanced_preferences.xml
783 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100784
John Reck35e9dd62011-04-25 09:01:54 -0700785 public String getSearchEngineName() {
Panos Thomas64c77e02014-12-17 22:15:23 -0800786 // The following is a NOP if the SEARCH_ENGINE restriction has already been created. Otherwise,
787 // it creates the restriction and if enabled it sets the <default_search_engine_value>.
788 SearchEngineRestriction.getInstance();
789
luxiaol221b3932013-07-19 15:27:57 +0800790 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
791 if (defaultSearchEngineValue == null) {
792 defaultSearchEngineValue = SearchEngine.GOOGLE;
793 }
794 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100795 }
John Reck63bb6872010-12-01 19:29:32 -0800796
Michael Kolb8d772b02012-01-19 13:56:00 -0800797 public boolean allowAppTabs() {
798 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
799 }
800
John Reck35e9dd62011-04-25 09:01:54 -0700801 public boolean openInBackground() {
802 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800803 }
John Reck35e9dd62011-04-25 09:01:54 -0700804
805 public boolean enableJavascript() {
806 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
807 }
808
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800809 public boolean enableMemoryMonitor() {
810 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
811 }
812
Tarun Nainani8eb00912014-07-17 12:28:32 -0700813
John Reck35e9dd62011-04-25 09:01:54 -0700814 public boolean loadPageInOverviewMode() {
815 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
816 }
817
818 public boolean autofitPages() {
819 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
820 }
821
822 public boolean blockPopupWindows() {
Pankaj Garg11015212015-07-06 14:38:43 -0700823 return !PermissionsServiceFactory.getDefaultPermissions(
824 PermissionsServiceFactory.PermissionType.POPUP);
John Reck35e9dd62011-04-25 09:01:54 -0700825 }
826
827 public boolean loadImages() {
828 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
829 }
830
831 public String getDefaultTextEncoding() {
Tarun Nainani88381422015-04-02 19:47:21 -0700832 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, "auto");
John Reck35e9dd62011-04-25 09:01:54 -0700833 }
834
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700835 public String getEdgeSwipeAction() {
836 return mPrefs.getString(PREF_EDGE_SWIPE,
837 mContext.getResources().getString(R.string.value_unknown_edge_swipe));
838 }
839
840 public void setEdgeSwipeTemporal() {
841 mPrefs.edit().putString(PREF_EDGE_SWIPE,
842 mContext.getResources().getString(R.string.value_temporal_edge_swipe)).apply();
843 }
844
845 public void setEdgeSwipeSpatial() {
846 mPrefs.edit().putString(PREF_EDGE_SWIPE,
847 mContext.getResources().getString(R.string.value_spatial_edge_swipe)).apply();
848 }
849
850 public void setEdgeSwipeDisabled() {
851 mPrefs.edit().putString(PREF_EDGE_SWIPE,
852 mContext.getResources().getString(R.string.value_disable_edge_swipe)).apply();
853 }
854
John Reck35e9dd62011-04-25 09:01:54 -0700855 // -----------------------------
856 // getter/setters for general_preferences.xml
857 // -----------------------------
858
859 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700860 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
861 }
862
863 public void setHomePage(String value) {
864 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
865 }
866
867 public boolean isAutofillEnabled() {
868 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
869 }
870
871 public void setAutofillEnabled(boolean value) {
872 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
873 }
874
Site Maoabb7bd32015-03-20 15:34:02 -0700875 public boolean isPowerSaveModeEnabled() {
876 return mPrefs.getBoolean(PREF_POWERSAVE_ENABLED, false);
877 }
878
879 public void setPowerSaveModeEnabled(boolean value) {
880 mPrefs.edit().putBoolean(PREF_POWERSAVE_ENABLED, value).apply();
881 }
882
Yida Wang078425c2015-06-02 14:14:06 -0400883 public boolean isNightModeEnabled() {
884 return mPrefs.getBoolean(PREF_NIGHTMODE_ENABLED, false);
885 }
886
887 public void setNightModeEnabled(boolean value) {
888 mPrefs.edit().putBoolean(PREF_NIGHTMODE_ENABLED, value).apply();
889 }
890
John Reck35e9dd62011-04-25 09:01:54 -0700891 // -----------------------------
892 // getter/setters for debug_preferences.xml
893 // -----------------------------
894
895 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700896 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700897 return true;
898 }
899 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
900 }
901
Derek Sollenberger31adf672011-07-08 11:31:30 -0400902 public boolean isSkiaHardwareAccelerated() {
903 if (!isDebugEnabled()) {
904 return false;
905 }
906 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
907 }
908
Site Mao7521b092015-07-20 18:27:20 -0700909 public boolean isDisablePerfFeatures() {
910 // This value is flipped in the prefs.
911 return !mPrefs.getBoolean(PREF_DISABLE_PERF, true);
912 }
913
John Reck35e9dd62011-04-25 09:01:54 -0700914 // -----------------------------
915 // getter/setters for hidden_debug_preferences.xml
916 // -----------------------------
917
918 public boolean enableVisualIndicator() {
919 if (!isDebugEnabled()) {
920 return false;
921 }
922 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
923 }
924
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700925 public boolean enableCpuUploadPath() {
926 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700927 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700928 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700929 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700930 }
931
John Reck35e9dd62011-04-25 09:01:54 -0700932 public boolean isSmallScreen() {
933 if (!isDebugEnabled()) {
934 return false;
935 }
936 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
937 }
938
939 public boolean isWideViewport() {
940 if (!isDebugEnabled()) {
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800941 return true;
John Reck35e9dd62011-04-25 09:01:54 -0700942 }
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800943 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
John Reck35e9dd62011-04-25 09:01:54 -0700944 }
945
946 public boolean isNormalLayout() {
947 if (!isDebugEnabled()) {
948 return false;
949 }
950 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
951 }
952
953 public boolean isTracing() {
954 if (!isDebugEnabled()) {
955 return false;
956 }
957 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
958 }
959
960 public boolean enableLightTouch() {
961 if (!isDebugEnabled()) {
962 return false;
963 }
964 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
965 }
966
967 public boolean enableNavDump() {
968 if (!isDebugEnabled()) {
969 return false;
970 }
971 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
972 }
973
974 public String getJsEngineFlags() {
975 if (!isDebugEnabled()) {
976 return "";
977 }
978 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
979 }
980
981 // -----------------------------
982 // getter/setters for lab_preferences.xml
983 // -----------------------------
984
John Reck35e9dd62011-04-25 09:01:54 -0700985 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700986 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700987 }
988
Michael Kolbc38c6042011-04-27 10:46:06 -0700989 public boolean useFullscreen() {
990 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
991 }
992
John Reck2fd9d0e2011-07-15 11:13:48 -0700993 public boolean useInvertedRendering() {
994 return mPrefs.getBoolean(PREF_INVERTED, false);
995 }
996
Nicolas Roard5d513102011-08-03 15:35:34 -0700997 public float getInvertedContrast() {
998 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
999 }
1000
John Reck35e9dd62011-04-25 09:01:54 -07001001 // -----------------------------
1002 // getter/setters for privacy_security_preferences.xml
1003 // -----------------------------
1004
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001005 public boolean doNotTrack() {
Dave Tharpb487d5d2015-05-15 15:54:08 -07001006 boolean dntVal;
1007 if (DoNotTrackRestriction.getInstance().isEnabled()) {
1008 dntVal = DoNotTrackRestriction.getInstance().getValue();
1009 }
1010 else {
1011 dntVal = mPrefs.getBoolean(PREF_DO_NOT_TRACK, true);
1012 }
1013 return dntVal;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001014 }
1015
John Reck35e9dd62011-04-25 09:01:54 -07001016 public boolean acceptCookies() {
Pankaj Garg32e1b942015-06-03 18:13:24 -07001017 return PermissionsServiceFactory.getDefaultPermissions(
1018 PermissionsServiceFactory.PermissionType.COOKIE);
John Reck35e9dd62011-04-25 09:01:54 -07001019 }
1020
1021 public boolean saveFormdata() {
1022 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
1023 }
1024
1025 public boolean enableGeolocation() {
1026 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
1027 }
1028
1029 public boolean rememberPasswords() {
1030 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
1031 }
1032
Michael Kolb14612442011-06-24 13:06:29 -07001033 // -----------------------------
1034 // getter/setters for bandwidth_preferences.xml
1035 // -----------------------------
1036
Mathew Inwood467813f2011-09-02 15:43:17 +01001037 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
1038 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -07001039 }
Mathew Inwood467813f2011-09-02 15:43:17 +01001040
1041 public static String getPreloadAlwaysPreferenceString(Context context) {
1042 return context.getResources().getString(R.string.pref_data_preload_value_always);
1043 }
1044
Mathew Inwood825fba72011-10-04 14:52:52 +01001045 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1046 "browser_default_preload_setting";
1047
1048 public String getDefaultPreloadSetting() {
1049 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1050 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1051 if (preload == null) {
1052 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1053 }
1054 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001055 }
1056
1057 public String getPreloadEnabled() {
1058 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1059 }
1060
Victoria Lease96497832012-03-23 14:19:56 -07001061 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1062 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1063 }
1064
1065 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1066 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1067 }
1068
1069 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1070 "browser_default_link_prefetch_setting";
1071
1072 public String getDefaultLinkPrefetchSetting() {
1073 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1074 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1075 if (preload == null) {
1076 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1077 }
1078 return preload;
1079 }
1080
1081 public String getLinkPrefetchEnabled() {
1082 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1083 }
1084
George Mount3636d0a2011-11-21 09:08:21 -08001085 // -----------------------------
1086 // getter/setters for browser recovery
1087 // -----------------------------
1088 /**
1089 * The last time browser was started.
1090 * @return The last browser start time as System.currentTimeMillis. This
1091 * can be 0 if this is the first time or the last tab was closed.
1092 */
1093 public long getLastRecovered() {
1094 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1095 }
1096
1097 /**
1098 * Sets the last browser start time.
1099 * @param time The last time as System.currentTimeMillis that the browser
1100 * was started. This should be set to 0 if the last tab is closed.
1101 */
1102 public void setLastRecovered(long time) {
1103 mPrefs.edit()
1104 .putLong(KEY_LAST_RECOVERED, time)
1105 .apply();
1106 }
1107
1108 /**
1109 * Used to determine whether or not the previous browser run crashed. Once
1110 * the previous state has been determined, the value will be set to false
1111 * until a pause is received.
1112 * @return true if the last browser run was paused or false if it crashed.
1113 */
1114 public boolean wasLastRunPaused() {
1115 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1116 }
1117
1118 /**
1119 * Sets whether or not the last run was a pause or crash.
1120 * @param isPaused Set to true When a pause is received or false after
1121 * resuming.
1122 */
1123 public void setLastRunPaused(boolean isPaused) {
1124 mPrefs.edit()
1125 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1126 .apply();
1127 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001128}