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