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 | |
John Reck | bafe58a | 2011-01-11 10:26:02 -0800 | [diff] [blame] | 19 | import com.android.browser.homepages.HomeProvider; |
John Reck | 8706008 | 2011-05-18 13:07:57 -0700 | [diff] [blame] | 20 | import com.android.browser.provider.BrowserProvider; |
Bjorn Bringert | d69f51d | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 21 | import com.android.browser.search.SearchEngine; |
| 22 | import com.android.browser.search.SearchEngines; |
| 23 | |
Grace Kloba | 9804c43 | 2009-12-02 11:07:40 -0800 | [diff] [blame] | 24 | import android.app.ActivityManager; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 25 | import android.content.ContentResolver; |
| 26 | import android.content.Context; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 27 | import android.content.SharedPreferences; |
John Reck | 812d2d6 | 2011-01-18 14:16:15 -0800 | [diff] [blame] | 28 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
John Reck | f48314f | 2011-04-27 17:52:17 -0700 | [diff] [blame] | 29 | import android.os.Build; |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 30 | import android.os.Message; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 31 | import android.preference.PreferenceManager; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 32 | import android.provider.Browser; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 33 | import android.webkit.CookieManager; |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 34 | import android.webkit.GeolocationPermissions; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 35 | import android.webkit.WebIconDatabase; |
| 36 | import android.webkit.WebSettings; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 37 | import android.webkit.WebSettings.AutoFillProfile; |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 38 | import android.webkit.WebSettings.LayoutAlgorithm; |
| 39 | import android.webkit.WebSettings.PluginState; |
| 40 | import android.webkit.WebSettings.TextSize; |
| 41 | import android.webkit.WebSettings.ZoomDensity; |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 42 | import android.webkit.WebStorage; |
John Reck | 812d2d6 | 2011-01-18 14:16:15 -0800 | [diff] [blame] | 43 | import android.webkit.WebView; |
| 44 | import android.webkit.WebViewDatabase; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 45 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 46 | import java.lang.ref.WeakReference; |
| 47 | import java.util.Iterator; |
| 48 | import java.util.LinkedList; |
John Reck | b8b2af8 | 2011-05-20 15:58:33 -0700 | [diff] [blame] | 49 | import java.util.WeakHashMap; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 50 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Class for managing settings |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 53 | */ |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 54 | public class BrowserSettings implements OnSharedPreferenceChangeListener, |
| 55 | PreferenceKeys { |
Andrei Popescu | 0106870 | 2009-08-03 16:03:24 +0100 | [diff] [blame] | 56 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 57 | // TODO: Do something with this UserAgent stuff |
John Reck | 7fd1e8f | 2011-04-27 18:22:57 -0700 | [diff] [blame] | 58 | private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; " + |
| 59 | "Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) " + |
| 60 | "Chrome/11.0.696.34 Safari/534.24"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 61 | |
| 62 | private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " + |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 63 | "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " + |
| 64 | "(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] | 65 | |
| 66 | private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " + |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 67 | "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " + |
| 68 | "(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] | 69 | |
| 70 | private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " + |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 71 | "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " + |
| 72 | "(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] | 73 | |
John Reck | 7fd1e8f | 2011-04-27 18:22:57 -0700 | [diff] [blame] | 74 | private static final String HONEYCOMB_USERAGENT = "Mozilla/5.0 (Linux; U; " + |
| 75 | "Android 3.1; en-us; Xoom Build/HMJ25) AppleWebKit/534.13 " + |
| 76 | "(KHTML, like Gecko) Version/4.0 Safari/534.13"; |
| 77 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 78 | private static final String USER_AGENTS[] = { null, |
| 79 | DESKTOP_USERAGENT, |
| 80 | IPHONE_USERAGENT, |
| 81 | IPAD_USERAGENT, |
John Reck | 7fd1e8f | 2011-04-27 18:22:57 -0700 | [diff] [blame] | 82 | FROYO_USERAGENT, |
| 83 | HONEYCOMB_USERAGENT, |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 84 | }; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 85 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 86 | private static BrowserSettings sInstance; |
Jeff Davidson | 4361029 | 2010-07-16 16:03:58 -0700 | [diff] [blame] | 87 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 88 | private Context mContext; |
| 89 | private SharedPreferences mPrefs; |
| 90 | private LinkedList<WeakReference<WebSettings>> mManagedSettings; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 91 | private Controller mController; |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 92 | private WebStorageSizeManager mWebStorageSizeManager; |
| 93 | private AutofillHandler mAutofillHandler; |
John Reck | b8b2af8 | 2011-05-20 15:58:33 -0700 | [diff] [blame] | 94 | private WeakHashMap<WebSettings, String> mCustomUserAgents; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 95 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 96 | // Cached settings |
| 97 | private SearchEngine mSearchEngine; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 98 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 99 | public static void initialize(final Context context) { |
| 100 | sInstance = new BrowserSettings(context); |
| 101 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 102 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 103 | public static BrowserSettings getInstance() { |
| 104 | return sInstance; |
| 105 | } |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 106 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 107 | private BrowserSettings(Context context) { |
| 108 | mContext = context; |
| 109 | mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext); |
John Reck | f48314f | 2011-04-27 17:52:17 -0700 | [diff] [blame] | 110 | if (Build.VERSION.CODENAME.equals("REL")) { |
| 111 | // This is a release build, always startup with debug disabled |
| 112 | setDebugEnabled(false); |
| 113 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 114 | mAutofillHandler = new AutofillHandler(mContext); |
| 115 | mManagedSettings = new LinkedList<WeakReference<WebSettings>>(); |
John Reck | b8b2af8 | 2011-05-20 15:58:33 -0700 | [diff] [blame] | 116 | mCustomUserAgents = new WeakHashMap<WebSettings, String>(); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 117 | mWebStorageSizeManager = new WebStorageSizeManager(mContext, |
| 118 | new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()), |
| 119 | new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath())); |
| 120 | mPrefs.registerOnSharedPreferenceChangeListener(this); |
| 121 | mAutofillHandler.asyncLoadFromDb(); |
| 122 | } |
| 123 | |
| 124 | public void setController(Controller controller) { |
| 125 | mController = controller; |
| 126 | syncSharedSettings(); |
| 127 | |
| 128 | if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) { |
| 129 | ((InstantSearchEngine) mSearchEngine).setController(mController); |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 133 | public void startManagingSettings(WebSettings settings) { |
| 134 | synchronized (mManagedSettings) { |
| 135 | syncStaticSettings(settings); |
| 136 | syncSetting(settings); |
| 137 | mManagedSettings.add(new WeakReference<WebSettings>(settings)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 142 | * Syncs all the settings that have a Preference UI |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 143 | */ |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 144 | private void syncSetting(WebSettings settings) { |
| 145 | settings.setGeolocationEnabled(enableGeolocation()); |
| 146 | settings.setJavaScriptEnabled(enableJavascript()); |
| 147 | settings.setLightTouchEnabled(enableLightTouch()); |
| 148 | settings.setNavDump(enableNavDump()); |
| 149 | settings.setShowVisualIndicator(enableVisualIndicator()); |
| 150 | settings.setDefaultTextEncodingName(getDefaultTextEncoding()); |
| 151 | settings.setDefaultZoom(getDefaultZoom()); |
| 152 | settings.setMinimumFontSize(getMinimumFontSize()); |
| 153 | settings.setMinimumLogicalFontSize(getMinimumFontSize()); |
John Reck | 92f25f8 | 2011-04-26 16:57:10 -0700 | [diff] [blame] | 154 | settings.setForceUserScalable(forceEnableUserScalable()); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 155 | settings.setPluginState(getPluginState()); |
| 156 | settings.setTextSize(getTextSize()); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 157 | settings.setAutoFillEnabled(isAutofillEnabled()); |
| 158 | settings.setLayoutAlgorithm(getLayoutAlgorithm()); |
| 159 | settings.setJavaScriptCanOpenWindowsAutomatically(blockPopupWindows()); |
| 160 | settings.setLoadsImagesAutomatically(loadImages()); |
| 161 | settings.setLoadWithOverviewMode(loadPageInOverviewMode()); |
| 162 | settings.setSavePassword(rememberPasswords()); |
| 163 | settings.setSaveFormData(saveFormdata()); |
| 164 | settings.setUseWideViewPort(isWideViewport()); |
| 165 | settings.setAutoFillProfile(getAutoFillProfile()); |
John Reck | b8b2af8 | 2011-05-20 15:58:33 -0700 | [diff] [blame] | 166 | |
| 167 | String ua = mCustomUserAgents.get(settings); |
| 168 | if (enableUseragentSwitcher() && ua != null) { |
| 169 | settings.setUserAgentString(ua); |
| 170 | } else { |
| 171 | settings.setUserAgentString(USER_AGENTS[getUserAgent()]); |
| 172 | } |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 173 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 174 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 175 | /** |
| 176 | * Syncs all the settings that have no UI |
| 177 | * These cannot change, so we only need to set them once per WebSettings |
| 178 | */ |
| 179 | private void syncStaticSettings(WebSettings settings) { |
| 180 | settings.setDefaultFontSize(16); |
| 181 | settings.setDefaultFixedFontSize(13); |
| 182 | settings.setPageCacheCapacity(getPageCacheCapacity()); |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 183 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 184 | // WebView inside Browser doesn't want initial focus to be set. |
| 185 | settings.setNeedInitialFocus(false); |
| 186 | // Browser supports multiple windows |
| 187 | settings.setSupportMultipleWindows(true); |
| 188 | // enable smooth transition for better performance during panning or |
| 189 | // zooming |
| 190 | settings.setEnableSmoothTransition(true); |
| 191 | // disable content url access |
| 192 | settings.setAllowContentAccess(false); |
| 193 | |
| 194 | // HTML5 API flags |
| 195 | settings.setAppCacheEnabled(true); |
| 196 | settings.setDatabaseEnabled(true); |
| 197 | settings.setDomStorageEnabled(true); |
| 198 | settings.setWorkersEnabled(true); // This only affects V8. |
| 199 | |
| 200 | // HTML5 configuration parametersettings. |
| 201 | settings.setAppCacheMaxSize(mWebStorageSizeManager.getAppCacheMaxSize()); |
| 202 | settings.setAppCachePath(getAppCachePath()); |
| 203 | settings.setDatabasePath(mContext.getDir("databases", 0).getPath()); |
| 204 | settings.setGeolocationDatabasePath(mContext.getDir("geolocation", 0).getPath()); |
| 205 | } |
| 206 | |
| 207 | private void syncSharedSettings() { |
| 208 | CookieManager.getInstance().setAcceptCookie(acceptCookies()); |
| 209 | if (mController != null) { |
| 210 | mController.setShouldShowErrorConsole(enableJavascriptConsole()); |
Shimeng (Simon) Wang | e83e906 | 2010-03-29 16:13:09 -0700 | [diff] [blame] | 211 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 212 | } |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 213 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 214 | private void syncManagedSettings() { |
| 215 | syncSharedSettings(); |
| 216 | synchronized (mManagedSettings) { |
| 217 | Iterator<WeakReference<WebSettings>> iter = mManagedSettings.iterator(); |
| 218 | while (iter.hasNext()) { |
| 219 | WeakReference<WebSettings> ref = iter.next(); |
| 220 | WebSettings settings = ref.get(); |
| 221 | if (settings == null) { |
| 222 | iter.remove(); |
| 223 | continue; |
| 224 | } |
| 225 | syncSetting(settings); |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 226 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 229 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 230 | @Override |
| 231 | public void onSharedPreferenceChanged( |
| 232 | SharedPreferences sharedPreferences, String key) { |
| 233 | syncManagedSettings(); |
| 234 | if (PREF_SEARCH_ENGINE.equals(key)) { |
| 235 | updateSearchEngine(false); |
| 236 | } |
| 237 | if (PREF_USE_INSTANT_SEARCH.equals(key)) { |
| 238 | updateSearchEngine(true); |
| 239 | } |
Michael Kolb | c38c604 | 2011-04-27 10:46:06 -0700 | [diff] [blame] | 240 | if (PREF_FULLSCREEN.equals(key)) { |
| 241 | if (mController.getUi() != null) { |
| 242 | mController.getUi().setFullscreen(useFullscreen()); |
| 243 | } |
| 244 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static String getFactoryResetHomeUrl(Context context) { |
| 248 | String url = context.getResources().getString(R.string.homepage_base); |
| 249 | if (url.indexOf("{CID}") != -1) { |
| 250 | url = url.replace("{CID}", |
| 251 | BrowserProvider.getClientId(context.getContentResolver())); |
| 252 | } |
| 253 | return url; |
| 254 | } |
| 255 | |
| 256 | public LayoutAlgorithm getLayoutAlgorithm() { |
| 257 | LayoutAlgorithm layoutAlgorithm = LayoutAlgorithm.NORMAL; |
| 258 | if (autofitPages()) { |
| 259 | layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS; |
| 260 | } |
| 261 | if (isDebugEnabled()) { |
| 262 | if (isSmallScreen()) { |
| 263 | layoutAlgorithm = LayoutAlgorithm.SINGLE_COLUMN; |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 264 | } else { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 265 | if (isNormalLayout()) { |
| 266 | layoutAlgorithm = LayoutAlgorithm.NORMAL; |
| 267 | } else { |
| 268 | layoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS; |
| 269 | } |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 270 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 271 | } |
| 272 | return layoutAlgorithm; |
| 273 | } |
Ben Murdoch | ef67165 | 2010-11-25 17:17:58 +0000 | [diff] [blame] | 274 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 275 | // TODO: Cache |
| 276 | public int getPageCacheCapacity() { |
| 277 | // the cost of one cached page is ~3M (measured using nytimes.com). For |
| 278 | // low end devices, we only cache one page. For high end devices, we try |
| 279 | // to cache more pages, currently choose 5. |
| 280 | if (ActivityManager.staticGetMemoryClass() > 16) { |
| 281 | return 5; |
| 282 | } else { |
| 283 | return 1; |
Grace Kloba | 9804c43 | 2009-12-02 11:07:40 -0800 | [diff] [blame] | 284 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 285 | } |
| 286 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 287 | public WebStorageSizeManager getWebStorageSizeManager() { |
| 288 | return mWebStorageSizeManager; |
| 289 | } |
| 290 | |
| 291 | // TODO: Cache |
| 292 | private String getAppCachePath() { |
| 293 | return mContext.getDir("appcache", 0).getPath(); |
| 294 | } |
| 295 | |
| 296 | private void updateSearchEngine(boolean force) { |
| 297 | String searchEngineName = getSearchEngineName(); |
| 298 | if (force || mSearchEngine == null || |
| 299 | !mSearchEngine.getName().equals(searchEngineName)) { |
| 300 | if (mSearchEngine != null) { |
| 301 | if (mSearchEngine.supportsVoiceSearch()) { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 302 | // One or more tabs could have been in voice search mode. |
| 303 | // Clear it, since the new SearchEngine may not support |
| 304 | // it, or may handle it differently. |
| 305 | for (int i = 0; i < mController.getTabControl().getTabCount(); i++) { |
| 306 | mController.getTabControl().getTab(i).revertVoiceSearchMode(); |
| 307 | } |
| 308 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 309 | mSearchEngine.close(); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 310 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 311 | mSearchEngine = SearchEngines.get(mContext, searchEngineName); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 312 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 313 | if (mController != null && (mSearchEngine instanceof InstantSearchEngine)) { |
| 314 | ((InstantSearchEngine) mSearchEngine).setController(mController); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
Bjorn Bringert | d69f51d | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 319 | public SearchEngine getSearchEngine() { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 320 | if (mSearchEngine == null) { |
| 321 | updateSearchEngine(false); |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 322 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 323 | return mSearchEngine; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 324 | } |
| 325 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 326 | public boolean isDebugEnabled() { |
| 327 | return mPrefs.getBoolean(PREF_DEBUG_MENU, false); |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 328 | } |
| 329 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 330 | public void setDebugEnabled(boolean value) { |
| 331 | mPrefs.edit().putBoolean(PREF_DEBUG_MENU, value).apply(); |
Ben Murdoch | 6fa32ba | 2010-10-20 14:01:25 +0100 | [diff] [blame] | 332 | } |
| 333 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 334 | public void clearCache() { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 335 | WebIconDatabase.getInstance().removeAllIcons(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 336 | if (mController != null) { |
| 337 | WebView current = mController.getCurrentWebView(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 338 | if (current != null) { |
| 339 | current.clearCache(true); |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 344 | public void clearCookies() { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 345 | CookieManager.getInstance().removeAllCookie(); |
| 346 | } |
| 347 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 348 | public void clearHistory() { |
| 349 | ContentResolver resolver = mContext.getContentResolver(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 350 | Browser.clearHistory(resolver); |
| 351 | Browser.clearSearches(resolver); |
| 352 | } |
| 353 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 354 | public void clearFormData() { |
| 355 | WebViewDatabase.getInstance(mContext).clearFormData(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 356 | if (mController!= null) { |
| 357 | WebView currentTopView = mController.getCurrentTopWebView(); |
Henrik Baard | 794dc72 | 2010-02-19 16:28:06 +0100 | [diff] [blame] | 358 | if (currentTopView != null) { |
| 359 | currentTopView.clearFormData(); |
| 360 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 364 | public void clearPasswords() { |
| 365 | WebViewDatabase db = WebViewDatabase.getInstance(mContext); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 366 | db.clearUsernamePassword(); |
| 367 | db.clearHttpAuthUsernamePassword(); |
| 368 | } |
| 369 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 370 | public void clearDatabases() { |
Andrei Popescu | 824faeb | 2009-07-21 18:24:06 +0100 | [diff] [blame] | 371 | WebStorage.getInstance().deleteAllData(); |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 372 | } |
| 373 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 374 | public void clearLocationAccess() { |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 375 | GeolocationPermissions.getInstance().clearAll(); |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 376 | } |
| 377 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 378 | public void resetDefaultPreferences() { |
| 379 | mPrefs.edit().clear().apply(); |
| 380 | syncManagedSettings(); |
Grace Kloba | f2c5c1b | 2009-05-26 10:48:31 -0700 | [diff] [blame] | 381 | } |
| 382 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 383 | public AutoFillProfile getAutoFillProfile() { |
| 384 | mAutofillHandler.waitForLoad(); |
| 385 | return mAutofillHandler.getAutoFillProfile(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 386 | } |
| 387 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 388 | public void setAutoFillProfile(AutoFillProfile profile, Message msg) { |
| 389 | mAutofillHandler.waitForLoad(); |
| 390 | mAutofillHandler.setAutoFillProfile(profile, msg); |
Shimeng (Simon) Wang | f739271 | 2010-03-10 16:44:31 -0800 | [diff] [blame] | 391 | } |
| 392 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 393 | public void toggleDebugSettings() { |
| 394 | setDebugEnabled(!isDebugEnabled()); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 395 | } |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 396 | |
John Reck | b8b2af8 | 2011-05-20 15:58:33 -0700 | [diff] [blame] | 397 | public boolean hasDesktopUseragent(WebView view) { |
| 398 | return view != null && mCustomUserAgents.get(view.getSettings()) != null; |
| 399 | } |
| 400 | |
| 401 | public void toggleDesktopUseragent(WebView view) { |
John Reck | b0a86db | 2011-05-24 14:05:58 -0700 | [diff] [blame^] | 402 | if (view == null) { |
| 403 | return; |
| 404 | } |
John Reck | b8b2af8 | 2011-05-20 15:58:33 -0700 | [diff] [blame] | 405 | WebSettings settings = view.getSettings(); |
| 406 | if (mCustomUserAgents.get(settings) != null) { |
| 407 | mCustomUserAgents.remove(settings); |
| 408 | settings.setUserAgentString(USER_AGENTS[getUserAgent()]); |
| 409 | } else { |
| 410 | mCustomUserAgents.put(settings, DESKTOP_USERAGENT); |
| 411 | settings.setUserAgentString(DESKTOP_USERAGENT); |
| 412 | } |
| 413 | } |
| 414 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 415 | // ----------------------------- |
| 416 | // getter/setters for accessibility_preferences.xml |
| 417 | // ----------------------------- |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 418 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 419 | // TODO: Cache |
| 420 | public TextSize getTextSize() { |
| 421 | String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL"); |
| 422 | return TextSize.valueOf(textSize); |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 423 | } |
| 424 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 425 | public int getMinimumFontSize() { |
| 426 | return mPrefs.getInt(PREF_MIN_FONT_SIZE, 1); |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 427 | } |
| 428 | |
John Reck | 92f25f8 | 2011-04-26 16:57:10 -0700 | [diff] [blame] | 429 | public boolean forceEnableUserScalable() { |
| 430 | return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false); |
| 431 | } |
| 432 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 433 | // ----------------------------- |
| 434 | // getter/setters for advanced_preferences.xml |
| 435 | // ----------------------------- |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 436 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 437 | public String getSearchEngineName() { |
| 438 | return mPrefs.getString(PREF_SEARCH_ENGINE, SearchEngine.GOOGLE); |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 439 | } |
John Reck | 63bb687 | 2010-12-01 19:29:32 -0800 | [diff] [blame] | 440 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 441 | public boolean openInBackground() { |
| 442 | return mPrefs.getBoolean(PREF_OPEN_IN_BACKGROUND, false); |
John Reck | 63bb687 | 2010-12-01 19:29:32 -0800 | [diff] [blame] | 443 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 444 | |
| 445 | public boolean enableJavascript() { |
| 446 | return mPrefs.getBoolean(PREF_ENABLE_JAVASCRIPT, true); |
| 447 | } |
| 448 | |
| 449 | // TODO: Cache |
| 450 | public PluginState getPluginState() { |
| 451 | String state = mPrefs.getString(PREF_PLUGIN_STATE, "ON"); |
| 452 | return PluginState.valueOf(state); |
| 453 | } |
| 454 | |
| 455 | // TODO: Cache |
| 456 | public ZoomDensity getDefaultZoom() { |
| 457 | String zoom = mPrefs.getString(PREF_DEFAULT_ZOOM, "MEDIUM"); |
| 458 | return ZoomDensity.valueOf(zoom); |
| 459 | } |
| 460 | |
| 461 | public boolean loadPageInOverviewMode() { |
| 462 | return mPrefs.getBoolean(PREF_LOAD_PAGE, true); |
| 463 | } |
| 464 | |
| 465 | public boolean autofitPages() { |
| 466 | return mPrefs.getBoolean(PREF_AUTOFIT_PAGES, true); |
| 467 | } |
| 468 | |
| 469 | public boolean blockPopupWindows() { |
| 470 | return mPrefs.getBoolean(PREF_BLOCK_POPUP_WINDOWS, true); |
| 471 | } |
| 472 | |
| 473 | public boolean loadImages() { |
| 474 | return mPrefs.getBoolean(PREF_LOAD_IMAGES, true); |
| 475 | } |
| 476 | |
| 477 | public String getDefaultTextEncoding() { |
| 478 | return mPrefs.getString(PREF_DEFAULT_TEXT_ENCODING, null); |
| 479 | } |
| 480 | |
| 481 | // ----------------------------- |
| 482 | // getter/setters for general_preferences.xml |
| 483 | // ----------------------------- |
| 484 | |
| 485 | public String getHomePage() { |
| 486 | if (useMostVisitedHomepage()) { |
| 487 | return HomeProvider.MOST_VISITED; |
| 488 | } |
| 489 | return mPrefs.getString(PREF_HOMEPAGE, getFactoryResetHomeUrl(mContext)); |
| 490 | } |
| 491 | |
| 492 | public void setHomePage(String value) { |
| 493 | mPrefs.edit().putString(PREF_HOMEPAGE, value).apply(); |
| 494 | } |
| 495 | |
| 496 | public boolean isAutofillEnabled() { |
| 497 | return mPrefs.getBoolean(PREF_AUTOFILL_ENABLED, true); |
| 498 | } |
| 499 | |
| 500 | public void setAutofillEnabled(boolean value) { |
| 501 | mPrefs.edit().putBoolean(PREF_AUTOFILL_ENABLED, value).apply(); |
| 502 | } |
| 503 | |
| 504 | // ----------------------------- |
| 505 | // getter/setters for debug_preferences.xml |
| 506 | // ----------------------------- |
| 507 | |
| 508 | public boolean isHardwareAccelerated() { |
John Reck | f48314f | 2011-04-27 17:52:17 -0700 | [diff] [blame] | 509 | if (!isDebugEnabled()) { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 510 | return true; |
| 511 | } |
| 512 | return mPrefs.getBoolean(PREF_ENABLE_HARDWARE_ACCEL, true); |
| 513 | } |
| 514 | |
| 515 | public int getUserAgent() { |
John Reck | f48314f | 2011-04-27 17:52:17 -0700 | [diff] [blame] | 516 | if (!isDebugEnabled()) { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 517 | return 0; |
| 518 | } |
| 519 | return Integer.parseInt(mPrefs.getString(PREF_USER_AGENT, "0")); |
| 520 | } |
| 521 | |
| 522 | // ----------------------------- |
| 523 | // getter/setters for hidden_debug_preferences.xml |
| 524 | // ----------------------------- |
| 525 | |
| 526 | public boolean enableVisualIndicator() { |
| 527 | if (!isDebugEnabled()) { |
| 528 | return false; |
| 529 | } |
| 530 | return mPrefs.getBoolean(PREF_ENABLE_VISUAL_INDICATOR, false); |
| 531 | } |
| 532 | |
| 533 | public boolean enableJavascriptConsole() { |
| 534 | if (!isDebugEnabled()) { |
| 535 | return false; |
| 536 | } |
| 537 | return mPrefs.getBoolean(PREF_JAVASCRIPT_CONSOLE, true); |
| 538 | } |
| 539 | |
| 540 | public boolean isSmallScreen() { |
| 541 | if (!isDebugEnabled()) { |
| 542 | return false; |
| 543 | } |
| 544 | return mPrefs.getBoolean(PREF_SMALL_SCREEN, false); |
| 545 | } |
| 546 | |
| 547 | public boolean isWideViewport() { |
| 548 | if (!isDebugEnabled()) { |
| 549 | return true; |
| 550 | } |
| 551 | return mPrefs.getBoolean(PREF_WIDE_VIEWPORT, true); |
| 552 | } |
| 553 | |
| 554 | public boolean isNormalLayout() { |
| 555 | if (!isDebugEnabled()) { |
| 556 | return false; |
| 557 | } |
| 558 | return mPrefs.getBoolean(PREF_NORMAL_LAYOUT, false); |
| 559 | } |
| 560 | |
| 561 | public boolean isTracing() { |
| 562 | if (!isDebugEnabled()) { |
| 563 | return false; |
| 564 | } |
| 565 | return mPrefs.getBoolean(PREF_ENABLE_TRACING, false); |
| 566 | } |
| 567 | |
| 568 | public boolean enableLightTouch() { |
| 569 | if (!isDebugEnabled()) { |
| 570 | return false; |
| 571 | } |
| 572 | return mPrefs.getBoolean(PREF_ENABLE_LIGHT_TOUCH, false); |
| 573 | } |
| 574 | |
| 575 | public boolean enableNavDump() { |
| 576 | if (!isDebugEnabled()) { |
| 577 | return false; |
| 578 | } |
| 579 | return mPrefs.getBoolean(PREF_ENABLE_NAV_DUMP, false); |
| 580 | } |
| 581 | |
| 582 | public String getJsEngineFlags() { |
| 583 | if (!isDebugEnabled()) { |
| 584 | return ""; |
| 585 | } |
| 586 | return mPrefs.getString(PREF_JS_ENGINE_FLAGS, ""); |
| 587 | } |
| 588 | |
| 589 | // ----------------------------- |
| 590 | // getter/setters for lab_preferences.xml |
| 591 | // ----------------------------- |
| 592 | |
| 593 | public boolean useQuickControls() { |
| 594 | return mPrefs.getBoolean(PREF_ENABLE_QUICK_CONTROLS, false); |
| 595 | } |
| 596 | |
| 597 | public boolean useMostVisitedHomepage() { |
| 598 | return mPrefs.getBoolean(PREF_USE_MOST_VISITED_HOMEPAGE, false); |
| 599 | } |
| 600 | |
| 601 | public boolean useInstantSearch() { |
| 602 | return mPrefs.getBoolean(PREF_USE_INSTANT_SEARCH, false); |
| 603 | } |
| 604 | |
Michael Kolb | c38c604 | 2011-04-27 10:46:06 -0700 | [diff] [blame] | 605 | public boolean useFullscreen() { |
| 606 | return mPrefs.getBoolean(PREF_FULLSCREEN, false); |
| 607 | } |
| 608 | |
John Reck | b8b2af8 | 2011-05-20 15:58:33 -0700 | [diff] [blame] | 609 | public boolean enableUseragentSwitcher() { |
| 610 | return mPrefs.getBoolean(PREF_ENABLE_USERAGENT_SWITCHER, false); |
| 611 | } |
| 612 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 613 | // ----------------------------- |
| 614 | // getter/setters for privacy_security_preferences.xml |
| 615 | // ----------------------------- |
| 616 | |
| 617 | public boolean showSecurityWarnings() { |
| 618 | return mPrefs.getBoolean(PREF_SHOW_SECURITY_WARNINGS, true); |
| 619 | } |
| 620 | |
| 621 | public boolean acceptCookies() { |
| 622 | return mPrefs.getBoolean(PREF_ACCEPT_COOKIES, true); |
| 623 | } |
| 624 | |
| 625 | public boolean saveFormdata() { |
| 626 | return mPrefs.getBoolean(PREF_SAVE_FORMDATA, true); |
| 627 | } |
| 628 | |
| 629 | public boolean enableGeolocation() { |
| 630 | return mPrefs.getBoolean(PREF_ENABLE_GEOLOCATION, true); |
| 631 | } |
| 632 | |
| 633 | public boolean rememberPasswords() { |
| 634 | return mPrefs.getBoolean(PREF_REMEMBER_PASSWORDS, true); |
| 635 | } |
| 636 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 637 | } |