blob: e8ecd93c210094de800442b044da20ee179a66a4 [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;
John Reck35e9dd62011-04-25 09:01:54 -070056/**
57 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080058 */
John Reck35e9dd62011-04-25 09:01:54 -070059public class BrowserSettings implements OnSharedPreferenceChangeListener,
60 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010061
John Reck35e9dd62011-04-25 09:01:54 -070062 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070063 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
64 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
65 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080066
67 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070068 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
69 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070070
71 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070072 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
73 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070074
75 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070076 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
77 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080078
John Reck7fd1e8f2011-04-27 18:22:57 -070079 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
80 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
81 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
82
John Reck35e9dd62011-04-25 09:01:54 -070083 private static final String USER_AGENTS[] = { null,
84 DESKTOP_USERAGENT,
85 IPHONE_USERAGENT,
86 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070087 FROYO_USERAGENT,
88 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070089 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080090
qqzhoue6ff8b42013-07-23 17:28:48 +080091 private static final String TAG = "BrowserSettings";
John Reck8fc22a12011-06-16 14:57:41 -070092 // The minimum min font size
93 // Aka, the lower bounds for the min font size range
94 // which is 1:5..24
95 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070096 // The initial value in the text zoom range
97 // This is what represents 100% in the SeekBarPreference range
98 private static final int TEXT_ZOOM_START_VAL = 10;
99 // The size of a single step in the text zoom range, in percent
100 private static final int TEXT_ZOOM_STEP = 5;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700101 // The initial value in the double tap zoom range
102 // This is what represents 100% in the SeekBarPreference range
103 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
104 // The size of a single step in the double tap zoom range, in percent
105 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -0700106
John Reck35e9dd62011-04-25 09:01:54 -0700107 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -0700108
John Reck35e9dd62011-04-25 09:01:54 -0700109 private Context mContext;
110 private SharedPreferences mPrefs;
111 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700112 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700113 private WebStorageSizeManager mWebStorageSizeManager;
114 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700115 private WeakHashMap<WebSettings, String> mCustomUserAgents;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100116 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -0700117 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -0700118 private float mFontSizeMult = 1.0f;
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700119 private boolean enableWideViewport = true;
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
Bijan Amirzada3bb46302014-06-17 16:31:31 -0700133 public static synchronized void initialize(final Context context) {
134 if (sInstance == null)
135 sInstance = new BrowserSettings(context);
John Reck35e9dd62011-04-25 09:01:54 -0700136 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800137
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700138 public static BrowserSettings getInstance() {
John Reck35e9dd62011-04-25 09:01:54 -0700139 return sInstance;
140 }
Ben Murdochef671652010-11-25 17:17:58 +0000141
John Reck35e9dd62011-04-25 09:01:54 -0700142 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100143 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700144 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
145 mAutofillHandler = new AutofillHandler(mContext);
146 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700147 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reckcadae722011-07-25 11:36:17 -0700148 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700149 }
150
151 public void setController(Controller controller) {
152 mController = controller;
John Reckc477e712011-08-17 11:27:15 -0700153 if (sInitialized) {
154 syncSharedSettings();
155 }
Ben Murdochef671652010-11-25 17:17:58 +0000156 }
157
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800158 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800159
John Reckc477e712011-08-17 11:27:15 -0700160 if (mNeedsSharedSync) {
161 syncSharedSettings();
162 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800163
John Reck35e9dd62011-04-25 09:01:54 -0700164 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800165 syncStaticSettings(settings);
166 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700167 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800168 }
169 }
170
John Reckd1d87312012-03-08 13:25:00 -0800171 public void stopManagingSettings(WebSettings settings) {
172 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
173 while (iter.hasNext()) {
174 WeakReference<WebSettings> ref = iter.next();
175 if (ref.get() == settings) {
176 iter.remove();
177 return;
178 }
179 }
180 }
181
John Reckcadae722011-07-25 11:36:17 -0700182 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700183
184 @Override
185 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700186 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
187 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700188 // the cost of one cached page is ~3M (measured using nytimes.com). For
189 // low end devices, we only cache one page. For high end devices, we try
190 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800191
192 // SWE_TODO : assume a high-memory device
193 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700194 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800195 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700196 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
197 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
198 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700199 // Workaround b/5254577
200 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700201 if (Build.VERSION.CODENAME.equals("REL")) {
202 // This is a release build, always startup with debug disabled
203 setDebugEnabled(false);
204 }
205 if (mPrefs.contains(PREF_TEXT_SIZE)) {
206 /*
207 * Update from TextSize enum to zoom percent
208 * SMALLEST is 50%
209 * SMALLER is 75%
210 * NORMAL is 100%
211 * LARGER is 150%
212 * LARGEST is 200%
213 */
214 switch (getTextSize()) {
215 case SMALLEST:
216 setTextZoom(50);
217 break;
218 case SMALLER:
219 setTextZoom(75);
220 break;
221 case LARGER:
222 setTextZoom(150);
223 break;
224 case LARGEST:
225 setTextZoom(200);
226 break;
227 }
228 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
229 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100230
qqzhou8c5b0a32013-07-22 15:31:03 +0800231 // add for carrier homepage feature
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700232 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
qqzhou8c5b0a32013-07-22 15:31:03 +0800233
kaiyizbf086ea2013-08-02 11:03:58 +0800234 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700235 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING, "UTF-8").apply();
kaiyizbf086ea2013-08-02 11:03:58 +0800236 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700237
Ben Murdochaaa1f372011-07-25 15:40:58 +0100238 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
239 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
240 BrowserProvider.getClientId(mContext.getContentResolver()));
241 }
242
Ben Murdochaaa1f372011-07-25 15:40:58 +0100243 synchronized (BrowserSettings.class) {
244 sInitialized = true;
245 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700246 }
247 }
248 };
249
Ben Murdochaaa1f372011-07-25 15:40:58 +0100250 private static void requireInitialization() {
251 synchronized (BrowserSettings.class) {
252 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700253 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100254 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700255 } catch (InterruptedException e) {
256 }
257 }
258 }
259 }
260
The Android Open Source Project0c908882009-03-03 19:32:16 -0800261 /**
John Reck35e9dd62011-04-25 09:01:54 -0700262 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800263 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800264 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700265 settings.setGeolocationEnabled(enableGeolocation());
266 settings.setJavaScriptEnabled(enableJavascript());
267 settings.setLightTouchEnabled(enableLightTouch());
268 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700269 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
270 settings.setDefaultZoom(getDefaultZoom());
271 settings.setMinimumFontSize(getMinimumFontSize());
272 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck7dc444b2011-06-16 17:44:29 -0700273 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700274 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100275 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700276 settings.setLoadsImagesAutomatically(loadImages());
277 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
278 settings.setSavePassword(rememberPasswords());
279 settings.setSaveFormData(saveFormdata());
280 settings.setUseWideViewPort(isWideViewport());
Panos Thomasb10bbda2014-06-23 10:18:36 -0700281 settings.setDoNotTrack(doNotTrack());
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700282 setUserAgent(settings);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700283 settings.setMediaPlaybackRequiresUserGesture(false);
284 settings.setAllowMediaDownloads(allowMediaDownloads());
285 setExtraHTTPRequestHeaders(settings);
John Reck2fd9d0e2011-07-15 11:13:48 -0700286
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800287 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800288 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
289 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
290 settingsClassic.setForceUserScalable(forceEnableUserScalable());
291 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
292 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800293
John Reckea17e782011-10-27 17:15:59 -0700294 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800295 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700296 useInverted ? "true" : "false");
297 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800298 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700299 Float.toString(getInvertedContrast()));
300 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700301
John Reckea17e782011-10-27 17:15:59 -0700302 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800303 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700304 enableCpuUploadPath() ? "true" : "false");
305 }
Victoria Lease96497832012-03-23 14:19:56 -0700306
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800307 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000308 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800309
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700310 private void setUserAgent(WebSettings settings){
311 String ua = mContext.getResources().getString(R.string.def_useragent);
312
313 if (!TextUtils.isEmpty(ua))
314 ua = constructUserAgent(ua);
315
316 if (TextUtils.isEmpty(ua))
317 ua = mCustomUserAgents.get(settings);
318
319 if (!TextUtils.isEmpty(ua)){
320 settings.setUserAgentString(ua);
321 } else {
322 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
323 }
324 }
325
Tarun Nainani8eb00912014-07-17 12:28:32 -0700326 private void setExtraHTTPRequestHeaders(WebSettings settings){
327 String headers = mContext.getResources().getString(R.string.def_extra_http_headers);
328 if (!TextUtils.isEmpty(headers)){
329 settings.setHTTPRequestHeaders(headers);
330 }
331 }
332
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700333 private String constructUserAgent(String userAgent) {
334 try {
335 userAgent = userAgent.replaceAll("<%build_model>", Build.MODEL);
336 userAgent = userAgent.replaceAll("<%build_version>", Build.VERSION.RELEASE);
337 userAgent = userAgent.replaceAll("<%build_id>", Build.ID);
338 userAgent = userAgent.replaceAll("<%language>", Locale.getDefault().getLanguage());
339 userAgent = userAgent.replaceAll("<%country>", Locale.getDefault().getCountry());
340 return userAgent;
341 } catch (Exception ex) {
342 return null;
343 }
344 }
345
John Reck35e9dd62011-04-25 09:01:54 -0700346 /**
347 * Syncs all the settings that have no UI
348 * These cannot change, so we only need to set them once per WebSettings
349 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800350 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700351 settings.setDefaultFontSize(16);
352 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000353
John Reck35e9dd62011-04-25 09:01:54 -0700354 // WebView inside Browser doesn't want initial focus to be set.
355 settings.setNeedInitialFocus(false);
356 // Browser supports multiple windows
357 settings.setSupportMultipleWindows(true);
358 // enable smooth transition for better performance during panning or
359 // zooming
360 settings.setEnableSmoothTransition(true);
361 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700362 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700363
364 // HTML5 API flags
365 settings.setAppCacheEnabled(true);
366 settings.setDatabaseEnabled(true);
367 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700368
369 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700370 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700371 settings.setAppCachePath(getAppCachePath());
372 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
373 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700374 // origin policy for file access
375 settings.setAllowUniversalAccessFromFileURLs(false);
376 settings.setAllowFileAccessFromFileURLs(false);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800377
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800378 //if (!(settings instanceof WebSettingsClassic)) return;
379 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800380
381 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
382 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
383 // WebView should be preserving the memory as much as possible.
384 // However, apps like browser wish to turn on the performance mode which
385 // would require more memory.
386 // TODO: We need to dynamically allocate/deallocate temporary memory for
387 // apps which are trying to use minimal memory. Currently, double
388 // buffering is always turned on, which is unnecessary.
389 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
390 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800391 */
John Reck35e9dd62011-04-25 09:01:54 -0700392 }
393
394 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700395 mNeedsSharedSync = false;
Axesh R. Ajmera579c70c2014-04-17 13:24:56 -0700396 CookieManager.getInstance().setAcceptCookie(acceptCookies());
397
John Reck35e9dd62011-04-25 09:01:54 -0700398 if (mController != null) {
399 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700400 }
John Reck35e9dd62011-04-25 09:01:54 -0700401 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700402
John Reck35e9dd62011-04-25 09:01:54 -0700403 private void syncManagedSettings() {
404 syncSharedSettings();
405 synchronized (mManagedSettings) {
406 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
407 while (iter.hasNext()) {
408 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800409 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700410 if (settings == null) {
411 iter.remove();
412 continue;
413 }
414 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000415 }
John Reck35e9dd62011-04-25 09:01:54 -0700416 }
417 }
Ben Murdochef671652010-11-25 17:17:58 +0000418
John Reck35e9dd62011-04-25 09:01:54 -0700419 @Override
420 public void onSharedPreferenceChanged(
421 SharedPreferences sharedPreferences, String key) {
422 syncManagedSettings();
423 if (PREF_SEARCH_ENGINE.equals(key)) {
424 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700425 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200426 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700427 mController.getUi().setFullscreen(useFullscreen());
428 }
Victoria Lease96497832012-03-23 14:19:56 -0700429 } else if (PREF_LINK_PREFETCH.equals(key)) {
430 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700431 }
John Reck35e9dd62011-04-25 09:01:54 -0700432 }
433
John Reck961d35d2011-06-23 09:45:54 -0700434 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100435 requireInitialization();
436 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700437 }
438
439 public LayoutAlgorithm getLayoutAlgorithm() {
440 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
441 if (autofitPages()) {
Tarun Nainani003bec52014-07-02 02:20:34 -0700442 layoutAlgorithm = LayoutAlgorithm.TEXT_AUTOSIZING;
John Reck35e9dd62011-04-25 09:01:54 -0700443 }
444 if (isDebugEnabled()) {
445 if (isSmallScreen()) {
446 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000447 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700448 if (isNormalLayout()) {
449 layoutAlgorithm = LayoutAlgorithm.NORMAL;
450 } else {
451 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
452 }
Ben Murdochef671652010-11-25 17:17:58 +0000453 }
John Reck35e9dd62011-04-25 09:01:54 -0700454 }
455 return layoutAlgorithm;
456 }
Ben Murdochef671652010-11-25 17:17:58 +0000457
John Reck35e9dd62011-04-25 09:01:54 -0700458 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700459 requireInitialization();
460 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800461 }
462
John Reck35e9dd62011-04-25 09:01:54 -0700463 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700464 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700465 return mWebStorageSizeManager;
466 }
467
John Reck35e9dd62011-04-25 09:01:54 -0700468 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700469 if (mAppCachePath == null) {
470 mAppCachePath = mContext.getDir("appcache", 0).getPath();
471 }
472 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700473 }
474
475 private void updateSearchEngine(boolean force) {
476 String searchEngineName = getSearchEngineName();
477 if (force || mSearchEngine == null ||
478 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700479 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000480 }
481 }
482
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100483 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700484 if (mSearchEngine == null) {
485 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100486 }
John Reck35e9dd62011-04-25 09:01:54 -0700487 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100488 }
489
John Reck35e9dd62011-04-25 09:01:54 -0700490 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700491 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700492 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100493 }
494
John Reck35e9dd62011-04-25 09:01:54 -0700495 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700496 Editor edit = mPrefs.edit();
497 edit.putBoolean(PREF_DEBUG_MENU, value);
498 if (!value) {
499 // Reset to "safe" value
500 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
501 }
502 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100503 }
504
John Reck35e9dd62011-04-25 09:01:54 -0700505 public void clearCache() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700506 if (mController != null) {
507 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800508 if (current != null) {
509 current.clearCache(true);
510 }
511 }
512 }
513
John Reck35e9dd62011-04-25 09:01:54 -0700514 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800515 CookieManager.getInstance().removeAllCookie();
516 }
517
John Reck35e9dd62011-04-25 09:01:54 -0700518 public void clearHistory() {
519 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800520 Browser.clearHistory(resolver);
521 Browser.clearSearches(resolver);
522 }
523
John Reck35e9dd62011-04-25 09:01:54 -0700524 public void clearFormData() {
525 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700526 if (mController!= null) {
527 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100528 if (currentTopView != null) {
529 currentTopView.clearFormData();
530 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800531 }
532 }
533
Bijan Amirzada3bbb3a42014-06-17 12:06:18 -0700534 public WebView getTopWebView(){
535 if (mController!= null)
536 return mController.getCurrentTopWebView();
537
538 return null;
539 }
540
John Reck35e9dd62011-04-25 09:01:54 -0700541 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700542 // Clear password store maintained by SWE engine
543 WebSettings settings = null;
544 // find a valid settings object
545 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
546 while (iter.hasNext()) {
547 WeakReference<WebSettings> ref = iter.next();
548 settings = (WebSettings)ref.get();
549 if (settings != null) {
550 break;
551 }
552 }
553 if (settings != null) {
Panos Thomasdbaea4e2014-05-22 06:48:47 -0700554 settings.clearPasswords();
Panos Thomasfa948b82014-03-09 03:42:42 -0700555 }
Vivek Sekhare8a8ec22014-07-10 14:52:43 -0700556
557 // Clear passwords in WebView database
558 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
559 db.clearHttpAuthUsernamePassword();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800560 }
561
John Reck35e9dd62011-04-25 09:01:54 -0700562 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100563 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100564 }
565
John Reck35e9dd62011-04-25 09:01:54 -0700566 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100567 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100568 }
569
John Reck35e9dd62011-04-25 09:01:54 -0700570 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700571 mPrefs.edit()
572 .clear()
John Reckbd315192011-07-29 10:05:47 -0700573 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200574 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700575 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700576 }
577
Björn Isakssonc885a242012-06-05 17:19:04 +0200578 private void resetCachedValues() {
579 updateSearchEngine(false);
580 }
581
John Reck35e9dd62011-04-25 09:01:54 -0700582 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800583 // query the profile from components autofill database 524
584 if (mAutofillHandler.mAutoFillProfile == null &&
585 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
586 WebSettings settings = null;
587 // find a valid settings object
588 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
589 while (iter.hasNext()) {
590 WeakReference<WebSettings> ref = iter.next();
591 settings = (WebSettings)ref.get();
592 if (settings != null) {
593 break;
594 }
595 }
596 if (settings != null) {
597 AutoFillProfile profile =
598 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
599 mAutofillHandler.setAutoFillProfile(profile);
600 }
601 }
John Reck35e9dd62011-04-25 09:01:54 -0700602 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800603 }
604
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800605 public String getAutoFillProfileId() {
606 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800607 }
608
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800609 public void updateAutoFillProfile(AutoFillProfile profile) {
610 syncAutoFillProfile(profile);
611 }
612
613 private void syncAutoFillProfile(AutoFillProfile profile) {
614 synchronized (mManagedSettings) {
615 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
616 while (iter.hasNext()) {
617 WeakReference<WebSettings> ref = iter.next();
618 WebSettings settings = (WebSettings)ref.get();
619 if (settings == null) {
620 iter.remove();
621 continue;
622 }
623 // update the profile only once.
624 settings.setAutoFillProfile(profile);
625 // Now we should have the guid
626 mAutofillHandler.setAutoFillProfile(profile);
627 break;
628 }
629 }
630 }
John Reck35e9dd62011-04-25 09:01:54 -0700631 public void toggleDebugSettings() {
632 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800633 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100634
John Reckb8b2af82011-05-20 15:58:33 -0700635 public boolean hasDesktopUseragent(WebView view) {
636 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
637 }
638
639 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700640 if (view == null) {
641 return;
642 }
John Reckb8b2af82011-05-20 15:58:33 -0700643 WebSettings settings = view.getSettings();
644 if (mCustomUserAgents.get(settings) != null) {
645 mCustomUserAgents.remove(settings);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700646 setUserAgent(settings);
John Reckb8b2af82011-05-20 15:58:33 -0700647 } else {
648 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
649 settings.setUserAgentString(DESKTOP_USERAGENT);
650 }
651 }
652
John Reck7dc444b2011-06-16 17:44:29 -0700653 public static int getAdjustedMinimumFontSize(int rawValue) {
654 rawValue++; // Preference starts at 0, min font at 1
655 if (rawValue > 1) {
656 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
657 }
658 return rawValue;
659 }
660
John Reck5ba3c762011-09-14 15:00:15 -0700661 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700662 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700663 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700664 }
665
666 static int getRawTextZoom(int percent) {
667 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
668 }
669
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700670 public int getAdjustedDoubleTapZoom(int rawValue) {
671 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
672 return (int) ((rawValue + 100) * mFontSizeMult);
673 }
674
675 static int getRawDoubleTapZoom(int percent) {
676 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
677 }
678
John Reckcadae722011-07-25 11:36:17 -0700679 public SharedPreferences getPreferences() {
680 return mPrefs;
681 }
682
Victoria Lease96497832012-03-23 14:19:56 -0700683 // update connectivity-dependent options
684 public void updateConnectionType() {
685 ConnectivityManager cm = (ConnectivityManager)
686 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
687 String linkPrefetchPreference = getLinkPrefetchEnabled();
688 boolean linkPrefetchAllowed = linkPrefetchPreference.
689 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
690 NetworkInfo ni = cm.getActiveNetworkInfo();
691 if (ni != null) {
692 switch (ni.getType()) {
693 case ConnectivityManager.TYPE_WIFI:
694 case ConnectivityManager.TYPE_ETHERNET:
695 case ConnectivityManager.TYPE_BLUETOOTH:
696 linkPrefetchAllowed |= linkPrefetchPreference.
697 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
698 break;
699 case ConnectivityManager.TYPE_MOBILE:
700 case ConnectivityManager.TYPE_MOBILE_DUN:
701 case ConnectivityManager.TYPE_MOBILE_MMS:
702 case ConnectivityManager.TYPE_MOBILE_SUPL:
703 case ConnectivityManager.TYPE_WIMAX:
704 default:
705 break;
706 }
707 }
708 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
709 mLinkPrefetchAllowed = linkPrefetchAllowed;
710 syncManagedSettings();
711 }
712 }
713
luxiaol62677b02013-07-22 07:54:49 +0800714 public String getDownloadPath() {
715 return mPrefs.getString(PREF_DOWNLOAD_PATH,
716 DownloadHandler.getDefaultDownloadPath(mContext));
717 }
John Reck35e9dd62011-04-25 09:01:54 -0700718 // -----------------------------
719 // getter/setters for accessibility_preferences.xml
720 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100721
John Reck7dc444b2011-06-16 17:44:29 -0700722 @Deprecated
723 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700724 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
725 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100726 }
727
John Reck35e9dd62011-04-25 09:01:54 -0700728 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700729 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700730 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100731 }
732
John Reck92f25f82011-04-26 16:57:10 -0700733 public boolean forceEnableUserScalable() {
734 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
735 }
736
John Reck7dc444b2011-06-16 17:44:29 -0700737 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700738 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700739 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
740 return getAdjustedTextZoom(textZoom);
741 }
742
743 public void setTextZoom(int percent) {
744 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
745 }
746
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700747 public int getDoubleTapZoom() {
748 requireInitialization();
749 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
750 return getAdjustedDoubleTapZoom(doubleTapZoom);
751 }
752
753 public void setDoubleTapZoom(int percent) {
754 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
755 }
756
John Reck35e9dd62011-04-25 09:01:54 -0700757 // -----------------------------
758 // getter/setters for advanced_preferences.xml
759 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100760
John Reck35e9dd62011-04-25 09:01:54 -0700761 public String getSearchEngineName() {
luxiaol221b3932013-07-19 15:27:57 +0800762 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
763 if (defaultSearchEngineValue == null) {
764 defaultSearchEngineValue = SearchEngine.GOOGLE;
765 }
766 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100767 }
John Reck63bb6872010-12-01 19:29:32 -0800768
Michael Kolb8d772b02012-01-19 13:56:00 -0800769 public boolean allowAppTabs() {
770 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
771 }
772
John Reck35e9dd62011-04-25 09:01:54 -0700773 public boolean openInBackground() {
774 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800775 }
John Reck35e9dd62011-04-25 09:01:54 -0700776
777 public boolean enableJavascript() {
778 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
779 }
780
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800781 public boolean enableMemoryMonitor() {
782 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
783 }
784
Tarun Nainani8eb00912014-07-17 12:28:32 -0700785 public boolean allowMediaDownloads() {
786 boolean enableMediaDownloads = mController.getContext().getResources().getBoolean(
787 R.bool.def_enable_media_downloads);
788 boolean shouldAllowMediaDownloads = mPrefs.getBoolean(
789 PREF_ALLOW_MEDIA_DOWNLOADS, enableMediaDownloads);
790
791 if(!mPrefs.contains(PREF_ALLOW_MEDIA_DOWNLOADS)){
792 Editor edit = mPrefs.edit();
793 edit.putBoolean(PREF_ALLOW_MEDIA_DOWNLOADS, shouldAllowMediaDownloads);
794 edit.apply();
795 }
796
797 return shouldAllowMediaDownloads;
798 }
799
John Reck35e9dd62011-04-25 09:01:54 -0700800 // TODO: Cache
John Reck35e9dd62011-04-25 09:01:54 -0700801 public ZoomDensity getDefaultZoom() {
802 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700803 ZoomDensity zoomDensity = ZoomDensity.valueOf(zoom);
804 //SWE: If zoomDensity is not MEDIUM, set enableWideViewport to false.
805 if(zoomDensity != ZoomDensity.MEDIUM)
806 enableWideViewport = false;
807 else
808 enableWideViewport = true;
809 return zoomDensity;
John Reck35e9dd62011-04-25 09:01:54 -0700810 }
811
812 public boolean loadPageInOverviewMode() {
813 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
814 }
815
816 public boolean autofitPages() {
817 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
818 }
819
820 public boolean blockPopupWindows() {
821 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
822 }
823
824 public boolean loadImages() {
825 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
826 }
827
828 public String getDefaultTextEncoding() {
829 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
830 }
831
832 // -----------------------------
833 // getter/setters for general_preferences.xml
834 // -----------------------------
835
836 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700837 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
838 }
839
840 public void setHomePage(String value) {
841 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
842 }
843
844 public boolean isAutofillEnabled() {
845 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
846 }
847
848 public void setAutofillEnabled(boolean value) {
849 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
850 }
851
852 // -----------------------------
853 // getter/setters for debug_preferences.xml
854 // -----------------------------
855
856 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700857 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700858 return true;
859 }
860 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
861 }
862
Derek Sollenberger31adf672011-07-08 11:31:30 -0400863 public boolean isSkiaHardwareAccelerated() {
864 if (!isDebugEnabled()) {
865 return false;
866 }
867 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
868 }
869
John Reck35e9dd62011-04-25 09:01:54 -0700870 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700871 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700872 return 0;
873 }
874 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
875 }
876
877 // -----------------------------
878 // getter/setters for hidden_debug_preferences.xml
879 // -----------------------------
880
881 public boolean enableVisualIndicator() {
882 if (!isDebugEnabled()) {
883 return false;
884 }
885 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
886 }
887
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700888 public boolean enableCpuUploadPath() {
889 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700890 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700891 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700892 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700893 }
894
John Reck35e9dd62011-04-25 09:01:54 -0700895 public boolean enableJavascriptConsole() {
896 if (!isDebugEnabled()) {
897 return false;
898 }
899 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
900 }
901
902 public boolean isSmallScreen() {
903 if (!isDebugEnabled()) {
904 return false;
905 }
906 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
907 }
908
909 public boolean isWideViewport() {
910 if (!isDebugEnabled()) {
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700911 return enableWideViewport;
John Reck35e9dd62011-04-25 09:01:54 -0700912 }
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700913 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, enableWideViewport);
John Reck35e9dd62011-04-25 09:01:54 -0700914 }
915
916 public boolean isNormalLayout() {
917 if (!isDebugEnabled()) {
918 return false;
919 }
920 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
921 }
922
923 public boolean isTracing() {
924 if (!isDebugEnabled()) {
925 return false;
926 }
927 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
928 }
929
930 public boolean enableLightTouch() {
931 if (!isDebugEnabled()) {
932 return false;
933 }
934 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
935 }
936
937 public boolean enableNavDump() {
938 if (!isDebugEnabled()) {
939 return false;
940 }
941 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
942 }
943
944 public String getJsEngineFlags() {
945 if (!isDebugEnabled()) {
946 return "";
947 }
948 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
949 }
950
951 // -----------------------------
952 // getter/setters for lab_preferences.xml
953 // -----------------------------
954
John Reck35e9dd62011-04-25 09:01:54 -0700955 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700956 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700957 }
958
Michael Kolbc38c6042011-04-27 10:46:06 -0700959 public boolean useFullscreen() {
960 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
961 }
962
John Reck2fd9d0e2011-07-15 11:13:48 -0700963 public boolean useInvertedRendering() {
964 return mPrefs.getBoolean(PREF_INVERTED, false);
965 }
966
Nicolas Roard5d513102011-08-03 15:35:34 -0700967 public float getInvertedContrast() {
968 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
969 }
970
John Reck35e9dd62011-04-25 09:01:54 -0700971 // -----------------------------
972 // getter/setters for privacy_security_preferences.xml
973 // -----------------------------
974
975 public boolean showSecurityWarnings() {
976 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
977 }
978
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700979 public boolean doNotTrack() {
980 return mPrefs.getBoolean(PREF_DO_NOT_TRACK, true);
981 }
982
John Reck35e9dd62011-04-25 09:01:54 -0700983 public boolean acceptCookies() {
984 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
985 }
986
987 public boolean saveFormdata() {
988 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
989 }
990
991 public boolean enableGeolocation() {
992 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
993 }
994
995 public boolean rememberPasswords() {
996 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
997 }
998
Michael Kolb14612442011-06-24 13:06:29 -0700999 // -----------------------------
1000 // getter/setters for bandwidth_preferences.xml
1001 // -----------------------------
1002
Mathew Inwood467813f2011-09-02 15:43:17 +01001003 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
1004 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -07001005 }
Mathew Inwood467813f2011-09-02 15:43:17 +01001006
1007 public static String getPreloadAlwaysPreferenceString(Context context) {
1008 return context.getResources().getString(R.string.pref_data_preload_value_always);
1009 }
1010
Mathew Inwood825fba72011-10-04 14:52:52 +01001011 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1012 "browser_default_preload_setting";
1013
1014 public String getDefaultPreloadSetting() {
1015 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1016 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1017 if (preload == null) {
1018 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1019 }
1020 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001021 }
1022
1023 public String getPreloadEnabled() {
1024 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1025 }
1026
Victoria Lease96497832012-03-23 14:19:56 -07001027 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1028 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1029 }
1030
1031 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1032 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1033 }
1034
1035 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1036 "browser_default_link_prefetch_setting";
1037
1038 public String getDefaultLinkPrefetchSetting() {
1039 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1040 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1041 if (preload == null) {
1042 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1043 }
1044 return preload;
1045 }
1046
1047 public String getLinkPrefetchEnabled() {
1048 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1049 }
1050
George Mount3636d0a2011-11-21 09:08:21 -08001051 // -----------------------------
1052 // getter/setters for browser recovery
1053 // -----------------------------
1054 /**
1055 * The last time browser was started.
1056 * @return The last browser start time as System.currentTimeMillis. This
1057 * can be 0 if this is the first time or the last tab was closed.
1058 */
1059 public long getLastRecovered() {
1060 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1061 }
1062
1063 /**
1064 * Sets the last browser start time.
1065 * @param time The last time as System.currentTimeMillis that the browser
1066 * was started. This should be set to 0 if the last tab is closed.
1067 */
1068 public void setLastRecovered(long time) {
1069 mPrefs.edit()
1070 .putLong(KEY_LAST_RECOVERED, time)
1071 .apply();
1072 }
1073
1074 /**
1075 * Used to determine whether or not the previous browser run crashed. Once
1076 * the previous state has been determined, the value will be set to false
1077 * until a pause is received.
1078 * @return true if the last browser run was paused or false if it crashed.
1079 */
1080 public boolean wasLastRunPaused() {
1081 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1082 }
1083
1084 /**
1085 * Sets whether or not the last run was a pause or crash.
1086 * @param isPaused Set to true When a pause is received or false after
1087 * resuming.
1088 */
1089 public void setLastRunPaused(boolean isPaused) {
1090 mPrefs.edit()
1091 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1092 .apply();
1093 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001094}