blob: c03364706c1684d714d953259bb6b2c401cb2be0 [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;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.webkit.WebIconDatabase;
Nicolas Roard78a98e42009-05-11 13:34:17 +010032import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033
Bijan Amirzada41242f22014-03-21 12:12:18 -070034import com.android.browser.R;
35import com.android.browser.homepages.HomeProvider;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070036import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070037import com.android.browser.provider.BrowserProvider;
Bijan Amirzada41242f22014-03-21 12:12:18 -070038import com.android.browser.search.SearchEngine;
39import com.android.browser.search.SearchEngines;
John Reck46500332011-06-07 14:36:10 -070040
John Reck35e9dd62011-04-25 09:01:54 -070041import java.lang.ref.WeakReference;
42import java.util.Iterator;
43import java.util.LinkedList;
qqzhou8c5b0a32013-07-22 15:31:03 +080044import java.util.Locale;
John Reckb8b2af82011-05-20 15:58:33 -070045import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080046
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080047import org.codeaurora.swe.AutoFillProfile;
48import org.codeaurora.swe.CookieManager;
49import org.codeaurora.swe.GeolocationPermissions;
50import org.codeaurora.swe.WebSettings.LayoutAlgorithm;
51import org.codeaurora.swe.WebSettings.PluginState;
52import org.codeaurora.swe.WebSettings.TextSize;
53import org.codeaurora.swe.WebSettings.ZoomDensity;
54import org.codeaurora.swe.WebSettings;
55import org.codeaurora.swe.WebView;
Vivek Sekhare8a8ec22014-07-10 14:52:43 -070056import org.codeaurora.swe.WebViewDatabase;
John Reck35e9dd62011-04-25 09:01:54 -070057/**
58 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080059 */
John Reck35e9dd62011-04-25 09:01:54 -070060public class BrowserSettings implements OnSharedPreferenceChangeListener,
61 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010062
John Reck35e9dd62011-04-25 09:01:54 -070063 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070064 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
65 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
66 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080067
68 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070069 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
70 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070071
72 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070073 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
74 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070075
76 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070077 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
78 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080079
John Reck7fd1e8f2011-04-27 18:22:57 -070080 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
81 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
82 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
83
John Reck35e9dd62011-04-25 09:01:54 -070084 private static final String USER_AGENTS[] = { null,
85 DESKTOP_USERAGENT,
86 IPHONE_USERAGENT,
87 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070088 FROYO_USERAGENT,
89 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070090 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080091
qqzhoue6ff8b42013-07-23 17:28:48 +080092 private static final String TAG = "BrowserSettings";
John Reck8fc22a12011-06-16 14:57:41 -070093 // The minimum min font size
94 // Aka, the lower bounds for the min font size range
95 // which is 1:5..24
96 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070097 // The initial value in the text zoom range
98 // This is what represents 100% in the SeekBarPreference range
99 private static final int TEXT_ZOOM_START_VAL = 10;
100 // The size of a single step in the text zoom range, in percent
101 private static final int TEXT_ZOOM_STEP = 5;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700102 // The initial value in the double tap zoom range
103 // This is what represents 100% in the SeekBarPreference range
104 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
105 // The size of a single step in the double tap zoom range, in percent
106 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -0700107
John Reck35e9dd62011-04-25 09:01:54 -0700108 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -0700109
John Reck35e9dd62011-04-25 09:01:54 -0700110 private Context mContext;
111 private SharedPreferences mPrefs;
112 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700113 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700114 private WebStorageSizeManager mWebStorageSizeManager;
115 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700116 private WeakHashMap<WebSettings, String> mCustomUserAgents;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100117 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -0700118 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -0700119 private float mFontSizeMult = 1.0f;
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700120 private boolean enableWideViewport = true;
John Reck78a6a1d2011-07-21 13:54:03 -0700121
Victoria Lease96497832012-03-23 14:19:56 -0700122 // Current state of network-dependent settings
123 private boolean mLinkPrefetchAllowed = true;
124
John Reck78a6a1d2011-07-21 13:54:03 -0700125 // Cached values
126 private int mPageCacheCapacity = 1;
127 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800128
John Reck35e9dd62011-04-25 09:01:54 -0700129 // Cached settings
130 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800131
Ben Murdochaaa1f372011-07-25 15:40:58 +0100132 private static String sFactoryResetUrl;
133
Bijan Amirzada3bb46302014-06-17 16:31:31 -0700134 public static synchronized void initialize(final Context context) {
135 if (sInstance == null)
136 sInstance = new BrowserSettings(context);
John Reck35e9dd62011-04-25 09:01:54 -0700137 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800138
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700139 public static BrowserSettings getInstance() {
John Reck35e9dd62011-04-25 09:01:54 -0700140 return sInstance;
141 }
Ben Murdochef671652010-11-25 17:17:58 +0000142
John Reck35e9dd62011-04-25 09:01:54 -0700143 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100144 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700145 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
146 mAutofillHandler = new AutofillHandler(mContext);
147 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700148 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reckcadae722011-07-25 11:36:17 -0700149 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700150 }
151
152 public void setController(Controller controller) {
153 mController = controller;
John Reckc477e712011-08-17 11:27:15 -0700154 if (sInitialized) {
155 syncSharedSettings();
156 }
Ben Murdochef671652010-11-25 17:17:58 +0000157 }
158
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800159 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800160
John Reckc477e712011-08-17 11:27:15 -0700161 if (mNeedsSharedSync) {
162 syncSharedSettings();
163 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800164
John Reck35e9dd62011-04-25 09:01:54 -0700165 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800166 syncStaticSettings(settings);
167 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700168 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800169 }
170 }
171
John Reckd1d87312012-03-08 13:25:00 -0800172 public void stopManagingSettings(WebSettings settings) {
173 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
174 while (iter.hasNext()) {
175 WeakReference<WebSettings> ref = iter.next();
176 if (ref.get() == settings) {
177 iter.remove();
178 return;
179 }
180 }
181 }
182
John Reckcadae722011-07-25 11:36:17 -0700183 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700184
185 @Override
186 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700187 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
188 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700189 // the cost of one cached page is ~3M (measured using nytimes.com). For
190 // low end devices, we only cache one page. For high end devices, we try
191 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800192
193 // SWE_TODO : assume a high-memory device
194 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700195 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800196 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700197 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
198 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
199 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700200 // Workaround b/5254577
201 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700202 if (Build.VERSION.CODENAME.equals("REL")) {
203 // This is a release build, always startup with debug disabled
204 setDebugEnabled(false);
205 }
206 if (mPrefs.contains(PREF_TEXT_SIZE)) {
207 /*
208 * Update from TextSize enum to zoom percent
209 * SMALLEST is 50%
210 * SMALLER is 75%
211 * NORMAL is 100%
212 * LARGER is 150%
213 * LARGEST is 200%
214 */
215 switch (getTextSize()) {
216 case SMALLEST:
217 setTextZoom(50);
218 break;
219 case SMALLER:
220 setTextZoom(75);
221 break;
222 case LARGER:
223 setTextZoom(150);
224 break;
225 case LARGEST:
226 setTextZoom(200);
227 break;
228 }
229 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
230 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100231
qqzhou8c5b0a32013-07-22 15:31:03 +0800232 // add for carrier homepage feature
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700233 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
qqzhou8c5b0a32013-07-22 15:31:03 +0800234
kaiyizbf086ea2013-08-02 11:03:58 +0800235 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700236 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING, "UTF-8").apply();
kaiyizbf086ea2013-08-02 11:03:58 +0800237 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700238
Ben Murdochaaa1f372011-07-25 15:40:58 +0100239 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
240 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
241 BrowserProvider.getClientId(mContext.getContentResolver()));
242 }
243
Ben Murdochaaa1f372011-07-25 15:40:58 +0100244 synchronized (BrowserSettings.class) {
245 sInitialized = true;
246 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700247 }
248 }
249 };
250
Ben Murdochaaa1f372011-07-25 15:40:58 +0100251 private static void requireInitialization() {
252 synchronized (BrowserSettings.class) {
253 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700254 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100255 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700256 } catch (InterruptedException e) {
257 }
258 }
259 }
260 }
261
The Android Open Source Project0c908882009-03-03 19:32:16 -0800262 /**
John Reck35e9dd62011-04-25 09:01:54 -0700263 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800264 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800265 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700266 settings.setGeolocationEnabled(enableGeolocation());
267 settings.setJavaScriptEnabled(enableJavascript());
268 settings.setLightTouchEnabled(enableLightTouch());
269 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700270 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
271 settings.setDefaultZoom(getDefaultZoom());
272 settings.setMinimumFontSize(getMinimumFontSize());
273 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck7dc444b2011-06-16 17:44:29 -0700274 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700275 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100276 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700277 settings.setLoadsImagesAutomatically(loadImages());
278 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
279 settings.setSavePassword(rememberPasswords());
280 settings.setSaveFormData(saveFormdata());
281 settings.setUseWideViewPort(isWideViewport());
Panos Thomasb10bbda2014-06-23 10:18:36 -0700282 settings.setDoNotTrack(doNotTrack());
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700283 setUserAgent(settings);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700284 settings.setMediaPlaybackRequiresUserGesture(false);
285 settings.setAllowMediaDownloads(allowMediaDownloads());
286 setExtraHTTPRequestHeaders(settings);
John Reck2fd9d0e2011-07-15 11:13:48 -0700287
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800288 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800289 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
290 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
291 settingsClassic.setForceUserScalable(forceEnableUserScalable());
292 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
293 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800294
John Reckea17e782011-10-27 17:15:59 -0700295 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800296 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700297 useInverted ? "true" : "false");
298 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800299 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700300 Float.toString(getInvertedContrast()));
301 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700302
John Reckea17e782011-10-27 17:15:59 -0700303 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800304 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700305 enableCpuUploadPath() ? "true" : "false");
306 }
Victoria Lease96497832012-03-23 14:19:56 -0700307
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800308 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000309 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800310
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700311 private void setUserAgent(WebSettings settings){
312 String ua = mContext.getResources().getString(R.string.def_useragent);
313
314 if (!TextUtils.isEmpty(ua))
315 ua = constructUserAgent(ua);
316
317 if (TextUtils.isEmpty(ua))
318 ua = mCustomUserAgents.get(settings);
319
320 if (!TextUtils.isEmpty(ua)){
321 settings.setUserAgentString(ua);
322 } else {
323 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
324 }
325 }
326
Tarun Nainani8eb00912014-07-17 12:28:32 -0700327 private void setExtraHTTPRequestHeaders(WebSettings settings){
328 String headers = mContext.getResources().getString(R.string.def_extra_http_headers);
329 if (!TextUtils.isEmpty(headers)){
330 settings.setHTTPRequestHeaders(headers);
331 }
332 }
333
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700334 private String constructUserAgent(String userAgent) {
335 try {
336 userAgent = userAgent.replaceAll("<%build_model>", Build.MODEL);
337 userAgent = userAgent.replaceAll("<%build_version>", Build.VERSION.RELEASE);
338 userAgent = userAgent.replaceAll("<%build_id>", Build.ID);
339 userAgent = userAgent.replaceAll("<%language>", Locale.getDefault().getLanguage());
340 userAgent = userAgent.replaceAll("<%country>", Locale.getDefault().getCountry());
341 return userAgent;
342 } catch (Exception ex) {
343 return null;
344 }
345 }
346
John Reck35e9dd62011-04-25 09:01:54 -0700347 /**
348 * Syncs all the settings that have no UI
349 * These cannot change, so we only need to set them once per WebSettings
350 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800351 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700352 settings.setDefaultFontSize(16);
353 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000354
John Reck35e9dd62011-04-25 09:01:54 -0700355 // WebView inside Browser doesn't want initial focus to be set.
356 settings.setNeedInitialFocus(false);
357 // Browser supports multiple windows
358 settings.setSupportMultipleWindows(true);
359 // enable smooth transition for better performance during panning or
360 // zooming
361 settings.setEnableSmoothTransition(true);
362 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700363 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700364
365 // HTML5 API flags
366 settings.setAppCacheEnabled(true);
367 settings.setDatabaseEnabled(true);
368 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700369
370 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700371 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700372 settings.setAppCachePath(getAppCachePath());
373 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
374 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700375 // origin policy for file access
376 settings.setAllowUniversalAccessFromFileURLs(false);
377 settings.setAllowFileAccessFromFileURLs(false);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800378
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800379 //if (!(settings instanceof WebSettingsClassic)) return;
380 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800381
382 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
383 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
384 // WebView should be preserving the memory as much as possible.
385 // However, apps like browser wish to turn on the performance mode which
386 // would require more memory.
387 // TODO: We need to dynamically allocate/deallocate temporary memory for
388 // apps which are trying to use minimal memory. Currently, double
389 // buffering is always turned on, which is unnecessary.
390 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
391 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800392 */
John Reck35e9dd62011-04-25 09:01:54 -0700393 }
394
395 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700396 mNeedsSharedSync = false;
Axesh R. Ajmera579c70c2014-04-17 13:24:56 -0700397 CookieManager.getInstance().setAcceptCookie(acceptCookies());
398
John Reck35e9dd62011-04-25 09:01:54 -0700399 if (mController != null) {
400 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700401 }
John Reck35e9dd62011-04-25 09:01:54 -0700402 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700403
John Reck35e9dd62011-04-25 09:01:54 -0700404 private void syncManagedSettings() {
405 syncSharedSettings();
406 synchronized (mManagedSettings) {
407 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
408 while (iter.hasNext()) {
409 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800410 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700411 if (settings == null) {
412 iter.remove();
413 continue;
414 }
415 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000416 }
John Reck35e9dd62011-04-25 09:01:54 -0700417 }
418 }
Ben Murdochef671652010-11-25 17:17:58 +0000419
John Reck35e9dd62011-04-25 09:01:54 -0700420 @Override
421 public void onSharedPreferenceChanged(
422 SharedPreferences sharedPreferences, String key) {
423 syncManagedSettings();
424 if (PREF_SEARCH_ENGINE.equals(key)) {
425 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700426 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200427 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700428 mController.getUi().setFullscreen(useFullscreen());
429 }
Michael Kolb0241e752011-07-07 14:58:50 -0700430 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200431 if (mController != null && mController.getUi() != null) {
Michael Kolb0241e752011-07-07 14:58:50 -0700432 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
433 }
Victoria Lease96497832012-03-23 14:19:56 -0700434 } else if (PREF_LINK_PREFETCH.equals(key)) {
435 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700436 }
John Reck35e9dd62011-04-25 09:01:54 -0700437 }
438
John Reck961d35d2011-06-23 09:45:54 -0700439 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100440 requireInitialization();
441 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700442 }
443
444 public LayoutAlgorithm getLayoutAlgorithm() {
445 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
446 if (autofitPages()) {
Tarun Nainani003bec52014-07-02 02:20:34 -0700447 layoutAlgorithm = LayoutAlgorithm.TEXT_AUTOSIZING;
John Reck35e9dd62011-04-25 09:01:54 -0700448 }
449 if (isDebugEnabled()) {
450 if (isSmallScreen()) {
451 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000452 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700453 if (isNormalLayout()) {
454 layoutAlgorithm = LayoutAlgorithm.NORMAL;
455 } else {
456 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
457 }
Ben Murdochef671652010-11-25 17:17:58 +0000458 }
John Reck35e9dd62011-04-25 09:01:54 -0700459 }
460 return layoutAlgorithm;
461 }
Ben Murdochef671652010-11-25 17:17:58 +0000462
John Reck35e9dd62011-04-25 09:01:54 -0700463 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700464 requireInitialization();
465 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800466 }
467
John Reck35e9dd62011-04-25 09:01:54 -0700468 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700469 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700470 return mWebStorageSizeManager;
471 }
472
John Reck35e9dd62011-04-25 09:01:54 -0700473 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700474 if (mAppCachePath == null) {
475 mAppCachePath = mContext.getDir("appcache", 0).getPath();
476 }
477 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700478 }
479
480 private void updateSearchEngine(boolean force) {
481 String searchEngineName = getSearchEngineName();
482 if (force || mSearchEngine == null ||
483 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700484 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000485 }
486 }
487
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100488 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700489 if (mSearchEngine == null) {
490 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100491 }
John Reck35e9dd62011-04-25 09:01:54 -0700492 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100493 }
494
John Reck35e9dd62011-04-25 09:01:54 -0700495 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700496 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700497 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100498 }
499
John Reck35e9dd62011-04-25 09:01:54 -0700500 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700501 Editor edit = mPrefs.edit();
502 edit.putBoolean(PREF_DEBUG_MENU, value);
503 if (!value) {
504 // Reset to "safe" value
505 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
506 }
507 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100508 }
509
John Reck35e9dd62011-04-25 09:01:54 -0700510 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800511 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700512 if (mController != null) {
513 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800514 if (current != null) {
515 current.clearCache(true);
516 }
517 }
518 }
519
John Reck35e9dd62011-04-25 09:01:54 -0700520 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800521 CookieManager.getInstance().removeAllCookie();
522 }
523
John Reck35e9dd62011-04-25 09:01:54 -0700524 public void clearHistory() {
525 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800526 Browser.clearHistory(resolver);
527 Browser.clearSearches(resolver);
528 }
529
John Reck35e9dd62011-04-25 09:01:54 -0700530 public void clearFormData() {
531 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700532 if (mController!= null) {
533 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100534 if (currentTopView != null) {
535 currentTopView.clearFormData();
536 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800537 }
538 }
539
Bijan Amirzada3bbb3a42014-06-17 12:06:18 -0700540 public WebView getTopWebView(){
541 if (mController!= null)
542 return mController.getCurrentTopWebView();
543
544 return null;
545 }
546
John Reck35e9dd62011-04-25 09:01:54 -0700547 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700548 // Clear password store maintained by SWE engine
549 WebSettings settings = null;
550 // find a valid settings object
551 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
552 while (iter.hasNext()) {
553 WeakReference<WebSettings> ref = iter.next();
554 settings = (WebSettings)ref.get();
555 if (settings != null) {
556 break;
557 }
558 }
559 if (settings != null) {
Panos Thomasdbaea4e2014-05-22 06:48:47 -0700560 settings.clearPasswords();
Panos Thomasfa948b82014-03-09 03:42:42 -0700561 }
Vivek Sekhare8a8ec22014-07-10 14:52:43 -0700562
563 // Clear passwords in WebView database
564 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
565 db.clearHttpAuthUsernamePassword();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800566 }
567
John Reck35e9dd62011-04-25 09:01:54 -0700568 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100569 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100570 }
571
John Reck35e9dd62011-04-25 09:01:54 -0700572 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100573 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100574 }
575
John Reck35e9dd62011-04-25 09:01:54 -0700576 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700577 mPrefs.edit()
578 .clear()
John Reckbd315192011-07-29 10:05:47 -0700579 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200580 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700581 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700582 }
583
Björn Isakssonc885a242012-06-05 17:19:04 +0200584 private void resetCachedValues() {
585 updateSearchEngine(false);
586 }
587
John Reck35e9dd62011-04-25 09:01:54 -0700588 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800589 // query the profile from components autofill database 524
590 if (mAutofillHandler.mAutoFillProfile == null &&
591 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
592 WebSettings settings = null;
593 // find a valid settings object
594 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
595 while (iter.hasNext()) {
596 WeakReference<WebSettings> ref = iter.next();
597 settings = (WebSettings)ref.get();
598 if (settings != null) {
599 break;
600 }
601 }
602 if (settings != null) {
603 AutoFillProfile profile =
604 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
605 mAutofillHandler.setAutoFillProfile(profile);
606 }
607 }
John Reck35e9dd62011-04-25 09:01:54 -0700608 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800609 }
610
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800611 public String getAutoFillProfileId() {
612 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800613 }
614
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800615 public void updateAutoFillProfile(AutoFillProfile profile) {
616 syncAutoFillProfile(profile);
617 }
618
619 private void syncAutoFillProfile(AutoFillProfile profile) {
620 synchronized (mManagedSettings) {
621 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
622 while (iter.hasNext()) {
623 WeakReference<WebSettings> ref = iter.next();
624 WebSettings settings = (WebSettings)ref.get();
625 if (settings == null) {
626 iter.remove();
627 continue;
628 }
629 // update the profile only once.
630 settings.setAutoFillProfile(profile);
631 // Now we should have the guid
632 mAutofillHandler.setAutoFillProfile(profile);
633 break;
634 }
635 }
636 }
John Reck35e9dd62011-04-25 09:01:54 -0700637 public void toggleDebugSettings() {
638 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800639 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100640
John Reckb8b2af82011-05-20 15:58:33 -0700641 public boolean hasDesktopUseragent(WebView view) {
642 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
643 }
644
645 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700646 if (view == null) {
647 return;
648 }
John Reckb8b2af82011-05-20 15:58:33 -0700649 WebSettings settings = view.getSettings();
650 if (mCustomUserAgents.get(settings) != null) {
651 mCustomUserAgents.remove(settings);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700652 setUserAgent(settings);
John Reckb8b2af82011-05-20 15:58:33 -0700653 } else {
654 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
655 settings.setUserAgentString(DESKTOP_USERAGENT);
656 }
657 }
658
John Reck7dc444b2011-06-16 17:44:29 -0700659 public static int getAdjustedMinimumFontSize(int rawValue) {
660 rawValue++; // Preference starts at 0, min font at 1
661 if (rawValue > 1) {
662 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
663 }
664 return rawValue;
665 }
666
John Reck5ba3c762011-09-14 15:00:15 -0700667 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700668 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700669 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700670 }
671
672 static int getRawTextZoom(int percent) {
673 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
674 }
675
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700676 public int getAdjustedDoubleTapZoom(int rawValue) {
677 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
678 return (int) ((rawValue + 100) * mFontSizeMult);
679 }
680
681 static int getRawDoubleTapZoom(int percent) {
682 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
683 }
684
John Reckcadae722011-07-25 11:36:17 -0700685 public SharedPreferences getPreferences() {
686 return mPrefs;
687 }
688
Victoria Lease96497832012-03-23 14:19:56 -0700689 // update connectivity-dependent options
690 public void updateConnectionType() {
691 ConnectivityManager cm = (ConnectivityManager)
692 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
693 String linkPrefetchPreference = getLinkPrefetchEnabled();
694 boolean linkPrefetchAllowed = linkPrefetchPreference.
695 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
696 NetworkInfo ni = cm.getActiveNetworkInfo();
697 if (ni != null) {
698 switch (ni.getType()) {
699 case ConnectivityManager.TYPE_WIFI:
700 case ConnectivityManager.TYPE_ETHERNET:
701 case ConnectivityManager.TYPE_BLUETOOTH:
702 linkPrefetchAllowed |= linkPrefetchPreference.
703 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
704 break;
705 case ConnectivityManager.TYPE_MOBILE:
706 case ConnectivityManager.TYPE_MOBILE_DUN:
707 case ConnectivityManager.TYPE_MOBILE_MMS:
708 case ConnectivityManager.TYPE_MOBILE_SUPL:
709 case ConnectivityManager.TYPE_WIMAX:
710 default:
711 break;
712 }
713 }
714 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
715 mLinkPrefetchAllowed = linkPrefetchAllowed;
716 syncManagedSettings();
717 }
718 }
719
luxiaol62677b02013-07-22 07:54:49 +0800720 public String getDownloadPath() {
721 return mPrefs.getString(PREF_DOWNLOAD_PATH,
722 DownloadHandler.getDefaultDownloadPath(mContext));
723 }
John Reck35e9dd62011-04-25 09:01:54 -0700724 // -----------------------------
725 // getter/setters for accessibility_preferences.xml
726 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100727
John Reck7dc444b2011-06-16 17:44:29 -0700728 @Deprecated
729 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700730 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
731 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100732 }
733
John Reck35e9dd62011-04-25 09:01:54 -0700734 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700735 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700736 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100737 }
738
John Reck92f25f82011-04-26 16:57:10 -0700739 public boolean forceEnableUserScalable() {
740 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
741 }
742
John Reck7dc444b2011-06-16 17:44:29 -0700743 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700744 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700745 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
746 return getAdjustedTextZoom(textZoom);
747 }
748
749 public void setTextZoom(int percent) {
750 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
751 }
752
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700753 public int getDoubleTapZoom() {
754 requireInitialization();
755 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
756 return getAdjustedDoubleTapZoom(doubleTapZoom);
757 }
758
759 public void setDoubleTapZoom(int percent) {
760 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
761 }
762
John Reck35e9dd62011-04-25 09:01:54 -0700763 // -----------------------------
764 // getter/setters for advanced_preferences.xml
765 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100766
John Reck35e9dd62011-04-25 09:01:54 -0700767 public String getSearchEngineName() {
luxiaol221b3932013-07-19 15:27:57 +0800768 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
769 if (defaultSearchEngineValue == null) {
770 defaultSearchEngineValue = SearchEngine.GOOGLE;
771 }
772 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100773 }
John Reck63bb6872010-12-01 19:29:32 -0800774
Michael Kolb8d772b02012-01-19 13:56:00 -0800775 public boolean allowAppTabs() {
776 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
777 }
778
John Reck35e9dd62011-04-25 09:01:54 -0700779 public boolean openInBackground() {
780 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800781 }
John Reck35e9dd62011-04-25 09:01:54 -0700782
783 public boolean enableJavascript() {
784 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
785 }
786
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800787 public boolean enableMemoryMonitor() {
788 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
789 }
790
Tarun Nainani8eb00912014-07-17 12:28:32 -0700791 public boolean allowMediaDownloads() {
792 boolean enableMediaDownloads = mController.getContext().getResources().getBoolean(
793 R.bool.def_enable_media_downloads);
794 boolean shouldAllowMediaDownloads = mPrefs.getBoolean(
795 PREF_ALLOW_MEDIA_DOWNLOADS, enableMediaDownloads);
796
797 if(!mPrefs.contains(PREF_ALLOW_MEDIA_DOWNLOADS)){
798 Editor edit = mPrefs.edit();
799 edit.putBoolean(PREF_ALLOW_MEDIA_DOWNLOADS, shouldAllowMediaDownloads);
800 edit.apply();
801 }
802
803 return shouldAllowMediaDownloads;
804 }
805
John Reck35e9dd62011-04-25 09:01:54 -0700806 // TODO: Cache
John Reck35e9dd62011-04-25 09:01:54 -0700807 public ZoomDensity getDefaultZoom() {
808 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700809 ZoomDensity zoomDensity = ZoomDensity.valueOf(zoom);
810 //SWE: If zoomDensity is not MEDIUM, set enableWideViewport to false.
811 if(zoomDensity != ZoomDensity.MEDIUM)
812 enableWideViewport = false;
813 else
814 enableWideViewport = true;
815 return zoomDensity;
John Reck35e9dd62011-04-25 09:01:54 -0700816 }
817
818 public boolean loadPageInOverviewMode() {
819 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
820 }
821
822 public boolean autofitPages() {
823 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
824 }
825
826 public boolean blockPopupWindows() {
827 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
828 }
829
830 public boolean loadImages() {
831 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
832 }
833
834 public String getDefaultTextEncoding() {
835 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
836 }
837
838 // -----------------------------
839 // getter/setters for general_preferences.xml
840 // -----------------------------
841
842 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700843 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
844 }
845
846 public void setHomePage(String value) {
847 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
848 }
849
850 public boolean isAutofillEnabled() {
851 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
852 }
853
854 public void setAutofillEnabled(boolean value) {
855 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
856 }
857
858 // -----------------------------
859 // getter/setters for debug_preferences.xml
860 // -----------------------------
861
862 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700863 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700864 return true;
865 }
866 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
867 }
868
Derek Sollenberger31adf672011-07-08 11:31:30 -0400869 public boolean isSkiaHardwareAccelerated() {
870 if (!isDebugEnabled()) {
871 return false;
872 }
873 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
874 }
875
John Reck35e9dd62011-04-25 09:01:54 -0700876 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700877 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700878 return 0;
879 }
880 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
881 }
882
883 // -----------------------------
884 // getter/setters for hidden_debug_preferences.xml
885 // -----------------------------
886
887 public boolean enableVisualIndicator() {
888 if (!isDebugEnabled()) {
889 return false;
890 }
891 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
892 }
893
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700894 public boolean enableCpuUploadPath() {
895 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700896 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700897 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700898 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700899 }
900
John Reck35e9dd62011-04-25 09:01:54 -0700901 public boolean enableJavascriptConsole() {
902 if (!isDebugEnabled()) {
903 return false;
904 }
905 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
906 }
907
908 public boolean isSmallScreen() {
909 if (!isDebugEnabled()) {
910 return false;
911 }
912 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
913 }
914
915 public boolean isWideViewport() {
916 if (!isDebugEnabled()) {
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700917 return enableWideViewport;
John Reck35e9dd62011-04-25 09:01:54 -0700918 }
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700919 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, enableWideViewport);
John Reck35e9dd62011-04-25 09:01:54 -0700920 }
921
922 public boolean isNormalLayout() {
923 if (!isDebugEnabled()) {
924 return false;
925 }
926 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
927 }
928
929 public boolean isTracing() {
930 if (!isDebugEnabled()) {
931 return false;
932 }
933 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
934 }
935
936 public boolean enableLightTouch() {
937 if (!isDebugEnabled()) {
938 return false;
939 }
940 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
941 }
942
943 public boolean enableNavDump() {
944 if (!isDebugEnabled()) {
945 return false;
946 }
947 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
948 }
949
950 public String getJsEngineFlags() {
951 if (!isDebugEnabled()) {
952 return "";
953 }
954 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
955 }
956
957 // -----------------------------
958 // getter/setters for lab_preferences.xml
959 // -----------------------------
960
961 public boolean useQuickControls() {
962 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
963 }
964
965 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700966 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700967 }
968
Michael Kolbc38c6042011-04-27 10:46:06 -0700969 public boolean useFullscreen() {
970 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
971 }
972
John Reck2fd9d0e2011-07-15 11:13:48 -0700973 public boolean useInvertedRendering() {
974 return mPrefs.getBoolean(PREF_INVERTED, false);
975 }
976
Nicolas Roard5d513102011-08-03 15:35:34 -0700977 public float getInvertedContrast() {
978 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
979 }
980
John Reck35e9dd62011-04-25 09:01:54 -0700981 // -----------------------------
982 // getter/setters for privacy_security_preferences.xml
983 // -----------------------------
984
985 public boolean showSecurityWarnings() {
986 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
987 }
988
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700989 public boolean doNotTrack() {
990 return mPrefs.getBoolean(PREF_DO_NOT_TRACK, true);
991 }
992
John Reck35e9dd62011-04-25 09:01:54 -0700993 public boolean acceptCookies() {
994 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
995 }
996
997 public boolean saveFormdata() {
998 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
999 }
1000
1001 public boolean enableGeolocation() {
1002 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
1003 }
1004
1005 public boolean rememberPasswords() {
1006 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
1007 }
1008
Michael Kolb14612442011-06-24 13:06:29 -07001009 // -----------------------------
1010 // getter/setters for bandwidth_preferences.xml
1011 // -----------------------------
1012
Mathew Inwood467813f2011-09-02 15:43:17 +01001013 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
1014 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -07001015 }
Mathew Inwood467813f2011-09-02 15:43:17 +01001016
1017 public static String getPreloadAlwaysPreferenceString(Context context) {
1018 return context.getResources().getString(R.string.pref_data_preload_value_always);
1019 }
1020
Mathew Inwood825fba72011-10-04 14:52:52 +01001021 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1022 "browser_default_preload_setting";
1023
1024 public String getDefaultPreloadSetting() {
1025 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1026 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1027 if (preload == null) {
1028 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1029 }
1030 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001031 }
1032
1033 public String getPreloadEnabled() {
1034 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1035 }
1036
Victoria Lease96497832012-03-23 14:19:56 -07001037 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1038 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1039 }
1040
1041 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1042 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1043 }
1044
1045 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1046 "browser_default_link_prefetch_setting";
1047
1048 public String getDefaultLinkPrefetchSetting() {
1049 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1050 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1051 if (preload == null) {
1052 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1053 }
1054 return preload;
1055 }
1056
1057 public String getLinkPrefetchEnabled() {
1058 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1059 }
1060
George Mount3636d0a2011-11-21 09:08:21 -08001061 // -----------------------------
1062 // getter/setters for browser recovery
1063 // -----------------------------
1064 /**
1065 * The last time browser was started.
1066 * @return The last browser start time as System.currentTimeMillis. This
1067 * can be 0 if this is the first time or the last tab was closed.
1068 */
1069 public long getLastRecovered() {
1070 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1071 }
1072
1073 /**
1074 * Sets the last browser start time.
1075 * @param time The last time as System.currentTimeMillis that the browser
1076 * was started. This should be set to 0 if the last tab is closed.
1077 */
1078 public void setLastRecovered(long time) {
1079 mPrefs.edit()
1080 .putLong(KEY_LAST_RECOVERED, time)
1081 .apply();
1082 }
1083
1084 /**
1085 * Used to determine whether or not the previous browser run crashed. Once
1086 * the previous state has been determined, the value will be set to false
1087 * until a pause is received.
1088 * @return true if the last browser run was paused or false if it crashed.
1089 */
1090 public boolean wasLastRunPaused() {
1091 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1092 }
1093
1094 /**
1095 * Sets whether or not the last run was a pause or crash.
1096 * @param isPaused Set to true When a pause is received or false after
1097 * resuming.
1098 */
1099 public void setLastRunPaused(boolean isPaused) {
1100 mPrefs.edit()
1101 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1102 .apply();
1103 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001104}