blob: 12013ab5cde027740de39330678d38cc43dc7bc5 [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;
John Reck812d2d62011-01-18 14:16:15 -080033import android.webkit.WebViewDatabase;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034
Bijan Amirzada41242f22014-03-21 12:12:18 -070035import com.android.browser.R;
36import com.android.browser.homepages.HomeProvider;
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070037import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070038import com.android.browser.provider.BrowserProvider;
Bijan Amirzada41242f22014-03-21 12:12:18 -070039import com.android.browser.search.SearchEngine;
40import com.android.browser.search.SearchEngines;
John Reck46500332011-06-07 14:36:10 -070041
John Reck35e9dd62011-04-25 09:01:54 -070042import java.lang.ref.WeakReference;
43import java.util.Iterator;
44import java.util.LinkedList;
qqzhou8c5b0a32013-07-22 15:31:03 +080045import java.util.Locale;
John Reckb8b2af82011-05-20 15:58:33 -070046import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080048import org.codeaurora.swe.AutoFillProfile;
49import org.codeaurora.swe.CookieManager;
50import org.codeaurora.swe.GeolocationPermissions;
51import org.codeaurora.swe.WebSettings.LayoutAlgorithm;
52import org.codeaurora.swe.WebSettings.PluginState;
53import org.codeaurora.swe.WebSettings.TextSize;
54import org.codeaurora.swe.WebSettings.ZoomDensity;
55import org.codeaurora.swe.WebSettings;
56import org.codeaurora.swe.WebView;
57
John Reck35e9dd62011-04-25 09:01:54 -070058/**
59 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080060 */
John Reck35e9dd62011-04-25 09:01:54 -070061public class BrowserSettings implements OnSharedPreferenceChangeListener,
62 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010063
John Reck35e9dd62011-04-25 09:01:54 -070064 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070065 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
66 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
67 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080068
69 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070070 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
71 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070072
73 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070074 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
75 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070076
77 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070078 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
79 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080080
John Reck7fd1e8f2011-04-27 18:22:57 -070081 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
82 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
83 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
84
John Reck35e9dd62011-04-25 09:01:54 -070085 private static final String USER_AGENTS[] = { null,
86 DESKTOP_USERAGENT,
87 IPHONE_USERAGENT,
88 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070089 FROYO_USERAGENT,
90 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070091 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080092
qqzhoue6ff8b42013-07-23 17:28:48 +080093 private static final String TAG = "BrowserSettings";
John Reck8fc22a12011-06-16 14:57:41 -070094 // The minimum min font size
95 // Aka, the lower bounds for the min font size range
96 // which is 1:5..24
97 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -070098 // The initial value in the text zoom range
99 // This is what represents 100% in the SeekBarPreference range
100 private static final int TEXT_ZOOM_START_VAL = 10;
101 // The size of a single step in the text zoom range, in percent
102 private static final int TEXT_ZOOM_STEP = 5;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700103 // The initial value in the double tap zoom range
104 // This is what represents 100% in the SeekBarPreference range
105 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
106 // The size of a single step in the double tap zoom range, in percent
107 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -0700108
John Reck35e9dd62011-04-25 09:01:54 -0700109 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -0700110
John Reck35e9dd62011-04-25 09:01:54 -0700111 private Context mContext;
112 private SharedPreferences mPrefs;
113 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700114 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700115 private WebStorageSizeManager mWebStorageSizeManager;
116 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700117 private WeakHashMap<WebSettings, String> mCustomUserAgents;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100118 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -0700119 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -0700120 private float mFontSizeMult = 1.0f;
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700121 private boolean enableWideViewport = true;
John Reck78a6a1d2011-07-21 13:54:03 -0700122
Victoria Lease96497832012-03-23 14:19:56 -0700123 // Current state of network-dependent settings
124 private boolean mLinkPrefetchAllowed = true;
125
John Reck78a6a1d2011-07-21 13:54:03 -0700126 // Cached values
127 private int mPageCacheCapacity = 1;
128 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800129
John Reck35e9dd62011-04-25 09:01:54 -0700130 // Cached settings
131 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800132
Ben Murdochaaa1f372011-07-25 15:40:58 +0100133 private static String sFactoryResetUrl;
134
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700135 public static void initialize(final Context context) {
John Reck35e9dd62011-04-25 09:01:54 -0700136 sInstance = new BrowserSettings(context);
137 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800138
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700139 public static BrowserSettings getInstance() {
John Reck35e9dd62011-04-25 09:01:54 -0700140 return sInstance;
141 }
Ben Murdochef671652010-11-25 17:17:58 +0000142
John Reck35e9dd62011-04-25 09:01:54 -0700143 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100144 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700145 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
146 mAutofillHandler = new AutofillHandler(mContext);
147 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700148 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reckcadae722011-07-25 11:36:17 -0700149 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700150 }
151
152 public void setController(Controller controller) {
153 mController = controller;
John Reckc477e712011-08-17 11:27:15 -0700154 if (sInitialized) {
155 syncSharedSettings();
156 }
Ben Murdochef671652010-11-25 17:17:58 +0000157 }
158
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800159 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800160
John Reckc477e712011-08-17 11:27:15 -0700161 if (mNeedsSharedSync) {
162 syncSharedSettings();
163 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800164
John Reck35e9dd62011-04-25 09:01:54 -0700165 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800166 syncStaticSettings(settings);
167 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700168 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800169 }
170 }
171
John Reckd1d87312012-03-08 13:25:00 -0800172 public void stopManagingSettings(WebSettings settings) {
173 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
174 while (iter.hasNext()) {
175 WeakReference<WebSettings> ref = iter.next();
176 if (ref.get() == settings) {
177 iter.remove();
178 return;
179 }
180 }
181 }
182
John Reckcadae722011-07-25 11:36:17 -0700183 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700184
185 @Override
186 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700187 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
188 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700189 // the cost of one cached page is ~3M (measured using nytimes.com). For
190 // low end devices, we only cache one page. For high end devices, we try
191 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800192
193 // SWE_TODO : assume a high-memory device
194 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700195 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800196 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700197 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
198 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
199 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700200 // Workaround b/5254577
201 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700202 if (Build.VERSION.CODENAME.equals("REL")) {
203 // This is a release build, always startup with debug disabled
204 setDebugEnabled(false);
205 }
206 if (mPrefs.contains(PREF_TEXT_SIZE)) {
207 /*
208 * Update from TextSize enum to zoom percent
209 * SMALLEST is 50%
210 * SMALLER is 75%
211 * NORMAL is 100%
212 * LARGER is 150%
213 * LARGEST is 200%
214 */
215 switch (getTextSize()) {
216 case SMALLEST:
217 setTextZoom(50);
218 break;
219 case SMALLER:
220 setTextZoom(75);
221 break;
222 case LARGER:
223 setTextZoom(150);
224 break;
225 case LARGEST:
226 setTextZoom(200);
227 break;
228 }
229 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
230 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100231
qqzhou8c5b0a32013-07-22 15:31:03 +0800232 // add for carrier homepage feature
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700233 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
qqzhou8c5b0a32013-07-22 15:31:03 +0800234
kaiyizbf086ea2013-08-02 11:03:58 +0800235 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700236 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING, "UTF-8").apply();
kaiyizbf086ea2013-08-02 11:03:58 +0800237 }
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700238
Ben Murdochaaa1f372011-07-25 15:40:58 +0100239 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
240 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
241 BrowserProvider.getClientId(mContext.getContentResolver()));
242 }
243
Ben Murdochaaa1f372011-07-25 15:40:58 +0100244 synchronized (BrowserSettings.class) {
245 sInitialized = true;
246 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700247 }
248 }
249 };
250
Ben Murdochaaa1f372011-07-25 15:40:58 +0100251 private static void requireInitialization() {
252 synchronized (BrowserSettings.class) {
253 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700254 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100255 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700256 } catch (InterruptedException e) {
257 }
258 }
259 }
260 }
261
The Android Open Source Project0c908882009-03-03 19:32:16 -0800262 /**
John Reck35e9dd62011-04-25 09:01:54 -0700263 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800264 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800265 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700266 settings.setGeolocationEnabled(enableGeolocation());
267 settings.setJavaScriptEnabled(enableJavascript());
268 settings.setLightTouchEnabled(enableLightTouch());
269 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700270 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
271 settings.setDefaultZoom(getDefaultZoom());
272 settings.setMinimumFontSize(getMinimumFontSize());
273 settings.setMinimumLogicalFontSize(getMinimumFontSize());
John Reck7dc444b2011-06-16 17:44:29 -0700274 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700275 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100276 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700277 settings.setLoadsImagesAutomatically(loadImages());
278 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
279 settings.setSavePassword(rememberPasswords());
280 settings.setSaveFormData(saveFormdata());
281 settings.setUseWideViewPort(isWideViewport());
Panos Thomasb10bbda2014-06-23 10:18:36 -0700282 settings.setDoNotTrack(doNotTrack());
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700283 setUserAgent(settings);
John Reck2fd9d0e2011-07-15 11:13:48 -0700284
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800285 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800286 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
287 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
288 settingsClassic.setForceUserScalable(forceEnableUserScalable());
289 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
290 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800291
John Reckea17e782011-10-27 17:15:59 -0700292 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800293 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700294 useInverted ? "true" : "false");
295 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800296 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700297 Float.toString(getInvertedContrast()));
298 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700299
John Reckea17e782011-10-27 17:15:59 -0700300 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800301 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700302 enableCpuUploadPath() ? "true" : "false");
303 }
Victoria Lease96497832012-03-23 14:19:56 -0700304
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800305 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000306 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800307
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700308 private void setUserAgent(WebSettings settings){
309 String ua = mContext.getResources().getString(R.string.def_useragent);
310
311 if (!TextUtils.isEmpty(ua))
312 ua = constructUserAgent(ua);
313
314 if (TextUtils.isEmpty(ua))
315 ua = mCustomUserAgents.get(settings);
316
317 if (!TextUtils.isEmpty(ua)){
318 settings.setUserAgentString(ua);
319 } else {
320 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
321 }
322 }
323
324 private String constructUserAgent(String userAgent) {
325 try {
326 userAgent = userAgent.replaceAll("<%build_model>", Build.MODEL);
327 userAgent = userAgent.replaceAll("<%build_version>", Build.VERSION.RELEASE);
328 userAgent = userAgent.replaceAll("<%build_id>", Build.ID);
329 userAgent = userAgent.replaceAll("<%language>", Locale.getDefault().getLanguage());
330 userAgent = userAgent.replaceAll("<%country>", Locale.getDefault().getCountry());
331 return userAgent;
332 } catch (Exception ex) {
333 return null;
334 }
335 }
336
John Reck35e9dd62011-04-25 09:01:54 -0700337 /**
338 * Syncs all the settings that have no UI
339 * These cannot change, so we only need to set them once per WebSettings
340 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800341 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700342 settings.setDefaultFontSize(16);
343 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000344
John Reck35e9dd62011-04-25 09:01:54 -0700345 // WebView inside Browser doesn't want initial focus to be set.
346 settings.setNeedInitialFocus(false);
347 // Browser supports multiple windows
348 settings.setSupportMultipleWindows(true);
349 // enable smooth transition for better performance during panning or
350 // zooming
351 settings.setEnableSmoothTransition(true);
352 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700353 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700354
355 // HTML5 API flags
356 settings.setAppCacheEnabled(true);
357 settings.setDatabaseEnabled(true);
358 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700359
360 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700361 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700362 settings.setAppCachePath(getAppCachePath());
363 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
364 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700365 // origin policy for file access
366 settings.setAllowUniversalAccessFromFileURLs(false);
367 settings.setAllowFileAccessFromFileURLs(false);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800368
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800369 //if (!(settings instanceof WebSettingsClassic)) return;
370 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800371
372 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
373 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
374 // WebView should be preserving the memory as much as possible.
375 // However, apps like browser wish to turn on the performance mode which
376 // would require more memory.
377 // TODO: We need to dynamically allocate/deallocate temporary memory for
378 // apps which are trying to use minimal memory. Currently, double
379 // buffering is always turned on, which is unnecessary.
380 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
381 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800382 */
John Reck35e9dd62011-04-25 09:01:54 -0700383 }
384
385 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700386 mNeedsSharedSync = false;
Axesh R. Ajmera579c70c2014-04-17 13:24:56 -0700387 CookieManager.getInstance().setAcceptCookie(acceptCookies());
388
John Reck35e9dd62011-04-25 09:01:54 -0700389 if (mController != null) {
390 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700391 }
John Reck35e9dd62011-04-25 09:01:54 -0700392 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700393
John Reck35e9dd62011-04-25 09:01:54 -0700394 private void syncManagedSettings() {
395 syncSharedSettings();
396 synchronized (mManagedSettings) {
397 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
398 while (iter.hasNext()) {
399 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800400 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700401 if (settings == null) {
402 iter.remove();
403 continue;
404 }
405 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000406 }
John Reck35e9dd62011-04-25 09:01:54 -0700407 }
408 }
Ben Murdochef671652010-11-25 17:17:58 +0000409
John Reck35e9dd62011-04-25 09:01:54 -0700410 @Override
411 public void onSharedPreferenceChanged(
412 SharedPreferences sharedPreferences, String key) {
413 syncManagedSettings();
414 if (PREF_SEARCH_ENGINE.equals(key)) {
415 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700416 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200417 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700418 mController.getUi().setFullscreen(useFullscreen());
419 }
Michael Kolb0241e752011-07-07 14:58:50 -0700420 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200421 if (mController != null && mController.getUi() != null) {
Michael Kolb0241e752011-07-07 14:58:50 -0700422 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
423 }
Victoria Lease96497832012-03-23 14:19:56 -0700424 } else if (PREF_LINK_PREFETCH.equals(key)) {
425 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700426 }
John Reck35e9dd62011-04-25 09:01:54 -0700427 }
428
John Reck961d35d2011-06-23 09:45:54 -0700429 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100430 requireInitialization();
431 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700432 }
433
434 public LayoutAlgorithm getLayoutAlgorithm() {
435 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
436 if (autofitPages()) {
437 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
438 }
439 if (isDebugEnabled()) {
440 if (isSmallScreen()) {
441 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000442 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700443 if (isNormalLayout()) {
444 layoutAlgorithm = LayoutAlgorithm.NORMAL;
445 } else {
446 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
447 }
Ben Murdochef671652010-11-25 17:17:58 +0000448 }
John Reck35e9dd62011-04-25 09:01:54 -0700449 }
450 return layoutAlgorithm;
451 }
Ben Murdochef671652010-11-25 17:17:58 +0000452
John Reck35e9dd62011-04-25 09:01:54 -0700453 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700454 requireInitialization();
455 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800456 }
457
John Reck35e9dd62011-04-25 09:01:54 -0700458 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700459 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700460 return mWebStorageSizeManager;
461 }
462
John Reck35e9dd62011-04-25 09:01:54 -0700463 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700464 if (mAppCachePath == null) {
465 mAppCachePath = mContext.getDir("appcache", 0).getPath();
466 }
467 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700468 }
469
470 private void updateSearchEngine(boolean force) {
471 String searchEngineName = getSearchEngineName();
472 if (force || mSearchEngine == null ||
473 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700474 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000475 }
476 }
477
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100478 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700479 if (mSearchEngine == null) {
480 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100481 }
John Reck35e9dd62011-04-25 09:01:54 -0700482 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100483 }
484
John Reck35e9dd62011-04-25 09:01:54 -0700485 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700486 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700487 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100488 }
489
John Reck35e9dd62011-04-25 09:01:54 -0700490 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700491 Editor edit = mPrefs.edit();
492 edit.putBoolean(PREF_DEBUG_MENU, value);
493 if (!value) {
494 // Reset to "safe" value
495 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
496 }
497 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100498 }
499
John Reck35e9dd62011-04-25 09:01:54 -0700500 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800501 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700502 if (mController != null) {
503 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800504 if (current != null) {
505 current.clearCache(true);
506 }
507 }
508 }
509
John Reck35e9dd62011-04-25 09:01:54 -0700510 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800511 CookieManager.getInstance().removeAllCookie();
512 }
513
John Reck35e9dd62011-04-25 09:01:54 -0700514 public void clearHistory() {
515 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800516 Browser.clearHistory(resolver);
517 Browser.clearSearches(resolver);
518 }
519
John Reck35e9dd62011-04-25 09:01:54 -0700520 public void clearFormData() {
521 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700522 if (mController!= null) {
523 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100524 if (currentTopView != null) {
525 currentTopView.clearFormData();
526 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800527 }
528 }
529
Bijan Amirzada3bbb3a42014-06-17 12:06:18 -0700530 public WebView getTopWebView(){
531 if (mController!= null)
532 return mController.getCurrentTopWebView();
533
534 return null;
535 }
536
John Reck35e9dd62011-04-25 09:01:54 -0700537 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700538 // Clear password store maintained by SWE engine
539 WebSettings settings = null;
540 // find a valid settings object
541 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
542 while (iter.hasNext()) {
543 WeakReference<WebSettings> ref = iter.next();
544 settings = (WebSettings)ref.get();
545 if (settings != null) {
546 break;
547 }
548 }
549 if (settings != null) {
Panos Thomasdbaea4e2014-05-22 06:48:47 -0700550 settings.clearPasswords();
Panos Thomasfa948b82014-03-09 03:42:42 -0700551 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800552 }
553
John Reck35e9dd62011-04-25 09:01:54 -0700554 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100555 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100556 }
557
John Reck35e9dd62011-04-25 09:01:54 -0700558 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100559 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100560 }
561
John Reck35e9dd62011-04-25 09:01:54 -0700562 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700563 mPrefs.edit()
564 .clear()
John Reckbd315192011-07-29 10:05:47 -0700565 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200566 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700567 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700568 }
569
Björn Isakssonc885a242012-06-05 17:19:04 +0200570 private void resetCachedValues() {
571 updateSearchEngine(false);
572 }
573
John Reck35e9dd62011-04-25 09:01:54 -0700574 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800575 // query the profile from components autofill database 524
576 if (mAutofillHandler.mAutoFillProfile == null &&
577 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
578 WebSettings settings = null;
579 // find a valid settings object
580 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
581 while (iter.hasNext()) {
582 WeakReference<WebSettings> ref = iter.next();
583 settings = (WebSettings)ref.get();
584 if (settings != null) {
585 break;
586 }
587 }
588 if (settings != null) {
589 AutoFillProfile profile =
590 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
591 mAutofillHandler.setAutoFillProfile(profile);
592 }
593 }
John Reck35e9dd62011-04-25 09:01:54 -0700594 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800595 }
596
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800597 public String getAutoFillProfileId() {
598 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800599 }
600
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800601 public void updateAutoFillProfile(AutoFillProfile profile) {
602 syncAutoFillProfile(profile);
603 }
604
605 private void syncAutoFillProfile(AutoFillProfile profile) {
606 synchronized (mManagedSettings) {
607 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
608 while (iter.hasNext()) {
609 WeakReference<WebSettings> ref = iter.next();
610 WebSettings settings = (WebSettings)ref.get();
611 if (settings == null) {
612 iter.remove();
613 continue;
614 }
615 // update the profile only once.
616 settings.setAutoFillProfile(profile);
617 // Now we should have the guid
618 mAutofillHandler.setAutoFillProfile(profile);
619 break;
620 }
621 }
622 }
John Reck35e9dd62011-04-25 09:01:54 -0700623 public void toggleDebugSettings() {
624 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800625 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100626
John Reckb8b2af82011-05-20 15:58:33 -0700627 public boolean hasDesktopUseragent(WebView view) {
628 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
629 }
630
631 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700632 if (view == null) {
633 return;
634 }
John Reckb8b2af82011-05-20 15:58:33 -0700635 WebSettings settings = view.getSettings();
636 if (mCustomUserAgents.get(settings) != null) {
637 mCustomUserAgents.remove(settings);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700638 setUserAgent(settings);
John Reckb8b2af82011-05-20 15:58:33 -0700639 } else {
640 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
641 settings.setUserAgentString(DESKTOP_USERAGENT);
642 }
643 }
644
John Reck7dc444b2011-06-16 17:44:29 -0700645 public static int getAdjustedMinimumFontSize(int rawValue) {
646 rawValue++; // Preference starts at 0, min font at 1
647 if (rawValue > 1) {
648 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
649 }
650 return rawValue;
651 }
652
John Reck5ba3c762011-09-14 15:00:15 -0700653 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700654 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700655 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700656 }
657
658 static int getRawTextZoom(int percent) {
659 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
660 }
661
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700662 public int getAdjustedDoubleTapZoom(int rawValue) {
663 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
664 return (int) ((rawValue + 100) * mFontSizeMult);
665 }
666
667 static int getRawDoubleTapZoom(int percent) {
668 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
669 }
670
John Reckcadae722011-07-25 11:36:17 -0700671 public SharedPreferences getPreferences() {
672 return mPrefs;
673 }
674
Victoria Lease96497832012-03-23 14:19:56 -0700675 // update connectivity-dependent options
676 public void updateConnectionType() {
677 ConnectivityManager cm = (ConnectivityManager)
678 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
679 String linkPrefetchPreference = getLinkPrefetchEnabled();
680 boolean linkPrefetchAllowed = linkPrefetchPreference.
681 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
682 NetworkInfo ni = cm.getActiveNetworkInfo();
683 if (ni != null) {
684 switch (ni.getType()) {
685 case ConnectivityManager.TYPE_WIFI:
686 case ConnectivityManager.TYPE_ETHERNET:
687 case ConnectivityManager.TYPE_BLUETOOTH:
688 linkPrefetchAllowed |= linkPrefetchPreference.
689 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
690 break;
691 case ConnectivityManager.TYPE_MOBILE:
692 case ConnectivityManager.TYPE_MOBILE_DUN:
693 case ConnectivityManager.TYPE_MOBILE_MMS:
694 case ConnectivityManager.TYPE_MOBILE_SUPL:
695 case ConnectivityManager.TYPE_WIMAX:
696 default:
697 break;
698 }
699 }
700 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
701 mLinkPrefetchAllowed = linkPrefetchAllowed;
702 syncManagedSettings();
703 }
704 }
705
luxiaol62677b02013-07-22 07:54:49 +0800706 public String getDownloadPath() {
707 return mPrefs.getString(PREF_DOWNLOAD_PATH,
708 DownloadHandler.getDefaultDownloadPath(mContext));
709 }
John Reck35e9dd62011-04-25 09:01:54 -0700710 // -----------------------------
711 // getter/setters for accessibility_preferences.xml
712 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100713
John Reck7dc444b2011-06-16 17:44:29 -0700714 @Deprecated
715 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700716 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
717 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100718 }
719
John Reck35e9dd62011-04-25 09:01:54 -0700720 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700721 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700722 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100723 }
724
John Reck92f25f82011-04-26 16:57:10 -0700725 public boolean forceEnableUserScalable() {
726 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
727 }
728
John Reck7dc444b2011-06-16 17:44:29 -0700729 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700730 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700731 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
732 return getAdjustedTextZoom(textZoom);
733 }
734
735 public void setTextZoom(int percent) {
736 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
737 }
738
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700739 public int getDoubleTapZoom() {
740 requireInitialization();
741 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
742 return getAdjustedDoubleTapZoom(doubleTapZoom);
743 }
744
745 public void setDoubleTapZoom(int percent) {
746 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
747 }
748
John Reck35e9dd62011-04-25 09:01:54 -0700749 // -----------------------------
750 // getter/setters for advanced_preferences.xml
751 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100752
John Reck35e9dd62011-04-25 09:01:54 -0700753 public String getSearchEngineName() {
luxiaol221b3932013-07-19 15:27:57 +0800754 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
755 if (defaultSearchEngineValue == null) {
756 defaultSearchEngineValue = SearchEngine.GOOGLE;
757 }
758 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100759 }
John Reck63bb6872010-12-01 19:29:32 -0800760
Michael Kolb8d772b02012-01-19 13:56:00 -0800761 public boolean allowAppTabs() {
762 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
763 }
764
John Reck35e9dd62011-04-25 09:01:54 -0700765 public boolean openInBackground() {
766 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800767 }
John Reck35e9dd62011-04-25 09:01:54 -0700768
769 public boolean enableJavascript() {
770 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
771 }
772
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800773 public boolean enableMemoryMonitor() {
774 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
775 }
776
John Reck35e9dd62011-04-25 09:01:54 -0700777 // TODO: Cache
John Reck35e9dd62011-04-25 09:01:54 -0700778 public ZoomDensity getDefaultZoom() {
779 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700780 ZoomDensity zoomDensity = ZoomDensity.valueOf(zoom);
781 //SWE: If zoomDensity is not MEDIUM, set enableWideViewport to false.
782 if(zoomDensity != ZoomDensity.MEDIUM)
783 enableWideViewport = false;
784 else
785 enableWideViewport = true;
786 return zoomDensity;
John Reck35e9dd62011-04-25 09:01:54 -0700787 }
788
789 public boolean loadPageInOverviewMode() {
790 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
791 }
792
793 public boolean autofitPages() {
794 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
795 }
796
797 public boolean blockPopupWindows() {
798 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
799 }
800
801 public boolean loadImages() {
802 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
803 }
804
805 public String getDefaultTextEncoding() {
806 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
807 }
808
809 // -----------------------------
810 // getter/setters for general_preferences.xml
811 // -----------------------------
812
813 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700814 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
815 }
816
817 public void setHomePage(String value) {
818 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
819 }
820
821 public boolean isAutofillEnabled() {
822 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
823 }
824
825 public void setAutofillEnabled(boolean value) {
826 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
827 }
828
829 // -----------------------------
830 // getter/setters for debug_preferences.xml
831 // -----------------------------
832
833 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700834 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700835 return true;
836 }
837 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
838 }
839
Derek Sollenberger31adf672011-07-08 11:31:30 -0400840 public boolean isSkiaHardwareAccelerated() {
841 if (!isDebugEnabled()) {
842 return false;
843 }
844 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
845 }
846
John Reck35e9dd62011-04-25 09:01:54 -0700847 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700848 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700849 return 0;
850 }
851 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
852 }
853
854 // -----------------------------
855 // getter/setters for hidden_debug_preferences.xml
856 // -----------------------------
857
858 public boolean enableVisualIndicator() {
859 if (!isDebugEnabled()) {
860 return false;
861 }
862 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
863 }
864
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700865 public boolean enableCpuUploadPath() {
866 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700867 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700868 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700869 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700870 }
871
John Reck35e9dd62011-04-25 09:01:54 -0700872 public boolean enableJavascriptConsole() {
873 if (!isDebugEnabled()) {
874 return false;
875 }
876 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
877 }
878
879 public boolean isSmallScreen() {
880 if (!isDebugEnabled()) {
881 return false;
882 }
883 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
884 }
885
886 public boolean isWideViewport() {
887 if (!isDebugEnabled()) {
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700888 return enableWideViewport;
John Reck35e9dd62011-04-25 09:01:54 -0700889 }
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700890 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, enableWideViewport);
John Reck35e9dd62011-04-25 09:01:54 -0700891 }
892
893 public boolean isNormalLayout() {
894 if (!isDebugEnabled()) {
895 return false;
896 }
897 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
898 }
899
900 public boolean isTracing() {
901 if (!isDebugEnabled()) {
902 return false;
903 }
904 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
905 }
906
907 public boolean enableLightTouch() {
908 if (!isDebugEnabled()) {
909 return false;
910 }
911 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
912 }
913
914 public boolean enableNavDump() {
915 if (!isDebugEnabled()) {
916 return false;
917 }
918 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
919 }
920
921 public String getJsEngineFlags() {
922 if (!isDebugEnabled()) {
923 return "";
924 }
925 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
926 }
927
928 // -----------------------------
929 // getter/setters for lab_preferences.xml
930 // -----------------------------
931
932 public boolean useQuickControls() {
933 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
934 }
935
936 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700937 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700938 }
939
Michael Kolbc38c6042011-04-27 10:46:06 -0700940 public boolean useFullscreen() {
941 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
942 }
943
John Reck2fd9d0e2011-07-15 11:13:48 -0700944 public boolean useInvertedRendering() {
945 return mPrefs.getBoolean(PREF_INVERTED, false);
946 }
947
Nicolas Roard5d513102011-08-03 15:35:34 -0700948 public float getInvertedContrast() {
949 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
950 }
951
John Reck35e9dd62011-04-25 09:01:54 -0700952 // -----------------------------
953 // getter/setters for privacy_security_preferences.xml
954 // -----------------------------
955
956 public boolean showSecurityWarnings() {
957 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
958 }
959
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700960 public boolean doNotTrack() {
961 return mPrefs.getBoolean(PREF_DO_NOT_TRACK, true);
962 }
963
John Reck35e9dd62011-04-25 09:01:54 -0700964 public boolean acceptCookies() {
965 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
966 }
967
968 public boolean saveFormdata() {
969 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
970 }
971
972 public boolean enableGeolocation() {
973 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
974 }
975
976 public boolean rememberPasswords() {
977 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
978 }
979
Michael Kolb14612442011-06-24 13:06:29 -0700980 // -----------------------------
981 // getter/setters for bandwidth_preferences.xml
982 // -----------------------------
983
Mathew Inwood467813f2011-09-02 15:43:17 +0100984 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
985 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -0700986 }
Mathew Inwood467813f2011-09-02 15:43:17 +0100987
988 public static String getPreloadAlwaysPreferenceString(Context context) {
989 return context.getResources().getString(R.string.pref_data_preload_value_always);
990 }
991
Mathew Inwood825fba72011-10-04 14:52:52 +0100992 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
993 "browser_default_preload_setting";
994
995 public String getDefaultPreloadSetting() {
996 String preload = Settings.Secure.getString(mContext.getContentResolver(),
997 DEAULT_PRELOAD_SECURE_SETTING_KEY);
998 if (preload == null) {
999 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1000 }
1001 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001002 }
1003
1004 public String getPreloadEnabled() {
1005 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1006 }
1007
Victoria Lease96497832012-03-23 14:19:56 -07001008 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1009 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1010 }
1011
1012 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1013 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1014 }
1015
1016 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1017 "browser_default_link_prefetch_setting";
1018
1019 public String getDefaultLinkPrefetchSetting() {
1020 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1021 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1022 if (preload == null) {
1023 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1024 }
1025 return preload;
1026 }
1027
1028 public String getLinkPrefetchEnabled() {
1029 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1030 }
1031
George Mount3636d0a2011-11-21 09:08:21 -08001032 // -----------------------------
1033 // getter/setters for browser recovery
1034 // -----------------------------
1035 /**
1036 * The last time browser was started.
1037 * @return The last browser start time as System.currentTimeMillis. This
1038 * can be 0 if this is the first time or the last tab was closed.
1039 */
1040 public long getLastRecovered() {
1041 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1042 }
1043
1044 /**
1045 * Sets the last browser start time.
1046 * @param time The last time as System.currentTimeMillis that the browser
1047 * was started. This should be set to 0 if the last tab is closed.
1048 */
1049 public void setLastRecovered(long time) {
1050 mPrefs.edit()
1051 .putLong(KEY_LAST_RECOVERED, time)
1052 .apply();
1053 }
1054
1055 /**
1056 * Used to determine whether or not the previous browser run crashed. Once
1057 * the previous state has been determined, the value will be set to false
1058 * until a pause is received.
1059 * @return true if the last browser run was paused or false if it crashed.
1060 */
1061 public boolean wasLastRunPaused() {
1062 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1063 }
1064
1065 /**
1066 * Sets whether or not the last run was a pause or crash.
1067 * @param isPaused Set to true When a pause is received or false after
1068 * resuming.
1069 */
1070 public void setLastRunPaused(boolean isPaused) {
1071 mPrefs.edit()
1072 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1073 .apply();
1074 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001075}