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