blob: 77ee04d1cb4532708945a23cde77f263e764c148 [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());
274 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700275 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700276 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100277 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700278 settings.setLoadsImagesAutomatically(loadImages());
279 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
280 settings.setSavePassword(rememberPasswords());
281 settings.setSaveFormData(saveFormdata());
282 settings.setUseWideViewPort(isWideViewport());
Panos Thomasb10bbda2014-06-23 10:18:36 -0700283 settings.setDoNotTrack(doNotTrack());
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700284 setUserAgent(settings);
John Reck2fd9d0e2011-07-15 11:13:48 -0700285
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800286 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800287 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
288 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
289 settingsClassic.setForceUserScalable(forceEnableUserScalable());
290 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
291 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800292
John Reckea17e782011-10-27 17:15:59 -0700293 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800294 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700295 useInverted ? "true" : "false");
296 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800297 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700298 Float.toString(getInvertedContrast()));
299 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700300
John Reckea17e782011-10-27 17:15:59 -0700301 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800302 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700303 enableCpuUploadPath() ? "true" : "false");
304 }
Victoria Lease96497832012-03-23 14:19:56 -0700305
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800306 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000307 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800308
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700309 private void setUserAgent(WebSettings settings){
310 String ua = mContext.getResources().getString(R.string.def_useragent);
311
312 if (!TextUtils.isEmpty(ua))
313 ua = constructUserAgent(ua);
314
315 if (TextUtils.isEmpty(ua))
316 ua = mCustomUserAgents.get(settings);
317
318 if (!TextUtils.isEmpty(ua)){
319 settings.setUserAgentString(ua);
320 } else {
321 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
322 }
323 }
324
325 private String constructUserAgent(String userAgent) {
326 try {
327 userAgent = userAgent.replaceAll("<%build_model>", Build.MODEL);
328 userAgent = userAgent.replaceAll("<%build_version>", Build.VERSION.RELEASE);
329 userAgent = userAgent.replaceAll("<%build_id>", Build.ID);
330 userAgent = userAgent.replaceAll("<%language>", Locale.getDefault().getLanguage());
331 userAgent = userAgent.replaceAll("<%country>", Locale.getDefault().getCountry());
332 return userAgent;
333 } catch (Exception ex) {
334 return null;
335 }
336 }
337
John Reck35e9dd62011-04-25 09:01:54 -0700338 /**
339 * Syncs all the settings that have no UI
340 * These cannot change, so we only need to set them once per WebSettings
341 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800342 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700343 settings.setDefaultFontSize(16);
344 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000345
John Reck35e9dd62011-04-25 09:01:54 -0700346 // WebView inside Browser doesn't want initial focus to be set.
347 settings.setNeedInitialFocus(false);
348 // Browser supports multiple windows
349 settings.setSupportMultipleWindows(true);
350 // enable smooth transition for better performance during panning or
351 // zooming
352 settings.setEnableSmoothTransition(true);
353 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700354 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700355
356 // HTML5 API flags
357 settings.setAppCacheEnabled(true);
358 settings.setDatabaseEnabled(true);
359 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700360
361 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700362 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700363 settings.setAppCachePath(getAppCachePath());
364 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
365 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700366 // origin policy for file access
367 settings.setAllowUniversalAccessFromFileURLs(false);
368 settings.setAllowFileAccessFromFileURLs(false);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800369
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800370 //if (!(settings instanceof WebSettingsClassic)) return;
371 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800372
373 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
374 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
375 // WebView should be preserving the memory as much as possible.
376 // However, apps like browser wish to turn on the performance mode which
377 // would require more memory.
378 // TODO: We need to dynamically allocate/deallocate temporary memory for
379 // apps which are trying to use minimal memory. Currently, double
380 // buffering is always turned on, which is unnecessary.
381 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
382 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800383 */
John Reck35e9dd62011-04-25 09:01:54 -0700384 }
385
386 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700387 mNeedsSharedSync = false;
Axesh R. Ajmera579c70c2014-04-17 13:24:56 -0700388 CookieManager.getInstance().setAcceptCookie(acceptCookies());
389
John Reck35e9dd62011-04-25 09:01:54 -0700390 if (mController != null) {
391 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700392 }
John Reck35e9dd62011-04-25 09:01:54 -0700393 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700394
John Reck35e9dd62011-04-25 09:01:54 -0700395 private void syncManagedSettings() {
396 syncSharedSettings();
397 synchronized (mManagedSettings) {
398 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
399 while (iter.hasNext()) {
400 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800401 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700402 if (settings == null) {
403 iter.remove();
404 continue;
405 }
406 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000407 }
John Reck35e9dd62011-04-25 09:01:54 -0700408 }
409 }
Ben Murdochef671652010-11-25 17:17:58 +0000410
John Reck35e9dd62011-04-25 09:01:54 -0700411 @Override
412 public void onSharedPreferenceChanged(
413 SharedPreferences sharedPreferences, String key) {
414 syncManagedSettings();
415 if (PREF_SEARCH_ENGINE.equals(key)) {
416 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700417 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200418 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700419 mController.getUi().setFullscreen(useFullscreen());
420 }
Michael Kolb0241e752011-07-07 14:58:50 -0700421 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200422 if (mController != null && mController.getUi() != null) {
Michael Kolb0241e752011-07-07 14:58:50 -0700423 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
424 }
Victoria Lease96497832012-03-23 14:19:56 -0700425 } else if (PREF_LINK_PREFETCH.equals(key)) {
426 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700427 }
John Reck35e9dd62011-04-25 09:01:54 -0700428 }
429
John Reck961d35d2011-06-23 09:45:54 -0700430 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100431 requireInitialization();
432 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700433 }
434
435 public LayoutAlgorithm getLayoutAlgorithm() {
436 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
437 if (autofitPages()) {
438 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
439 }
440 if (isDebugEnabled()) {
441 if (isSmallScreen()) {
442 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000443 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700444 if (isNormalLayout()) {
445 layoutAlgorithm = LayoutAlgorithm.NORMAL;
446 } else {
447 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
448 }
Ben Murdochef671652010-11-25 17:17:58 +0000449 }
John Reck35e9dd62011-04-25 09:01:54 -0700450 }
451 return layoutAlgorithm;
452 }
Ben Murdochef671652010-11-25 17:17:58 +0000453
John Reck35e9dd62011-04-25 09:01:54 -0700454 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700455 requireInitialization();
456 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800457 }
458
John Reck35e9dd62011-04-25 09:01:54 -0700459 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700460 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700461 return mWebStorageSizeManager;
462 }
463
John Reck35e9dd62011-04-25 09:01:54 -0700464 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700465 if (mAppCachePath == null) {
466 mAppCachePath = mContext.getDir("appcache", 0).getPath();
467 }
468 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700469 }
470
471 private void updateSearchEngine(boolean force) {
472 String searchEngineName = getSearchEngineName();
473 if (force || mSearchEngine == null ||
474 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700475 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000476 }
477 }
478
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100479 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700480 if (mSearchEngine == null) {
481 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100482 }
John Reck35e9dd62011-04-25 09:01:54 -0700483 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100484 }
485
John Reck35e9dd62011-04-25 09:01:54 -0700486 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700487 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700488 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100489 }
490
John Reck35e9dd62011-04-25 09:01:54 -0700491 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700492 Editor edit = mPrefs.edit();
493 edit.putBoolean(PREF_DEBUG_MENU, value);
494 if (!value) {
495 // Reset to "safe" value
496 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
497 }
498 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100499 }
500
John Reck35e9dd62011-04-25 09:01:54 -0700501 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800502 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700503 if (mController != null) {
504 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800505 if (current != null) {
506 current.clearCache(true);
507 }
508 }
509 }
510
John Reck35e9dd62011-04-25 09:01:54 -0700511 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800512 CookieManager.getInstance().removeAllCookie();
513 }
514
John Reck35e9dd62011-04-25 09:01:54 -0700515 public void clearHistory() {
516 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800517 Browser.clearHistory(resolver);
518 Browser.clearSearches(resolver);
519 }
520
John Reck35e9dd62011-04-25 09:01:54 -0700521 public void clearFormData() {
522 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700523 if (mController!= null) {
524 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100525 if (currentTopView != null) {
526 currentTopView.clearFormData();
527 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800528 }
529 }
530
Bijan Amirzada3bbb3a42014-06-17 12:06:18 -0700531 public WebView getTopWebView(){
532 if (mController!= null)
533 return mController.getCurrentTopWebView();
534
535 return null;
536 }
537
John Reck35e9dd62011-04-25 09:01:54 -0700538 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700539 // Clear password store maintained by SWE engine
540 WebSettings settings = null;
541 // find a valid settings object
542 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
543 while (iter.hasNext()) {
544 WeakReference<WebSettings> ref = iter.next();
545 settings = (WebSettings)ref.get();
546 if (settings != null) {
547 break;
548 }
549 }
550 if (settings != null) {
Panos Thomasdbaea4e2014-05-22 06:48:47 -0700551 settings.clearPasswords();
Panos Thomasfa948b82014-03-09 03:42:42 -0700552 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800553 }
554
John Reck35e9dd62011-04-25 09:01:54 -0700555 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100556 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100557 }
558
John Reck35e9dd62011-04-25 09:01:54 -0700559 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100560 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100561 }
562
John Reck35e9dd62011-04-25 09:01:54 -0700563 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700564 mPrefs.edit()
565 .clear()
John Reckbd315192011-07-29 10:05:47 -0700566 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200567 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700568 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700569 }
570
Björn Isakssonc885a242012-06-05 17:19:04 +0200571 private void resetCachedValues() {
572 updateSearchEngine(false);
573 }
574
John Reck35e9dd62011-04-25 09:01:54 -0700575 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800576 // query the profile from components autofill database 524
577 if (mAutofillHandler.mAutoFillProfile == null &&
578 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
579 WebSettings settings = null;
580 // find a valid settings object
581 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
582 while (iter.hasNext()) {
583 WeakReference<WebSettings> ref = iter.next();
584 settings = (WebSettings)ref.get();
585 if (settings != null) {
586 break;
587 }
588 }
589 if (settings != null) {
590 AutoFillProfile profile =
591 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
592 mAutofillHandler.setAutoFillProfile(profile);
593 }
594 }
John Reck35e9dd62011-04-25 09:01:54 -0700595 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800596 }
597
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800598 public String getAutoFillProfileId() {
599 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800600 }
601
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800602 public void updateAutoFillProfile(AutoFillProfile profile) {
603 syncAutoFillProfile(profile);
604 }
605
606 private void syncAutoFillProfile(AutoFillProfile profile) {
607 synchronized (mManagedSettings) {
608 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
609 while (iter.hasNext()) {
610 WeakReference<WebSettings> ref = iter.next();
611 WebSettings settings = (WebSettings)ref.get();
612 if (settings == null) {
613 iter.remove();
614 continue;
615 }
616 // update the profile only once.
617 settings.setAutoFillProfile(profile);
618 // Now we should have the guid
619 mAutofillHandler.setAutoFillProfile(profile);
620 break;
621 }
622 }
623 }
John Reck35e9dd62011-04-25 09:01:54 -0700624 public void toggleDebugSettings() {
625 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800626 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100627
John Reckb8b2af82011-05-20 15:58:33 -0700628 public boolean hasDesktopUseragent(WebView view) {
629 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
630 }
631
632 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700633 if (view == null) {
634 return;
635 }
John Reckb8b2af82011-05-20 15:58:33 -0700636 WebSettings settings = view.getSettings();
637 if (mCustomUserAgents.get(settings) != null) {
638 mCustomUserAgents.remove(settings);
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700639 setUserAgent(settings);
John Reckb8b2af82011-05-20 15:58:33 -0700640 } else {
641 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
642 settings.setUserAgentString(DESKTOP_USERAGENT);
643 }
644 }
645
John Reck7dc444b2011-06-16 17:44:29 -0700646 public static int getAdjustedMinimumFontSize(int rawValue) {
647 rawValue++; // Preference starts at 0, min font at 1
648 if (rawValue > 1) {
649 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
650 }
651 return rawValue;
652 }
653
John Reck5ba3c762011-09-14 15:00:15 -0700654 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700655 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700656 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700657 }
658
659 static int getRawTextZoom(int percent) {
660 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
661 }
662
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700663 public int getAdjustedDoubleTapZoom(int rawValue) {
664 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
665 return (int) ((rawValue + 100) * mFontSizeMult);
666 }
667
668 static int getRawDoubleTapZoom(int percent) {
669 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
670 }
671
John Reckcadae722011-07-25 11:36:17 -0700672 public SharedPreferences getPreferences() {
673 return mPrefs;
674 }
675
Victoria Lease96497832012-03-23 14:19:56 -0700676 // update connectivity-dependent options
677 public void updateConnectionType() {
678 ConnectivityManager cm = (ConnectivityManager)
679 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
680 String linkPrefetchPreference = getLinkPrefetchEnabled();
681 boolean linkPrefetchAllowed = linkPrefetchPreference.
682 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
683 NetworkInfo ni = cm.getActiveNetworkInfo();
684 if (ni != null) {
685 switch (ni.getType()) {
686 case ConnectivityManager.TYPE_WIFI:
687 case ConnectivityManager.TYPE_ETHERNET:
688 case ConnectivityManager.TYPE_BLUETOOTH:
689 linkPrefetchAllowed |= linkPrefetchPreference.
690 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
691 break;
692 case ConnectivityManager.TYPE_MOBILE:
693 case ConnectivityManager.TYPE_MOBILE_DUN:
694 case ConnectivityManager.TYPE_MOBILE_MMS:
695 case ConnectivityManager.TYPE_MOBILE_SUPL:
696 case ConnectivityManager.TYPE_WIMAX:
697 default:
698 break;
699 }
700 }
701 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
702 mLinkPrefetchAllowed = linkPrefetchAllowed;
703 syncManagedSettings();
704 }
705 }
706
luxiaol62677b02013-07-22 07:54:49 +0800707 public String getDownloadPath() {
708 return mPrefs.getString(PREF_DOWNLOAD_PATH,
709 DownloadHandler.getDefaultDownloadPath(mContext));
710 }
John Reck35e9dd62011-04-25 09:01:54 -0700711 // -----------------------------
712 // getter/setters for accessibility_preferences.xml
713 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100714
John Reck7dc444b2011-06-16 17:44:29 -0700715 @Deprecated
716 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700717 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
718 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100719 }
720
John Reck35e9dd62011-04-25 09:01:54 -0700721 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700722 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700723 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100724 }
725
John Reck92f25f82011-04-26 16:57:10 -0700726 public boolean forceEnableUserScalable() {
727 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
728 }
729
John Reck7dc444b2011-06-16 17:44:29 -0700730 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700731 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700732 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
733 return getAdjustedTextZoom(textZoom);
734 }
735
736 public void setTextZoom(int percent) {
737 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
738 }
739
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700740 public int getDoubleTapZoom() {
741 requireInitialization();
742 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
743 return getAdjustedDoubleTapZoom(doubleTapZoom);
744 }
745
746 public void setDoubleTapZoom(int percent) {
747 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
748 }
749
John Reck35e9dd62011-04-25 09:01:54 -0700750 // -----------------------------
751 // getter/setters for advanced_preferences.xml
752 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100753
John Reck35e9dd62011-04-25 09:01:54 -0700754 public String getSearchEngineName() {
luxiaol221b3932013-07-19 15:27:57 +0800755 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
756 if (defaultSearchEngineValue == null) {
757 defaultSearchEngineValue = SearchEngine.GOOGLE;
758 }
759 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100760 }
John Reck63bb6872010-12-01 19:29:32 -0800761
Michael Kolb8d772b02012-01-19 13:56:00 -0800762 public boolean allowAppTabs() {
763 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
764 }
765
John Reck35e9dd62011-04-25 09:01:54 -0700766 public boolean openInBackground() {
767 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800768 }
John Reck35e9dd62011-04-25 09:01:54 -0700769
770 public boolean enableJavascript() {
771 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
772 }
773
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800774 public boolean enableMemoryMonitor() {
775 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
776 }
777
John Reck35e9dd62011-04-25 09:01:54 -0700778 // TODO: Cache
779 public PluginState getPluginState() {
780 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
781 return PluginState.valueOf(state);
782 }
783
784 // TODO: Cache
785 public ZoomDensity getDefaultZoom() {
786 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700787 ZoomDensity zoomDensity = ZoomDensity.valueOf(zoom);
788 //SWE: If zoomDensity is not MEDIUM, set enableWideViewport to false.
789 if(zoomDensity != ZoomDensity.MEDIUM)
790 enableWideViewport = false;
791 else
792 enableWideViewport = true;
793 return zoomDensity;
John Reck35e9dd62011-04-25 09:01:54 -0700794 }
795
796 public boolean loadPageInOverviewMode() {
797 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
798 }
799
800 public boolean autofitPages() {
801 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
802 }
803
804 public boolean blockPopupWindows() {
805 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
806 }
807
808 public boolean loadImages() {
809 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
810 }
811
812 public String getDefaultTextEncoding() {
813 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
814 }
815
816 // -----------------------------
817 // getter/setters for general_preferences.xml
818 // -----------------------------
819
820 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700821 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
822 }
823
824 public void setHomePage(String value) {
825 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
826 }
827
828 public boolean isAutofillEnabled() {
829 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
830 }
831
832 public void setAutofillEnabled(boolean value) {
833 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
834 }
835
836 // -----------------------------
837 // getter/setters for debug_preferences.xml
838 // -----------------------------
839
840 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700841 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700842 return true;
843 }
844 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
845 }
846
Derek Sollenberger31adf672011-07-08 11:31:30 -0400847 public boolean isSkiaHardwareAccelerated() {
848 if (!isDebugEnabled()) {
849 return false;
850 }
851 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
852 }
853
John Reck35e9dd62011-04-25 09:01:54 -0700854 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700855 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700856 return 0;
857 }
858 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
859 }
860
861 // -----------------------------
862 // getter/setters for hidden_debug_preferences.xml
863 // -----------------------------
864
865 public boolean enableVisualIndicator() {
866 if (!isDebugEnabled()) {
867 return false;
868 }
869 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
870 }
871
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700872 public boolean enableCpuUploadPath() {
873 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700874 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700875 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700876 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700877 }
878
John Reck35e9dd62011-04-25 09:01:54 -0700879 public boolean enableJavascriptConsole() {
880 if (!isDebugEnabled()) {
881 return false;
882 }
883 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
884 }
885
886 public boolean isSmallScreen() {
887 if (!isDebugEnabled()) {
888 return false;
889 }
890 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
891 }
892
893 public boolean isWideViewport() {
894 if (!isDebugEnabled()) {
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700895 return enableWideViewport;
John Reck35e9dd62011-04-25 09:01:54 -0700896 }
Tarun Nainanic8e6dda2014-04-17 13:11:25 -0700897 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, enableWideViewport);
John Reck35e9dd62011-04-25 09:01:54 -0700898 }
899
900 public boolean isNormalLayout() {
901 if (!isDebugEnabled()) {
902 return false;
903 }
904 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
905 }
906
907 public boolean isTracing() {
908 if (!isDebugEnabled()) {
909 return false;
910 }
911 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
912 }
913
914 public boolean enableLightTouch() {
915 if (!isDebugEnabled()) {
916 return false;
917 }
918 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
919 }
920
921 public boolean enableNavDump() {
922 if (!isDebugEnabled()) {
923 return false;
924 }
925 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
926 }
927
928 public String getJsEngineFlags() {
929 if (!isDebugEnabled()) {
930 return "";
931 }
932 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
933 }
934
935 // -----------------------------
936 // getter/setters for lab_preferences.xml
937 // -----------------------------
938
939 public boolean useQuickControls() {
940 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
941 }
942
943 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700944 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700945 }
946
Michael Kolbc38c6042011-04-27 10:46:06 -0700947 public boolean useFullscreen() {
948 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
949 }
950
John Reck2fd9d0e2011-07-15 11:13:48 -0700951 public boolean useInvertedRendering() {
952 return mPrefs.getBoolean(PREF_INVERTED, false);
953 }
954
Nicolas Roard5d513102011-08-03 15:35:34 -0700955 public float getInvertedContrast() {
956 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
957 }
958
John Reck35e9dd62011-04-25 09:01:54 -0700959 // -----------------------------
960 // getter/setters for privacy_security_preferences.xml
961 // -----------------------------
962
963 public boolean showSecurityWarnings() {
964 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
965 }
966
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700967 public boolean doNotTrack() {
968 return mPrefs.getBoolean(PREF_DO_NOT_TRACK, true);
969 }
970
John Reck35e9dd62011-04-25 09:01:54 -0700971 public boolean acceptCookies() {
972 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
973 }
974
975 public boolean saveFormdata() {
976 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
977 }
978
979 public boolean enableGeolocation() {
980 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
981 }
982
983 public boolean rememberPasswords() {
984 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
985 }
986
Michael Kolb14612442011-06-24 13:06:29 -0700987 // -----------------------------
988 // getter/setters for bandwidth_preferences.xml
989 // -----------------------------
990
Mathew Inwood467813f2011-09-02 15:43:17 +0100991 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
992 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -0700993 }
Mathew Inwood467813f2011-09-02 15:43:17 +0100994
995 public static String getPreloadAlwaysPreferenceString(Context context) {
996 return context.getResources().getString(R.string.pref_data_preload_value_always);
997 }
998
Mathew Inwood825fba72011-10-04 14:52:52 +0100999 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1000 "browser_default_preload_setting";
1001
1002 public String getDefaultPreloadSetting() {
1003 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1004 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1005 if (preload == null) {
1006 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1007 }
1008 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001009 }
1010
1011 public String getPreloadEnabled() {
1012 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1013 }
1014
Victoria Lease96497832012-03-23 14:19:56 -07001015 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1016 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1017 }
1018
1019 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1020 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1021 }
1022
1023 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1024 "browser_default_link_prefetch_setting";
1025
1026 public String getDefaultLinkPrefetchSetting() {
1027 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1028 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1029 if (preload == null) {
1030 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1031 }
1032 return preload;
1033 }
1034
1035 public String getLinkPrefetchEnabled() {
1036 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1037 }
1038
George Mount3636d0a2011-11-21 09:08:21 -08001039 // -----------------------------
1040 // getter/setters for browser recovery
1041 // -----------------------------
1042 /**
1043 * The last time browser was started.
1044 * @return The last browser start time as System.currentTimeMillis. This
1045 * can be 0 if this is the first time or the last tab was closed.
1046 */
1047 public long getLastRecovered() {
1048 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1049 }
1050
1051 /**
1052 * Sets the last browser start time.
1053 * @param time The last time as System.currentTimeMillis that the browser
1054 * was started. This should be set to 0 if the last tab is closed.
1055 */
1056 public void setLastRecovered(long time) {
1057 mPrefs.edit()
1058 .putLong(KEY_LAST_RECOVERED, time)
1059 .apply();
1060 }
1061
1062 /**
1063 * Used to determine whether or not the previous browser run crashed. Once
1064 * the previous state has been determined, the value will be set to false
1065 * until a pause is received.
1066 * @return true if the last browser run was paused or false if it crashed.
1067 */
1068 public boolean wasLastRunPaused() {
1069 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1070 }
1071
1072 /**
1073 * Sets whether or not the last run was a pause or crash.
1074 * @param isPaused Set to true When a pause is received or false after
1075 * resuming.
1076 */
1077 public void setLastRunPaused(boolean isPaused) {
1078 mPrefs.edit()
1079 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1080 .apply();
1081 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001082}