The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 2 | * Copyright (C) 2011 The Android Open Source Project |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
Grace Kloba | 9804c43 | 2009-12-02 11:07:40 -0800 | [diff] [blame] | 19 | import android.app.ActivityManager; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 20 | import android.content.ContentResolver; |
| 21 | import android.content.Context; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 22 | import android.content.SharedPreferences; |
John Reck | 1da8188 | 2011-10-04 17:21:10 -0700 | [diff] [blame] | 23 | import android.content.SharedPreferences.Editor; |
John Reck | 812d2d6 | 2011-01-18 14:16:15 -0800 | [diff] [blame] | 24 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
John Reck | f48314f | 2011-04-27 17:52:17 -0700 | [diff] [blame] | 25 | import android.os.Build; |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 26 | import android.os.Message; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 27 | import android.preference.PreferenceManager; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 28 | import android.provider.Browser; |
Mathew Inwood | 825fba7 | 2011-10-04 14:52:52 +0100 | [diff] [blame] | 29 | import android.provider.Settings; |
John Reck | 5ba3c76 | 2011-09-14 15:00:15 -0700 | [diff] [blame] | 30 | import android.util.DisplayMetrics; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 31 | import android.webkit.CookieManager; |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 32 | import android.webkit.GeolocationPermissions; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 33 | import android.webkit.WebIconDatabase; |
| 34 | import android.webkit.WebSettings; |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 35 | import android.webkit.WebSettings.LayoutAlgorithm; |
| 36 | import android.webkit.WebSettings.PluginState; |
| 37 | import android.webkit.WebSettings.TextSize; |
| 38 | import android.webkit.WebSettings.ZoomDensity; |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 39 | import android.webkit.WebSettingsClassic; |
| 40 | import android.webkit.WebSettingsClassic.AutoFillProfile; |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 41 | import android.webkit.WebStorage; |
John Reck | 812d2d6 | 2011-01-18 14:16:15 -0800 | [diff] [blame] | 42 | import android.webkit.WebView; |
| 43 | import android.webkit.WebViewDatabase; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 44 | |
John Reck | 4650033 | 2011-06-07 14:36:10 -0700 | [diff] [blame] | 45 | import com.android.browser.homepages.HomeProvider; |
| 46 | import com.android.browser.provider.BrowserProvider; |
| 47 | import com.android.browser.search.SearchEngine; |
| 48 | import com.android.browser.search.SearchEngines; |
| 49 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 50 | import java.lang.ref.WeakReference; |
| 51 | import java.util.Iterator; |
| 52 | import java.util.LinkedList; |
John Reck | b8b2af8 | 2011-05-20 15:58:33 -0700 | [diff] [blame] | 53 | import java.util.WeakHashMap; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 54 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Class for managing settings |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 57 | */ |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 58 | public class BrowserSettings implements OnSharedPreferenceChangeListener, |
| 59 | PreferenceKeys { |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 60 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 61 | // TODO: Do something with this UserAgent stuff |
John Reck | 7fd1e8f | 2011-04-27 18:22:57 -0700 | [diff] [blame] | 62 | private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " + |
| 63 | "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " + |
| 64 | "Chrome/11.0.696.34 Safari/534.24"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 65 | |
| 66 | private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " + |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 67 | "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " + |
| 68 | "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7"; |
Bart Sears | f6915fb | 2010-07-08 19:58:22 -0700 | [diff] [blame] | 69 | |
| 70 | private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " + |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 71 | "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " + |
| 72 | "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10"; |
Bart Sears | f6915fb | 2010-07-08 19:58:22 -0700 | [diff] [blame] | 73 | |
| 74 | private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " + |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 75 | "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " + |
| 76 | "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 77 | |
John Reck | 7fd1e8f | 2011-04-27 18:22:57 -0700 | [diff] [blame] | 78 | private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " + |
| 79 | "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " + |
| 80 | "(KHTML, like Gecko) Version/4.0 Safari/534.13"; |
| 81 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 82 | private static final String USER_AGENTS[] = { null, |
| 83 | DESKTOP_USERAGENT, |
| 84 | IPHONE_USERAGENT, |
| 85 | IPAD_USERAGENT, |
John Reck | 7fd1e8f | 2011-04-27 18:22:57 -0700 | [diff] [blame] | 86 | FROYO_USERAGENT, |
| 87 | HONEYCOMB_USERAGENT, |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 88 | }; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 89 | |
John Reck | 8fc22a1 | 2011-06-16 14:57:41 -0700 | [diff] [blame] | 90 | // The minimum min font size |
| 91 | // Aka, the lower bounds for the min font size range |
| 92 | // which is 1:5..24 |
| 93 | private static final int MIN_FONT_SIZE_OFFSET = 5; |
John Reck | 7dc444b | 2011-06-16 17:44:29 -0700 | [diff] [blame] | 94 | // The initial value in the text zoom range |
| 95 | // This is what represents 100% in the SeekBarPreference range |
| 96 | private static final int TEXT_ZOOM_START_VAL = 10; |
| 97 | // The size of a single step in the text zoom range, in percent |
| 98 | private static final int TEXT_ZOOM_STEP = 5; |
Mangesh Ghiware | 67f45c2 | 2011-10-12 14:43:32 -0700 | [diff] [blame] | 99 | // The initial value in the double tap zoom range |
| 100 | // This is what represents 100% in the SeekBarPreference range |
| 101 | private static final int DOUBLE_TAP_ZOOM_START_VAL = 5; |
| 102 | // The size of a single step in the double tap zoom range, in percent |
| 103 | private static final int DOUBLE_TAP_ZOOM_STEP = 5; |
John Reck | 8fc22a1 | 2011-06-16 14:57:41 -0700 | [diff] [blame] | 104 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 105 | private static BrowserSettings sInstance; |
Jeff Davidson | 4361029 | 2010-07-16 16:03:58 -0700 | [diff] [blame] | 106 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 107 | private Context mContext; |
| 108 | private SharedPreferences mPrefs; |
| 109 | private LinkedList<WeakReference<WebSettings>> mManagedSettings; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 110 | private Controller mController; |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 111 | private WebStorageSizeManager mWebStorageSizeManager; |
| 112 | private AutofillHandler mAutofillHandler; |
John Reck | b8b2af8 | 2011-05-20 15:58:33 -0700 | [diff] [blame] | 113 | private WeakHashMap<WebSettings, String> mCustomUserAgents; |
Ben Murdoch | aaa1f37 | 2011-07-25 15:40:58 +0100 | [diff] [blame] | 114 | private static boolean sInitialized = false; |
John Reck | c477e71 | 2011-08-17 11:27:15 -0700 | [diff] [blame] | 115 | private boolean mNeedsSharedSync = true; |
John Reck | 5ba3c76 | 2011-09-14 15:00:15 -0700 | [diff] [blame] | 116 | private float mFontSizeMult = 1.0f; |
John Reck | 78a6a1d | 2011-07-21 13:54:03 -0700 | [diff] [blame] | 117 | |
| 118 | // Cached values |
| 119 | private int mPageCacheCapacity = 1; |
| 120 | private String mAppCachePath; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 121 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 122 | // Cached settings |
| 123 | private SearchEngine mSearchEngine; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 124 | |
Ben Murdoch | aaa1f37 | 2011-07-25 15:40:58 +0100 | [diff] [blame] | 125 | private static String sFactoryResetUrl; |
| 126 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 127 | public static void initialize(final Context context) { |
| 128 | sInstance = new BrowserSettings(context); |
| 129 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 130 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 131 | public static BrowserSettings getInstance() { |
| 132 | return sInstance; |
| 133 | } |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 134 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 135 | private BrowserSettings(Context context) { |
Ben Murdoch | 914c559 | 2011-08-01 13:58:47 +0100 | [diff] [blame] | 136 | mContext = context.getApplicationContext(); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 137 | mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext); |
| 138 | mAutofillHandler = new AutofillHandler(mContext); |
| 139 | mManagedSettings = new LinkedList<WeakReference<WebSettings>>(); |
John Reck | b8b2af8 | 2011-05-20 15:58:33 -0700 | [diff] [blame] | 140 | mCustomUserAgents = new WeakHashMap<WebSettings, String>(); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 141 | mAutofillHandler.asyncLoadFromDb(); |
John Reck | cadae72 | 2011-07-25 11:36:17 -0700 | [diff] [blame] | 142 | BackgroundHandler.execute(mSetup); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | public void setController(Controller controller) { |
| 146 | mController = controller; |
John Reck | c477e71 | 2011-08-17 11:27:15 -0700 | [diff] [blame] | 147 | if (sInitialized) { |
| 148 | syncSharedSettings(); |
| 149 | } |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 150 | } |
| 151 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 152 | public void startManagingSettings(WebSettings settings) { |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 153 | WebSettingsClassic settingsClassic = (WebSettingsClassic) settings; |
John Reck | c477e71 | 2011-08-17 11:27:15 -0700 | [diff] [blame] | 154 | if (mNeedsSharedSync) { |
| 155 | syncSharedSettings(); |
| 156 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 157 | synchronized (mManagedSettings) { |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 158 | syncStaticSettings(settingsClassic); |
| 159 | syncSetting(settingsClassic); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 160 | mManagedSettings.add(new WeakReference<WebSettings>(settings)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
John Reck | d1d8731 | 2012-03-08 13:25:00 -0800 | [diff] [blame] | 164 | public void stopManagingSettings(WebSettings settings) { |
| 165 | Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator(); |
| 166 | while (iter.hasNext()) { |
| 167 | WeakReference<WebSettings> ref = iter.next(); |
| 168 | if (ref.get() == settings) { |
| 169 | iter.remove(); |
| 170 | return; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
John Reck | cadae72 | 2011-07-25 11:36:17 -0700 | [diff] [blame] | 175 | private Runnable mSetup = new Runnable() { |
John Reck | 78a6a1d | 2011-07-21 13:54:03 -0700 | [diff] [blame] | 176 | |
| 177 | @Override |
| 178 | public void run() { |
John Reck | 5ba3c76 | 2011-09-14 15:00:15 -0700 | [diff] [blame] | 179 | DisplayMetrics metrics = mContext.getResources().getDisplayMetrics(); |
| 180 | mFontSizeMult = metrics.scaledDensity / metrics.density; |
John Reck | 78a6a1d | 2011-07-21 13:54:03 -0700 | [diff] [blame] | 181 | // the cost of one cached page is ~3M (measured using nytimes.com). For |
| 182 | // low end devices, we only cache one page. For high end devices, we try |
| 183 | // to cache more pages, currently choose 5. |
| 184 | if (ActivityManager.staticGetMemoryClass() > 16) { |
| 185 | mPageCacheCapacity = 5; |
| 186 | } |
| 187 | mWebStorageSizeManager = new WebStorageSizeManager(mContext, |
| 188 | new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()), |
| 189 | new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath())); |
John Reck | 276b135 | 2011-09-02 15:47:33 -0700 | [diff] [blame] | 190 | // Workaround b/5254577 |
| 191 | mPrefs.registerOnSharedPreferenceChangeListener(BrowserSettings.this); |
John Reck | 78a6a1d | 2011-07-21 13:54:03 -0700 | [diff] [blame] | 192 | if (Build.VERSION.CODENAME.equals("REL")) { |
| 193 | // This is a release build, always startup with debug disabled |
| 194 | setDebugEnabled(false); |
| 195 | } |
| 196 | if (mPrefs.contains(PREF_TEXT_SIZE)) { |
| 197 | /* |
| 198 | * Update from TextSize enum to zoom percent |
| 199 | * SMALLEST is 50% |
| 200 | * SMALLER is 75% |
| 201 | * NORMAL is 100% |
| 202 | * LARGER is 150% |
| 203 | * LARGEST is 200% |
| 204 | */ |
| 205 | switch (getTextSize()) { |
| 206 | case SMALLEST: |
| 207 | setTextZoom(50); |
| 208 | break; |
| 209 | case SMALLER: |
| 210 | setTextZoom(75); |
| 211 | break; |
| 212 | case LARGER: |
| 213 | setTextZoom(150); |
| 214 | break; |
| 215 | case LARGEST: |
| 216 | setTextZoom(200); |
| 217 | break; |
| 218 | } |
| 219 | mPrefs.edit().remove(PREF_TEXT_SIZE).apply(); |
| 220 | } |
Ben Murdoch | aaa1f37 | 2011-07-25 15:40:58 +0100 | [diff] [blame] | 221 | |
| 222 | sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base); |
| 223 | if (sFactoryResetUrl.indexOf("{CID}") != -1) { |
| 224 | sFactoryResetUrl = sFactoryResetUrl.replace("{CID}", |
| 225 | BrowserProvider.getClientId(mContext.getContentResolver())); |
| 226 | } |
| 227 | |
Ben Murdoch | aaa1f37 | 2011-07-25 15:40:58 +0100 | [diff] [blame] | 228 | synchronized (BrowserSettings.class) { |
| 229 | sInitialized = true; |
| 230 | BrowserSettings.class.notifyAll(); |
John Reck | 78a6a1d | 2011-07-21 13:54:03 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | }; |
| 234 | |
Ben Murdoch | aaa1f37 | 2011-07-25 15:40:58 +0100 | [diff] [blame] | 235 | private static void requireInitialization() { |
| 236 | synchronized (BrowserSettings.class) { |
| 237 | while (!sInitialized) { |
John Reck | 78a6a1d | 2011-07-21 13:54:03 -0700 | [diff] [blame] | 238 | try { |
Ben Murdoch | aaa1f37 | 2011-07-25 15:40:58 +0100 | [diff] [blame] | 239 | BrowserSettings.class.wait(); |
John Reck | 78a6a1d | 2011-07-21 13:54:03 -0700 | [diff] [blame] | 240 | } catch (InterruptedException e) { |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 246 | /** |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 247 | * Syncs all the settings that have a Preference UI |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 248 | */ |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 249 | private void syncSetting(WebSettingsClassic settings) { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 250 | settings.setGeolocationEnabled(enableGeolocation()); |
| 251 | settings.setJavaScriptEnabled(enableJavascript()); |
| 252 | settings.setLightTouchEnabled(enableLightTouch()); |
| 253 | settings.setNavDump(enableNavDump()); |
Derek Sollenberger | 31adf67 | 2011-07-08 11:31:30 -0400 | [diff] [blame] | 254 | settings.setHardwareAccelSkiaEnabled(isSkiaHardwareAccelerated()); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 255 | settings.setShowVisualIndicator(enableVisualIndicator()); |
| 256 | settings.setDefaultTextEncodingName(getDefaultTextEncoding()); |
| 257 | settings.setDefaultZoom(getDefaultZoom()); |
| 258 | settings.setMinimumFontSize(getMinimumFontSize()); |
| 259 | settings.setMinimumLogicalFontSize(getMinimumFontSize()); |
John Reck | 92f25f8 | 2011-04-26 16:57:10 -0700 | [diff] [blame] | 260 | settings.setForceUserScalable(forceEnableUserScalable()); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 261 | settings.setPluginState(getPluginState()); |
John Reck | 7dc444b | 2011-06-16 17:44:29 -0700 | [diff] [blame] | 262 | settings.setTextZoom(getTextZoom()); |
Mangesh Ghiware | 67f45c2 | 2011-10-12 14:43:32 -0700 | [diff] [blame] | 263 | settings.setDoubleTapZoom(getDoubleTapZoom()); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 264 | settings.setAutoFillEnabled(isAutofillEnabled()); |
| 265 | settings.setLayoutAlgorithm(getLayoutAlgorithm()); |
Steve Block | dc657fa | 2011-08-03 19:27:13 +0100 | [diff] [blame] | 266 | settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows()); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 267 | settings.setLoadsImagesAutomatically(loadImages()); |
| 268 | settings.setLoadWithOverviewMode(loadPageInOverviewMode()); |
| 269 | settings.setSavePassword(rememberPasswords()); |
| 270 | settings.setSaveFormData(saveFormdata()); |
| 271 | settings.setUseWideViewPort(isWideViewport()); |
| 272 | settings.setAutoFillProfile(getAutoFillProfile()); |
John Reck | b8b2af8 | 2011-05-20 15:58:33 -0700 | [diff] [blame] | 273 | |
| 274 | String ua = mCustomUserAgents.get(settings); |
John Reck | 4650033 | 2011-06-07 14:36:10 -0700 | [diff] [blame] | 275 | if (ua != null) { |
John Reck | b8b2af8 | 2011-05-20 15:58:33 -0700 | [diff] [blame] | 276 | settings.setUserAgentString(ua); |
| 277 | } else { |
| 278 | settings.setUserAgentString(USER_AGENTS[getUserAgent()]); |
| 279 | } |
John Reck | 2fd9d0e | 2011-07-15 11:13:48 -0700 | [diff] [blame] | 280 | |
John Reck | ea17e78 | 2011-10-27 17:15:59 -0700 | [diff] [blame] | 281 | boolean useInverted = useInvertedRendering(); |
John Reck | eabe5da | 2011-08-08 13:24:13 -0700 | [diff] [blame] | 282 | settings.setProperty(WebViewProperties.gfxInvertedScreen, |
John Reck | ea17e78 | 2011-10-27 17:15:59 -0700 | [diff] [blame] | 283 | useInverted ? "true" : "false"); |
| 284 | if (useInverted) { |
| 285 | settings.setProperty(WebViewProperties.gfxInvertedScreenContrast, |
| 286 | Float.toString(getInvertedContrast())); |
| 287 | } |
Nicolas Roard | 5d51310 | 2011-08-03 15:35:34 -0700 | [diff] [blame] | 288 | |
John Reck | ea17e78 | 2011-10-27 17:15:59 -0700 | [diff] [blame] | 289 | if (isDebugEnabled()) { |
| 290 | settings.setProperty(WebViewProperties.gfxEnableCpuUploadPath, |
| 291 | enableCpuUploadPath() ? "true" : "false"); |
| 292 | } |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 293 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 294 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 295 | /** |
| 296 | * Syncs all the settings that have no UI |
| 297 | * These cannot change, so we only need to set them once per WebSettings |
| 298 | */ |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 299 | private void syncStaticSettings(WebSettingsClassic settings) { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 300 | settings.setDefaultFontSize(16); |
| 301 | settings.setDefaultFixedFontSize(13); |
| 302 | settings.setPageCacheCapacity(getPageCacheCapacity()); |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 303 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 304 | // WebView inside Browser doesn't want initial focus to be set. |
| 305 | settings.setNeedInitialFocus(false); |
| 306 | // Browser supports multiple windows |
| 307 | settings.setSupportMultipleWindows(true); |
| 308 | // enable smooth transition for better performance during panning or |
| 309 | // zooming |
| 310 | settings.setEnableSmoothTransition(true); |
Teng-Hui Zhu | 164f74e | 2011-11-07 12:54:43 -0800 | [diff] [blame] | 311 | // WebView should be preserving the memory as much as possible. |
| 312 | // However, apps like browser wish to turn on the performance mode which |
| 313 | // would require more memory. |
| 314 | // TODO: We need to dynamically allocate/deallocate temporary memory for |
| 315 | // apps which are trying to use minimal memory. Currently, double |
| 316 | // buffering is always turned on, which is unnecessary. |
| 317 | settings.setProperty(WebViewProperties.gfxUseMinimalMemory, "false"); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 318 | // disable content url access |
| 319 | settings.setAllowContentAccess(false); |
| 320 | |
| 321 | // HTML5 API flags |
| 322 | settings.setAppCacheEnabled(true); |
| 323 | settings.setDatabaseEnabled(true); |
| 324 | settings.setDomStorageEnabled(true); |
| 325 | settings.setWorkersEnabled(true); // This only affects V8. |
| 326 | |
| 327 | // HTML5 configuration parametersettings. |
John Reck | 78a6a1d | 2011-07-21 13:54:03 -0700 | [diff] [blame] | 328 | settings.setAppCacheMaxSize(getWebStorageSizeManager().getAppCacheMaxSize()); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 329 | settings.setAppCachePath(getAppCachePath()); |
| 330 | settings.setDatabasePath(mContext.getDir("databases", 0).getPath()); |
| 331 | settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath()); |
Selim Gurun | 37bf044 | 2012-03-29 18:27:04 -0700 | [diff] [blame^] | 332 | // origin policy for file access |
| 333 | settings.setAllowUniversalAccessFromFileURLs(false); |
| 334 | settings.setAllowFileAccessFromFileURLs(false); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | private void syncSharedSettings() { |
John Reck | c477e71 | 2011-08-17 11:27:15 -0700 | [diff] [blame] | 338 | mNeedsSharedSync = false; |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 339 | CookieManager.getInstance().setAcceptCookie(acceptCookies()); |
| 340 | if (mController != null) { |
| 341 | mController.setShouldShowErrorConsole(enableJavascriptConsole()); |
Shimeng (Simon) Wang | e83e906 | 2010-03-29 16:13:09 -0700 | [diff] [blame] | 342 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 343 | } |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 344 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 345 | private void syncManagedSettings() { |
| 346 | syncSharedSettings(); |
| 347 | synchronized (mManagedSettings) { |
| 348 | Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator(); |
| 349 | while (iter.hasNext()) { |
| 350 | WeakReference<WebSettings> ref = iter.next(); |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 351 | WebSettingsClassic settings = (WebSettingsClassic)ref.get(); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 352 | if (settings == null) { |
| 353 | iter.remove(); |
| 354 | continue; |
| 355 | } |
| 356 | syncSetting(settings); |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 357 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 360 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 361 | @Override |
| 362 | public void onSharedPreferenceChanged( |
| 363 | SharedPreferences sharedPreferences, String key) { |
| 364 | syncManagedSettings(); |
| 365 | if (PREF_SEARCH_ENGINE.equals(key)) { |
| 366 | updateSearchEngine(false); |
| 367 | } |
Michael Kolb | c38c604 | 2011-04-27 10:46:06 -0700 | [diff] [blame] | 368 | if (PREF_FULLSCREEN.equals(key)) { |
| 369 | if (mController.getUi() != null) { |
| 370 | mController.getUi().setFullscreen(useFullscreen()); |
| 371 | } |
Michael Kolb | 0241e75 | 2011-07-07 14:58:50 -0700 | [diff] [blame] | 372 | } else if (PREF_ENABLE_QUICK_CONTROLS.equals(key)) { |
| 373 | if (mController.getUi() != null) { |
| 374 | mController.getUi().setUseQuickControls(sharedPreferences.getBoolean(key, false)); |
| 375 | } |
Michael Kolb | c38c604 | 2011-04-27 10:46:06 -0700 | [diff] [blame] | 376 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 377 | } |
| 378 | |
John Reck | 961d35d | 2011-06-23 09:45:54 -0700 | [diff] [blame] | 379 | public static String getFactoryResetHomeUrl(Context context) { |
Ben Murdoch | aaa1f37 | 2011-07-25 15:40:58 +0100 | [diff] [blame] | 380 | requireInitialization(); |
| 381 | return sFactoryResetUrl; |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | public LayoutAlgorithm getLayoutAlgorithm() { |
| 385 | LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL; |
| 386 | if (autofitPages()) { |
| 387 | layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS; |
| 388 | } |
| 389 | if (isDebugEnabled()) { |
| 390 | if (isSmallScreen()) { |
| 391 | layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN; |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 392 | } else { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 393 | if (isNormalLayout()) { |
| 394 | layoutAlgorithm = LayoutAlgorithm.NORMAL; |
| 395 | } else { |
| 396 | layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS; |
| 397 | } |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 398 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 399 | } |
| 400 | return layoutAlgorithm; |
| 401 | } |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 402 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 403 | public int getPageCacheCapacity() { |
John Reck | 78a6a1d | 2011-07-21 13:54:03 -0700 | [diff] [blame] | 404 | requireInitialization(); |
| 405 | return mPageCacheCapacity; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 406 | } |
| 407 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 408 | public WebStorageSizeManager getWebStorageSizeManager() { |
John Reck | 78a6a1d | 2011-07-21 13:54:03 -0700 | [diff] [blame] | 409 | requireInitialization(); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 410 | return mWebStorageSizeManager; |
| 411 | } |
| 412 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 413 | private String getAppCachePath() { |
John Reck | 78a6a1d | 2011-07-21 13:54:03 -0700 | [diff] [blame] | 414 | if (mAppCachePath == null) { |
| 415 | mAppCachePath = mContext.getDir("appcache", 0).getPath(); |
| 416 | } |
| 417 | return mAppCachePath; |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | private void updateSearchEngine(boolean force) { |
| 421 | String searchEngineName = getSearchEngineName(); |
| 422 | if (force || mSearchEngine == null || |
| 423 | !mSearchEngine.getName().equals(searchEngineName)) { |
| 424 | if (mSearchEngine != null) { |
| 425 | if (mSearchEngine.supportsVoiceSearch()) { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 426 | // One or more tabs could have been in voice search mode. |
| 427 | // Clear it, since the new SearchEngine may not support |
| 428 | // it, or may handle it differently. |
| 429 | for (int i = 0; i < mController.getTabControl().getTabCount(); i++) { |
| 430 | mController.getTabControl().getTab(i).revertVoiceSearchMode(); |
| 431 | } |
| 432 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 433 | mSearchEngine.close(); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 434 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 435 | mSearchEngine = SearchEngines.get(mContext, searchEngineName); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
Bjorn Bringert | d69f51d | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 439 | public SearchEngine getSearchEngine() { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 440 | if (mSearchEngine == null) { |
| 441 | updateSearchEngine(false); |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 442 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 443 | return mSearchEngine; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 444 | } |
| 445 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 446 | public boolean isDebugEnabled() { |
John Reck | c477e71 | 2011-08-17 11:27:15 -0700 | [diff] [blame] | 447 | requireInitialization(); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 448 | return mPrefs.getBoolean(PREF_DEBUG_MENU, false); |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 449 | } |
| 450 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 451 | public void setDebugEnabled(boolean value) { |
John Reck | 1da8188 | 2011-10-04 17:21:10 -0700 | [diff] [blame] | 452 | Editor edit = mPrefs.edit(); |
| 453 | edit.putBoolean(PREF_DEBUG_MENU, value); |
| 454 | if (!value) { |
| 455 | // Reset to "safe" value |
| 456 | edit.putBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false); |
| 457 | } |
| 458 | edit.apply(); |
Ben Murdoch | 6fa32ba | 2010-10-20 14:01:25 +0100 | [diff] [blame] | 459 | } |
| 460 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 461 | public void clearCache() { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 462 | WebIconDatabase.getInstance().removeAllIcons(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 463 | if (mController != null) { |
| 464 | WebView current = mController.getCurrentWebView(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 465 | if (current != null) { |
| 466 | current.clearCache(true); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 471 | public void clearCookies() { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 472 | CookieManager.getInstance().removeAllCookie(); |
| 473 | } |
| 474 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 475 | public void clearHistory() { |
| 476 | ContentResolver resolver = mContext.getContentResolver(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 477 | Browser.clearHistory(resolver); |
| 478 | Browser.clearSearches(resolver); |
| 479 | } |
| 480 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 481 | public void clearFormData() { |
| 482 | WebViewDatabase.getInstance(mContext).clearFormData(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 483 | if (mController!= null) { |
| 484 | WebView currentTopView = mController.getCurrentTopWebView(); |
Henrik Baard | 794dc72 | 2010-02-19 16:28:06 +0100 | [diff] [blame] | 485 | if (currentTopView != null) { |
| 486 | currentTopView.clearFormData(); |
| 487 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 491 | public void clearPasswords() { |
| 492 | WebViewDatabase db = WebViewDatabase.getInstance(mContext); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 493 | db.clearUsernamePassword(); |
| 494 | db.clearHttpAuthUsernamePassword(); |
| 495 | } |
| 496 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 497 | public void clearDatabases() { |
Andrei Popescu | 824faeb | 2009-07-21 18:24:06 +0100 | [diff] [blame] | 498 | WebStorage.getInstance().deleteAllData(); |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 499 | } |
| 500 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 501 | public void clearLocationAccess() { |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 502 | GeolocationPermissions.getInstance().clearAll(); |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 503 | } |
| 504 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 505 | public void resetDefaultPreferences() { |
John Reck | bd31519 | 2011-07-29 10:05:47 -0700 | [diff] [blame] | 506 | // Preserve autologin setting |
| 507 | long gal = mPrefs.getLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, -1); |
| 508 | mPrefs.edit() |
| 509 | .clear() |
| 510 | .putLong(GoogleAccountLogin.PREF_AUTOLOGIN_TIME, gal) |
| 511 | .apply(); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 512 | syncManagedSettings(); |
Grace Kloba | f2c5c1b | 2009-05-26 10:48:31 -0700 | [diff] [blame] | 513 | } |
| 514 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 515 | public AutoFillProfile getAutoFillProfile() { |
| 516 | mAutofillHandler.waitForLoad(); |
| 517 | return mAutofillHandler.getAutoFillProfile(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 518 | } |
| 519 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 520 | public void setAutoFillProfile(AutoFillProfile profile, Message msg) { |
| 521 | mAutofillHandler.waitForLoad(); |
| 522 | mAutofillHandler.setAutoFillProfile(profile, msg); |
Ben Murdoch | 273330a | 2011-07-08 18:37:22 +0100 | [diff] [blame] | 523 | // Auto-fill will reuse the same profile ID when making edits to the profile, |
| 524 | // so we need to force a settings sync (otherwise the SharedPreferences |
| 525 | // manager will optimise out the call to onSharedPreferenceChanged(), as |
| 526 | // it thinks nothing has changed). |
| 527 | syncManagedSettings(); |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 528 | } |
| 529 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 530 | public void toggleDebugSettings() { |
| 531 | setDebugEnabled(!isDebugEnabled()); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 532 | } |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 533 | |
John Reck | b8b2af8 | 2011-05-20 15:58:33 -0700 | [diff] [blame] | 534 | public boolean hasDesktopUseragent(WebView view) { |
| 535 | return view != null && mCustomUserAgents.get(view.getSettings()) != null; |
| 536 | } |
| 537 | |
| 538 | public void toggleDesktopUseragent(WebView view) { |
John Reck | b0a86db | 2011-05-24 14:05:58 -0700 | [diff] [blame] | 539 | if (view == null) { |
| 540 | return; |
| 541 | } |
John Reck | b8b2af8 | 2011-05-20 15:58:33 -0700 | [diff] [blame] | 542 | WebSettings settings = view.getSettings(); |
| 543 | if (mCustomUserAgents.get(settings) != null) { |
| 544 | mCustomUserAgents.remove(settings); |
| 545 | settings.setUserAgentString(USER_AGENTS[getUserAgent()]); |
| 546 | } else { |
| 547 | mCustomUserAgents.put(settings, DESKTOP_USERAGENT); |
| 548 | settings.setUserAgentString(DESKTOP_USERAGENT); |
| 549 | } |
| 550 | } |
| 551 | |
John Reck | 7dc444b | 2011-06-16 17:44:29 -0700 | [diff] [blame] | 552 | public static int getAdjustedMinimumFontSize(int rawValue) { |
| 553 | rawValue++; // Preference starts at 0, min font at 1 |
| 554 | if (rawValue > 1) { |
| 555 | rawValue += (MIN_FONT_SIZE_OFFSET - 2); |
| 556 | } |
| 557 | return rawValue; |
| 558 | } |
| 559 | |
John Reck | 5ba3c76 | 2011-09-14 15:00:15 -0700 | [diff] [blame] | 560 | public int getAdjustedTextZoom(int rawValue) { |
John Reck | 7dc444b | 2011-06-16 17:44:29 -0700 | [diff] [blame] | 561 | rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP; |
John Reck | 5ba3c76 | 2011-09-14 15:00:15 -0700 | [diff] [blame] | 562 | return (int) ((rawValue + 100) * mFontSizeMult); |
John Reck | 7dc444b | 2011-06-16 17:44:29 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | static int getRawTextZoom(int percent) { |
| 566 | return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL; |
| 567 | } |
| 568 | |
Mangesh Ghiware | 67f45c2 | 2011-10-12 14:43:32 -0700 | [diff] [blame] | 569 | public int getAdjustedDoubleTapZoom(int rawValue) { |
| 570 | rawValue = (rawValue - DOUBLE_TAP_ZOOM_START_VAL) * DOUBLE_TAP_ZOOM_STEP; |
| 571 | return (int) ((rawValue + 100) * mFontSizeMult); |
| 572 | } |
| 573 | |
| 574 | static int getRawDoubleTapZoom(int percent) { |
| 575 | return (percent - 100) / DOUBLE_TAP_ZOOM_STEP + DOUBLE_TAP_ZOOM_START_VAL; |
| 576 | } |
| 577 | |
John Reck | cadae72 | 2011-07-25 11:36:17 -0700 | [diff] [blame] | 578 | public SharedPreferences getPreferences() { |
| 579 | return mPrefs; |
| 580 | } |
| 581 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 582 | // ----------------------------- |
| 583 | // getter/setters for accessibility_preferences.xml |
| 584 | // ----------------------------- |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 585 | |
John Reck | 7dc444b | 2011-06-16 17:44:29 -0700 | [diff] [blame] | 586 | @Deprecated |
| 587 | private TextSize getTextSize() { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 588 | String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL"); |
| 589 | return TextSize.valueOf(textSize); |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 590 | } |
| 591 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 592 | public int getMinimumFontSize() { |
John Reck | 8fc22a1 | 2011-06-16 14:57:41 -0700 | [diff] [blame] | 593 | int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0); |
John Reck | 7dc444b | 2011-06-16 17:44:29 -0700 | [diff] [blame] | 594 | return getAdjustedMinimumFontSize(minFont); |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 595 | } |
| 596 | |
John Reck | 92f25f8 | 2011-04-26 16:57:10 -0700 | [diff] [blame] | 597 | public boolean forceEnableUserScalable() { |
| 598 | return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false); |
| 599 | } |
| 600 | |
John Reck | 7dc444b | 2011-06-16 17:44:29 -0700 | [diff] [blame] | 601 | public int getTextZoom() { |
John Reck | 78a6a1d | 2011-07-21 13:54:03 -0700 | [diff] [blame] | 602 | requireInitialization(); |
John Reck | 7dc444b | 2011-06-16 17:44:29 -0700 | [diff] [blame] | 603 | int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10); |
| 604 | return getAdjustedTextZoom(textZoom); |
| 605 | } |
| 606 | |
| 607 | public void setTextZoom(int percent) { |
| 608 | mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply(); |
| 609 | } |
| 610 | |
Mangesh Ghiware | 67f45c2 | 2011-10-12 14:43:32 -0700 | [diff] [blame] | 611 | public int getDoubleTapZoom() { |
| 612 | requireInitialization(); |
| 613 | int doubleTapZoom = mPrefs.getInt(PREF_DOUBLE_TAP_ZOOM, 5); |
| 614 | return getAdjustedDoubleTapZoom(doubleTapZoom); |
| 615 | } |
| 616 | |
| 617 | public void setDoubleTapZoom(int percent) { |
| 618 | mPrefs.edit().putInt(PREF_DOUBLE_TAP_ZOOM, getRawDoubleTapZoom(percent)).apply(); |
| 619 | } |
| 620 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 621 | // ----------------------------- |
| 622 | // getter/setters for advanced_preferences.xml |
| 623 | // ----------------------------- |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 624 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 625 | public String getSearchEngineName() { |
| 626 | return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE); |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 627 | } |
John Reck | 63bb687 | 2010-12-01 19:29:32 -0800 | [diff] [blame] | 628 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 629 | public boolean openInBackground() { |
| 630 | return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false); |
John Reck | 63bb687 | 2010-12-01 19:29:32 -0800 | [diff] [blame] | 631 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 632 | |
| 633 | public boolean enableJavascript() { |
| 634 | return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true); |
| 635 | } |
| 636 | |
| 637 | // TODO: Cache |
| 638 | public PluginState getPluginState() { |
| 639 | String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON"); |
| 640 | return PluginState.valueOf(state); |
| 641 | } |
| 642 | |
| 643 | // TODO: Cache |
| 644 | public ZoomDensity getDefaultZoom() { |
| 645 | String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM"); |
| 646 | return ZoomDensity.valueOf(zoom); |
| 647 | } |
| 648 | |
| 649 | public boolean loadPageInOverviewMode() { |
| 650 | return mPrefs.getBoolean(PREF_LOAD_PAGE, true); |
| 651 | } |
| 652 | |
| 653 | public boolean autofitPages() { |
| 654 | return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true); |
| 655 | } |
| 656 | |
| 657 | public boolean blockPopupWindows() { |
| 658 | return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true); |
| 659 | } |
| 660 | |
| 661 | public boolean loadImages() { |
| 662 | return mPrefs.getBoolean(PREF_LOAD_IMAGES, true); |
| 663 | } |
| 664 | |
| 665 | public String getDefaultTextEncoding() { |
| 666 | return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null); |
| 667 | } |
| 668 | |
| 669 | // ----------------------------- |
| 670 | // getter/setters for general_preferences.xml |
| 671 | // ----------------------------- |
| 672 | |
| 673 | public String getHomePage() { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 674 | return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext)); |
| 675 | } |
| 676 | |
| 677 | public void setHomePage(String value) { |
| 678 | mPrefs.edit().putString(PREF_HOMEPAGE, value).apply(); |
| 679 | } |
| 680 | |
| 681 | public boolean isAutofillEnabled() { |
| 682 | return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true); |
| 683 | } |
| 684 | |
| 685 | public void setAutofillEnabled(boolean value) { |
| 686 | mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply(); |
| 687 | } |
| 688 | |
| 689 | // ----------------------------- |
| 690 | // getter/setters for debug_preferences.xml |
| 691 | // ----------------------------- |
| 692 | |
| 693 | public boolean isHardwareAccelerated() { |
John Reck | f48314f | 2011-04-27 17:52:17 -0700 | [diff] [blame] | 694 | if (!isDebugEnabled()) { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 695 | return true; |
| 696 | } |
| 697 | return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true); |
| 698 | } |
| 699 | |
Derek Sollenberger | 31adf67 | 2011-07-08 11:31:30 -0400 | [diff] [blame] | 700 | public boolean isSkiaHardwareAccelerated() { |
| 701 | if (!isDebugEnabled()) { |
| 702 | return false; |
| 703 | } |
| 704 | return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL_SKIA, false); |
| 705 | } |
| 706 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 707 | public int getUserAgent() { |
John Reck | f48314f | 2011-04-27 17:52:17 -0700 | [diff] [blame] | 708 | if (!isDebugEnabled()) { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 709 | return 0; |
| 710 | } |
| 711 | return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0")); |
| 712 | } |
| 713 | |
| 714 | // ----------------------------- |
| 715 | // getter/setters for hidden_debug_preferences.xml |
| 716 | // ----------------------------- |
| 717 | |
| 718 | public boolean enableVisualIndicator() { |
| 719 | if (!isDebugEnabled()) { |
| 720 | return false; |
| 721 | } |
| 722 | return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false); |
| 723 | } |
| 724 | |
Teng-Hui Zhu | 85de57a | 2011-09-22 15:34:29 -0700 | [diff] [blame] | 725 | public boolean enableCpuUploadPath() { |
| 726 | if (!isDebugEnabled()) { |
Teng-Hui Zhu | dbe001b | 2011-09-30 10:37:01 -0700 | [diff] [blame] | 727 | return false; |
Teng-Hui Zhu | 85de57a | 2011-09-22 15:34:29 -0700 | [diff] [blame] | 728 | } |
Teng-Hui Zhu | dbe001b | 2011-09-30 10:37:01 -0700 | [diff] [blame] | 729 | return mPrefs.getBoolean(PREF_ENABLE_CPU_UPLOAD_PATH, false); |
Teng-Hui Zhu | 85de57a | 2011-09-22 15:34:29 -0700 | [diff] [blame] | 730 | } |
| 731 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 732 | public boolean enableJavascriptConsole() { |
| 733 | if (!isDebugEnabled()) { |
| 734 | return false; |
| 735 | } |
| 736 | return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true); |
| 737 | } |
| 738 | |
| 739 | public boolean isSmallScreen() { |
| 740 | if (!isDebugEnabled()) { |
| 741 | return false; |
| 742 | } |
| 743 | return mPrefs.getBoolean(PREF_SMALL_SCREEN, false); |
| 744 | } |
| 745 | |
| 746 | public boolean isWideViewport() { |
| 747 | if (!isDebugEnabled()) { |
| 748 | return true; |
| 749 | } |
| 750 | return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true); |
| 751 | } |
| 752 | |
| 753 | public boolean isNormalLayout() { |
| 754 | if (!isDebugEnabled()) { |
| 755 | return false; |
| 756 | } |
| 757 | return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false); |
| 758 | } |
| 759 | |
| 760 | public boolean isTracing() { |
| 761 | if (!isDebugEnabled()) { |
| 762 | return false; |
| 763 | } |
| 764 | return mPrefs.getBoolean(PREF_ENABLE_TRACING, false); |
| 765 | } |
| 766 | |
| 767 | public boolean enableLightTouch() { |
| 768 | if (!isDebugEnabled()) { |
| 769 | return false; |
| 770 | } |
| 771 | return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false); |
| 772 | } |
| 773 | |
| 774 | public boolean enableNavDump() { |
| 775 | if (!isDebugEnabled()) { |
| 776 | return false; |
| 777 | } |
| 778 | return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false); |
| 779 | } |
| 780 | |
| 781 | public String getJsEngineFlags() { |
| 782 | if (!isDebugEnabled()) { |
| 783 | return ""; |
| 784 | } |
| 785 | return mPrefs.getString(PREF_JS_ENGINE_FLAGS, ""); |
| 786 | } |
| 787 | |
| 788 | // ----------------------------- |
| 789 | // getter/setters for lab_preferences.xml |
| 790 | // ----------------------------- |
| 791 | |
| 792 | public boolean useQuickControls() { |
| 793 | return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false); |
| 794 | } |
| 795 | |
| 796 | public boolean useMostVisitedHomepage() { |
John Reck | 961d35d | 2011-06-23 09:45:54 -0700 | [diff] [blame] | 797 | return HomeProvider.MOST_VISITED.equals(getHomePage()); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Michael Kolb | c38c604 | 2011-04-27 10:46:06 -0700 | [diff] [blame] | 800 | public boolean useFullscreen() { |
| 801 | return mPrefs.getBoolean(PREF_FULLSCREEN, false); |
| 802 | } |
| 803 | |
John Reck | 2fd9d0e | 2011-07-15 11:13:48 -0700 | [diff] [blame] | 804 | public boolean useInvertedRendering() { |
| 805 | return mPrefs.getBoolean(PREF_INVERTED, false); |
| 806 | } |
| 807 | |
Nicolas Roard | 5d51310 | 2011-08-03 15:35:34 -0700 | [diff] [blame] | 808 | public float getInvertedContrast() { |
| 809 | return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f); |
| 810 | } |
| 811 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 812 | // ----------------------------- |
| 813 | // getter/setters for privacy_security_preferences.xml |
| 814 | // ----------------------------- |
| 815 | |
| 816 | public boolean showSecurityWarnings() { |
| 817 | return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true); |
| 818 | } |
| 819 | |
| 820 | public boolean acceptCookies() { |
| 821 | return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true); |
| 822 | } |
| 823 | |
| 824 | public boolean saveFormdata() { |
| 825 | return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true); |
| 826 | } |
| 827 | |
| 828 | public boolean enableGeolocation() { |
| 829 | return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true); |
| 830 | } |
| 831 | |
| 832 | public boolean rememberPasswords() { |
| 833 | return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true); |
| 834 | } |
| 835 | |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 836 | // ----------------------------- |
| 837 | // getter/setters for bandwidth_preferences.xml |
| 838 | // ----------------------------- |
| 839 | |
Mathew Inwood | 467813f | 2011-09-02 15:43:17 +0100 | [diff] [blame] | 840 | public static String getPreloadOnWifiOnlyPreferenceString(Context context) { |
| 841 | return context.getResources().getString(R.string.pref_data_preload_value_wifi_only); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 842 | } |
Mathew Inwood | 467813f | 2011-09-02 15:43:17 +0100 | [diff] [blame] | 843 | |
| 844 | public static String getPreloadAlwaysPreferenceString(Context context) { |
| 845 | return context.getResources().getString(R.string.pref_data_preload_value_always); |
| 846 | } |
| 847 | |
Mathew Inwood | 825fba7 | 2011-10-04 14:52:52 +0100 | [diff] [blame] | 848 | private static final String DEAULT_PRELOAD_SECURE_SETTING_KEY = |
| 849 | "browser_default_preload_setting"; |
| 850 | |
| 851 | public String getDefaultPreloadSetting() { |
| 852 | String preload = Settings.Secure.getString(mContext.getContentResolver(), |
| 853 | DEAULT_PRELOAD_SECURE_SETTING_KEY); |
| 854 | if (preload == null) { |
| 855 | preload = mContext.getResources().getString(R.string.pref_data_preload_default_value); |
| 856 | } |
| 857 | return preload; |
Mathew Inwood | 467813f | 2011-09-02 15:43:17 +0100 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | public String getPreloadEnabled() { |
| 861 | return mPrefs.getString(PREF_DATA_PRELOAD, getDefaultPreloadSetting()); |
| 862 | } |
| 863 | |
George Mount | 3636d0a | 2011-11-21 09:08:21 -0800 | [diff] [blame] | 864 | // ----------------------------- |
| 865 | // getter/setters for browser recovery |
| 866 | // ----------------------------- |
| 867 | /** |
| 868 | * The last time browser was started. |
| 869 | * @return The last browser start time as System.currentTimeMillis. This |
| 870 | * can be 0 if this is the first time or the last tab was closed. |
| 871 | */ |
| 872 | public long getLastRecovered() { |
| 873 | return mPrefs.getLong(KEY_LAST_RECOVERED, 0); |
| 874 | } |
| 875 | |
| 876 | /** |
| 877 | * Sets the last browser start time. |
| 878 | * @param time The last time as System.currentTimeMillis that the browser |
| 879 | * was started. This should be set to 0 if the last tab is closed. |
| 880 | */ |
| 881 | public void setLastRecovered(long time) { |
| 882 | mPrefs.edit() |
| 883 | .putLong(KEY_LAST_RECOVERED, time) |
| 884 | .apply(); |
| 885 | } |
| 886 | |
| 887 | /** |
| 888 | * Used to determine whether or not the previous browser run crashed. Once |
| 889 | * the previous state has been determined, the value will be set to false |
| 890 | * until a pause is received. |
| 891 | * @return true if the last browser run was paused or false if it crashed. |
| 892 | */ |
| 893 | public boolean wasLastRunPaused() { |
| 894 | return mPrefs.getBoolean(KEY_LAST_RUN_PAUSED, false); |
| 895 | } |
| 896 | |
| 897 | /** |
| 898 | * Sets whether or not the last run was a pause or crash. |
| 899 | * @param isPaused Set to true When a pause is received or false after |
| 900 | * resuming. |
| 901 | */ |
| 902 | public void setLastRunPaused(boolean isPaused) { |
| 903 | mPrefs.edit() |
| 904 | .putBoolean(KEY_LAST_RUN_PAUSED, isPaused) |
| 905 | .apply(); |
| 906 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 907 | } |