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