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