blob: 6aa86834d23c46ad6d3aee7c38b09db11aee74c2 [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";
Ben Murdochc42addf2010-01-28 15:19:59 +000093 // Special case the logtag for messages for the Console to make it easier to
94 // filter them and match the logtag used for these messages in older versions
95 // of the browser.
96 private static final String CONSOLE_LOGTAG = "browser";
97
Michael Kolb9ef259a2011-07-12 15:33:08 -070098 private static final int MSG_CAPTURE = 42;
99 private static final int CAPTURE_DELAY = 500;
100
John Reck1cf4b792011-07-26 10:22:22 -0700101 private static Bitmap sDefaultFavicon;
102
John Reck30c714c2010-12-16 17:30:34 -0800103 public enum LockIcon {
104 LOCK_ICON_UNSECURE,
105 LOCK_ICON_SECURE,
106 LOCK_ICON_MIXED,
107 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700108
Michael Kolb14612442011-06-24 13:06:29 -0700109 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700110 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700111
Michael Kolbc831b632011-05-11 09:30:34 -0700112 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700113 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700114
Grace Kloba22ac16e2009-10-07 18:00:23 -0700115 // The Geolocation permissions prompt
116 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
117 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800118 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700119 // Main WebView
120 private WebView mMainView;
121 // Subwindow container
122 private View mSubViewContainer;
123 // Subwindow WebView
124 private WebView mSubView;
125 // Saved bundle for when we are running low on memory. It contains the
126 // information needed to restore the WebView if the user goes back to the
127 // tab.
128 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700129 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
130 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700131 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700132 // Tab that constructed by this Tab. This is used when this Tab is
133 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700134 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700135 // If true, the tab is in the foreground of the current activity.
136 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700137 // If true, the tab is in page loading state (after onPageStarted,
138 // before onPageFinsihed)
139 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800140 // The last reported progress of the current page
141 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000142 // The time the load started, used to find load page time
143 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700144 // Application identifier used to find tabs that another application wants
145 // to reuse.
146 private String mAppId;
147 // Keep the original url around to avoid killing the old WebView if the url
148 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700149 // Error console for the tab
150 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500151 // The listener that gets invoked when a download is started from the
152 // mMainView
153 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500154 // Listener used to know when we move forward or back in the history list.
155 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800156 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500157 // State of the auto-login request.
158 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700159
160 // AsyncTask for downloading touch icons
161 DownloadTouchIcon mTouchIconLoader;
162
John Reck35e9dd62011-04-25 09:01:54 -0700163 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700164 private int mCaptureWidth;
165 private int mCaptureHeight;
166 private Bitmap mCapture;
167 private Handler mHandler;
168
John Reck1cf4b792011-07-26 10:22:22 -0700169 private static synchronized Bitmap getDefaultFavicon(Context context) {
170 if (sDefaultFavicon == null) {
171 sDefaultFavicon = BitmapFactory.decodeResource(
172 context.getResources(), R.drawable.app_web_browser_sm);
173 }
174 return sDefaultFavicon;
175 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800176
John Reck30c714c2010-12-16 17:30:34 -0800177 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700178 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800179 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700180 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800181 String mTitle;
182 LockIcon mLockIcon;
183 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800184 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800185
186 PageState(Context c, boolean incognito) {
187 if (incognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700188 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800189 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800190 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700191 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800192 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800193 }
John Reck1cf4b792011-07-26 10:22:22 -0700194 mFavicon = null;
John Reck30c714c2010-12-16 17:30:34 -0800195 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
196 }
197
198 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reckdb22ec42011-06-29 11:31:24 -0700199 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800200 mTitle = null;
201 if (URLUtil.isHttpsUrl(url)) {
202 mLockIcon = LockIcon.LOCK_ICON_SECURE;
203 } else {
204 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
205 }
John Reck1cf4b792011-07-26 10:22:22 -0700206 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800207 }
John Reck1cf4b792011-07-26 10:22:22 -0700208
Grace Kloba22ac16e2009-10-07 18:00:23 -0700209 }
210
John Reck30c714c2010-12-16 17:30:34 -0800211 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700212 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800213
Grace Kloba22ac16e2009-10-07 18:00:23 -0700214 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700215 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700216 static final String CURRURL = "currentUrl";
217 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700218 static final String PARENTTAB = "parentTab";
219 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700220 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700221 static final String USERAGENT = "useragent";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700222
223 // -------------------------------------------------------------------------
224
Leon Scroggins58d56c62010-01-28 15:12:40 -0500225 /**
226 * Private information regarding the latest voice search. If the Tab is not
227 * in voice search mode, this will be null.
228 */
229 private VoiceSearchData mVoiceSearchData;
230 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400231 * Remove voice search mode from this tab.
232 */
233 public void revertVoiceSearchMode() {
234 if (mVoiceSearchData != null) {
235 mVoiceSearchData = null;
236 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700237 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400238 }
239 }
240 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700241
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400242 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500243 * Return whether the tab is in voice search mode.
244 */
245 public boolean isInVoiceSearchMode() {
246 return mVoiceSearchData != null;
247 }
248 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400249 * Return true if the Tab is in voice search mode and the voice search
250 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500251 */
252 public boolean voiceSearchSourceIsGoogle() {
253 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
254 }
255 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500256 * Get the title to display for the current voice search page. If the Tab
257 * is not in voice search mode, return null.
258 */
259 public String getVoiceDisplayTitle() {
260 if (mVoiceSearchData == null) return null;
261 return mVoiceSearchData.mLastVoiceSearchTitle;
262 }
263 /**
264 * Get the latest array of voice search results, to be passed to the
265 * BrowserProvider. If the Tab is not in voice search mode, return null.
266 */
267 public ArrayList<String> getVoiceSearchResults() {
268 if (mVoiceSearchData == null) return null;
269 return mVoiceSearchData.mVoiceSearchResults;
270 }
271 /**
272 * Activate voice search mode.
273 * @param intent Intent which has the results to use, or an index into the
274 * results when reusing the old results.
275 */
276 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500277 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500278 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500279 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500280 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500281 ArrayList<String> urls = intent.getStringArrayListExtra(
282 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
283 ArrayList<String> htmls = intent.getStringArrayListExtra(
284 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
285 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
286 RecognizerResultsIntent
287 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500288 // This tab is now entering voice search mode for the first time, or
289 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500290 int size = results.size();
291 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500292 throw new AssertionError("improper extras passed in Intent");
293 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500294 if (htmls == null || htmls.size() != size || baseUrls == null ||
295 (baseUrls.size() != size && baseUrls.size() != 1)) {
296 // If either of these arrays are empty/incorrectly sized, ignore
297 // them.
298 htmls = null;
299 baseUrls = null;
300 }
301 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
302 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500303 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
304 RecognizerResultsIntent
305 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500306 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
307 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400308 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500309 }
310 String extraData = intent.getStringExtra(
311 SearchManager.EXTRA_DATA_KEY);
312 if (extraData != null) {
313 index = Integer.parseInt(extraData);
314 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
315 throw new AssertionError("index must be less than "
316 + "size of mVoiceSearchResults");
317 }
318 if (mVoiceSearchData.mSourceIsGoogle) {
319 Intent logIntent = new Intent(
320 LoggingEvents.ACTION_LOG_EVENT);
321 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
322 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
323 logIntent.putExtra(
324 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
325 index);
Michael Kolb14612442011-06-24 13:06:29 -0700326 mContext.sendBroadcast(logIntent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500327 }
328 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400329 // Copy the Intent, so that each history item will have its own
330 // Intent, with different (or none) extra data.
331 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
332 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
333 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500334 }
335 }
336 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500337 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500338 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700339 mWebViewController.activateVoiceSearchMode(
340 mVoiceSearchData.mLastVoiceSearchTitle,
341 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500342 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500343 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
344 // When index was found it was already ensured that it was valid
345 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
346 if (uriString != null) {
347 Uri dataUri = Uri.parse(uriString);
348 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
349 dataUri.getScheme())) {
350 // If there is only one base URL, use it. If there are
351 // more, there will be one for each index, so use the base
352 // URL corresponding to the index.
353 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
354 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
355 index : 0);
356 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
357 mMainView.loadDataWithBaseURL(baseUrl,
358 uriString.substring(RecognizerResultsIntent
359 .URI_SCHEME_INLINE.length() + 1), "text/html",
360 "utf-8", baseUrl);
361 return;
362 }
363 }
364 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500365 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500366 = mVoiceSearchData.mVoiceSearchUrls.get(index);
367 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700368 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500369 mVoiceSearchData.mLastVoiceSearchTitle);
370 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500371 Map<String, String> headers = null;
372 if (mVoiceSearchData.mHeaders != null) {
373 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
374 : index;
375 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
376 if (bundle != null && !bundle.isEmpty()) {
377 Iterator<String> iter = bundle.keySet().iterator();
378 headers = new HashMap<String, String>();
379 while (iter.hasNext()) {
380 String key = iter.next();
381 headers.put(key, bundle.getString(key));
382 }
383 }
384 }
385 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500386 }
387 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500388 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500389 ArrayList<String> urls, ArrayList<String> htmls,
390 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500391 mVoiceSearchResults = results;
392 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500393 mVoiceSearchHtmls = htmls;
394 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500395 }
396 /*
397 * ArrayList of suggestions to be displayed when opening the
398 * SearchManager
399 */
400 public ArrayList<String> mVoiceSearchResults;
401 /*
402 * ArrayList of urls, associated with the suggestions in
403 * mVoiceSearchResults.
404 */
405 public ArrayList<String> mVoiceSearchUrls;
406 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500407 * ArrayList holding content to load for each item in
408 * mVoiceSearchResults.
409 */
410 public ArrayList<String> mVoiceSearchHtmls;
411 /*
412 * ArrayList holding base urls for the items in mVoiceSearchResults.
413 * If non null, this will either have the same size as
414 * mVoiceSearchResults or have a size of 1, in which case all will use
415 * the same base url
416 */
417 public ArrayList<String> mVoiceSearchBaseUrls;
418 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500419 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500420 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500421 */
422 public String mLastVoiceSearchUrl;
423 /**
424 * The last title used for voice search. Needed to update the title bar
425 * when switching tabs.
426 */
427 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500428 /**
429 * Whether the Intent which turned on voice search mode contained the
430 * String signifying that Google was the source.
431 */
432 public boolean mSourceIsGoogle;
433 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500434 * List of headers to be passed into the WebView containing location
435 * information
436 */
437 public ArrayList<Bundle> mHeaders;
438 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500439 * The Intent used to invoke voice search. Placed on the
440 * WebHistoryItem so that when coming back to a previous voice search
441 * page we can again activate voice search.
442 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500443 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500444 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500445 * String used to identify Google as the source of voice search.
446 */
447 public static String SOURCE_IS_GOOGLE
448 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500449 }
450
Grace Kloba22ac16e2009-10-07 18:00:23 -0700451 // Container class for the next error dialog that needs to be displayed
452 private class ErrorDialog {
453 public final int mTitle;
454 public final String mDescription;
455 public final int mError;
456 ErrorDialog(int title, String desc, int error) {
457 mTitle = title;
458 mDescription = desc;
459 mError = error;
460 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700461 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700462
463 private void processNextError() {
464 if (mQueuedErrors == null) {
465 return;
466 }
467 // The first one is currently displayed so just remove it.
468 mQueuedErrors.removeFirst();
469 if (mQueuedErrors.size() == 0) {
470 mQueuedErrors = null;
471 return;
472 }
473 showError(mQueuedErrors.getFirst());
474 }
475
476 private DialogInterface.OnDismissListener mDialogListener =
477 new DialogInterface.OnDismissListener() {
478 public void onDismiss(DialogInterface d) {
479 processNextError();
480 }
481 };
482 private LinkedList<ErrorDialog> mQueuedErrors;
483
484 private void queueError(int err, String desc) {
485 if (mQueuedErrors == null) {
486 mQueuedErrors = new LinkedList<ErrorDialog>();
487 }
488 for (ErrorDialog d : mQueuedErrors) {
489 if (d.mError == err) {
490 // Already saw a similar error, ignore the new one.
491 return;
492 }
493 }
494 ErrorDialog errDialog = new ErrorDialog(
495 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
496 R.string.browserFrameFileErrorLabel :
497 R.string.browserFrameNetworkErrorLabel,
498 desc, err);
499 mQueuedErrors.addLast(errDialog);
500
501 // Show the dialog now if the queue was empty and it is in foreground
502 if (mQueuedErrors.size() == 1 && mInForeground) {
503 showError(errDialog);
504 }
505 }
506
507 private void showError(ErrorDialog errDialog) {
508 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700509 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700510 .setTitle(errDialog.mTitle)
511 .setMessage(errDialog.mDescription)
512 .setPositiveButton(R.string.ok, null)
513 .create();
514 d.setOnDismissListener(mDialogListener);
515 d.show();
516 }
517 }
518
519 // -------------------------------------------------------------------------
520 // WebViewClient implementation for the main WebView
521 // -------------------------------------------------------------------------
522
523 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500524 private Message mDontResend;
525 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700526 @Override
527 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700528 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800529 mPageLoadProgress = 0;
Michael Kolb14612442011-06-24 13:06:29 -0700530 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800531 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000532 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500533 if (mVoiceSearchData != null
534 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500535 if (mVoiceSearchData.mSourceIsGoogle) {
536 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
537 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700538 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500539 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400540 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500541 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700542
Grace Kloba22ac16e2009-10-07 18:00:23 -0700543
544 // If we start a touch icon load and then load a new page, we don't
545 // want to cancel the current touch icon loader. But, we do want to
546 // create a new one when the touch icon url is known.
547 if (mTouchIconLoader != null) {
548 mTouchIconLoader.mTab = null;
549 mTouchIconLoader = null;
550 }
551
552 // reset the error console
553 if (mErrorConsole != null) {
554 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700555 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700556 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
557 }
558 }
559
Patrick Scott92066772011-03-10 08:46:27 -0500560 // Cancel the auto-login process.
561 if (mDeviceAccountLogin != null) {
562 mDeviceAccountLogin.cancel();
563 mDeviceAccountLogin = null;
564 mWebViewController.hideAutoLogin(Tab.this);
565 }
566
Grace Kloba22ac16e2009-10-07 18:00:23 -0700567 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800568 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500569
John Recke969cc52010-12-21 17:24:43 -0800570 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700571 }
572
573 @Override
574 public void onPageFinished(WebView view, String url) {
John Recka1696282011-07-08 14:10:37 -0700575 if (!mInPageLoad) {
576 // In page navigation links (www.something.com#footer) will
577 // trigger an onPageFinished which we don't care about.
578 return;
579 }
John Reck5b691842010-11-29 11:21:13 -0800580 if (!isPrivateBrowsingEnabled()) {
581 LogTag.logPageFinishedLoading(
582 url, SystemClock.uptimeMillis() - mLoadStartTime);
583 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700584 mInPageLoad = false;
John Reck1cf4b792011-07-26 10:22:22 -0700585 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800586 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700587 }
588
589 // return true if want to hijack the url to let another app to handle it
590 @Override
591 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400592 if (voiceSearchSourceIsGoogle()) {
593 // This method is called when the user clicks on a link.
594 // VoiceSearchMode is turned off when the user leaves the
595 // Google results page, so at this point the user must be on
596 // that page. If the user clicked a link on that page, assume
597 // that the voice search was effective, and broadcast an Intent
598 // so a receiver can take note of that fact.
599 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
600 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
601 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700602 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400603 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700604 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800605 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
606 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700607 } else {
608 return false;
609 }
610 }
611
612 /**
613 * Updates the lock icon. This method is called when we discover another
614 * resource to be loaded for this page (for example, javascript). While
615 * we update the icon type, we do not update the lock icon itself until
616 * we are done loading, it is slightly more secure this way.
617 */
618 @Override
619 public void onLoadResource(WebView view, String url) {
620 if (url != null && url.length() > 0) {
621 // It is only if the page claims to be secure that we may have
622 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800623 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700624 // If NOT a 'safe' url, change the lock to mixed content!
625 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
626 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800627 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700628 }
629 }
630 }
631 }
632
633 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700634 * Show a dialog informing the user of the network error reported by
635 * WebCore if it is in the foreground.
636 */
637 @Override
638 public void onReceivedError(WebView view, int errorCode,
639 String description, String failingUrl) {
640 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
641 errorCode != WebViewClient.ERROR_CONNECT &&
642 errorCode != WebViewClient.ERROR_BAD_URL &&
643 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
644 errorCode != WebViewClient.ERROR_FILE) {
645 queueError(errorCode, description);
646 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500647
648 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700649 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500650 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
651 + " " + description);
652 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700653 }
654
655 /**
656 * Check with the user if it is ok to resend POST data as the page they
657 * are trying to navigate to is the result of a POST.
658 */
659 @Override
660 public void onFormResubmission(WebView view, final Message dontResend,
661 final Message resend) {
662 if (!mInForeground) {
663 dontResend.sendToTarget();
664 return;
665 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500666 if (mDontResend != null) {
667 Log.w(LOGTAG, "onFormResubmission should not be called again "
668 + "while dialog is still up");
669 dontResend.sendToTarget();
670 return;
671 }
672 mDontResend = dontResend;
673 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700674 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700675 R.string.browserFrameFormResubmitLabel).setMessage(
676 R.string.browserFrameFormResubmitMessage)
677 .setPositiveButton(R.string.ok,
678 new DialogInterface.OnClickListener() {
679 public void onClick(DialogInterface dialog,
680 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500681 if (mResend != null) {
682 mResend.sendToTarget();
683 mResend = null;
684 mDontResend = null;
685 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700686 }
687 }).setNegativeButton(R.string.cancel,
688 new DialogInterface.OnClickListener() {
689 public void onClick(DialogInterface dialog,
690 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500691 if (mDontResend != null) {
692 mDontResend.sendToTarget();
693 mResend = null;
694 mDontResend = null;
695 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700696 }
697 }).setOnCancelListener(new OnCancelListener() {
698 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500699 if (mDontResend != null) {
700 mDontResend.sendToTarget();
701 mResend = null;
702 mDontResend = null;
703 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700704 }
705 }).show();
706 }
707
708 /**
709 * Insert the url into the visited history database.
710 * @param url The url to be inserted.
711 * @param isReload True if this url is being reloaded.
712 * FIXME: Not sure what to do when reloading the page.
713 */
714 @Override
715 public void doUpdateVisitedHistory(WebView view, String url,
716 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800717 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700718 }
719
720 /**
721 * Displays SSL error(s) dialog to the user.
722 */
723 @Override
724 public void onReceivedSslError(final WebView view,
725 final SslErrorHandler handler, final SslError error) {
726 if (!mInForeground) {
727 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800728 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700729 return;
730 }
John Reck35e9dd62011-04-25 09:01:54 -0700731 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700732 final LayoutInflater factory =
Michael Kolb14612442011-06-24 13:06:29 -0700733 LayoutInflater.from(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700734 final View warningsView =
735 factory.inflate(R.layout.ssl_warnings, null);
736 final LinearLayout placeholder =
737 (LinearLayout)warningsView.findViewById(R.id.placeholder);
738
739 if (error.hasError(SslError.SSL_UNTRUSTED)) {
740 LinearLayout ll = (LinearLayout)factory
741 .inflate(R.layout.ssl_warning, null);
742 ((TextView)ll.findViewById(R.id.warning))
743 .setText(R.string.ssl_untrusted);
744 placeholder.addView(ll);
745 }
746
747 if (error.hasError(SslError.SSL_IDMISMATCH)) {
748 LinearLayout ll = (LinearLayout)factory
749 .inflate(R.layout.ssl_warning, null);
750 ((TextView)ll.findViewById(R.id.warning))
751 .setText(R.string.ssl_mismatch);
752 placeholder.addView(ll);
753 }
754
755 if (error.hasError(SslError.SSL_EXPIRED)) {
756 LinearLayout ll = (LinearLayout)factory
757 .inflate(R.layout.ssl_warning, null);
758 ((TextView)ll.findViewById(R.id.warning))
759 .setText(R.string.ssl_expired);
760 placeholder.addView(ll);
761 }
762
763 if (error.hasError(SslError.SSL_NOTYETVALID)) {
764 LinearLayout ll = (LinearLayout)factory
765 .inflate(R.layout.ssl_warning, null);
766 ((TextView)ll.findViewById(R.id.warning))
767 .setText(R.string.ssl_not_yet_valid);
768 placeholder.addView(ll);
769 }
770
Michael Kolb14612442011-06-24 13:06:29 -0700771 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700772 R.string.security_warning).setIcon(
773 android.R.drawable.ic_dialog_alert).setView(
774 warningsView).setPositiveButton(R.string.ssl_continue,
775 new DialogInterface.OnClickListener() {
776 public void onClick(DialogInterface dialog,
777 int whichButton) {
778 handler.proceed();
779 }
780 }).setNeutralButton(R.string.view_certificate,
781 new DialogInterface.OnClickListener() {
782 public void onClick(DialogInterface dialog,
783 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700784 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700785 handler, error);
786 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000787 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700788 new DialogInterface.OnClickListener() {
789 public void onClick(DialogInterface dialog,
790 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800791 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700792 }
793 }).setOnCancelListener(
794 new DialogInterface.OnCancelListener() {
795 public void onCancel(DialogInterface dialog) {
796 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800797 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
798 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700799 }
800 }).show();
801 } else {
802 handler.proceed();
803 }
804 }
805
806 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700807 * Displays client certificate request to the user.
808 */
809 @Override
810 public void onReceivedClientCertRequest(final WebView view,
811 final ClientCertRequestHandler handler, final String host_and_port) {
812 if (!mInForeground) {
813 handler.ignore();
814 return;
815 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700816 int colon = host_and_port.lastIndexOf(':');
817 String host;
818 int port;
819 if (colon == -1) {
820 host = host_and_port;
821 port = -1;
822 } else {
823 String portString = host_and_port.substring(colon + 1);
824 try {
825 port = Integer.parseInt(portString);
826 host = host_and_port.substring(0, colon);
827 } catch (NumberFormatException e) {
828 host = host_and_port;
829 port = -1;
830 }
831 }
Michael Kolb14612442011-06-24 13:06:29 -0700832 KeyChain.choosePrivateKeyAlias(
833 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700834 @Override public void alias(String alias) {
835 if (alias == null) {
836 handler.cancel();
837 return;
838 }
Michael Kolb14612442011-06-24 13:06:29 -0700839 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700840 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700841 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700842 }
843
844 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700845 * Handles an HTTP authentication request.
846 *
847 * @param handler The authentication handler
848 * @param host The host
849 * @param realm The realm
850 */
851 @Override
852 public void onReceivedHttpAuthRequest(WebView view,
853 final HttpAuthHandler handler, final String host,
854 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700855 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700856 }
857
858 @Override
John Reck438bf462011-01-12 18:11:46 -0800859 public WebResourceResponse shouldInterceptRequest(WebView view,
860 String url) {
861 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700862 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800863 return res;
864 }
865
866 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700867 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
868 if (!mInForeground) {
869 return false;
870 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700871 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700872 }
873
874 @Override
875 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700876 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700877 return;
878 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700879 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700880 }
Patrick Scott92066772011-03-10 08:46:27 -0500881
882 @Override
883 public void onReceivedLoginRequest(WebView view, String realm,
884 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700885 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500886 .handleLogin(realm, account, args);
887 }
888
Grace Kloba22ac16e2009-10-07 18:00:23 -0700889 };
890
John Reck1cf4b792011-07-26 10:22:22 -0700891 private void syncCurrentState(WebView view, String url) {
892 // Sync state (in case of stop/timeout)
893 mCurrentState.mUrl = view.getUrl();
894 if (mCurrentState.mUrl == null) {
895 mCurrentState.mUrl = url != null ? url : "";
896 }
897 mCurrentState.mOriginalUrl = view.getOriginalUrl();
898 mCurrentState.mTitle = view.getTitle();
899 mCurrentState.mFavicon = view.getFavicon();
900 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
901 // In case we stop when loading an HTTPS page from an HTTP page
902 // but before a provisional load occurred
903 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
904 }
905 }
906
Patrick Scott92066772011-03-10 08:46:27 -0500907 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
908 // displayed.
909 void setDeviceAccountLogin(DeviceAccountLogin login) {
910 mDeviceAccountLogin = login;
911 }
912
913 // Returns non-null if the title bar should display the auto-login UI.
914 DeviceAccountLogin getDeviceAccountLogin() {
915 return mDeviceAccountLogin;
916 }
917
Grace Kloba22ac16e2009-10-07 18:00:23 -0700918 // -------------------------------------------------------------------------
919 // WebChromeClient implementation for the main WebView
920 // -------------------------------------------------------------------------
921
922 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
923 // Helper method to create a new tab or sub window.
924 private void createWindow(final boolean dialog, final Message msg) {
925 WebView.WebViewTransport transport =
926 (WebView.WebViewTransport) msg.obj;
927 if (dialog) {
928 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700929 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700930 transport.setWebView(mSubView);
931 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700932 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700933 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700934 transport.setWebView(newTab.getWebView());
935 }
936 msg.sendToTarget();
937 }
938
939 @Override
940 public boolean onCreateWindow(WebView view, final boolean dialog,
941 final boolean userGesture, final Message resultMsg) {
942 // only allow new window or sub window for the foreground case
943 if (!mInForeground) {
944 return false;
945 }
946 // Short-circuit if we can't create any more tabs or sub windows.
947 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700948 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700949 .setTitle(R.string.too_many_subwindows_dialog_title)
950 .setIcon(android.R.drawable.ic_dialog_alert)
951 .setMessage(R.string.too_many_subwindows_dialog_message)
952 .setPositiveButton(R.string.ok, null)
953 .show();
954 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700955 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700956 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700957 .setTitle(R.string.too_many_windows_dialog_title)
958 .setIcon(android.R.drawable.ic_dialog_alert)
959 .setMessage(R.string.too_many_windows_dialog_message)
960 .setPositiveButton(R.string.ok, null)
961 .show();
962 return false;
963 }
964
965 // Short-circuit if this was a user gesture.
966 if (userGesture) {
967 createWindow(dialog, resultMsg);
968 return true;
969 }
970
971 // Allow the popup and create the appropriate window.
972 final AlertDialog.OnClickListener allowListener =
973 new AlertDialog.OnClickListener() {
974 public void onClick(DialogInterface d,
975 int which) {
976 createWindow(dialog, resultMsg);
977 }
978 };
979
980 // Block the popup by returning a null WebView.
981 final AlertDialog.OnClickListener blockListener =
982 new AlertDialog.OnClickListener() {
983 public void onClick(DialogInterface d, int which) {
984 resultMsg.sendToTarget();
985 }
986 };
987
988 // Build a confirmation dialog to display to the user.
989 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700990 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700991 .setTitle(R.string.attention)
992 .setIcon(android.R.drawable.ic_dialog_alert)
993 .setMessage(R.string.popup_window_attempt)
994 .setPositiveButton(R.string.allow, allowListener)
995 .setNegativeButton(R.string.block, blockListener)
996 .setCancelable(false)
997 .create();
998
999 // Show the confirmation dialog.
1000 d.show();
1001 return true;
1002 }
1003
1004 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -05001005 public void onRequestFocus(WebView view) {
1006 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001007 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -05001008 }
1009 }
1010
1011 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001012 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -07001013 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001014 // JavaScript can only close popup window.
1015 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001016 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001017 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001018 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001019 }
1020 }
1021
1022 @Override
1023 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001024 mPageLoadProgress = newProgress;
1025 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001026 }
1027
1028 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001029 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001030 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001031 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001032 }
1033
1034 @Override
1035 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001036 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001037 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001038 }
1039
1040 @Override
1041 public void onReceivedTouchIconUrl(WebView view, String url,
1042 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001043 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001044 // Let precomposed icons take precedence over non-composed
1045 // icons.
1046 if (precomposed && mTouchIconLoader != null) {
1047 mTouchIconLoader.cancel(false);
1048 mTouchIconLoader = null;
1049 }
1050 // Have only one async task at a time.
1051 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001052 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001053 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001054 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001055 }
1056 }
1057
1058 @Override
1059 public void onShowCustomView(View view,
1060 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001061 Activity activity = mWebViewController.getActivity();
1062 if (activity != null) {
1063 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1064 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001065 }
1066
1067 @Override
1068 public void onShowCustomView(View view, int requestedOrientation,
1069 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001070 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001071 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001072 }
1073
1074 @Override
1075 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001076 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001077 }
1078
1079 /**
1080 * The origin has exceeded its database quota.
1081 * @param url the URL that exceeded the quota
1082 * @param databaseIdentifier the identifier of the database on which the
1083 * transaction that caused the quota overflow was run
1084 * @param currentQuota the current quota for the origin.
1085 * @param estimatedSize the estimated size of the database.
1086 * @param totalUsedQuota is the sum of all origins' quota.
1087 * @param quotaUpdater The callback to run when a decision to allow or
1088 * deny quota has been made. Don't forget to call this!
1089 */
1090 @Override
1091 public void onExceededDatabaseQuota(String url,
1092 String databaseIdentifier, long currentQuota, long estimatedSize,
1093 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001094 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001095 .onExceededDatabaseQuota(url, databaseIdentifier,
1096 currentQuota, estimatedSize, totalUsedQuota,
1097 quotaUpdater);
1098 }
1099
1100 /**
1101 * The Application Cache has exceeded its max size.
1102 * @param spaceNeeded is the amount of disk space that would be needed
1103 * in order for the last appcache operation to succeed.
1104 * @param totalUsedQuota is the sum of all origins' quota.
1105 * @param quotaUpdater A callback to inform the WebCore thread that a
1106 * new app cache size is available. This callback must always
1107 * be executed at some point to ensure that the sleeping
1108 * WebCore thread is woken up.
1109 */
1110 @Override
1111 public void onReachedMaxAppCacheSize(long spaceNeeded,
1112 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001113 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001114 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1115 quotaUpdater);
1116 }
1117
1118 /**
1119 * Instructs the browser to show a prompt to ask the user to set the
1120 * Geolocation permission state for the specified origin.
1121 * @param origin The origin for which Geolocation permissions are
1122 * requested.
1123 * @param callback The callback to call once the user has set the
1124 * Geolocation permission state.
1125 */
1126 @Override
1127 public void onGeolocationPermissionsShowPrompt(String origin,
1128 GeolocationPermissions.Callback callback) {
1129 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001130 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001131 }
1132 }
1133
1134 /**
1135 * Instructs the browser to hide the Geolocation permissions prompt.
1136 */
1137 @Override
1138 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001139 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001140 mGeolocationPermissionsPrompt.hide();
1141 }
1142 }
1143
Ben Murdoch65acc352009-11-19 18:16:04 +00001144 /* Adds a JavaScript error message to the system log and if the JS
1145 * console is enabled in the about:debug options, to that console
1146 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001147 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001148 */
1149 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001150 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001151 if (mInForeground) {
1152 // call getErrorConsole(true) so it will create one if needed
1153 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001154 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001155 if (mWebViewController.shouldShowErrorConsole()
1156 && errorConsole.getShowState() !=
1157 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001158 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1159 }
1160 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001161
Jeff Hamilton47654f42010-09-07 09:57:51 -05001162 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001163 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001164
Ben Murdochc42addf2010-01-28 15:19:59 +00001165 String message = "Console: " + consoleMessage.message() + " "
1166 + consoleMessage.sourceId() + ":"
1167 + consoleMessage.lineNumber();
1168
1169 switch (consoleMessage.messageLevel()) {
1170 case TIP:
1171 Log.v(CONSOLE_LOGTAG, message);
1172 break;
1173 case LOG:
1174 Log.i(CONSOLE_LOGTAG, message);
1175 break;
1176 case WARNING:
1177 Log.w(CONSOLE_LOGTAG, message);
1178 break;
1179 case ERROR:
1180 Log.e(CONSOLE_LOGTAG, message);
1181 break;
1182 case DEBUG:
1183 Log.d(CONSOLE_LOGTAG, message);
1184 break;
1185 }
1186
1187 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001188 }
1189
1190 /**
1191 * Ask the browser for an icon to represent a <video> element.
1192 * This icon will be used if the Web page did not specify a poster attribute.
1193 * @return Bitmap The icon or null if no such icon is available.
1194 */
1195 @Override
1196 public Bitmap getDefaultVideoPoster() {
1197 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001198 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001199 }
1200 return null;
1201 }
1202
1203 /**
1204 * Ask the host application for a custom progress view to show while
1205 * a <video> is loading.
1206 * @return View The progress view.
1207 */
1208 @Override
1209 public View getVideoLoadingProgressView() {
1210 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001211 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001212 }
1213 return null;
1214 }
1215
1216 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001217 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001218 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001219 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001220 } else {
1221 uploadMsg.onReceiveValue(null);
1222 }
1223 }
1224
1225 /**
1226 * Deliver a list of already-visited URLs
1227 */
1228 @Override
1229 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001230 mWebViewController.getVisitedHistory(callback);
1231 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001232
1233 @Override
1234 public void setupAutoFill(Message message) {
1235 // Prompt the user to set up their profile.
1236 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001237 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1238 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001239 Context.LAYOUT_INFLATER_SERVICE);
1240 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1241
1242 builder.setView(layout)
Ben Murdochb7e6f942011-07-08 13:00:21 +01001243 .setTitle(R.string.autofill_setup_dialog_title)
Ben Murdoch1d676b62011-01-17 12:54:24 +00001244 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1245 @Override
1246 public void onClick(DialogInterface dialog, int id) {
1247 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1248 R.id.setup_autofill_dialog_disable_autofill);
1249
1250 if (disableAutoFill.isChecked()) {
1251 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001252 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001253 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001254 R.string.autofill_setup_dialog_negative_toast,
1255 Toast.LENGTH_LONG).show();
1256 } else {
1257 // Take user to the AutoFill profile editor. When they return,
1258 // we will send the message that we pass here which will trigger
1259 // the form to get filled out with their new profile.
1260 mWebViewController.setupAutoFill(msg);
1261 }
1262 }
1263 })
1264 .setNegativeButton(R.string.cancel, null)
1265 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001266 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001267 };
1268
1269 // -------------------------------------------------------------------------
1270 // WebViewClient implementation for the sub window
1271 // -------------------------------------------------------------------------
1272
1273 // Subclass of WebViewClient used in subwindows to notify the main
1274 // WebViewClient of certain WebView activities.
1275 private static class SubWindowClient extends WebViewClient {
1276 // The main WebViewClient.
1277 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001278 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001279
Michael Kolb8233fac2010-10-26 16:08:53 -07001280 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001281 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001282 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001283 }
1284 @Override
1285 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1286 // Unlike the others, do not call mClient's version, which would
1287 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001288 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001289 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001290 }
1291 @Override
1292 public void doUpdateVisitedHistory(WebView view, String url,
1293 boolean isReload) {
1294 mClient.doUpdateVisitedHistory(view, url, isReload);
1295 }
1296 @Override
1297 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1298 return mClient.shouldOverrideUrlLoading(view, url);
1299 }
1300 @Override
1301 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1302 SslError error) {
1303 mClient.onReceivedSslError(view, handler, error);
1304 }
1305 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001306 public void onReceivedClientCertRequest(WebView view,
1307 ClientCertRequestHandler handler, String host_and_port) {
1308 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1309 }
1310 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001311 public void onReceivedHttpAuthRequest(WebView view,
1312 HttpAuthHandler handler, String host, String realm) {
1313 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1314 }
1315 @Override
1316 public void onFormResubmission(WebView view, Message dontResend,
1317 Message resend) {
1318 mClient.onFormResubmission(view, dontResend, resend);
1319 }
1320 @Override
1321 public void onReceivedError(WebView view, int errorCode,
1322 String description, String failingUrl) {
1323 mClient.onReceivedError(view, errorCode, description, failingUrl);
1324 }
1325 @Override
1326 public boolean shouldOverrideKeyEvent(WebView view,
1327 android.view.KeyEvent event) {
1328 return mClient.shouldOverrideKeyEvent(view, event);
1329 }
1330 @Override
1331 public void onUnhandledKeyEvent(WebView view,
1332 android.view.KeyEvent event) {
1333 mClient.onUnhandledKeyEvent(view, event);
1334 }
1335 }
1336
1337 // -------------------------------------------------------------------------
1338 // WebChromeClient implementation for the sub window
1339 // -------------------------------------------------------------------------
1340
1341 private class SubWindowChromeClient extends WebChromeClient {
1342 // The main WebChromeClient.
1343 private final WebChromeClient mClient;
1344
1345 SubWindowChromeClient(WebChromeClient client) {
1346 mClient = client;
1347 }
1348 @Override
1349 public void onProgressChanged(WebView view, int newProgress) {
1350 mClient.onProgressChanged(view, newProgress);
1351 }
1352 @Override
1353 public boolean onCreateWindow(WebView view, boolean dialog,
1354 boolean userGesture, android.os.Message resultMsg) {
1355 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1356 }
1357 @Override
1358 public void onCloseWindow(WebView window) {
1359 if (window != mSubView) {
1360 Log.e(LOGTAG, "Can't close the window");
1361 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001362 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001363 }
1364 }
1365
1366 // -------------------------------------------------------------------------
1367
1368 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001369 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001370 this(wvcontroller, w, null);
1371 }
1372
1373 Tab(WebViewController wvcontroller, Bundle state) {
1374 this(wvcontroller, null, state);
1375 }
1376
1377 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001378 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001379 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001380 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001381 mDataController = DataController.getInstance(mContext);
1382 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001383 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001384 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001385 mInForeground = false;
1386
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001387 mDownloadListener = new DownloadListener() {
1388 public void onDownloadStart(String url, String userAgent,
1389 String contentDisposition, String mimetype,
1390 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001391 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001392 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001393 }
1394 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001395 mWebBackForwardListClient = new WebBackForwardListClient() {
1396 @Override
1397 public void onNewHistoryItem(WebHistoryItem item) {
1398 if (isInVoiceSearchMode()) {
1399 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1400 }
1401 }
1402 @Override
1403 public void onIndexChanged(WebHistoryItem item, int index) {
1404 Object data = item.getCustomData();
1405 if (data != null && data instanceof Intent) {
1406 activateVoiceSearchMode((Intent) data);
1407 }
1408 }
1409 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001410
John Reck1cf4b792011-07-26 10:22:22 -07001411 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1412 R.dimen.tab_thumbnail_width);
1413 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1414 R.dimen.tab_thumbnail_height);
1415 updateShouldCaptureThumbnails();
1416 restoreState(state);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001417 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001418 mHandler = new Handler() {
1419 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001420 switch (m.what) {
1421 case MSG_CAPTURE:
1422 capture();
1423 break;
1424 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001425 }
1426 };
John Reck1cf4b792011-07-26 10:22:22 -07001427 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001428
John Reck1cf4b792011-07-26 10:22:22 -07001429 public void updateShouldCaptureThumbnails() {
1430 if (mWebViewController.shouldCaptureThumbnails()) {
1431 synchronized (Tab.this) {
1432 if (mCapture == null) {
1433 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1434 Bitmap.Config.RGB_565);
1435 if (mInForeground) {
1436 postCapture();
1437 }
1438 }
1439 }
1440 } else {
1441 synchronized (Tab.this) {
1442 mCapture = null;
1443 deleteThumbnail();
1444 }
1445 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001446 }
1447
Michael Kolb14612442011-06-24 13:06:29 -07001448 public void setController(WebViewController ctl) {
1449 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001450 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001451 }
1452
Michael Kolbc831b632011-05-11 09:30:34 -07001453 public void setId(long id) {
1454 mId = id;
1455 }
1456
1457 public long getId() {
1458 return mId;
1459 }
1460
Grace Kloba22ac16e2009-10-07 18:00:23 -07001461 /**
1462 * Sets the WebView for this tab, correctly removing the old WebView from
1463 * the container view.
1464 */
1465 void setWebView(WebView w) {
1466 if (mMainView == w) {
1467 return;
1468 }
Michael Kolba713ec82010-11-29 17:27:06 -08001469
Grace Kloba22ac16e2009-10-07 18:00:23 -07001470 // If the WebView is changing, the page will be reloaded, so any ongoing
1471 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001472 if (mGeolocationPermissionsPrompt != null) {
1473 mGeolocationPermissionsPrompt.hide();
1474 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001475
Michael Kolba713ec82010-11-29 17:27:06 -08001476 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001477
John Reck1cf4b792011-07-26 10:22:22 -07001478 if (mMainView != null) {
1479 if (w != null) {
1480 syncCurrentState(w, null);
1481 } else {
1482 mCurrentState = new PageState(mContext, false);
1483 }
1484 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001485 // set the new one
1486 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001487 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001488 if (mMainView != null) {
1489 mMainView.setWebViewClient(mWebViewClient);
1490 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001491 // Attach DownloadManager so that downloads can start in an active
1492 // or a non-active window. This can happen when going to a site that
1493 // does a redirect after a period of time. The user could have
1494 // switched to another tab while waiting for the download to start.
1495 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001496 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001497 mMainView.setPictureListener(this);
John Reck1cf4b792011-07-26 10:22:22 -07001498 if (mSavedState != null) {
1499 mMainView.restoreState(mSavedState);
1500 mSavedState = null;
1501 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001502 }
1503 }
1504
1505 /**
1506 * Destroy the tab's main WebView and subWindow if any
1507 */
1508 void destroy() {
1509 if (mMainView != null) {
1510 dismissSubWindow();
John Reckef654f12011-07-12 16:42:08 -07001511 // Make sure the embedded title bar isn't still attached
1512 mMainView.setEmbeddedTitleBar(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001513 // save the WebView to call destroy() after detach it from the tab
1514 WebView webView = mMainView;
1515 setWebView(null);
1516 webView.destroy();
1517 }
1518 }
1519
1520 /**
1521 * Remove the tab from the parent
1522 */
1523 void removeFromTree() {
1524 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001525 if (mChildren != null) {
1526 for(Tab t : mChildren) {
1527 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001528 }
1529 }
1530 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001531 if (mParent != null) {
1532 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001533 }
John Reck1cf4b792011-07-26 10:22:22 -07001534 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001535 }
1536
1537 /**
1538 * Create a new subwindow unless a subwindow already exists.
1539 * @return True if a new subwindow was created. False if one already exists.
1540 */
1541 boolean createSubWindow() {
1542 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001543 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001544 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001545 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001546 mSubView.setWebChromeClient(new SubWindowChromeClient(
1547 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001548 // Set a different DownloadListener for the mSubView, since it will
1549 // just need to dismiss the mSubView, rather than close the Tab
1550 mSubView.setDownloadListener(new DownloadListener() {
1551 public void onDownloadStart(String url, String userAgent,
1552 String contentDisposition, String mimetype,
1553 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001554 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001555 contentDisposition, mimetype, contentLength);
1556 if (mSubView.copyBackForwardList().getSize() == 0) {
1557 // This subwindow was opened for the sole purpose of
1558 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001559 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001560 }
1561 }
1562 });
Michael Kolb14612442011-06-24 13:06:29 -07001563 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001564 return true;
1565 }
1566 return false;
1567 }
1568
1569 /**
1570 * Dismiss the subWindow for the tab.
1571 */
1572 void dismissSubWindow() {
1573 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001574 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001575 mSubView.destroy();
1576 mSubView = null;
1577 mSubViewContainer = null;
1578 }
1579 }
1580
Grace Kloba22ac16e2009-10-07 18:00:23 -07001581
1582 /**
1583 * Set the parent tab of this tab.
1584 */
Michael Kolbc831b632011-05-11 09:30:34 -07001585 void setParent(Tab parent) {
1586 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001587 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001588 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001590 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001591 if (mSavedState != null) {
1592 if (parent == null) {
1593 mSavedState.remove(PARENTTAB);
1594 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001595 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001596 }
1597 }
John Reckb0a86db2011-05-24 14:05:58 -07001598
1599 // Sync the WebView useragent with the parent
1600 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1601 != mSettings.hasDesktopUseragent(getWebView())) {
1602 mSettings.toggleDesktopUseragent(getWebView());
1603 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001604 }
1605
1606 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001607 * If this Tab was created through another Tab, then this method returns
1608 * that Tab.
1609 * @return the Tab parent or null
1610 */
1611 public Tab getParent() {
1612 return mParent;
1613 }
1614
1615 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001616 * When a Tab is created through the content of another Tab, then we
1617 * associate the Tabs.
1618 * @param child the Tab that was created from this Tab
1619 */
1620 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001621 if (mChildren == null) {
1622 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001623 }
Michael Kolbc831b632011-05-11 09:30:34 -07001624 mChildren.add(child);
1625 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001626 }
1627
Michael Kolbc831b632011-05-11 09:30:34 -07001628 Vector<Tab> getChildren() {
1629 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001630 }
1631
1632 void resume() {
1633 if (mMainView != null) {
1634 mMainView.onResume();
1635 if (mSubView != null) {
1636 mSubView.onResume();
1637 }
1638 }
1639 }
1640
1641 void pause() {
1642 if (mMainView != null) {
1643 mMainView.onPause();
1644 if (mSubView != null) {
1645 mSubView.onPause();
1646 }
1647 }
1648 }
1649
1650 void putInForeground() {
1651 mInForeground = true;
1652 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001653 Activity activity = mWebViewController.getActivity();
1654 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001655 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001656 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001657 }
1658 // Show the pending error dialog if the queue is not empty
1659 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1660 showError(mQueuedErrors.getFirst());
1661 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001662 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001663 }
1664
1665 void putInBackground() {
1666 mInForeground = false;
1667 pause();
1668 mMainView.setOnCreateContextMenuListener(null);
1669 if (mSubView != null) {
1670 mSubView.setOnCreateContextMenuListener(null);
1671 }
1672 }
1673
Michael Kolb8233fac2010-10-26 16:08:53 -07001674 boolean inForeground() {
1675 return mInForeground;
1676 }
1677
Grace Kloba22ac16e2009-10-07 18:00:23 -07001678 /**
1679 * Return the top window of this tab; either the subwindow if it is not
1680 * null or the main window.
1681 * @return The top window of this tab.
1682 */
1683 WebView getTopWindow() {
1684 if (mSubView != null) {
1685 return mSubView;
1686 }
1687 return mMainView;
1688 }
1689
1690 /**
1691 * Return the main window of this tab. Note: if a tab is freed in the
1692 * background, this can return null. It is only guaranteed to be
1693 * non-null for the current tab.
1694 * @return The main WebView of this tab.
1695 */
1696 WebView getWebView() {
1697 return mMainView;
1698 }
1699
Michael Kolba713ec82010-11-29 17:27:06 -08001700 void setViewContainer(View container) {
1701 mContainer = container;
1702 }
1703
Michael Kolb8233fac2010-10-26 16:08:53 -07001704 View getViewContainer() {
1705 return mContainer;
1706 }
1707
Grace Kloba22ac16e2009-10-07 18:00:23 -07001708 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001709 * Return whether private browsing is enabled for the main window of
1710 * this tab.
1711 * @return True if private browsing is enabled.
1712 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001713 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001714 WebView webView = getWebView();
1715 if (webView == null) {
1716 return false;
1717 }
1718 return webView.isPrivateBrowsingEnabled();
1719 }
1720
1721 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001722 * Return the subwindow of this tab or null if there is no subwindow.
1723 * @return The subwindow of this tab or null.
1724 */
1725 WebView getSubWebView() {
1726 return mSubView;
1727 }
1728
Michael Kolb1514bb72010-11-22 09:11:48 -08001729 void setSubWebView(WebView subView) {
1730 mSubView = subView;
1731 }
1732
Michael Kolb8233fac2010-10-26 16:08:53 -07001733 View getSubViewContainer() {
1734 return mSubViewContainer;
1735 }
1736
Michael Kolb1514bb72010-11-22 09:11:48 -08001737 void setSubViewContainer(View subViewContainer) {
1738 mSubViewContainer = subViewContainer;
1739 }
1740
Grace Kloba22ac16e2009-10-07 18:00:23 -07001741 /**
1742 * @return The geolocation permissions prompt for this tab.
1743 */
1744 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001745 if (mGeolocationPermissionsPrompt == null) {
1746 ViewStub stub = (ViewStub) mContainer
1747 .findViewById(R.id.geolocation_permissions_prompt);
1748 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1749 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001750 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 return mGeolocationPermissionsPrompt;
1752 }
1753
1754 /**
1755 * @return The application id string
1756 */
1757 String getAppId() {
1758 return mAppId;
1759 }
1760
1761 /**
1762 * Set the application id string
1763 * @param id
1764 */
1765 void setAppId(String id) {
1766 mAppId = id;
1767 }
1768
Grace Kloba22ac16e2009-10-07 18:00:23 -07001769 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001770 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001771 }
1772
John Reck49a603c2011-03-03 09:33:05 -08001773 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001774 if (mCurrentState.mOriginalUrl == null) {
1775 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001776 }
John Reckdb22ec42011-06-29 11:31:24 -07001777 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001778 }
1779
Grace Kloba22ac16e2009-10-07 18:00:23 -07001780 /**
John Reck30c714c2010-12-16 17:30:34 -08001781 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001782 */
1783 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001784 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001785 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001786 }
John Reck30c714c2010-12-16 17:30:34 -08001787 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001788 }
1789
1790 /**
John Reck30c714c2010-12-16 17:30:34 -08001791 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001792 */
1793 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001794 if (mCurrentState.mFavicon != null) {
1795 return mCurrentState.mFavicon;
1796 }
1797 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001798 }
1799
John Recke969cc52010-12-21 17:24:43 -08001800 public boolean isBookmarkedSite() {
1801 return mCurrentState.mIsBookmarkedSite;
1802 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001803
Grace Kloba22ac16e2009-10-07 18:00:23 -07001804 /**
1805 * Return the tab's error console. Creates the console if createIfNEcessary
1806 * is true and we haven't already created the console.
1807 * @param createIfNecessary Flag to indicate if the console should be
1808 * created if it has not been already.
1809 * @return The tab's error console, or null if one has not been created and
1810 * createIfNecessary is false.
1811 */
1812 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1813 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001814 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001815 mErrorConsole.setWebView(mMainView);
1816 }
1817 return mErrorConsole;
1818 }
1819
John Reck30c714c2010-12-16 17:30:34 -08001820 private void setLockIconType(LockIcon icon) {
1821 mCurrentState.mLockIcon = icon;
1822 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001823 }
1824
1825 /**
1826 * @return The tab's lock icon type.
1827 */
John Reck30c714c2010-12-16 17:30:34 -08001828 LockIcon getLockIconType() {
1829 return mCurrentState.mLockIcon;
1830 }
1831
1832 int getLoadProgress() {
1833 if (mInPageLoad) {
1834 return mPageLoadProgress;
1835 }
1836 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001837 }
1838
1839 /**
1840 * @return TRUE if onPageStarted is called while onPageFinished is not
1841 * called yet.
1842 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001843 boolean inPageLoad() {
1844 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001845 }
1846
1847 // force mInLoad to be false. This should only be called before closing the
1848 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001849 void clearInPageLoad() {
1850 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001851 }
1852
Grace Kloba22ac16e2009-10-07 18:00:23 -07001853 /**
John Reck1cf4b792011-07-26 10:22:22 -07001854 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001855 */
John Reck1cf4b792011-07-26 10:22:22 -07001856 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001857 // If the WebView is null it means we ran low on memory and we already
1858 // stored the saved state in mSavedState.
1859 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001860 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001861 }
John Reck24f18262011-06-17 14:47:20 -07001862 // If the tab is the homepage or has no URL, don't save it
1863 String homepage = BrowserSettings.getInstance().getHomePage();
1864 if (TextUtils.equals(homepage, mCurrentState.mUrl)
1865 || TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001866 return null;
John Reck24f18262011-06-17 14:47:20 -07001867 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001868
1869 mSavedState = new Bundle();
John Reck541f55a2011-06-07 16:34:43 -07001870 mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001871
Michael Kolbc831b632011-05-11 09:30:34 -07001872 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001873 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1874 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001875 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001876 if (mAppId != null) {
1877 mSavedState.putString(APPID, mAppId);
1878 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001879 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001880 if (mParent != null) {
1881 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001882 }
John Reckb0a86db2011-05-24 14:05:58 -07001883 mSavedState.putBoolean(USERAGENT,
1884 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001885 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001886 }
1887
1888 /*
1889 * Restore the state of the tab.
1890 */
John Reck1cf4b792011-07-26 10:22:22 -07001891 private void restoreState(Bundle b) {
1892 mSavedState = b;
1893 if (mSavedState == null) {
1894 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001895 }
1896 // Restore the internal state even if the WebView fails to restore.
1897 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001898 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001899 mAppId = b.getString(APPID);
John Reckb0a86db2011-05-24 14:05:58 -07001900 if (b.getBoolean(USERAGENT)
1901 != mSettings.hasDesktopUseragent(getWebView())) {
1902 mSettings.toggleDesktopUseragent(getWebView());
1903 }
John Reck1cf4b792011-07-26 10:22:22 -07001904 String url = b.getString(CURRURL);
1905 String title = b.getString(CURRTITLE);
1906 boolean incognito = b.getBoolean(INCOGNITO);
1907 mCurrentState = new PageState(mContext, incognito, url, null);
1908 mCurrentState.mTitle = title;
1909 synchronized (Tab.this) {
1910 if (mCapture != null) {
1911 BackgroundHandler.execute(mLoadThumbnail);
1912 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001913 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001914 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001915
Leon Scroggins1961ed22010-12-07 15:22:21 -05001916 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001917 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001918 }
1919
John Recke969cc52010-12-21 17:24:43 -08001920 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1921 = new DataController.OnQueryUrlIsBookmark() {
1922 @Override
1923 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1924 if (mCurrentState.mUrl.equals(url)) {
1925 mCurrentState.mIsBookmarkedSite = isBookmark;
1926 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1927 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001928 }
John Recke969cc52010-12-21 17:24:43 -08001929 };
Michael Kolb1acef692011-03-08 14:12:06 -08001930
Michael Kolbeb95db42011-03-03 10:38:40 -08001931 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001932 synchronized (Tab.this) {
1933 return mCapture;
1934 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001935 }
1936
John Reck541f55a2011-06-07 16:34:43 -07001937 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001938 return false;
1939 }
1940
John Reckd8c74522011-06-14 08:45:00 -07001941 public ContentValues createSnapshotValues() {
1942 if (mMainView == null) return null;
John Reck8cc92352011-07-06 17:41:52 -07001943 ByteArrayOutputStream bos = new ByteArrayOutputStream();
1944 try {
1945 GZIPOutputStream stream = new GZIPOutputStream(bos);
1946 if (!mMainView.saveViewState(stream)) {
1947 return null;
1948 }
1949 stream.flush();
1950 stream.close();
1951 } catch (Exception e) {
1952 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07001953 return null;
1954 }
John Reck8cc92352011-07-06 17:41:52 -07001955 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07001956 ContentValues values = new ContentValues();
1957 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1958 values.put(Snapshots.URL, mCurrentState.mUrl);
1959 values.put(Snapshots.VIEWSTATE, data);
1960 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001961 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1962 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
1963 Bitmap screenshot = Controller.createScreenshot(mMainView,
1964 Controller.getDesiredThumbnailWidth(mContext),
1965 Controller.getDesiredThumbnailHeight(mContext));
1966 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001967 return values;
John Reck541f55a2011-06-07 16:34:43 -07001968 }
1969
John Reck8cc92352011-07-06 17:41:52 -07001970 public byte[] compressBitmap(Bitmap bitmap) {
1971 if (bitmap == null) {
1972 return null;
1973 }
1974 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1975 bitmap.compress(CompressFormat.PNG, 100, stream);
1976 return stream.toByteArray();
1977 }
1978
John Reck26b18322011-06-21 13:08:58 -07001979 public void loadUrl(String url, Map<String, String> headers) {
1980 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001981 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001982 mWebViewController.onPageStarted(this, mMainView, null);
1983 mMainView.loadUrl(url, headers);
1984 }
1985 }
1986
Michael Kolb9ef259a2011-07-12 15:33:08 -07001987 protected void capture() {
1988 if (mMainView == null || mCapture == null) return;
1989 Canvas c = new Canvas(mCapture);
1990 final int left = mMainView.getScrollX();
1991 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
1992 c.translate(-left, -top);
1993 float scale = mCaptureWidth / (float) mMainView.getWidth();
1994 c.scale(scale, scale, left, top);
1995 mMainView.draw(c);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07001996 c.setBitmap(null);
John Reck1cf4b792011-07-26 10:22:22 -07001997 persistThumbnail();
Michael Kolb9ef259a2011-07-12 15:33:08 -07001998 }
1999
2000 @Override
2001 public void onNewPicture(WebView view, Picture picture) {
2002 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002003 postCapture();
2004 }
2005
2006 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002007 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2008 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2009 }
2010 }
2011
John Reckef654f12011-07-12 16:42:08 -07002012 public boolean canGoBack() {
2013 return mMainView != null ? mMainView.canGoBack() : false;
2014 }
2015
2016 public boolean canGoForward() {
2017 return mMainView != null ? mMainView.canGoForward() : false;
2018 }
2019
2020 public void goBack() {
2021 if (mMainView != null) {
2022 mMainView.goBack();
2023 }
2024 }
2025
2026 public void goForward() {
2027 if (mMainView != null) {
2028 mMainView.goForward();
2029 }
2030 }
2031
John Reck1cf4b792011-07-26 10:22:22 -07002032 protected void persistThumbnail() {
2033 BackgroundHandler.execute(mSaveThumbnail);
2034 }
2035
2036 protected void deleteThumbnail() {
2037 BackgroundHandler.execute(mDeleteThumbnail);
2038 }
2039
2040 private void updateCaptureFromBlob(byte[] blob) {
2041 synchronized (Tab.this) {
2042 if (mCapture == null) {
2043 return;
2044 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002045 ByteBuffer buffer = ByteBuffer.wrap(blob);
2046 if (buffer.capacity() != blob.length) {
2047 Log.e(LOGTAG, "Load capture has mismatched sizes: "
2048 + buffer.capacity() + " vs " + blob.length);
2049 }
2050 mCapture.copyPixelsFromBuffer(buffer);
John Reck1cf4b792011-07-26 10:22:22 -07002051 }
2052 }
2053
2054 private byte[] getCaptureBlob() {
2055 synchronized (Tab.this) {
2056 if (mCapture == null) {
2057 return null;
2058 }
2059 ByteBuffer buffer = ByteBuffer.allocate(mCapture.getByteCount());
2060 mCapture.copyPixelsToBuffer(buffer);
2061 return buffer.array();
2062 }
2063 }
2064
2065 private Runnable mSaveThumbnail = new Runnable() {
2066
2067 @Override
2068 public void run() {
2069 byte[] blob = getCaptureBlob();
2070 if (blob == null) {
2071 return;
2072 }
2073 ContentResolver cr = mContext.getContentResolver();
2074 ContentValues values = new ContentValues();
2075 values.put(Thumbnails._ID, mId);
2076 values.put(Thumbnails.THUMBNAIL, blob);
2077 cr.insert(Thumbnails.CONTENT_URI, values);
2078 }
2079 };
2080
2081 private Runnable mDeleteThumbnail = new Runnable() {
2082
2083 @Override
2084 public void run() {
2085 ContentResolver cr = mContext.getContentResolver();
2086 try {
2087 cr.delete(ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId),
2088 null, null);
2089 } catch (Throwable t) {}
2090 }
2091 };
2092
2093 private Runnable mLoadThumbnail = new Runnable() {
2094
2095 @Override
2096 public void run() {
2097 ContentResolver cr = mContext.getContentResolver();
2098 Cursor c = null;
2099 try {
2100 Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId);
2101 c = cr.query(uri, new String[] {Thumbnails._ID,
2102 Thumbnails.THUMBNAIL}, null, null, null);
2103 if (c.moveToFirst()) {
2104 byte[] data = c.getBlob(1);
2105 if (data != null && data.length > 0) {
2106 updateCaptureFromBlob(data);
2107 }
2108 }
2109 } finally {
2110 if (c != null) {
2111 c.close();
2112 }
2113 }
2114 }
2115 };
2116
Grace Kloba22ac16e2009-10-07 18:00:23 -07002117}