blob: ead1576d56fb8bea6f2f326ef466636643c1ba57 [file] [log] [blame]
Grace Kloba22ac16e2009-10-07 18:00:23 -07001/*
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
17package com.android.browser;
18
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070020import android.app.AlertDialog;
Leon Scroggins58d56c62010-01-28 15:12:40 -050021import android.app.SearchManager;
Grace Kloba22ac16e2009-10-07 18:00:23 -070022import android.content.ContentResolver;
John Reck1cf4b792011-07-26 10:22:22 -070023import android.content.ContentUris;
John Reckd8c74522011-06-14 08:45:00 -070024import android.content.ContentValues;
John Reck30c714c2010-12-16 17:30:34 -080025import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070026import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070027import android.content.DialogInterface.OnCancelListener;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050028import android.content.Intent;
John Reck1cf4b792011-07-26 10:22:22 -070029import android.database.Cursor;
Grace Kloba22ac16e2009-10-07 18:00:23 -070030import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070031import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070032import android.graphics.BitmapFactory;
33import android.graphics.Canvas;
34import android.graphics.Picture;
Grace Kloba22ac16e2009-10-07 18:00:23 -070035import android.net.Uri;
36import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070037import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070038import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000040import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070041import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070042import android.security.KeyChainAliasCallback;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050043import android.speech.RecognizerResultsIntent;
John Reck24f18262011-06-17 14:47:20 -070044import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070045import android.util.Log;
46import android.view.KeyEvent;
47import android.view.LayoutInflater;
48import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070049import android.view.ViewStub;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070050import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000051import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050052import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070053import android.webkit.GeolocationPermissions;
54import android.webkit.HttpAuthHandler;
55import android.webkit.SslErrorHandler;
56import android.webkit.URLUtil;
57import android.webkit.ValueCallback;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050058import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070059import android.webkit.WebChromeClient;
60import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080061import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070062import android.webkit.WebStorage;
63import android.webkit.WebView;
Michael Kolb9ef259a2011-07-12 15:33:08 -070064import android.webkit.WebView.PictureListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070065import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000066import android.widget.CheckBox;
Grace Kloba22ac16e2009-10-07 18:00:23 -070067import android.widget.LinearLayout;
68import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000069import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070070
John Reck541f55a2011-06-07 16:34:43 -070071import com.android.browser.homepages.HomeProvider;
John Reck1cf4b792011-07-26 10:22:22 -070072import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070073import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070074import com.android.common.speech.LoggingEvents;
75
76import java.io.ByteArrayOutputStream;
John Reck1cf4b792011-07-26 10:22:22 -070077import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070078import java.util.ArrayList;
79import java.util.HashMap;
80import java.util.Iterator;
81import java.util.LinkedList;
82import java.util.Map;
83import java.util.Vector;
John Reck8cc92352011-07-06 17:41:52 -070084import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070085
Grace Kloba22ac16e2009-10-07 18:00:23 -070086/**
87 * Class for maintaining Tabs with a main WebView and a subwindow.
88 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070089class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070090
Grace Kloba22ac16e2009-10-07 18:00:23 -070091 // Log Tag
92 private static final String LOGTAG = "Tab";
Mathew Inwood1dd8e822011-08-03 14:34:29 +010093 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000094 // Special case the logtag for messages for the Console to make it easier to
95 // filter them and match the logtag used for these messages in older versions
96 // of the browser.
97 private static final String CONSOLE_LOGTAG = "browser";
98
Michael Kolb9ef259a2011-07-12 15:33:08 -070099 private static final int MSG_CAPTURE = 42;
100 private static final int CAPTURE_DELAY = 500;
101
John Reck1cf4b792011-07-26 10:22:22 -0700102 private static Bitmap sDefaultFavicon;
103
John Reck30c714c2010-12-16 17:30:34 -0800104 public enum LockIcon {
105 LOCK_ICON_UNSECURE,
106 LOCK_ICON_SECURE,
107 LOCK_ICON_MIXED,
108 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700109
Michael Kolb14612442011-06-24 13:06:29 -0700110 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700111 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700112
Michael Kolbc831b632011-05-11 09:30:34 -0700113 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700114 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700115
Grace Kloba22ac16e2009-10-07 18:00:23 -0700116 // The Geolocation permissions prompt
117 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
118 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800119 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700120 // Main WebView
121 private WebView mMainView;
122 // Subwindow container
123 private View mSubViewContainer;
124 // Subwindow WebView
125 private WebView mSubView;
126 // Saved bundle for when we are running low on memory. It contains the
127 // information needed to restore the WebView if the user goes back to the
128 // tab.
129 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700130 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
131 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700132 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700133 // Tab that constructed by this Tab. This is used when this Tab is
134 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700135 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700136 // If true, the tab is in the foreground of the current activity.
137 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700138 // If true, the tab is in page loading state (after onPageStarted,
139 // before onPageFinsihed)
140 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800141 // The last reported progress of the current page
142 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000143 // The time the load started, used to find load page time
144 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700145 // Application identifier used to find tabs that another application wants
146 // to reuse.
147 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700148 // flag to indicate if tab should be closed on back
149 private boolean mCloseOnBack;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700150 // Keep the original url around to avoid killing the old WebView if the url
151 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700152 // Error console for the tab
153 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500154 // The listener that gets invoked when a download is started from the
155 // mMainView
156 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500157 // Listener used to know when we move forward or back in the history list.
158 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800159 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500160 // State of the auto-login request.
161 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700162
163 // AsyncTask for downloading touch icons
164 DownloadTouchIcon mTouchIconLoader;
165
John Reck35e9dd62011-04-25 09:01:54 -0700166 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700167 private int mCaptureWidth;
168 private int mCaptureHeight;
169 private Bitmap mCapture;
170 private Handler mHandler;
171
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100172 /**
173 * See {@link #clearBackStackWhenItemAdded(String)}.
174 */
175 private String mClearHistoryMatchUrl;
176
John Reck1cf4b792011-07-26 10:22:22 -0700177 private static synchronized Bitmap getDefaultFavicon(Context context) {
178 if (sDefaultFavicon == null) {
179 sDefaultFavicon = BitmapFactory.decodeResource(
180 context.getResources(), R.drawable.app_web_browser_sm);
181 }
182 return sDefaultFavicon;
183 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800184
John Reck30c714c2010-12-16 17:30:34 -0800185 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700186 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800187 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700188 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800189 String mTitle;
190 LockIcon mLockIcon;
191 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800192 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800193
194 PageState(Context c, boolean incognito) {
195 if (incognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700196 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800197 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800198 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700199 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800200 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800201 }
John Reck1cf4b792011-07-26 10:22:22 -0700202 mFavicon = null;
John Reck30c714c2010-12-16 17:30:34 -0800203 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
204 }
205
206 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reckdb22ec42011-06-29 11:31:24 -0700207 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800208 mTitle = null;
209 if (URLUtil.isHttpsUrl(url)) {
210 mLockIcon = LockIcon.LOCK_ICON_SECURE;
211 } else {
212 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
213 }
John Reck1cf4b792011-07-26 10:22:22 -0700214 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800215 }
John Reck1cf4b792011-07-26 10:22:22 -0700216
Grace Kloba22ac16e2009-10-07 18:00:23 -0700217 }
218
John Reck30c714c2010-12-16 17:30:34 -0800219 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700220 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800221
Grace Kloba22ac16e2009-10-07 18:00:23 -0700222 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700223 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700224 static final String CURRURL = "currentUrl";
225 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700226 static final String PARENTTAB = "parentTab";
227 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700228 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700229 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700230 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700231
232 // -------------------------------------------------------------------------
233
Leon Scroggins58d56c62010-01-28 15:12:40 -0500234 /**
235 * Private information regarding the latest voice search. If the Tab is not
236 * in voice search mode, this will be null.
237 */
238 private VoiceSearchData mVoiceSearchData;
239 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400240 * Remove voice search mode from this tab.
241 */
242 public void revertVoiceSearchMode() {
243 if (mVoiceSearchData != null) {
244 mVoiceSearchData = null;
245 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700246 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400247 }
248 }
249 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700250
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400251 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500252 * Return whether the tab is in voice search mode.
253 */
254 public boolean isInVoiceSearchMode() {
255 return mVoiceSearchData != null;
256 }
257 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400258 * Return true if the Tab is in voice search mode and the voice search
259 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500260 */
261 public boolean voiceSearchSourceIsGoogle() {
262 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
263 }
264 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500265 * Get the title to display for the current voice search page. If the Tab
266 * is not in voice search mode, return null.
267 */
268 public String getVoiceDisplayTitle() {
269 if (mVoiceSearchData == null) return null;
270 return mVoiceSearchData.mLastVoiceSearchTitle;
271 }
272 /**
273 * Get the latest array of voice search results, to be passed to the
274 * BrowserProvider. If the Tab is not in voice search mode, return null.
275 */
276 public ArrayList<String> getVoiceSearchResults() {
277 if (mVoiceSearchData == null) return null;
278 return mVoiceSearchData.mVoiceSearchResults;
279 }
280 /**
281 * Activate voice search mode.
282 * @param intent Intent which has the results to use, or an index into the
283 * results when reusing the old results.
284 */
285 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500286 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500287 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500288 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500289 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500290 ArrayList<String> urls = intent.getStringArrayListExtra(
291 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
292 ArrayList<String> htmls = intent.getStringArrayListExtra(
293 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
294 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
295 RecognizerResultsIntent
296 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500297 // This tab is now entering voice search mode for the first time, or
298 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500299 int size = results.size();
300 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500301 throw new AssertionError("improper extras passed in Intent");
302 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500303 if (htmls == null || htmls.size() != size || baseUrls == null ||
304 (baseUrls.size() != size && baseUrls.size() != 1)) {
305 // If either of these arrays are empty/incorrectly sized, ignore
306 // them.
307 htmls = null;
308 baseUrls = null;
309 }
310 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
311 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500312 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
313 RecognizerResultsIntent
314 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500315 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
316 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400317 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500318 }
319 String extraData = intent.getStringExtra(
320 SearchManager.EXTRA_DATA_KEY);
321 if (extraData != null) {
322 index = Integer.parseInt(extraData);
323 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
324 throw new AssertionError("index must be less than "
325 + "size of mVoiceSearchResults");
326 }
327 if (mVoiceSearchData.mSourceIsGoogle) {
328 Intent logIntent = new Intent(
329 LoggingEvents.ACTION_LOG_EVENT);
330 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
331 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
332 logIntent.putExtra(
333 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
334 index);
Michael Kolb14612442011-06-24 13:06:29 -0700335 mContext.sendBroadcast(logIntent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500336 }
337 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400338 // Copy the Intent, so that each history item will have its own
339 // Intent, with different (or none) extra data.
340 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
341 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
342 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500343 }
344 }
345 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500346 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500347 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700348 mWebViewController.activateVoiceSearchMode(
349 mVoiceSearchData.mLastVoiceSearchTitle,
350 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500351 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500352 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
353 // When index was found it was already ensured that it was valid
354 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
355 if (uriString != null) {
356 Uri dataUri = Uri.parse(uriString);
357 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
358 dataUri.getScheme())) {
359 // If there is only one base URL, use it. If there are
360 // more, there will be one for each index, so use the base
361 // URL corresponding to the index.
362 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
363 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
364 index : 0);
365 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
366 mMainView.loadDataWithBaseURL(baseUrl,
367 uriString.substring(RecognizerResultsIntent
368 .URI_SCHEME_INLINE.length() + 1), "text/html",
369 "utf-8", baseUrl);
370 return;
371 }
372 }
373 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500374 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500375 = mVoiceSearchData.mVoiceSearchUrls.get(index);
376 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700377 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500378 mVoiceSearchData.mLastVoiceSearchTitle);
379 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500380 Map<String, String> headers = null;
381 if (mVoiceSearchData.mHeaders != null) {
382 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
383 : index;
384 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
385 if (bundle != null && !bundle.isEmpty()) {
386 Iterator<String> iter = bundle.keySet().iterator();
387 headers = new HashMap<String, String>();
388 while (iter.hasNext()) {
389 String key = iter.next();
390 headers.put(key, bundle.getString(key));
391 }
392 }
393 }
394 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500395 }
396 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500397 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500398 ArrayList<String> urls, ArrayList<String> htmls,
399 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500400 mVoiceSearchResults = results;
401 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500402 mVoiceSearchHtmls = htmls;
403 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500404 }
405 /*
406 * ArrayList of suggestions to be displayed when opening the
407 * SearchManager
408 */
409 public ArrayList<String> mVoiceSearchResults;
410 /*
411 * ArrayList of urls, associated with the suggestions in
412 * mVoiceSearchResults.
413 */
414 public ArrayList<String> mVoiceSearchUrls;
415 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500416 * ArrayList holding content to load for each item in
417 * mVoiceSearchResults.
418 */
419 public ArrayList<String> mVoiceSearchHtmls;
420 /*
421 * ArrayList holding base urls for the items in mVoiceSearchResults.
422 * If non null, this will either have the same size as
423 * mVoiceSearchResults or have a size of 1, in which case all will use
424 * the same base url
425 */
426 public ArrayList<String> mVoiceSearchBaseUrls;
427 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500428 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500429 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500430 */
431 public String mLastVoiceSearchUrl;
432 /**
433 * The last title used for voice search. Needed to update the title bar
434 * when switching tabs.
435 */
436 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500437 /**
438 * Whether the Intent which turned on voice search mode contained the
439 * String signifying that Google was the source.
440 */
441 public boolean mSourceIsGoogle;
442 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500443 * List of headers to be passed into the WebView containing location
444 * information
445 */
446 public ArrayList<Bundle> mHeaders;
447 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500448 * The Intent used to invoke voice search. Placed on the
449 * WebHistoryItem so that when coming back to a previous voice search
450 * page we can again activate voice search.
451 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500452 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500453 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500454 * String used to identify Google as the source of voice search.
455 */
456 public static String SOURCE_IS_GOOGLE
457 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500458 }
459
Grace Kloba22ac16e2009-10-07 18:00:23 -0700460 // Container class for the next error dialog that needs to be displayed
461 private class ErrorDialog {
462 public final int mTitle;
463 public final String mDescription;
464 public final int mError;
465 ErrorDialog(int title, String desc, int error) {
466 mTitle = title;
467 mDescription = desc;
468 mError = error;
469 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700470 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700471
472 private void processNextError() {
473 if (mQueuedErrors == null) {
474 return;
475 }
476 // The first one is currently displayed so just remove it.
477 mQueuedErrors.removeFirst();
478 if (mQueuedErrors.size() == 0) {
479 mQueuedErrors = null;
480 return;
481 }
482 showError(mQueuedErrors.getFirst());
483 }
484
485 private DialogInterface.OnDismissListener mDialogListener =
486 new DialogInterface.OnDismissListener() {
487 public void onDismiss(DialogInterface d) {
488 processNextError();
489 }
490 };
491 private LinkedList<ErrorDialog> mQueuedErrors;
492
493 private void queueError(int err, String desc) {
494 if (mQueuedErrors == null) {
495 mQueuedErrors = new LinkedList<ErrorDialog>();
496 }
497 for (ErrorDialog d : mQueuedErrors) {
498 if (d.mError == err) {
499 // Already saw a similar error, ignore the new one.
500 return;
501 }
502 }
503 ErrorDialog errDialog = new ErrorDialog(
504 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
505 R.string.browserFrameFileErrorLabel :
506 R.string.browserFrameNetworkErrorLabel,
507 desc, err);
508 mQueuedErrors.addLast(errDialog);
509
510 // Show the dialog now if the queue was empty and it is in foreground
511 if (mQueuedErrors.size() == 1 && mInForeground) {
512 showError(errDialog);
513 }
514 }
515
516 private void showError(ErrorDialog errDialog) {
517 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700518 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700519 .setTitle(errDialog.mTitle)
520 .setMessage(errDialog.mDescription)
521 .setPositiveButton(R.string.ok, null)
522 .create();
523 d.setOnDismissListener(mDialogListener);
524 d.show();
525 }
526 }
527
528 // -------------------------------------------------------------------------
529 // WebViewClient implementation for the main WebView
530 // -------------------------------------------------------------------------
531
532 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500533 private Message mDontResend;
534 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700535 @Override
536 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700537 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800538 mPageLoadProgress = 0;
Michael Kolb14612442011-06-24 13:06:29 -0700539 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800540 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000541 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500542 if (mVoiceSearchData != null
543 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500544 if (mVoiceSearchData.mSourceIsGoogle) {
545 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
546 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700547 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500548 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400549 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500550 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700551
Grace Kloba22ac16e2009-10-07 18:00:23 -0700552
553 // If we start a touch icon load and then load a new page, we don't
554 // want to cancel the current touch icon loader. But, we do want to
555 // create a new one when the touch icon url is known.
556 if (mTouchIconLoader != null) {
557 mTouchIconLoader.mTab = null;
558 mTouchIconLoader = null;
559 }
560
561 // reset the error console
562 if (mErrorConsole != null) {
563 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700564 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700565 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
566 }
567 }
568
Patrick Scott92066772011-03-10 08:46:27 -0500569 // Cancel the auto-login process.
570 if (mDeviceAccountLogin != null) {
571 mDeviceAccountLogin.cancel();
572 mDeviceAccountLogin = null;
573 mWebViewController.hideAutoLogin(Tab.this);
574 }
575
Grace Kloba22ac16e2009-10-07 18:00:23 -0700576 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800577 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500578
John Recke969cc52010-12-21 17:24:43 -0800579 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700580 }
581
582 @Override
583 public void onPageFinished(WebView view, String url) {
John Recka1696282011-07-08 14:10:37 -0700584 if (!mInPageLoad) {
585 // In page navigation links (www.something.com#footer) will
586 // trigger an onPageFinished which we don't care about.
587 return;
588 }
John Reck5b691842010-11-29 11:21:13 -0800589 if (!isPrivateBrowsingEnabled()) {
590 LogTag.logPageFinishedLoading(
591 url, SystemClock.uptimeMillis() - mLoadStartTime);
592 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700593 mInPageLoad = false;
John Reck1cf4b792011-07-26 10:22:22 -0700594 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800595 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700596 }
597
598 // return true if want to hijack the url to let another app to handle it
599 @Override
600 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400601 if (voiceSearchSourceIsGoogle()) {
602 // This method is called when the user clicks on a link.
603 // VoiceSearchMode is turned off when the user leaves the
604 // Google results page, so at this point the user must be on
605 // that page. If the user clicked a link on that page, assume
606 // that the voice search was effective, and broadcast an Intent
607 // so a receiver can take note of that fact.
608 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
609 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
610 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700611 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400612 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700613 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800614 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
615 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700616 } else {
617 return false;
618 }
619 }
620
621 /**
622 * Updates the lock icon. This method is called when we discover another
623 * resource to be loaded for this page (for example, javascript). While
624 * we update the icon type, we do not update the lock icon itself until
625 * we are done loading, it is slightly more secure this way.
626 */
627 @Override
628 public void onLoadResource(WebView view, String url) {
629 if (url != null && url.length() > 0) {
630 // It is only if the page claims to be secure that we may have
631 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800632 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700633 // If NOT a 'safe' url, change the lock to mixed content!
634 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
635 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800636 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700637 }
638 }
639 }
640 }
641
642 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700643 * Show a dialog informing the user of the network error reported by
644 * WebCore if it is in the foreground.
645 */
646 @Override
647 public void onReceivedError(WebView view, int errorCode,
648 String description, String failingUrl) {
649 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
650 errorCode != WebViewClient.ERROR_CONNECT &&
651 errorCode != WebViewClient.ERROR_BAD_URL &&
652 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
653 errorCode != WebViewClient.ERROR_FILE) {
654 queueError(errorCode, description);
655 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500656
657 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700658 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500659 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
660 + " " + description);
661 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700662 }
663
664 /**
665 * Check with the user if it is ok to resend POST data as the page they
666 * are trying to navigate to is the result of a POST.
667 */
668 @Override
669 public void onFormResubmission(WebView view, final Message dontResend,
670 final Message resend) {
671 if (!mInForeground) {
672 dontResend.sendToTarget();
673 return;
674 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500675 if (mDontResend != null) {
676 Log.w(LOGTAG, "onFormResubmission should not be called again "
677 + "while dialog is still up");
678 dontResend.sendToTarget();
679 return;
680 }
681 mDontResend = dontResend;
682 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700683 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700684 R.string.browserFrameFormResubmitLabel).setMessage(
685 R.string.browserFrameFormResubmitMessage)
686 .setPositiveButton(R.string.ok,
687 new DialogInterface.OnClickListener() {
688 public void onClick(DialogInterface dialog,
689 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500690 if (mResend != null) {
691 mResend.sendToTarget();
692 mResend = null;
693 mDontResend = null;
694 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700695 }
696 }).setNegativeButton(R.string.cancel,
697 new DialogInterface.OnClickListener() {
698 public void onClick(DialogInterface dialog,
699 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500700 if (mDontResend != null) {
701 mDontResend.sendToTarget();
702 mResend = null;
703 mDontResend = null;
704 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700705 }
706 }).setOnCancelListener(new OnCancelListener() {
707 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500708 if (mDontResend != null) {
709 mDontResend.sendToTarget();
710 mResend = null;
711 mDontResend = null;
712 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700713 }
714 }).show();
715 }
716
717 /**
718 * Insert the url into the visited history database.
719 * @param url The url to be inserted.
720 * @param isReload True if this url is being reloaded.
721 * FIXME: Not sure what to do when reloading the page.
722 */
723 @Override
724 public void doUpdateVisitedHistory(WebView view, String url,
725 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800726 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700727 }
728
729 /**
730 * Displays SSL error(s) dialog to the user.
731 */
732 @Override
733 public void onReceivedSslError(final WebView view,
734 final SslErrorHandler handler, final SslError error) {
735 if (!mInForeground) {
736 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800737 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700738 return;
739 }
John Reck35e9dd62011-04-25 09:01:54 -0700740 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700741 final LayoutInflater factory =
Michael Kolb14612442011-06-24 13:06:29 -0700742 LayoutInflater.from(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700743 final View warningsView =
744 factory.inflate(R.layout.ssl_warnings, null);
745 final LinearLayout placeholder =
746 (LinearLayout)warningsView.findViewById(R.id.placeholder);
747
748 if (error.hasError(SslError.SSL_UNTRUSTED)) {
749 LinearLayout ll = (LinearLayout)factory
750 .inflate(R.layout.ssl_warning, null);
751 ((TextView)ll.findViewById(R.id.warning))
752 .setText(R.string.ssl_untrusted);
753 placeholder.addView(ll);
754 }
755
756 if (error.hasError(SslError.SSL_IDMISMATCH)) {
757 LinearLayout ll = (LinearLayout)factory
758 .inflate(R.layout.ssl_warning, null);
759 ((TextView)ll.findViewById(R.id.warning))
760 .setText(R.string.ssl_mismatch);
761 placeholder.addView(ll);
762 }
763
764 if (error.hasError(SslError.SSL_EXPIRED)) {
765 LinearLayout ll = (LinearLayout)factory
766 .inflate(R.layout.ssl_warning, null);
767 ((TextView)ll.findViewById(R.id.warning))
768 .setText(R.string.ssl_expired);
769 placeholder.addView(ll);
770 }
771
772 if (error.hasError(SslError.SSL_NOTYETVALID)) {
773 LinearLayout ll = (LinearLayout)factory
774 .inflate(R.layout.ssl_warning, null);
775 ((TextView)ll.findViewById(R.id.warning))
776 .setText(R.string.ssl_not_yet_valid);
777 placeholder.addView(ll);
778 }
779
Michael Kolb14612442011-06-24 13:06:29 -0700780 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700781 R.string.security_warning).setIcon(
782 android.R.drawable.ic_dialog_alert).setView(
783 warningsView).setPositiveButton(R.string.ssl_continue,
784 new DialogInterface.OnClickListener() {
785 public void onClick(DialogInterface dialog,
786 int whichButton) {
787 handler.proceed();
788 }
789 }).setNeutralButton(R.string.view_certificate,
790 new DialogInterface.OnClickListener() {
791 public void onClick(DialogInterface dialog,
792 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700793 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700794 handler, error);
795 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000796 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700797 new DialogInterface.OnClickListener() {
798 public void onClick(DialogInterface dialog,
799 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800800 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700801 }
802 }).setOnCancelListener(
803 new DialogInterface.OnCancelListener() {
804 public void onCancel(DialogInterface dialog) {
805 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800806 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
807 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700808 }
809 }).show();
810 } else {
811 handler.proceed();
812 }
813 }
814
815 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700816 * Displays client certificate request to the user.
817 */
818 @Override
819 public void onReceivedClientCertRequest(final WebView view,
820 final ClientCertRequestHandler handler, final String host_and_port) {
821 if (!mInForeground) {
822 handler.ignore();
823 return;
824 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700825 int colon = host_and_port.lastIndexOf(':');
826 String host;
827 int port;
828 if (colon == -1) {
829 host = host_and_port;
830 port = -1;
831 } else {
832 String portString = host_and_port.substring(colon + 1);
833 try {
834 port = Integer.parseInt(portString);
835 host = host_and_port.substring(0, colon);
836 } catch (NumberFormatException e) {
837 host = host_and_port;
838 port = -1;
839 }
840 }
Michael Kolb14612442011-06-24 13:06:29 -0700841 KeyChain.choosePrivateKeyAlias(
842 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700843 @Override public void alias(String alias) {
844 if (alias == null) {
845 handler.cancel();
846 return;
847 }
Michael Kolb14612442011-06-24 13:06:29 -0700848 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700849 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700850 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700851 }
852
853 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700854 * Handles an HTTP authentication request.
855 *
856 * @param handler The authentication handler
857 * @param host The host
858 * @param realm The realm
859 */
860 @Override
861 public void onReceivedHttpAuthRequest(WebView view,
862 final HttpAuthHandler handler, final String host,
863 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700865 }
866
867 @Override
John Reck438bf462011-01-12 18:11:46 -0800868 public WebResourceResponse shouldInterceptRequest(WebView view,
869 String url) {
870 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700871 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800872 return res;
873 }
874
875 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700876 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
877 if (!mInForeground) {
878 return false;
879 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700880 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700881 }
882
883 @Override
884 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700885 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700886 return;
887 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700888 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700889 }
Patrick Scott92066772011-03-10 08:46:27 -0500890
891 @Override
892 public void onReceivedLoginRequest(WebView view, String realm,
893 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700894 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500895 .handleLogin(realm, account, args);
896 }
897
Grace Kloba22ac16e2009-10-07 18:00:23 -0700898 };
899
John Reck1cf4b792011-07-26 10:22:22 -0700900 private void syncCurrentState(WebView view, String url) {
901 // Sync state (in case of stop/timeout)
902 mCurrentState.mUrl = view.getUrl();
903 if (mCurrentState.mUrl == null) {
904 mCurrentState.mUrl = url != null ? url : "";
905 }
906 mCurrentState.mOriginalUrl = view.getOriginalUrl();
907 mCurrentState.mTitle = view.getTitle();
908 mCurrentState.mFavicon = view.getFavicon();
909 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
910 // In case we stop when loading an HTTPS page from an HTTP page
911 // but before a provisional load occurred
912 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
913 }
914 }
915
Patrick Scott92066772011-03-10 08:46:27 -0500916 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
917 // displayed.
918 void setDeviceAccountLogin(DeviceAccountLogin login) {
919 mDeviceAccountLogin = login;
920 }
921
922 // Returns non-null if the title bar should display the auto-login UI.
923 DeviceAccountLogin getDeviceAccountLogin() {
924 return mDeviceAccountLogin;
925 }
926
Grace Kloba22ac16e2009-10-07 18:00:23 -0700927 // -------------------------------------------------------------------------
928 // WebChromeClient implementation for the main WebView
929 // -------------------------------------------------------------------------
930
931 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
932 // Helper method to create a new tab or sub window.
933 private void createWindow(final boolean dialog, final Message msg) {
934 WebView.WebViewTransport transport =
935 (WebView.WebViewTransport) msg.obj;
936 if (dialog) {
937 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700939 transport.setWebView(mSubView);
940 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700941 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700942 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700943 transport.setWebView(newTab.getWebView());
944 }
945 msg.sendToTarget();
946 }
947
948 @Override
949 public boolean onCreateWindow(WebView view, final boolean dialog,
950 final boolean userGesture, final Message resultMsg) {
951 // only allow new window or sub window for the foreground case
952 if (!mInForeground) {
953 return false;
954 }
955 // Short-circuit if we can't create any more tabs or sub windows.
956 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700957 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700958 .setTitle(R.string.too_many_subwindows_dialog_title)
959 .setIcon(android.R.drawable.ic_dialog_alert)
960 .setMessage(R.string.too_many_subwindows_dialog_message)
961 .setPositiveButton(R.string.ok, null)
962 .show();
963 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700964 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700965 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700966 .setTitle(R.string.too_many_windows_dialog_title)
967 .setIcon(android.R.drawable.ic_dialog_alert)
968 .setMessage(R.string.too_many_windows_dialog_message)
969 .setPositiveButton(R.string.ok, null)
970 .show();
971 return false;
972 }
973
974 // Short-circuit if this was a user gesture.
975 if (userGesture) {
976 createWindow(dialog, resultMsg);
977 return true;
978 }
979
980 // Allow the popup and create the appropriate window.
981 final AlertDialog.OnClickListener allowListener =
982 new AlertDialog.OnClickListener() {
983 public void onClick(DialogInterface d,
984 int which) {
985 createWindow(dialog, resultMsg);
986 }
987 };
988
989 // Block the popup by returning a null WebView.
990 final AlertDialog.OnClickListener blockListener =
991 new AlertDialog.OnClickListener() {
992 public void onClick(DialogInterface d, int which) {
993 resultMsg.sendToTarget();
994 }
995 };
996
997 // Build a confirmation dialog to display to the user.
998 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700999 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -07001000 .setTitle(R.string.attention)
1001 .setIcon(android.R.drawable.ic_dialog_alert)
1002 .setMessage(R.string.popup_window_attempt)
1003 .setPositiveButton(R.string.allow, allowListener)
1004 .setNegativeButton(R.string.block, blockListener)
1005 .setCancelable(false)
1006 .create();
1007
1008 // Show the confirmation dialog.
1009 d.show();
1010 return true;
1011 }
1012
1013 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -05001014 public void onRequestFocus(WebView view) {
1015 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001016 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -05001017 }
1018 }
1019
1020 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001021 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -07001022 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001023 // JavaScript can only close popup window.
1024 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001025 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001026 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001027 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001028 }
1029 }
1030
1031 @Override
1032 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001033 mPageLoadProgress = newProgress;
1034 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001035 }
1036
1037 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001038 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001039 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001040 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001041 }
1042
1043 @Override
1044 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001045 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001046 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001047 }
1048
1049 @Override
1050 public void onReceivedTouchIconUrl(WebView view, String url,
1051 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001052 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001053 // Let precomposed icons take precedence over non-composed
1054 // icons.
1055 if (precomposed && mTouchIconLoader != null) {
1056 mTouchIconLoader.cancel(false);
1057 mTouchIconLoader = null;
1058 }
1059 // Have only one async task at a time.
1060 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001061 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001062 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001063 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001064 }
1065 }
1066
1067 @Override
1068 public void onShowCustomView(View view,
1069 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001070 Activity activity = mWebViewController.getActivity();
1071 if (activity != null) {
1072 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1073 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001074 }
1075
1076 @Override
1077 public void onShowCustomView(View view, int requestedOrientation,
1078 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001079 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001080 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001081 }
1082
1083 @Override
1084 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001085 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001086 }
1087
1088 /**
1089 * The origin has exceeded its database quota.
1090 * @param url the URL that exceeded the quota
1091 * @param databaseIdentifier the identifier of the database on which the
1092 * transaction that caused the quota overflow was run
1093 * @param currentQuota the current quota for the origin.
1094 * @param estimatedSize the estimated size of the database.
1095 * @param totalUsedQuota is the sum of all origins' quota.
1096 * @param quotaUpdater The callback to run when a decision to allow or
1097 * deny quota has been made. Don't forget to call this!
1098 */
1099 @Override
1100 public void onExceededDatabaseQuota(String url,
1101 String databaseIdentifier, long currentQuota, long estimatedSize,
1102 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001103 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001104 .onExceededDatabaseQuota(url, databaseIdentifier,
1105 currentQuota, estimatedSize, totalUsedQuota,
1106 quotaUpdater);
1107 }
1108
1109 /**
1110 * The Application Cache has exceeded its max size.
1111 * @param spaceNeeded is the amount of disk space that would be needed
1112 * in order for the last appcache operation to succeed.
1113 * @param totalUsedQuota is the sum of all origins' quota.
1114 * @param quotaUpdater A callback to inform the WebCore thread that a
1115 * new app cache size is available. This callback must always
1116 * be executed at some point to ensure that the sleeping
1117 * WebCore thread is woken up.
1118 */
1119 @Override
1120 public void onReachedMaxAppCacheSize(long spaceNeeded,
1121 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001122 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001123 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1124 quotaUpdater);
1125 }
1126
1127 /**
1128 * Instructs the browser to show a prompt to ask the user to set the
1129 * Geolocation permission state for the specified origin.
1130 * @param origin The origin for which Geolocation permissions are
1131 * requested.
1132 * @param callback The callback to call once the user has set the
1133 * Geolocation permission state.
1134 */
1135 @Override
1136 public void onGeolocationPermissionsShowPrompt(String origin,
1137 GeolocationPermissions.Callback callback) {
1138 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001139 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001140 }
1141 }
1142
1143 /**
1144 * Instructs the browser to hide the Geolocation permissions prompt.
1145 */
1146 @Override
1147 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001148 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001149 mGeolocationPermissionsPrompt.hide();
1150 }
1151 }
1152
Ben Murdoch65acc352009-11-19 18:16:04 +00001153 /* Adds a JavaScript error message to the system log and if the JS
1154 * console is enabled in the about:debug options, to that console
1155 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001156 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001157 */
1158 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001159 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001160 if (mInForeground) {
1161 // call getErrorConsole(true) so it will create one if needed
1162 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001163 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001164 if (mWebViewController.shouldShowErrorConsole()
1165 && errorConsole.getShowState() !=
1166 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001167 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1168 }
1169 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001170
Jeff Hamilton47654f42010-09-07 09:57:51 -05001171 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001172 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001173
Ben Murdochc42addf2010-01-28 15:19:59 +00001174 String message = "Console: " + consoleMessage.message() + " "
1175 + consoleMessage.sourceId() + ":"
1176 + consoleMessage.lineNumber();
1177
1178 switch (consoleMessage.messageLevel()) {
1179 case TIP:
1180 Log.v(CONSOLE_LOGTAG, message);
1181 break;
1182 case LOG:
1183 Log.i(CONSOLE_LOGTAG, message);
1184 break;
1185 case WARNING:
1186 Log.w(CONSOLE_LOGTAG, message);
1187 break;
1188 case ERROR:
1189 Log.e(CONSOLE_LOGTAG, message);
1190 break;
1191 case DEBUG:
1192 Log.d(CONSOLE_LOGTAG, message);
1193 break;
1194 }
1195
1196 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001197 }
1198
1199 /**
1200 * Ask the browser for an icon to represent a <video> element.
1201 * This icon will be used if the Web page did not specify a poster attribute.
1202 * @return Bitmap The icon or null if no such icon is available.
1203 */
1204 @Override
1205 public Bitmap getDefaultVideoPoster() {
1206 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001207 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001208 }
1209 return null;
1210 }
1211
1212 /**
1213 * Ask the host application for a custom progress view to show while
1214 * a <video> is loading.
1215 * @return View The progress view.
1216 */
1217 @Override
1218 public View getVideoLoadingProgressView() {
1219 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001220 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001221 }
1222 return null;
1223 }
1224
1225 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001226 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001227 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001228 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001229 } else {
1230 uploadMsg.onReceiveValue(null);
1231 }
1232 }
1233
1234 /**
1235 * Deliver a list of already-visited URLs
1236 */
1237 @Override
1238 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001239 mWebViewController.getVisitedHistory(callback);
1240 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001241
1242 @Override
1243 public void setupAutoFill(Message message) {
1244 // Prompt the user to set up their profile.
1245 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001246 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1247 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001248 Context.LAYOUT_INFLATER_SERVICE);
1249 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1250
1251 builder.setView(layout)
Ben Murdochb7e6f942011-07-08 13:00:21 +01001252 .setTitle(R.string.autofill_setup_dialog_title)
Ben Murdoch1d676b62011-01-17 12:54:24 +00001253 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1254 @Override
1255 public void onClick(DialogInterface dialog, int id) {
1256 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1257 R.id.setup_autofill_dialog_disable_autofill);
1258
1259 if (disableAutoFill.isChecked()) {
1260 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001261 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001262 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001263 R.string.autofill_setup_dialog_negative_toast,
1264 Toast.LENGTH_LONG).show();
1265 } else {
1266 // Take user to the AutoFill profile editor. When they return,
1267 // we will send the message that we pass here which will trigger
1268 // the form to get filled out with their new profile.
1269 mWebViewController.setupAutoFill(msg);
1270 }
1271 }
1272 })
1273 .setNegativeButton(R.string.cancel, null)
1274 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001275 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001276 };
1277
1278 // -------------------------------------------------------------------------
1279 // WebViewClient implementation for the sub window
1280 // -------------------------------------------------------------------------
1281
1282 // Subclass of WebViewClient used in subwindows to notify the main
1283 // WebViewClient of certain WebView activities.
1284 private static class SubWindowClient extends WebViewClient {
1285 // The main WebViewClient.
1286 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001287 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001288
Michael Kolb8233fac2010-10-26 16:08:53 -07001289 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001290 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001291 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001292 }
1293 @Override
1294 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1295 // Unlike the others, do not call mClient's version, which would
1296 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001297 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001298 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001299 }
1300 @Override
1301 public void doUpdateVisitedHistory(WebView view, String url,
1302 boolean isReload) {
1303 mClient.doUpdateVisitedHistory(view, url, isReload);
1304 }
1305 @Override
1306 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1307 return mClient.shouldOverrideUrlLoading(view, url);
1308 }
1309 @Override
1310 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1311 SslError error) {
1312 mClient.onReceivedSslError(view, handler, error);
1313 }
1314 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001315 public void onReceivedClientCertRequest(WebView view,
1316 ClientCertRequestHandler handler, String host_and_port) {
1317 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1318 }
1319 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001320 public void onReceivedHttpAuthRequest(WebView view,
1321 HttpAuthHandler handler, String host, String realm) {
1322 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1323 }
1324 @Override
1325 public void onFormResubmission(WebView view, Message dontResend,
1326 Message resend) {
1327 mClient.onFormResubmission(view, dontResend, resend);
1328 }
1329 @Override
1330 public void onReceivedError(WebView view, int errorCode,
1331 String description, String failingUrl) {
1332 mClient.onReceivedError(view, errorCode, description, failingUrl);
1333 }
1334 @Override
1335 public boolean shouldOverrideKeyEvent(WebView view,
1336 android.view.KeyEvent event) {
1337 return mClient.shouldOverrideKeyEvent(view, event);
1338 }
1339 @Override
1340 public void onUnhandledKeyEvent(WebView view,
1341 android.view.KeyEvent event) {
1342 mClient.onUnhandledKeyEvent(view, event);
1343 }
1344 }
1345
1346 // -------------------------------------------------------------------------
1347 // WebChromeClient implementation for the sub window
1348 // -------------------------------------------------------------------------
1349
1350 private class SubWindowChromeClient extends WebChromeClient {
1351 // The main WebChromeClient.
1352 private final WebChromeClient mClient;
1353
1354 SubWindowChromeClient(WebChromeClient client) {
1355 mClient = client;
1356 }
1357 @Override
1358 public void onProgressChanged(WebView view, int newProgress) {
1359 mClient.onProgressChanged(view, newProgress);
1360 }
1361 @Override
1362 public boolean onCreateWindow(WebView view, boolean dialog,
1363 boolean userGesture, android.os.Message resultMsg) {
1364 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1365 }
1366 @Override
1367 public void onCloseWindow(WebView window) {
1368 if (window != mSubView) {
1369 Log.e(LOGTAG, "Can't close the window");
1370 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001371 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001372 }
1373 }
1374
1375 // -------------------------------------------------------------------------
1376
1377 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001378 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001379 this(wvcontroller, w, null);
1380 }
1381
1382 Tab(WebViewController wvcontroller, Bundle state) {
1383 this(wvcontroller, null, state);
1384 }
1385
1386 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001387 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001388 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001389 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001390 mDataController = DataController.getInstance(mContext);
1391 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001392 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001393 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001394 mInForeground = false;
1395
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001396 mDownloadListener = new DownloadListener() {
1397 public void onDownloadStart(String url, String userAgent,
1398 String contentDisposition, String mimetype,
1399 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001400 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001401 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001402 }
1403 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001404 mWebBackForwardListClient = new WebBackForwardListClient() {
1405 @Override
1406 public void onNewHistoryItem(WebHistoryItem item) {
1407 if (isInVoiceSearchMode()) {
1408 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1409 }
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001410 if (mClearHistoryMatchUrl != null) {
1411 if (LOGD_ENABLED) {
1412 Log.d(LOGTAG, "onNewHistoryItem:\n\t" + item.getUrl() + "\n\t"
1413 + mClearHistoryMatchUrl);
1414 }
1415 if (TextUtils.equals(item.getOriginalUrl(), mClearHistoryMatchUrl)) {
1416 if (mMainView != null) {
1417 mMainView.clearHistory();
1418 }
1419 }
1420 mClearHistoryMatchUrl = null;
1421 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001422 }
1423 @Override
1424 public void onIndexChanged(WebHistoryItem item, int index) {
1425 Object data = item.getCustomData();
1426 if (data != null && data instanceof Intent) {
1427 activateVoiceSearchMode((Intent) data);
1428 }
1429 }
1430 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001431
John Reck1cf4b792011-07-26 10:22:22 -07001432 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1433 R.dimen.tab_thumbnail_width);
1434 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1435 R.dimen.tab_thumbnail_height);
1436 updateShouldCaptureThumbnails();
1437 restoreState(state);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001438 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001439 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001440 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001441 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001442 switch (m.what) {
1443 case MSG_CAPTURE:
1444 capture();
1445 break;
1446 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001447 }
1448 };
John Reck1cf4b792011-07-26 10:22:22 -07001449 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001450
John Reck1cf4b792011-07-26 10:22:22 -07001451 public void updateShouldCaptureThumbnails() {
1452 if (mWebViewController.shouldCaptureThumbnails()) {
1453 synchronized (Tab.this) {
1454 if (mCapture == null) {
1455 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1456 Bitmap.Config.RGB_565);
1457 if (mInForeground) {
1458 postCapture();
1459 }
1460 }
1461 }
1462 } else {
1463 synchronized (Tab.this) {
1464 mCapture = null;
1465 deleteThumbnail();
1466 }
1467 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001468 }
1469
Michael Kolb14612442011-06-24 13:06:29 -07001470 public void setController(WebViewController ctl) {
1471 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001472 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001473 }
1474
Michael Kolbc831b632011-05-11 09:30:34 -07001475 public void setId(long id) {
1476 mId = id;
1477 }
1478
1479 public long getId() {
1480 return mId;
1481 }
1482
Grace Kloba22ac16e2009-10-07 18:00:23 -07001483 /**
1484 * Sets the WebView for this tab, correctly removing the old WebView from
1485 * the container view.
1486 */
1487 void setWebView(WebView w) {
1488 if (mMainView == w) {
1489 return;
1490 }
Michael Kolba713ec82010-11-29 17:27:06 -08001491
Grace Kloba22ac16e2009-10-07 18:00:23 -07001492 // If the WebView is changing, the page will be reloaded, so any ongoing
1493 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001494 if (mGeolocationPermissionsPrompt != null) {
1495 mGeolocationPermissionsPrompt.hide();
1496 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001497
Michael Kolba713ec82010-11-29 17:27:06 -08001498 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001499
John Reck1cf4b792011-07-26 10:22:22 -07001500 if (mMainView != null) {
1501 if (w != null) {
1502 syncCurrentState(w, null);
1503 } else {
1504 mCurrentState = new PageState(mContext, false);
1505 }
1506 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001507 // set the new one
1508 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001509 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001510 if (mMainView != null) {
1511 mMainView.setWebViewClient(mWebViewClient);
1512 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001513 // Attach DownloadManager so that downloads can start in an active
1514 // or a non-active window. This can happen when going to a site that
1515 // does a redirect after a period of time. The user could have
1516 // switched to another tab while waiting for the download to start.
1517 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001518 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001519 mMainView.setPictureListener(this);
John Reck1cf4b792011-07-26 10:22:22 -07001520 if (mSavedState != null) {
1521 mMainView.restoreState(mSavedState);
1522 mSavedState = null;
1523 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001524 }
1525 }
1526
1527 /**
1528 * Destroy the tab's main WebView and subWindow if any
1529 */
1530 void destroy() {
1531 if (mMainView != null) {
1532 dismissSubWindow();
John Reckef654f12011-07-12 16:42:08 -07001533 // Make sure the embedded title bar isn't still attached
1534 mMainView.setEmbeddedTitleBar(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001535 // save the WebView to call destroy() after detach it from the tab
1536 WebView webView = mMainView;
1537 setWebView(null);
1538 webView.destroy();
1539 }
1540 }
1541
1542 /**
1543 * Remove the tab from the parent
1544 */
1545 void removeFromTree() {
1546 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001547 if (mChildren != null) {
1548 for(Tab t : mChildren) {
1549 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001550 }
1551 }
1552 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001553 if (mParent != null) {
1554 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001555 }
John Reck1cf4b792011-07-26 10:22:22 -07001556 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001557 }
1558
1559 /**
1560 * Create a new subwindow unless a subwindow already exists.
1561 * @return True if a new subwindow was created. False if one already exists.
1562 */
1563 boolean createSubWindow() {
1564 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001565 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001566 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001567 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001568 mSubView.setWebChromeClient(new SubWindowChromeClient(
1569 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001570 // Set a different DownloadListener for the mSubView, since it will
1571 // just need to dismiss the mSubView, rather than close the Tab
1572 mSubView.setDownloadListener(new DownloadListener() {
1573 public void onDownloadStart(String url, String userAgent,
1574 String contentDisposition, String mimetype,
1575 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001576 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001577 contentDisposition, mimetype, contentLength);
1578 if (mSubView.copyBackForwardList().getSize() == 0) {
1579 // This subwindow was opened for the sole purpose of
1580 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001581 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001582 }
1583 }
1584 });
Michael Kolb14612442011-06-24 13:06:29 -07001585 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001586 return true;
1587 }
1588 return false;
1589 }
1590
1591 /**
1592 * Dismiss the subWindow for the tab.
1593 */
1594 void dismissSubWindow() {
1595 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001596 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001597 mSubView.destroy();
1598 mSubView = null;
1599 mSubViewContainer = null;
1600 }
1601 }
1602
Grace Kloba22ac16e2009-10-07 18:00:23 -07001603
1604 /**
1605 * Set the parent tab of this tab.
1606 */
Michael Kolbc831b632011-05-11 09:30:34 -07001607 void setParent(Tab parent) {
1608 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001609 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001610 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001611 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001612 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001613 if (mSavedState != null) {
1614 if (parent == null) {
1615 mSavedState.remove(PARENTTAB);
1616 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001617 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001618 }
1619 }
John Reckb0a86db2011-05-24 14:05:58 -07001620
1621 // Sync the WebView useragent with the parent
1622 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1623 != mSettings.hasDesktopUseragent(getWebView())) {
1624 mSettings.toggleDesktopUseragent(getWebView());
1625 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001626 }
1627
1628 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001629 * If this Tab was created through another Tab, then this method returns
1630 * that Tab.
1631 * @return the Tab parent or null
1632 */
1633 public Tab getParent() {
1634 return mParent;
1635 }
1636
1637 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001638 * When a Tab is created through the content of another Tab, then we
1639 * associate the Tabs.
1640 * @param child the Tab that was created from this Tab
1641 */
1642 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001643 if (mChildren == null) {
1644 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001645 }
Michael Kolbc831b632011-05-11 09:30:34 -07001646 mChildren.add(child);
1647 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001648 }
1649
Michael Kolbc831b632011-05-11 09:30:34 -07001650 Vector<Tab> getChildren() {
1651 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001652 }
1653
1654 void resume() {
1655 if (mMainView != null) {
1656 mMainView.onResume();
1657 if (mSubView != null) {
1658 mSubView.onResume();
1659 }
1660 }
1661 }
1662
1663 void pause() {
1664 if (mMainView != null) {
1665 mMainView.onPause();
1666 if (mSubView != null) {
1667 mSubView.onPause();
1668 }
1669 }
1670 }
1671
1672 void putInForeground() {
1673 mInForeground = true;
1674 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001675 Activity activity = mWebViewController.getActivity();
1676 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001677 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001678 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001679 }
1680 // Show the pending error dialog if the queue is not empty
1681 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1682 showError(mQueuedErrors.getFirst());
1683 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001684 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001685 }
1686
1687 void putInBackground() {
1688 mInForeground = false;
1689 pause();
1690 mMainView.setOnCreateContextMenuListener(null);
1691 if (mSubView != null) {
1692 mSubView.setOnCreateContextMenuListener(null);
1693 }
1694 }
1695
Michael Kolb8233fac2010-10-26 16:08:53 -07001696 boolean inForeground() {
1697 return mInForeground;
1698 }
1699
Grace Kloba22ac16e2009-10-07 18:00:23 -07001700 /**
1701 * Return the top window of this tab; either the subwindow if it is not
1702 * null or the main window.
1703 * @return The top window of this tab.
1704 */
1705 WebView getTopWindow() {
1706 if (mSubView != null) {
1707 return mSubView;
1708 }
1709 return mMainView;
1710 }
1711
1712 /**
1713 * Return the main window of this tab. Note: if a tab is freed in the
1714 * background, this can return null. It is only guaranteed to be
1715 * non-null for the current tab.
1716 * @return The main WebView of this tab.
1717 */
1718 WebView getWebView() {
1719 return mMainView;
1720 }
1721
Michael Kolba713ec82010-11-29 17:27:06 -08001722 void setViewContainer(View container) {
1723 mContainer = container;
1724 }
1725
Michael Kolb8233fac2010-10-26 16:08:53 -07001726 View getViewContainer() {
1727 return mContainer;
1728 }
1729
Grace Kloba22ac16e2009-10-07 18:00:23 -07001730 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001731 * Return whether private browsing is enabled for the main window of
1732 * this tab.
1733 * @return True if private browsing is enabled.
1734 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001735 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001736 WebView webView = getWebView();
1737 if (webView == null) {
1738 return false;
1739 }
1740 return webView.isPrivateBrowsingEnabled();
1741 }
1742
1743 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001744 * Return the subwindow of this tab or null if there is no subwindow.
1745 * @return The subwindow of this tab or null.
1746 */
1747 WebView getSubWebView() {
1748 return mSubView;
1749 }
1750
Michael Kolb1514bb72010-11-22 09:11:48 -08001751 void setSubWebView(WebView subView) {
1752 mSubView = subView;
1753 }
1754
Michael Kolb8233fac2010-10-26 16:08:53 -07001755 View getSubViewContainer() {
1756 return mSubViewContainer;
1757 }
1758
Michael Kolb1514bb72010-11-22 09:11:48 -08001759 void setSubViewContainer(View subViewContainer) {
1760 mSubViewContainer = subViewContainer;
1761 }
1762
Grace Kloba22ac16e2009-10-07 18:00:23 -07001763 /**
1764 * @return The geolocation permissions prompt for this tab.
1765 */
1766 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001767 if (mGeolocationPermissionsPrompt == null) {
1768 ViewStub stub = (ViewStub) mContainer
1769 .findViewById(R.id.geolocation_permissions_prompt);
1770 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1771 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001772 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001773 return mGeolocationPermissionsPrompt;
1774 }
1775
1776 /**
1777 * @return The application id string
1778 */
1779 String getAppId() {
1780 return mAppId;
1781 }
1782
1783 /**
1784 * Set the application id string
1785 * @param id
1786 */
1787 void setAppId(String id) {
1788 mAppId = id;
1789 }
1790
Michael Kolbe28b3472011-08-04 16:54:31 -07001791 boolean closeOnBack() {
1792 return mCloseOnBack;
1793 }
1794
1795 void setCloseOnBack(boolean close) {
1796 mCloseOnBack = close;
1797 }
1798
Grace Kloba22ac16e2009-10-07 18:00:23 -07001799 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001800 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001801 }
1802
John Reck49a603c2011-03-03 09:33:05 -08001803 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001804 if (mCurrentState.mOriginalUrl == null) {
1805 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001806 }
John Reckdb22ec42011-06-29 11:31:24 -07001807 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001808 }
1809
Grace Kloba22ac16e2009-10-07 18:00:23 -07001810 /**
John Reck30c714c2010-12-16 17:30:34 -08001811 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001812 */
1813 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001814 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001815 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001816 }
John Reck30c714c2010-12-16 17:30:34 -08001817 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001818 }
1819
1820 /**
John Reck30c714c2010-12-16 17:30:34 -08001821 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001822 */
1823 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001824 if (mCurrentState.mFavicon != null) {
1825 return mCurrentState.mFavicon;
1826 }
1827 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001828 }
1829
John Recke969cc52010-12-21 17:24:43 -08001830 public boolean isBookmarkedSite() {
1831 return mCurrentState.mIsBookmarkedSite;
1832 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001833
Grace Kloba22ac16e2009-10-07 18:00:23 -07001834 /**
1835 * Return the tab's error console. Creates the console if createIfNEcessary
1836 * is true and we haven't already created the console.
1837 * @param createIfNecessary Flag to indicate if the console should be
1838 * created if it has not been already.
1839 * @return The tab's error console, or null if one has not been created and
1840 * createIfNecessary is false.
1841 */
1842 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1843 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001844 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001845 mErrorConsole.setWebView(mMainView);
1846 }
1847 return mErrorConsole;
1848 }
1849
John Reck30c714c2010-12-16 17:30:34 -08001850 private void setLockIconType(LockIcon icon) {
1851 mCurrentState.mLockIcon = icon;
1852 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001853 }
1854
1855 /**
1856 * @return The tab's lock icon type.
1857 */
John Reck30c714c2010-12-16 17:30:34 -08001858 LockIcon getLockIconType() {
1859 return mCurrentState.mLockIcon;
1860 }
1861
1862 int getLoadProgress() {
1863 if (mInPageLoad) {
1864 return mPageLoadProgress;
1865 }
1866 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001867 }
1868
1869 /**
1870 * @return TRUE if onPageStarted is called while onPageFinished is not
1871 * called yet.
1872 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001873 boolean inPageLoad() {
1874 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001875 }
1876
1877 // force mInLoad to be false. This should only be called before closing the
1878 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001879 void clearInPageLoad() {
1880 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001881 }
1882
Grace Kloba22ac16e2009-10-07 18:00:23 -07001883 /**
John Reck1cf4b792011-07-26 10:22:22 -07001884 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001885 */
John Reck1cf4b792011-07-26 10:22:22 -07001886 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001887 // If the WebView is null it means we ran low on memory and we already
1888 // stored the saved state in mSavedState.
1889 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001890 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001891 }
John Reck24f18262011-06-17 14:47:20 -07001892 // If the tab is the homepage or has no URL, don't save it
1893 String homepage = BrowserSettings.getInstance().getHomePage();
1894 if (TextUtils.equals(homepage, mCurrentState.mUrl)
1895 || TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001896 return null;
John Reck24f18262011-06-17 14:47:20 -07001897 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001898
1899 mSavedState = new Bundle();
John Reck541f55a2011-06-07 16:34:43 -07001900 mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001901
Michael Kolbc831b632011-05-11 09:30:34 -07001902 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001903 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1904 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001905 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001906 if (mAppId != null) {
1907 mSavedState.putString(APPID, mAppId);
1908 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001909 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001910 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001911 if (mParent != null) {
1912 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001913 }
John Reckb0a86db2011-05-24 14:05:58 -07001914 mSavedState.putBoolean(USERAGENT,
1915 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001916 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001917 }
1918
1919 /*
1920 * Restore the state of the tab.
1921 */
John Reck1cf4b792011-07-26 10:22:22 -07001922 private void restoreState(Bundle b) {
1923 mSavedState = b;
1924 if (mSavedState == null) {
1925 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001926 }
1927 // Restore the internal state even if the WebView fails to restore.
1928 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001929 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001930 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001931 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reckb0a86db2011-05-24 14:05:58 -07001932 if (b.getBoolean(USERAGENT)
1933 != mSettings.hasDesktopUseragent(getWebView())) {
1934 mSettings.toggleDesktopUseragent(getWebView());
1935 }
John Reck1cf4b792011-07-26 10:22:22 -07001936 String url = b.getString(CURRURL);
1937 String title = b.getString(CURRTITLE);
1938 boolean incognito = b.getBoolean(INCOGNITO);
1939 mCurrentState = new PageState(mContext, incognito, url, null);
1940 mCurrentState.mTitle = title;
1941 synchronized (Tab.this) {
1942 if (mCapture != null) {
1943 BackgroundHandler.execute(mLoadThumbnail);
1944 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001945 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001946 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001947
Leon Scroggins1961ed22010-12-07 15:22:21 -05001948 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001949 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001950 }
1951
John Recke969cc52010-12-21 17:24:43 -08001952 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1953 = new DataController.OnQueryUrlIsBookmark() {
1954 @Override
1955 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1956 if (mCurrentState.mUrl.equals(url)) {
1957 mCurrentState.mIsBookmarkedSite = isBookmark;
1958 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1959 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001960 }
John Recke969cc52010-12-21 17:24:43 -08001961 };
Michael Kolb1acef692011-03-08 14:12:06 -08001962
Michael Kolbeb95db42011-03-03 10:38:40 -08001963 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001964 synchronized (Tab.this) {
1965 return mCapture;
1966 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001967 }
1968
John Reck541f55a2011-06-07 16:34:43 -07001969 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001970 return false;
1971 }
1972
John Reckd8c74522011-06-14 08:45:00 -07001973 public ContentValues createSnapshotValues() {
1974 if (mMainView == null) return null;
John Reck8cc92352011-07-06 17:41:52 -07001975 ByteArrayOutputStream bos = new ByteArrayOutputStream();
1976 try {
1977 GZIPOutputStream stream = new GZIPOutputStream(bos);
1978 if (!mMainView.saveViewState(stream)) {
1979 return null;
1980 }
1981 stream.flush();
1982 stream.close();
1983 } catch (Exception e) {
1984 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07001985 return null;
1986 }
John Reck8cc92352011-07-06 17:41:52 -07001987 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07001988 ContentValues values = new ContentValues();
1989 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1990 values.put(Snapshots.URL, mCurrentState.mUrl);
1991 values.put(Snapshots.VIEWSTATE, data);
1992 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001993 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1994 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
1995 Bitmap screenshot = Controller.createScreenshot(mMainView,
1996 Controller.getDesiredThumbnailWidth(mContext),
1997 Controller.getDesiredThumbnailHeight(mContext));
1998 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001999 return values;
John Reck541f55a2011-06-07 16:34:43 -07002000 }
2001
John Reck8cc92352011-07-06 17:41:52 -07002002 public byte[] compressBitmap(Bitmap bitmap) {
2003 if (bitmap == null) {
2004 return null;
2005 }
2006 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2007 bitmap.compress(CompressFormat.PNG, 100, stream);
2008 return stream.toByteArray();
2009 }
2010
John Reck26b18322011-06-21 13:08:58 -07002011 public void loadUrl(String url, Map<String, String> headers) {
2012 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07002013 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002014 mWebViewController.onPageStarted(this, mMainView, null);
2015 mMainView.loadUrl(url, headers);
2016 }
2017 }
2018
Michael Kolb9ef259a2011-07-12 15:33:08 -07002019 protected void capture() {
2020 if (mMainView == null || mCapture == null) return;
2021 Canvas c = new Canvas(mCapture);
2022 final int left = mMainView.getScrollX();
2023 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
2024 c.translate(-left, -top);
2025 float scale = mCaptureWidth / (float) mMainView.getWidth();
2026 c.scale(scale, scale, left, top);
2027 mMainView.draw(c);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002028 c.setBitmap(null);
John Reck1cf4b792011-07-26 10:22:22 -07002029 persistThumbnail();
Michael Kolb9ef259a2011-07-12 15:33:08 -07002030 }
2031
2032 @Override
2033 public void onNewPicture(WebView view, Picture picture) {
2034 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002035 postCapture();
2036 }
2037
2038 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002039 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2040 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2041 }
2042 }
2043
John Reckef654f12011-07-12 16:42:08 -07002044 public boolean canGoBack() {
2045 return mMainView != null ? mMainView.canGoBack() : false;
2046 }
2047
2048 public boolean canGoForward() {
2049 return mMainView != null ? mMainView.canGoForward() : false;
2050 }
2051
2052 public void goBack() {
2053 if (mMainView != null) {
2054 mMainView.goBack();
2055 }
2056 }
2057
2058 public void goForward() {
2059 if (mMainView != null) {
2060 mMainView.goForward();
2061 }
2062 }
2063
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002064 /**
2065 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2066 * to be added to the stack.
2067 *
2068 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2069 * not appear in the back stack.
2070 */
2071 public void clearBackStackWhenItemAdded(String urlToMatch) {
2072 mClearHistoryMatchUrl = urlToMatch;
2073 }
2074
John Reck1cf4b792011-07-26 10:22:22 -07002075 protected void persistThumbnail() {
2076 BackgroundHandler.execute(mSaveThumbnail);
2077 }
2078
2079 protected void deleteThumbnail() {
2080 BackgroundHandler.execute(mDeleteThumbnail);
2081 }
2082
2083 private void updateCaptureFromBlob(byte[] blob) {
2084 synchronized (Tab.this) {
2085 if (mCapture == null) {
2086 return;
2087 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002088 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002089 try {
2090 mCapture.copyPixelsFromBuffer(buffer);
2091 } catch (RuntimeException rex) {
2092 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2093 + buffer.capacity() + " blob: " + blob.length
2094 + "capture: " + mCapture.getByteCount());
2095 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002096 }
John Reck1cf4b792011-07-26 10:22:22 -07002097 }
2098 }
2099
2100 private byte[] getCaptureBlob() {
2101 synchronized (Tab.this) {
2102 if (mCapture == null) {
2103 return null;
2104 }
2105 ByteBuffer buffer = ByteBuffer.allocate(mCapture.getByteCount());
2106 mCapture.copyPixelsToBuffer(buffer);
2107 return buffer.array();
2108 }
2109 }
2110
2111 private Runnable mSaveThumbnail = new Runnable() {
2112
2113 @Override
2114 public void run() {
2115 byte[] blob = getCaptureBlob();
2116 if (blob == null) {
2117 return;
2118 }
2119 ContentResolver cr = mContext.getContentResolver();
2120 ContentValues values = new ContentValues();
2121 values.put(Thumbnails._ID, mId);
2122 values.put(Thumbnails.THUMBNAIL, blob);
2123 cr.insert(Thumbnails.CONTENT_URI, values);
2124 }
2125 };
2126
2127 private Runnable mDeleteThumbnail = new Runnable() {
2128
2129 @Override
2130 public void run() {
2131 ContentResolver cr = mContext.getContentResolver();
2132 try {
2133 cr.delete(ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId),
2134 null, null);
2135 } catch (Throwable t) {}
2136 }
2137 };
2138
2139 private Runnable mLoadThumbnail = new Runnable() {
2140
2141 @Override
2142 public void run() {
2143 ContentResolver cr = mContext.getContentResolver();
2144 Cursor c = null;
2145 try {
2146 Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId);
2147 c = cr.query(uri, new String[] {Thumbnails._ID,
2148 Thumbnails.THUMBNAIL}, null, null, null);
2149 if (c.moveToFirst()) {
2150 byte[] data = c.getBlob(1);
2151 if (data != null && data.length > 0) {
2152 updateCaptureFromBlob(data);
2153 }
2154 }
2155 } finally {
2156 if (c != null) {
2157 c.close();
2158 }
2159 }
2160 }
2161 };
2162
Grace Kloba22ac16e2009-10-07 18:00:23 -07002163}