blob: fc70a760861566b6228a9a2e6676f75871ffb1b4 [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;
qqzhou8c5b0a32013-07-22 15:31:03 +080024import android.content.res.AssetManager;
Victoria Lease96497832012-03-23 14:19:56 -070025import android.net.ConnectivityManager;
26import android.net.NetworkInfo;
John Reckf48314f2011-04-27 17:52:17 -070027import android.os.Build;
Ben Murdoch0cb81892010-10-08 12:41:33 +010028import android.preference.PreferenceManager;
Ben Murdoch0cb81892010-10-08 12:41:33 +010029import android.provider.Browser;
Mathew Inwood825fba72011-10-04 14:52:52 +010030import android.provider.Settings;
John Reck5ba3c762011-09-14 15:00:15 -070031import android.util.DisplayMetrics;
qqzhou8c5b0a32013-07-22 15:31:03 +080032import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.webkit.WebIconDatabase;
Nicolas Roard78a98e42009-05-11 13:34:17 +010034import android.webkit.WebStorage;
John Reck812d2d62011-01-18 14:16:15 -080035import android.webkit.WebViewDatabase;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036
Bijan Amirzada41242f22014-03-21 12:12:18 -070037import com.android.browser.R;
38import com.android.browser.homepages.HomeProvider;
39import com.android.browser.provider.BrowserProvider;
40import com.android.browser.reflect.ReflectHelper;
41import com.android.browser.search.SearchEngine;
42import com.android.browser.search.SearchEngines;
John Reck46500332011-06-07 14:36:10 -070043
qqzhou8c5b0a32013-07-22 15:31:03 +080044import java.io.InputStream;
John Reck35e9dd62011-04-25 09:01:54 -070045import java.lang.ref.WeakReference;
46import java.util.Iterator;
47import java.util.LinkedList;
qqzhou8c5b0a32013-07-22 15:31:03 +080048import java.util.Locale;
John Reckb8b2af82011-05-20 15:58:33 -070049import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080051import org.codeaurora.swe.AutoFillProfile;
52import org.codeaurora.swe.CookieManager;
53import org.codeaurora.swe.GeolocationPermissions;
54import org.codeaurora.swe.WebSettings.LayoutAlgorithm;
55import org.codeaurora.swe.WebSettings.PluginState;
56import org.codeaurora.swe.WebSettings.TextSize;
57import org.codeaurora.swe.WebSettings.ZoomDensity;
58import org.codeaurora.swe.WebSettings;
59import org.codeaurora.swe.WebView;
60
John Reck35e9dd62011-04-25 09:01:54 -070061/**
62 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080063 */
John Reck35e9dd62011-04-25 09:01:54 -070064public class BrowserSettings implements OnSharedPreferenceChangeListener,
65 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010066
John Reck35e9dd62011-04-25 09:01:54 -070067 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070068 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
69 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
70 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080071
72 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070073 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
74 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070075
76 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070077 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
78 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070079
80 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070081 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
82 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080083
John Reck7fd1e8f2011-04-27 18:22:57 -070084 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
85 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
86 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
87
John Reck35e9dd62011-04-25 09:01:54 -070088 private static final String USER_AGENTS[] = { null,
89 DESKTOP_USERAGENT,
90 IPHONE_USERAGENT,
91 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070092 FROYO_USERAGENT,
93 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070094 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080095
qqzhoue6ff8b42013-07-23 17:28:48 +080096 private static final String TAG = "BrowserSettings";
John Reck8fc22a12011-06-16 14:57:41 -070097 // The minimum min font size
98 // Aka, the lower bounds for the min font size range
99 // which is 1:5..24
100 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -0700101 // The initial value in the text zoom range
102 // This is what represents 100% in the SeekBarPreference range
103 private static final int TEXT_ZOOM_START_VAL = 10;
104 // The size of a single step in the text zoom range, in percent
105 private static final int TEXT_ZOOM_STEP = 5;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700106 // The initial value in the double tap zoom range
107 // This is what represents 100% in the SeekBarPreference range
108 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
109 // The size of a single step in the double tap zoom range, in percent
110 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -0700111
John Reck35e9dd62011-04-25 09:01:54 -0700112 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -0700113
John Reck35e9dd62011-04-25 09:01:54 -0700114 private Context mContext;
115 private SharedPreferences mPrefs;
116 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700117 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700118 private WebStorageSizeManager mWebStorageSizeManager;
119 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700120 private WeakHashMap<WebSettings, String> mCustomUserAgents;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100121 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -0700122 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -0700123 private float mFontSizeMult = 1.0f;
John Reck78a6a1d2011-07-21 13:54:03 -0700124
Victoria Lease96497832012-03-23 14:19:56 -0700125 // Current state of network-dependent settings
126 private boolean mLinkPrefetchAllowed = true;
127
John Reck78a6a1d2011-07-21 13:54:03 -0700128 // Cached values
129 private int mPageCacheCapacity = 1;
130 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800131
John Reck35e9dd62011-04-25 09:01:54 -0700132 // Cached settings
133 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800134
Ben Murdochaaa1f372011-07-25 15:40:58 +0100135 private static String sFactoryResetUrl;
136
qqzhou8c5b0a32013-07-22 15:31:03 +0800137 // add for carrier feature
138 private static Context sResPackageCtx;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800139
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700140 public static void initialize(final Context context) {
John Reck35e9dd62011-04-25 09:01:54 -0700141 sInstance = new BrowserSettings(context);
142 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800143
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700144 public static BrowserSettings getInstance() {
John Reck35e9dd62011-04-25 09:01:54 -0700145 return sInstance;
146 }
Ben Murdochef671652010-11-25 17:17:58 +0000147
John Reck35e9dd62011-04-25 09:01:54 -0700148 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100149 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700150 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
151 mAutofillHandler = new AutofillHandler(mContext);
152 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700153 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
qqzhou8c5b0a32013-07-22 15:31:03 +0800154
155 // add for carrier feature
156 try {
157 sResPackageCtx = context.createPackageContext(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700158 "com.android.browser.res",
qqzhou8c5b0a32013-07-22 15:31:03 +0800159 Context.CONTEXT_IGNORE_SECURITY);
160 } catch (Exception e) {
161 Log.e("Res_Update", "Create Res Apk Failed");
162 }
John Reckcadae722011-07-25 11:36:17 -0700163 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700164 }
165
166 public void setController(Controller controller) {
167 mController = controller;
John Reckc477e712011-08-17 11:27:15 -0700168 if (sInitialized) {
169 syncSharedSettings();
170 }
Ben Murdochef671652010-11-25 17:17:58 +0000171 }
172
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800173 public void startManagingSettings(final WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800174
John Reckc477e712011-08-17 11:27:15 -0700175 if (mNeedsSharedSync) {
176 syncSharedSettings();
177 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800178
John Reck35e9dd62011-04-25 09:01:54 -0700179 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800180 syncStaticSettings(settings);
181 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700182 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800183 }
184 }
185
John Reckd1d87312012-03-08 13:25:00 -0800186 public void stopManagingSettings(WebSettings settings) {
187 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
188 while (iter.hasNext()) {
189 WeakReference<WebSettings> ref = iter.next();
190 if (ref.get() == settings) {
191 iter.remove();
192 return;
193 }
194 }
195 }
196
John Reckcadae722011-07-25 11:36:17 -0700197 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700198
199 @Override
200 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700201 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
202 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700203 // the cost of one cached page is ~3M (measured using nytimes.com). For
204 // low end devices, we only cache one page. For high end devices, we try
205 // to cache more pages, currently choose 5.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800206
207 // SWE_TODO : assume a high-memory device
208 //if (ActivityManager.staticGetMemoryClass() > 16) {
John Reck78a6a1d2011-07-21 13:54:03 -0700209 mPageCacheCapacity = 5;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800210 //}
John Reck78a6a1d2011-07-21 13:54:03 -0700211 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
212 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
213 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700214 // Workaround b/5254577
215 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700216 if (Build.VERSION.CODENAME.equals("REL")) {
217 // This is a release build, always startup with debug disabled
218 setDebugEnabled(false);
219 }
220 if (mPrefs.contains(PREF_TEXT_SIZE)) {
221 /*
222 * Update from TextSize enum to zoom percent
223 * SMALLEST is 50%
224 * SMALLER is 75%
225 * NORMAL is 100%
226 * LARGER is 150%
227 * LARGEST is 200%
228 */
229 switch (getTextSize()) {
230 case SMALLEST:
231 setTextZoom(50);
232 break;
233 case SMALLER:
234 setTextZoom(75);
235 break;
236 case LARGER:
237 setTextZoom(150);
238 break;
239 case LARGEST:
240 setTextZoom(200);
241 break;
242 }
243 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
244 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100245
qqzhou8c5b0a32013-07-22 15:31:03 +0800246 // add for carrier homepage feature
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700247 String browserRes = mContext.getResources().getString(R.string.config_carrier_resource);
kaiyiz08f9e2b2013-08-16 09:15:48 +0800248 if ("cu".equals(browserRes) || "cmcc".equals(browserRes)) {
qqzhou8c5b0a32013-07-22 15:31:03 +0800249 int resID = sResPackageCtx.getResources().getIdentifier(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700250 "homepage_base", "string", "com.android.browser.res");
qqzhou8c5b0a32013-07-22 15:31:03 +0800251 sFactoryResetUrl = sResPackageCtx.getResources().getString(resID);
252 } else if ("ct".equals(browserRes)) {
253 int resID = sResPackageCtx.getResources().getIdentifier(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700254 "homepage_base", "string", "com.android.browser.res");
qqzhou8c5b0a32013-07-22 15:31:03 +0800255 sFactoryResetUrl = sResPackageCtx.getResources().getString(resID);
256
257 int pathID = sResPackageCtx.getResources().getIdentifier(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700258 "homepage_path", "string", "com.android.browser.res");
qqzhou8c5b0a32013-07-22 15:31:03 +0800259 String path = sResPackageCtx.getResources().getString(pathID);
260 Locale locale = Locale.getDefault();
261 path = path.replace("%y", locale.getLanguage().toLowerCase());
262 path = path.replace("%z", '_'+locale.getCountry().toLowerCase());
263 boolean useCountry = true;
264 boolean useLanguage = true;
265 InputStream is = null;
266 AssetManager am = mContext.getAssets();
267 try {
268 is = am.open(path);
269 } catch (Exception ignored) {
270 useCountry = false;
271 path = sResPackageCtx.getResources().getString(pathID);
272 path = path.replace("%y", locale.getLanguage().toLowerCase());
273 path = path.replace("%z", "");
274 try {
275 is = am.open(path);
276 } catch (Exception ignoredlanguage) {
277 useLanguage = false;
278 }
279 } finally {
280 if (is != null) {
281 try {
282 is.close();
283 } catch (Exception ignored) {}
284 }
285 }
286
287 if (!useCountry && !useLanguage) {
288 sFactoryResetUrl = sFactoryResetUrl.replace("%y%z", "en");
289 } else {
290 sFactoryResetUrl = sFactoryResetUrl.replace("%y",
291 locale.getLanguage().toLowerCase());
292 sFactoryResetUrl = sFactoryResetUrl.replace("%z", useCountry ?
293 '_' + locale.getCountry().toLowerCase() : "");
294 }
295 } else {
296 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
297 }
298
kaiyizbf086ea2013-08-02 11:03:58 +0800299 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
300 if (!"default".equals(browserRes)) {
301 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING,
Vivek Sekhar22bd57b2014-04-05 22:19:43 -0700302 "UTF-8").apply();
kaiyizbf086ea2013-08-02 11:03:58 +0800303 }
304 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100305 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
306 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
307 BrowserProvider.getClientId(mContext.getContentResolver()));
308 }
309
Ben Murdochaaa1f372011-07-25 15:40:58 +0100310 synchronized (BrowserSettings.class) {
311 sInitialized = true;
312 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700313 }
314 }
315 };
316
Ben Murdochaaa1f372011-07-25 15:40:58 +0100317 private static void requireInitialization() {
318 synchronized (BrowserSettings.class) {
319 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700320 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100321 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700322 } catch (InterruptedException e) {
323 }
324 }
325 }
326 }
327
The Android Open Source Project0c908882009-03-03 19:32:16 -0800328 /**
John Reck35e9dd62011-04-25 09:01:54 -0700329 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800330 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800331 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700332 settings.setGeolocationEnabled(enableGeolocation());
333 settings.setJavaScriptEnabled(enableJavascript());
334 settings.setLightTouchEnabled(enableLightTouch());
335 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700336 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
337 settings.setDefaultZoom(getDefaultZoom());
338 settings.setMinimumFontSize(getMinimumFontSize());
339 settings.setMinimumLogicalFontSize(getMinimumFontSize());
340 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700341 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700342 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100343 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700344 settings.setLoadsImagesAutomatically(loadImages());
345 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
346 settings.setSavePassword(rememberPasswords());
347 settings.setSaveFormData(saveFormdata());
348 settings.setUseWideViewPort(isWideViewport());
John Reckb8b2af82011-05-20 15:58:33 -0700349
qqzhoue6ff8b42013-07-23 17:28:48 +0800350 // add for carrier useragent feature
351 String ua = null;
Bijan Amirzada58383e72014-04-01 14:45:22 -0700352 Object objUserAgentHandler = ReflectHelper.newObject(
353 "com.qrd.useragent.UserAgentHandler", null, null);
354 Object[] params = {mContext};
355 Class[] type = new Class[] {Context.class};
356 ua = (String) ReflectHelper.invokeMethod(objUserAgentHandler,"getUAString",
357 type, params);
358 if (ua == null)
qqzhoue6ff8b42013-07-23 17:28:48 +0800359 ua = mCustomUserAgents.get(settings);
Bijan Amirzada58383e72014-04-01 14:45:22 -0700360
361 if (ua != null){
John Reckb8b2af82011-05-20 15:58:33 -0700362 settings.setUserAgentString(ua);
363 } else {
364 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
365 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700366
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800367 WebSettings settingsClassic = (WebSettings) settings;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800368 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
369 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
370 settingsClassic.setForceUserScalable(forceEnableUserScalable());
371 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
372 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800373
John Reckea17e782011-10-27 17:15:59 -0700374 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800375 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700376 useInverted ? "true" : "false");
377 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800378 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700379 Float.toString(getInvertedContrast()));
380 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700381
John Reckea17e782011-10-27 17:15:59 -0700382 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800383 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700384 enableCpuUploadPath() ? "true" : "false");
385 }
Victoria Lease96497832012-03-23 14:19:56 -0700386
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800387 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000388 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800389
John Reck35e9dd62011-04-25 09:01:54 -0700390 /**
391 * Syncs all the settings that have no UI
392 * These cannot change, so we only need to set them once per WebSettings
393 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800394 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700395 settings.setDefaultFontSize(16);
396 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000397
John Reck35e9dd62011-04-25 09:01:54 -0700398 // WebView inside Browser doesn't want initial focus to be set.
399 settings.setNeedInitialFocus(false);
400 // Browser supports multiple windows
401 settings.setSupportMultipleWindows(true);
402 // enable smooth transition for better performance during panning or
403 // zooming
404 settings.setEnableSmoothTransition(true);
405 // disable content url access
Bijan Amirzada59d4a342014-03-27 13:20:12 -0700406 settings.setAllowContentAccess(true);
John Reck35e9dd62011-04-25 09:01:54 -0700407
408 // HTML5 API flags
409 settings.setAppCacheEnabled(true);
410 settings.setDatabaseEnabled(true);
411 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700412
413 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700414 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700415 settings.setAppCachePath(getAppCachePath());
416 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
417 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700418 // origin policy for file access
419 settings.setAllowUniversalAccessFromFileURLs(false);
420 settings.setAllowFileAccessFromFileURLs(false);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800421
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800422 //if (!(settings instanceof WebSettingsClassic)) return;
423 /*
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800424
425 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
426 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
427 // WebView should be preserving the memory as much as possible.
428 // However, apps like browser wish to turn on the performance mode which
429 // would require more memory.
430 // TODO: We need to dynamically allocate/deallocate temporary memory for
431 // apps which are trying to use minimal memory. Currently, double
432 // buffering is always turned on, which is unnecessary.
433 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
434 settingsClassic.setWorkersEnabled(true); // This only affects V8.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800435 */
John Reck35e9dd62011-04-25 09:01:54 -0700436 }
437
438 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700439 mNeedsSharedSync = false;
Axesh R. Ajmera579c70c2014-04-17 13:24:56 -0700440 CookieManager.getInstance().setAcceptCookie(acceptCookies());
441
John Reck35e9dd62011-04-25 09:01:54 -0700442 if (mController != null) {
443 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700444 }
John Reck35e9dd62011-04-25 09:01:54 -0700445 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700446
John Reck35e9dd62011-04-25 09:01:54 -0700447 private void syncManagedSettings() {
448 syncSharedSettings();
449 synchronized (mManagedSettings) {
450 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
451 while (iter.hasNext()) {
452 WeakReference<WebSettings> ref = iter.next();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800453 WebSettings settings = (WebSettings)ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700454 if (settings == null) {
455 iter.remove();
456 continue;
457 }
458 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000459 }
John Reck35e9dd62011-04-25 09:01:54 -0700460 }
461 }
Ben Murdochef671652010-11-25 17:17:58 +0000462
John Reck35e9dd62011-04-25 09:01:54 -0700463 @Override
464 public void onSharedPreferenceChanged(
465 SharedPreferences sharedPreferences, String key) {
466 syncManagedSettings();
467 if (PREF_SEARCH_ENGINE.equals(key)) {
468 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700469 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200470 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700471 mController.getUi().setFullscreen(useFullscreen());
472 }
Michael Kolb0241e752011-07-07 14:58:50 -0700473 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200474 if (mController != null && mController.getUi() != null) {
Michael Kolb0241e752011-07-07 14:58:50 -0700475 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
476 }
Victoria Lease96497832012-03-23 14:19:56 -0700477 } else if (PREF_LINK_PREFETCH.equals(key)) {
478 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700479 }
John Reck35e9dd62011-04-25 09:01:54 -0700480 }
481
John Reck961d35d2011-06-23 09:45:54 -0700482 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100483 requireInitialization();
484 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700485 }
486
487 public LayoutAlgorithm getLayoutAlgorithm() {
488 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
489 if (autofitPages()) {
490 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
491 }
492 if (isDebugEnabled()) {
493 if (isSmallScreen()) {
494 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000495 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700496 if (isNormalLayout()) {
497 layoutAlgorithm = LayoutAlgorithm.NORMAL;
498 } else {
499 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
500 }
Ben Murdochef671652010-11-25 17:17:58 +0000501 }
John Reck35e9dd62011-04-25 09:01:54 -0700502 }
503 return layoutAlgorithm;
504 }
Ben Murdochef671652010-11-25 17:17:58 +0000505
John Reck35e9dd62011-04-25 09:01:54 -0700506 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700507 requireInitialization();
508 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800509 }
510
John Reck35e9dd62011-04-25 09:01:54 -0700511 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700512 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700513 return mWebStorageSizeManager;
514 }
515
John Reck35e9dd62011-04-25 09:01:54 -0700516 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700517 if (mAppCachePath == null) {
518 mAppCachePath = mContext.getDir("appcache", 0).getPath();
519 }
520 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700521 }
522
523 private void updateSearchEngine(boolean force) {
524 String searchEngineName = getSearchEngineName();
525 if (force || mSearchEngine == null ||
526 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700527 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000528 }
529 }
530
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100531 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700532 if (mSearchEngine == null) {
533 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100534 }
John Reck35e9dd62011-04-25 09:01:54 -0700535 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100536 }
537
John Reck35e9dd62011-04-25 09:01:54 -0700538 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700539 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700540 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100541 }
542
John Reck35e9dd62011-04-25 09:01:54 -0700543 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700544 Editor edit = mPrefs.edit();
545 edit.putBoolean(PREF_DEBUG_MENU, value);
546 if (!value) {
547 // Reset to "safe" value
548 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
549 }
550 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100551 }
552
John Reck35e9dd62011-04-25 09:01:54 -0700553 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800554 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700555 if (mController != null) {
556 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800557 if (current != null) {
558 current.clearCache(true);
559 }
560 }
561 }
562
John Reck35e9dd62011-04-25 09:01:54 -0700563 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800564 CookieManager.getInstance().removeAllCookie();
565 }
566
John Reck35e9dd62011-04-25 09:01:54 -0700567 public void clearHistory() {
568 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800569 Browser.clearHistory(resolver);
570 Browser.clearSearches(resolver);
571 }
572
John Reck35e9dd62011-04-25 09:01:54 -0700573 public void clearFormData() {
574 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700575 if (mController!= null) {
576 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100577 if (currentTopView != null) {
578 currentTopView.clearFormData();
579 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800580 }
581 }
582
John Reck35e9dd62011-04-25 09:01:54 -0700583 public void clearPasswords() {
Panos Thomasfa948b82014-03-09 03:42:42 -0700584 // Clear password store maintained by SWE engine
585 WebSettings settings = null;
586 // find a valid settings object
587 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
588 while (iter.hasNext()) {
589 WeakReference<WebSettings> ref = iter.next();
590 settings = (WebSettings)ref.get();
591 if (settings != null) {
592 break;
593 }
594 }
595 if (settings != null) {
596 settings.clearPasswords();
597 }
598
599 // Clear passwords in WebView database
John Reck35e9dd62011-04-25 09:01:54 -0700600 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800601 db.clearUsernamePassword();
602 db.clearHttpAuthUsernamePassword();
603 }
604
John Reck35e9dd62011-04-25 09:01:54 -0700605 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100606 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100607 }
608
John Reck35e9dd62011-04-25 09:01:54 -0700609 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100610 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100611 }
612
John Reck35e9dd62011-04-25 09:01:54 -0700613 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700614 mPrefs.edit()
615 .clear()
John Reckbd315192011-07-29 10:05:47 -0700616 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200617 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700618 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700619 }
620
Björn Isakssonc885a242012-06-05 17:19:04 +0200621 private void resetCachedValues() {
622 updateSearchEngine(false);
623 }
624
John Reck35e9dd62011-04-25 09:01:54 -0700625 public AutoFillProfile getAutoFillProfile() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800626 // query the profile from components autofill database 524
627 if (mAutofillHandler.mAutoFillProfile == null &&
628 !mAutofillHandler.mAutoFillActiveProfileId.equals("")) {
629 WebSettings settings = null;
630 // find a valid settings object
631 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
632 while (iter.hasNext()) {
633 WeakReference<WebSettings> ref = iter.next();
634 settings = (WebSettings)ref.get();
635 if (settings != null) {
636 break;
637 }
638 }
639 if (settings != null) {
640 AutoFillProfile profile =
641 settings.getAutoFillProfile(mAutofillHandler.mAutoFillActiveProfileId);
642 mAutofillHandler.setAutoFillProfile(profile);
643 }
644 }
John Reck35e9dd62011-04-25 09:01:54 -0700645 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800646 }
647
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800648 public String getAutoFillProfileId() {
649 return mAutofillHandler.getAutoFillProfileId();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800650 }
651
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800652 public void updateAutoFillProfile(AutoFillProfile profile) {
653 syncAutoFillProfile(profile);
654 }
655
656 private void syncAutoFillProfile(AutoFillProfile profile) {
657 synchronized (mManagedSettings) {
658 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
659 while (iter.hasNext()) {
660 WeakReference<WebSettings> ref = iter.next();
661 WebSettings settings = (WebSettings)ref.get();
662 if (settings == null) {
663 iter.remove();
664 continue;
665 }
666 // update the profile only once.
667 settings.setAutoFillProfile(profile);
668 // Now we should have the guid
669 mAutofillHandler.setAutoFillProfile(profile);
670 break;
671 }
672 }
673 }
John Reck35e9dd62011-04-25 09:01:54 -0700674 public void toggleDebugSettings() {
675 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800676 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100677
John Reckb8b2af82011-05-20 15:58:33 -0700678 public boolean hasDesktopUseragent(WebView view) {
679 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
680 }
681
682 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700683 if (view == null) {
684 return;
685 }
John Reckb8b2af82011-05-20 15:58:33 -0700686 WebSettings settings = view.getSettings();
687 if (mCustomUserAgents.get(settings) != null) {
688 mCustomUserAgents.remove(settings);
689 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
690 } else {
691 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
692 settings.setUserAgentString(DESKTOP_USERAGENT);
693 }
694 }
695
John Reck7dc444b2011-06-16 17:44:29 -0700696 public static int getAdjustedMinimumFontSize(int rawValue) {
697 rawValue++; // Preference starts at 0, min font at 1
698 if (rawValue > 1) {
699 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
700 }
701 return rawValue;
702 }
703
John Reck5ba3c762011-09-14 15:00:15 -0700704 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700705 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700706 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700707 }
708
709 static int getRawTextZoom(int percent) {
710 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
711 }
712
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700713 public int getAdjustedDoubleTapZoom(int rawValue) {
714 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
715 return (int) ((rawValue + 100) * mFontSizeMult);
716 }
717
718 static int getRawDoubleTapZoom(int percent) {
719 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
720 }
721
John Reckcadae722011-07-25 11:36:17 -0700722 public SharedPreferences getPreferences() {
723 return mPrefs;
724 }
725
Victoria Lease96497832012-03-23 14:19:56 -0700726 // update connectivity-dependent options
727 public void updateConnectionType() {
728 ConnectivityManager cm = (ConnectivityManager)
729 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
730 String linkPrefetchPreference = getLinkPrefetchEnabled();
731 boolean linkPrefetchAllowed = linkPrefetchPreference.
732 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
733 NetworkInfo ni = cm.getActiveNetworkInfo();
734 if (ni != null) {
735 switch (ni.getType()) {
736 case ConnectivityManager.TYPE_WIFI:
737 case ConnectivityManager.TYPE_ETHERNET:
738 case ConnectivityManager.TYPE_BLUETOOTH:
739 linkPrefetchAllowed |= linkPrefetchPreference.
740 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
741 break;
742 case ConnectivityManager.TYPE_MOBILE:
743 case ConnectivityManager.TYPE_MOBILE_DUN:
744 case ConnectivityManager.TYPE_MOBILE_MMS:
745 case ConnectivityManager.TYPE_MOBILE_SUPL:
746 case ConnectivityManager.TYPE_WIMAX:
747 default:
748 break;
749 }
750 }
751 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
752 mLinkPrefetchAllowed = linkPrefetchAllowed;
753 syncManagedSettings();
754 }
755 }
756
luxiaol62677b02013-07-22 07:54:49 +0800757 public String getDownloadPath() {
758 return mPrefs.getString(PREF_DOWNLOAD_PATH,
759 DownloadHandler.getDefaultDownloadPath(mContext));
760 }
John Reck35e9dd62011-04-25 09:01:54 -0700761 // -----------------------------
762 // getter/setters for accessibility_preferences.xml
763 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100764
John Reck7dc444b2011-06-16 17:44:29 -0700765 @Deprecated
766 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700767 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
768 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100769 }
770
John Reck35e9dd62011-04-25 09:01:54 -0700771 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700772 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700773 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100774 }
775
John Reck92f25f82011-04-26 16:57:10 -0700776 public boolean forceEnableUserScalable() {
777 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
778 }
779
John Reck7dc444b2011-06-16 17:44:29 -0700780 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700781 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700782 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
783 return getAdjustedTextZoom(textZoom);
784 }
785
786 public void setTextZoom(int percent) {
787 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
788 }
789
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700790 public int getDoubleTapZoom() {
791 requireInitialization();
792 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
793 return getAdjustedDoubleTapZoom(doubleTapZoom);
794 }
795
796 public void setDoubleTapZoom(int percent) {
797 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
798 }
799
John Reck35e9dd62011-04-25 09:01:54 -0700800 // -----------------------------
801 // getter/setters for advanced_preferences.xml
802 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100803
John Reck35e9dd62011-04-25 09:01:54 -0700804 public String getSearchEngineName() {
luxiaol221b3932013-07-19 15:27:57 +0800805 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
806 if (defaultSearchEngineValue == null) {
807 defaultSearchEngineValue = SearchEngine.GOOGLE;
808 }
809 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100810 }
John Reck63bb6872010-12-01 19:29:32 -0800811
Michael Kolb8d772b02012-01-19 13:56:00 -0800812 public boolean allowAppTabs() {
813 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
814 }
815
John Reck35e9dd62011-04-25 09:01:54 -0700816 public boolean openInBackground() {
817 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800818 }
John Reck35e9dd62011-04-25 09:01:54 -0700819
820 public boolean enableJavascript() {
821 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
822 }
823
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800824 public boolean enableMemoryMonitor() {
825 return mPrefs.getBoolean(PREF_ENABLE_MEMORY_MONITOR, true);
826 }
827
John Reck35e9dd62011-04-25 09:01:54 -0700828 // TODO: Cache
829 public PluginState getPluginState() {
830 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
831 return PluginState.valueOf(state);
832 }
833
834 // TODO: Cache
835 public ZoomDensity getDefaultZoom() {
836 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
837 return ZoomDensity.valueOf(zoom);
838 }
839
840 public boolean loadPageInOverviewMode() {
841 return mPrefs.getBoolean(PREF_LOAD_PAGE, true);
842 }
843
844 public boolean autofitPages() {
845 return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true);
846 }
847
848 public boolean blockPopupWindows() {
849 return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true);
850 }
851
852 public boolean loadImages() {
853 return mPrefs.getBoolean(PREF_LOAD_IMAGES, true);
854 }
855
856 public String getDefaultTextEncoding() {
857 return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null);
858 }
859
860 // -----------------------------
861 // getter/setters for general_preferences.xml
862 // -----------------------------
863
864 public String getHomePage() {
John Reck35e9dd62011-04-25 09:01:54 -0700865 return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext));
866 }
867
868 public void setHomePage(String value) {
869 mPrefs.edit().putString(PREF_HOMEPAGE, value).apply();
870 }
871
872 public boolean isAutofillEnabled() {
873 return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true);
874 }
875
876 public void setAutofillEnabled(boolean value) {
877 mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply();
878 }
879
880 // -----------------------------
881 // getter/setters for debug_preferences.xml
882 // -----------------------------
883
884 public boolean isHardwareAccelerated() {
John Reckf48314f2011-04-27 17:52:17 -0700885 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700886 return true;
887 }
888 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true);
889 }
890
Derek Sollenberger31adf672011-07-08 11:31:30 -0400891 public boolean isSkiaHardwareAccelerated() {
892 if (!isDebugEnabled()) {
893 return false;
894 }
895 return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
896 }
897
John Reck35e9dd62011-04-25 09:01:54 -0700898 public int getUserAgent() {
John Reckf48314f2011-04-27 17:52:17 -0700899 if (!isDebugEnabled()) {
John Reck35e9dd62011-04-25 09:01:54 -0700900 return 0;
901 }
902 return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0"));
903 }
904
905 // -----------------------------
906 // getter/setters for hidden_debug_preferences.xml
907 // -----------------------------
908
909 public boolean enableVisualIndicator() {
910 if (!isDebugEnabled()) {
911 return false;
912 }
913 return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false);
914 }
915
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700916 public boolean enableCpuUploadPath() {
917 if (!isDebugEnabled()) {
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700918 return false;
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700919 }
Teng-Hui Zhudbe001b2011-09-30 10:37:01 -0700920 return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false);
Teng-Hui Zhu85de57a2011-09-22 15:34:29 -0700921 }
922
John Reck35e9dd62011-04-25 09:01:54 -0700923 public boolean enableJavascriptConsole() {
924 if (!isDebugEnabled()) {
925 return false;
926 }
927 return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true);
928 }
929
930 public boolean isSmallScreen() {
931 if (!isDebugEnabled()) {
932 return false;
933 }
934 return mPrefs.getBoolean(PREF_SMALL_SCREEN, false);
935 }
936
937 public boolean isWideViewport() {
938 if (!isDebugEnabled()) {
939 return true;
940 }
941 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
942 }
943
944 public boolean isNormalLayout() {
945 if (!isDebugEnabled()) {
946 return false;
947 }
948 return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false);
949 }
950
951 public boolean isTracing() {
952 if (!isDebugEnabled()) {
953 return false;
954 }
955 return mPrefs.getBoolean(PREF_ENABLE_TRACING, false);
956 }
957
958 public boolean enableLightTouch() {
959 if (!isDebugEnabled()) {
960 return false;
961 }
962 return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false);
963 }
964
965 public boolean enableNavDump() {
966 if (!isDebugEnabled()) {
967 return false;
968 }
969 return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false);
970 }
971
972 public String getJsEngineFlags() {
973 if (!isDebugEnabled()) {
974 return "";
975 }
976 return mPrefs.getString(PREF_JS_ENGINE_FLAGS, "");
977 }
978
979 // -----------------------------
980 // getter/setters for lab_preferences.xml
981 // -----------------------------
982
983 public boolean useQuickControls() {
984 return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false);
985 }
986
987 public boolean useMostVisitedHomepage() {
John Reck961d35d2011-06-23 09:45:54 -0700988 return HomeProvider.MOST_VISITED.equals(getHomePage());
John Reck35e9dd62011-04-25 09:01:54 -0700989 }
990
Michael Kolbc38c6042011-04-27 10:46:06 -0700991 public boolean useFullscreen() {
992 return mPrefs.getBoolean(PREF_FULLSCREEN, false);
993 }
994
John Reck2fd9d0e2011-07-15 11:13:48 -0700995 public boolean useInvertedRendering() {
996 return mPrefs.getBoolean(PREF_INVERTED, false);
997 }
998
Nicolas Roard5d513102011-08-03 15:35:34 -0700999 public float getInvertedContrast() {
1000 return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
1001 }
1002
John Reck35e9dd62011-04-25 09:01:54 -07001003 // -----------------------------
1004 // getter/setters for privacy_security_preferences.xml
1005 // -----------------------------
1006
1007 public boolean showSecurityWarnings() {
1008 return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true);
1009 }
1010
1011 public boolean acceptCookies() {
1012 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
1013 }
1014
1015 public boolean saveFormdata() {
1016 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
1017 }
1018
1019 public boolean enableGeolocation() {
1020 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
1021 }
1022
1023 public boolean rememberPasswords() {
1024 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
1025 }
1026
Michael Kolb14612442011-06-24 13:06:29 -07001027 // -----------------------------
1028 // getter/setters for bandwidth_preferences.xml
1029 // -----------------------------
1030
Mathew Inwood467813f2011-09-02 15:43:17 +01001031 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
1032 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -07001033 }
Mathew Inwood467813f2011-09-02 15:43:17 +01001034
1035 public static String getPreloadAlwaysPreferenceString(Context context) {
1036 return context.getResources().getString(R.string.pref_data_preload_value_always);
1037 }
1038
Mathew Inwood825fba72011-10-04 14:52:52 +01001039 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
1040 "browser_default_preload_setting";
1041
1042 public String getDefaultPreloadSetting() {
1043 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1044 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1045 if (preload == null) {
1046 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1047 }
1048 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001049 }
1050
1051 public String getPreloadEnabled() {
1052 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1053 }
1054
Victoria Lease96497832012-03-23 14:19:56 -07001055 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1056 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1057 }
1058
1059 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1060 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1061 }
1062
1063 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1064 "browser_default_link_prefetch_setting";
1065
1066 public String getDefaultLinkPrefetchSetting() {
1067 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1068 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1069 if (preload == null) {
1070 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1071 }
1072 return preload;
1073 }
1074
1075 public String getLinkPrefetchEnabled() {
1076 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1077 }
1078
George Mount3636d0a2011-11-21 09:08:21 -08001079 // -----------------------------
1080 // getter/setters for browser recovery
1081 // -----------------------------
1082 /**
1083 * The last time browser was started.
1084 * @return The last browser start time as System.currentTimeMillis. This
1085 * can be 0 if this is the first time or the last tab was closed.
1086 */
1087 public long getLastRecovered() {
1088 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1089 }
1090
1091 /**
1092 * Sets the last browser start time.
1093 * @param time The last time as System.currentTimeMillis that the browser
1094 * was started. This should be set to 0 if the last tab is closed.
1095 */
1096 public void setLastRecovered(long time) {
1097 mPrefs.edit()
1098 .putLong(KEY_LAST_RECOVERED, time)
1099 .apply();
1100 }
1101
1102 /**
1103 * Used to determine whether or not the previous browser run crashed. Once
1104 * the previous state has been determined, the value will be set to false
1105 * until a pause is received.
1106 * @return true if the last browser run was paused or false if it crashed.
1107 */
1108 public boolean wasLastRunPaused() {
1109 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1110 }
1111
1112 /**
1113 * Sets whether or not the last run was a pause or crash.
1114 * @param isPaused Set to true When a pause is received or false after
1115 * resuming.
1116 */
1117 public void setLastRunPaused(boolean isPaused) {
1118 mPrefs.edit()
1119 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1120 .apply();
1121 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001122}