blob: 74765d5945d19b9d9a3cd1f2f6f630708c84f52c [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;
Ben Murdoch0cb81892010-10-08 12:41:33 +010028import android.provider.Browser;
Mathew Inwood825fba72011-10-04 14:52:52 +010029import android.provider.Settings;
Bijan Amirzadae75909d2014-05-06 14:18:54 -070030import android.text.TextUtils;
John Reck5ba3c762011-09-14 15:00:15 -070031import android.util.DisplayMetrics;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.webkit.WebIconDatabase;
Nicolas Roard78a98e42009-05-11 13:34:17 +010033import android.webkit.WebStorage;
John Reck812d2d62011-01-18 14:16:15 -080034import android.webkit.WebViewDatabase;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035
Bijan Amirzada41242f22014-03-21 12:12:18 -070036import com.android.browser.R;
37import com.android.browser.homepages.HomeProvider;
38import com.android.browser.provider.BrowserProvider;
39import com.android.browser.reflect.ReflectHelper;
40import com.android.browser.search.SearchEngine;
41import com.android.browser.search.SearchEngines;
John Reck46500332011-06-07 14:36:10 -070042
John Reck35e9dd62011-04-25 09:01:54 -070043import java.lang.ref.WeakReference;
44import java.util.Iterator;
45import java.util.LinkedList;
qqzhou8c5b0a32013-07-22 15:31:03 +080046import java.util.Locale;
John Reckb8b2af82011-05-20 15:58:33 -070047import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080048
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080049import org.codeaurora.swe.AutoFillProfile;
50import org.codeaurora.swe.CookieManager;
51import org.codeaurora.swe.GeolocationPermissions;
52import org.codeaurora.swe.WebSettings.LayoutAlgorithm;
53import org.codeaurora.swe.WebSettings.PluginState;
54import org.codeaurora.swe.WebSettings.TextSize;
55import org.codeaurora.swe.WebSettings.ZoomDensity;
56import org.codeaurora.swe.WebSettings;
57import org.codeaurora.swe.WebView;
58
John Reck35e9dd62011-04-25 09:01:54 -070059/**
60 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080061 */
John Reck35e9dd62011-04-25 09:01:54 -070062public class BrowserSettings implements OnSharedPreferenceChangeListener,
63 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010064
John Reck35e9dd62011-04-25 09:01:54 -070065 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070066 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
67 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
68 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080069
70 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070071 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
72 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070073
74 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070075 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
76 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070077
78 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070079 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
80 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080081
John Reck7fd1e8f2011-04-27 18:22:57 -070082 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
83 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
84 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
85
John Reck35e9dd62011-04-25 09:01:54 -070086 private static final String USER_AGENTS[] = { null,
87 DESKTOP_USERAGENT,
88 IPHONE_USERAGENT,
89 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070090 FROYO_USERAGENT,
91 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070092 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080093
qqzhoue6ff8b42013-07-23 17:28:48 +080094 private static final String TAG = "BrowserSettings";
John Reck8fc22a12011-06-16 14:57:41 -070095 // The minimum min font size
96 // Aka, the lower bounds for the min font size range
97 // which is 1:5..24
98 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070099 // The initial value in the text zoom range
100 // This is what represents 100% in the SeekBarPreference range
101 private static final int TEXT_ZOOM_START_VAL = 10;
102 // The size of a single step in the text zoom range, in percent
103 private static final int TEXT_ZOOM_STEP = 5;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700104 // The initial value in the double tap zoom range
105 // This is what represents 100% in the SeekBarPreference range
106 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
107 // The size of a single step in the double tap zoom range, in percent
108 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -0700109
John Reck35e9dd62011-04-25 09:01:54 -0700110 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -0700111
John Reck35e9dd62011-04-25 09:01:54 -0700112 private Context mContext;
113 private SharedPreferences mPrefs;
114 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700116 private WebStorageSizeManager mWebStorageSizeManager;
117 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700118 private WeakHashMap<WebSettings, String> mCustomUserAgents;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100119 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -0700120 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -0700121 private float mFontSizeMult = 1.0f;
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700122 private boolean enableWideViewport = true;
John Reck78a6a1d2011-07-21 13:54:03 -0700123
Victoria Lease96497832012-03-23 14:19:56 -0700124 // Current state of network-dependent settings
125 private boolean mLinkPrefetchAllowed = true;
126
John Reck78a6a1d2011-07-21 13:54:03 -0700127 // Cached values
128 private int mPageCacheCapacity = 1;
129 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800130
John Reck35e9dd62011-04-25 09:01:54 -0700131 // Cached settings
132 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800133
Ben Murdochaaa1f372011-07-25 15:40:58 +0100134 private static String sFactoryResetUrl;
135
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700136 public static void initialize(final Context context) {
John Reck35e9dd62011-04-25 09:01:54 -0700137 sInstance = new BrowserSettings(context);
138 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800139
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700140 public static BrowserSettings getInstance() {
John Reck35e9dd62011-04-25 09:01:54 -0700141 return sInstance;
142 }
Ben Murdochef671652010-11-25 17:17:58 +0000143
John Reck35e9dd62011-04-25 09:01:54 -0700144 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100145 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700146 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
147 mAutofillHandler = new AutofillHandler(mContext);
148 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700149 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reckcadae722011-07-25 11:36:17 -0700150 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700151 }
152
153 public void setController(Controller controller) {
154 mController = controller;
John Reckc477e712011-08-17 11:27:15 -0700155 if (sInitialized) {
156 syncSharedSettings();
157 }
Ben Murdochef671652010-11-25 17:17:58 +0000158 }
159
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800160 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800161
John Reckc477e712011-08-17 11:27:15 -0700162 if (mNeedsSharedSync) {
163 syncSharedSettings();
164 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800165
John Reck35e9dd62011-04-25 09:01:54 -0700166 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800167 syncStaticSettings(settings);
168 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700169 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800170 }
171 }
172
John Reckd1d87312012-03-08 13:25:00 -0800173 public void stopManagingSettings(WebSettings settings) {
174 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
175 while (iter.hasNext()) {
176 WeakReference<WebSettings> ref = iter.next();
177 if (ref.get() == settings) {
178 iter.remove();
179 return;
180 }
181 }
182 }
183
John Reckcadae722011-07-25 11:36:17 -0700184 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700185
186 @Override
187 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700188 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
189 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700190 // the cost of one cached page is ~3M (measured using nytimes.com). For
191 // low end devices, we only cache one page. For high end devices, we try
192 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800193
194 // SWE_TODO : assume a high-memory device
195 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700196 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800197 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700198 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
199 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
200 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700201 // Workaround b/5254577
202 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700203 if (Build.VERSION.CODENAME.equals("REL")) {
204 // This is a release build, always startup with debug disabled
205 setDebugEnabled(false);
206 }
207 if (mPrefs.contains(PREF_TEXT_SIZE)) {
208 /*
209 * Update from TextSize enum to zoom percent
210 * SMALLEST is 50%
211 * SMALLER is 75%
212 * NORMAL is 100%
213 * LARGER is 150%
214 * LARGEST is 200%
215 */
216 switch (getTextSize()) {
217 case SMALLEST:
218 setTextZoom(50);
219 break;
220 case SMALLER:
221 setTextZoom(75);
222 break;
223 case LARGER:
224 setTextZoom(150);
225 break;
226 case LARGEST:
227 setTextZoom(200);
228 break;
229 }
230 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
231 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100232
qqzhou8c5b0a32013-07-22 15:31:03 +0800233 // add for carrier homepage feature
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700234 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
qqzhou8c5b0a32013-07-22 15:31:03 +0800235
kaiyizbf086ea2013-08-02 11:03:58 +0800236 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700237 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING, "UTF-8").apply();
kaiyizbf086ea2013-08-02 11:03:58 +0800238 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700239
Ben Murdochaaa1f372011-07-25 15:40:58 +0100240 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
241 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
242 BrowserProvider.getClientId(mContext.getContentResolver()));
243 }
244
Ben Murdochaaa1f372011-07-25 15:40:58 +0100245 synchronized (BrowserSettings.class) {
246 sInitialized = true;
247 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700248 }
249 }
250 };
251
Ben Murdochaaa1f372011-07-25 15:40:58 +0100252 private static void requireInitialization() {
253 synchronized (BrowserSettings.class) {
254 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700255 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100256 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700257 } catch (InterruptedException e) {
258 }
259 }
260 }
261 }
262
The Android Open Source Project0c908882009-03-03 19:32:16 -0800263 /**
John Reck35e9dd62011-04-25 09:01:54 -0700264 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800265 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800266 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700267 settings.setGeolocationEnabled(enableGeolocation());
268 settings.setJavaScriptEnabled(enableJavascript());
269 settings.setLightTouchEnabled(enableLightTouch());
270 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700271 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
272 settings.setDefaultZoom(getDefaultZoom());
273 settings.setMinimumFontSize(getMinimumFontSize());
274 settings.setMinimumLogicalFontSize(getMinimumFontSize());
275 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700276 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700277 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100278 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700279 settings.setLoadsImagesAutomatically(loadImages());
280 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
281 settings.setSavePassword(rememberPasswords());
282 settings.setSaveFormData(saveFormdata());
283 settings.setUseWideViewPort(isWideViewport());
Panos Thomasb10bbda2014-06-23 10:18:36 -0700284 settings.setDoNotTrack(doNotTrack());
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700285 setUserAgent(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
326 private String constructUserAgent(String userAgent) {
327 try {
328 userAgent = userAgent.replaceAll("<%build_model>", Build.MODEL);
329 userAgent = userAgent.replaceAll("<%build_version>", Build.VERSION.RELEASE);
330 userAgent = userAgent.replaceAll("<%build_id>", Build.ID);
331 userAgent = userAgent.replaceAll("<%language>", Locale.getDefault().getLanguage());
332 userAgent = userAgent.replaceAll("<%country>", Locale.getDefault().getCountry());
333 return userAgent;
334 } catch (Exception ex) {
335 return null;
336 }
337 }
338
John Reck35e9dd62011-04-25 09:01:54 -0700339 /**
340 * Syncs all the settings that have no UI
341 * These cannot change, so we only need to set them once per WebSettings
342 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800343 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700344 settings.setDefaultFontSize(16);
345 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000346
John Reck35e9dd62011-04-25 09:01:54 -0700347 // WebView inside Browser doesn't want initial focus to be set.
348 settings.setNeedInitialFocus(false);
349 // Browser supports multiple windows
350 settings.setSupportMultipleWindows(true);
351 // enable smooth transition for better performance during panning or
352 // zooming
353 settings.setEnableSmoothTransition(true);
354 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700355 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700356
357 // HTML5 API flags
358 settings.setAppCacheEnabled(true);
359 settings.setDatabaseEnabled(true);
360 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700361
362 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700363 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700364 settings.setAppCachePath(getAppCachePath());
365 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
366 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700367 // origin policy for file access
368 settings.setAllowUniversalAccessFromFileURLs(false);
369 settings.setAllowFileAccessFromFileURLs(false);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800370
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800371 //if (!(settings instanceof WebSettingsClassic)) return;
372 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800373
374 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
375 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
376 // WebView should be preserving the memory as much as possible.
377 // However, apps like browser wish to turn on the performance mode which
378 // would require more memory.
379 // TODO: We need to dynamically allocate/deallocate temporary memory for
380 // apps which are trying to use minimal memory. Currently, double
381 // buffering is always turned on, which is unnecessary.
382 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
383 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800384 */
John Reck35e9dd62011-04-25 09:01:54 -0700385 }
386
387 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700388 mNeedsSharedSync = false;
Axesh R. Ajmera579c70c2014-04-17 13:24:56 -0700389 CookieManager.getInstance().setAcceptCookie(acceptCookies());
390
John Reck35e9dd62011-04-25 09:01:54 -0700391 if (mController != null) {
392 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700393 }
John Reck35e9dd62011-04-25 09:01:54 -0700394 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700395
John Reck35e9dd62011-04-25 09:01:54 -0700396 private void syncManagedSettings() {
397 syncSharedSettings();
398 synchronized (mManagedSettings) {
399 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
400 while (iter.hasNext()) {
401 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800402 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700403 if (settings == null) {
404 iter.remove();
405 continue;
406 }
407 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000408 }
John Reck35e9dd62011-04-25 09:01:54 -0700409 }
410 }
Ben Murdochef671652010-11-25 17:17:58 +0000411
John Reck35e9dd62011-04-25 09:01:54 -0700412 @Override
413 public void onSharedPreferenceChanged(
414 SharedPreferences sharedPreferences, String key) {
415 syncManagedSettings();
416 if (PREF_SEARCH_ENGINE.equals(key)) {
417 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700418 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200419 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700420 mController.getUi().setFullscreen(useFullscreen());
421 }
Michael Kolb0241e752011-07-07 14:58:50 -0700422 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200423 if (mController != null && mController.getUi() != null) {
Michael Kolb0241e752011-07-07 14:58:50 -0700424 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
425 }
Victoria Lease96497832012-03-23 14:19:56 -0700426 } else if (PREF_LINK_PREFETCH.equals(key)) {
427 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700428 }
John Reck35e9dd62011-04-25 09:01:54 -0700429 }
430
John Reck961d35d2011-06-23 09:45:54 -0700431 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100432 requireInitialization();
433 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700434 }
435
436 public LayoutAlgorithm getLayoutAlgorithm() {
437 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
438 if (autofitPages()) {
439 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
440 }
441 if (isDebugEnabled()) {
442 if (isSmallScreen()) {
443 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000444 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700445 if (isNormalLayout()) {
446 layoutAlgorithm = LayoutAlgorithm.NORMAL;
447 } else {
448 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
449 }
Ben Murdochef671652010-11-25 17:17:58 +0000450 }
John Reck35e9dd62011-04-25 09:01:54 -0700451 }
452 return layoutAlgorithm;
453 }
Ben Murdochef671652010-11-25 17:17:58 +0000454
John Reck35e9dd62011-04-25 09:01:54 -0700455 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700456 requireInitialization();
457 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800458 }
459
John Reck35e9dd62011-04-25 09:01:54 -0700460 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700461 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700462 return mWebStorageSizeManager;
463 }
464
John Reck35e9dd62011-04-25 09:01:54 -0700465 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700466 if (mAppCachePath == null) {
467 mAppCachePath = mContext.getDir("appcache", 0).getPath();
468 }
469 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700470 }
471
472 private void updateSearchEngine(boolean force) {
473 String searchEngineName = getSearchEngineName();
474 if (force || mSearchEngine == null ||
475 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700476 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000477 }
478 }
479
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100480 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700481 if (mSearchEngine == null) {
482 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100483 }
John Reck35e9dd62011-04-25 09:01:54 -0700484 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100485 }
486
John Reck35e9dd62011-04-25 09:01:54 -0700487 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700488 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700489 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100490 }
491
John Reck35e9dd62011-04-25 09:01:54 -0700492 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700493 Editor edit = mPrefs.edit();
494 edit.putBoolean(PREF_DEBUG_MENU, value);
495 if (!value) {
496 // Reset to "safe" value
497 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
498 }
499 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100500 }
501
John Reck35e9dd62011-04-25 09:01:54 -0700502 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800503 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 if (mController != null) {
505 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800506 if (current != null) {
507 current.clearCache(true);
508 }
509 }
510 }
511
John Reck35e9dd62011-04-25 09:01:54 -0700512 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800513 CookieManager.getInstance().removeAllCookie();
514 }
515
John Reck35e9dd62011-04-25 09:01:54 -0700516 public void clearHistory() {
517 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800518 Browser.clearHistory(resolver);
519 Browser.clearSearches(resolver);
520 }
521
John Reck35e9dd62011-04-25 09:01:54 -0700522 public void clearFormData() {
523 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700524 if (mController!= null) {
525 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100526 if (currentTopView != null) {
527 currentTopView.clearFormData();
528 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800529 }
530 }
531
Bijan Amirzada3bbb3a42014-06-17 12:06:18 -0700532 public WebView getTopWebView(){
533 if (mController!= null)
534 return mController.getCurrentTopWebView();
535
536 return null;
537 }
538
John Reck35e9dd62011-04-25 09:01:54 -0700539 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700540 // Clear password store maintained by SWE engine
541 WebSettings settings = null;
542 // find a valid settings object
543 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
544 while (iter.hasNext()) {
545 WeakReference<WebSettings> ref = iter.next();
546 settings = (WebSettings)ref.get();
547 if (settings != null) {
548 break;
549 }
550 }
551 if (settings != null) {
Panos Thomasdbaea4e2014-05-22 06:48:47 -0700552 settings.clearPasswords();
Panos Thomasfa948b82014-03-09 03:42:42 -0700553 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800554 }
555
John Reck35e9dd62011-04-25 09:01:54 -0700556 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100557 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100558 }
559
John Reck35e9dd62011-04-25 09:01:54 -0700560 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100561 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100562 }
563
John Reck35e9dd62011-04-25 09:01:54 -0700564 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700565 mPrefs.edit()
566 .clear()
John Reckbd315192011-07-29 10:05:47 -0700567 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200568 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700569 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700570 }
571
Björn Isakssonc885a242012-06-05 17:19:04 +0200572 private void resetCachedValues() {
573 updateSearchEngine(false);
574 }
575
John Reck35e9dd62011-04-25 09:01:54 -0700576 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800577 // query the profile from components autofill database 524
578 if (mAutofillHandler.mAutoFillProfile == null &&
579 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
580 WebSettings settings = null;
581 // find a valid settings object
582 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
583 while (iter.hasNext()) {
584 WeakReference<WebSettings> ref = iter.next();
585 settings = (WebSettings)ref.get();
586 if (settings != null) {
587 break;
588 }
589 }
590 if (settings != null) {
591 AutoFillProfile profile =
592 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
593 mAutofillHandler.setAutoFillProfile(profile);
594 }
595 }
John Reck35e9dd62011-04-25 09:01:54 -0700596 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800597 }
598
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800599 public String getAutoFillProfileId() {
600 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800601 }
602
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800603 public void updateAutoFillProfile(AutoFillProfile profile) {
604 syncAutoFillProfile(profile);
605 }
606
607 private void syncAutoFillProfile(AutoFillProfile profile) {
608 synchronized (mManagedSettings) {
609 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
610 while (iter.hasNext()) {
611 WeakReference<WebSettings> ref = iter.next();
612 WebSettings settings = (WebSettings)ref.get();
613 if (settings == null) {
614 iter.remove();
615 continue;
616 }
617 // update the profile only once.
618 settings.setAutoFillProfile(profile);
619 // Now we should have the guid
620 mAutofillHandler.setAutoFillProfile(profile);
621 break;
622 }
623 }
624 }
John Reck35e9dd62011-04-25 09:01:54 -0700625 public void toggleDebugSettings() {
626 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800627 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100628
John Reckb8b2af82011-05-20 15:58:33 -0700629 public boolean hasDesktopUseragent(WebView view) {
630 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
631 }
632
633 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700634 if (view == null) {
635 return;
636 }
John Reckb8b2af82011-05-20 15:58:33 -0700637 WebSettings settings = view.getSettings();
638 if (mCustomUserAgents.get(settings) != null) {
639 mCustomUserAgents.remove(settings);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700640 setUserAgent(settings);
John Reckb8b2af82011-05-20 15:58:33 -0700641 } else {
642 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
643 settings.setUserAgentString(DESKTOP_USERAGENT);
644 }
645 }
646
John Reck7dc444b2011-06-16 17:44:29 -0700647 public static int getAdjustedMinimumFontSize(int rawValue) {
648 rawValue++; // Preference starts at 0, min font at 1
649 if (rawValue > 1) {
650 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
651 }
652 return rawValue;
653 }
654
John Reck5ba3c762011-09-14 15:00:15 -0700655 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700656 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700657 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700658 }
659
660 static int getRawTextZoom(int percent) {
661 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
662 }
663
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700664 public int getAdjustedDoubleTapZoom(int rawValue) {
665 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
666 return (int) ((rawValue + 100) * mFontSizeMult);
667 }
668
669 static int getRawDoubleTapZoom(int percent) {
670 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
671 }
672
John Reckcadae722011-07-25 11:36:17 -0700673 public SharedPreferences getPreferences() {
674 return mPrefs;
675 }
676
Victoria Lease96497832012-03-23 14:19:56 -0700677 // update connectivity-dependent options
678 public void updateConnectionType() {
679 ConnectivityManager cm = (ConnectivityManager)
680 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
681 String linkPrefetchPreference = getLinkPrefetchEnabled();
682 boolean linkPrefetchAllowed = linkPrefetchPreference.
683 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
684 NetworkInfo ni = cm.getActiveNetworkInfo();
685 if (ni != null) {
686 switch (ni.getType()) {
687 case ConnectivityManager.TYPE_WIFI:
688 case ConnectivityManager.TYPE_ETHERNET:
689 case ConnectivityManager.TYPE_BLUETOOTH:
690 linkPrefetchAllowed |= linkPrefetchPreference.
691 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
692 break;
693 case ConnectivityManager.TYPE_MOBILE:
694 case ConnectivityManager.TYPE_MOBILE_DUN:
695 case ConnectivityManager.TYPE_MOBILE_MMS:
696 case ConnectivityManager.TYPE_MOBILE_SUPL:
697 case ConnectivityManager.TYPE_WIMAX:
698 default:
699 break;
700 }
701 }
702 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
703 mLinkPrefetchAllowed = linkPrefetchAllowed;
704 syncManagedSettings();
705 }
706 }
707
luxiaol62677b02013-07-22 07:54:49 +0800708 public String getDownloadPath() {
709 return mPrefs.getString(PREF_DOWNLOAD_PATH,
710 DownloadHandler.getDefaultDownloadPath(mContext));
711 }
John Reck35e9dd62011-04-25 09:01:54 -0700712 // -----------------------------
713 // getter/setters for accessibility_preferences.xml
714 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100715
John Reck7dc444b2011-06-16 17:44:29 -0700716 @Deprecated
717 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700718 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
719 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100720 }
721
John Reck35e9dd62011-04-25 09:01:54 -0700722 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700723 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700724 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100725 }
726
John Reck92f25f82011-04-26 16:57:10 -0700727 public boolean forceEnableUserScalable() {
728 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
729 }
730
John Reck7dc444b2011-06-16 17:44:29 -0700731 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700732 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700733 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
734 return getAdjustedTextZoom(textZoom);
735 }
736
737 public void setTextZoom(int percent) {
738 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
739 }
740
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700741 public int getDoubleTapZoom() {
742 requireInitialization();
743 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
744 return getAdjustedDoubleTapZoom(doubleTapZoom);
745 }
746
747 public void setDoubleTapZoom(int percent) {
748 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
749 }
750
John Reck35e9dd62011-04-25 09:01:54 -0700751 // -----------------------------
752 // getter/setters for advanced_preferences.xml
753 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100754
John Reck35e9dd62011-04-25 09:01:54 -0700755 public String getSearchEngineName() {
luxiaol221b3932013-07-19 15:27:57 +0800756 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
757 if (defaultSearchEngineValue == null) {
758 defaultSearchEngineValue = SearchEngine.GOOGLE;
759 }
760 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100761 }
John Reck63bb6872010-12-01 19:29:32 -0800762
Michael Kolb8d772b02012-01-19 13:56:00 -0800763 public boolean allowAppTabs() {
764 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
765 }
766
John Reck35e9dd62011-04-25 09:01:54 -0700767 public boolean openInBackground() {
768 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800769 }
John Reck35e9dd62011-04-25 09:01:54 -0700770
771 public boolean enableJavascript() {
772 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
773 }
774
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800775 public boolean enableMemoryMonitor() {
776 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
777 }
778
John Reck35e9dd62011-04-25 09:01:54 -0700779 // TODO: Cache
780 public PluginState getPluginState() {
781 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
782 return PluginState.valueOf(state);
783 }
784
785 // TODO: Cache
786 public ZoomDensity getDefaultZoom() {
787 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700788 ZoomDensity zoomDensity = ZoomDensity.valueOf(zoom);
789 //SWE: If zoomDensity is not MEDIUM, set enableWideViewport to false.
790 if(zoomDensity != ZoomDensity.MEDIUM)
791 enableWideViewport = false;
792 else
793 enableWideViewport = true;
794 return zoomDensity;
John Reck35e9dd62011-04-25 09:01:54 -0700795 }
796
797 public boolean loadPageInOverviewMode() {
798 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
799 }
800
801 public boolean autofitPages() {
802 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
803 }
804
805 public boolean blockPopupWindows() {
806 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
807 }
808
809 public boolean loadImages() {
810 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
811 }
812
813 public String getDefaultTextEncoding() {
814 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
815 }
816
817 // -----------------------------
818 // getter/setters for general_preferences.xml
819 // -----------------------------
820
821 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700822 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
823 }
824
825 public void setHomePage(String value) {
826 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
827 }
828
829 public boolean isAutofillEnabled() {
830 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
831 }
832
833 public void setAutofillEnabled(boolean value) {
834 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
835 }
836
837 // -----------------------------
838 // getter/setters for debug_preferences.xml
839 // -----------------------------
840
841 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700842 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700843 return true;
844 }
845 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
846 }
847
Derek Sollenberger31adf672011-07-08 11:31:30 -0400848 public boolean isSkiaHardwareAccelerated() {
849 if (!isDebugEnabled()) {
850 return false;
851 }
852 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
853 }
854
John Reck35e9dd62011-04-25 09:01:54 -0700855 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700856 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700857 return 0;
858 }
859 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
860 }
861
862 // -----------------------------
863 // getter/setters for hidden_debug_preferences.xml
864 // -----------------------------
865
866 public boolean enableVisualIndicator() {
867 if (!isDebugEnabled()) {
868 return false;
869 }
870 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
871 }
872
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700873 public boolean enableCpuUploadPath() {
874 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700875 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700876 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700877 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700878 }
879
John Reck35e9dd62011-04-25 09:01:54 -0700880 public boolean enableJavascriptConsole() {
881 if (!isDebugEnabled()) {
882 return false;
883 }
884 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
885 }
886
887 public boolean isSmallScreen() {
888 if (!isDebugEnabled()) {
889 return false;
890 }
891 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
892 }
893
894 public boolean isWideViewport() {
895 if (!isDebugEnabled()) {
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700896 return enableWideViewport;
John Reck35e9dd62011-04-25 09:01:54 -0700897 }
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700898 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, enableWideViewport);
John Reck35e9dd62011-04-25 09:01:54 -0700899 }
900
901 public boolean isNormalLayout() {
902 if (!isDebugEnabled()) {
903 return false;
904 }
905 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
906 }
907
908 public boolean isTracing() {
909 if (!isDebugEnabled()) {
910 return false;
911 }
912 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
913 }
914
915 public boolean enableLightTouch() {
916 if (!isDebugEnabled()) {
917 return false;
918 }
919 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
920 }
921
922 public boolean enableNavDump() {
923 if (!isDebugEnabled()) {
924 return false;
925 }
926 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
927 }
928
929 public String getJsEngineFlags() {
930 if (!isDebugEnabled()) {
931 return "";
932 }
933 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
934 }
935
936 // -----------------------------
937 // getter/setters for lab_preferences.xml
938 // -----------------------------
939
940 public boolean useQuickControls() {
941 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
942 }
943
944 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700945 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700946 }
947
Michael Kolbc38c6042011-04-27 10:46:06 -0700948 public boolean useFullscreen() {
949 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
950 }
951
John Reck2fd9d0e2011-07-15 11:13:48 -0700952 public boolean useInvertedRendering() {
953 return mPrefs.getBoolean(PREF_INVERTED, false);
954 }
955
Nicolas Roard5d513102011-08-03 15:35:34 -0700956 public float getInvertedContrast() {
957 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
958 }
959
John Reck35e9dd62011-04-25 09:01:54 -0700960 // -----------------------------
961 // getter/setters for privacy_security_preferences.xml
962 // -----------------------------
963
964 public boolean showSecurityWarnings() {
965 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
966 }
967
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700968 public boolean doNotTrack() {
969 return mPrefs.getBoolean(PREF_DO_NOT_TRACK, true);
970 }
971
John Reck35e9dd62011-04-25 09:01:54 -0700972 public boolean acceptCookies() {
973 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
974 }
975
976 public boolean saveFormdata() {
977 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
978 }
979
980 public boolean enableGeolocation() {
981 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
982 }
983
984 public boolean rememberPasswords() {
985 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
986 }
987
Michael Kolb14612442011-06-24 13:06:29 -0700988 // -----------------------------
989 // getter/setters for bandwidth_preferences.xml
990 // -----------------------------
991
Mathew Inwood467813f2011-09-02 15:43:17 +0100992 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
993 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -0700994 }
Mathew Inwood467813f2011-09-02 15:43:17 +0100995
996 public static String getPreloadAlwaysPreferenceString(Context context) {
997 return context.getResources().getString(R.string.pref_data_preload_value_always);
998 }
999
Mathew Inwood825fba72011-10-04 14:52:52 +01001000 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1001 "browser_default_preload_setting";
1002
1003 public String getDefaultPreloadSetting() {
1004 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1005 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1006 if (preload == null) {
1007 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1008 }
1009 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001010 }
1011
1012 public String getPreloadEnabled() {
1013 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1014 }
1015
Victoria Lease96497832012-03-23 14:19:56 -07001016 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1017 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1018 }
1019
1020 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1021 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1022 }
1023
1024 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1025 "browser_default_link_prefetch_setting";
1026
1027 public String getDefaultLinkPrefetchSetting() {
1028 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1029 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1030 if (preload == null) {
1031 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1032 }
1033 return preload;
1034 }
1035
1036 public String getLinkPrefetchEnabled() {
1037 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1038 }
1039
George Mount3636d0a2011-11-21 09:08:21 -08001040 // -----------------------------
1041 // getter/setters for browser recovery
1042 // -----------------------------
1043 /**
1044 * The last time browser was started.
1045 * @return The last browser start time as System.currentTimeMillis. This
1046 * can be 0 if this is the first time or the last tab was closed.
1047 */
1048 public long getLastRecovered() {
1049 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1050 }
1051
1052 /**
1053 * Sets the last browser start time.
1054 * @param time The last time as System.currentTimeMillis that the browser
1055 * was started. This should be set to 0 if the last tab is closed.
1056 */
1057 public void setLastRecovered(long time) {
1058 mPrefs.edit()
1059 .putLong(KEY_LAST_RECOVERED, time)
1060 .apply();
1061 }
1062
1063 /**
1064 * Used to determine whether or not the previous browser run crashed. Once
1065 * the previous state has been determined, the value will be set to false
1066 * until a pause is received.
1067 * @return true if the last browser run was paused or false if it crashed.
1068 */
1069 public boolean wasLastRunPaused() {
1070 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1071 }
1072
1073 /**
1074 * Sets whether or not the last run was a pause or crash.
1075 * @param isPaused Set to true When a pause is received or false after
1076 * resuming.
1077 */
1078 public void setLastRunPaused(boolean isPaused) {
1079 mPrefs.edit()
1080 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1081 .apply();
1082 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001083}