blob: b6ccfe5f03f339aab502df7f366c3242cfa758fd [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
17package com.android.browser;
18
Grace Kloba9804c432009-12-02 11:07:40 -080019import android.app.ActivityManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080020import android.content.ContentResolver;
21import android.content.Context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.content.SharedPreferences;
John Reck1da81882011-10-04 17:21:10 -070023import android.content.SharedPreferences.Editor;
John Reck812d2d62011-01-18 14:16:15 -080024import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
qqzhou8c5b0a32013-07-22 15:31:03 +080025import android.content.res.AssetManager;
Victoria Lease96497832012-03-23 14:19:56 -070026import android.net.ConnectivityManager;
27import android.net.NetworkInfo;
John Reckf48314f2011-04-27 17:52:17 -070028import android.os.Build;
Ben Murdoch23da30e2010-10-26 15:18:44 +010029import android.os.Message;
qqzhou8c5b0a32013-07-22 15:31:03 +080030import android.os.SystemProperties;
Ben Murdoch0cb81892010-10-08 12:41:33 +010031import android.preference.PreferenceManager;
Ben Murdoch0cb81892010-10-08 12:41:33 +010032import android.provider.Browser;
Mathew Inwood825fba72011-10-04 14:52:52 +010033import android.provider.Settings;
John Reck5ba3c762011-09-14 15:00:15 -070034import android.util.DisplayMetrics;
qqzhou8c5b0a32013-07-22 15:31:03 +080035import android.util.Log;
qqzhoue6ff8b42013-07-23 17:28:48 +080036import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.webkit.CookieManager;
Steve Blockf344d032009-07-30 10:50:45 +010038import android.webkit.GeolocationPermissions;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.webkit.WebIconDatabase;
40import android.webkit.WebSettings;
John Reck35e9dd62011-04-25 09:01:54 -070041import android.webkit.WebSettings.LayoutAlgorithm;
42import android.webkit.WebSettings.PluginState;
43import android.webkit.WebSettings.TextSize;
44import android.webkit.WebSettings.ZoomDensity;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000045import android.webkit.WebSettingsClassic;
46import android.webkit.WebSettingsClassic.AutoFillProfile;
Nicolas Roard78a98e42009-05-11 13:34:17 +010047import android.webkit.WebStorage;
John Reck812d2d62011-01-18 14:16:15 -080048import android.webkit.WebView;
49import android.webkit.WebViewDatabase;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050
John Reck46500332011-06-07 14:36:10 -070051import com.android.browser.homepages.HomeProvider;
52import com.android.browser.provider.BrowserProvider;
53import com.android.browser.search.SearchEngine;
54import com.android.browser.search.SearchEngines;
55
qqzhou8c5b0a32013-07-22 15:31:03 +080056import java.io.InputStream;
qqzhoue6ff8b42013-07-23 17:28:48 +080057import java.lang.Class;
John Reck35e9dd62011-04-25 09:01:54 -070058import java.lang.ref.WeakReference;
qqzhoue6ff8b42013-07-23 17:28:48 +080059import java.lang.reflect.Method;
John Reck35e9dd62011-04-25 09:01:54 -070060import java.util.Iterator;
61import java.util.LinkedList;
qqzhou8c5b0a32013-07-22 15:31:03 +080062import java.util.Locale;
John Reckb8b2af82011-05-20 15:58:33 -070063import java.util.WeakHashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080064
John Reck35e9dd62011-04-25 09:01:54 -070065/**
66 * Class for managing settings
The Android Open Source Project0c908882009-03-03 19:32:16 -080067 */
John Reck35e9dd62011-04-25 09:01:54 -070068public class BrowserSettings implements OnSharedPreferenceChangeListener,
69 PreferenceKeys {
Andrei Popescu01068702009-08-03 16:03:24 +010070
John Reck35e9dd62011-04-25 09:01:54 -070071 // TODO: Do something with this UserAgent stuff
John Reck7fd1e8f2011-04-27 18:22:57 -070072 private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " +
73 "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " +
74 "Chrome/11.0.696.34 Safari/534.24";
The Android Open Source Project0c908882009-03-03 19:32:16 -080075
76 private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070077 "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
78 "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
Bart Searsf6915fb2010-07-08 19:58:22 -070079
80 private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070081 "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
82 "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
Bart Searsf6915fb2010-07-08 19:58:22 -070083
84 private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
John Reck35e9dd62011-04-25 09:01:54 -070085 "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
86 "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
The Android Open Source Project0c908882009-03-03 19:32:16 -080087
John Reck7fd1e8f2011-04-27 18:22:57 -070088 private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " +
89 "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " +
90 "(KHTML, like Gecko) Version/4.0 Safari/534.13";
91
John Reck35e9dd62011-04-25 09:01:54 -070092 private static final String USER_AGENTS[] = { null,
93 DESKTOP_USERAGENT,
94 IPHONE_USERAGENT,
95 IPAD_USERAGENT,
John Reck7fd1e8f2011-04-27 18:22:57 -070096 FROYO_USERAGENT,
97 HONEYCOMB_USERAGENT,
John Reck35e9dd62011-04-25 09:01:54 -070098 };
The Android Open Source Project0c908882009-03-03 19:32:16 -080099
qqzhoue6ff8b42013-07-23 17:28:48 +0800100 private static final String TAG = "BrowserSettings";
101
John Reck8fc22a12011-06-16 14:57:41 -0700102 // The minimum min font size
103 // Aka, the lower bounds for the min font size range
104 // which is 1:5..24
105 private static final int MIN_FONT_SIZE_OFFSET = 5;
John Reck7dc444b2011-06-16 17:44:29 -0700106 // The initial value in the text zoom range
107 // This is what represents 100% in the SeekBarPreference range
108 private static final int TEXT_ZOOM_START_VAL = 10;
109 // The size of a single step in the text zoom range, in percent
110 private static final int TEXT_ZOOM_STEP = 5;
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700111 // The initial value in the double tap zoom range
112 // This is what represents 100% in the SeekBarPreference range
113 private static final int DOUBLE_TAP_ZOOM_START_VAL = 5;
114 // The size of a single step in the double tap zoom range, in percent
115 private static final int DOUBLE_TAP_ZOOM_STEP = 5;
John Reck8fc22a12011-06-16 14:57:41 -0700116
John Reck35e9dd62011-04-25 09:01:54 -0700117 private static BrowserSettings sInstance;
Jeff Davidson43610292010-07-16 16:03:58 -0700118
John Reck35e9dd62011-04-25 09:01:54 -0700119 private Context mContext;
120 private SharedPreferences mPrefs;
121 private LinkedList<WeakReference<WebSettings>> mManagedSettings;
Michael Kolb8233fac2010-10-26 16:08:53 -0700122 private Controller mController;
John Reck35e9dd62011-04-25 09:01:54 -0700123 private WebStorageSizeManager mWebStorageSizeManager;
124 private AutofillHandler mAutofillHandler;
John Reckb8b2af82011-05-20 15:58:33 -0700125 private WeakHashMap<WebSettings, String> mCustomUserAgents;
Ben Murdochaaa1f372011-07-25 15:40:58 +0100126 private static boolean sInitialized = false;
John Reckc477e712011-08-17 11:27:15 -0700127 private boolean mNeedsSharedSync = true;
John Reck5ba3c762011-09-14 15:00:15 -0700128 private float mFontSizeMult = 1.0f;
John Reck78a6a1d2011-07-21 13:54:03 -0700129
Victoria Lease96497832012-03-23 14:19:56 -0700130 // Current state of network-dependent settings
131 private boolean mLinkPrefetchAllowed = true;
132
John Reck78a6a1d2011-07-21 13:54:03 -0700133 // Cached values
134 private int mPageCacheCapacity = 1;
135 private String mAppCachePath;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800136
John Reck35e9dd62011-04-25 09:01:54 -0700137 // Cached settings
138 private SearchEngine mSearchEngine;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800139
Ben Murdochaaa1f372011-07-25 15:40:58 +0100140 private static String sFactoryResetUrl;
141
qqzhou8c5b0a32013-07-22 15:31:03 +0800142 // add for carrier feature
143 private static Context sResPackageCtx;
144
John Reck35e9dd62011-04-25 09:01:54 -0700145 public static void initialize(final Context context) {
146 sInstance = new BrowserSettings(context);
147 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800148
John Reck35e9dd62011-04-25 09:01:54 -0700149 public static BrowserSettings getInstance() {
150 return sInstance;
151 }
Ben Murdochef671652010-11-25 17:17:58 +0000152
John Reck35e9dd62011-04-25 09:01:54 -0700153 private BrowserSettings(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100154 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -0700155 mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
156 mAutofillHandler = new AutofillHandler(mContext);
157 mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
John Reckb8b2af82011-05-20 15:58:33 -0700158 mCustomUserAgents = new WeakHashMap<WebSettings, String>();
John Reck35e9dd62011-04-25 09:01:54 -0700159 mAutofillHandler.asyncLoadFromDb();
qqzhou8c5b0a32013-07-22 15:31:03 +0800160
161 // add for carrier feature
162 try {
163 sResPackageCtx = context.createPackageContext(
164 "com.android.browser.res",
165 Context.CONTEXT_IGNORE_SECURITY);
166 } catch (Exception e) {
167 Log.e("Res_Update", "Create Res Apk Failed");
168 }
169
John Reckcadae722011-07-25 11:36:17 -0700170 BackgroundHandler.execute(mSetup);
John Reck35e9dd62011-04-25 09:01:54 -0700171 }
172
173 public void setController(Controller controller) {
174 mController = controller;
John Reckc477e712011-08-17 11:27:15 -0700175 if (sInitialized) {
176 syncSharedSettings();
177 }
Ben Murdochef671652010-11-25 17:17:58 +0000178 }
179
John Reck35e9dd62011-04-25 09:01:54 -0700180 public void startManagingSettings(WebSettings settings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800181
John Reckc477e712011-08-17 11:27:15 -0700182 if (mNeedsSharedSync) {
183 syncSharedSettings();
184 }
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800185
John Reck35e9dd62011-04-25 09:01:54 -0700186 synchronized (mManagedSettings) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800187 syncStaticSettings(settings);
188 syncSetting(settings);
John Reck35e9dd62011-04-25 09:01:54 -0700189 mManagedSettings.add(new WeakReference<WebSettings>(settings));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 }
191 }
192
John Reckd1d87312012-03-08 13:25:00 -0800193 public void stopManagingSettings(WebSettings settings) {
194 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
195 while (iter.hasNext()) {
196 WeakReference<WebSettings> ref = iter.next();
197 if (ref.get() == settings) {
198 iter.remove();
199 return;
200 }
201 }
202 }
203
John Reckcadae722011-07-25 11:36:17 -0700204 private Runnable mSetup = new Runnable() {
John Reck78a6a1d2011-07-21 13:54:03 -0700205
206 @Override
207 public void run() {
John Reck5ba3c762011-09-14 15:00:15 -0700208 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
209 mFontSizeMult = metrics.scaledDensity / metrics.density;
John Reck78a6a1d2011-07-21 13:54:03 -0700210 // the cost of one cached page is ~3M (measured using nytimes.com). For
211 // low end devices, we only cache one page. For high end devices, we try
212 // to cache more pages, currently choose 5.
213 if (ActivityManager.staticGetMemoryClass() > 16) {
214 mPageCacheCapacity = 5;
215 }
216 mWebStorageSizeManager = new WebStorageSizeManager(mContext,
217 new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
218 new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));
John Reck276b1352011-09-02 15:47:33 -0700219 // Workaround b/5254577
220 mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
John Reck78a6a1d2011-07-21 13:54:03 -0700221 if (Build.VERSION.CODENAME.equals("REL")) {
222 // This is a release build, always startup with debug disabled
223 setDebugEnabled(false);
224 }
225 if (mPrefs.contains(PREF_TEXT_SIZE)) {
226 /*
227 * Update from TextSize enum to zoom percent
228 * SMALLEST is 50%
229 * SMALLER is 75%
230 * NORMAL is 100%
231 * LARGER is 150%
232 * LARGEST is 200%
233 */
234 switch (getTextSize()) {
235 case SMALLEST:
236 setTextZoom(50);
237 break;
238 case SMALLER:
239 setTextZoom(75);
240 break;
241 case LARGER:
242 setTextZoom(150);
243 break;
244 case LARGEST:
245 setTextZoom(200);
246 break;
247 }
248 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
249 }
Ben Murdochaaa1f372011-07-25 15:40:58 +0100250
qqzhou8c5b0a32013-07-22 15:31:03 +0800251
252 // add for carrier homepage feature
253 String browserRes = SystemProperties.get("persist.env.c.browser.resource", "default");
254 if ("cu".equals(browserRes)) {
255 int resID = sResPackageCtx.getResources().getIdentifier(
256 "homepage_base", "string", "com.android.browser.res");
257 sFactoryResetUrl = sResPackageCtx.getResources().getString(resID);
258 } else if ("ct".equals(browserRes)) {
259 int resID = sResPackageCtx.getResources().getIdentifier(
260 "homepage_base", "string", "com.android.browser.res");
261 sFactoryResetUrl = sResPackageCtx.getResources().getString(resID);
262
263 int pathID = sResPackageCtx.getResources().getIdentifier(
264 "homepage_path", "string", "com.android.browser.res");
265 String path = sResPackageCtx.getResources().getString(pathID);
266 Locale locale = Locale.getDefault();
267 path = path.replace("%y", locale.getLanguage().toLowerCase());
268 path = path.replace("%z", '_'+locale.getCountry().toLowerCase());
269 boolean useCountry = true;
270 boolean useLanguage = true;
271 InputStream is = null;
272 AssetManager am = mContext.getAssets();
273 try {
274 is = am.open(path);
275 } catch (Exception ignored) {
276 useCountry = false;
277 path = sResPackageCtx.getResources().getString(pathID);
278 path = path.replace("%y", locale.getLanguage().toLowerCase());
279 path = path.replace("%z", "");
280 try {
281 is = am.open(path);
282 } catch (Exception ignoredlanguage) {
283 useLanguage = false;
284 }
285 } finally {
286 if (is != null) {
287 try {
288 is.close();
289 } catch (Exception ignored) {}
290 }
291 }
292
293 if (!useCountry && !useLanguage) {
294 sFactoryResetUrl = sFactoryResetUrl.replace("%y%z", "en");
295 } else {
296 sFactoryResetUrl = sFactoryResetUrl.replace("%y",
297 locale.getLanguage().toLowerCase());
298 sFactoryResetUrl = sFactoryResetUrl.replace("%z", useCountry ?
299 '_' + locale.getCountry().toLowerCase() : "");
300 }
301 } else {
302 sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
303 }
304
kaiyizbf086ea2013-08-02 11:03:58 +0800305 if (!mPrefs.contains(PREF_DEFAULT_TEXT_ENCODING)) {
306 if (!"default".equals(browserRes)) {
307 mPrefs.edit().putString(PREF_DEFAULT_TEXT_ENCODING,
308 "GBK").apply();
309 }
310 }
311
Ben Murdochaaa1f372011-07-25 15:40:58 +0100312 if (sFactoryResetUrl.indexOf("{CID}") != -1) {
313 sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
314 BrowserProvider.getClientId(mContext.getContentResolver()));
315 }
316
Ben Murdochaaa1f372011-07-25 15:40:58 +0100317 synchronized (BrowserSettings.class) {
318 sInitialized = true;
319 BrowserSettings.class.notifyAll();
John Reck78a6a1d2011-07-21 13:54:03 -0700320 }
321 }
322 };
323
Ben Murdochaaa1f372011-07-25 15:40:58 +0100324 private static void requireInitialization() {
325 synchronized (BrowserSettings.class) {
326 while (!sInitialized) {
John Reck78a6a1d2011-07-21 13:54:03 -0700327 try {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100328 BrowserSettings.class.wait();
John Reck78a6a1d2011-07-21 13:54:03 -0700329 } catch (InterruptedException e) {
330 }
331 }
332 }
333 }
334
The Android Open Source Project0c908882009-03-03 19:32:16 -0800335 /**
John Reck35e9dd62011-04-25 09:01:54 -0700336 * Syncs all the settings that have a Preference UI
The Android Open Source Project0c908882009-03-03 19:32:16 -0800337 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800338 private void syncSetting(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700339 settings.setGeolocationEnabled(enableGeolocation());
340 settings.setJavaScriptEnabled(enableJavascript());
341 settings.setLightTouchEnabled(enableLightTouch());
342 settings.setNavDump(enableNavDump());
John Reck35e9dd62011-04-25 09:01:54 -0700343 settings.setDefaultTextEncodingName(getDefaultTextEncoding());
344 settings.setDefaultZoom(getDefaultZoom());
345 settings.setMinimumFontSize(getMinimumFontSize());
346 settings.setMinimumLogicalFontSize(getMinimumFontSize());
347 settings.setPluginState(getPluginState());
John Reck7dc444b2011-06-16 17:44:29 -0700348 settings.setTextZoom(getTextZoom());
John Reck35e9dd62011-04-25 09:01:54 -0700349 settings.setLayoutAlgorithm(getLayoutAlgorithm());
Steve Blockdc657fa2011-08-03 19:27:13 +0100350 settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows());
John Reck35e9dd62011-04-25 09:01:54 -0700351 settings.setLoadsImagesAutomatically(loadImages());
352 settings.setLoadWithOverviewMode(loadPageInOverviewMode());
353 settings.setSavePassword(rememberPasswords());
354 settings.setSaveFormData(saveFormdata());
355 settings.setUseWideViewPort(isWideViewport());
John Reckb8b2af82011-05-20 15:58:33 -0700356
qqzhoue6ff8b42013-07-23 17:28:48 +0800357 // add for carrier useragent feature
358 String ua = null;
359 try {
360 Class c = Class.forName("com.qrd.useragent.UserAgentHandler");
361 Object cObj = c.newInstance();
362 Method m = c.getDeclaredMethod("getUAString", Context.class);
363 ua = (String)m.invoke(cObj, mContext);
364 } catch (Exception e) {
365 Log.e(TAG, "plug in Load failed, err " + e);
366 ua = mCustomUserAgents.get(settings);
367 }
368
John Reck46500332011-06-07 14:36:10 -0700369 if (ua != null) {
John Reckb8b2af82011-05-20 15:58:33 -0700370 settings.setUserAgentString(ua);
371 } else {
372 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
373 }
John Reck2fd9d0e2011-07-15 11:13:48 -0700374
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800375 if (!(settings instanceof WebSettingsClassic)) return;
376
377 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
378 settingsClassic.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated());
379 settingsClassic.setShowVisualIndicator(enableVisualIndicator());
380 settingsClassic.setForceUserScalable(forceEnableUserScalable());
381 settingsClassic.setDoubleTapZoom(getDoubleTapZoom());
382 settingsClassic.setAutoFillEnabled(isAutofillEnabled());
383 settingsClassic.setAutoFillProfile(getAutoFillProfile());
384
John Reckea17e782011-10-27 17:15:59 -0700385 boolean useInverted = useInvertedRendering();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800386 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreen,
John Reckea17e782011-10-27 17:15:59 -0700387 useInverted ? "true" : "false");
388 if (useInverted) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800389 settingsClassic.setProperty(WebViewProperties.gfxInvertedScreenContrast,
John Reckea17e782011-10-27 17:15:59 -0700390 Float.toString(getInvertedContrast()));
391 }
Nicolas Roard5d513102011-08-03 15:35:34 -0700392
John Reckea17e782011-10-27 17:15:59 -0700393 if (isDebugEnabled()) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800394 settingsClassic.setProperty(WebViewProperties.gfxEnableCpuUploadPath,
John Reckea17e782011-10-27 17:15:59 -0700395 enableCpuUploadPath() ? "true" : "false");
396 }
Victoria Lease96497832012-03-23 14:19:56 -0700397
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800398 settingsClassic.setLinkPrefetchEnabled(mLinkPrefetchAllowed);
Ben Murdochef671652010-11-25 17:17:58 +0000399 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800400
John Reck35e9dd62011-04-25 09:01:54 -0700401 /**
402 * Syncs all the settings that have no UI
403 * These cannot change, so we only need to set them once per WebSettings
404 */
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800405 private void syncStaticSettings(WebSettings settings) {
John Reck35e9dd62011-04-25 09:01:54 -0700406 settings.setDefaultFontSize(16);
407 settings.setDefaultFixedFontSize(13);
Ben Murdochef671652010-11-25 17:17:58 +0000408
John Reck35e9dd62011-04-25 09:01:54 -0700409 // WebView inside Browser doesn't want initial focus to be set.
410 settings.setNeedInitialFocus(false);
411 // Browser supports multiple windows
412 settings.setSupportMultipleWindows(true);
413 // enable smooth transition for better performance during panning or
414 // zooming
415 settings.setEnableSmoothTransition(true);
416 // disable content url access
417 settings.setAllowContentAccess(false);
418
419 // HTML5 API flags
420 settings.setAppCacheEnabled(true);
421 settings.setDatabaseEnabled(true);
422 settings.setDomStorageEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700423
424 // HTML5 configuration parametersettings.
John Reck78a6a1d2011-07-21 13:54:03 -0700425 settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize());
John Reck35e9dd62011-04-25 09:01:54 -0700426 settings.setAppCachePath(getAppCachePath());
427 settings.setDatabasePath(mContext.getDir("databases", 0).getPath());
428 settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath());
Selim Gurun37bf0442012-03-29 18:27:04 -0700429 // origin policy for file access
430 settings.setAllowUniversalAccessFromFileURLs(false);
431 settings.setAllowFileAccessFromFileURLs(false);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800432
433 if (!(settings instanceof WebSettingsClassic)) return;
434
435 WebSettingsClassic settingsClassic = (WebSettingsClassic) settings;
436 settingsClassic.setPageCacheCapacity(getPageCacheCapacity());
437 // WebView should be preserving the memory as much as possible.
438 // However, apps like browser wish to turn on the performance mode which
439 // would require more memory.
440 // TODO: We need to dynamically allocate/deallocate temporary memory for
441 // apps which are trying to use minimal memory. Currently, double
442 // buffering is always turned on, which is unnecessary.
443 settingsClassic.setProperty(WebViewProperties.gfxUseMinimalMemory, "false");
444 settingsClassic.setWorkersEnabled(true); // This only affects V8.
John Reck35e9dd62011-04-25 09:01:54 -0700445 }
446
447 private void syncSharedSettings() {
John Reckc477e712011-08-17 11:27:15 -0700448 mNeedsSharedSync = false;
John Reck35e9dd62011-04-25 09:01:54 -0700449 CookieManager.getInstance().setAcceptCookie(acceptCookies());
450 if (mController != null) {
451 mController.setShouldShowErrorConsole(enableJavascriptConsole());
Shimeng (Simon) Wange83e9062010-03-29 16:13:09 -0700452 }
John Reck35e9dd62011-04-25 09:01:54 -0700453 }
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700454
John Reck35e9dd62011-04-25 09:01:54 -0700455 private void syncManagedSettings() {
456 syncSharedSettings();
457 synchronized (mManagedSettings) {
458 Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator();
459 while (iter.hasNext()) {
460 WeakReference<WebSettings> ref = iter.next();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800461 WebSettings settings = ref.get();
John Reck35e9dd62011-04-25 09:01:54 -0700462 if (settings == null) {
463 iter.remove();
464 continue;
465 }
466 syncSetting(settings);
Ben Murdochef671652010-11-25 17:17:58 +0000467 }
John Reck35e9dd62011-04-25 09:01:54 -0700468 }
469 }
Ben Murdochef671652010-11-25 17:17:58 +0000470
John Reck35e9dd62011-04-25 09:01:54 -0700471 @Override
472 public void onSharedPreferenceChanged(
473 SharedPreferences sharedPreferences, String key) {
474 syncManagedSettings();
475 if (PREF_SEARCH_ENGINE.equals(key)) {
476 updateSearchEngine(false);
Victoria Lease96497832012-03-23 14:19:56 -0700477 } else if (PREF_FULLSCREEN.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200478 if (mController != null && mController.getUi() != null) {
Michael Kolbc38c6042011-04-27 10:46:06 -0700479 mController.getUi().setFullscreen(useFullscreen());
480 }
Michael Kolb0241e752011-07-07 14:58:50 -0700481 } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) {
Magnus Hallqvist47ed4b82012-08-31 13:30:39 +0200482 if (mController != null && mController.getUi() != null) {
Michael Kolb0241e752011-07-07 14:58:50 -0700483 mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false));
484 }
Victoria Lease96497832012-03-23 14:19:56 -0700485 } else if (PREF_LINK_PREFETCH.equals(key)) {
486 updateConnectionType();
Michael Kolbc38c6042011-04-27 10:46:06 -0700487 }
John Reck35e9dd62011-04-25 09:01:54 -0700488 }
489
John Reck961d35d2011-06-23 09:45:54 -0700490 public static String getFactoryResetHomeUrl(Context context) {
Ben Murdochaaa1f372011-07-25 15:40:58 +0100491 requireInitialization();
492 return sFactoryResetUrl;
John Reck35e9dd62011-04-25 09:01:54 -0700493 }
494
495 public LayoutAlgorithm getLayoutAlgorithm() {
496 LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL;
497 if (autofitPages()) {
498 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
499 }
500 if (isDebugEnabled()) {
501 if (isSmallScreen()) {
502 layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN;
Ben Murdochef671652010-11-25 17:17:58 +0000503 } else {
John Reck35e9dd62011-04-25 09:01:54 -0700504 if (isNormalLayout()) {
505 layoutAlgorithm = LayoutAlgorithm.NORMAL;
506 } else {
507 layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
508 }
Ben Murdochef671652010-11-25 17:17:58 +0000509 }
John Reck35e9dd62011-04-25 09:01:54 -0700510 }
511 return layoutAlgorithm;
512 }
Ben Murdochef671652010-11-25 17:17:58 +0000513
John Reck35e9dd62011-04-25 09:01:54 -0700514 public int getPageCacheCapacity() {
John Reck78a6a1d2011-07-21 13:54:03 -0700515 requireInitialization();
516 return mPageCacheCapacity;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800517 }
518
John Reck35e9dd62011-04-25 09:01:54 -0700519 public WebStorageSizeManager getWebStorageSizeManager() {
John Reck78a6a1d2011-07-21 13:54:03 -0700520 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700521 return mWebStorageSizeManager;
522 }
523
John Reck35e9dd62011-04-25 09:01:54 -0700524 private String getAppCachePath() {
John Reck78a6a1d2011-07-21 13:54:03 -0700525 if (mAppCachePath == null) {
526 mAppCachePath = mContext.getDir("appcache", 0).getPath();
527 }
528 return mAppCachePath;
John Reck35e9dd62011-04-25 09:01:54 -0700529 }
530
531 private void updateSearchEngine(boolean force) {
532 String searchEngineName = getSearchEngineName();
533 if (force || mSearchEngine == null ||
534 !mSearchEngine.getName().equals(searchEngineName)) {
John Reck35e9dd62011-04-25 09:01:54 -0700535 mSearchEngine = SearchEngines.get(mContext, searchEngineName);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000536 }
537 }
538
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100539 public SearchEngine getSearchEngine() {
John Reck35e9dd62011-04-25 09:01:54 -0700540 if (mSearchEngine == null) {
541 updateSearchEngine(false);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100542 }
John Reck35e9dd62011-04-25 09:01:54 -0700543 return mSearchEngine;
Ben Murdoch0cb81892010-10-08 12:41:33 +0100544 }
545
John Reck35e9dd62011-04-25 09:01:54 -0700546 public boolean isDebugEnabled() {
John Reckc477e712011-08-17 11:27:15 -0700547 requireInitialization();
John Reck35e9dd62011-04-25 09:01:54 -0700548 return mPrefs.getBoolean(PREF_DEBUG_MENU, false);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100549 }
550
John Reck35e9dd62011-04-25 09:01:54 -0700551 public void setDebugEnabled(boolean value) {
John Reck1da81882011-10-04 17:21:10 -0700552 Editor edit = mPrefs.edit();
553 edit.putBoolean(PREF_DEBUG_MENU, value);
554 if (!value) {
555 // Reset to "safe" value
556 edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false);
557 }
558 edit.apply();
Ben Murdoch6fa32ba2010-10-20 14:01:25 +0100559 }
560
John Reck35e9dd62011-04-25 09:01:54 -0700561 public void clearCache() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800562 WebIconDatabase.getInstance().removeAllIcons();
Michael Kolb8233fac2010-10-26 16:08:53 -0700563 if (mController != null) {
564 WebView current = mController.getCurrentWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800565 if (current != null) {
566 current.clearCache(true);
567 }
568 }
569 }
570
John Reck35e9dd62011-04-25 09:01:54 -0700571 public void clearCookies() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800572 CookieManager.getInstance().removeAllCookie();
573 }
574
John Reck35e9dd62011-04-25 09:01:54 -0700575 public void clearHistory() {
576 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800577 Browser.clearHistory(resolver);
578 Browser.clearSearches(resolver);
579 }
580
John Reck35e9dd62011-04-25 09:01:54 -0700581 public void clearFormData() {
582 WebViewDatabase.getInstance(mContext).clearFormData();
Michael Kolb8233fac2010-10-26 16:08:53 -0700583 if (mController!= null) {
584 WebView currentTopView = mController.getCurrentTopWebView();
Henrik Baard794dc722010-02-19 16:28:06 +0100585 if (currentTopView != null) {
586 currentTopView.clearFormData();
587 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800588 }
589 }
590
John Reck35e9dd62011-04-25 09:01:54 -0700591 public void clearPasswords() {
592 WebViewDatabase db = WebViewDatabase.getInstance(mContext);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800593 db.clearUsernamePassword();
594 db.clearHttpAuthUsernamePassword();
595 }
596
John Reck35e9dd62011-04-25 09:01:54 -0700597 public void clearDatabases() {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100598 WebStorage.getInstance().deleteAllData();
Steve Blockf344d032009-07-30 10:50:45 +0100599 }
600
John Reck35e9dd62011-04-25 09:01:54 -0700601 public void clearLocationAccess() {
Steve Blockf344d032009-07-30 10:50:45 +0100602 GeolocationPermissions.getInstance().clearAll();
Nicolas Roard78a98e42009-05-11 13:34:17 +0100603 }
604
John Reck35e9dd62011-04-25 09:01:54 -0700605 public void resetDefaultPreferences() {
John Reckbd315192011-07-29 10:05:47 -0700606 // Preserve autologin setting
607 long gal = mPrefs.getLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, -1);
608 mPrefs.edit()
609 .clear()
610 .putLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, gal)
611 .apply();
Björn Isakssonc885a242012-06-05 17:19:04 +0200612 resetCachedValues();
John Reck35e9dd62011-04-25 09:01:54 -0700613 syncManagedSettings();
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700614 }
615
Björn Isakssonc885a242012-06-05 17:19:04 +0200616 private void resetCachedValues() {
617 updateSearchEngine(false);
618 }
619
John Reck35e9dd62011-04-25 09:01:54 -0700620 public AutoFillProfile getAutoFillProfile() {
John Reck35e9dd62011-04-25 09:01:54 -0700621 return mAutofillHandler.getAutoFillProfile();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800622 }
623
John Reck35e9dd62011-04-25 09:01:54 -0700624 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
John Reck35e9dd62011-04-25 09:01:54 -0700625 mAutofillHandler.setAutoFillProfile(profile, msg);
Ben Murdoch273330a2011-07-08 18:37:22 +0100626 // Auto-fill will reuse the same profile ID when making edits to the profile,
627 // so we need to force a settings sync (otherwise the SharedPreferences
628 // manager will optimise out the call to onSharedPreferenceChanged(), as
629 // it thinks nothing has changed).
630 syncManagedSettings();
Shimeng (Simon) Wangf7392712010-03-10 16:44:31 -0800631 }
632
John Reck35e9dd62011-04-25 09:01:54 -0700633 public void toggleDebugSettings() {
634 setDebugEnabled(!isDebugEnabled());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800635 }
Ben Murdoch0cb81892010-10-08 12:41:33 +0100636
John Reckb8b2af82011-05-20 15:58:33 -0700637 public boolean hasDesktopUseragent(WebView view) {
638 return view != null && mCustomUserAgents.get(view.getSettings()) != null;
639 }
640
641 public void toggleDesktopUseragent(WebView view) {
John Reckb0a86db2011-05-24 14:05:58 -0700642 if (view == null) {
643 return;
644 }
John Reckb8b2af82011-05-20 15:58:33 -0700645 WebSettings settings = view.getSettings();
646 if (mCustomUserAgents.get(settings) != null) {
647 mCustomUserAgents.remove(settings);
648 settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
649 } else {
650 mCustomUserAgents.put(settings, DESKTOP_USERAGENT);
651 settings.setUserAgentString(DESKTOP_USERAGENT);
652 }
653 }
654
John Reck7dc444b2011-06-16 17:44:29 -0700655 public static int getAdjustedMinimumFontSize(int rawValue) {
656 rawValue++; // Preference starts at 0, min font at 1
657 if (rawValue > 1) {
658 rawValue += (MIN_FONT_SIZE_OFFSET - 2);
659 }
660 return rawValue;
661 }
662
John Reck5ba3c762011-09-14 15:00:15 -0700663 public int getAdjustedTextZoom(int rawValue) {
John Reck7dc444b2011-06-16 17:44:29 -0700664 rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
John Reck5ba3c762011-09-14 15:00:15 -0700665 return (int) ((rawValue + 100) * mFontSizeMult);
John Reck7dc444b2011-06-16 17:44:29 -0700666 }
667
668 static int getRawTextZoom(int percent) {
669 return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
670 }
671
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700672 public int getAdjustedDoubleTapZoom(int rawValue) {
673 rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP;
674 return (int) ((rawValue + 100) * mFontSizeMult);
675 }
676
677 static int getRawDoubleTapZoom(int percent) {
678 return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL;
679 }
680
John Reckcadae722011-07-25 11:36:17 -0700681 public SharedPreferences getPreferences() {
682 return mPrefs;
683 }
684
Victoria Lease96497832012-03-23 14:19:56 -0700685 // update connectivity-dependent options
686 public void updateConnectionType() {
687 ConnectivityManager cm = (ConnectivityManager)
688 mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
689 String linkPrefetchPreference = getLinkPrefetchEnabled();
690 boolean linkPrefetchAllowed = linkPrefetchPreference.
691 equals(getLinkPrefetchAlwaysPreferenceString(mContext));
692 NetworkInfo ni = cm.getActiveNetworkInfo();
693 if (ni != null) {
694 switch (ni.getType()) {
695 case ConnectivityManager.TYPE_WIFI:
696 case ConnectivityManager.TYPE_ETHERNET:
697 case ConnectivityManager.TYPE_BLUETOOTH:
698 linkPrefetchAllowed |= linkPrefetchPreference.
699 equals(getLinkPrefetchOnWifiOnlyPreferenceString(mContext));
700 break;
701 case ConnectivityManager.TYPE_MOBILE:
702 case ConnectivityManager.TYPE_MOBILE_DUN:
703 case ConnectivityManager.TYPE_MOBILE_MMS:
704 case ConnectivityManager.TYPE_MOBILE_SUPL:
705 case ConnectivityManager.TYPE_WIMAX:
706 default:
707 break;
708 }
709 }
710 if (mLinkPrefetchAllowed != linkPrefetchAllowed) {
711 mLinkPrefetchAllowed = linkPrefetchAllowed;
712 syncManagedSettings();
713 }
714 }
715
luxiaol62677b02013-07-22 07:54:49 +0800716 public String getDownloadPath() {
717 return mPrefs.getString(PREF_DOWNLOAD_PATH,
718 DownloadHandler.getDefaultDownloadPath(mContext));
719 }
720
John Reck35e9dd62011-04-25 09:01:54 -0700721 // -----------------------------
722 // getter/setters for accessibility_preferences.xml
723 // -----------------------------
Ben Murdoch0cb81892010-10-08 12:41:33 +0100724
John Reck7dc444b2011-06-16 17:44:29 -0700725 @Deprecated
726 private TextSize getTextSize() {
John Reck35e9dd62011-04-25 09:01:54 -0700727 String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
728 return TextSize.valueOf(textSize);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100729 }
730
John Reck35e9dd62011-04-25 09:01:54 -0700731 public int getMinimumFontSize() {
John Reck8fc22a12011-06-16 14:57:41 -0700732 int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
John Reck7dc444b2011-06-16 17:44:29 -0700733 return getAdjustedMinimumFontSize(minFont);
Ben Murdoch0cb81892010-10-08 12:41:33 +0100734 }
735
John Reck92f25f82011-04-26 16:57:10 -0700736 public boolean forceEnableUserScalable() {
737 return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
738 }
739
John Reck7dc444b2011-06-16 17:44:29 -0700740 public int getTextZoom() {
John Reck78a6a1d2011-07-21 13:54:03 -0700741 requireInitialization();
John Reck7dc444b2011-06-16 17:44:29 -0700742 int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
743 return getAdjustedTextZoom(textZoom);
744 }
745
746 public void setTextZoom(int percent) {
747 mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
748 }
749
Mangesh Ghiware67f45c22011-10-12 14:43:32 -0700750 public int getDoubleTapZoom() {
751 requireInitialization();
752 int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5);
753 return getAdjustedDoubleTapZoom(doubleTapZoom);
754 }
755
756 public void setDoubleTapZoom(int percent) {
757 mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply();
758 }
759
John Reck35e9dd62011-04-25 09:01:54 -0700760 // -----------------------------
761 // getter/setters for advanced_preferences.xml
762 // -----------------------------
Ben Murdoch23da30e2010-10-26 15:18:44 +0100763
John Reck35e9dd62011-04-25 09:01:54 -0700764 public String getSearchEngineName() {
luxiaol221b3932013-07-19 15:27:57 +0800765 String defaultSearchEngineValue = mContext.getString(R.string.default_search_engine_value);
766 if (defaultSearchEngineValue == null) {
767 defaultSearchEngineValue = SearchEngine.GOOGLE;
768 }
769 return mPrefs.getString(PREF_SEARCH_ENGINE, defaultSearchEngineValue);
Ben Murdoch23da30e2010-10-26 15:18:44 +0100770 }
John Reck63bb6872010-12-01 19:29:32 -0800771
Michael Kolb8d772b02012-01-19 13:56:00 -0800772 public boolean allowAppTabs() {
773 return mPrefs.getBoolean(PREF_ALLOW_APP_TABS, false);
774 }
775
John Reck35e9dd62011-04-25 09:01:54 -0700776 public boolean openInBackground() {
777 return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false);
John Reck63bb6872010-12-01 19:29:32 -0800778 }
John Reck35e9dd62011-04-25 09:01:54 -0700779
780 public boolean enableJavascript() {
781 return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true);
782 }
783
784 // TODO: Cache
785 public PluginState getPluginState() {
786 String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON");
787 return PluginState.valueOf(state);
788 }
789
790 // TODO: Cache
791 public ZoomDensity getDefaultZoom() {
792 String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM");
793 return ZoomDensity.valueOf(zoom);
794 }
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()) {
895 return true;
896 }
897 return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true);
898 }
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
967 public boolean acceptCookies() {
968 return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true);
969 }
970
971 public boolean saveFormdata() {
972 return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true);
973 }
974
975 public boolean enableGeolocation() {
976 return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true);
977 }
978
979 public boolean rememberPasswords() {
980 return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true);
981 }
982
Michael Kolb14612442011-06-24 13:06:29 -0700983 // -----------------------------
984 // getter/setters for bandwidth_preferences.xml
985 // -----------------------------
986
Mathew Inwood467813f2011-09-02 15:43:17 +0100987 public static String getPreloadOnWifiOnlyPreferenceString(Context context) {
988 return context.getResources().getString(R.string.pref_data_preload_value_wifi_only);
Michael Kolb14612442011-06-24 13:06:29 -0700989 }
Mathew Inwood467813f2011-09-02 15:43:17 +0100990
991 public static String getPreloadAlwaysPreferenceString(Context context) {
992 return context.getResources().getString(R.string.pref_data_preload_value_always);
993 }
994
Mathew Inwood825fba72011-10-04 14:52:52 +0100995 private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY =
996 "browser_default_preload_setting";
997
998 public String getDefaultPreloadSetting() {
999 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1000 DEAULT_PRELOAD_SECURE_SETTING_KEY);
1001 if (preload == null) {
1002 preload = mContext.getResources().getString(R.string.pref_data_preload_default_value);
1003 }
1004 return preload;
Mathew Inwood467813f2011-09-02 15:43:17 +01001005 }
1006
1007 public String getPreloadEnabled() {
1008 return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting());
1009 }
1010
Victoria Lease96497832012-03-23 14:19:56 -07001011 public static String getLinkPrefetchOnWifiOnlyPreferenceString(Context context) {
1012 return context.getResources().getString(R.string.pref_link_prefetch_value_wifi_only);
1013 }
1014
1015 public static String getLinkPrefetchAlwaysPreferenceString(Context context) {
1016 return context.getResources().getString(R.string.pref_link_prefetch_value_always);
1017 }
1018
1019 private static final String DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY =
1020 "browser_default_link_prefetch_setting";
1021
1022 public String getDefaultLinkPrefetchSetting() {
1023 String preload = Settings.Secure.getString(mContext.getContentResolver(),
1024 DEFAULT_LINK_PREFETCH_SECURE_SETTING_KEY);
1025 if (preload == null) {
1026 preload = mContext.getResources().getString(R.string.pref_link_prefetch_default_value);
1027 }
1028 return preload;
1029 }
1030
1031 public String getLinkPrefetchEnabled() {
1032 return mPrefs.getString(PREF_LINK_PREFETCH, getDefaultLinkPrefetchSetting());
1033 }
1034
George Mount3636d0a2011-11-21 09:08:21 -08001035 // -----------------------------
1036 // getter/setters for browser recovery
1037 // -----------------------------
1038 /**
1039 * The last time browser was started.
1040 * @return The last browser start time as System.currentTimeMillis. This
1041 * can be 0 if this is the first time or the last tab was closed.
1042 */
1043 public long getLastRecovered() {
1044 return mPrefs.getLong(KEY_LAST_RECOVERED, 0);
1045 }
1046
1047 /**
1048 * Sets the last browser start time.
1049 * @param time The last time as System.currentTimeMillis that the browser
1050 * was started. This should be set to 0 if the last tab is closed.
1051 */
1052 public void setLastRecovered(long time) {
1053 mPrefs.edit()
1054 .putLong(KEY_LAST_RECOVERED, time)
1055 .apply();
1056 }
1057
1058 /**
1059 * Used to determine whether or not the previous browser run crashed. Once
1060 * the previous state has been determined, the value will be set to false
1061 * until a pause is received.
1062 * @return true if the last browser run was paused or false if it crashed.
1063 */
1064 public boolean wasLastRunPaused() {
1065 return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false);
1066 }
1067
1068 /**
1069 * Sets whether or not the last run was a pause or crash.
1070 * @param isPaused Set to true When a pause is received or false after
1071 * resuming.
1072 */
1073 public void setLastRunPaused(boolean isPaused) {
1074 mPrefs.edit()
1075 .putBoolean(KEY_LAST_RUN_PAUSED, isPaused)
1076 .apply();
1077 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001078}