blob: ba827b96ac3325f2557d6482cbd1eb860029a685 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
John Reck35e9dd62011-04-25 09:01:54 -07002 * Copyright (C) 2011 The Android Open Source Project
The Android Open Source Project0c908882009-03-03 19:32:16 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080018
The Android Open Source Project0c908882009-03-03 19:32:16 -080019import android.content.ContentResolver;
20import android.content.Context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.content.SharedPreferences;
John Reck1da81882011-10-04 17:21:10 -070022import android.content.SharedPreferences.Editor;
John Reck812d2d62011-01-18 14:16:15 -080023import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Victoria Lease96497832012-03-23 14:19:56 -070024import android.net.ConnectivityManager;
25import android.net.NetworkInfo;
John Reckf48314f2011-04-27 17:52:17 -070026import android.os.Build;
Ben Murdoch0cb81892010-10-08 12:41:33 +010027import android.preference.PreferenceManager;
Mathew Inwood825fba72011-10-04 14:52:52 +010028import android.provider.Settings;
Bijan Amirzadae75909d2014-05-06 14:18:54 -070029import android.text.TextUtils;
John Reck5ba3c762011-09-14 15:00:15 -070030import android.util.DisplayMetrics;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.webkit.WebIconDatabase;
Nicolas Roard78a98e42009-05-11 13:34:17 +010032import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033
Bijan Amirzada41242f22014-03-21 12:12:18 -070034import com.android.browser.R;
35import com.android.browser.homepages.HomeProvider;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070036import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070037import com.android.browser.provider.BrowserProvider;
Bijan Amirzada41242f22014-03-21 12:12:18 -070038import com.android.browser.search.SearchEngine;
39import com.android.browser.search.SearchEngines;
John Reck46500332011-06-07 14:36:10 -070040
John Reck35e9dd62011-04-25 09:01:54 -070041import java.lang.ref.WeakReference;
42import java.util.Iterator;
43import java.util.LinkedList;
qqzhou8c5b0a32013-07-22 15:31:03 +080044import java.util.Locale;
John Reckb8b2af82011-05-20 15:58:33 -070045import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080046
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080047import org.codeaurora.swe.AutoFillProfile;
48import org.codeaurora.swe.CookieManager;
49import org.codeaurora.swe.GeolocationPermissions;
50import org.codeaurora.swe.WebSettings.LayoutAlgorithm;
51import org.codeaurora.swe.WebSettings.PluginState;
52import org.codeaurora.swe.WebSettings.TextSize;
53import org.codeaurora.swe.WebSettings.ZoomDensity;
54import org.codeaurora.swe.WebSettings;
55import org.codeaurora.swe.WebView;
Vivek Sekhare8a8ec22014-07-10 14:52:43 -070056import org.codeaurora.swe.WebViewDatabase;
John Reck35e9dd62011-04-25 09:01:54 -070057/**
58 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080059 */
John Reck35e9dd62011-04-25 09:01:54 -070060public class BrowserSettings implements OnSharedPreferenceChangeListener,
61 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010062
John Reck35e9dd62011-04-25 09:01:54 -070063 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070064 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
65 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
66 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080067
68 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070069 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
70 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070071
72 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070073 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
74 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070075
76 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070077 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
78 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080079
John Reck7fd1e8f2011-04-27 18:22:57 -070080 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
81 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
82 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
83
John Reck35e9dd62011-04-25 09:01:54 -070084 private static final String USER_AGENTS[] = { null,
85 DESKTOP_USERAGENT,
86 IPHONE_USERAGENT,
87 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070088 FROYO_USERAGENT,
89 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070090 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080091
qqzhoue6ff8b42013-07-23 17:28:48 +080092 private static final String TAG = "BrowserSettings";
John Reck8fc22a12011-06-16 14:57:41 -070093 // The minimum min font size
94 // Aka, the lower bounds for the min font size range
95 // which is 1:5..24
96 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070097 // The initial value in the text zoom range
98 // This is what represents 100% in the SeekBarPreference range
99 private static final int TEXT_ZOOM_START_VAL = 10;
100 // The size of a single step in the text zoom range, in percent
101 private static final int TEXT_ZOOM_STEP = 5;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700102 // The initial value in the double tap zoom range
103 // This is what represents 100% in the SeekBarPreference range
104 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
105 // The size of a single step in the double tap zoom range, in percent
106 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -0700107
John Reck35e9dd62011-04-25 09:01:54 -0700108 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -0700109
John Reck35e9dd62011-04-25 09:01:54 -0700110 private Context mContext;
111 private SharedPreferences mPrefs;
112 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700113 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700114 private WebStorageSizeManager mWebStorageSizeManager;
115 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700116 private WeakHashMap<WebSettings, String> mCustomUserAgents;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100117 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -0700118 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -0700119 private float mFontSizeMult = 1.0f;
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700120 private boolean enableWideViewport = true;
John Reck78a6a1d2011-07-21 13:54:03 -0700121
Victoria Lease96497832012-03-23 14:19:56 -0700122 // Current state of network-dependent settings
123 private boolean mLinkPrefetchAllowed = true;
124
John Reck78a6a1d2011-07-21 13:54:03 -0700125 // Cached values
126 private int mPageCacheCapacity = 1;
127 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800128
John Reck35e9dd62011-04-25 09:01:54 -0700129 // Cached settings
130 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800131
Ben Murdochaaa1f372011-07-25 15:40:58 +0100132 private static String sFactoryResetUrl;
133
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700134 public static void initialize(final Context context) {
John Reck35e9dd62011-04-25 09:01:54 -0700135 sInstance = new BrowserSettings(context);
136 }
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);
John Reck2fd9d0e2011-07-15 11:13:48 -0700283
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800284 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800285 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
286 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
287 settingsClassic.setForceUserScalable(forceEnableUserScalable());
288 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
289 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800290
John Reckea17e782011-10-27 17:15:59 -0700291 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800292 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700293 useInverted ? "true" : "false");
294 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800295 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700296 Float.toString(getInvertedContrast()));
297 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700298
John Reckea17e782011-10-27 17:15:59 -0700299 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800300 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700301 enableCpuUploadPath() ? "true" : "false");
302 }
Victoria Lease96497832012-03-23 14:19:56 -0700303
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800304 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000305 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800306
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700307 private void setUserAgent(WebSettings settings){
308 String ua = mContext.getResources().getString(R.string.def_useragent);
309
310 if (!TextUtils.isEmpty(ua))
311 ua = constructUserAgent(ua);
312
313 if (TextUtils.isEmpty(ua))
314 ua = mCustomUserAgents.get(settings);
315
316 if (!TextUtils.isEmpty(ua)){
317 settings.setUserAgentString(ua);
318 } else {
319 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
320 }
321 }
322
323 private String constructUserAgent(String userAgent) {
324 try {
325 userAgent = userAgent.replaceAll("<%build_model>", Build.MODEL);
326 userAgent = userAgent.replaceAll("<%build_version>", Build.VERSION.RELEASE);
327 userAgent = userAgent.replaceAll("<%build_id>", Build.ID);
328 userAgent = userAgent.replaceAll("<%language>", Locale.getDefault().getLanguage());
329 userAgent = userAgent.replaceAll("<%country>", Locale.getDefault().getCountry());
330 return userAgent;
331 } catch (Exception ex) {
332 return null;
333 }
334 }
335
John Reck35e9dd62011-04-25 09:01:54 -0700336 /**
337 * Syncs all the settings that have no UI
338 * These cannot change, so we only need to set them once per WebSettings
339 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800340 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700341 settings.setDefaultFontSize(16);
342 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000343
John Reck35e9dd62011-04-25 09:01:54 -0700344 // WebView inside Browser doesn't want initial focus to be set.
345 settings.setNeedInitialFocus(false);
346 // Browser supports multiple windows
347 settings.setSupportMultipleWindows(true);
348 // enable smooth transition for better performance during panning or
349 // zooming
350 settings.setEnableSmoothTransition(true);
351 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700352 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700353
354 // HTML5 API flags
355 settings.setAppCacheEnabled(true);
356 settings.setDatabaseEnabled(true);
357 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700358
359 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700360 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700361 settings.setAppCachePath(getAppCachePath());
362 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
363 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700364 // origin policy for file access
365 settings.setAllowUniversalAccessFromFileURLs(false);
366 settings.setAllowFileAccessFromFileURLs(false);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800367
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800368 //if (!(settings instanceof WebSettingsClassic)) return;
369 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800370
371 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
372 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
373 // WebView should be preserving the memory as much as possible.
374 // However, apps like browser wish to turn on the performance mode which
375 // would require more memory.
376 // TODO: We need to dynamically allocate/deallocate temporary memory for
377 // apps which are trying to use minimal memory. Currently, double
378 // buffering is always turned on, which is unnecessary.
379 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
380 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800381 */
John Reck35e9dd62011-04-25 09:01:54 -0700382 }
383
384 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700385 mNeedsSharedSync = false;
Axesh R. Ajmera579c70c2014-04-17 13:24:56 -0700386 CookieManager.getInstance().setAcceptCookie(acceptCookies());
387
John Reck35e9dd62011-04-25 09:01:54 -0700388 if (mController != null) {
389 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700390 }
John Reck35e9dd62011-04-25 09:01:54 -0700391 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700392
John Reck35e9dd62011-04-25 09:01:54 -0700393 private void syncManagedSettings() {
394 syncSharedSettings();
395 synchronized (mManagedSettings) {
396 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
397 while (iter.hasNext()) {
398 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800399 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700400 if (settings == null) {
401 iter.remove();
402 continue;
403 }
404 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000405 }
John Reck35e9dd62011-04-25 09:01:54 -0700406 }
407 }
Ben Murdochef671652010-11-25 17:17:58 +0000408
John Reck35e9dd62011-04-25 09:01:54 -0700409 @Override
410 public void onSharedPreferenceChanged(
411 SharedPreferences sharedPreferences, String key) {
412 syncManagedSettings();
413 if (PREF_SEARCH_ENGINE.equals(key)) {
414 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700415 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200416 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700417 mController.getUi().setFullscreen(useFullscreen());
418 }
Michael Kolb0241e752011-07-07 14:58:50 -0700419 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200420 if (mController != null && mController.getUi() != null) {
Michael Kolb0241e752011-07-07 14:58:50 -0700421 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
422 }
Victoria Lease96497832012-03-23 14:19:56 -0700423 } else if (PREF_LINK_PREFETCH.equals(key)) {
424 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700425 }
John Reck35e9dd62011-04-25 09:01:54 -0700426 }
427
John Reck961d35d2011-06-23 09:45:54 -0700428 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100429 requireInitialization();
430 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700431 }
432
433 public LayoutAlgorithm getLayoutAlgorithm() {
434 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
435 if (autofitPages()) {
Tarun Nainani003bec52014-07-02 02:20:34 -0700436 layoutAlgorithm = LayoutAlgorithm.TEXT_AUTOSIZING;
John Reck35e9dd62011-04-25 09:01:54 -0700437 }
438 if (isDebugEnabled()) {
439 if (isSmallScreen()) {
440 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000441 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700442 if (isNormalLayout()) {
443 layoutAlgorithm = LayoutAlgorithm.NORMAL;
444 } else {
445 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
446 }
Ben Murdochef671652010-11-25 17:17:58 +0000447 }
John Reck35e9dd62011-04-25 09:01:54 -0700448 }
449 return layoutAlgorithm;
450 }
Ben Murdochef671652010-11-25 17:17:58 +0000451
John Reck35e9dd62011-04-25 09:01:54 -0700452 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700453 requireInitialization();
454 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800455 }
456
John Reck35e9dd62011-04-25 09:01:54 -0700457 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700458 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700459 return mWebStorageSizeManager;
460 }
461
John Reck35e9dd62011-04-25 09:01:54 -0700462 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700463 if (mAppCachePath == null) {
464 mAppCachePath = mContext.getDir("appcache", 0).getPath();
465 }
466 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700467 }
468
469 private void updateSearchEngine(boolean force) {
470 String searchEngineName = getSearchEngineName();
471 if (force || mSearchEngine == null ||
472 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700473 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000474 }
475 }
476
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100477 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700478 if (mSearchEngine == null) {
479 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100480 }
John Reck35e9dd62011-04-25 09:01:54 -0700481 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100482 }
483
John Reck35e9dd62011-04-25 09:01:54 -0700484 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700485 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700486 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100487 }
488
John Reck35e9dd62011-04-25 09:01:54 -0700489 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700490 Editor edit = mPrefs.edit();
491 edit.putBoolean(PREF_DEBUG_MENU, value);
492 if (!value) {
493 // Reset to "safe" value
494 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
495 }
496 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100497 }
498
John Reck35e9dd62011-04-25 09:01:54 -0700499 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800500 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700501 if (mController != null) {
502 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800503 if (current != null) {
504 current.clearCache(true);
505 }
506 }
507 }
508
John Reck35e9dd62011-04-25 09:01:54 -0700509 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800510 CookieManager.getInstance().removeAllCookie();
511 }
512
John Reck35e9dd62011-04-25 09:01:54 -0700513 public void clearHistory() {
514 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800515 Browser.clearHistory(resolver);
516 Browser.clearSearches(resolver);
517 }
518
John Reck35e9dd62011-04-25 09:01:54 -0700519 public void clearFormData() {
520 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700521 if (mController!= null) {
522 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100523 if (currentTopView != null) {
524 currentTopView.clearFormData();
525 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800526 }
527 }
528
Bijan Amirzada3bbb3a42014-06-17 12:06:18 -0700529 public WebView getTopWebView(){
530 if (mController!= null)
531 return mController.getCurrentTopWebView();
532
533 return null;
534 }
535
John Reck35e9dd62011-04-25 09:01:54 -0700536 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700537 // Clear password store maintained by SWE engine
538 WebSettings settings = null;
539 // find a valid settings object
540 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
541 while (iter.hasNext()) {
542 WeakReference<WebSettings> ref = iter.next();
543 settings = (WebSettings)ref.get();
544 if (settings != null) {
545 break;
546 }
547 }
548 if (settings != null) {
Panos Thomasdbaea4e2014-05-22 06:48:47 -0700549 settings.clearPasswords();
Panos Thomasfa948b82014-03-09 03:42:42 -0700550 }
Vivek Sekhare8a8ec22014-07-10 14:52:43 -0700551
552 // Clear passwords in WebView database
553 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
554 db.clearHttpAuthUsernamePassword();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800555 }
556
John Reck35e9dd62011-04-25 09:01:54 -0700557 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100558 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100559 }
560
John Reck35e9dd62011-04-25 09:01:54 -0700561 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100562 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100563 }
564
John Reck35e9dd62011-04-25 09:01:54 -0700565 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700566 mPrefs.edit()
567 .clear()
John Reckbd315192011-07-29 10:05:47 -0700568 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200569 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700570 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700571 }
572
Björn Isakssonc885a242012-06-05 17:19:04 +0200573 private void resetCachedValues() {
574 updateSearchEngine(false);
575 }
576
John Reck35e9dd62011-04-25 09:01:54 -0700577 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800578 // query the profile from components autofill database 524
579 if (mAutofillHandler.mAutoFillProfile == null &&
580 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
581 WebSettings settings = null;
582 // find a valid settings object
583 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
584 while (iter.hasNext()) {
585 WeakReference<WebSettings> ref = iter.next();
586 settings = (WebSettings)ref.get();
587 if (settings != null) {
588 break;
589 }
590 }
591 if (settings != null) {
592 AutoFillProfile profile =
593 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
594 mAutofillHandler.setAutoFillProfile(profile);
595 }
596 }
John Reck35e9dd62011-04-25 09:01:54 -0700597 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800598 }
599
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800600 public String getAutoFillProfileId() {
601 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800602 }
603
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800604 public void updateAutoFillProfile(AutoFillProfile profile) {
605 syncAutoFillProfile(profile);
606 }
607
608 private void syncAutoFillProfile(AutoFillProfile profile) {
609 synchronized (mManagedSettings) {
610 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
611 while (iter.hasNext()) {
612 WeakReference<WebSettings> ref = iter.next();
613 WebSettings settings = (WebSettings)ref.get();
614 if (settings == null) {
615 iter.remove();
616 continue;
617 }
618 // update the profile only once.
619 settings.setAutoFillProfile(profile);
620 // Now we should have the guid
621 mAutofillHandler.setAutoFillProfile(profile);
622 break;
623 }
624 }
625 }
John Reck35e9dd62011-04-25 09:01:54 -0700626 public void toggleDebugSettings() {
627 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800628 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100629
John Reckb8b2af82011-05-20 15:58:33 -0700630 public boolean hasDesktopUseragent(WebView view) {
631 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
632 }
633
634 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700635 if (view == null) {
636 return;
637 }
John Reckb8b2af82011-05-20 15:58:33 -0700638 WebSettings settings = view.getSettings();
639 if (mCustomUserAgents.get(settings) != null) {
640 mCustomUserAgents.remove(settings);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700641 setUserAgent(settings);
John Reckb8b2af82011-05-20 15:58:33 -0700642 } else {
643 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
644 settings.setUserAgentString(DESKTOP_USERAGENT);
645 }
646 }
647
John Reck7dc444b2011-06-16 17:44:29 -0700648 public static int getAdjustedMinimumFontSize(int rawValue) {
649 rawValue++; // Preference starts at 0, min font at 1
650 if (rawValue > 1) {
651 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
652 }
653 return rawValue;
654 }
655
John Reck5ba3c762011-09-14 15:00:15 -0700656 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700657 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700658 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700659 }
660
661 static int getRawTextZoom(int percent) {
662 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
663 }
664
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700665 public int getAdjustedDoubleTapZoom(int rawValue) {
666 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
667 return (int) ((rawValue + 100) * mFontSizeMult);
668 }
669
670 static int getRawDoubleTapZoom(int percent) {
671 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
672 }
673
John Reckcadae722011-07-25 11:36:17 -0700674 public SharedPreferences getPreferences() {
675 return mPrefs;
676 }
677
Victoria Lease96497832012-03-23 14:19:56 -0700678 // update connectivity-dependent options
679 public void updateConnectionType() {
680 ConnectivityManager cm = (ConnectivityManager)
681 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
682 String linkPrefetchPreference = getLinkPrefetchEnabled();
683 boolean linkPrefetchAllowed = linkPrefetchPreference.
684 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
685 NetworkInfo ni = cm.getActiveNetworkInfo();
686 if (ni != null) {
687 switch (ni.getType()) {
688 case ConnectivityManager.TYPE_WIFI:
689 case ConnectivityManager.TYPE_ETHERNET:
690 case ConnectivityManager.TYPE_BLUETOOTH:
691 linkPrefetchAllowed |= linkPrefetchPreference.
692 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
693 break;
694 case ConnectivityManager.TYPE_MOBILE:
695 case ConnectivityManager.TYPE_MOBILE_DUN:
696 case ConnectivityManager.TYPE_MOBILE_MMS:
697 case ConnectivityManager.TYPE_MOBILE_SUPL:
698 case ConnectivityManager.TYPE_WIMAX:
699 default:
700 break;
701 }
702 }
703 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
704 mLinkPrefetchAllowed = linkPrefetchAllowed;
705 syncManagedSettings();
706 }
707 }
708
luxiaol62677b02013-07-22 07:54:49 +0800709 public String getDownloadPath() {
710 return mPrefs.getString(PREF_DOWNLOAD_PATH,
711 DownloadHandler.getDefaultDownloadPath(mContext));
712 }
John Reck35e9dd62011-04-25 09:01:54 -0700713 // -----------------------------
714 // getter/setters for accessibility_preferences.xml
715 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100716
John Reck7dc444b2011-06-16 17:44:29 -0700717 @Deprecated
718 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700719 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
720 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100721 }
722
John Reck35e9dd62011-04-25 09:01:54 -0700723 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700724 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700725 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100726 }
727
John Reck92f25f82011-04-26 16:57:10 -0700728 public boolean forceEnableUserScalable() {
729 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
730 }
731
John Reck7dc444b2011-06-16 17:44:29 -0700732 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700733 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700734 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
735 return getAdjustedTextZoom(textZoom);
736 }
737
738 public void setTextZoom(int percent) {
739 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
740 }
741
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700742 public int getDoubleTapZoom() {
743 requireInitialization();
744 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
745 return getAdjustedDoubleTapZoom(doubleTapZoom);
746 }
747
748 public void setDoubleTapZoom(int percent) {
749 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
750 }
751
John Reck35e9dd62011-04-25 09:01:54 -0700752 // -----------------------------
753 // getter/setters for advanced_preferences.xml
754 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100755
John Reck35e9dd62011-04-25 09:01:54 -0700756 public String getSearchEngineName() {
luxiaol221b3932013-07-19 15:27:57 +0800757 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
758 if (defaultSearchEngineValue == null) {
759 defaultSearchEngineValue = SearchEngine.GOOGLE;
760 }
761 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100762 }
John Reck63bb6872010-12-01 19:29:32 -0800763
Michael Kolb8d772b02012-01-19 13:56:00 -0800764 public boolean allowAppTabs() {
765 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
766 }
767
John Reck35e9dd62011-04-25 09:01:54 -0700768 public boolean openInBackground() {
769 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800770 }
John Reck35e9dd62011-04-25 09:01:54 -0700771
772 public boolean enableJavascript() {
773 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
774 }
775
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800776 public boolean enableMemoryMonitor() {
777 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
778 }
779
John Reck35e9dd62011-04-25 09:01:54 -0700780 // TODO: Cache
John Reck35e9dd62011-04-25 09:01:54 -0700781 public ZoomDensity getDefaultZoom() {
782 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700783 ZoomDensity zoomDensity = ZoomDensity.valueOf(zoom);
784 //SWE: If zoomDensity is not MEDIUM, set enableWideViewport to false.
785 if(zoomDensity != ZoomDensity.MEDIUM)
786 enableWideViewport = false;
787 else
788 enableWideViewport = true;
789 return zoomDensity;
John Reck35e9dd62011-04-25 09:01:54 -0700790 }
791
792 public boolean loadPageInOverviewMode() {
793 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
794 }
795
796 public boolean autofitPages() {
797 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
798 }
799
800 public boolean blockPopupWindows() {
801 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
802 }
803
804 public boolean loadImages() {
805 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
806 }
807
808 public String getDefaultTextEncoding() {
809 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
810 }
811
812 // -----------------------------
813 // getter/setters for general_preferences.xml
814 // -----------------------------
815
816 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700817 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
818 }
819
820 public void setHomePage(String value) {
821 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
822 }
823
824 public boolean isAutofillEnabled() {
825 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
826 }
827
828 public void setAutofillEnabled(boolean value) {
829 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
830 }
831
832 // -----------------------------
833 // getter/setters for debug_preferences.xml
834 // -----------------------------
835
836 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700837 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700838 return true;
839 }
840 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
841 }
842
Derek Sollenberger31adf672011-07-08 11:31:30 -0400843 public boolean isSkiaHardwareAccelerated() {
844 if (!isDebugEnabled()) {
845 return false;
846 }
847 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
848 }
849
John Reck35e9dd62011-04-25 09:01:54 -0700850 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700851 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700852 return 0;
853 }
854 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
855 }
856
857 // -----------------------------
858 // getter/setters for hidden_debug_preferences.xml
859 // -----------------------------
860
861 public boolean enableVisualIndicator() {
862 if (!isDebugEnabled()) {
863 return false;
864 }
865 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
866 }
867
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700868 public boolean enableCpuUploadPath() {
869 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700870 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700871 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700872 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700873 }
874
John Reck35e9dd62011-04-25 09:01:54 -0700875 public boolean enableJavascriptConsole() {
876 if (!isDebugEnabled()) {
877 return false;
878 }
879 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
880 }
881
882 public boolean isSmallScreen() {
883 if (!isDebugEnabled()) {
884 return false;
885 }
886 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
887 }
888
889 public boolean isWideViewport() {
890 if (!isDebugEnabled()) {
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700891 return enableWideViewport;
John Reck35e9dd62011-04-25 09:01:54 -0700892 }
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700893 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, enableWideViewport);
John Reck35e9dd62011-04-25 09:01:54 -0700894 }
895
896 public boolean isNormalLayout() {
897 if (!isDebugEnabled()) {
898 return false;
899 }
900 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
901 }
902
903 public boolean isTracing() {
904 if (!isDebugEnabled()) {
905 return false;
906 }
907 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
908 }
909
910 public boolean enableLightTouch() {
911 if (!isDebugEnabled()) {
912 return false;
913 }
914 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
915 }
916
917 public boolean enableNavDump() {
918 if (!isDebugEnabled()) {
919 return false;
920 }
921 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
922 }
923
924 public String getJsEngineFlags() {
925 if (!isDebugEnabled()) {
926 return "";
927 }
928 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
929 }
930
931 // -----------------------------
932 // getter/setters for lab_preferences.xml
933 // -----------------------------
934
935 public boolean useQuickControls() {
936 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
937 }
938
939 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700940 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700941 }
942
Michael Kolbc38c6042011-04-27 10:46:06 -0700943 public boolean useFullscreen() {
944 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
945 }
946
John Reck2fd9d0e2011-07-15 11:13:48 -0700947 public boolean useInvertedRendering() {
948 return mPrefs.getBoolean(PREF_INVERTED, false);
949 }
950
Nicolas Roard5d513102011-08-03 15:35:34 -0700951 public float getInvertedContrast() {
952 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
953 }
954
John Reck35e9dd62011-04-25 09:01:54 -0700955 // -----------------------------
956 // getter/setters for privacy_security_preferences.xml
957 // -----------------------------
958
959 public boolean showSecurityWarnings() {
960 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
961 }
962
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700963 public boolean doNotTrack() {
964 return mPrefs.getBoolean(PREF_DO_NOT_TRACK, true);
965 }
966
John Reck35e9dd62011-04-25 09:01:54 -0700967 public boolean acceptCookies() {
968 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
969 }
970
971 public boolean saveFormdata() {
972 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
973 }
974
975 public boolean enableGeolocation() {
976 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
977 }
978
979 public boolean rememberPasswords() {
980 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
981 }
982
Michael Kolb14612442011-06-24 13:06:29 -0700983 // -----------------------------
984 // getter/setters for bandwidth_preferences.xml
985 // -----------------------------
986
Mathew Inwood467813f2011-09-02 15:43:17 +0100987 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
988 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -0700989 }
Mathew Inwood467813f2011-09-02 15:43:17 +0100990
991 public static String getPreloadAlwaysPreferenceString(Context context) {
992 return context.getResources().getString(R.string.pref_data_preload_value_always);
993 }
994
Mathew Inwood825fba72011-10-04 14:52:52 +0100995 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
996 "browser_default_preload_setting";
997
998 public String getDefaultPreloadSetting() {
999 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1000 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1001 if (preload == null) {
1002 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1003 }
1004 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001005 }
1006
1007 public String getPreloadEnabled() {
1008 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1009 }
1010
Victoria Lease96497832012-03-23 14:19:56 -07001011 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1012 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1013 }
1014
1015 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1016 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1017 }
1018
1019 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1020 "browser_default_link_prefetch_setting";
1021
1022 public String getDefaultLinkPrefetchSetting() {
1023 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1024 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1025 if (preload == null) {
1026 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1027 }
1028 return preload;
1029 }
1030
1031 public String getLinkPrefetchEnabled() {
1032 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1033 }
1034
George Mount3636d0a2011-11-21 09:08:21 -08001035 // -----------------------------
1036 // getter/setters for browser recovery
1037 // -----------------------------
1038 /**
1039 * The last time browser was started.
1040 * @return The last browser start time as System.currentTimeMillis. This
1041 * can be 0 if this is the first time or the last tab was closed.
1042 */
1043 public long getLastRecovered() {
1044 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1045 }
1046
1047 /**
1048 * Sets the last browser start time.
1049 * @param time The last time as System.currentTimeMillis that the browser
1050 * was started. This should be set to 0 if the last tab is closed.
1051 */
1052 public void setLastRecovered(long time) {
1053 mPrefs.edit()
1054 .putLong(KEY_LAST_RECOVERED, time)
1055 .apply();
1056 }
1057
1058 /**
1059 * Used to determine whether or not the previous browser run crashed. Once
1060 * the previous state has been determined, the value will be set to false
1061 * until a pause is received.
1062 * @return true if the last browser run was paused or false if it crashed.
1063 */
1064 public boolean wasLastRunPaused() {
1065 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1066 }
1067
1068 /**
1069 * Sets whether or not the last run was a pause or crash.
1070 * @param isPaused Set to true When a pause is received or false after
1071 * resuming.
1072 */
1073 public void setLastRunPaused(boolean isPaused) {
1074 mPrefs.edit()
1075 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1076 .apply();
1077 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001078}