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