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()) { |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 747 | new AlertDialog.Builder(mContext) |
| 748 | .setTitle(R.string.security_warning) |
| 749 | .setMessage(R.string.ssl_warnings_header) |
| 750 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 751 | .setPositiveButton(R.string.ssl_continue, |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 752 | new DialogInterface.OnClickListener() { |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 753 | @Override |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 754 | public void onClick(DialogInterface dialog, |
| 755 | int whichButton) { |
| 756 | handler.proceed(); |
| 757 | } |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 758 | }) |
| 759 | .setNeutralButton(R.string.view_certificate, |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 760 | new DialogInterface.OnClickListener() { |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 761 | @Override |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 762 | public void onClick(DialogInterface dialog, |
| 763 | int whichButton) { |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 764 | mWebViewController.showSslCertificateOnError( |
| 765 | view, handler, error); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 766 | } |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 767 | }) |
| 768 | .setNegativeButton(R.string.ssl_go_back, |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 769 | new DialogInterface.OnClickListener() { |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 770 | @Override |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 771 | public void onClick(DialogInterface dialog, |
| 772 | int whichButton) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 773 | dialog.cancel(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 774 | } |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 775 | }) |
| 776 | .setOnCancelListener( |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 777 | new DialogInterface.OnCancelListener() { |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 778 | @Override |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 779 | public void onCancel(DialogInterface dialog) { |
| 780 | handler.cancel(); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 781 | setLockIconType(LockIcon.LOCK_ICON_UNSECURE); |
| 782 | mWebViewController.onUserCanceledSsl(Tab.this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 783 | } |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 784 | }) |
| 785 | .show(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 786 | } else { |
| 787 | handler.proceed(); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | /** |
Brian Carlstrom | 8862c1d | 2011-06-02 01:05:55 -0700 | [diff] [blame] | 792 | * Displays client certificate request to the user. |
| 793 | */ |
| 794 | @Override |
| 795 | public void onReceivedClientCertRequest(final WebView view, |
| 796 | final ClientCertRequestHandler handler, final String host_and_port) { |
| 797 | if (!mInForeground) { |
| 798 | handler.ignore(); |
| 799 | return; |
| 800 | } |
Brian Carlstrom | 6d85fab | 2011-06-24 14:26:46 -0700 | [diff] [blame] | 801 | int colon = host_and_port.lastIndexOf(':'); |
| 802 | String host; |
| 803 | int port; |
| 804 | if (colon == -1) { |
| 805 | host = host_and_port; |
| 806 | port = -1; |
| 807 | } else { |
| 808 | String portString = host_and_port.substring(colon + 1); |
| 809 | try { |
| 810 | port = Integer.parseInt(portString); |
| 811 | host = host_and_port.substring(0, colon); |
| 812 | } catch (NumberFormatException e) { |
| 813 | host = host_and_port; |
| 814 | port = -1; |
| 815 | } |
| 816 | } |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 817 | KeyChain.choosePrivateKeyAlias( |
| 818 | mWebViewController.getActivity(), new KeyChainAliasCallback() { |
Brian Carlstrom | 8862c1d | 2011-06-02 01:05:55 -0700 | [diff] [blame] | 819 | @Override public void alias(String alias) { |
| 820 | if (alias == null) { |
| 821 | handler.cancel(); |
| 822 | return; |
| 823 | } |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 824 | new KeyChainLookup(mContext, handler, alias).execute(); |
Brian Carlstrom | 8862c1d | 2011-06-02 01:05:55 -0700 | [diff] [blame] | 825 | } |
Brian Carlstrom | 6d85fab | 2011-06-24 14:26:46 -0700 | [diff] [blame] | 826 | }, null, null, host, port, null); |
Brian Carlstrom | 8862c1d | 2011-06-02 01:05:55 -0700 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /** |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 830 | * Handles an HTTP authentication request. |
| 831 | * |
| 832 | * @param handler The authentication handler |
| 833 | * @param host The host |
| 834 | * @param realm The realm |
| 835 | */ |
| 836 | @Override |
| 837 | public void onReceivedHttpAuthRequest(WebView view, |
| 838 | final HttpAuthHandler handler, final String host, |
| 839 | final String realm) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 840 | mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | @Override |
John Reck | 438bf46 | 2011-01-12 18:11:46 -0800 | [diff] [blame] | 844 | public WebResourceResponse shouldInterceptRequest(WebView view, |
| 845 | String url) { |
| 846 | WebResourceResponse res = HomeProvider.shouldInterceptRequest( |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 847 | mContext, url); |
John Reck | 438bf46 | 2011-01-12 18:11:46 -0800 | [diff] [blame] | 848 | return res; |
| 849 | } |
| 850 | |
| 851 | @Override |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 852 | public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) { |
| 853 | if (!mInForeground) { |
| 854 | return false; |
| 855 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 856 | return mWebViewController.shouldOverrideKeyEvent(event); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | @Override |
| 860 | public void onUnhandledKeyEvent(WebView view, KeyEvent event) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 861 | if (!mInForeground) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 862 | return; |
| 863 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 864 | mWebViewController.onUnhandledKeyEvent(event); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 865 | } |
Patrick Scott | 9206677 | 2011-03-10 08:46:27 -0500 | [diff] [blame] | 866 | |
| 867 | @Override |
| 868 | public void onReceivedLoginRequest(WebView view, String realm, |
| 869 | String account, String args) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 870 | new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController) |
Patrick Scott | 9206677 | 2011-03-10 08:46:27 -0500 | [diff] [blame] | 871 | .handleLogin(realm, account, args); |
| 872 | } |
| 873 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 874 | }; |
| 875 | |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 876 | private void syncCurrentState(WebView view, String url) { |
| 877 | // Sync state (in case of stop/timeout) |
| 878 | mCurrentState.mUrl = view.getUrl(); |
| 879 | if (mCurrentState.mUrl == null) { |
| 880 | mCurrentState.mUrl = url != null ? url : ""; |
| 881 | } |
| 882 | mCurrentState.mOriginalUrl = view.getOriginalUrl(); |
| 883 | mCurrentState.mTitle = view.getTitle(); |
| 884 | mCurrentState.mFavicon = view.getFavicon(); |
| 885 | if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) { |
| 886 | // In case we stop when loading an HTTPS page from an HTTP page |
| 887 | // but before a provisional load occurred |
| 888 | mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE; |
| 889 | } |
John Reck | 502a353 | 2011-08-16 14:21:46 -0700 | [diff] [blame] | 890 | mCurrentState.mIncognito = view.isPrivateBrowsingEnabled(); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Patrick Scott | 9206677 | 2011-03-10 08:46:27 -0500 | [diff] [blame] | 893 | // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI |
| 894 | // displayed. |
| 895 | void setDeviceAccountLogin(DeviceAccountLogin login) { |
| 896 | mDeviceAccountLogin = login; |
| 897 | } |
| 898 | |
| 899 | // Returns non-null if the title bar should display the auto-login UI. |
| 900 | DeviceAccountLogin getDeviceAccountLogin() { |
| 901 | return mDeviceAccountLogin; |
| 902 | } |
| 903 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 904 | // ------------------------------------------------------------------------- |
| 905 | // WebChromeClient implementation for the main WebView |
| 906 | // ------------------------------------------------------------------------- |
| 907 | |
| 908 | private final WebChromeClient mWebChromeClient = new WebChromeClient() { |
| 909 | // Helper method to create a new tab or sub window. |
| 910 | private void createWindow(final boolean dialog, final Message msg) { |
| 911 | WebView.WebViewTransport transport = |
| 912 | (WebView.WebViewTransport) msg.obj; |
| 913 | if (dialog) { |
| 914 | createSubWindow(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 915 | mWebViewController.attachSubWindow(Tab.this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 916 | transport.setWebView(mSubView); |
| 917 | } else { |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 918 | final Tab newTab = mWebViewController.openTab(null, |
John Reck | 5949c66 | 2011-05-27 09:52:29 -0700 | [diff] [blame] | 919 | Tab.this, true, true); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 920 | transport.setWebView(newTab.getWebView()); |
| 921 | } |
| 922 | msg.sendToTarget(); |
| 923 | } |
| 924 | |
| 925 | @Override |
| 926 | public boolean onCreateWindow(WebView view, final boolean dialog, |
| 927 | final boolean userGesture, final Message resultMsg) { |
| 928 | // only allow new window or sub window for the foreground case |
| 929 | if (!mInForeground) { |
| 930 | return false; |
| 931 | } |
| 932 | // Short-circuit if we can't create any more tabs or sub windows. |
| 933 | if (dialog && mSubView != null) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 934 | new AlertDialog.Builder(mContext) |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 935 | .setTitle(R.string.too_many_subwindows_dialog_title) |
| 936 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 937 | .setMessage(R.string.too_many_subwindows_dialog_message) |
| 938 | .setPositiveButton(R.string.ok, null) |
| 939 | .show(); |
| 940 | return false; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 941 | } else if (!mWebViewController.getTabControl().canCreateNewTab()) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 942 | new AlertDialog.Builder(mContext) |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 943 | .setTitle(R.string.too_many_windows_dialog_title) |
| 944 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 945 | .setMessage(R.string.too_many_windows_dialog_message) |
| 946 | .setPositiveButton(R.string.ok, null) |
| 947 | .show(); |
| 948 | return false; |
| 949 | } |
| 950 | |
| 951 | // Short-circuit if this was a user gesture. |
| 952 | if (userGesture) { |
| 953 | createWindow(dialog, resultMsg); |
| 954 | return true; |
| 955 | } |
| 956 | |
| 957 | // Allow the popup and create the appropriate window. |
| 958 | final AlertDialog.OnClickListener allowListener = |
| 959 | new AlertDialog.OnClickListener() { |
| 960 | public void onClick(DialogInterface d, |
| 961 | int which) { |
| 962 | createWindow(dialog, resultMsg); |
| 963 | } |
| 964 | }; |
| 965 | |
| 966 | // Block the popup by returning a null WebView. |
| 967 | final AlertDialog.OnClickListener blockListener = |
| 968 | new AlertDialog.OnClickListener() { |
| 969 | public void onClick(DialogInterface d, int which) { |
| 970 | resultMsg.sendToTarget(); |
| 971 | } |
| 972 | }; |
| 973 | |
| 974 | // Build a confirmation dialog to display to the user. |
| 975 | final AlertDialog d = |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 976 | new AlertDialog.Builder(mContext) |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 977 | .setTitle(R.string.attention) |
| 978 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 979 | .setMessage(R.string.popup_window_attempt) |
| 980 | .setPositiveButton(R.string.allow, allowListener) |
| 981 | .setNegativeButton(R.string.block, blockListener) |
| 982 | .setCancelable(false) |
| 983 | .create(); |
| 984 | |
| 985 | // Show the confirmation dialog. |
| 986 | d.show(); |
| 987 | return true; |
| 988 | } |
| 989 | |
| 990 | @Override |
Patrick Scott | eb5061b | 2009-11-18 15:00:30 -0500 | [diff] [blame] | 991 | public void onRequestFocus(WebView view) { |
| 992 | if (!mInForeground) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 993 | mWebViewController.switchToTab(Tab.this); |
Patrick Scott | eb5061b | 2009-11-18 15:00:30 -0500 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | |
| 997 | @Override |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 998 | public void onCloseWindow(WebView window) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 999 | if (mParent != null) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1000 | // JavaScript can only close popup window. |
| 1001 | if (mInForeground) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1002 | mWebViewController.switchToTab(mParent); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1003 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1004 | mWebViewController.closeTab(Tab.this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | @Override |
| 1009 | public void onProgressChanged(WebView view, int newProgress) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1010 | mPageLoadProgress = newProgress; |
| 1011 | mWebViewController.onProgressChanged(Tab.this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | @Override |
Leon Scroggins | 21d9b90 | 2010-03-11 09:33:11 -0500 | [diff] [blame] | 1015 | public void onReceivedTitle(WebView view, final String title) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1016 | mCurrentState.mTitle = title; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1017 | mWebViewController.onReceivedTitle(Tab.this, title); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | @Override |
| 1021 | public void onReceivedIcon(WebView view, Bitmap icon) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1022 | mCurrentState.mFavicon = icon; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1023 | mWebViewController.onFavicon(Tab.this, view, icon); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | @Override |
| 1027 | public void onReceivedTouchIconUrl(WebView view, String url, |
| 1028 | boolean precomposed) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1029 | final ContentResolver cr = mContext.getContentResolver(); |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 1030 | // Let precomposed icons take precedence over non-composed |
| 1031 | // icons. |
| 1032 | if (precomposed && mTouchIconLoader != null) { |
| 1033 | mTouchIconLoader.cancel(false); |
| 1034 | mTouchIconLoader = null; |
| 1035 | } |
| 1036 | // Have only one async task at a time. |
| 1037 | if (mTouchIconLoader == null) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1038 | mTouchIconLoader = new DownloadTouchIcon(Tab.this, |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1039 | mContext, cr, view); |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 1040 | mTouchIconLoader.execute(url); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | @Override |
| 1045 | public void onShowCustomView(View view, |
| 1046 | WebChromeClient.CustomViewCallback callback) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1047 | Activity activity = mWebViewController.getActivity(); |
| 1048 | if (activity != null) { |
| 1049 | onShowCustomView(view, activity.getRequestedOrientation(), callback); |
| 1050 | } |
Derek Sollenberger | 2d4f1e2 | 2011-06-01 14:50:42 -0400 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | @Override |
| 1054 | public void onShowCustomView(View view, int requestedOrientation, |
| 1055 | WebChromeClient.CustomViewCallback callback) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1056 | if (mInForeground) mWebViewController.showCustomView(Tab.this, view, |
Derek Sollenberger | 2d4f1e2 | 2011-06-01 14:50:42 -0400 | [diff] [blame] | 1057 | requestedOrientation, callback); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | @Override |
| 1061 | public void onHideCustomView() { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1062 | if (mInForeground) mWebViewController.hideCustomView(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /** |
| 1066 | * The origin has exceeded its database quota. |
| 1067 | * @param url the URL that exceeded the quota |
| 1068 | * @param databaseIdentifier the identifier of the database on which the |
| 1069 | * transaction that caused the quota overflow was run |
| 1070 | * @param currentQuota the current quota for the origin. |
| 1071 | * @param estimatedSize the estimated size of the database. |
| 1072 | * @param totalUsedQuota is the sum of all origins' quota. |
| 1073 | * @param quotaUpdater The callback to run when a decision to allow or |
| 1074 | * deny quota has been made. Don't forget to call this! |
| 1075 | */ |
| 1076 | @Override |
| 1077 | public void onExceededDatabaseQuota(String url, |
| 1078 | String databaseIdentifier, long currentQuota, long estimatedSize, |
| 1079 | long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 1080 | mSettings.getWebStorageSizeManager() |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1081 | .onExceededDatabaseQuota(url, databaseIdentifier, |
| 1082 | currentQuota, estimatedSize, totalUsedQuota, |
| 1083 | quotaUpdater); |
| 1084 | } |
| 1085 | |
| 1086 | /** |
| 1087 | * The Application Cache has exceeded its max size. |
| 1088 | * @param spaceNeeded is the amount of disk space that would be needed |
| 1089 | * in order for the last appcache operation to succeed. |
| 1090 | * @param totalUsedQuota is the sum of all origins' quota. |
| 1091 | * @param quotaUpdater A callback to inform the WebCore thread that a |
| 1092 | * new app cache size is available. This callback must always |
| 1093 | * be executed at some point to ensure that the sleeping |
| 1094 | * WebCore thread is woken up. |
| 1095 | */ |
| 1096 | @Override |
| 1097 | public void onReachedMaxAppCacheSize(long spaceNeeded, |
| 1098 | long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 1099 | mSettings.getWebStorageSizeManager() |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1100 | .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota, |
| 1101 | quotaUpdater); |
| 1102 | } |
| 1103 | |
| 1104 | /** |
| 1105 | * Instructs the browser to show a prompt to ask the user to set the |
| 1106 | * Geolocation permission state for the specified origin. |
| 1107 | * @param origin The origin for which Geolocation permissions are |
| 1108 | * requested. |
| 1109 | * @param callback The callback to call once the user has set the |
| 1110 | * Geolocation permission state. |
| 1111 | */ |
| 1112 | @Override |
| 1113 | public void onGeolocationPermissionsShowPrompt(String origin, |
| 1114 | GeolocationPermissions.Callback callback) { |
| 1115 | if (mInForeground) { |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 1116 | getGeolocationPermissionsPrompt().show(origin, callback); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | /** |
| 1121 | * Instructs the browser to hide the Geolocation permissions prompt. |
| 1122 | */ |
| 1123 | @Override |
| 1124 | public void onGeolocationPermissionsHidePrompt() { |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 1125 | if (mInForeground && mGeolocationPermissionsPrompt != null) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1126 | mGeolocationPermissionsPrompt.hide(); |
| 1127 | } |
| 1128 | } |
| 1129 | |
Ben Murdoch | 65acc35 | 2009-11-19 18:16:04 +0000 | [diff] [blame] | 1130 | /* Adds a JavaScript error message to the system log and if the JS |
| 1131 | * console is enabled in the about:debug options, to that console |
| 1132 | * also. |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 1133 | * @param consoleMessage the message object. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1134 | */ |
| 1135 | @Override |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 1136 | public boolean onConsoleMessage(ConsoleMessage consoleMessage) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1137 | if (mInForeground) { |
| 1138 | // call getErrorConsole(true) so it will create one if needed |
| 1139 | ErrorConsoleView errorConsole = getErrorConsole(true); |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 1140 | errorConsole.addErrorMessage(consoleMessage); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1141 | if (mWebViewController.shouldShowErrorConsole() |
| 1142 | && errorConsole.getShowState() != |
| 1143 | ErrorConsoleView.SHOW_MAXIMIZED) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1144 | errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED); |
| 1145 | } |
| 1146 | } |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 1147 | |
Jeff Hamilton | 47654f4 | 2010-09-07 09:57:51 -0500 | [diff] [blame] | 1148 | // Don't log console messages in private browsing mode |
Rob Tsuk | f8bdfce | 2010-10-07 15:41:16 -0700 | [diff] [blame] | 1149 | if (isPrivateBrowsingEnabled()) return true; |
Jeff Hamilton | 47654f4 | 2010-09-07 09:57:51 -0500 | [diff] [blame] | 1150 | |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 1151 | String message = "Console: " + consoleMessage.message() + " " |
| 1152 | + consoleMessage.sourceId() + ":" |
| 1153 | + consoleMessage.lineNumber(); |
| 1154 | |
| 1155 | switch (consoleMessage.messageLevel()) { |
| 1156 | case TIP: |
| 1157 | Log.v(CONSOLE_LOGTAG, message); |
| 1158 | break; |
| 1159 | case LOG: |
| 1160 | Log.i(CONSOLE_LOGTAG, message); |
| 1161 | break; |
| 1162 | case WARNING: |
| 1163 | Log.w(CONSOLE_LOGTAG, message); |
| 1164 | break; |
| 1165 | case ERROR: |
| 1166 | Log.e(CONSOLE_LOGTAG, message); |
| 1167 | break; |
| 1168 | case DEBUG: |
| 1169 | Log.d(CONSOLE_LOGTAG, message); |
| 1170 | break; |
| 1171 | } |
| 1172 | |
| 1173 | return true; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | /** |
| 1177 | * Ask the browser for an icon to represent a <video> element. |
| 1178 | * This icon will be used if the Web page did not specify a poster attribute. |
| 1179 | * @return Bitmap The icon or null if no such icon is available. |
| 1180 | */ |
| 1181 | @Override |
| 1182 | public Bitmap getDefaultVideoPoster() { |
| 1183 | if (mInForeground) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1184 | return mWebViewController.getDefaultVideoPoster(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1185 | } |
| 1186 | return null; |
| 1187 | } |
| 1188 | |
| 1189 | /** |
| 1190 | * Ask the host application for a custom progress view to show while |
| 1191 | * a <video> is loading. |
| 1192 | * @return View The progress view. |
| 1193 | */ |
| 1194 | @Override |
| 1195 | public View getVideoLoadingProgressView() { |
| 1196 | if (mInForeground) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1197 | return mWebViewController.getVideoLoadingProgressView(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1198 | } |
| 1199 | return null; |
| 1200 | } |
| 1201 | |
| 1202 | @Override |
Ben Murdoch | 62b1b7e | 2010-05-19 20:38:56 +0100 | [diff] [blame] | 1203 | public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1204 | if (mInForeground) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1205 | mWebViewController.openFileChooser(uploadMsg, acceptType); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1206 | } else { |
| 1207 | uploadMsg.onReceiveValue(null); |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | /** |
| 1212 | * Deliver a list of already-visited URLs |
| 1213 | */ |
| 1214 | @Override |
| 1215 | public void getVisitedHistory(final ValueCallback<String[]> callback) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1216 | mWebViewController.getVisitedHistory(callback); |
| 1217 | } |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 1218 | |
| 1219 | @Override |
| 1220 | public void setupAutoFill(Message message) { |
| 1221 | // Prompt the user to set up their profile. |
| 1222 | final Message msg = message; |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1223 | AlertDialog.Builder builder = new AlertDialog.Builder(mContext); |
| 1224 | LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( |
Ben Murdoch | 1d676b6 | 2011-01-17 12:54:24 +0000 | [diff] [blame] | 1225 | Context.LAYOUT_INFLATER_SERVICE); |
| 1226 | final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null); |
| 1227 | |
| 1228 | builder.setView(layout) |
Ben Murdoch | b7e6f94 | 2011-07-08 13:00:21 +0100 | [diff] [blame] | 1229 | .setTitle(R.string.autofill_setup_dialog_title) |
Ben Murdoch | 1d676b6 | 2011-01-17 12:54:24 +0000 | [diff] [blame] | 1230 | .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { |
| 1231 | @Override |
| 1232 | public void onClick(DialogInterface dialog, int id) { |
| 1233 | CheckBox disableAutoFill = (CheckBox) layout.findViewById( |
| 1234 | R.id.setup_autofill_dialog_disable_autofill); |
| 1235 | |
| 1236 | if (disableAutoFill.isChecked()) { |
| 1237 | // 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] | 1238 | mSettings.setAutofillEnabled(false); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1239 | Toast.makeText(mContext, |
Ben Murdoch | 1d676b6 | 2011-01-17 12:54:24 +0000 | [diff] [blame] | 1240 | R.string.autofill_setup_dialog_negative_toast, |
| 1241 | Toast.LENGTH_LONG).show(); |
| 1242 | } else { |
| 1243 | // Take user to the AutoFill profile editor. When they return, |
| 1244 | // we will send the message that we pass here which will trigger |
| 1245 | // the form to get filled out with their new profile. |
| 1246 | mWebViewController.setupAutoFill(msg); |
| 1247 | } |
| 1248 | } |
| 1249 | }) |
| 1250 | .setNegativeButton(R.string.cancel, null) |
| 1251 | .show(); |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 1252 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1253 | }; |
| 1254 | |
| 1255 | // ------------------------------------------------------------------------- |
| 1256 | // WebViewClient implementation for the sub window |
| 1257 | // ------------------------------------------------------------------------- |
| 1258 | |
| 1259 | // Subclass of WebViewClient used in subwindows to notify the main |
| 1260 | // WebViewClient of certain WebView activities. |
| 1261 | private static class SubWindowClient extends WebViewClient { |
| 1262 | // The main WebViewClient. |
| 1263 | private final WebViewClient mClient; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1264 | private final WebViewController mController; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1265 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1266 | SubWindowClient(WebViewClient client, WebViewController controller) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1267 | mClient = client; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1268 | mController = controller; |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 1269 | } |
| 1270 | @Override |
| 1271 | public void onPageStarted(WebView view, String url, Bitmap favicon) { |
| 1272 | // Unlike the others, do not call mClient's version, which would |
| 1273 | // change the progress bar. However, we do want to remove the |
Cary Clark | 01cfcdd | 2010-06-04 16:36:45 -0400 | [diff] [blame] | 1274 | // find or select dialog. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1275 | mController.endActionMode(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1276 | } |
| 1277 | @Override |
| 1278 | public void doUpdateVisitedHistory(WebView view, String url, |
| 1279 | boolean isReload) { |
| 1280 | mClient.doUpdateVisitedHistory(view, url, isReload); |
| 1281 | } |
| 1282 | @Override |
| 1283 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| 1284 | return mClient.shouldOverrideUrlLoading(view, url); |
| 1285 | } |
| 1286 | @Override |
| 1287 | public void onReceivedSslError(WebView view, SslErrorHandler handler, |
| 1288 | SslError error) { |
| 1289 | mClient.onReceivedSslError(view, handler, error); |
| 1290 | } |
| 1291 | @Override |
Brian Carlstrom | 8862c1d | 2011-06-02 01:05:55 -0700 | [diff] [blame] | 1292 | public void onReceivedClientCertRequest(WebView view, |
| 1293 | ClientCertRequestHandler handler, String host_and_port) { |
| 1294 | mClient.onReceivedClientCertRequest(view, handler, host_and_port); |
| 1295 | } |
| 1296 | @Override |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1297 | public void onReceivedHttpAuthRequest(WebView view, |
| 1298 | HttpAuthHandler handler, String host, String realm) { |
| 1299 | mClient.onReceivedHttpAuthRequest(view, handler, host, realm); |
| 1300 | } |
| 1301 | @Override |
| 1302 | public void onFormResubmission(WebView view, Message dontResend, |
| 1303 | Message resend) { |
| 1304 | mClient.onFormResubmission(view, dontResend, resend); |
| 1305 | } |
| 1306 | @Override |
| 1307 | public void onReceivedError(WebView view, int errorCode, |
| 1308 | String description, String failingUrl) { |
| 1309 | mClient.onReceivedError(view, errorCode, description, failingUrl); |
| 1310 | } |
| 1311 | @Override |
| 1312 | public boolean shouldOverrideKeyEvent(WebView view, |
| 1313 | android.view.KeyEvent event) { |
| 1314 | return mClient.shouldOverrideKeyEvent(view, event); |
| 1315 | } |
| 1316 | @Override |
| 1317 | public void onUnhandledKeyEvent(WebView view, |
| 1318 | android.view.KeyEvent event) { |
| 1319 | mClient.onUnhandledKeyEvent(view, event); |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | // ------------------------------------------------------------------------- |
| 1324 | // WebChromeClient implementation for the sub window |
| 1325 | // ------------------------------------------------------------------------- |
| 1326 | |
| 1327 | private class SubWindowChromeClient extends WebChromeClient { |
| 1328 | // The main WebChromeClient. |
| 1329 | private final WebChromeClient mClient; |
| 1330 | |
| 1331 | SubWindowChromeClient(WebChromeClient client) { |
| 1332 | mClient = client; |
| 1333 | } |
| 1334 | @Override |
| 1335 | public void onProgressChanged(WebView view, int newProgress) { |
| 1336 | mClient.onProgressChanged(view, newProgress); |
| 1337 | } |
| 1338 | @Override |
| 1339 | public boolean onCreateWindow(WebView view, boolean dialog, |
| 1340 | boolean userGesture, android.os.Message resultMsg) { |
| 1341 | return mClient.onCreateWindow(view, dialog, userGesture, resultMsg); |
| 1342 | } |
| 1343 | @Override |
| 1344 | public void onCloseWindow(WebView window) { |
| 1345 | if (window != mSubView) { |
| 1346 | Log.e(LOGTAG, "Can't close the window"); |
| 1347 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1348 | mWebViewController.dismissSubWindow(Tab.this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | // ------------------------------------------------------------------------- |
| 1353 | |
| 1354 | // Construct a new tab |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 1355 | Tab(WebViewController wvcontroller, WebView w) { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1356 | this(wvcontroller, w, null); |
| 1357 | } |
| 1358 | |
| 1359 | Tab(WebViewController wvcontroller, Bundle state) { |
| 1360 | this(wvcontroller, null, state); |
| 1361 | } |
| 1362 | |
| 1363 | Tab(WebViewController wvcontroller, WebView w, Bundle state) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1364 | mWebViewController = wvcontroller; |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1365 | mContext = mWebViewController.getContext(); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 1366 | mSettings = BrowserSettings.getInstance(); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1367 | mDataController = DataController.getInstance(mContext); |
| 1368 | mCurrentState = new PageState(mContext, w != null |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 1369 | ? w.isPrivateBrowsingEnabled() : false); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1370 | mInPageLoad = false; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1371 | mInForeground = false; |
| 1372 | |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1373 | mDownloadListener = new DownloadListener() { |
| 1374 | public void onDownloadStart(String url, String userAgent, |
| 1375 | String contentDisposition, String mimetype, |
| 1376 | long contentLength) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1377 | mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition, |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1378 | mimetype, contentLength); |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1379 | } |
| 1380 | }; |
Leon Scroggins | 0c75a8e | 2010-03-03 16:40:58 -0500 | [diff] [blame] | 1381 | mWebBackForwardListClient = new WebBackForwardListClient() { |
| 1382 | @Override |
| 1383 | public void onNewHistoryItem(WebHistoryItem item) { |
| 1384 | if (isInVoiceSearchMode()) { |
| 1385 | item.setCustomData(mVoiceSearchData.mVoiceSearchIntent); |
| 1386 | } |
Mathew Inwood | 1dd8e82 | 2011-08-03 14:34:29 +0100 | [diff] [blame] | 1387 | if (mClearHistoryMatchUrl != null) { |
| 1388 | if (LOGD_ENABLED) { |
| 1389 | Log.d(LOGTAG, "onNewHistoryItem:\n\t" + item.getUrl() + "\n\t" |
| 1390 | + mClearHistoryMatchUrl); |
| 1391 | } |
| 1392 | if (TextUtils.equals(item.getOriginalUrl(), mClearHistoryMatchUrl)) { |
| 1393 | if (mMainView != null) { |
| 1394 | mMainView.clearHistory(); |
| 1395 | } |
| 1396 | } |
| 1397 | mClearHistoryMatchUrl = null; |
| 1398 | } |
Leon Scroggins | 0c75a8e | 2010-03-03 16:40:58 -0500 | [diff] [blame] | 1399 | } |
| 1400 | @Override |
| 1401 | public void onIndexChanged(WebHistoryItem item, int index) { |
| 1402 | Object data = item.getCustomData(); |
| 1403 | if (data != null && data instanceof Intent) { |
| 1404 | activateVoiceSearchMode((Intent) data); |
| 1405 | } |
| 1406 | } |
| 1407 | }; |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1408 | |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1409 | mCaptureWidth = mContext.getResources().getDimensionPixelSize( |
| 1410 | R.dimen.tab_thumbnail_width); |
| 1411 | mCaptureHeight = mContext.getResources().getDimensionPixelSize( |
| 1412 | R.dimen.tab_thumbnail_height); |
| 1413 | updateShouldCaptureThumbnails(); |
| 1414 | restoreState(state); |
John Reck | 52be478 | 2011-08-26 15:37:29 -0700 | [diff] [blame^] | 1415 | if (getId() == -1) { |
| 1416 | mId = TabControl.getNextId(); |
| 1417 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1418 | setWebView(w); |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 1419 | mHandler = new Handler() { |
Mathew Inwood | 1dd8e82 | 2011-08-03 14:34:29 +0100 | [diff] [blame] | 1420 | @Override |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 1421 | public void handleMessage(Message m) { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1422 | switch (m.what) { |
| 1423 | case MSG_CAPTURE: |
| 1424 | capture(); |
| 1425 | break; |
| 1426 | } |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 1427 | } |
| 1428 | }; |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1429 | } |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 1430 | |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1431 | public void updateShouldCaptureThumbnails() { |
| 1432 | if (mWebViewController.shouldCaptureThumbnails()) { |
| 1433 | synchronized (Tab.this) { |
| 1434 | if (mCapture == null) { |
| 1435 | mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, |
| 1436 | Bitmap.Config.RGB_565); |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 1437 | mCapture.eraseColor(Color.WHITE); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1438 | if (mInForeground) { |
| 1439 | postCapture(); |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | } else { |
| 1444 | synchronized (Tab.this) { |
| 1445 | mCapture = null; |
| 1446 | deleteThumbnail(); |
| 1447 | } |
| 1448 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1451 | public void setController(WebViewController ctl) { |
| 1452 | mWebViewController = ctl; |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1453 | updateShouldCaptureThumbnails(); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1456 | public long getId() { |
| 1457 | return mId; |
| 1458 | } |
| 1459 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1460 | /** |
| 1461 | * Sets the WebView for this tab, correctly removing the old WebView from |
| 1462 | * the container view. |
| 1463 | */ |
| 1464 | void setWebView(WebView w) { |
| 1465 | if (mMainView == w) { |
| 1466 | return; |
| 1467 | } |
Michael Kolb | a713ec8 | 2010-11-29 17:27:06 -0800 | [diff] [blame] | 1468 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1469 | // If the WebView is changing, the page will be reloaded, so any ongoing |
| 1470 | // Geolocation permission requests are void. |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 1471 | if (mGeolocationPermissionsPrompt != null) { |
| 1472 | mGeolocationPermissionsPrompt.hide(); |
| 1473 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1474 | |
Michael Kolb | a713ec8 | 2010-11-29 17:27:06 -0800 | [diff] [blame] | 1475 | mWebViewController.onSetWebView(this, w); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1476 | |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1477 | if (mMainView != null) { |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 1478 | mMainView.setPictureListener(null); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1479 | if (w != null) { |
| 1480 | syncCurrentState(w, null); |
| 1481 | } else { |
| 1482 | mCurrentState = new PageState(mContext, false); |
| 1483 | } |
| 1484 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1485 | // set the new one |
| 1486 | mMainView = w; |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1487 | // attach the WebViewClient, WebChromeClient and DownloadListener |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1488 | if (mMainView != null) { |
| 1489 | mMainView.setWebViewClient(mWebViewClient); |
| 1490 | mMainView.setWebChromeClient(mWebChromeClient); |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1491 | // Attach DownloadManager so that downloads can start in an active |
| 1492 | // or a non-active window. This can happen when going to a site that |
| 1493 | // does a redirect after a period of time. The user could have |
| 1494 | // switched to another tab while waiting for the download to start. |
| 1495 | mMainView.setDownloadListener(mDownloadListener); |
Leon Scroggins | 0c75a8e | 2010-03-03 16:40:58 -0500 | [diff] [blame] | 1496 | mMainView.setWebBackForwardListClient(mWebBackForwardListClient); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 1497 | TabControl tc = mWebViewController.getTabControl(); |
| 1498 | if (tc != null && tc.getOnThumbnailUpdatedListener() != null) { |
| 1499 | mMainView.setPictureListener(this); |
| 1500 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1501 | if (mSavedState != null) { |
John Reck | 6c2e2f3 | 2011-08-22 13:41:23 -0700 | [diff] [blame] | 1502 | WebBackForwardList restoredState |
| 1503 | = mMainView.restoreState(mSavedState); |
| 1504 | if (restoredState == null || restoredState.getSize() == 0) { |
| 1505 | Log.w(LOGTAG, "Failed to restore WebView state!"); |
| 1506 | loadUrl(mCurrentState.mOriginalUrl, null); |
| 1507 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1508 | mSavedState = null; |
| 1509 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | /** |
| 1514 | * Destroy the tab's main WebView and subWindow if any |
| 1515 | */ |
| 1516 | void destroy() { |
| 1517 | if (mMainView != null) { |
| 1518 | dismissSubWindow(); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 1519 | // Make sure the embedded title bar isn't still attached |
| 1520 | mMainView.setEmbeddedTitleBar(null); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1521 | // save the WebView to call destroy() after detach it from the tab |
| 1522 | WebView webView = mMainView; |
| 1523 | setWebView(null); |
| 1524 | webView.destroy(); |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | /** |
| 1529 | * Remove the tab from the parent |
| 1530 | */ |
| 1531 | void removeFromTree() { |
| 1532 | // detach the children |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1533 | if (mChildren != null) { |
| 1534 | for(Tab t : mChildren) { |
| 1535 | t.setParent(null); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1536 | } |
| 1537 | } |
| 1538 | // remove itself from the parent list |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1539 | if (mParent != null) { |
| 1540 | mParent.mChildren.remove(this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1541 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1542 | deleteThumbnail(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | /** |
| 1546 | * Create a new subwindow unless a subwindow already exists. |
| 1547 | * @return True if a new subwindow was created. False if one already exists. |
| 1548 | */ |
| 1549 | boolean createSubWindow() { |
| 1550 | if (mSubView == null) { |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 1551 | mWebViewController.createSubWindow(this); |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 1552 | mSubView.setWebViewClient(new SubWindowClient(mWebViewClient, |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1553 | mWebViewController)); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1554 | mSubView.setWebChromeClient(new SubWindowChromeClient( |
| 1555 | mWebChromeClient)); |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1556 | // Set a different DownloadListener for the mSubView, since it will |
| 1557 | // just need to dismiss the mSubView, rather than close the Tab |
| 1558 | mSubView.setDownloadListener(new DownloadListener() { |
| 1559 | public void onDownloadStart(String url, String userAgent, |
| 1560 | String contentDisposition, String mimetype, |
| 1561 | long contentLength) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1562 | mWebViewController.onDownloadStart(Tab.this, url, userAgent, |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1563 | contentDisposition, mimetype, contentLength); |
| 1564 | if (mSubView.copyBackForwardList().getSize() == 0) { |
| 1565 | // This subwindow was opened for the sole purpose of |
| 1566 | // downloading a file. Remove it. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1567 | mWebViewController.dismissSubWindow(Tab.this); |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | }); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1571 | mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity()); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1572 | return true; |
| 1573 | } |
| 1574 | return false; |
| 1575 | } |
| 1576 | |
| 1577 | /** |
| 1578 | * Dismiss the subWindow for the tab. |
| 1579 | */ |
| 1580 | void dismissSubWindow() { |
| 1581 | if (mSubView != null) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1582 | mWebViewController.endActionMode(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1583 | mSubView.destroy(); |
| 1584 | mSubView = null; |
| 1585 | mSubViewContainer = null; |
| 1586 | } |
| 1587 | } |
| 1588 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1589 | |
| 1590 | /** |
| 1591 | * Set the parent tab of this tab. |
| 1592 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1593 | void setParent(Tab parent) { |
John Reck | 52be478 | 2011-08-26 15:37:29 -0700 | [diff] [blame^] | 1594 | if (parent == this) { |
| 1595 | throw new IllegalStateException("Cannot set parent to self!"); |
| 1596 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1597 | mParent = parent; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1598 | // 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] | 1599 | // 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] | 1600 | // (most likely due to removing the parent tab) we must update the |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1601 | // parent tab id in the saved Bundle. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1602 | if (mSavedState != null) { |
| 1603 | if (parent == null) { |
| 1604 | mSavedState.remove(PARENTTAB); |
| 1605 | } else { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1606 | mSavedState.putLong(PARENTTAB, parent.getId()); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1607 | } |
| 1608 | } |
John Reck | b0a86db | 2011-05-24 14:05:58 -0700 | [diff] [blame] | 1609 | |
| 1610 | // Sync the WebView useragent with the parent |
| 1611 | if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView()) |
| 1612 | != mSettings.hasDesktopUseragent(getWebView())) { |
| 1613 | mSettings.toggleDesktopUseragent(getWebView()); |
| 1614 | } |
John Reck | 52be478 | 2011-08-26 15:37:29 -0700 | [diff] [blame^] | 1615 | |
| 1616 | if (parent != null && parent.getId() == getId()) { |
| 1617 | throw new IllegalStateException("Parent has same ID as child!"); |
| 1618 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | /** |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1622 | * If this Tab was created through another Tab, then this method returns |
| 1623 | * that Tab. |
| 1624 | * @return the Tab parent or null |
| 1625 | */ |
| 1626 | public Tab getParent() { |
| 1627 | return mParent; |
| 1628 | } |
| 1629 | |
| 1630 | /** |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1631 | * When a Tab is created through the content of another Tab, then we |
| 1632 | * associate the Tabs. |
| 1633 | * @param child the Tab that was created from this Tab |
| 1634 | */ |
| 1635 | void addChildTab(Tab child) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1636 | if (mChildren == null) { |
| 1637 | mChildren = new Vector<Tab>(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1638 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1639 | mChildren.add(child); |
| 1640 | child.setParent(this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1641 | } |
| 1642 | |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1643 | Vector<Tab> getChildren() { |
| 1644 | return mChildren; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | void resume() { |
| 1648 | if (mMainView != null) { |
John Reck | 56c1fcf | 2011-08-17 10:15:16 -0700 | [diff] [blame] | 1649 | setupHwAcceleration(mMainView); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1650 | mMainView.onResume(); |
| 1651 | if (mSubView != null) { |
| 1652 | mSubView.onResume(); |
| 1653 | } |
| 1654 | } |
| 1655 | } |
| 1656 | |
John Reck | 56c1fcf | 2011-08-17 10:15:16 -0700 | [diff] [blame] | 1657 | private void setupHwAcceleration(View web) { |
| 1658 | if (web == null) return; |
| 1659 | BrowserSettings settings = BrowserSettings.getInstance(); |
| 1660 | if (settings.isHardwareAccelerated()) { |
| 1661 | web.setLayerType(View.LAYER_TYPE_NONE, null); |
| 1662 | } else { |
| 1663 | web.setLayerType(View.LAYER_TYPE_SOFTWARE, null); |
| 1664 | } |
| 1665 | } |
| 1666 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1667 | void pause() { |
| 1668 | if (mMainView != null) { |
| 1669 | mMainView.onPause(); |
| 1670 | if (mSubView != null) { |
| 1671 | mSubView.onPause(); |
| 1672 | } |
| 1673 | } |
| 1674 | } |
| 1675 | |
| 1676 | void putInForeground() { |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 1677 | if (mInForeground) { |
| 1678 | return; |
| 1679 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1680 | mInForeground = true; |
| 1681 | resume(); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1682 | Activity activity = mWebViewController.getActivity(); |
| 1683 | mMainView.setOnCreateContextMenuListener(activity); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1684 | if (mSubView != null) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1685 | mSubView.setOnCreateContextMenuListener(activity); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1686 | } |
| 1687 | // Show the pending error dialog if the queue is not empty |
| 1688 | if (mQueuedErrors != null && mQueuedErrors.size() > 0) { |
| 1689 | showError(mQueuedErrors.getFirst()); |
| 1690 | } |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 1691 | mWebViewController.bookmarkedStatusHasChanged(this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | void putInBackground() { |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 1695 | if (!mInForeground) { |
| 1696 | return; |
| 1697 | } |
John Reck | 52be478 | 2011-08-26 15:37:29 -0700 | [diff] [blame^] | 1698 | capture(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1699 | mInForeground = false; |
| 1700 | pause(); |
| 1701 | mMainView.setOnCreateContextMenuListener(null); |
| 1702 | if (mSubView != null) { |
| 1703 | mSubView.setOnCreateContextMenuListener(null); |
| 1704 | } |
| 1705 | } |
| 1706 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1707 | boolean inForeground() { |
| 1708 | return mInForeground; |
| 1709 | } |
| 1710 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1711 | /** |
| 1712 | * Return the top window of this tab; either the subwindow if it is not |
| 1713 | * null or the main window. |
| 1714 | * @return The top window of this tab. |
| 1715 | */ |
| 1716 | WebView getTopWindow() { |
| 1717 | if (mSubView != null) { |
| 1718 | return mSubView; |
| 1719 | } |
| 1720 | return mMainView; |
| 1721 | } |
| 1722 | |
| 1723 | /** |
| 1724 | * Return the main window of this tab. Note: if a tab is freed in the |
| 1725 | * background, this can return null. It is only guaranteed to be |
| 1726 | * non-null for the current tab. |
| 1727 | * @return The main WebView of this tab. |
| 1728 | */ |
| 1729 | WebView getWebView() { |
| 1730 | return mMainView; |
| 1731 | } |
| 1732 | |
Michael Kolb | a713ec8 | 2010-11-29 17:27:06 -0800 | [diff] [blame] | 1733 | void setViewContainer(View container) { |
| 1734 | mContainer = container; |
| 1735 | } |
| 1736 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1737 | View getViewContainer() { |
| 1738 | return mContainer; |
| 1739 | } |
| 1740 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1741 | /** |
Rob Tsuk | f8bdfce | 2010-10-07 15:41:16 -0700 | [diff] [blame] | 1742 | * Return whether private browsing is enabled for the main window of |
| 1743 | * this tab. |
| 1744 | * @return True if private browsing is enabled. |
| 1745 | */ |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1746 | boolean isPrivateBrowsingEnabled() { |
John Reck | 502a353 | 2011-08-16 14:21:46 -0700 | [diff] [blame] | 1747 | return mCurrentState.mIncognito; |
Rob Tsuk | f8bdfce | 2010-10-07 15:41:16 -0700 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | /** |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1751 | * Return the subwindow of this tab or null if there is no subwindow. |
| 1752 | * @return The subwindow of this tab or null. |
| 1753 | */ |
| 1754 | WebView getSubWebView() { |
| 1755 | return mSubView; |
| 1756 | } |
| 1757 | |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 1758 | void setSubWebView(WebView subView) { |
| 1759 | mSubView = subView; |
| 1760 | } |
| 1761 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1762 | View getSubViewContainer() { |
| 1763 | return mSubViewContainer; |
| 1764 | } |
| 1765 | |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 1766 | void setSubViewContainer(View subViewContainer) { |
| 1767 | mSubViewContainer = subViewContainer; |
| 1768 | } |
| 1769 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1770 | /** |
| 1771 | * @return The geolocation permissions prompt for this tab. |
| 1772 | */ |
| 1773 | GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() { |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 1774 | if (mGeolocationPermissionsPrompt == null) { |
| 1775 | ViewStub stub = (ViewStub) mContainer |
| 1776 | .findViewById(R.id.geolocation_permissions_prompt); |
| 1777 | mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub |
| 1778 | .inflate(); |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 1779 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1780 | return mGeolocationPermissionsPrompt; |
| 1781 | } |
| 1782 | |
| 1783 | /** |
| 1784 | * @return The application id string |
| 1785 | */ |
| 1786 | String getAppId() { |
| 1787 | return mAppId; |
| 1788 | } |
| 1789 | |
| 1790 | /** |
| 1791 | * Set the application id string |
| 1792 | * @param id |
| 1793 | */ |
| 1794 | void setAppId(String id) { |
| 1795 | mAppId = id; |
| 1796 | } |
| 1797 | |
Michael Kolb | e28b347 | 2011-08-04 16:54:31 -0700 | [diff] [blame] | 1798 | boolean closeOnBack() { |
| 1799 | return mCloseOnBack; |
| 1800 | } |
| 1801 | |
| 1802 | void setCloseOnBack(boolean close) { |
| 1803 | mCloseOnBack = close; |
| 1804 | } |
| 1805 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1806 | String getUrl() { |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 1807 | return UrlUtils.filteredUrl(mCurrentState.mUrl); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
John Reck | 49a603c | 2011-03-03 09:33:05 -0800 | [diff] [blame] | 1810 | String getOriginalUrl() { |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame] | 1811 | if (mCurrentState.mOriginalUrl == null) { |
| 1812 | return getUrl(); |
John Reck | 49a603c | 2011-03-03 09:33:05 -0800 | [diff] [blame] | 1813 | } |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame] | 1814 | return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl); |
John Reck | 49a603c | 2011-03-03 09:33:05 -0800 | [diff] [blame] | 1815 | } |
| 1816 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1817 | /** |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1818 | * Get the title of this tab. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1819 | */ |
| 1820 | String getTitle() { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1821 | if (mCurrentState.mTitle == null && mInPageLoad) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1822 | return mContext.getString(R.string.title_bar_loading); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1823 | } |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1824 | return mCurrentState.mTitle; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | /** |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1828 | * Get the favicon of this tab. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1829 | */ |
| 1830 | Bitmap getFavicon() { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1831 | if (mCurrentState.mFavicon != null) { |
| 1832 | return mCurrentState.mFavicon; |
| 1833 | } |
| 1834 | return getDefaultFavicon(mContext); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1835 | } |
| 1836 | |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 1837 | public boolean isBookmarkedSite() { |
| 1838 | return mCurrentState.mIsBookmarkedSite; |
| 1839 | } |
Rob Tsuk | f8bdfce | 2010-10-07 15:41:16 -0700 | [diff] [blame] | 1840 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1841 | /** |
| 1842 | * Return the tab's error console. Creates the console if createIfNEcessary |
| 1843 | * is true and we haven't already created the console. |
| 1844 | * @param createIfNecessary Flag to indicate if the console should be |
| 1845 | * created if it has not been already. |
| 1846 | * @return The tab's error console, or null if one has not been created and |
| 1847 | * createIfNecessary is false. |
| 1848 | */ |
| 1849 | ErrorConsoleView getErrorConsole(boolean createIfNecessary) { |
| 1850 | if (createIfNecessary && mErrorConsole == null) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1851 | mErrorConsole = new ErrorConsoleView(mContext); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1852 | mErrorConsole.setWebView(mMainView); |
| 1853 | } |
| 1854 | return mErrorConsole; |
| 1855 | } |
| 1856 | |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1857 | private void setLockIconType(LockIcon icon) { |
| 1858 | mCurrentState.mLockIcon = icon; |
| 1859 | mWebViewController.onUpdatedLockIcon(this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | /** |
| 1863 | * @return The tab's lock icon type. |
| 1864 | */ |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1865 | LockIcon getLockIconType() { |
| 1866 | return mCurrentState.mLockIcon; |
| 1867 | } |
| 1868 | |
| 1869 | int getLoadProgress() { |
| 1870 | if (mInPageLoad) { |
| 1871 | return mPageLoadProgress; |
| 1872 | } |
| 1873 | return 100; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | /** |
| 1877 | * @return TRUE if onPageStarted is called while onPageFinished is not |
| 1878 | * called yet. |
| 1879 | */ |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1880 | boolean inPageLoad() { |
| 1881 | return mInPageLoad; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1882 | } |
| 1883 | |
| 1884 | // force mInLoad to be false. This should only be called before closing the |
| 1885 | // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1886 | void clearInPageLoad() { |
| 1887 | mInPageLoad = false; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1888 | } |
| 1889 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1890 | /** |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1891 | * @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] | 1892 | */ |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1893 | public Bundle saveState() { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1894 | // If the WebView is null it means we ran low on memory and we already |
| 1895 | // stored the saved state in mSavedState. |
| 1896 | if (mMainView == null) { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1897 | return mSavedState; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1898 | } |
John Reck | 6c2e2f3 | 2011-08-22 13:41:23 -0700 | [diff] [blame] | 1899 | |
| 1900 | if (TextUtils.isEmpty(mCurrentState.mUrl)) { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1901 | return null; |
John Reck | 24f1826 | 2011-06-17 14:47:20 -0700 | [diff] [blame] | 1902 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1903 | |
| 1904 | mSavedState = new Bundle(); |
John Reck | 6c2e2f3 | 2011-08-22 13:41:23 -0700 | [diff] [blame] | 1905 | WebBackForwardList savedList = mMainView.saveState(mSavedState); |
| 1906 | if (savedList == null || savedList.getSize() == 0) { |
| 1907 | Log.w(LOGTAG, "Failed to save back/forward list for " |
| 1908 | + mCurrentState.mUrl); |
| 1909 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1910 | |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1911 | mSavedState.putLong(ID, mId); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1912 | mSavedState.putString(CURRURL, mCurrentState.mUrl); |
| 1913 | mSavedState.putString(CURRTITLE, mCurrentState.mTitle); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1914 | mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled()); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1915 | if (mAppId != null) { |
| 1916 | mSavedState.putString(APPID, mAppId); |
| 1917 | } |
Michael Kolb | e28b347 | 2011-08-04 16:54:31 -0700 | [diff] [blame] | 1918 | mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1919 | // Remember the parent tab so the relationship can be restored. |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1920 | if (mParent != null) { |
| 1921 | mSavedState.putLong(PARENTTAB, mParent.mId); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1922 | } |
John Reck | b0a86db | 2011-05-24 14:05:58 -0700 | [diff] [blame] | 1923 | mSavedState.putBoolean(USERAGENT, |
| 1924 | mSettings.hasDesktopUseragent(getWebView())); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1925 | return mSavedState; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | /* |
| 1929 | * Restore the state of the tab. |
| 1930 | */ |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1931 | private void restoreState(Bundle b) { |
| 1932 | mSavedState = b; |
| 1933 | if (mSavedState == null) { |
| 1934 | return; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1935 | } |
| 1936 | // Restore the internal state even if the WebView fails to restore. |
| 1937 | // 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] | 1938 | mId = b.getLong(ID); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1939 | mAppId = b.getString(APPID); |
Michael Kolb | e28b347 | 2011-08-04 16:54:31 -0700 | [diff] [blame] | 1940 | mCloseOnBack = b.getBoolean(CLOSEFLAG); |
John Reck | b0a86db | 2011-05-24 14:05:58 -0700 | [diff] [blame] | 1941 | if (b.getBoolean(USERAGENT) |
| 1942 | != mSettings.hasDesktopUseragent(getWebView())) { |
| 1943 | mSettings.toggleDesktopUseragent(getWebView()); |
| 1944 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1945 | String url = b.getString(CURRURL); |
| 1946 | String title = b.getString(CURRTITLE); |
| 1947 | boolean incognito = b.getBoolean(INCOGNITO); |
| 1948 | mCurrentState = new PageState(mContext, incognito, url, null); |
| 1949 | mCurrentState.mTitle = title; |
| 1950 | synchronized (Tab.this) { |
| 1951 | if (mCapture != null) { |
| 1952 | BackgroundHandler.execute(mLoadThumbnail); |
| 1953 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1954 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1955 | } |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 1956 | |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 1957 | public void updateBookmarkedStatus() { |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 1958 | mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback); |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 1959 | } |
| 1960 | |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 1961 | private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback |
| 1962 | = new DataController.OnQueryUrlIsBookmark() { |
| 1963 | @Override |
| 1964 | public void onQueryUrlIsBookmark(String url, boolean isBookmark) { |
| 1965 | if (mCurrentState.mUrl.equals(url)) { |
| 1966 | mCurrentState.mIsBookmarkedSite = isBookmark; |
| 1967 | mWebViewController.bookmarkedStatusHasChanged(Tab.this); |
| 1968 | } |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 1969 | } |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 1970 | }; |
Michael Kolb | 1acef69 | 2011-03-08 14:12:06 -0800 | [diff] [blame] | 1971 | |
Michael Kolb | eb95db4 | 2011-03-03 10:38:40 -0800 | [diff] [blame] | 1972 | public Bitmap getScreenshot() { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1973 | synchronized (Tab.this) { |
| 1974 | return mCapture; |
| 1975 | } |
Michael Kolb | eb95db4 | 2011-03-03 10:38:40 -0800 | [diff] [blame] | 1976 | } |
| 1977 | |
John Reck | 541f55a | 2011-06-07 16:34:43 -0700 | [diff] [blame] | 1978 | public boolean isSnapshot() { |
John Reck | 541f55a | 2011-06-07 16:34:43 -0700 | [diff] [blame] | 1979 | return false; |
| 1980 | } |
| 1981 | |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 1982 | public ContentValues createSnapshotValues() { |
| 1983 | if (mMainView == null) return null; |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 1984 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 1985 | try { |
| 1986 | GZIPOutputStream stream = new GZIPOutputStream(bos); |
| 1987 | if (!mMainView.saveViewState(stream)) { |
| 1988 | return null; |
| 1989 | } |
| 1990 | stream.flush(); |
| 1991 | stream.close(); |
| 1992 | } catch (Exception e) { |
| 1993 | Log.w(LOGTAG, "Failed to save view state", e); |
John Reck | 541f55a | 2011-06-07 16:34:43 -0700 | [diff] [blame] | 1994 | return null; |
| 1995 | } |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 1996 | byte[] data = bos.toByteArray(); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 1997 | ContentValues values = new ContentValues(); |
| 1998 | values.put(Snapshots.TITLE, mCurrentState.mTitle); |
| 1999 | values.put(Snapshots.URL, mCurrentState.mUrl); |
| 2000 | values.put(Snapshots.VIEWSTATE, data); |
| 2001 | values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor()); |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 2002 | values.put(Snapshots.DATE_CREATED, System.currentTimeMillis()); |
| 2003 | values.put(Snapshots.FAVICON, compressBitmap(getFavicon())); |
| 2004 | Bitmap screenshot = Controller.createScreenshot(mMainView, |
| 2005 | Controller.getDesiredThumbnailWidth(mContext), |
| 2006 | Controller.getDesiredThumbnailHeight(mContext)); |
| 2007 | values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot)); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 2008 | return values; |
John Reck | 541f55a | 2011-06-07 16:34:43 -0700 | [diff] [blame] | 2009 | } |
| 2010 | |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 2011 | public byte[] compressBitmap(Bitmap bitmap) { |
| 2012 | if (bitmap == null) { |
| 2013 | return null; |
| 2014 | } |
| 2015 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 2016 | bitmap.compress(CompressFormat.PNG, 100, stream); |
| 2017 | return stream.toByteArray(); |
| 2018 | } |
| 2019 | |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2020 | public void loadUrl(String url, Map<String, String> headers) { |
| 2021 | if (mMainView != null) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 2022 | mCurrentState = new PageState(mContext, false, url, null); |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 2023 | mWebViewController.onPageStarted(this, mMainView, null); |
| 2024 | mMainView.loadUrl(url, headers); |
| 2025 | } |
| 2026 | } |
| 2027 | |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 2028 | protected void capture() { |
| 2029 | if (mMainView == null || mCapture == null) return; |
| 2030 | Canvas c = new Canvas(mCapture); |
| 2031 | final int left = mMainView.getScrollX(); |
| 2032 | final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight(); |
| 2033 | c.translate(-left, -top); |
| 2034 | float scale = mCaptureWidth / (float) mMainView.getWidth(); |
| 2035 | c.scale(scale, scale, left, top); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 2036 | if (mMainView instanceof BrowserWebView) { |
| 2037 | ((BrowserWebView)mMainView).drawContent(c); |
| 2038 | } else { |
| 2039 | mMainView.draw(c); |
| 2040 | } |
Dianne Hackborn | 43cfe8a | 2011-08-02 16:59:35 -0700 | [diff] [blame] | 2041 | c.setBitmap(null); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 2042 | mHandler.removeMessages(MSG_CAPTURE); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 2043 | persistThumbnail(); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 2044 | TabControl tc = mWebViewController.getTabControl(); |
| 2045 | if (tc != null) { |
| 2046 | OnThumbnailUpdatedListener updateListener |
| 2047 | = tc.getOnThumbnailUpdatedListener(); |
| 2048 | if (updateListener != null) { |
| 2049 | updateListener.onThumbnailUpdated(this); |
| 2050 | } |
| 2051 | } |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | @Override |
| 2055 | public void onNewPicture(WebView view, Picture picture) { |
| 2056 | //update screenshot |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 2057 | postCapture(); |
| 2058 | } |
| 2059 | |
| 2060 | private void postCapture() { |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 2061 | if (!mHandler.hasMessages(MSG_CAPTURE)) { |
| 2062 | mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY); |
| 2063 | } |
| 2064 | } |
| 2065 | |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 2066 | public boolean canGoBack() { |
| 2067 | return mMainView != null ? mMainView.canGoBack() : false; |
| 2068 | } |
| 2069 | |
| 2070 | public boolean canGoForward() { |
| 2071 | return mMainView != null ? mMainView.canGoForward() : false; |
| 2072 | } |
| 2073 | |
| 2074 | public void goBack() { |
| 2075 | if (mMainView != null) { |
| 2076 | mMainView.goBack(); |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | public void goForward() { |
| 2081 | if (mMainView != null) { |
| 2082 | mMainView.goForward(); |
| 2083 | } |
| 2084 | } |
| 2085 | |
Mathew Inwood | 1dd8e82 | 2011-08-03 14:34:29 +0100 | [diff] [blame] | 2086 | /** |
| 2087 | * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL |
| 2088 | * to be added to the stack. |
| 2089 | * |
| 2090 | * This is used to ensure that preloaded URLs that are not subsequently seen by the user do |
| 2091 | * not appear in the back stack. |
| 2092 | */ |
| 2093 | public void clearBackStackWhenItemAdded(String urlToMatch) { |
| 2094 | mClearHistoryMatchUrl = urlToMatch; |
| 2095 | } |
| 2096 | |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 2097 | protected void persistThumbnail() { |
| 2098 | BackgroundHandler.execute(mSaveThumbnail); |
| 2099 | } |
| 2100 | |
| 2101 | protected void deleteThumbnail() { |
| 2102 | BackgroundHandler.execute(mDeleteThumbnail); |
| 2103 | } |
| 2104 | |
| 2105 | private void updateCaptureFromBlob(byte[] blob) { |
| 2106 | synchronized (Tab.this) { |
| 2107 | if (mCapture == null) { |
| 2108 | return; |
| 2109 | } |
Michael Kolb | bd4c00a | 2011-08-02 11:27:12 -0700 | [diff] [blame] | 2110 | ByteBuffer buffer = ByteBuffer.wrap(blob); |
Michael Kolb | d837a11 | 2011-08-09 14:00:18 -0700 | [diff] [blame] | 2111 | try { |
| 2112 | mCapture.copyPixelsFromBuffer(buffer); |
| 2113 | } catch (RuntimeException rex) { |
| 2114 | Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: " |
| 2115 | + buffer.capacity() + " blob: " + blob.length |
| 2116 | + "capture: " + mCapture.getByteCount()); |
| 2117 | throw rex; |
Michael Kolb | bd4c00a | 2011-08-02 11:27:12 -0700 | [diff] [blame] | 2118 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | private byte[] getCaptureBlob() { |
| 2123 | synchronized (Tab.this) { |
| 2124 | if (mCapture == null) { |
| 2125 | return null; |
| 2126 | } |
| 2127 | ByteBuffer buffer = ByteBuffer.allocate(mCapture.getByteCount()); |
| 2128 | mCapture.copyPixelsToBuffer(buffer); |
| 2129 | return buffer.array(); |
| 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | private Runnable mSaveThumbnail = new Runnable() { |
| 2134 | |
| 2135 | @Override |
| 2136 | public void run() { |
| 2137 | byte[] blob = getCaptureBlob(); |
| 2138 | if (blob == null) { |
| 2139 | return; |
| 2140 | } |
| 2141 | ContentResolver cr = mContext.getContentResolver(); |
| 2142 | ContentValues values = new ContentValues(); |
| 2143 | values.put(Thumbnails._ID, mId); |
| 2144 | values.put(Thumbnails.THUMBNAIL, blob); |
| 2145 | cr.insert(Thumbnails.CONTENT_URI, values); |
| 2146 | } |
| 2147 | }; |
| 2148 | |
| 2149 | private Runnable mDeleteThumbnail = new Runnable() { |
| 2150 | |
| 2151 | @Override |
| 2152 | public void run() { |
| 2153 | ContentResolver cr = mContext.getContentResolver(); |
| 2154 | try { |
| 2155 | cr.delete(ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId), |
| 2156 | null, null); |
| 2157 | } catch (Throwable t) {} |
| 2158 | } |
| 2159 | }; |
| 2160 | |
| 2161 | private Runnable mLoadThumbnail = new Runnable() { |
| 2162 | |
| 2163 | @Override |
| 2164 | public void run() { |
| 2165 | ContentResolver cr = mContext.getContentResolver(); |
| 2166 | Cursor c = null; |
| 2167 | try { |
| 2168 | Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId); |
| 2169 | c = cr.query(uri, new String[] {Thumbnails._ID, |
| 2170 | Thumbnails.THUMBNAIL}, null, null, null); |
| 2171 | if (c.moveToFirst()) { |
| 2172 | byte[] data = c.getBlob(1); |
| 2173 | if (data != null && data.length > 0) { |
| 2174 | updateCaptureFromBlob(data); |
| 2175 | } |
| 2176 | } |
| 2177 | } finally { |
| 2178 | if (c != null) { |
| 2179 | c.close(); |
| 2180 | } |
| 2181 | } |
| 2182 | } |
| 2183 | }; |
| 2184 | |
John Reck | 52be478 | 2011-08-26 15:37:29 -0700 | [diff] [blame^] | 2185 | @Override |
| 2186 | public String toString() { |
| 2187 | StringBuilder builder = new StringBuilder(100); |
| 2188 | builder.append(mId); |
| 2189 | builder.append(") has parent: "); |
| 2190 | if (getParent() != null) { |
| 2191 | builder.append("true["); |
| 2192 | builder.append(getParent().getId()); |
| 2193 | builder.append("]"); |
| 2194 | } else { |
| 2195 | builder.append("false"); |
| 2196 | } |
| 2197 | builder.append(", incog: "); |
| 2198 | builder.append(isPrivateBrowsingEnabled()); |
| 2199 | if (!isPrivateBrowsingEnabled()) { |
| 2200 | builder.append(", title: "); |
| 2201 | builder.append(getTitle()); |
| 2202 | builder.append(", url: "); |
| 2203 | builder.append(getUrl()); |
| 2204 | } |
| 2205 | return builder.toString(); |
| 2206 | } |
| 2207 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 2208 | } |