blob: 8cd2a088033fdd92226a8714198a7b16ebdb9ce5 [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;
Bijan Amirzadae75909d2014-05-06 14:18:54 -070029import android.text.TextUtils;
John Reck5ba3c762011-09-14 15:00:15 -070030import android.util.DisplayMetrics;
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.R;
34import com.android.browser.homepages.HomeProvider;
Dave Tharpb487d5d2015-05-15 15:54:08 -070035import com.android.browser.mdm.DoNotTrackRestriction;
Panos Thomasa9ff2c72014-12-17 22:15:23 -080036import com.android.browser.mdm.ProxyRestriction;
Panos Thomas64c77e02014-12-17 22:15:23 -080037import com.android.browser.mdm.SearchEngineRestriction;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070038import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070039import com.android.browser.provider.BrowserProvider;
Bijan Amirzada41242f22014-03-21 12:12:18 -070040import com.android.browser.search.SearchEngine;
41import com.android.browser.search.SearchEngines;
John Reck46500332011-06-07 14:36:10 -070042
John Reck35e9dd62011-04-25 09:01:54 -070043import java.lang.ref.WeakReference;
44import java.util.Iterator;
45import java.util.LinkedList;
John Reckb8b2af82011-05-20 15:58:33 -070046import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080048import org.codeaurora.swe.AutoFillProfile;
49import org.codeaurora.swe.CookieManager;
50import org.codeaurora.swe.GeolocationPermissions;
51import org.codeaurora.swe.WebSettings.LayoutAlgorithm;
52import org.codeaurora.swe.WebSettings.PluginState;
53import org.codeaurora.swe.WebSettings.TextSize;
54import org.codeaurora.swe.WebSettings.ZoomDensity;
55import org.codeaurora.swe.WebSettings;
56import org.codeaurora.swe.WebView;
Vivek Sekhare8a8ec22014-07-10 14:52:43 -070057import org.codeaurora.swe.WebViewDatabase;
Panos Thomas3f35d8d2014-11-08 22:40:23 -080058
John Reck35e9dd62011-04-25 09:01:54 -070059/**
60 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080061 */
John Reck35e9dd62011-04-25 09:01:54 -070062public class BrowserSettings implements OnSharedPreferenceChangeListener,
63 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010064
qqzhoue6ff8b42013-07-23 17:28:48 +080065 private static final String TAG = "BrowserSettings";
John Reck8fc22a12011-06-16 14:57:41 -070066 // The minimum min font size
67 // Aka, the lower bounds for the min font size range
68 // which is 1:5..24
69 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070070 // The initial value in the text zoom range
71 // This is what represents 100% in the SeekBarPreference range
72 private static final int TEXT_ZOOM_START_VAL = 10;
73 // The size of a single step in the text zoom range, in percent
74 private static final int TEXT_ZOOM_STEP = 5;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -070075 // The initial value in the double tap zoom range
76 // This is what represents 100% in the SeekBarPreference range
77 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
78 // The size of a single step in the double tap zoom range, in percent
79 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -070080
John Reck35e9dd62011-04-25 09:01:54 -070081 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -070082
John Reck35e9dd62011-04-25 09:01:54 -070083 private Context mContext;
84 private SharedPreferences mPrefs;
85 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -070086 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -070087 private WebStorageSizeManager mWebStorageSizeManager;
88 private AutofillHandler mAutofillHandler;
Ben Murdochaaa1f372011-07-25 15:40:58 +010089 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -070090 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -070091 private float mFontSizeMult = 1.0f;
John Reck78a6a1d2011-07-21 13:54:03 -070092
Victoria Lease96497832012-03-23 14:19:56 -070093 // Current state of network-dependent settings
94 private boolean mLinkPrefetchAllowed = true;
95
John Reck78a6a1d2011-07-21 13:54:03 -070096 // Cached values
97 private int mPageCacheCapacity = 1;
98 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -080099
John Reck35e9dd62011-04-25 09:01:54 -0700100 // Cached settings
101 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800102
Ben Murdochaaa1f372011-07-25 15:40:58 +0100103 private static String sFactoryResetUrl;
104
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700105 private boolean mEngineInitialized = false;
106 private boolean mSyncManagedSettings = false;
107
Bijan Amirzada3bb46302014-06-17 16:31:31 -0700108 public static synchronized void initialize(final Context context) {
109 if (sInstance == null)
110 sInstance = new BrowserSettings(context);
John Reck35e9dd62011-04-25 09:01:54 -0700111 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800112
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700113 public static BrowserSettings getInstance() {
John Reck35e9dd62011-04-25 09:01:54 -0700114 return sInstance;
115 }
Ben Murdochef671652010-11-25 17:17:58 +0000116
John Reck35e9dd62011-04-25 09:01:54 -0700117 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100118 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700119 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
John Reck35e9dd62011-04-25 09:01:54 -0700120 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckcadae722011-07-25 11:36:17 -0700121 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700122 }
123
124 public void setController(Controller controller) {
125 mController = controller;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700126 mNeedsSharedSync = true;
127 }
128
129 public void onEngineInitializationComplete() {
130 mEngineInitialized = true;
131 mAutofillHandler = new AutofillHandler(mContext);
132 if (mSyncManagedSettings) {
133 syncManagedSettings();
134 }
135 if (mNeedsSharedSync) {
John Reckc477e712011-08-17 11:27:15 -0700136 syncSharedSettings();
137 }
Panos Thomasa9ff2c72014-12-17 22:15:23 -0800138 // Instantiate ProxyRestriction after engine initialization
139 // to ensure ProxyChangeListener is already created.
140 ProxyRestriction.getInstance();
Ben Murdochef671652010-11-25 17:17:58 +0000141 }
142
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800143 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800144
John Reckc477e712011-08-17 11:27:15 -0700145 if (mNeedsSharedSync) {
146 syncSharedSettings();
147 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800148
John Reck35e9dd62011-04-25 09:01:54 -0700149 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800150 syncStaticSettings(settings);
151 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700152 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800153 }
154 }
155
John Reckd1d87312012-03-08 13:25:00 -0800156 public void stopManagingSettings(WebSettings settings) {
157 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
158 while (iter.hasNext()) {
159 WeakReference<WebSettings> ref = iter.next();
160 if (ref.get() == settings) {
161 iter.remove();
162 return;
163 }
164 }
165 }
166
John Reckcadae722011-07-25 11:36:17 -0700167 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700168
169 @Override
170 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700171 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
172 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700173 // the cost of one cached page is ~3M (measured using nytimes.com). For
174 // low end devices, we only cache one page. For high end devices, we try
175 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800176
177 // SWE_TODO : assume a high-memory device
178 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700179 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800180 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700181 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
182 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
183 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700184 // Workaround b/5254577
185 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700186 if (Build.VERSION.CODENAME.equals("REL")) {
187 // This is a release build, always startup with debug disabled
188 setDebugEnabled(false);
189 }
190 if (mPrefs.contains(PREF_TEXT_SIZE)) {
191 /*
192 * Update from TextSize enum to zoom percent
193 * SMALLEST is 50%
194 * SMALLER is 75%
195 * NORMAL is 100%
196 * LARGER is 150%
197 * LARGEST is 200%
198 */
199 switch (getTextSize()) {
200 case SMALLEST:
201 setTextZoom(50);
202 break;
203 case SMALLER:
204 setTextZoom(75);
205 break;
206 case LARGER:
207 setTextZoom(150);
208 break;
209 case LARGEST:
210 setTextZoom(200);
211 break;
212 }
213 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
214 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100215
qqzhou8c5b0a32013-07-22 15:31:03 +0800216 // add for carrier homepage feature
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700217 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
qqzhou8c5b0a32013-07-22 15:31:03 +0800218
kaiyizbf086ea2013-08-02 11:03:58 +0800219 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
Tarun Nainani95b79682014-12-09 09:38:04 -0800220 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING, "auto").apply();
kaiyizbf086ea2013-08-02 11:03:58 +0800221 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700222
Ben Murdochaaa1f372011-07-25 15:40:58 +0100223 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
224 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
225 BrowserProvider.getClientId(mContext.getContentResolver()));
226 }
227
Ben Murdochaaa1f372011-07-25 15:40:58 +0100228 synchronized (BrowserSettings.class) {
229 sInitialized = true;
230 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700231 }
232 }
233 };
234
Ben Murdochaaa1f372011-07-25 15:40:58 +0100235 private static void requireInitialization() {
236 synchronized (BrowserSettings.class) {
237 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700238 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100239 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700240 } catch (InterruptedException e) {
241 }
242 }
243 }
244 }
245
The Android Open Source Project0c908882009-03-03 19:32:16 -0800246 /**
John Reck35e9dd62011-04-25 09:01:54 -0700247 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800248 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800249 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700250 settings.setGeolocationEnabled(enableGeolocation());
251 settings.setJavaScriptEnabled(enableJavascript());
252 settings.setLightTouchEnabled(enableLightTouch());
253 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700254 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
John Reck35e9dd62011-04-25 09:01:54 -0700255 settings.setMinimumFontSize(getMinimumFontSize());
256 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck7dc444b2011-06-16 17:44:29 -0700257 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700258 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100259 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700260 settings.setLoadsImagesAutomatically(loadImages());
261 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
262 settings.setSavePassword(rememberPasswords());
263 settings.setSaveFormData(saveFormdata());
264 settings.setUseWideViewPort(isWideViewport());
Panos Thomasb10bbda2014-06-23 10:18:36 -0700265 settings.setDoNotTrack(doNotTrack());
Tarun Nainani8eb00912014-07-17 12:28:32 -0700266 settings.setMediaPlaybackRequiresUserGesture(false);
John Reck2fd9d0e2011-07-15 11:13:48 -0700267
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800268 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800269 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
270 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
271 settingsClassic.setForceUserScalable(forceEnableUserScalable());
272 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
273 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800274
John Reckea17e782011-10-27 17:15:59 -0700275 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800276 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700277 useInverted ? "true" : "false");
278 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800279 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700280 Float.toString(getInvertedContrast()));
281 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700282
John Reckea17e782011-10-27 17:15:59 -0700283 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800284 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700285 enableCpuUploadPath() ? "true" : "false");
286 }
Victoria Lease96497832012-03-23 14:19:56 -0700287
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800288 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000289 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800290
Tarun Nainani8eb00912014-07-17 12:28:32 -0700291
John Reck35e9dd62011-04-25 09:01:54 -0700292 /**
293 * Syncs all the settings that have no UI
294 * These cannot change, so we only need to set them once per WebSettings
295 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800296 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700297 settings.setDefaultFontSize(16);
298 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000299
John Reck35e9dd62011-04-25 09:01:54 -0700300 // WebView inside Browser doesn't want initial focus to be set.
301 settings.setNeedInitialFocus(false);
302 // Browser supports multiple windows
303 settings.setSupportMultipleWindows(true);
304 // enable smooth transition for better performance during panning or
305 // zooming
306 settings.setEnableSmoothTransition(true);
307 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700308 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700309
310 // HTML5 API flags
311 settings.setAppCacheEnabled(true);
312 settings.setDatabaseEnabled(true);
313 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700314
315 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700316 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700317 settings.setAppCachePath(getAppCachePath());
318 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
319 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700320 // origin policy for file access
321 settings.setAllowUniversalAccessFromFileURLs(false);
322 settings.setAllowFileAccessFromFileURLs(false);
Sudheer Koganti24766882014-10-02 10:58:09 -0700323 settings.setFullscreenSupported(true);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800324
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800325 //if (!(settings instanceof WebSettingsClassic)) return;
326 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800327
328 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
329 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
330 // WebView should be preserving the memory as much as possible.
331 // However, apps like browser wish to turn on the performance mode which
332 // would require more memory.
333 // TODO: We need to dynamically allocate/deallocate temporary memory for
334 // apps which are trying to use minimal memory. Currently, double
335 // buffering is always turned on, which is unnecessary.
336 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
337 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800338 */
John Reck35e9dd62011-04-25 09:01:54 -0700339 }
340
341 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700342 mNeedsSharedSync = false;
Axesh R. Ajmera579c70c2014-04-17 13:24:56 -0700343 CookieManager.getInstance().setAcceptCookie(acceptCookies());
Vivek Sekhared791da2015-02-22 12:39:05 -0800344
John Reck35e9dd62011-04-25 09:01:54 -0700345 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700346
John Reck35e9dd62011-04-25 09:01:54 -0700347 private void syncManagedSettings() {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700348 if (!mEngineInitialized) {
349 mSyncManagedSettings = true;
350 return;
351 }
352 mSyncManagedSettings = false;
John Reck35e9dd62011-04-25 09:01:54 -0700353 syncSharedSettings();
354 synchronized (mManagedSettings) {
355 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
356 while (iter.hasNext()) {
357 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800358 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700359 if (settings == null) {
360 iter.remove();
361 continue;
362 }
363 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000364 }
John Reck35e9dd62011-04-25 09:01:54 -0700365 }
366 }
Ben Murdochef671652010-11-25 17:17:58 +0000367
John Reck35e9dd62011-04-25 09:01:54 -0700368 @Override
369 public void onSharedPreferenceChanged(
370 SharedPreferences sharedPreferences, String key) {
371 syncManagedSettings();
372 if (PREF_SEARCH_ENGINE.equals(key)) {
373 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700374 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200375 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700376 mController.getUi().setFullscreen(useFullscreen());
377 }
Victoria Lease96497832012-03-23 14:19:56 -0700378 } else if (PREF_LINK_PREFETCH.equals(key)) {
379 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700380 }
John Reck35e9dd62011-04-25 09:01:54 -0700381 }
382
John Reck961d35d2011-06-23 09:45:54 -0700383 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100384 requireInitialization();
385 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700386 }
387
388 public LayoutAlgorithm getLayoutAlgorithm() {
389 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
390 if (autofitPages()) {
Tarun Nainani003bec52014-07-02 02:20:34 -0700391 layoutAlgorithm = LayoutAlgorithm.TEXT_AUTOSIZING;
John Reck35e9dd62011-04-25 09:01:54 -0700392 }
393 if (isDebugEnabled()) {
394 if (isSmallScreen()) {
395 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000396 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700397 if (isNormalLayout()) {
398 layoutAlgorithm = LayoutAlgorithm.NORMAL;
399 } else {
400 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
401 }
Ben Murdochef671652010-11-25 17:17:58 +0000402 }
John Reck35e9dd62011-04-25 09:01:54 -0700403 }
404 return layoutAlgorithm;
405 }
Ben Murdochef671652010-11-25 17:17:58 +0000406
John Reck35e9dd62011-04-25 09:01:54 -0700407 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700408 requireInitialization();
409 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800410 }
411
John Reck35e9dd62011-04-25 09:01:54 -0700412 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700413 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700414 return mWebStorageSizeManager;
415 }
416
John Reck35e9dd62011-04-25 09:01:54 -0700417 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700418 if (mAppCachePath == null) {
419 mAppCachePath = mContext.getDir("appcache", 0).getPath();
420 }
421 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700422 }
423
424 private void updateSearchEngine(boolean force) {
425 String searchEngineName = getSearchEngineName();
426 if (force || mSearchEngine == null ||
427 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700428 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000429 }
430 }
431
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100432 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700433 if (mSearchEngine == null) {
434 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100435 }
John Reck35e9dd62011-04-25 09:01:54 -0700436 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100437 }
438
John Reck35e9dd62011-04-25 09:01:54 -0700439 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700440 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700441 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100442 }
443
John Reck35e9dd62011-04-25 09:01:54 -0700444 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700445 Editor edit = mPrefs.edit();
446 edit.putBoolean(PREF_DEBUG_MENU, value);
447 if (!value) {
448 // Reset to "safe" value
449 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
450 }
451 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100452 }
453
John Reck35e9dd62011-04-25 09:01:54 -0700454 public void clearCache() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700455 if (mController != null) {
456 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800457 if (current != null) {
458 current.clearCache(true);
459 }
460 }
461 }
462
John Reck35e9dd62011-04-25 09:01:54 -0700463 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800464 CookieManager.getInstance().removeAllCookie();
465 }
466
John Reck35e9dd62011-04-25 09:01:54 -0700467 public void clearHistory() {
468 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800469 Browser.clearHistory(resolver);
470 Browser.clearSearches(resolver);
471 }
472
John Reck35e9dd62011-04-25 09:01:54 -0700473 public void clearFormData() {
474 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700475 if (mController!= null) {
476 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100477 if (currentTopView != null) {
478 currentTopView.clearFormData();
479 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800480 }
481 }
482
Bijan Amirzada3bbb3a42014-06-17 12:06:18 -0700483 public WebView getTopWebView(){
484 if (mController!= null)
485 return mController.getCurrentTopWebView();
486
487 return null;
488 }
489
John Reck35e9dd62011-04-25 09:01:54 -0700490 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700491 // Clear password store maintained by SWE engine
492 WebSettings settings = null;
493 // find a valid settings object
494 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
495 while (iter.hasNext()) {
496 WeakReference<WebSettings> ref = iter.next();
497 settings = (WebSettings)ref.get();
498 if (settings != null) {
499 break;
500 }
501 }
502 if (settings != null) {
Panos Thomasdbaea4e2014-05-22 06:48:47 -0700503 settings.clearPasswords();
Panos Thomasfa948b82014-03-09 03:42:42 -0700504 }
Vivek Sekhare8a8ec22014-07-10 14:52:43 -0700505
506 // Clear passwords in WebView database
507 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
508 db.clearHttpAuthUsernamePassword();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800509 }
510
John Reck35e9dd62011-04-25 09:01:54 -0700511 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100512 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100513 }
514
John Reck35e9dd62011-04-25 09:01:54 -0700515 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100516 GeolocationPermissions.getInstance().clearAll();
Panos Thomasb298aad2014-10-22 12:24:21 -0700517 if (GeolocationPermissions.isIncognitoCreated()) {
518 GeolocationPermissions.getIncognitoInstance().clearAll();
519 }
Nicolas Roard78a98e42009-05-11 13:34:17 +0100520 }
521
John Reck35e9dd62011-04-25 09:01:54 -0700522 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700523 mPrefs.edit()
524 .clear()
John Reckbd315192011-07-29 10:05:47 -0700525 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200526 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700527 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700528 }
529
Björn Isakssonc885a242012-06-05 17:19:04 +0200530 private void resetCachedValues() {
531 updateSearchEngine(false);
532 }
533
John Reck35e9dd62011-04-25 09:01:54 -0700534 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800535 // query the profile from components autofill database 524
536 if (mAutofillHandler.mAutoFillProfile == null &&
537 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
538 WebSettings settings = null;
539 // find a valid settings object
540 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
541 while (iter.hasNext()) {
542 WeakReference<WebSettings> ref = iter.next();
543 settings = (WebSettings)ref.get();
544 if (settings != null) {
545 break;
546 }
547 }
548 if (settings != null) {
549 AutoFillProfile profile =
550 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
551 mAutofillHandler.setAutoFillProfile(profile);
552 }
553 }
John Reck35e9dd62011-04-25 09:01:54 -0700554 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800555 }
556
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800557 public String getAutoFillProfileId() {
558 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800559 }
560
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800561 public void updateAutoFillProfile(AutoFillProfile profile) {
562 syncAutoFillProfile(profile);
563 }
564
565 private void syncAutoFillProfile(AutoFillProfile profile) {
566 synchronized (mManagedSettings) {
567 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
568 while (iter.hasNext()) {
569 WeakReference<WebSettings> ref = iter.next();
570 WebSettings settings = (WebSettings)ref.get();
571 if (settings == null) {
572 iter.remove();
573 continue;
574 }
575 // update the profile only once.
576 settings.setAutoFillProfile(profile);
577 // Now we should have the guid
578 mAutofillHandler.setAutoFillProfile(profile);
579 break;
580 }
581 }
582 }
John Reck35e9dd62011-04-25 09:01:54 -0700583 public void toggleDebugSettings() {
584 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800585 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100586
John Reckb8b2af82011-05-20 15:58:33 -0700587 public boolean hasDesktopUseragent(WebView view) {
Vivek Sekhar7b3d2da2014-11-13 16:20:11 -0800588 return view != null && view.getUseDesktopUserAgent();
John Reckb8b2af82011-05-20 15:58:33 -0700589 }
590
591 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700592 if (view == null) {
593 return;
594 }
Vivek Sekhar7b3d2da2014-11-13 16:20:11 -0800595 if (hasDesktopUseragent(view))
596 view.setUseDesktopUserAgent(false, true);
597 else
598 view.setUseDesktopUserAgent(true, true);
John Reckb8b2af82011-05-20 15:58:33 -0700599 }
600
John Reck7dc444b2011-06-16 17:44:29 -0700601 public static int getAdjustedMinimumFontSize(int rawValue) {
602 rawValue++; // Preference starts at 0, min font at 1
603 if (rawValue > 1) {
604 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
605 }
606 return rawValue;
607 }
608
John Reck5ba3c762011-09-14 15:00:15 -0700609 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700610 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700611 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700612 }
613
614 static int getRawTextZoom(int percent) {
615 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
616 }
617
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700618 public int getAdjustedDoubleTapZoom(int rawValue) {
619 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
620 return (int) ((rawValue + 100) * mFontSizeMult);
621 }
622
623 static int getRawDoubleTapZoom(int percent) {
624 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
625 }
626
John Reckcadae722011-07-25 11:36:17 -0700627 public SharedPreferences getPreferences() {
628 return mPrefs;
629 }
630
Victoria Lease96497832012-03-23 14:19:56 -0700631 // update connectivity-dependent options
632 public void updateConnectionType() {
633 ConnectivityManager cm = (ConnectivityManager)
634 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
635 String linkPrefetchPreference = getLinkPrefetchEnabled();
636 boolean linkPrefetchAllowed = linkPrefetchPreference.
637 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
638 NetworkInfo ni = cm.getActiveNetworkInfo();
639 if (ni != null) {
640 switch (ni.getType()) {
641 case ConnectivityManager.TYPE_WIFI:
642 case ConnectivityManager.TYPE_ETHERNET:
643 case ConnectivityManager.TYPE_BLUETOOTH:
644 linkPrefetchAllowed |= linkPrefetchPreference.
645 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
646 break;
647 case ConnectivityManager.TYPE_MOBILE:
648 case ConnectivityManager.TYPE_MOBILE_DUN:
649 case ConnectivityManager.TYPE_MOBILE_MMS:
650 case ConnectivityManager.TYPE_MOBILE_SUPL:
651 case ConnectivityManager.TYPE_WIMAX:
652 default:
653 break;
654 }
655 }
656 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
657 mLinkPrefetchAllowed = linkPrefetchAllowed;
658 syncManagedSettings();
659 }
660 }
661
luxiaol62677b02013-07-22 07:54:49 +0800662 public String getDownloadPath() {
663 return mPrefs.getString(PREF_DOWNLOAD_PATH,
664 DownloadHandler.getDefaultDownloadPath(mContext));
665 }
John Reck35e9dd62011-04-25 09:01:54 -0700666 // -----------------------------
667 // getter/setters for accessibility_preferences.xml
668 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100669
John Reck7dc444b2011-06-16 17:44:29 -0700670 @Deprecated
671 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700672 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
673 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100674 }
675
John Reck35e9dd62011-04-25 09:01:54 -0700676 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700677 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700678 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100679 }
680
John Reck92f25f82011-04-26 16:57:10 -0700681 public boolean forceEnableUserScalable() {
682 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
683 }
684
John Reck7dc444b2011-06-16 17:44:29 -0700685 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700686 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700687 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
688 return getAdjustedTextZoom(textZoom);
689 }
690
691 public void setTextZoom(int percent) {
692 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
693 }
694
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700695 public int getDoubleTapZoom() {
696 requireInitialization();
697 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
698 return getAdjustedDoubleTapZoom(doubleTapZoom);
699 }
700
701 public void setDoubleTapZoom(int percent) {
702 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
703 }
704
John Reck35e9dd62011-04-25 09:01:54 -0700705 // -----------------------------
706 // getter/setters for advanced_preferences.xml
707 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100708
John Reck35e9dd62011-04-25 09:01:54 -0700709 public String getSearchEngineName() {
Panos Thomas64c77e02014-12-17 22:15:23 -0800710 // The following is a NOP if the SEARCH_ENGINE restriction has already been created. Otherwise,
711 // it creates the restriction and if enabled it sets the <default_search_engine_value>.
712 SearchEngineRestriction.getInstance();
713
luxiaol221b3932013-07-19 15:27:57 +0800714 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
715 if (defaultSearchEngineValue == null) {
716 defaultSearchEngineValue = SearchEngine.GOOGLE;
717 }
718 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100719 }
John Reck63bb6872010-12-01 19:29:32 -0800720
Michael Kolb8d772b02012-01-19 13:56:00 -0800721 public boolean allowAppTabs() {
722 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
723 }
724
John Reck35e9dd62011-04-25 09:01:54 -0700725 public boolean openInBackground() {
726 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800727 }
John Reck35e9dd62011-04-25 09:01:54 -0700728
729 public boolean enableJavascript() {
730 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
731 }
732
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800733 public boolean enableMemoryMonitor() {
734 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
735 }
736
Tarun Nainani8eb00912014-07-17 12:28:32 -0700737
John Reck35e9dd62011-04-25 09:01:54 -0700738 public boolean loadPageInOverviewMode() {
739 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
740 }
741
742 public boolean autofitPages() {
743 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
744 }
745
746 public boolean blockPopupWindows() {
747 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
748 }
749
750 public boolean loadImages() {
751 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
752 }
753
754 public String getDefaultTextEncoding() {
Tarun Nainani88381422015-04-02 19:47:21 -0700755 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, "auto");
John Reck35e9dd62011-04-25 09:01:54 -0700756 }
757
758 // -----------------------------
759 // getter/setters for general_preferences.xml
760 // -----------------------------
761
762 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700763 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
764 }
765
766 public void setHomePage(String value) {
767 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
768 }
769
770 public boolean isAutofillEnabled() {
771 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
772 }
773
774 public void setAutofillEnabled(boolean value) {
775 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
776 }
777
Site Maoabb7bd32015-03-20 15:34:02 -0700778 public boolean isPowerSaveModeEnabled() {
779 return mPrefs.getBoolean(PREF_POWERSAVE_ENABLED, false);
780 }
781
782 public void setPowerSaveModeEnabled(boolean value) {
783 mPrefs.edit().putBoolean(PREF_POWERSAVE_ENABLED, value).apply();
784 }
785
John Reck35e9dd62011-04-25 09:01:54 -0700786 // -----------------------------
787 // getter/setters for debug_preferences.xml
788 // -----------------------------
789
790 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700791 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700792 return true;
793 }
794 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
795 }
796
Derek Sollenberger31adf672011-07-08 11:31:30 -0400797 public boolean isSkiaHardwareAccelerated() {
798 if (!isDebugEnabled()) {
799 return false;
800 }
801 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
802 }
803
John Reck35e9dd62011-04-25 09:01:54 -0700804 // -----------------------------
805 // getter/setters for hidden_debug_preferences.xml
806 // -----------------------------
807
808 public boolean enableVisualIndicator() {
809 if (!isDebugEnabled()) {
810 return false;
811 }
812 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
813 }
814
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700815 public boolean enableCpuUploadPath() {
816 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700817 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700818 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700819 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700820 }
821
John Reck35e9dd62011-04-25 09:01:54 -0700822 public boolean isSmallScreen() {
823 if (!isDebugEnabled()) {
824 return false;
825 }
826 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
827 }
828
829 public boolean isWideViewport() {
830 if (!isDebugEnabled()) {
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800831 return true;
John Reck35e9dd62011-04-25 09:01:54 -0700832 }
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800833 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
John Reck35e9dd62011-04-25 09:01:54 -0700834 }
835
836 public boolean isNormalLayout() {
837 if (!isDebugEnabled()) {
838 return false;
839 }
840 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
841 }
842
843 public boolean isTracing() {
844 if (!isDebugEnabled()) {
845 return false;
846 }
847 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
848 }
849
850 public boolean enableLightTouch() {
851 if (!isDebugEnabled()) {
852 return false;
853 }
854 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
855 }
856
857 public boolean enableNavDump() {
858 if (!isDebugEnabled()) {
859 return false;
860 }
861 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
862 }
863
864 public String getJsEngineFlags() {
865 if (!isDebugEnabled()) {
866 return "";
867 }
868 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
869 }
870
871 // -----------------------------
872 // getter/setters for lab_preferences.xml
873 // -----------------------------
874
John Reck35e9dd62011-04-25 09:01:54 -0700875 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700876 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700877 }
878
Michael Kolbc38c6042011-04-27 10:46:06 -0700879 public boolean useFullscreen() {
880 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
881 }
882
John Reck2fd9d0e2011-07-15 11:13:48 -0700883 public boolean useInvertedRendering() {
884 return mPrefs.getBoolean(PREF_INVERTED, false);
885 }
886
Nicolas Roard5d513102011-08-03 15:35:34 -0700887 public float getInvertedContrast() {
888 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
889 }
890
John Reck35e9dd62011-04-25 09:01:54 -0700891 // -----------------------------
892 // getter/setters for privacy_security_preferences.xml
893 // -----------------------------
894
895 public boolean showSecurityWarnings() {
896 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
897 }
898
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700899 public boolean doNotTrack() {
Dave Tharpb487d5d2015-05-15 15:54:08 -0700900 boolean dntVal;
901 if (DoNotTrackRestriction.getInstance().isEnabled()) {
902 dntVal = DoNotTrackRestriction.getInstance().getValue();
903 }
904 else {
905 dntVal = mPrefs.getBoolean(PREF_DO_NOT_TRACK, true);
906 }
907 return dntVal;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700908 }
909
John Reck35e9dd62011-04-25 09:01:54 -0700910 public boolean acceptCookies() {
911 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
912 }
913
914 public boolean saveFormdata() {
915 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
916 }
917
918 public boolean enableGeolocation() {
919 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
920 }
921
922 public boolean rememberPasswords() {
923 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
924 }
925
Michael Kolb14612442011-06-24 13:06:29 -0700926 // -----------------------------
927 // getter/setters for bandwidth_preferences.xml
928 // -----------------------------
929
Mathew Inwood467813f2011-09-02 15:43:17 +0100930 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
931 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -0700932 }
Mathew Inwood467813f2011-09-02 15:43:17 +0100933
934 public static String getPreloadAlwaysPreferenceString(Context context) {
935 return context.getResources().getString(R.string.pref_data_preload_value_always);
936 }
937
Mathew Inwood825fba72011-10-04 14:52:52 +0100938 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
939 "browser_default_preload_setting";
940
941 public String getDefaultPreloadSetting() {
942 String preload = Settings.Secure.getString(mContext.getContentResolver(),
943 DEAULT_PRELOAD_SECURE_SETTING_KEY);
944 if (preload == null) {
945 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
946 }
947 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +0100948 }
949
950 public String getPreloadEnabled() {
951 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
952 }
953
Victoria Lease96497832012-03-23 14:19:56 -0700954 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
955 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
956 }
957
958 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
959 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
960 }
961
962 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
963 "browser_default_link_prefetch_setting";
964
965 public String getDefaultLinkPrefetchSetting() {
966 String preload = Settings.Secure.getString(mContext.getContentResolver(),
967 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
968 if (preload == null) {
969 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
970 }
971 return preload;
972 }
973
974 public String getLinkPrefetchEnabled() {
975 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
976 }
977
George Mount3636d0a2011-11-21 09:08:21 -0800978 // -----------------------------
979 // getter/setters for browser recovery
980 // -----------------------------
981 /**
982 * The last time browser was started.
983 * @return The last browser start time as System.currentTimeMillis. This
984 * can be 0 if this is the first time or the last tab was closed.
985 */
986 public long getLastRecovered() {
987 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
988 }
989
990 /**
991 * Sets the last browser start time.
992 * @param time The last time as System.currentTimeMillis that the browser
993 * was started. This should be set to 0 if the last tab is closed.
994 */
995 public void setLastRecovered(long time) {
996 mPrefs.edit()
997 .putLong(KEY_LAST_RECOVERED, time)
998 .apply();
999 }
1000
1001 /**
1002 * Used to determine whether or not the previous browser run crashed. Once
1003 * the previous state has been determined, the value will be set to false
1004 * until a pause is received.
1005 * @return true if the last browser run was paused or false if it crashed.
1006 */
1007 public boolean wasLastRunPaused() {
1008 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1009 }
1010
1011 /**
1012 * Sets whether or not the last run was a pause or crash.
1013 * @param isPaused Set to true When a pause is received or false after
1014 * resuming.
1015 */
1016 public void setLastRunPaused(boolean isPaused) {
1017 mPrefs.edit()
1018 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1019 .apply();
1020 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001021}