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