Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 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 | |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 19 | import com.android.common.speech.LoggingEvents; |
| 20 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 21 | import android.app.Activity; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 22 | import android.app.AlertDialog; |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 23 | import android.app.SearchManager; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 24 | import android.content.ContentResolver; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 26 | import android.content.DialogInterface; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 27 | import android.content.DialogInterface.OnCancelListener; |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 28 | import android.content.Intent; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 29 | import android.graphics.Bitmap; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 30 | import android.graphics.BitmapFactory; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 31 | import android.net.Uri; |
| 32 | import android.net.http.SslError; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 33 | import android.os.Bundle; |
| 34 | import android.os.Message; |
Kristian Monsen | 4dce3bf | 2010-02-02 13:37:09 +0000 | [diff] [blame] | 35 | import android.os.SystemClock; |
Leon Scroggins | a1cc3fd | 2010-02-01 16:14:11 -0500 | [diff] [blame] | 36 | import android.speech.RecognizerResultsIntent; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 37 | import android.util.Log; |
| 38 | import android.view.KeyEvent; |
| 39 | import android.view.LayoutInflater; |
| 40 | import android.view.View; |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 41 | import android.view.ViewStub; |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 42 | import android.webkit.ConsoleMessage; |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 43 | import android.webkit.DownloadListener; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 44 | import android.webkit.GeolocationPermissions; |
| 45 | import android.webkit.HttpAuthHandler; |
| 46 | import android.webkit.SslErrorHandler; |
| 47 | import android.webkit.URLUtil; |
| 48 | import android.webkit.ValueCallback; |
| 49 | import android.webkit.WebBackForwardList; |
Leon Scroggins | 0c75a8e | 2010-03-03 16:40:58 -0500 | [diff] [blame] | 50 | import android.webkit.WebBackForwardListClient; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 51 | import android.webkit.WebChromeClient; |
| 52 | import android.webkit.WebHistoryItem; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 53 | import android.webkit.WebStorage; |
| 54 | import android.webkit.WebView; |
| 55 | import android.webkit.WebViewClient; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 56 | import android.widget.LinearLayout; |
| 57 | import android.widget.TextView; |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 58 | import android.widget.Toast; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 59 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 60 | import java.util.ArrayList; |
| 61 | import java.util.HashMap; |
| 62 | import java.util.Iterator; |
| 63 | import java.util.LinkedList; |
| 64 | import java.util.Map; |
| 65 | import java.util.Vector; |
| 66 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Class for maintaining Tabs with a main WebView and a subwindow. |
| 69 | */ |
| 70 | class Tab { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 71 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 72 | // Log Tag |
| 73 | private static final String LOGTAG = "Tab"; |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 74 | // Special case the logtag for messages for the Console to make it easier to |
| 75 | // filter them and match the logtag used for these messages in older versions |
| 76 | // of the browser. |
| 77 | private static final String CONSOLE_LOGTAG = "browser"; |
| 78 | |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 79 | public enum LockIcon { |
| 80 | LOCK_ICON_UNSECURE, |
| 81 | LOCK_ICON_SECURE, |
| 82 | LOCK_ICON_MIXED, |
| 83 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 84 | |
| 85 | Activity mActivity; |
| 86 | private WebViewController mWebViewController; |
| 87 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 88 | // The Geolocation permissions prompt |
| 89 | private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt; |
| 90 | // Main WebView wrapper |
Michael Kolb | a713ec8 | 2010-11-29 17:27:06 -0800 | [diff] [blame] | 91 | private View mContainer; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 92 | // Main WebView |
| 93 | private WebView mMainView; |
| 94 | // Subwindow container |
| 95 | private View mSubViewContainer; |
| 96 | // Subwindow WebView |
| 97 | private WebView mSubView; |
| 98 | // Saved bundle for when we are running low on memory. It contains the |
| 99 | // information needed to restore the WebView if the user goes back to the |
| 100 | // tab. |
| 101 | private Bundle mSavedState; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 102 | // Parent Tab. This is the Tab that created this Tab, or null if the Tab was |
| 103 | // created by the UI |
| 104 | private Tab mParentTab; |
| 105 | // Tab that constructed by this Tab. This is used when this Tab is |
| 106 | // destroyed, it clears all mParentTab values in the children. |
| 107 | private Vector<Tab> mChildTabs; |
| 108 | // If true, the tab will be removed when back out of the first page. |
| 109 | private boolean mCloseOnExit; |
| 110 | // If true, the tab is in the foreground of the current activity. |
| 111 | private boolean mInForeground; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 112 | // If true, the tab is in page loading state (after onPageStarted, |
| 113 | // before onPageFinsihed) |
| 114 | private boolean mInPageLoad; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 115 | // The last reported progress of the current page |
| 116 | private int mPageLoadProgress; |
Kristian Monsen | 4dce3bf | 2010-02-02 13:37:09 +0000 | [diff] [blame] | 117 | // The time the load started, used to find load page time |
| 118 | private long mLoadStartTime; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 119 | // Application identifier used to find tabs that another application wants |
| 120 | // to reuse. |
| 121 | private String mAppId; |
| 122 | // Keep the original url around to avoid killing the old WebView if the url |
| 123 | // has not changed. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 124 | // Error console for the tab |
| 125 | private ErrorConsoleView mErrorConsole; |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 126 | // The listener that gets invoked when a download is started from the |
| 127 | // mMainView |
| 128 | private final DownloadListener mDownloadListener; |
Leon Scroggins | 0c75a8e | 2010-03-03 16:40:58 -0500 | [diff] [blame] | 129 | // Listener used to know when we move forward or back in the history list. |
| 130 | private final WebBackForwardListClient mWebBackForwardListClient; |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 131 | private DataController mDataController; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 132 | |
| 133 | // AsyncTask for downloading touch icons |
| 134 | DownloadTouchIcon mTouchIconLoader; |
| 135 | |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 136 | // All the state needed for a page |
| 137 | private static class PageState { |
| 138 | String mUrl; |
| 139 | String mTitle; |
| 140 | LockIcon mLockIcon; |
| 141 | Bitmap mFavicon; |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 142 | Boolean mIsBookmarkedSite = false; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 143 | |
| 144 | PageState(Context c, boolean incognito) { |
| 145 | if (incognito) { |
| 146 | mUrl = "browser:incognito"; |
| 147 | mTitle = c.getString(R.string.new_incognito_tab); |
| 148 | mFavicon = BitmapFactory.decodeResource( |
| 149 | c.getResources(), R.drawable.fav_incognito); |
| 150 | } else { |
| 151 | mUrl = ""; |
| 152 | mTitle = c.getString(R.string.new_tab); |
| 153 | mFavicon = BitmapFactory.decodeResource( |
| 154 | c.getResources(), R.drawable.app_web_browser_sm); |
| 155 | } |
| 156 | mLockIcon = LockIcon.LOCK_ICON_UNSECURE; |
| 157 | } |
| 158 | |
| 159 | PageState(Context c, boolean incognito, String url, Bitmap favicon) { |
| 160 | mUrl = url; |
| 161 | mTitle = null; |
| 162 | if (URLUtil.isHttpsUrl(url)) { |
| 163 | mLockIcon = LockIcon.LOCK_ICON_SECURE; |
| 164 | } else { |
| 165 | mLockIcon = LockIcon.LOCK_ICON_UNSECURE; |
| 166 | } |
| 167 | if (favicon != null) { |
| 168 | mFavicon = favicon; |
| 169 | } else { |
| 170 | if (incognito) { |
| 171 | mFavicon = BitmapFactory.decodeResource( |
| 172 | c.getResources(), R.drawable.fav_incognito); |
| 173 | } else { |
| 174 | mFavicon = BitmapFactory.decodeResource( |
| 175 | c.getResources(), R.drawable.app_web_browser_sm); |
| 176 | } |
| 177 | } |
| 178 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 179 | } |
| 180 | |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 181 | // The current/loading page's state |
| 182 | private PageState mCurrentState; |
| 183 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 184 | // Used for saving and restoring each Tab |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 185 | // TODO: Figure out who uses what and where |
| 186 | // Some of these aren't use in this class, and some are only used in |
| 187 | // restoring state but not saving it - FIX THIS |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 188 | static final String WEBVIEW = "webview"; |
| 189 | static final String NUMTABS = "numTabs"; |
| 190 | static final String CURRTAB = "currentTab"; |
| 191 | static final String CURRURL = "currentUrl"; |
| 192 | static final String CURRTITLE = "currentTitle"; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 193 | static final String CLOSEONEXIT = "closeonexit"; |
| 194 | static final String PARENTTAB = "parentTab"; |
| 195 | static final String APPID = "appid"; |
| 196 | static final String ORIGINALURL = "originalUrl"; |
Elliott Slaughter | 3d6df16 | 2010-08-25 13:17:44 -0700 | [diff] [blame] | 197 | static final String INCOGNITO = "privateBrowsingEnabled"; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 198 | |
| 199 | // ------------------------------------------------------------------------- |
| 200 | |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 201 | /** |
| 202 | * Private information regarding the latest voice search. If the Tab is not |
| 203 | * in voice search mode, this will be null. |
| 204 | */ |
| 205 | private VoiceSearchData mVoiceSearchData; |
| 206 | /** |
Leon Scroggins III | 95d9bfd | 2010-09-14 14:02:36 -0400 | [diff] [blame] | 207 | * Remove voice search mode from this tab. |
| 208 | */ |
| 209 | public void revertVoiceSearchMode() { |
| 210 | if (mVoiceSearchData != null) { |
| 211 | mVoiceSearchData = null; |
| 212 | if (mInForeground) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 213 | mWebViewController.revertVoiceSearchMode(this); |
Leon Scroggins III | 95d9bfd | 2010-09-14 14:02:36 -0400 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 217 | |
Leon Scroggins III | 95d9bfd | 2010-09-14 14:02:36 -0400 | [diff] [blame] | 218 | /** |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 219 | * Return whether the tab is in voice search mode. |
| 220 | */ |
| 221 | public boolean isInVoiceSearchMode() { |
| 222 | return mVoiceSearchData != null; |
| 223 | } |
| 224 | /** |
Leon Scroggins III | c1f5ae2 | 2010-06-29 17:11:29 -0400 | [diff] [blame] | 225 | * Return true if the Tab is in voice search mode and the voice search |
| 226 | * Intent came with a String identifying that Google provided the Intent. |
Leon Scroggins | 1fe13a5 | 2010-02-09 15:31:26 -0500 | [diff] [blame] | 227 | */ |
| 228 | public boolean voiceSearchSourceIsGoogle() { |
| 229 | return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle; |
| 230 | } |
| 231 | /** |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 232 | * Get the title to display for the current voice search page. If the Tab |
| 233 | * is not in voice search mode, return null. |
| 234 | */ |
| 235 | public String getVoiceDisplayTitle() { |
| 236 | if (mVoiceSearchData == null) return null; |
| 237 | return mVoiceSearchData.mLastVoiceSearchTitle; |
| 238 | } |
| 239 | /** |
| 240 | * Get the latest array of voice search results, to be passed to the |
| 241 | * BrowserProvider. If the Tab is not in voice search mode, return null. |
| 242 | */ |
| 243 | public ArrayList<String> getVoiceSearchResults() { |
| 244 | if (mVoiceSearchData == null) return null; |
| 245 | return mVoiceSearchData.mVoiceSearchResults; |
| 246 | } |
| 247 | /** |
| 248 | * Activate voice search mode. |
| 249 | * @param intent Intent which has the results to use, or an index into the |
| 250 | * results when reusing the old results. |
| 251 | */ |
| 252 | /* package */ void activateVoiceSearchMode(Intent intent) { |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame] | 253 | int index = 0; |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 254 | ArrayList<String> results = intent.getStringArrayListExtra( |
Leon Scroggins | a1cc3fd | 2010-02-01 16:14:11 -0500 | [diff] [blame] | 255 | RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 256 | if (results != null) { |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame] | 257 | ArrayList<String> urls = intent.getStringArrayListExtra( |
| 258 | RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS); |
| 259 | ArrayList<String> htmls = intent.getStringArrayListExtra( |
| 260 | RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML); |
| 261 | ArrayList<String> baseUrls = intent.getStringArrayListExtra( |
| 262 | RecognizerResultsIntent |
| 263 | .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 264 | // This tab is now entering voice search mode for the first time, or |
| 265 | // a new voice search was done. |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame] | 266 | int size = results.size(); |
| 267 | if (urls == null || size != urls.size()) { |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 268 | throw new AssertionError("improper extras passed in Intent"); |
| 269 | } |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame] | 270 | if (htmls == null || htmls.size() != size || baseUrls == null || |
| 271 | (baseUrls.size() != size && baseUrls.size() != 1)) { |
| 272 | // If either of these arrays are empty/incorrectly sized, ignore |
| 273 | // them. |
| 274 | htmls = null; |
| 275 | baseUrls = null; |
| 276 | } |
| 277 | mVoiceSearchData = new VoiceSearchData(results, urls, htmls, |
| 278 | baseUrls); |
Leon Scroggins | 9df9497 | 2010-03-08 18:20:35 -0500 | [diff] [blame] | 279 | mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra( |
| 280 | RecognizerResultsIntent |
| 281 | .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS); |
Leon Scroggins | 1fe13a5 | 2010-02-09 15:31:26 -0500 | [diff] [blame] | 282 | mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra( |
| 283 | VoiceSearchData.SOURCE_IS_GOOGLE, false); |
Leon Scroggins | 2ee4a5a | 2010-03-15 16:56:57 -0400 | [diff] [blame] | 284 | mVoiceSearchData.mVoiceSearchIntent = new Intent(intent); |
Leon Scroggins | e10dde5 | 2010-03-08 19:53:03 -0500 | [diff] [blame] | 285 | } |
| 286 | String extraData = intent.getStringExtra( |
| 287 | SearchManager.EXTRA_DATA_KEY); |
| 288 | if (extraData != null) { |
| 289 | index = Integer.parseInt(extraData); |
| 290 | if (index >= mVoiceSearchData.mVoiceSearchResults.size()) { |
| 291 | throw new AssertionError("index must be less than " |
| 292 | + "size of mVoiceSearchResults"); |
| 293 | } |
| 294 | if (mVoiceSearchData.mSourceIsGoogle) { |
| 295 | Intent logIntent = new Intent( |
| 296 | LoggingEvents.ACTION_LOG_EVENT); |
| 297 | logIntent.putExtra(LoggingEvents.EXTRA_EVENT, |
| 298 | LoggingEvents.VoiceSearch.N_BEST_CHOOSE); |
| 299 | logIntent.putExtra( |
| 300 | LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX, |
| 301 | index); |
| 302 | mActivity.sendBroadcast(logIntent); |
| 303 | } |
| 304 | if (mVoiceSearchData.mVoiceSearchIntent != null) { |
Leon Scroggins | 2ee4a5a | 2010-03-15 16:56:57 -0400 | [diff] [blame] | 305 | // Copy the Intent, so that each history item will have its own |
| 306 | // Intent, with different (or none) extra data. |
| 307 | Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent); |
| 308 | latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData); |
| 309 | mVoiceSearchData.mVoiceSearchIntent = latest; |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | mVoiceSearchData.mLastVoiceSearchTitle |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame] | 313 | = mVoiceSearchData.mVoiceSearchResults.get(index); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 314 | if (mInForeground) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 315 | mWebViewController.activateVoiceSearchMode(mVoiceSearchData.mLastVoiceSearchTitle); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 316 | } |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame] | 317 | if (mVoiceSearchData.mVoiceSearchHtmls != null) { |
| 318 | // When index was found it was already ensured that it was valid |
| 319 | String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index); |
| 320 | if (uriString != null) { |
| 321 | Uri dataUri = Uri.parse(uriString); |
| 322 | if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals( |
| 323 | dataUri.getScheme())) { |
| 324 | // If there is only one base URL, use it. If there are |
| 325 | // more, there will be one for each index, so use the base |
| 326 | // URL corresponding to the index. |
| 327 | String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get( |
| 328 | mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ? |
| 329 | index : 0); |
| 330 | mVoiceSearchData.mLastVoiceSearchUrl = baseUrl; |
| 331 | mMainView.loadDataWithBaseURL(baseUrl, |
| 332 | uriString.substring(RecognizerResultsIntent |
| 333 | .URI_SCHEME_INLINE.length() + 1), "text/html", |
| 334 | "utf-8", baseUrl); |
| 335 | return; |
| 336 | } |
| 337 | } |
| 338 | } |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 339 | mVoiceSearchData.mLastVoiceSearchUrl |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame] | 340 | = mVoiceSearchData.mVoiceSearchUrls.get(index); |
| 341 | if (null == mVoiceSearchData.mLastVoiceSearchUrl) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 342 | mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter( |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame] | 343 | mVoiceSearchData.mLastVoiceSearchTitle); |
| 344 | } |
Leon Scroggins | 9df9497 | 2010-03-08 18:20:35 -0500 | [diff] [blame] | 345 | Map<String, String> headers = null; |
| 346 | if (mVoiceSearchData.mHeaders != null) { |
| 347 | int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0 |
| 348 | : index; |
| 349 | Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex); |
| 350 | if (bundle != null && !bundle.isEmpty()) { |
| 351 | Iterator<String> iter = bundle.keySet().iterator(); |
| 352 | headers = new HashMap<String, String>(); |
| 353 | while (iter.hasNext()) { |
| 354 | String key = iter.next(); |
| 355 | headers.put(key, bundle.getString(key)); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 360 | } |
| 361 | /* package */ static class VoiceSearchData { |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 362 | public VoiceSearchData(ArrayList<String> results, |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame] | 363 | ArrayList<String> urls, ArrayList<String> htmls, |
| 364 | ArrayList<String> baseUrls) { |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 365 | mVoiceSearchResults = results; |
| 366 | mVoiceSearchUrls = urls; |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame] | 367 | mVoiceSearchHtmls = htmls; |
| 368 | mVoiceSearchBaseUrls = baseUrls; |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 369 | } |
| 370 | /* |
| 371 | * ArrayList of suggestions to be displayed when opening the |
| 372 | * SearchManager |
| 373 | */ |
| 374 | public ArrayList<String> mVoiceSearchResults; |
| 375 | /* |
| 376 | * ArrayList of urls, associated with the suggestions in |
| 377 | * mVoiceSearchResults. |
| 378 | */ |
| 379 | public ArrayList<String> mVoiceSearchUrls; |
| 380 | /* |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame] | 381 | * ArrayList holding content to load for each item in |
| 382 | * mVoiceSearchResults. |
| 383 | */ |
| 384 | public ArrayList<String> mVoiceSearchHtmls; |
| 385 | /* |
| 386 | * ArrayList holding base urls for the items in mVoiceSearchResults. |
| 387 | * If non null, this will either have the same size as |
| 388 | * mVoiceSearchResults or have a size of 1, in which case all will use |
| 389 | * the same base url |
| 390 | */ |
| 391 | public ArrayList<String> mVoiceSearchBaseUrls; |
| 392 | /* |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 393 | * The last url provided by voice search. Used for comparison to see if |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame] | 394 | * we are going to a page by some method besides voice search. |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 395 | */ |
| 396 | public String mLastVoiceSearchUrl; |
| 397 | /** |
| 398 | * The last title used for voice search. Needed to update the title bar |
| 399 | * when switching tabs. |
| 400 | */ |
| 401 | public String mLastVoiceSearchTitle; |
Leon Scroggins | 1fe13a5 | 2010-02-09 15:31:26 -0500 | [diff] [blame] | 402 | /** |
| 403 | * Whether the Intent which turned on voice search mode contained the |
| 404 | * String signifying that Google was the source. |
| 405 | */ |
| 406 | public boolean mSourceIsGoogle; |
| 407 | /** |
Leon Scroggins | 9df9497 | 2010-03-08 18:20:35 -0500 | [diff] [blame] | 408 | * List of headers to be passed into the WebView containing location |
| 409 | * information |
| 410 | */ |
| 411 | public ArrayList<Bundle> mHeaders; |
| 412 | /** |
Leon Scroggins | 0c75a8e | 2010-03-03 16:40:58 -0500 | [diff] [blame] | 413 | * The Intent used to invoke voice search. Placed on the |
| 414 | * WebHistoryItem so that when coming back to a previous voice search |
| 415 | * page we can again activate voice search. |
| 416 | */ |
Leon Scroggins | e10dde5 | 2010-03-08 19:53:03 -0500 | [diff] [blame] | 417 | public Intent mVoiceSearchIntent; |
Leon Scroggins | 0c75a8e | 2010-03-03 16:40:58 -0500 | [diff] [blame] | 418 | /** |
Leon Scroggins | 1fe13a5 | 2010-02-09 15:31:26 -0500 | [diff] [blame] | 419 | * String used to identify Google as the source of voice search. |
| 420 | */ |
| 421 | public static String SOURCE_IS_GOOGLE |
| 422 | = "android.speech.extras.SOURCE_IS_GOOGLE"; |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 423 | } |
| 424 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 425 | // Container class for the next error dialog that needs to be displayed |
| 426 | private class ErrorDialog { |
| 427 | public final int mTitle; |
| 428 | public final String mDescription; |
| 429 | public final int mError; |
| 430 | ErrorDialog(int title, String desc, int error) { |
| 431 | mTitle = title; |
| 432 | mDescription = desc; |
| 433 | mError = error; |
| 434 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 435 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 436 | |
| 437 | private void processNextError() { |
| 438 | if (mQueuedErrors == null) { |
| 439 | return; |
| 440 | } |
| 441 | // The first one is currently displayed so just remove it. |
| 442 | mQueuedErrors.removeFirst(); |
| 443 | if (mQueuedErrors.size() == 0) { |
| 444 | mQueuedErrors = null; |
| 445 | return; |
| 446 | } |
| 447 | showError(mQueuedErrors.getFirst()); |
| 448 | } |
| 449 | |
| 450 | private DialogInterface.OnDismissListener mDialogListener = |
| 451 | new DialogInterface.OnDismissListener() { |
| 452 | public void onDismiss(DialogInterface d) { |
| 453 | processNextError(); |
| 454 | } |
| 455 | }; |
| 456 | private LinkedList<ErrorDialog> mQueuedErrors; |
| 457 | |
| 458 | private void queueError(int err, String desc) { |
| 459 | if (mQueuedErrors == null) { |
| 460 | mQueuedErrors = new LinkedList<ErrorDialog>(); |
| 461 | } |
| 462 | for (ErrorDialog d : mQueuedErrors) { |
| 463 | if (d.mError == err) { |
| 464 | // Already saw a similar error, ignore the new one. |
| 465 | return; |
| 466 | } |
| 467 | } |
| 468 | ErrorDialog errDialog = new ErrorDialog( |
| 469 | err == WebViewClient.ERROR_FILE_NOT_FOUND ? |
| 470 | R.string.browserFrameFileErrorLabel : |
| 471 | R.string.browserFrameNetworkErrorLabel, |
| 472 | desc, err); |
| 473 | mQueuedErrors.addLast(errDialog); |
| 474 | |
| 475 | // Show the dialog now if the queue was empty and it is in foreground |
| 476 | if (mQueuedErrors.size() == 1 && mInForeground) { |
| 477 | showError(errDialog); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | private void showError(ErrorDialog errDialog) { |
| 482 | if (mInForeground) { |
| 483 | AlertDialog d = new AlertDialog.Builder(mActivity) |
| 484 | .setTitle(errDialog.mTitle) |
| 485 | .setMessage(errDialog.mDescription) |
| 486 | .setPositiveButton(R.string.ok, null) |
| 487 | .create(); |
| 488 | d.setOnDismissListener(mDialogListener); |
| 489 | d.show(); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | // ------------------------------------------------------------------------- |
| 494 | // WebViewClient implementation for the main WebView |
| 495 | // ------------------------------------------------------------------------- |
| 496 | |
| 497 | private final WebViewClient mWebViewClient = new WebViewClient() { |
Leon Scroggins | 4a64a8a | 2010-03-02 17:57:40 -0500 | [diff] [blame] | 498 | private Message mDontResend; |
| 499 | private Message mResend; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 500 | @Override |
| 501 | public void onPageStarted(WebView view, String url, Bitmap favicon) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 502 | mInPageLoad = true; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 503 | mPageLoadProgress = 0; |
| 504 | mCurrentState = new PageState(mActivity, |
| 505 | view.isPrivateBrowsingEnabled(), url, favicon); |
Kristian Monsen | 4dce3bf | 2010-02-02 13:37:09 +0000 | [diff] [blame] | 506 | mLoadStartTime = SystemClock.uptimeMillis(); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 507 | if (mVoiceSearchData != null |
| 508 | && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) { |
Leon Scroggins | 1fe13a5 | 2010-02-09 15:31:26 -0500 | [diff] [blame] | 509 | if (mVoiceSearchData.mSourceIsGoogle) { |
| 510 | Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT); |
| 511 | i.putExtra(LoggingEvents.EXTRA_FLUSH, true); |
| 512 | mActivity.sendBroadcast(i); |
| 513 | } |
Leon Scroggins III | 95d9bfd | 2010-09-14 14:02:36 -0400 | [diff] [blame] | 514 | revertVoiceSearchMode(); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 515 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 516 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 517 | |
| 518 | // If we start a touch icon load and then load a new page, we don't |
| 519 | // want to cancel the current touch icon loader. But, we do want to |
| 520 | // create a new one when the touch icon url is known. |
| 521 | if (mTouchIconLoader != null) { |
| 522 | mTouchIconLoader.mTab = null; |
| 523 | mTouchIconLoader = null; |
| 524 | } |
| 525 | |
| 526 | // reset the error console |
| 527 | if (mErrorConsole != null) { |
| 528 | mErrorConsole.clearErrorMessages(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 529 | if (mWebViewController.shouldShowErrorConsole()) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 530 | mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE); |
| 531 | } |
| 532 | } |
| 533 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 534 | // finally update the UI in the activity if it is in the foreground |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame^] | 535 | mWebViewController.onPageStarted(Tab.this, view, favicon); |
Leon Scroggins | 4cd9779 | 2010-12-03 15:31:56 -0500 | [diff] [blame] | 536 | |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 537 | updateBookmarkedStatus(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | @Override |
| 541 | public void onPageFinished(WebView view, String url) { |
John Reck | 5b69184 | 2010-11-29 11:21:13 -0800 | [diff] [blame] | 542 | if (!isPrivateBrowsingEnabled()) { |
| 543 | LogTag.logPageFinishedLoading( |
| 544 | url, SystemClock.uptimeMillis() - mLoadStartTime); |
| 545 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 546 | mInPageLoad = false; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 547 | // Sync state (in case of stop/timeout) |
| 548 | mCurrentState.mUrl = view.getUrl(); |
John Reck | 6c702ee | 2011-01-07 09:41:53 -0800 | [diff] [blame] | 549 | if (mCurrentState.mUrl == null) { |
| 550 | mCurrentState.mUrl = url != null ? url : ""; |
| 551 | } |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 552 | mCurrentState.mTitle = view.getTitle(); |
| 553 | mCurrentState.mFavicon = view.getFavicon(); |
| 554 | if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) { |
| 555 | // In case we stop when loading an HTTPS page from an HTTP page |
| 556 | // but before a provisional load occurred |
| 557 | mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE; |
| 558 | } |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame^] | 559 | mWebViewController.onPageFinished(Tab.this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | // return true if want to hijack the url to let another app to handle it |
| 563 | @Override |
| 564 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
Leon Scroggins III | c1f5ae2 | 2010-06-29 17:11:29 -0400 | [diff] [blame] | 565 | if (voiceSearchSourceIsGoogle()) { |
| 566 | // This method is called when the user clicks on a link. |
| 567 | // VoiceSearchMode is turned off when the user leaves the |
| 568 | // Google results page, so at this point the user must be on |
| 569 | // that page. If the user clicked a link on that page, assume |
| 570 | // that the voice search was effective, and broadcast an Intent |
| 571 | // so a receiver can take note of that fact. |
| 572 | Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT); |
| 573 | logIntent.putExtra(LoggingEvents.EXTRA_EVENT, |
| 574 | LoggingEvents.VoiceSearch.RESULT_CLICKED); |
| 575 | mActivity.sendBroadcast(logIntent); |
| 576 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 577 | if (mInForeground) { |
Michael Kolb | 18eb377 | 2010-12-10 14:29:51 -0800 | [diff] [blame] | 578 | return mWebViewController.shouldOverrideUrlLoading(Tab.this, |
| 579 | view, url); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 580 | } else { |
| 581 | return false; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Updates the lock icon. This method is called when we discover another |
| 587 | * resource to be loaded for this page (for example, javascript). While |
| 588 | * we update the icon type, we do not update the lock icon itself until |
| 589 | * we are done loading, it is slightly more secure this way. |
| 590 | */ |
| 591 | @Override |
| 592 | public void onLoadResource(WebView view, String url) { |
| 593 | if (url != null && url.length() > 0) { |
| 594 | // It is only if the page claims to be secure that we may have |
| 595 | // to update the lock: |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 596 | if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 597 | // If NOT a 'safe' url, change the lock to mixed content! |
| 598 | if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url) |
| 599 | || URLUtil.isAboutUrl(url))) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 600 | mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | /** |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 607 | * Show a dialog informing the user of the network error reported by |
| 608 | * WebCore if it is in the foreground. |
| 609 | */ |
| 610 | @Override |
| 611 | public void onReceivedError(WebView view, int errorCode, |
| 612 | String description, String failingUrl) { |
| 613 | if (errorCode != WebViewClient.ERROR_HOST_LOOKUP && |
| 614 | errorCode != WebViewClient.ERROR_CONNECT && |
| 615 | errorCode != WebViewClient.ERROR_BAD_URL && |
| 616 | errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME && |
| 617 | errorCode != WebViewClient.ERROR_FILE) { |
| 618 | queueError(errorCode, description); |
| 619 | } |
Jeff Hamilton | 47654f4 | 2010-09-07 09:57:51 -0500 | [diff] [blame] | 620 | |
| 621 | // Don't log URLs when in private browsing mode |
Rob Tsuk | f8bdfce | 2010-10-07 15:41:16 -0700 | [diff] [blame] | 622 | if (!isPrivateBrowsingEnabled()) { |
Jeff Hamilton | 47654f4 | 2010-09-07 09:57:51 -0500 | [diff] [blame] | 623 | Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl |
| 624 | + " " + description); |
| 625 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Check with the user if it is ok to resend POST data as the page they |
| 630 | * are trying to navigate to is the result of a POST. |
| 631 | */ |
| 632 | @Override |
| 633 | public void onFormResubmission(WebView view, final Message dontResend, |
| 634 | final Message resend) { |
| 635 | if (!mInForeground) { |
| 636 | dontResend.sendToTarget(); |
| 637 | return; |
| 638 | } |
Leon Scroggins | 4a64a8a | 2010-03-02 17:57:40 -0500 | [diff] [blame] | 639 | if (mDontResend != null) { |
| 640 | Log.w(LOGTAG, "onFormResubmission should not be called again " |
| 641 | + "while dialog is still up"); |
| 642 | dontResend.sendToTarget(); |
| 643 | return; |
| 644 | } |
| 645 | mDontResend = dontResend; |
| 646 | mResend = resend; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 647 | new AlertDialog.Builder(mActivity).setTitle( |
| 648 | R.string.browserFrameFormResubmitLabel).setMessage( |
| 649 | R.string.browserFrameFormResubmitMessage) |
| 650 | .setPositiveButton(R.string.ok, |
| 651 | new DialogInterface.OnClickListener() { |
| 652 | public void onClick(DialogInterface dialog, |
| 653 | int which) { |
Leon Scroggins | 4a64a8a | 2010-03-02 17:57:40 -0500 | [diff] [blame] | 654 | if (mResend != null) { |
| 655 | mResend.sendToTarget(); |
| 656 | mResend = null; |
| 657 | mDontResend = null; |
| 658 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 659 | } |
| 660 | }).setNegativeButton(R.string.cancel, |
| 661 | new DialogInterface.OnClickListener() { |
| 662 | public void onClick(DialogInterface dialog, |
| 663 | int which) { |
Leon Scroggins | 4a64a8a | 2010-03-02 17:57:40 -0500 | [diff] [blame] | 664 | if (mDontResend != null) { |
| 665 | mDontResend.sendToTarget(); |
| 666 | mResend = null; |
| 667 | mDontResend = null; |
| 668 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 669 | } |
| 670 | }).setOnCancelListener(new OnCancelListener() { |
| 671 | public void onCancel(DialogInterface dialog) { |
Leon Scroggins | 4a64a8a | 2010-03-02 17:57:40 -0500 | [diff] [blame] | 672 | if (mDontResend != null) { |
| 673 | mDontResend.sendToTarget(); |
| 674 | mResend = null; |
| 675 | mDontResend = null; |
| 676 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 677 | } |
| 678 | }).show(); |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * Insert the url into the visited history database. |
| 683 | * @param url The url to be inserted. |
| 684 | * @param isReload True if this url is being reloaded. |
| 685 | * FIXME: Not sure what to do when reloading the page. |
| 686 | */ |
| 687 | @Override |
| 688 | public void doUpdateVisitedHistory(WebView view, String url, |
| 689 | boolean isReload) { |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame^] | 690 | mWebViewController.doUpdateVisitedHistory(Tab.this, isReload); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Displays SSL error(s) dialog to the user. |
| 695 | */ |
| 696 | @Override |
| 697 | public void onReceivedSslError(final WebView view, |
| 698 | final SslErrorHandler handler, final SslError error) { |
| 699 | if (!mInForeground) { |
| 700 | handler.cancel(); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 701 | setLockIconType(LockIcon.LOCK_ICON_UNSECURE); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 702 | return; |
| 703 | } |
| 704 | if (BrowserSettings.getInstance().showSecurityWarnings()) { |
| 705 | final LayoutInflater factory = |
| 706 | LayoutInflater.from(mActivity); |
| 707 | final View warningsView = |
| 708 | factory.inflate(R.layout.ssl_warnings, null); |
| 709 | final LinearLayout placeholder = |
| 710 | (LinearLayout)warningsView.findViewById(R.id.placeholder); |
| 711 | |
| 712 | if (error.hasError(SslError.SSL_UNTRUSTED)) { |
| 713 | LinearLayout ll = (LinearLayout)factory |
| 714 | .inflate(R.layout.ssl_warning, null); |
| 715 | ((TextView)ll.findViewById(R.id.warning)) |
| 716 | .setText(R.string.ssl_untrusted); |
| 717 | placeholder.addView(ll); |
| 718 | } |
| 719 | |
| 720 | if (error.hasError(SslError.SSL_IDMISMATCH)) { |
| 721 | LinearLayout ll = (LinearLayout)factory |
| 722 | .inflate(R.layout.ssl_warning, null); |
| 723 | ((TextView)ll.findViewById(R.id.warning)) |
| 724 | .setText(R.string.ssl_mismatch); |
| 725 | placeholder.addView(ll); |
| 726 | } |
| 727 | |
| 728 | if (error.hasError(SslError.SSL_EXPIRED)) { |
| 729 | LinearLayout ll = (LinearLayout)factory |
| 730 | .inflate(R.layout.ssl_warning, null); |
| 731 | ((TextView)ll.findViewById(R.id.warning)) |
| 732 | .setText(R.string.ssl_expired); |
| 733 | placeholder.addView(ll); |
| 734 | } |
| 735 | |
| 736 | if (error.hasError(SslError.SSL_NOTYETVALID)) { |
| 737 | LinearLayout ll = (LinearLayout)factory |
| 738 | .inflate(R.layout.ssl_warning, null); |
| 739 | ((TextView)ll.findViewById(R.id.warning)) |
| 740 | .setText(R.string.ssl_not_yet_valid); |
| 741 | placeholder.addView(ll); |
| 742 | } |
| 743 | |
| 744 | new AlertDialog.Builder(mActivity).setTitle( |
| 745 | R.string.security_warning).setIcon( |
| 746 | android.R.drawable.ic_dialog_alert).setView( |
| 747 | warningsView).setPositiveButton(R.string.ssl_continue, |
| 748 | new DialogInterface.OnClickListener() { |
| 749 | public void onClick(DialogInterface dialog, |
| 750 | int whichButton) { |
| 751 | handler.proceed(); |
| 752 | } |
| 753 | }).setNeutralButton(R.string.view_certificate, |
| 754 | new DialogInterface.OnClickListener() { |
| 755 | public void onClick(DialogInterface dialog, |
| 756 | int whichButton) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 757 | mWebViewController.showSslCertificateOnError(view, |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 758 | handler, error); |
| 759 | } |
Ben Murdoch | a49b829 | 2010-11-16 11:56:04 +0000 | [diff] [blame] | 760 | }).setNegativeButton(R.string.ssl_go_back, |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 761 | new DialogInterface.OnClickListener() { |
| 762 | public void onClick(DialogInterface dialog, |
| 763 | int whichButton) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 764 | dialog.cancel(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 765 | } |
| 766 | }).setOnCancelListener( |
| 767 | new DialogInterface.OnCancelListener() { |
| 768 | public void onCancel(DialogInterface dialog) { |
| 769 | handler.cancel(); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 770 | setLockIconType(LockIcon.LOCK_ICON_UNSECURE); |
| 771 | mWebViewController.onUserCanceledSsl(Tab.this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 772 | } |
| 773 | }).show(); |
| 774 | } else { |
| 775 | handler.proceed(); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | /** |
| 780 | * Handles an HTTP authentication request. |
| 781 | * |
| 782 | * @param handler The authentication handler |
| 783 | * @param host The host |
| 784 | * @param realm The realm |
| 785 | */ |
| 786 | @Override |
| 787 | public void onReceivedHttpAuthRequest(WebView view, |
| 788 | final HttpAuthHandler handler, final String host, |
| 789 | final String realm) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 790 | mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | @Override |
| 794 | public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) { |
| 795 | if (!mInForeground) { |
| 796 | return false; |
| 797 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 798 | return mWebViewController.shouldOverrideKeyEvent(event); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | @Override |
| 802 | public void onUnhandledKeyEvent(WebView view, KeyEvent event) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 803 | if (!mInForeground) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 804 | return; |
| 805 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 806 | mWebViewController.onUnhandledKeyEvent(event); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 807 | } |
| 808 | }; |
| 809 | |
| 810 | // ------------------------------------------------------------------------- |
| 811 | // WebChromeClient implementation for the main WebView |
| 812 | // ------------------------------------------------------------------------- |
| 813 | |
| 814 | private final WebChromeClient mWebChromeClient = new WebChromeClient() { |
| 815 | // Helper method to create a new tab or sub window. |
| 816 | private void createWindow(final boolean dialog, final Message msg) { |
| 817 | WebView.WebViewTransport transport = |
| 818 | (WebView.WebViewTransport) msg.obj; |
| 819 | if (dialog) { |
| 820 | createSubWindow(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 821 | mWebViewController.attachSubWindow(Tab.this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 822 | transport.setWebView(mSubView); |
| 823 | } else { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 824 | final Tab newTab = mWebViewController.openTabAndShow( |
Michael Kolb | 18eb377 | 2010-12-10 14:29:51 -0800 | [diff] [blame] | 825 | Tab.this, |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 826 | IntentHandler.EMPTY_URL_DATA, false, null); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 827 | if (newTab != Tab.this) { |
| 828 | Tab.this.addChildTab(newTab); |
| 829 | } |
| 830 | transport.setWebView(newTab.getWebView()); |
| 831 | } |
| 832 | msg.sendToTarget(); |
| 833 | } |
| 834 | |
| 835 | @Override |
| 836 | public boolean onCreateWindow(WebView view, final boolean dialog, |
| 837 | final boolean userGesture, final Message resultMsg) { |
| 838 | // only allow new window or sub window for the foreground case |
| 839 | if (!mInForeground) { |
| 840 | return false; |
| 841 | } |
| 842 | // Short-circuit if we can't create any more tabs or sub windows. |
| 843 | if (dialog && mSubView != null) { |
| 844 | new AlertDialog.Builder(mActivity) |
| 845 | .setTitle(R.string.too_many_subwindows_dialog_title) |
| 846 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 847 | .setMessage(R.string.too_many_subwindows_dialog_message) |
| 848 | .setPositiveButton(R.string.ok, null) |
| 849 | .show(); |
| 850 | return false; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 851 | } else if (!mWebViewController.getTabControl().canCreateNewTab()) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 852 | new AlertDialog.Builder(mActivity) |
| 853 | .setTitle(R.string.too_many_windows_dialog_title) |
| 854 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 855 | .setMessage(R.string.too_many_windows_dialog_message) |
| 856 | .setPositiveButton(R.string.ok, null) |
| 857 | .show(); |
| 858 | return false; |
| 859 | } |
| 860 | |
| 861 | // Short-circuit if this was a user gesture. |
| 862 | if (userGesture) { |
| 863 | createWindow(dialog, resultMsg); |
| 864 | return true; |
| 865 | } |
| 866 | |
| 867 | // Allow the popup and create the appropriate window. |
| 868 | final AlertDialog.OnClickListener allowListener = |
| 869 | new AlertDialog.OnClickListener() { |
| 870 | public void onClick(DialogInterface d, |
| 871 | int which) { |
| 872 | createWindow(dialog, resultMsg); |
| 873 | } |
| 874 | }; |
| 875 | |
| 876 | // Block the popup by returning a null WebView. |
| 877 | final AlertDialog.OnClickListener blockListener = |
| 878 | new AlertDialog.OnClickListener() { |
| 879 | public void onClick(DialogInterface d, int which) { |
| 880 | resultMsg.sendToTarget(); |
| 881 | } |
| 882 | }; |
| 883 | |
| 884 | // Build a confirmation dialog to display to the user. |
| 885 | final AlertDialog d = |
| 886 | new AlertDialog.Builder(mActivity) |
| 887 | .setTitle(R.string.attention) |
| 888 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 889 | .setMessage(R.string.popup_window_attempt) |
| 890 | .setPositiveButton(R.string.allow, allowListener) |
| 891 | .setNegativeButton(R.string.block, blockListener) |
| 892 | .setCancelable(false) |
| 893 | .create(); |
| 894 | |
| 895 | // Show the confirmation dialog. |
| 896 | d.show(); |
| 897 | return true; |
| 898 | } |
| 899 | |
| 900 | @Override |
Patrick Scott | eb5061b | 2009-11-18 15:00:30 -0500 | [diff] [blame] | 901 | public void onRequestFocus(WebView view) { |
| 902 | if (!mInForeground) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 903 | mWebViewController.switchToTab(mWebViewController.getTabControl().getTabIndex( |
Patrick Scott | eb5061b | 2009-11-18 15:00:30 -0500 | [diff] [blame] | 904 | Tab.this)); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | @Override |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 909 | public void onCloseWindow(WebView window) { |
| 910 | if (mParentTab != null) { |
| 911 | // JavaScript can only close popup window. |
| 912 | if (mInForeground) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 913 | mWebViewController.switchToTab(mWebViewController.getTabControl() |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 914 | .getTabIndex(mParentTab)); |
| 915 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 916 | mWebViewController.closeTab(Tab.this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | |
| 920 | @Override |
| 921 | public void onProgressChanged(WebView view, int newProgress) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 922 | mPageLoadProgress = newProgress; |
| 923 | mWebViewController.onProgressChanged(Tab.this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | @Override |
Leon Scroggins | 21d9b90 | 2010-03-11 09:33:11 -0500 | [diff] [blame] | 927 | public void onReceivedTitle(WebView view, final String title) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 928 | mCurrentState.mTitle = title; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 929 | mWebViewController.onReceivedTitle(Tab.this, title); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | @Override |
| 933 | public void onReceivedIcon(WebView view, Bitmap icon) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 934 | mCurrentState.mFavicon = icon; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 935 | mWebViewController.onFavicon(Tab.this, view, icon); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | @Override |
| 939 | public void onReceivedTouchIconUrl(WebView view, String url, |
| 940 | boolean precomposed) { |
| 941 | final ContentResolver cr = mActivity.getContentResolver(); |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 942 | // Let precomposed icons take precedence over non-composed |
| 943 | // icons. |
| 944 | if (precomposed && mTouchIconLoader != null) { |
| 945 | mTouchIconLoader.cancel(false); |
| 946 | mTouchIconLoader = null; |
| 947 | } |
| 948 | // Have only one async task at a time. |
| 949 | if (mTouchIconLoader == null) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 950 | mTouchIconLoader = new DownloadTouchIcon(Tab.this, |
| 951 | mActivity, cr, view); |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 952 | mTouchIconLoader.execute(url); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | |
| 956 | @Override |
| 957 | public void onShowCustomView(View view, |
| 958 | WebChromeClient.CustomViewCallback callback) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 959 | if (mInForeground) mWebViewController.showCustomView(Tab.this, view, |
| 960 | callback); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | @Override |
| 964 | public void onHideCustomView() { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 965 | if (mInForeground) mWebViewController.hideCustomView(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | /** |
| 969 | * The origin has exceeded its database quota. |
| 970 | * @param url the URL that exceeded the quota |
| 971 | * @param databaseIdentifier the identifier of the database on which the |
| 972 | * transaction that caused the quota overflow was run |
| 973 | * @param currentQuota the current quota for the origin. |
| 974 | * @param estimatedSize the estimated size of the database. |
| 975 | * @param totalUsedQuota is the sum of all origins' quota. |
| 976 | * @param quotaUpdater The callback to run when a decision to allow or |
| 977 | * deny quota has been made. Don't forget to call this! |
| 978 | */ |
| 979 | @Override |
| 980 | public void onExceededDatabaseQuota(String url, |
| 981 | String databaseIdentifier, long currentQuota, long estimatedSize, |
| 982 | long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { |
| 983 | BrowserSettings.getInstance().getWebStorageSizeManager() |
| 984 | .onExceededDatabaseQuota(url, databaseIdentifier, |
| 985 | currentQuota, estimatedSize, totalUsedQuota, |
| 986 | quotaUpdater); |
| 987 | } |
| 988 | |
| 989 | /** |
| 990 | * The Application Cache has exceeded its max size. |
| 991 | * @param spaceNeeded is the amount of disk space that would be needed |
| 992 | * in order for the last appcache operation to succeed. |
| 993 | * @param totalUsedQuota is the sum of all origins' quota. |
| 994 | * @param quotaUpdater A callback to inform the WebCore thread that a |
| 995 | * new app cache size is available. This callback must always |
| 996 | * be executed at some point to ensure that the sleeping |
| 997 | * WebCore thread is woken up. |
| 998 | */ |
| 999 | @Override |
| 1000 | public void onReachedMaxAppCacheSize(long spaceNeeded, |
| 1001 | long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { |
| 1002 | BrowserSettings.getInstance().getWebStorageSizeManager() |
| 1003 | .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota, |
| 1004 | quotaUpdater); |
| 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * Instructs the browser to show a prompt to ask the user to set the |
| 1009 | * Geolocation permission state for the specified origin. |
| 1010 | * @param origin The origin for which Geolocation permissions are |
| 1011 | * requested. |
| 1012 | * @param callback The callback to call once the user has set the |
| 1013 | * Geolocation permission state. |
| 1014 | */ |
| 1015 | @Override |
| 1016 | public void onGeolocationPermissionsShowPrompt(String origin, |
| 1017 | GeolocationPermissions.Callback callback) { |
| 1018 | if (mInForeground) { |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 1019 | getGeolocationPermissionsPrompt().show(origin, callback); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | /** |
| 1024 | * Instructs the browser to hide the Geolocation permissions prompt. |
| 1025 | */ |
| 1026 | @Override |
| 1027 | public void onGeolocationPermissionsHidePrompt() { |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 1028 | if (mInForeground && mGeolocationPermissionsPrompt != null) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1029 | mGeolocationPermissionsPrompt.hide(); |
| 1030 | } |
| 1031 | } |
| 1032 | |
Ben Murdoch | 65acc35 | 2009-11-19 18:16:04 +0000 | [diff] [blame] | 1033 | /* Adds a JavaScript error message to the system log and if the JS |
| 1034 | * console is enabled in the about:debug options, to that console |
| 1035 | * also. |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 1036 | * @param consoleMessage the message object. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1037 | */ |
| 1038 | @Override |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 1039 | public boolean onConsoleMessage(ConsoleMessage consoleMessage) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1040 | if (mInForeground) { |
| 1041 | // call getErrorConsole(true) so it will create one if needed |
| 1042 | ErrorConsoleView errorConsole = getErrorConsole(true); |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 1043 | errorConsole.addErrorMessage(consoleMessage); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1044 | if (mWebViewController.shouldShowErrorConsole() |
| 1045 | && errorConsole.getShowState() != |
| 1046 | ErrorConsoleView.SHOW_MAXIMIZED) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1047 | errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED); |
| 1048 | } |
| 1049 | } |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 1050 | |
Jeff Hamilton | 47654f4 | 2010-09-07 09:57:51 -0500 | [diff] [blame] | 1051 | // Don't log console messages in private browsing mode |
Rob Tsuk | f8bdfce | 2010-10-07 15:41:16 -0700 | [diff] [blame] | 1052 | if (isPrivateBrowsingEnabled()) return true; |
Jeff Hamilton | 47654f4 | 2010-09-07 09:57:51 -0500 | [diff] [blame] | 1053 | |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 1054 | String message = "Console: " + consoleMessage.message() + " " |
| 1055 | + consoleMessage.sourceId() + ":" |
| 1056 | + consoleMessage.lineNumber(); |
| 1057 | |
| 1058 | switch (consoleMessage.messageLevel()) { |
| 1059 | case TIP: |
| 1060 | Log.v(CONSOLE_LOGTAG, message); |
| 1061 | break; |
| 1062 | case LOG: |
| 1063 | Log.i(CONSOLE_LOGTAG, message); |
| 1064 | break; |
| 1065 | case WARNING: |
| 1066 | Log.w(CONSOLE_LOGTAG, message); |
| 1067 | break; |
| 1068 | case ERROR: |
| 1069 | Log.e(CONSOLE_LOGTAG, message); |
| 1070 | break; |
| 1071 | case DEBUG: |
| 1072 | Log.d(CONSOLE_LOGTAG, message); |
| 1073 | break; |
| 1074 | } |
| 1075 | |
| 1076 | return true; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /** |
| 1080 | * Ask the browser for an icon to represent a <video> element. |
| 1081 | * This icon will be used if the Web page did not specify a poster attribute. |
| 1082 | * @return Bitmap The icon or null if no such icon is available. |
| 1083 | */ |
| 1084 | @Override |
| 1085 | public Bitmap getDefaultVideoPoster() { |
| 1086 | if (mInForeground) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1087 | return mWebViewController.getDefaultVideoPoster(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1088 | } |
| 1089 | return null; |
| 1090 | } |
| 1091 | |
| 1092 | /** |
| 1093 | * Ask the host application for a custom progress view to show while |
| 1094 | * a <video> is loading. |
| 1095 | * @return View The progress view. |
| 1096 | */ |
| 1097 | @Override |
| 1098 | public View getVideoLoadingProgressView() { |
| 1099 | if (mInForeground) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1100 | return mWebViewController.getVideoLoadingProgressView(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1101 | } |
| 1102 | return null; |
| 1103 | } |
| 1104 | |
| 1105 | @Override |
Ben Murdoch | 62b1b7e | 2010-05-19 20:38:56 +0100 | [diff] [blame] | 1106 | public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1107 | if (mInForeground) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1108 | mWebViewController.openFileChooser(uploadMsg, acceptType); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1109 | } else { |
| 1110 | uploadMsg.onReceiveValue(null); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | /** |
| 1115 | * Deliver a list of already-visited URLs |
| 1116 | */ |
| 1117 | @Override |
| 1118 | public void getVisitedHistory(final ValueCallback<String[]> callback) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1119 | mWebViewController.getVisitedHistory(callback); |
| 1120 | } |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 1121 | |
| 1122 | @Override |
| 1123 | public void setupAutoFill(Message message) { |
| 1124 | // Prompt the user to set up their profile. |
| 1125 | final Message msg = message; |
| 1126 | AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); |
| 1127 | builder.setMessage(R.string.autofill_setup_dialog_message) |
| 1128 | .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { |
| 1129 | @Override |
| 1130 | public void onClick(DialogInterface dialog, int id) { |
| 1131 | // Take user to the AutoFill profile editor. When they return, |
| 1132 | // we will send the message that we pass here which will trigger |
| 1133 | // the form to get filled out with their new profile. |
| 1134 | mWebViewController.setupAutoFill(msg); |
| 1135 | } |
| 1136 | }) |
| 1137 | .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { |
| 1138 | @Override |
| 1139 | public void onClick(DialogInterface dialog, int id) { |
| 1140 | // Disable autofill and show a toast with how to turn it on again. |
| 1141 | BrowserSettings s = BrowserSettings.getInstance(); |
| 1142 | s.addObserver(mMainView.getSettings()); |
| 1143 | s.disableAutoFill(mActivity); |
| 1144 | s.update(); |
| 1145 | Toast.makeText(mActivity, R.string.autofill_setup_dialog_negative_toast, |
| 1146 | Toast.LENGTH_LONG).show(); |
| 1147 | } |
| 1148 | }).show(); |
| 1149 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1150 | }; |
| 1151 | |
| 1152 | // ------------------------------------------------------------------------- |
| 1153 | // WebViewClient implementation for the sub window |
| 1154 | // ------------------------------------------------------------------------- |
| 1155 | |
| 1156 | // Subclass of WebViewClient used in subwindows to notify the main |
| 1157 | // WebViewClient of certain WebView activities. |
| 1158 | private static class SubWindowClient extends WebViewClient { |
| 1159 | // The main WebViewClient. |
| 1160 | private final WebViewClient mClient; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1161 | private final WebViewController mController; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1162 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1163 | SubWindowClient(WebViewClient client, WebViewController controller) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1164 | mClient = client; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1165 | mController = controller; |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 1166 | } |
| 1167 | @Override |
| 1168 | public void onPageStarted(WebView view, String url, Bitmap favicon) { |
| 1169 | // Unlike the others, do not call mClient's version, which would |
| 1170 | // change the progress bar. However, we do want to remove the |
Cary Clark | 01cfcdd | 2010-06-04 16:36:45 -0400 | [diff] [blame] | 1171 | // find or select dialog. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1172 | mController.endActionMode(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1173 | } |
| 1174 | @Override |
| 1175 | public void doUpdateVisitedHistory(WebView view, String url, |
| 1176 | boolean isReload) { |
| 1177 | mClient.doUpdateVisitedHistory(view, url, isReload); |
| 1178 | } |
| 1179 | @Override |
| 1180 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| 1181 | return mClient.shouldOverrideUrlLoading(view, url); |
| 1182 | } |
| 1183 | @Override |
| 1184 | public void onReceivedSslError(WebView view, SslErrorHandler handler, |
| 1185 | SslError error) { |
| 1186 | mClient.onReceivedSslError(view, handler, error); |
| 1187 | } |
| 1188 | @Override |
| 1189 | public void onReceivedHttpAuthRequest(WebView view, |
| 1190 | HttpAuthHandler handler, String host, String realm) { |
| 1191 | mClient.onReceivedHttpAuthRequest(view, handler, host, realm); |
| 1192 | } |
| 1193 | @Override |
| 1194 | public void onFormResubmission(WebView view, Message dontResend, |
| 1195 | Message resend) { |
| 1196 | mClient.onFormResubmission(view, dontResend, resend); |
| 1197 | } |
| 1198 | @Override |
| 1199 | public void onReceivedError(WebView view, int errorCode, |
| 1200 | String description, String failingUrl) { |
| 1201 | mClient.onReceivedError(view, errorCode, description, failingUrl); |
| 1202 | } |
| 1203 | @Override |
| 1204 | public boolean shouldOverrideKeyEvent(WebView view, |
| 1205 | android.view.KeyEvent event) { |
| 1206 | return mClient.shouldOverrideKeyEvent(view, event); |
| 1207 | } |
| 1208 | @Override |
| 1209 | public void onUnhandledKeyEvent(WebView view, |
| 1210 | android.view.KeyEvent event) { |
| 1211 | mClient.onUnhandledKeyEvent(view, event); |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | // ------------------------------------------------------------------------- |
| 1216 | // WebChromeClient implementation for the sub window |
| 1217 | // ------------------------------------------------------------------------- |
| 1218 | |
| 1219 | private class SubWindowChromeClient extends WebChromeClient { |
| 1220 | // The main WebChromeClient. |
| 1221 | private final WebChromeClient mClient; |
| 1222 | |
| 1223 | SubWindowChromeClient(WebChromeClient client) { |
| 1224 | mClient = client; |
| 1225 | } |
| 1226 | @Override |
| 1227 | public void onProgressChanged(WebView view, int newProgress) { |
| 1228 | mClient.onProgressChanged(view, newProgress); |
| 1229 | } |
| 1230 | @Override |
| 1231 | public boolean onCreateWindow(WebView view, boolean dialog, |
| 1232 | boolean userGesture, android.os.Message resultMsg) { |
| 1233 | return mClient.onCreateWindow(view, dialog, userGesture, resultMsg); |
| 1234 | } |
| 1235 | @Override |
| 1236 | public void onCloseWindow(WebView window) { |
| 1237 | if (window != mSubView) { |
| 1238 | Log.e(LOGTAG, "Can't close the window"); |
| 1239 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1240 | mWebViewController.dismissSubWindow(Tab.this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | // ------------------------------------------------------------------------- |
| 1245 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1246 | // TODO temporarily use activity here |
| 1247 | // remove later |
| 1248 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1249 | // Construct a new tab |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1250 | Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId, |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1251 | String url) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1252 | mWebViewController = wvcontroller; |
| 1253 | mActivity = mWebViewController.getActivity(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1254 | mCloseOnExit = closeOnExit; |
| 1255 | mAppId = appId; |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 1256 | mDataController = DataController.getInstance(mActivity); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1257 | mCurrentState = new PageState(mActivity, w.isPrivateBrowsingEnabled()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1258 | mInPageLoad = false; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1259 | mInForeground = false; |
| 1260 | |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1261 | mDownloadListener = new DownloadListener() { |
| 1262 | public void onDownloadStart(String url, String userAgent, |
| 1263 | String contentDisposition, String mimetype, |
| 1264 | long contentLength) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1265 | mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition, |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1266 | mimetype, contentLength); |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1267 | } |
| 1268 | }; |
Leon Scroggins | 0c75a8e | 2010-03-03 16:40:58 -0500 | [diff] [blame] | 1269 | mWebBackForwardListClient = new WebBackForwardListClient() { |
| 1270 | @Override |
| 1271 | public void onNewHistoryItem(WebHistoryItem item) { |
| 1272 | if (isInVoiceSearchMode()) { |
| 1273 | item.setCustomData(mVoiceSearchData.mVoiceSearchIntent); |
| 1274 | } |
| 1275 | } |
| 1276 | @Override |
| 1277 | public void onIndexChanged(WebHistoryItem item, int index) { |
| 1278 | Object data = item.getCustomData(); |
| 1279 | if (data != null && data instanceof Intent) { |
| 1280 | activateVoiceSearchMode((Intent) data); |
| 1281 | } |
| 1282 | } |
| 1283 | }; |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1284 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1285 | setWebView(w); |
| 1286 | } |
| 1287 | |
| 1288 | /** |
| 1289 | * Sets the WebView for this tab, correctly removing the old WebView from |
| 1290 | * the container view. |
| 1291 | */ |
| 1292 | void setWebView(WebView w) { |
| 1293 | if (mMainView == w) { |
| 1294 | return; |
| 1295 | } |
Michael Kolb | a713ec8 | 2010-11-29 17:27:06 -0800 | [diff] [blame] | 1296 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1297 | // If the WebView is changing, the page will be reloaded, so any ongoing |
| 1298 | // Geolocation permission requests are void. |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 1299 | if (mGeolocationPermissionsPrompt != null) { |
| 1300 | mGeolocationPermissionsPrompt.hide(); |
| 1301 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1302 | |
Michael Kolb | a713ec8 | 2010-11-29 17:27:06 -0800 | [diff] [blame] | 1303 | mWebViewController.onSetWebView(this, w); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1304 | |
| 1305 | // set the new one |
| 1306 | mMainView = w; |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1307 | // attach the WebViewClient, WebChromeClient and DownloadListener |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1308 | if (mMainView != null) { |
| 1309 | mMainView.setWebViewClient(mWebViewClient); |
| 1310 | mMainView.setWebChromeClient(mWebChromeClient); |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1311 | // Attach DownloadManager so that downloads can start in an active |
| 1312 | // or a non-active window. This can happen when going to a site that |
| 1313 | // does a redirect after a period of time. The user could have |
| 1314 | // switched to another tab while waiting for the download to start. |
| 1315 | mMainView.setDownloadListener(mDownloadListener); |
Leon Scroggins | 0c75a8e | 2010-03-03 16:40:58 -0500 | [diff] [blame] | 1316 | mMainView.setWebBackForwardListClient(mWebBackForwardListClient); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | /** |
| 1321 | * Destroy the tab's main WebView and subWindow if any |
| 1322 | */ |
| 1323 | void destroy() { |
| 1324 | if (mMainView != null) { |
| 1325 | dismissSubWindow(); |
| 1326 | BrowserSettings.getInstance().deleteObserver(mMainView.getSettings()); |
| 1327 | // save the WebView to call destroy() after detach it from the tab |
| 1328 | WebView webView = mMainView; |
| 1329 | setWebView(null); |
| 1330 | webView.destroy(); |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | /** |
| 1335 | * Remove the tab from the parent |
| 1336 | */ |
| 1337 | void removeFromTree() { |
| 1338 | // detach the children |
| 1339 | if (mChildTabs != null) { |
| 1340 | for(Tab t : mChildTabs) { |
| 1341 | t.setParentTab(null); |
| 1342 | } |
| 1343 | } |
| 1344 | // remove itself from the parent list |
| 1345 | if (mParentTab != null) { |
| 1346 | mParentTab.mChildTabs.remove(this); |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | /** |
| 1351 | * Create a new subwindow unless a subwindow already exists. |
| 1352 | * @return True if a new subwindow was created. False if one already exists. |
| 1353 | */ |
| 1354 | boolean createSubWindow() { |
| 1355 | if (mSubView == null) { |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 1356 | mWebViewController.createSubWindow(this); |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 1357 | mSubView.setWebViewClient(new SubWindowClient(mWebViewClient, |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1358 | mWebViewController)); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1359 | mSubView.setWebChromeClient(new SubWindowChromeClient( |
| 1360 | mWebChromeClient)); |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1361 | // Set a different DownloadListener for the mSubView, since it will |
| 1362 | // just need to dismiss the mSubView, rather than close the Tab |
| 1363 | mSubView.setDownloadListener(new DownloadListener() { |
| 1364 | public void onDownloadStart(String url, String userAgent, |
| 1365 | String contentDisposition, String mimetype, |
| 1366 | long contentLength) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1367 | mWebViewController.onDownloadStart(Tab.this, url, userAgent, |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1368 | contentDisposition, mimetype, contentLength); |
| 1369 | if (mSubView.copyBackForwardList().getSize() == 0) { |
| 1370 | // This subwindow was opened for the sole purpose of |
| 1371 | // downloading a file. Remove it. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1372 | mWebViewController.dismissSubWindow(Tab.this); |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | }); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1376 | mSubView.setOnCreateContextMenuListener(mActivity); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1377 | return true; |
| 1378 | } |
| 1379 | return false; |
| 1380 | } |
| 1381 | |
| 1382 | /** |
| 1383 | * Dismiss the subWindow for the tab. |
| 1384 | */ |
| 1385 | void dismissSubWindow() { |
| 1386 | if (mSubView != null) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1387 | mWebViewController.endActionMode(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1388 | BrowserSettings.getInstance().deleteObserver( |
| 1389 | mSubView.getSettings()); |
| 1390 | mSubView.destroy(); |
| 1391 | mSubView = null; |
| 1392 | mSubViewContainer = null; |
| 1393 | } |
| 1394 | } |
| 1395 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1396 | |
| 1397 | /** |
| 1398 | * Set the parent tab of this tab. |
| 1399 | */ |
| 1400 | void setParentTab(Tab parent) { |
| 1401 | mParentTab = parent; |
| 1402 | // This tab may have been freed due to low memory. If that is the case, |
| 1403 | // the parent tab index is already saved. If we are changing that index |
| 1404 | // (most likely due to removing the parent tab) we must update the |
| 1405 | // parent tab index in the saved Bundle. |
| 1406 | if (mSavedState != null) { |
| 1407 | if (parent == null) { |
| 1408 | mSavedState.remove(PARENTTAB); |
| 1409 | } else { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1410 | mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl() |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1411 | .getTabIndex(parent)); |
| 1412 | } |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | /** |
| 1417 | * When a Tab is created through the content of another Tab, then we |
| 1418 | * associate the Tabs. |
| 1419 | * @param child the Tab that was created from this Tab |
| 1420 | */ |
| 1421 | void addChildTab(Tab child) { |
| 1422 | if (mChildTabs == null) { |
| 1423 | mChildTabs = new Vector<Tab>(); |
| 1424 | } |
| 1425 | mChildTabs.add(child); |
| 1426 | child.setParentTab(this); |
| 1427 | } |
| 1428 | |
| 1429 | Vector<Tab> getChildTabs() { |
| 1430 | return mChildTabs; |
| 1431 | } |
| 1432 | |
| 1433 | void resume() { |
| 1434 | if (mMainView != null) { |
| 1435 | mMainView.onResume(); |
| 1436 | if (mSubView != null) { |
| 1437 | mSubView.onResume(); |
| 1438 | } |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | void pause() { |
| 1443 | if (mMainView != null) { |
| 1444 | mMainView.onPause(); |
| 1445 | if (mSubView != null) { |
| 1446 | mSubView.onPause(); |
| 1447 | } |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | void putInForeground() { |
| 1452 | mInForeground = true; |
| 1453 | resume(); |
| 1454 | mMainView.setOnCreateContextMenuListener(mActivity); |
| 1455 | if (mSubView != null) { |
| 1456 | mSubView.setOnCreateContextMenuListener(mActivity); |
| 1457 | } |
| 1458 | // Show the pending error dialog if the queue is not empty |
| 1459 | if (mQueuedErrors != null && mQueuedErrors.size() > 0) { |
| 1460 | showError(mQueuedErrors.getFirst()); |
| 1461 | } |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 1462 | mWebViewController.bookmarkedStatusHasChanged(this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | void putInBackground() { |
| 1466 | mInForeground = false; |
| 1467 | pause(); |
| 1468 | mMainView.setOnCreateContextMenuListener(null); |
| 1469 | if (mSubView != null) { |
| 1470 | mSubView.setOnCreateContextMenuListener(null); |
| 1471 | } |
| 1472 | } |
| 1473 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1474 | boolean inForeground() { |
| 1475 | return mInForeground; |
| 1476 | } |
| 1477 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1478 | /** |
| 1479 | * Return the top window of this tab; either the subwindow if it is not |
| 1480 | * null or the main window. |
| 1481 | * @return The top window of this tab. |
| 1482 | */ |
| 1483 | WebView getTopWindow() { |
| 1484 | if (mSubView != null) { |
| 1485 | return mSubView; |
| 1486 | } |
| 1487 | return mMainView; |
| 1488 | } |
| 1489 | |
| 1490 | /** |
| 1491 | * Return the main window of this tab. Note: if a tab is freed in the |
| 1492 | * background, this can return null. It is only guaranteed to be |
| 1493 | * non-null for the current tab. |
| 1494 | * @return The main WebView of this tab. |
| 1495 | */ |
| 1496 | WebView getWebView() { |
| 1497 | return mMainView; |
| 1498 | } |
| 1499 | |
Michael Kolb | a713ec8 | 2010-11-29 17:27:06 -0800 | [diff] [blame] | 1500 | void setViewContainer(View container) { |
| 1501 | mContainer = container; |
| 1502 | } |
| 1503 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1504 | View getViewContainer() { |
| 1505 | return mContainer; |
| 1506 | } |
| 1507 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1508 | /** |
Rob Tsuk | f8bdfce | 2010-10-07 15:41:16 -0700 | [diff] [blame] | 1509 | * Return whether private browsing is enabled for the main window of |
| 1510 | * this tab. |
| 1511 | * @return True if private browsing is enabled. |
| 1512 | */ |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1513 | boolean isPrivateBrowsingEnabled() { |
Rob Tsuk | f8bdfce | 2010-10-07 15:41:16 -0700 | [diff] [blame] | 1514 | WebView webView = getWebView(); |
| 1515 | if (webView == null) { |
| 1516 | return false; |
| 1517 | } |
| 1518 | return webView.isPrivateBrowsingEnabled(); |
| 1519 | } |
| 1520 | |
| 1521 | /** |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1522 | * Return the subwindow of this tab or null if there is no subwindow. |
| 1523 | * @return The subwindow of this tab or null. |
| 1524 | */ |
| 1525 | WebView getSubWebView() { |
| 1526 | return mSubView; |
| 1527 | } |
| 1528 | |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 1529 | void setSubWebView(WebView subView) { |
| 1530 | mSubView = subView; |
| 1531 | } |
| 1532 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1533 | View getSubViewContainer() { |
| 1534 | return mSubViewContainer; |
| 1535 | } |
| 1536 | |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 1537 | void setSubViewContainer(View subViewContainer) { |
| 1538 | mSubViewContainer = subViewContainer; |
| 1539 | } |
| 1540 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1541 | /** |
| 1542 | * @return The geolocation permissions prompt for this tab. |
| 1543 | */ |
| 1544 | GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() { |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 1545 | if (mGeolocationPermissionsPrompt == null) { |
| 1546 | ViewStub stub = (ViewStub) mContainer |
| 1547 | .findViewById(R.id.geolocation_permissions_prompt); |
| 1548 | mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub |
| 1549 | .inflate(); |
| 1550 | mGeolocationPermissionsPrompt.init(); |
| 1551 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1552 | return mGeolocationPermissionsPrompt; |
| 1553 | } |
| 1554 | |
| 1555 | /** |
| 1556 | * @return The application id string |
| 1557 | */ |
| 1558 | String getAppId() { |
| 1559 | return mAppId; |
| 1560 | } |
| 1561 | |
| 1562 | /** |
| 1563 | * Set the application id string |
| 1564 | * @param id |
| 1565 | */ |
| 1566 | void setAppId(String id) { |
| 1567 | mAppId = id; |
| 1568 | } |
| 1569 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1570 | String getUrl() { |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame^] | 1571 | return UrlUtils.filteredUrl(mCurrentState.mUrl); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | /** |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1575 | * Get the title of this tab. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1576 | */ |
| 1577 | String getTitle() { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1578 | if (mCurrentState.mTitle == null && mInPageLoad) { |
| 1579 | return mActivity.getString(R.string.title_bar_loading); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1580 | } |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1581 | return mCurrentState.mTitle; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | /** |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1585 | * Get the favicon of this tab. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1586 | */ |
| 1587 | Bitmap getFavicon() { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1588 | return mCurrentState.mFavicon; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1589 | } |
| 1590 | |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 1591 | public boolean isBookmarkedSite() { |
| 1592 | return mCurrentState.mIsBookmarkedSite; |
| 1593 | } |
Rob Tsuk | f8bdfce | 2010-10-07 15:41:16 -0700 | [diff] [blame] | 1594 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1595 | /** |
| 1596 | * Return the tab's error console. Creates the console if createIfNEcessary |
| 1597 | * is true and we haven't already created the console. |
| 1598 | * @param createIfNecessary Flag to indicate if the console should be |
| 1599 | * created if it has not been already. |
| 1600 | * @return The tab's error console, or null if one has not been created and |
| 1601 | * createIfNecessary is false. |
| 1602 | */ |
| 1603 | ErrorConsoleView getErrorConsole(boolean createIfNecessary) { |
| 1604 | if (createIfNecessary && mErrorConsole == null) { |
| 1605 | mErrorConsole = new ErrorConsoleView(mActivity); |
| 1606 | mErrorConsole.setWebView(mMainView); |
| 1607 | } |
| 1608 | return mErrorConsole; |
| 1609 | } |
| 1610 | |
| 1611 | /** |
| 1612 | * If this Tab was created through another Tab, then this method returns |
| 1613 | * that Tab. |
| 1614 | * @return the Tab parent or null |
| 1615 | */ |
| 1616 | public Tab getParentTab() { |
| 1617 | return mParentTab; |
| 1618 | } |
| 1619 | |
| 1620 | /** |
| 1621 | * Return whether this tab should be closed when it is backing out of the |
| 1622 | * first page. |
| 1623 | * @return TRUE if this tab should be closed when exit. |
| 1624 | */ |
| 1625 | boolean closeOnExit() { |
| 1626 | return mCloseOnExit; |
| 1627 | } |
| 1628 | |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1629 | private void setLockIconType(LockIcon icon) { |
| 1630 | mCurrentState.mLockIcon = icon; |
| 1631 | mWebViewController.onUpdatedLockIcon(this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | /** |
| 1635 | * @return The tab's lock icon type. |
| 1636 | */ |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1637 | LockIcon getLockIconType() { |
| 1638 | return mCurrentState.mLockIcon; |
| 1639 | } |
| 1640 | |
| 1641 | int getLoadProgress() { |
| 1642 | if (mInPageLoad) { |
| 1643 | return mPageLoadProgress; |
| 1644 | } |
| 1645 | return 100; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | /** |
| 1649 | * @return TRUE if onPageStarted is called while onPageFinished is not |
| 1650 | * called yet. |
| 1651 | */ |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1652 | boolean inPageLoad() { |
| 1653 | return mInPageLoad; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | // force mInLoad to be false. This should only be called before closing the |
| 1657 | // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1658 | void clearInPageLoad() { |
| 1659 | mInPageLoad = false; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1662 | /** |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1663 | * Get the cached saved state bundle. |
| 1664 | * @return cached state bundle |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1665 | */ |
| 1666 | Bundle getSavedState() { |
| 1667 | return mSavedState; |
| 1668 | } |
| 1669 | |
| 1670 | /** |
| 1671 | * Set the saved state. |
| 1672 | */ |
| 1673 | void setSavedState(Bundle state) { |
| 1674 | mSavedState = state; |
| 1675 | } |
| 1676 | |
| 1677 | /** |
| 1678 | * @return TRUE if succeed in saving the state. |
| 1679 | */ |
| 1680 | boolean saveState() { |
| 1681 | // If the WebView is null it means we ran low on memory and we already |
| 1682 | // stored the saved state in mSavedState. |
| 1683 | if (mMainView == null) { |
| 1684 | return mSavedState != null; |
| 1685 | } |
| 1686 | |
| 1687 | mSavedState = new Bundle(); |
| 1688 | final WebBackForwardList list = mMainView.saveState(mSavedState); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1689 | |
| 1690 | // Store some extra info for displaying the tab in the picker. |
| 1691 | final WebHistoryItem item = list != null ? list.getCurrentItem() : null; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1692 | |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1693 | mSavedState.putString(CURRURL, mCurrentState.mUrl); |
| 1694 | mSavedState.putString(CURRTITLE, mCurrentState.mTitle); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1695 | mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit); |
| 1696 | if (mAppId != null) { |
| 1697 | mSavedState.putString(APPID, mAppId); |
| 1698 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1699 | // Remember the parent tab so the relationship can be restored. |
| 1700 | if (mParentTab != null) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1701 | mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex( |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1702 | mParentTab)); |
| 1703 | } |
| 1704 | return true; |
| 1705 | } |
| 1706 | |
| 1707 | /* |
| 1708 | * Restore the state of the tab. |
| 1709 | */ |
| 1710 | boolean restoreState(Bundle b) { |
| 1711 | if (b == null) { |
| 1712 | return false; |
| 1713 | } |
| 1714 | // Restore the internal state even if the WebView fails to restore. |
| 1715 | // This will maintain the app id, original url and close-on-exit values. |
| 1716 | mSavedState = null; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1717 | mCloseOnExit = b.getBoolean(CLOSEONEXIT); |
| 1718 | mAppId = b.getString(APPID); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1719 | |
| 1720 | final WebBackForwardList list = mMainView.restoreState(b); |
| 1721 | if (list == null) { |
| 1722 | return false; |
| 1723 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1724 | return true; |
| 1725 | } |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 1726 | |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 1727 | public void updateBookmarkedStatus() { |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 1728 | mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback); |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 1729 | } |
| 1730 | |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 1731 | private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback |
| 1732 | = new DataController.OnQueryUrlIsBookmark() { |
| 1733 | @Override |
| 1734 | public void onQueryUrlIsBookmark(String url, boolean isBookmark) { |
| 1735 | if (mCurrentState.mUrl.equals(url)) { |
| 1736 | mCurrentState.mIsBookmarkedSite = isBookmark; |
| 1737 | mWebViewController.bookmarkedStatusHasChanged(Tab.this); |
| 1738 | } |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 1739 | } |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 1740 | }; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1741 | } |