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