blob: 255aaf82136dabb098b9df8ceeacf4fa38882f11 [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;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070035import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070036import com.android.browser.provider.BrowserProvider;
Bijan Amirzada41242f22014-03-21 12:12:18 -070037import com.android.browser.search.SearchEngine;
38import com.android.browser.search.SearchEngines;
John Reck46500332011-06-07 14:36:10 -070039
John Reck35e9dd62011-04-25 09:01:54 -070040import java.lang.ref.WeakReference;
41import java.util.Iterator;
42import java.util.LinkedList;
qqzhou8c5b0a32013-07-22 15:31:03 +080043import java.util.Locale;
John Reckb8b2af82011-05-20 15:58:33 -070044import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080046import org.codeaurora.swe.AutoFillProfile;
47import org.codeaurora.swe.CookieManager;
48import org.codeaurora.swe.GeolocationPermissions;
49import org.codeaurora.swe.WebSettings.LayoutAlgorithm;
50import org.codeaurora.swe.WebSettings.PluginState;
51import org.codeaurora.swe.WebSettings.TextSize;
52import org.codeaurora.swe.WebSettings.ZoomDensity;
53import org.codeaurora.swe.WebSettings;
54import org.codeaurora.swe.WebView;
Vivek Sekhare8a8ec22014-07-10 14:52:43 -070055import org.codeaurora.swe.WebViewDatabase;
Panos Thomas3f35d8d2014-11-08 22:40:23 -080056
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;
John Reck78a6a1d2011-07-21 13:54:03 -0700120
Victoria Lease96497832012-03-23 14:19:56 -0700121 // Current state of network-dependent settings
122 private boolean mLinkPrefetchAllowed = true;
123
John Reck78a6a1d2011-07-21 13:54:03 -0700124 // Cached values
125 private int mPageCacheCapacity = 1;
126 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800127
John Reck35e9dd62011-04-25 09:01:54 -0700128 // Cached settings
129 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800130
Ben Murdochaaa1f372011-07-25 15:40:58 +0100131 private static String sFactoryResetUrl;
132
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700133 private boolean mEngineInitialized = false;
134 private boolean mSyncManagedSettings = false;
135
Bijan Amirzada3bb46302014-06-17 16:31:31 -0700136 public static synchronized void initialize(final Context context) {
137 if (sInstance == null)
138 sInstance = new BrowserSettings(context);
John Reck35e9dd62011-04-25 09:01:54 -0700139 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800140
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700141 public static BrowserSettings getInstance() {
John Reck35e9dd62011-04-25 09:01:54 -0700142 return sInstance;
143 }
Ben Murdochef671652010-11-25 17:17:58 +0000144
John Reck35e9dd62011-04-25 09:01:54 -0700145 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100146 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700147 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
John Reck35e9dd62011-04-25 09:01:54 -0700148 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700149 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reckcadae722011-07-25 11:36:17 -0700150 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700151 }
152
153 public void setController(Controller controller) {
154 mController = controller;
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700155 mNeedsSharedSync = true;
156 }
157
158 public void onEngineInitializationComplete() {
159 mEngineInitialized = true;
160 mAutofillHandler = new AutofillHandler(mContext);
161 if (mSyncManagedSettings) {
162 syncManagedSettings();
163 }
164 if (mNeedsSharedSync) {
John Reckc477e712011-08-17 11:27:15 -0700165 syncSharedSettings();
166 }
Ben Murdochef671652010-11-25 17:17:58 +0000167 }
168
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800169 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800170
John Reckc477e712011-08-17 11:27:15 -0700171 if (mNeedsSharedSync) {
172 syncSharedSettings();
173 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800174
John Reck35e9dd62011-04-25 09:01:54 -0700175 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800176 syncStaticSettings(settings);
177 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700178 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800179 }
180 }
181
John Reckd1d87312012-03-08 13:25:00 -0800182 public void stopManagingSettings(WebSettings settings) {
183 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
184 while (iter.hasNext()) {
185 WeakReference<WebSettings> ref = iter.next();
186 if (ref.get() == settings) {
187 iter.remove();
188 return;
189 }
190 }
191 }
192
John Reckcadae722011-07-25 11:36:17 -0700193 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700194
195 @Override
196 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700197 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
198 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700199 // the cost of one cached page is ~3M (measured using nytimes.com). For
200 // low end devices, we only cache one page. For high end devices, we try
201 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800202
203 // SWE_TODO : assume a high-memory device
204 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700205 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800206 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700207 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
208 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
209 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700210 // Workaround b/5254577
211 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700212 if (Build.VERSION.CODENAME.equals("REL")) {
213 // This is a release build, always startup with debug disabled
214 setDebugEnabled(false);
215 }
216 if (mPrefs.contains(PREF_TEXT_SIZE)) {
217 /*
218 * Update from TextSize enum to zoom percent
219 * SMALLEST is 50%
220 * SMALLER is 75%
221 * NORMAL is 100%
222 * LARGER is 150%
223 * LARGEST is 200%
224 */
225 switch (getTextSize()) {
226 case SMALLEST:
227 setTextZoom(50);
228 break;
229 case SMALLER:
230 setTextZoom(75);
231 break;
232 case LARGER:
233 setTextZoom(150);
234 break;
235 case LARGEST:
236 setTextZoom(200);
237 break;
238 }
239 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
240 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100241
qqzhou8c5b0a32013-07-22 15:31:03 +0800242 // add for carrier homepage feature
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700243 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
qqzhou8c5b0a32013-07-22 15:31:03 +0800244
kaiyizbf086ea2013-08-02 11:03:58 +0800245 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700246 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING, "UTF-8").apply();
kaiyizbf086ea2013-08-02 11:03:58 +0800247 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700248
Ben Murdochaaa1f372011-07-25 15:40:58 +0100249 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
250 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
251 BrowserProvider.getClientId(mContext.getContentResolver()));
252 }
253
Ben Murdochaaa1f372011-07-25 15:40:58 +0100254 synchronized (BrowserSettings.class) {
255 sInitialized = true;
256 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700257 }
258 }
259 };
260
Ben Murdochaaa1f372011-07-25 15:40:58 +0100261 private static void requireInitialization() {
262 synchronized (BrowserSettings.class) {
263 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700264 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100265 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700266 } catch (InterruptedException e) {
267 }
268 }
269 }
270 }
271
The Android Open Source Project0c908882009-03-03 19:32:16 -0800272 /**
John Reck35e9dd62011-04-25 09:01:54 -0700273 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800274 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800275 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700276 settings.setGeolocationEnabled(enableGeolocation());
277 settings.setJavaScriptEnabled(enableJavascript());
278 settings.setLightTouchEnabled(enableLightTouch());
279 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700280 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
281 settings.setDefaultZoom(getDefaultZoom());
282 settings.setMinimumFontSize(getMinimumFontSize());
283 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck7dc444b2011-06-16 17:44:29 -0700284 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700285 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100286 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700287 settings.setLoadsImagesAutomatically(loadImages());
288 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
289 settings.setSavePassword(rememberPasswords());
290 settings.setSaveFormData(saveFormdata());
291 settings.setUseWideViewPort(isWideViewport());
Panos Thomasb10bbda2014-06-23 10:18:36 -0700292 settings.setDoNotTrack(doNotTrack());
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700293 setUserAgent(settings);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700294 settings.setMediaPlaybackRequiresUserGesture(false);
295 settings.setAllowMediaDownloads(allowMediaDownloads());
296 setExtraHTTPRequestHeaders(settings);
John Reck2fd9d0e2011-07-15 11:13:48 -0700297
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800298 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800299 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
300 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
301 settingsClassic.setForceUserScalable(forceEnableUserScalable());
302 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
303 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800304
John Reckea17e782011-10-27 17:15:59 -0700305 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800306 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700307 useInverted ? "true" : "false");
308 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800309 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700310 Float.toString(getInvertedContrast()));
311 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700312
John Reckea17e782011-10-27 17:15:59 -0700313 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800314 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700315 enableCpuUploadPath() ? "true" : "false");
316 }
Victoria Lease96497832012-03-23 14:19:56 -0700317
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800318 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000319 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800320
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700321 private void setUserAgent(WebSettings settings){
322 String ua = mContext.getResources().getString(R.string.def_useragent);
323
324 if (!TextUtils.isEmpty(ua))
325 ua = constructUserAgent(ua);
326
327 if (TextUtils.isEmpty(ua))
328 ua = mCustomUserAgents.get(settings);
329
330 if (!TextUtils.isEmpty(ua)){
331 settings.setUserAgentString(ua);
332 } else {
333 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
334 }
335 }
336
Tarun Nainani8eb00912014-07-17 12:28:32 -0700337 private void setExtraHTTPRequestHeaders(WebSettings settings){
338 String headers = mContext.getResources().getString(R.string.def_extra_http_headers);
339 if (!TextUtils.isEmpty(headers)){
340 settings.setHTTPRequestHeaders(headers);
341 }
342 }
343
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700344 private String constructUserAgent(String userAgent) {
345 try {
346 userAgent = userAgent.replaceAll("<%build_model>", Build.MODEL);
347 userAgent = userAgent.replaceAll("<%build_version>", Build.VERSION.RELEASE);
348 userAgent = userAgent.replaceAll("<%build_id>", Build.ID);
349 userAgent = userAgent.replaceAll("<%language>", Locale.getDefault().getLanguage());
350 userAgent = userAgent.replaceAll("<%country>", Locale.getDefault().getCountry());
351 return userAgent;
352 } catch (Exception ex) {
353 return null;
354 }
355 }
356
John Reck35e9dd62011-04-25 09:01:54 -0700357 /**
358 * Syncs all the settings that have no UI
359 * These cannot change, so we only need to set them once per WebSettings
360 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800361 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700362 settings.setDefaultFontSize(16);
363 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000364
John Reck35e9dd62011-04-25 09:01:54 -0700365 // WebView inside Browser doesn't want initial focus to be set.
366 settings.setNeedInitialFocus(false);
367 // Browser supports multiple windows
368 settings.setSupportMultipleWindows(true);
369 // enable smooth transition for better performance during panning or
370 // zooming
371 settings.setEnableSmoothTransition(true);
372 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700373 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700374
375 // HTML5 API flags
376 settings.setAppCacheEnabled(true);
377 settings.setDatabaseEnabled(true);
378 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700379
380 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700381 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700382 settings.setAppCachePath(getAppCachePath());
383 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
384 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700385 // origin policy for file access
386 settings.setAllowUniversalAccessFromFileURLs(false);
387 settings.setAllowFileAccessFromFileURLs(false);
Sudheer Koganti24766882014-10-02 10:58:09 -0700388 settings.setFullscreenSupported(true);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800389
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800390 //if (!(settings instanceof WebSettingsClassic)) return;
391 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800392
393 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
394 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
395 // WebView should be preserving the memory as much as possible.
396 // However, apps like browser wish to turn on the performance mode which
397 // would require more memory.
398 // TODO: We need to dynamically allocate/deallocate temporary memory for
399 // apps which are trying to use minimal memory. Currently, double
400 // buffering is always turned on, which is unnecessary.
401 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
402 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800403 */
John Reck35e9dd62011-04-25 09:01:54 -0700404 }
405
406 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700407 mNeedsSharedSync = false;
Axesh R. Ajmera579c70c2014-04-17 13:24:56 -0700408 CookieManager.getInstance().setAcceptCookie(acceptCookies());
409
John Reck35e9dd62011-04-25 09:01:54 -0700410 if (mController != null) {
411 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700412 }
John Reck35e9dd62011-04-25 09:01:54 -0700413 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700414
John Reck35e9dd62011-04-25 09:01:54 -0700415 private void syncManagedSettings() {
Kulanthaivel Palanichamy77942682014-10-28 11:52:06 -0700416 if (!mEngineInitialized) {
417 mSyncManagedSettings = true;
418 return;
419 }
420 mSyncManagedSettings = false;
John Reck35e9dd62011-04-25 09:01:54 -0700421 syncSharedSettings();
422 synchronized (mManagedSettings) {
423 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
424 while (iter.hasNext()) {
425 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800426 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700427 if (settings == null) {
428 iter.remove();
429 continue;
430 }
431 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000432 }
John Reck35e9dd62011-04-25 09:01:54 -0700433 }
434 }
Ben Murdochef671652010-11-25 17:17:58 +0000435
John Reck35e9dd62011-04-25 09:01:54 -0700436 @Override
437 public void onSharedPreferenceChanged(
438 SharedPreferences sharedPreferences, String key) {
439 syncManagedSettings();
440 if (PREF_SEARCH_ENGINE.equals(key)) {
441 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700442 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200443 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700444 mController.getUi().setFullscreen(useFullscreen());
445 }
Victoria Lease96497832012-03-23 14:19:56 -0700446 } else if (PREF_LINK_PREFETCH.equals(key)) {
447 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700448 }
John Reck35e9dd62011-04-25 09:01:54 -0700449 }
450
John Reck961d35d2011-06-23 09:45:54 -0700451 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100452 requireInitialization();
453 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700454 }
455
456 public LayoutAlgorithm getLayoutAlgorithm() {
457 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
458 if (autofitPages()) {
Tarun Nainani003bec52014-07-02 02:20:34 -0700459 layoutAlgorithm = LayoutAlgorithm.TEXT_AUTOSIZING;
John Reck35e9dd62011-04-25 09:01:54 -0700460 }
461 if (isDebugEnabled()) {
462 if (isSmallScreen()) {
463 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000464 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700465 if (isNormalLayout()) {
466 layoutAlgorithm = LayoutAlgorithm.NORMAL;
467 } else {
468 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
469 }
Ben Murdochef671652010-11-25 17:17:58 +0000470 }
John Reck35e9dd62011-04-25 09:01:54 -0700471 }
472 return layoutAlgorithm;
473 }
Ben Murdochef671652010-11-25 17:17:58 +0000474
John Reck35e9dd62011-04-25 09:01:54 -0700475 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700476 requireInitialization();
477 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800478 }
479
John Reck35e9dd62011-04-25 09:01:54 -0700480 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700481 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700482 return mWebStorageSizeManager;
483 }
484
John Reck35e9dd62011-04-25 09:01:54 -0700485 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700486 if (mAppCachePath == null) {
487 mAppCachePath = mContext.getDir("appcache", 0).getPath();
488 }
489 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700490 }
491
492 private void updateSearchEngine(boolean force) {
493 String searchEngineName = getSearchEngineName();
494 if (force || mSearchEngine == null ||
495 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700496 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000497 }
498 }
499
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100500 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700501 if (mSearchEngine == null) {
502 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100503 }
John Reck35e9dd62011-04-25 09:01:54 -0700504 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100505 }
506
John Reck35e9dd62011-04-25 09:01:54 -0700507 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700508 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700509 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100510 }
511
John Reck35e9dd62011-04-25 09:01:54 -0700512 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700513 Editor edit = mPrefs.edit();
514 edit.putBoolean(PREF_DEBUG_MENU, value);
515 if (!value) {
516 // Reset to "safe" value
517 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
518 }
519 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100520 }
521
John Reck35e9dd62011-04-25 09:01:54 -0700522 public void clearCache() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700523 if (mController != null) {
524 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800525 if (current != null) {
526 current.clearCache(true);
527 }
528 }
529 }
530
John Reck35e9dd62011-04-25 09:01:54 -0700531 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800532 CookieManager.getInstance().removeAllCookie();
533 }
534
John Reck35e9dd62011-04-25 09:01:54 -0700535 public void clearHistory() {
536 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800537 Browser.clearHistory(resolver);
538 Browser.clearSearches(resolver);
539 }
540
John Reck35e9dd62011-04-25 09:01:54 -0700541 public void clearFormData() {
542 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700543 if (mController!= null) {
544 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100545 if (currentTopView != null) {
546 currentTopView.clearFormData();
547 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800548 }
549 }
550
Bijan Amirzada3bbb3a42014-06-17 12:06:18 -0700551 public WebView getTopWebView(){
552 if (mController!= null)
553 return mController.getCurrentTopWebView();
554
555 return null;
556 }
557
John Reck35e9dd62011-04-25 09:01:54 -0700558 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700559 // Clear password store maintained by SWE engine
560 WebSettings settings = null;
561 // find a valid settings object
562 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
563 while (iter.hasNext()) {
564 WeakReference<WebSettings> ref = iter.next();
565 settings = (WebSettings)ref.get();
566 if (settings != null) {
567 break;
568 }
569 }
570 if (settings != null) {
Panos Thomasdbaea4e2014-05-22 06:48:47 -0700571 settings.clearPasswords();
Panos Thomasfa948b82014-03-09 03:42:42 -0700572 }
Vivek Sekhare8a8ec22014-07-10 14:52:43 -0700573
574 // Clear passwords in WebView database
575 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
576 db.clearHttpAuthUsernamePassword();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800577 }
578
John Reck35e9dd62011-04-25 09:01:54 -0700579 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100580 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100581 }
582
John Reck35e9dd62011-04-25 09:01:54 -0700583 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100584 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100585 }
586
John Reck35e9dd62011-04-25 09:01:54 -0700587 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700588 mPrefs.edit()
589 .clear()
John Reckbd315192011-07-29 10:05:47 -0700590 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200591 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700592 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700593 }
594
Björn Isakssonc885a242012-06-05 17:19:04 +0200595 private void resetCachedValues() {
596 updateSearchEngine(false);
597 }
598
John Reck35e9dd62011-04-25 09:01:54 -0700599 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800600 // query the profile from components autofill database 524
601 if (mAutofillHandler.mAutoFillProfile == null &&
602 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
603 WebSettings settings = null;
604 // find a valid settings object
605 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
606 while (iter.hasNext()) {
607 WeakReference<WebSettings> ref = iter.next();
608 settings = (WebSettings)ref.get();
609 if (settings != null) {
610 break;
611 }
612 }
613 if (settings != null) {
614 AutoFillProfile profile =
615 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
616 mAutofillHandler.setAutoFillProfile(profile);
617 }
618 }
John Reck35e9dd62011-04-25 09:01:54 -0700619 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800620 }
621
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800622 public String getAutoFillProfileId() {
623 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800624 }
625
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800626 public void updateAutoFillProfile(AutoFillProfile profile) {
627 syncAutoFillProfile(profile);
628 }
629
630 private void syncAutoFillProfile(AutoFillProfile profile) {
631 synchronized (mManagedSettings) {
632 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
633 while (iter.hasNext()) {
634 WeakReference<WebSettings> ref = iter.next();
635 WebSettings settings = (WebSettings)ref.get();
636 if (settings == null) {
637 iter.remove();
638 continue;
639 }
640 // update the profile only once.
641 settings.setAutoFillProfile(profile);
642 // Now we should have the guid
643 mAutofillHandler.setAutoFillProfile(profile);
644 break;
645 }
646 }
647 }
John Reck35e9dd62011-04-25 09:01:54 -0700648 public void toggleDebugSettings() {
649 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800650 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100651
John Reckb8b2af82011-05-20 15:58:33 -0700652 public boolean hasDesktopUseragent(WebView view) {
653 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
654 }
655
656 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700657 if (view == null) {
658 return;
659 }
John Reckb8b2af82011-05-20 15:58:33 -0700660 WebSettings settings = view.getSettings();
661 if (mCustomUserAgents.get(settings) != null) {
662 mCustomUserAgents.remove(settings);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700663 setUserAgent(settings);
John Reckb8b2af82011-05-20 15:58:33 -0700664 } else {
665 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
666 settings.setUserAgentString(DESKTOP_USERAGENT);
667 }
668 }
669
John Reck7dc444b2011-06-16 17:44:29 -0700670 public static int getAdjustedMinimumFontSize(int rawValue) {
671 rawValue++; // Preference starts at 0, min font at 1
672 if (rawValue > 1) {
673 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
674 }
675 return rawValue;
676 }
677
John Reck5ba3c762011-09-14 15:00:15 -0700678 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700679 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700680 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700681 }
682
683 static int getRawTextZoom(int percent) {
684 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
685 }
686
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700687 public int getAdjustedDoubleTapZoom(int rawValue) {
688 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
689 return (int) ((rawValue + 100) * mFontSizeMult);
690 }
691
692 static int getRawDoubleTapZoom(int percent) {
693 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
694 }
695
John Reckcadae722011-07-25 11:36:17 -0700696 public SharedPreferences getPreferences() {
697 return mPrefs;
698 }
699
Victoria Lease96497832012-03-23 14:19:56 -0700700 // update connectivity-dependent options
701 public void updateConnectionType() {
702 ConnectivityManager cm = (ConnectivityManager)
703 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
704 String linkPrefetchPreference = getLinkPrefetchEnabled();
705 boolean linkPrefetchAllowed = linkPrefetchPreference.
706 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
707 NetworkInfo ni = cm.getActiveNetworkInfo();
708 if (ni != null) {
709 switch (ni.getType()) {
710 case ConnectivityManager.TYPE_WIFI:
711 case ConnectivityManager.TYPE_ETHERNET:
712 case ConnectivityManager.TYPE_BLUETOOTH:
713 linkPrefetchAllowed |= linkPrefetchPreference.
714 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
715 break;
716 case ConnectivityManager.TYPE_MOBILE:
717 case ConnectivityManager.TYPE_MOBILE_DUN:
718 case ConnectivityManager.TYPE_MOBILE_MMS:
719 case ConnectivityManager.TYPE_MOBILE_SUPL:
720 case ConnectivityManager.TYPE_WIMAX:
721 default:
722 break;
723 }
724 }
725 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
726 mLinkPrefetchAllowed = linkPrefetchAllowed;
727 syncManagedSettings();
728 }
729 }
730
luxiaol62677b02013-07-22 07:54:49 +0800731 public String getDownloadPath() {
732 return mPrefs.getString(PREF_DOWNLOAD_PATH,
733 DownloadHandler.getDefaultDownloadPath(mContext));
734 }
John Reck35e9dd62011-04-25 09:01:54 -0700735 // -----------------------------
736 // getter/setters for accessibility_preferences.xml
737 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100738
John Reck7dc444b2011-06-16 17:44:29 -0700739 @Deprecated
740 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700741 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
742 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100743 }
744
John Reck35e9dd62011-04-25 09:01:54 -0700745 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700746 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700747 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100748 }
749
John Reck92f25f82011-04-26 16:57:10 -0700750 public boolean forceEnableUserScalable() {
751 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
752 }
753
John Reck7dc444b2011-06-16 17:44:29 -0700754 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700755 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700756 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
757 return getAdjustedTextZoom(textZoom);
758 }
759
760 public void setTextZoom(int percent) {
761 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
762 }
763
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700764 public int getDoubleTapZoom() {
765 requireInitialization();
766 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
767 return getAdjustedDoubleTapZoom(doubleTapZoom);
768 }
769
770 public void setDoubleTapZoom(int percent) {
771 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
772 }
773
John Reck35e9dd62011-04-25 09:01:54 -0700774 // -----------------------------
775 // getter/setters for advanced_preferences.xml
776 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100777
John Reck35e9dd62011-04-25 09:01:54 -0700778 public String getSearchEngineName() {
luxiaol221b3932013-07-19 15:27:57 +0800779 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
780 if (defaultSearchEngineValue == null) {
781 defaultSearchEngineValue = SearchEngine.GOOGLE;
782 }
783 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100784 }
John Reck63bb6872010-12-01 19:29:32 -0800785
Michael Kolb8d772b02012-01-19 13:56:00 -0800786 public boolean allowAppTabs() {
787 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
788 }
789
John Reck35e9dd62011-04-25 09:01:54 -0700790 public boolean openInBackground() {
791 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800792 }
John Reck35e9dd62011-04-25 09:01:54 -0700793
794 public boolean enableJavascript() {
795 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
796 }
797
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800798 public boolean enableMemoryMonitor() {
799 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
800 }
801
Tarun Nainani8eb00912014-07-17 12:28:32 -0700802 public boolean allowMediaDownloads() {
803 boolean enableMediaDownloads = mController.getContext().getResources().getBoolean(
804 R.bool.def_enable_media_downloads);
805 boolean shouldAllowMediaDownloads = mPrefs.getBoolean(
806 PREF_ALLOW_MEDIA_DOWNLOADS, enableMediaDownloads);
807
808 if(!mPrefs.contains(PREF_ALLOW_MEDIA_DOWNLOADS)){
809 Editor edit = mPrefs.edit();
810 edit.putBoolean(PREF_ALLOW_MEDIA_DOWNLOADS, shouldAllowMediaDownloads);
811 edit.apply();
812 }
813
814 return shouldAllowMediaDownloads;
815 }
816
John Reck35e9dd62011-04-25 09:01:54 -0700817 // TODO: Cache
John Reck35e9dd62011-04-25 09:01:54 -0700818 public ZoomDensity getDefaultZoom() {
819 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800820 return ZoomDensity.valueOf(zoom);
John Reck35e9dd62011-04-25 09:01:54 -0700821 }
822
823 public boolean loadPageInOverviewMode() {
824 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
825 }
826
827 public boolean autofitPages() {
828 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
829 }
830
831 public boolean blockPopupWindows() {
832 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
833 }
834
835 public boolean loadImages() {
836 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
837 }
838
839 public String getDefaultTextEncoding() {
840 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
841 }
842
843 // -----------------------------
844 // getter/setters for general_preferences.xml
845 // -----------------------------
846
847 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700848 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
849 }
850
851 public void setHomePage(String value) {
852 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
853 }
854
855 public boolean isAutofillEnabled() {
856 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
857 }
858
859 public void setAutofillEnabled(boolean value) {
860 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
861 }
862
863 // -----------------------------
864 // getter/setters for debug_preferences.xml
865 // -----------------------------
866
867 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700868 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700869 return true;
870 }
871 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
872 }
873
Derek Sollenberger31adf672011-07-08 11:31:30 -0400874 public boolean isSkiaHardwareAccelerated() {
875 if (!isDebugEnabled()) {
876 return false;
877 }
878 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
879 }
880
John Reck35e9dd62011-04-25 09:01:54 -0700881 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700882 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700883 return 0;
884 }
885 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
886 }
887
888 // -----------------------------
889 // getter/setters for hidden_debug_preferences.xml
890 // -----------------------------
891
892 public boolean enableVisualIndicator() {
893 if (!isDebugEnabled()) {
894 return false;
895 }
896 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
897 }
898
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700899 public boolean enableCpuUploadPath() {
900 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700901 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700902 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700903 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700904 }
905
John Reck35e9dd62011-04-25 09:01:54 -0700906 public boolean enableJavascriptConsole() {
907 if (!isDebugEnabled()) {
908 return false;
909 }
910 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
911 }
912
913 public boolean isSmallScreen() {
914 if (!isDebugEnabled()) {
915 return false;
916 }
917 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
918 }
919
920 public boolean isWideViewport() {
921 if (!isDebugEnabled()) {
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800922 return true;
John Reck35e9dd62011-04-25 09:01:54 -0700923 }
Panos Thomas3f35d8d2014-11-08 22:40:23 -0800924 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
John Reck35e9dd62011-04-25 09:01:54 -0700925 }
926
927 public boolean isNormalLayout() {
928 if (!isDebugEnabled()) {
929 return false;
930 }
931 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
932 }
933
934 public boolean isTracing() {
935 if (!isDebugEnabled()) {
936 return false;
937 }
938 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
939 }
940
941 public boolean enableLightTouch() {
942 if (!isDebugEnabled()) {
943 return false;
944 }
945 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
946 }
947
948 public boolean enableNavDump() {
949 if (!isDebugEnabled()) {
950 return false;
951 }
952 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
953 }
954
955 public String getJsEngineFlags() {
956 if (!isDebugEnabled()) {
957 return "";
958 }
959 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
960 }
961
962 // -----------------------------
963 // getter/setters for lab_preferences.xml
964 // -----------------------------
965
John Reck35e9dd62011-04-25 09:01:54 -0700966 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700967 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700968 }
969
Michael Kolbc38c6042011-04-27 10:46:06 -0700970 public boolean useFullscreen() {
971 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
972 }
973
John Reck2fd9d0e2011-07-15 11:13:48 -0700974 public boolean useInvertedRendering() {
975 return mPrefs.getBoolean(PREF_INVERTED, false);
976 }
977
Nicolas Roard5d513102011-08-03 15:35:34 -0700978 public float getInvertedContrast() {
979 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
980 }
981
John Reck35e9dd62011-04-25 09:01:54 -0700982 // -----------------------------
983 // getter/setters for privacy_security_preferences.xml
984 // -----------------------------
985
986 public boolean showSecurityWarnings() {
987 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
988 }
989
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700990 public boolean doNotTrack() {
991 return mPrefs.getBoolean(PREF_DO_NOT_TRACK, true);
992 }
993
John Reck35e9dd62011-04-25 09:01:54 -0700994 public boolean acceptCookies() {
995 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
996 }
997
998 public boolean saveFormdata() {
999 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
1000 }
1001
1002 public boolean enableGeolocation() {
1003 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
1004 }
1005
1006 public boolean rememberPasswords() {
1007 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
1008 }
1009
Michael Kolb14612442011-06-24 13:06:29 -07001010 // -----------------------------
1011 // getter/setters for bandwidth_preferences.xml
1012 // -----------------------------
1013
Mathew Inwood467813f2011-09-02 15:43:17 +01001014 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
1015 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -07001016 }
Mathew Inwood467813f2011-09-02 15:43:17 +01001017
1018 public static String getPreloadAlwaysPreferenceString(Context context) {
1019 return context.getResources().getString(R.string.pref_data_preload_value_always);
1020 }
1021
Mathew Inwood825fba72011-10-04 14:52:52 +01001022 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1023 "browser_default_preload_setting";
1024
1025 public String getDefaultPreloadSetting() {
1026 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1027 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1028 if (preload == null) {
1029 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1030 }
1031 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001032 }
1033
1034 public String getPreloadEnabled() {
1035 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1036 }
1037
Victoria Lease96497832012-03-23 14:19:56 -07001038 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1039 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1040 }
1041
1042 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1043 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1044 }
1045
1046 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1047 "browser_default_link_prefetch_setting";
1048
1049 public String getDefaultLinkPrefetchSetting() {
1050 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1051 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1052 if (preload == null) {
1053 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1054 }
1055 return preload;
1056 }
1057
1058 public String getLinkPrefetchEnabled() {
1059 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1060 }
1061
George Mount3636d0a2011-11-21 09:08:21 -08001062 // -----------------------------
1063 // getter/setters for browser recovery
1064 // -----------------------------
1065 /**
1066 * The last time browser was started.
1067 * @return The last browser start time as System.currentTimeMillis. This
1068 * can be 0 if this is the first time or the last tab was closed.
1069 */
1070 public long getLastRecovered() {
1071 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1072 }
1073
1074 /**
1075 * Sets the last browser start time.
1076 * @param time The last time as System.currentTimeMillis that the browser
1077 * was started. This should be set to 0 if the last tab is closed.
1078 */
1079 public void setLastRecovered(long time) {
1080 mPrefs.edit()
1081 .putLong(KEY_LAST_RECOVERED, time)
1082 .apply();
1083 }
1084
1085 /**
1086 * Used to determine whether or not the previous browser run crashed. Once
1087 * the previous state has been determined, the value will be set to false
1088 * until a pause is received.
1089 * @return true if the last browser run was paused or false if it crashed.
1090 */
1091 public boolean wasLastRunPaused() {
1092 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1093 }
1094
1095 /**
1096 * Sets whether or not the last run was a pause or crash.
1097 * @param isPaused Set to true When a pause is received or false after
1098 * resuming.
1099 */
1100 public void setLastRunPaused(boolean isPaused) {
1101 mPrefs.edit()
1102 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1103 .apply();
1104 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001105}