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