blob: beac2ff4b71306addca826e7ab1b6491c346a0c8 [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 Reckd8c74522011-06-14 08:45:00 -070023import android.content.ContentValues;
John Reck30c714c2010-12-16 17:30:34 -080024import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070025import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070026import android.content.DialogInterface.OnCancelListener;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050027import android.content.Intent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070028import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070029import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070030import android.graphics.BitmapFactory;
31import android.graphics.Canvas;
32import android.graphics.Picture;
Grace Kloba22ac16e2009-10-07 18:00:23 -070033import android.net.Uri;
34import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070035import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070036import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070037import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000038import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070039import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070040import android.security.KeyChainAliasCallback;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050041import android.speech.RecognizerResultsIntent;
John Reck24f18262011-06-17 14:47:20 -070042import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070043import android.util.Log;
44import android.view.KeyEvent;
45import android.view.LayoutInflater;
46import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070047import android.view.ViewStub;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070048import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000049import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050050import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070051import android.webkit.GeolocationPermissions;
52import android.webkit.HttpAuthHandler;
53import android.webkit.SslErrorHandler;
54import android.webkit.URLUtil;
55import android.webkit.ValueCallback;
56import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050057import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070058import android.webkit.WebChromeClient;
59import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080060import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070061import android.webkit.WebStorage;
62import android.webkit.WebView;
Michael Kolb9ef259a2011-07-12 15:33:08 -070063import android.webkit.WebView.PictureListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070064import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000065import android.widget.CheckBox;
Grace Kloba22ac16e2009-10-07 18:00:23 -070066import android.widget.LinearLayout;
67import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000068import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070069
John Reck541f55a2011-06-07 16:34:43 -070070import com.android.browser.homepages.HomeProvider;
John Reck8cc92352011-07-06 17:41:52 -070071import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070072import com.android.common.speech.LoggingEvents;
73
74import java.io.ByteArrayOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070075import java.util.ArrayList;
76import java.util.HashMap;
77import java.util.Iterator;
78import java.util.LinkedList;
79import java.util.Map;
80import java.util.Vector;
John Reck8cc92352011-07-06 17:41:52 -070081import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070082
Grace Kloba22ac16e2009-10-07 18:00:23 -070083/**
84 * Class for maintaining Tabs with a main WebView and a subwindow.
85 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070086class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070087
Grace Kloba22ac16e2009-10-07 18:00:23 -070088 // Log Tag
89 private static final String LOGTAG = "Tab";
Ben Murdochc42addf2010-01-28 15:19:59 +000090 // Special case the logtag for messages for the Console to make it easier to
91 // filter them and match the logtag used for these messages in older versions
92 // of the browser.
93 private static final String CONSOLE_LOGTAG = "browser";
94
Michael Kolb9ef259a2011-07-12 15:33:08 -070095 private static final int MSG_CAPTURE = 42;
96 private static final int CAPTURE_DELAY = 500;
97
John Reck30c714c2010-12-16 17:30:34 -080098 public enum LockIcon {
99 LOCK_ICON_UNSECURE,
100 LOCK_ICON_SECURE,
101 LOCK_ICON_MIXED,
102 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700103
Michael Kolb14612442011-06-24 13:06:29 -0700104 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700105 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700106
Michael Kolbc831b632011-05-11 09:30:34 -0700107 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700108 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700109
Grace Kloba22ac16e2009-10-07 18:00:23 -0700110 // The Geolocation permissions prompt
111 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
112 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800113 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700114 // Main WebView
115 private WebView mMainView;
116 // Subwindow container
117 private View mSubViewContainer;
118 // Subwindow WebView
119 private WebView mSubView;
120 // Saved bundle for when we are running low on memory. It contains the
121 // information needed to restore the WebView if the user goes back to the
122 // tab.
123 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700124 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
125 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700126 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700127 // Tab that constructed by this Tab. This is used when this Tab is
128 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700129 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700130 // If true, the tab is in the foreground of the current activity.
131 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700132 // If true, the tab is in page loading state (after onPageStarted,
133 // before onPageFinsihed)
134 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800135 // The last reported progress of the current page
136 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000137 // The time the load started, used to find load page time
138 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700139 // Application identifier used to find tabs that another application wants
140 // to reuse.
141 private String mAppId;
142 // Keep the original url around to avoid killing the old WebView if the url
143 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700144 // Error console for the tab
145 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500146 // The listener that gets invoked when a download is started from the
147 // mMainView
148 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500149 // Listener used to know when we move forward or back in the history list.
150 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800151 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500152 // State of the auto-login request.
153 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700154
155 // AsyncTask for downloading touch icons
156 DownloadTouchIcon mTouchIconLoader;
157
John Reck35e9dd62011-04-25 09:01:54 -0700158 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700159 private int mCaptureWidth;
160 private int mCaptureHeight;
161 private Bitmap mCapture;
162 private Handler mHandler;
163
Michael Kolbeb95db42011-03-03 10:38:40 -0800164
John Reck30c714c2010-12-16 17:30:34 -0800165 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700166 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800167 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700168 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800169 String mTitle;
170 LockIcon mLockIcon;
171 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800172 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800173
174 PageState(Context c, boolean incognito) {
175 if (incognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700176 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800177 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800178 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700179 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800180 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800181 }
Justin Hodc6cbb72011-01-24 15:14:54 -0800182 mFavicon = BitmapFactory.decodeResource(
183 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800184 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
185 }
186
187 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reckdb22ec42011-06-29 11:31:24 -0700188 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800189 mTitle = null;
190 if (URLUtil.isHttpsUrl(url)) {
191 mLockIcon = LockIcon.LOCK_ICON_SECURE;
192 } else {
193 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
194 }
195 if (favicon != null) {
196 mFavicon = favicon;
197 } else {
Justin Hodc6cbb72011-01-24 15:14:54 -0800198 mFavicon = BitmapFactory.decodeResource(
199 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800200 }
201 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700202 }
203
John Reck30c714c2010-12-16 17:30:34 -0800204 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700205 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800206
Grace Kloba22ac16e2009-10-07 18:00:23 -0700207 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700208 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700209 static final String CURRURL = "currentUrl";
210 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700211 static final String PARENTTAB = "parentTab";
212 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700213 static final String INCOGNITO = "privateBrowsingEnabled";
Michael Kolbeb95db42011-03-03 10:38:40 -0800214 static final String SCREENSHOT = "screenshot";
John Reckb0a86db2011-05-24 14:05:58 -0700215 static final String USERAGENT = "useragent";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700216
217 // -------------------------------------------------------------------------
218
Leon Scroggins58d56c62010-01-28 15:12:40 -0500219 /**
220 * Private information regarding the latest voice search. If the Tab is not
221 * in voice search mode, this will be null.
222 */
223 private VoiceSearchData mVoiceSearchData;
224 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400225 * Remove voice search mode from this tab.
226 */
227 public void revertVoiceSearchMode() {
228 if (mVoiceSearchData != null) {
229 mVoiceSearchData = null;
230 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700231 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400232 }
233 }
234 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700235
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400236 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500237 * Return whether the tab is in voice search mode.
238 */
239 public boolean isInVoiceSearchMode() {
240 return mVoiceSearchData != null;
241 }
242 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400243 * Return true if the Tab is in voice search mode and the voice search
244 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500245 */
246 public boolean voiceSearchSourceIsGoogle() {
247 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
248 }
249 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500250 * Get the title to display for the current voice search page. If the Tab
251 * is not in voice search mode, return null.
252 */
253 public String getVoiceDisplayTitle() {
254 if (mVoiceSearchData == null) return null;
255 return mVoiceSearchData.mLastVoiceSearchTitle;
256 }
257 /**
258 * Get the latest array of voice search results, to be passed to the
259 * BrowserProvider. If the Tab is not in voice search mode, return null.
260 */
261 public ArrayList<String> getVoiceSearchResults() {
262 if (mVoiceSearchData == null) return null;
263 return mVoiceSearchData.mVoiceSearchResults;
264 }
265 /**
266 * Activate voice search mode.
267 * @param intent Intent which has the results to use, or an index into the
268 * results when reusing the old results.
269 */
270 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500271 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500272 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500273 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500274 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500275 ArrayList<String> urls = intent.getStringArrayListExtra(
276 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
277 ArrayList<String> htmls = intent.getStringArrayListExtra(
278 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
279 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
280 RecognizerResultsIntent
281 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500282 // This tab is now entering voice search mode for the first time, or
283 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500284 int size = results.size();
285 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500286 throw new AssertionError("improper extras passed in Intent");
287 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500288 if (htmls == null || htmls.size() != size || baseUrls == null ||
289 (baseUrls.size() != size && baseUrls.size() != 1)) {
290 // If either of these arrays are empty/incorrectly sized, ignore
291 // them.
292 htmls = null;
293 baseUrls = null;
294 }
295 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
296 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500297 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
298 RecognizerResultsIntent
299 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500300 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
301 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400302 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500303 }
304 String extraData = intent.getStringExtra(
305 SearchManager.EXTRA_DATA_KEY);
306 if (extraData != null) {
307 index = Integer.parseInt(extraData);
308 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
309 throw new AssertionError("index must be less than "
310 + "size of mVoiceSearchResults");
311 }
312 if (mVoiceSearchData.mSourceIsGoogle) {
313 Intent logIntent = new Intent(
314 LoggingEvents.ACTION_LOG_EVENT);
315 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
316 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
317 logIntent.putExtra(
318 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
319 index);
Michael Kolb14612442011-06-24 13:06:29 -0700320 mContext.sendBroadcast(logIntent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500321 }
322 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400323 // Copy the Intent, so that each history item will have its own
324 // Intent, with different (or none) extra data.
325 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
326 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
327 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500328 }
329 }
330 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500331 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500332 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700333 mWebViewController.activateVoiceSearchMode(
334 mVoiceSearchData.mLastVoiceSearchTitle,
335 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500336 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500337 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
338 // When index was found it was already ensured that it was valid
339 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
340 if (uriString != null) {
341 Uri dataUri = Uri.parse(uriString);
342 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
343 dataUri.getScheme())) {
344 // If there is only one base URL, use it. If there are
345 // more, there will be one for each index, so use the base
346 // URL corresponding to the index.
347 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
348 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
349 index : 0);
350 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
351 mMainView.loadDataWithBaseURL(baseUrl,
352 uriString.substring(RecognizerResultsIntent
353 .URI_SCHEME_INLINE.length() + 1), "text/html",
354 "utf-8", baseUrl);
355 return;
356 }
357 }
358 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500359 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500360 = mVoiceSearchData.mVoiceSearchUrls.get(index);
361 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700362 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500363 mVoiceSearchData.mLastVoiceSearchTitle);
364 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500365 Map<String, String> headers = null;
366 if (mVoiceSearchData.mHeaders != null) {
367 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
368 : index;
369 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
370 if (bundle != null && !bundle.isEmpty()) {
371 Iterator<String> iter = bundle.keySet().iterator();
372 headers = new HashMap<String, String>();
373 while (iter.hasNext()) {
374 String key = iter.next();
375 headers.put(key, bundle.getString(key));
376 }
377 }
378 }
379 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500380 }
381 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500382 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500383 ArrayList<String> urls, ArrayList<String> htmls,
384 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500385 mVoiceSearchResults = results;
386 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500387 mVoiceSearchHtmls = htmls;
388 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500389 }
390 /*
391 * ArrayList of suggestions to be displayed when opening the
392 * SearchManager
393 */
394 public ArrayList<String> mVoiceSearchResults;
395 /*
396 * ArrayList of urls, associated with the suggestions in
397 * mVoiceSearchResults.
398 */
399 public ArrayList<String> mVoiceSearchUrls;
400 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500401 * ArrayList holding content to load for each item in
402 * mVoiceSearchResults.
403 */
404 public ArrayList<String> mVoiceSearchHtmls;
405 /*
406 * ArrayList holding base urls for the items in mVoiceSearchResults.
407 * If non null, this will either have the same size as
408 * mVoiceSearchResults or have a size of 1, in which case all will use
409 * the same base url
410 */
411 public ArrayList<String> mVoiceSearchBaseUrls;
412 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500413 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500414 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500415 */
416 public String mLastVoiceSearchUrl;
417 /**
418 * The last title used for voice search. Needed to update the title bar
419 * when switching tabs.
420 */
421 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500422 /**
423 * Whether the Intent which turned on voice search mode contained the
424 * String signifying that Google was the source.
425 */
426 public boolean mSourceIsGoogle;
427 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500428 * List of headers to be passed into the WebView containing location
429 * information
430 */
431 public ArrayList<Bundle> mHeaders;
432 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500433 * The Intent used to invoke voice search. Placed on the
434 * WebHistoryItem so that when coming back to a previous voice search
435 * page we can again activate voice search.
436 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500437 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500438 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500439 * String used to identify Google as the source of voice search.
440 */
441 public static String SOURCE_IS_GOOGLE
442 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500443 }
444
Grace Kloba22ac16e2009-10-07 18:00:23 -0700445 // Container class for the next error dialog that needs to be displayed
446 private class ErrorDialog {
447 public final int mTitle;
448 public final String mDescription;
449 public final int mError;
450 ErrorDialog(int title, String desc, int error) {
451 mTitle = title;
452 mDescription = desc;
453 mError = error;
454 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700455 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700456
457 private void processNextError() {
458 if (mQueuedErrors == null) {
459 return;
460 }
461 // The first one is currently displayed so just remove it.
462 mQueuedErrors.removeFirst();
463 if (mQueuedErrors.size() == 0) {
464 mQueuedErrors = null;
465 return;
466 }
467 showError(mQueuedErrors.getFirst());
468 }
469
470 private DialogInterface.OnDismissListener mDialogListener =
471 new DialogInterface.OnDismissListener() {
472 public void onDismiss(DialogInterface d) {
473 processNextError();
474 }
475 };
476 private LinkedList<ErrorDialog> mQueuedErrors;
477
478 private void queueError(int err, String desc) {
479 if (mQueuedErrors == null) {
480 mQueuedErrors = new LinkedList<ErrorDialog>();
481 }
482 for (ErrorDialog d : mQueuedErrors) {
483 if (d.mError == err) {
484 // Already saw a similar error, ignore the new one.
485 return;
486 }
487 }
488 ErrorDialog errDialog = new ErrorDialog(
489 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
490 R.string.browserFrameFileErrorLabel :
491 R.string.browserFrameNetworkErrorLabel,
492 desc, err);
493 mQueuedErrors.addLast(errDialog);
494
495 // Show the dialog now if the queue was empty and it is in foreground
496 if (mQueuedErrors.size() == 1 && mInForeground) {
497 showError(errDialog);
498 }
499 }
500
501 private void showError(ErrorDialog errDialog) {
502 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700503 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700504 .setTitle(errDialog.mTitle)
505 .setMessage(errDialog.mDescription)
506 .setPositiveButton(R.string.ok, null)
507 .create();
508 d.setOnDismissListener(mDialogListener);
509 d.show();
510 }
511 }
512
513 // -------------------------------------------------------------------------
514 // WebViewClient implementation for the main WebView
515 // -------------------------------------------------------------------------
516
517 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500518 private Message mDontResend;
519 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700520 @Override
521 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700522 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800523 mPageLoadProgress = 0;
Michael Kolb14612442011-06-24 13:06:29 -0700524 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800525 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000526 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500527 if (mVoiceSearchData != null
528 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500529 if (mVoiceSearchData.mSourceIsGoogle) {
530 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
531 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700532 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500533 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400534 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500535 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700536
Grace Kloba22ac16e2009-10-07 18:00:23 -0700537
538 // If we start a touch icon load and then load a new page, we don't
539 // want to cancel the current touch icon loader. But, we do want to
540 // create a new one when the touch icon url is known.
541 if (mTouchIconLoader != null) {
542 mTouchIconLoader.mTab = null;
543 mTouchIconLoader = null;
544 }
545
546 // reset the error console
547 if (mErrorConsole != null) {
548 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700549 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700550 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
551 }
552 }
553
Patrick Scott92066772011-03-10 08:46:27 -0500554 // Cancel the auto-login process.
555 if (mDeviceAccountLogin != null) {
556 mDeviceAccountLogin.cancel();
557 mDeviceAccountLogin = null;
558 mWebViewController.hideAutoLogin(Tab.this);
559 }
560
Grace Kloba22ac16e2009-10-07 18:00:23 -0700561 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800562 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500563
John Recke969cc52010-12-21 17:24:43 -0800564 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700565 }
566
567 @Override
568 public void onPageFinished(WebView view, String url) {
John Recka1696282011-07-08 14:10:37 -0700569 if (!mInPageLoad) {
570 // In page navigation links (www.something.com#footer) will
571 // trigger an onPageFinished which we don't care about.
572 return;
573 }
John Reck5b691842010-11-29 11:21:13 -0800574 if (!isPrivateBrowsingEnabled()) {
575 LogTag.logPageFinishedLoading(
576 url, SystemClock.uptimeMillis() - mLoadStartTime);
577 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700578 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800579 // Sync state (in case of stop/timeout)
580 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800581 if (mCurrentState.mUrl == null) {
582 mCurrentState.mUrl = url != null ? url : "";
583 }
John Reckdb22ec42011-06-29 11:31:24 -0700584 mCurrentState.mOriginalUrl = view.getOriginalUrl();
John Reck30c714c2010-12-16 17:30:34 -0800585 mCurrentState.mTitle = view.getTitle();
586 mCurrentState.mFavicon = view.getFavicon();
587 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
588 // In case we stop when loading an HTTPS page from an HTTP page
589 // but before a provisional load occurred
590 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
591 }
John Reck324d4402011-01-11 16:56:42 -0800592 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700593 }
594
595 // return true if want to hijack the url to let another app to handle it
596 @Override
597 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400598 if (voiceSearchSourceIsGoogle()) {
599 // This method is called when the user clicks on a link.
600 // VoiceSearchMode is turned off when the user leaves the
601 // Google results page, so at this point the user must be on
602 // that page. If the user clicked a link on that page, assume
603 // that the voice search was effective, and broadcast an Intent
604 // so a receiver can take note of that fact.
605 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
606 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
607 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700608 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400609 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700610 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800611 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
612 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700613 } else {
614 return false;
615 }
616 }
617
618 /**
619 * Updates the lock icon. This method is called when we discover another
620 * resource to be loaded for this page (for example, javascript). While
621 * we update the icon type, we do not update the lock icon itself until
622 * we are done loading, it is slightly more secure this way.
623 */
624 @Override
625 public void onLoadResource(WebView view, String url) {
626 if (url != null && url.length() > 0) {
627 // It is only if the page claims to be secure that we may have
628 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800629 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700630 // If NOT a 'safe' url, change the lock to mixed content!
631 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
632 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800633 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700634 }
635 }
636 }
637 }
638
639 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700640 * Show a dialog informing the user of the network error reported by
641 * WebCore if it is in the foreground.
642 */
643 @Override
644 public void onReceivedError(WebView view, int errorCode,
645 String description, String failingUrl) {
646 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
647 errorCode != WebViewClient.ERROR_CONNECT &&
648 errorCode != WebViewClient.ERROR_BAD_URL &&
649 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
650 errorCode != WebViewClient.ERROR_FILE) {
651 queueError(errorCode, description);
652 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500653
654 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700655 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500656 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
657 + " " + description);
658 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700659 }
660
661 /**
662 * Check with the user if it is ok to resend POST data as the page they
663 * are trying to navigate to is the result of a POST.
664 */
665 @Override
666 public void onFormResubmission(WebView view, final Message dontResend,
667 final Message resend) {
668 if (!mInForeground) {
669 dontResend.sendToTarget();
670 return;
671 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500672 if (mDontResend != null) {
673 Log.w(LOGTAG, "onFormResubmission should not be called again "
674 + "while dialog is still up");
675 dontResend.sendToTarget();
676 return;
677 }
678 mDontResend = dontResend;
679 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700680 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700681 R.string.browserFrameFormResubmitLabel).setMessage(
682 R.string.browserFrameFormResubmitMessage)
683 .setPositiveButton(R.string.ok,
684 new DialogInterface.OnClickListener() {
685 public void onClick(DialogInterface dialog,
686 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500687 if (mResend != null) {
688 mResend.sendToTarget();
689 mResend = null;
690 mDontResend = null;
691 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700692 }
693 }).setNegativeButton(R.string.cancel,
694 new DialogInterface.OnClickListener() {
695 public void onClick(DialogInterface dialog,
696 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500697 if (mDontResend != null) {
698 mDontResend.sendToTarget();
699 mResend = null;
700 mDontResend = null;
701 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700702 }
703 }).setOnCancelListener(new OnCancelListener() {
704 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500705 if (mDontResend != null) {
706 mDontResend.sendToTarget();
707 mResend = null;
708 mDontResend = null;
709 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700710 }
711 }).show();
712 }
713
714 /**
715 * Insert the url into the visited history database.
716 * @param url The url to be inserted.
717 * @param isReload True if this url is being reloaded.
718 * FIXME: Not sure what to do when reloading the page.
719 */
720 @Override
721 public void doUpdateVisitedHistory(WebView view, String url,
722 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800723 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700724 }
725
726 /**
727 * Displays SSL error(s) dialog to the user.
728 */
729 @Override
730 public void onReceivedSslError(final WebView view,
731 final SslErrorHandler handler, final SslError error) {
732 if (!mInForeground) {
733 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800734 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700735 return;
736 }
John Reck35e9dd62011-04-25 09:01:54 -0700737 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700738 final LayoutInflater factory =
Michael Kolb14612442011-06-24 13:06:29 -0700739 LayoutInflater.from(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700740 final View warningsView =
741 factory.inflate(R.layout.ssl_warnings, null);
742 final LinearLayout placeholder =
743 (LinearLayout)warningsView.findViewById(R.id.placeholder);
744
745 if (error.hasError(SslError.SSL_UNTRUSTED)) {
746 LinearLayout ll = (LinearLayout)factory
747 .inflate(R.layout.ssl_warning, null);
748 ((TextView)ll.findViewById(R.id.warning))
749 .setText(R.string.ssl_untrusted);
750 placeholder.addView(ll);
751 }
752
753 if (error.hasError(SslError.SSL_IDMISMATCH)) {
754 LinearLayout ll = (LinearLayout)factory
755 .inflate(R.layout.ssl_warning, null);
756 ((TextView)ll.findViewById(R.id.warning))
757 .setText(R.string.ssl_mismatch);
758 placeholder.addView(ll);
759 }
760
761 if (error.hasError(SslError.SSL_EXPIRED)) {
762 LinearLayout ll = (LinearLayout)factory
763 .inflate(R.layout.ssl_warning, null);
764 ((TextView)ll.findViewById(R.id.warning))
765 .setText(R.string.ssl_expired);
766 placeholder.addView(ll);
767 }
768
769 if (error.hasError(SslError.SSL_NOTYETVALID)) {
770 LinearLayout ll = (LinearLayout)factory
771 .inflate(R.layout.ssl_warning, null);
772 ((TextView)ll.findViewById(R.id.warning))
773 .setText(R.string.ssl_not_yet_valid);
774 placeholder.addView(ll);
775 }
776
Michael Kolb14612442011-06-24 13:06:29 -0700777 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700778 R.string.security_warning).setIcon(
779 android.R.drawable.ic_dialog_alert).setView(
780 warningsView).setPositiveButton(R.string.ssl_continue,
781 new DialogInterface.OnClickListener() {
782 public void onClick(DialogInterface dialog,
783 int whichButton) {
784 handler.proceed();
785 }
786 }).setNeutralButton(R.string.view_certificate,
787 new DialogInterface.OnClickListener() {
788 public void onClick(DialogInterface dialog,
789 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700790 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700791 handler, error);
792 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000793 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700794 new DialogInterface.OnClickListener() {
795 public void onClick(DialogInterface dialog,
796 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800797 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700798 }
799 }).setOnCancelListener(
800 new DialogInterface.OnCancelListener() {
801 public void onCancel(DialogInterface dialog) {
802 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800803 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
804 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700805 }
806 }).show();
807 } else {
808 handler.proceed();
809 }
810 }
811
812 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700813 * Displays client certificate request to the user.
814 */
815 @Override
816 public void onReceivedClientCertRequest(final WebView view,
817 final ClientCertRequestHandler handler, final String host_and_port) {
818 if (!mInForeground) {
819 handler.ignore();
820 return;
821 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700822 int colon = host_and_port.lastIndexOf(':');
823 String host;
824 int port;
825 if (colon == -1) {
826 host = host_and_port;
827 port = -1;
828 } else {
829 String portString = host_and_port.substring(colon + 1);
830 try {
831 port = Integer.parseInt(portString);
832 host = host_and_port.substring(0, colon);
833 } catch (NumberFormatException e) {
834 host = host_and_port;
835 port = -1;
836 }
837 }
Michael Kolb14612442011-06-24 13:06:29 -0700838 KeyChain.choosePrivateKeyAlias(
839 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700840 @Override public void alias(String alias) {
841 if (alias == null) {
842 handler.cancel();
843 return;
844 }
Michael Kolb14612442011-06-24 13:06:29 -0700845 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700846 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700847 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700848 }
849
850 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700851 * Handles an HTTP authentication request.
852 *
853 * @param handler The authentication handler
854 * @param host The host
855 * @param realm The realm
856 */
857 @Override
858 public void onReceivedHttpAuthRequest(WebView view,
859 final HttpAuthHandler handler, final String host,
860 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700861 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700862 }
863
864 @Override
John Reck438bf462011-01-12 18:11:46 -0800865 public WebResourceResponse shouldInterceptRequest(WebView view,
866 String url) {
867 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700868 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800869 return res;
870 }
871
872 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700873 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
874 if (!mInForeground) {
875 return false;
876 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700877 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700878 }
879
880 @Override
881 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700882 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700883 return;
884 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700885 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700886 }
Patrick Scott92066772011-03-10 08:46:27 -0500887
888 @Override
889 public void onReceivedLoginRequest(WebView view, String realm,
890 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700891 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500892 .handleLogin(realm, account, args);
893 }
894
Grace Kloba22ac16e2009-10-07 18:00:23 -0700895 };
896
Patrick Scott92066772011-03-10 08:46:27 -0500897 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
898 // displayed.
899 void setDeviceAccountLogin(DeviceAccountLogin login) {
900 mDeviceAccountLogin = login;
901 }
902
903 // Returns non-null if the title bar should display the auto-login UI.
904 DeviceAccountLogin getDeviceAccountLogin() {
905 return mDeviceAccountLogin;
906 }
907
Grace Kloba22ac16e2009-10-07 18:00:23 -0700908 // -------------------------------------------------------------------------
909 // WebChromeClient implementation for the main WebView
910 // -------------------------------------------------------------------------
911
912 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
913 // Helper method to create a new tab or sub window.
914 private void createWindow(final boolean dialog, final Message msg) {
915 WebView.WebViewTransport transport =
916 (WebView.WebViewTransport) msg.obj;
917 if (dialog) {
918 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700919 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700920 transport.setWebView(mSubView);
921 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700922 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700923 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700924 transport.setWebView(newTab.getWebView());
925 }
926 msg.sendToTarget();
927 }
928
929 @Override
930 public boolean onCreateWindow(WebView view, final boolean dialog,
931 final boolean userGesture, final Message resultMsg) {
932 // only allow new window or sub window for the foreground case
933 if (!mInForeground) {
934 return false;
935 }
936 // Short-circuit if we can't create any more tabs or sub windows.
937 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700938 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700939 .setTitle(R.string.too_many_subwindows_dialog_title)
940 .setIcon(android.R.drawable.ic_dialog_alert)
941 .setMessage(R.string.too_many_subwindows_dialog_message)
942 .setPositiveButton(R.string.ok, null)
943 .show();
944 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700945 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700946 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700947 .setTitle(R.string.too_many_windows_dialog_title)
948 .setIcon(android.R.drawable.ic_dialog_alert)
949 .setMessage(R.string.too_many_windows_dialog_message)
950 .setPositiveButton(R.string.ok, null)
951 .show();
952 return false;
953 }
954
955 // Short-circuit if this was a user gesture.
956 if (userGesture) {
957 createWindow(dialog, resultMsg);
958 return true;
959 }
960
961 // Allow the popup and create the appropriate window.
962 final AlertDialog.OnClickListener allowListener =
963 new AlertDialog.OnClickListener() {
964 public void onClick(DialogInterface d,
965 int which) {
966 createWindow(dialog, resultMsg);
967 }
968 };
969
970 // Block the popup by returning a null WebView.
971 final AlertDialog.OnClickListener blockListener =
972 new AlertDialog.OnClickListener() {
973 public void onClick(DialogInterface d, int which) {
974 resultMsg.sendToTarget();
975 }
976 };
977
978 // Build a confirmation dialog to display to the user.
979 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700980 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700981 .setTitle(R.string.attention)
982 .setIcon(android.R.drawable.ic_dialog_alert)
983 .setMessage(R.string.popup_window_attempt)
984 .setPositiveButton(R.string.allow, allowListener)
985 .setNegativeButton(R.string.block, blockListener)
986 .setCancelable(false)
987 .create();
988
989 // Show the confirmation dialog.
990 d.show();
991 return true;
992 }
993
994 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500995 public void onRequestFocus(WebView view) {
996 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700997 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500998 }
999 }
1000
1001 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001002 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -07001003 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001004 // JavaScript can only close popup window.
1005 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001006 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001007 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001008 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001009 }
1010 }
1011
1012 @Override
1013 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001014 mPageLoadProgress = newProgress;
1015 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001016 }
1017
1018 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001019 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001020 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001021 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001022 }
1023
1024 @Override
1025 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001026 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001027 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001028 }
1029
1030 @Override
1031 public void onReceivedTouchIconUrl(WebView view, String url,
1032 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001033 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001034 // Let precomposed icons take precedence over non-composed
1035 // icons.
1036 if (precomposed && mTouchIconLoader != null) {
1037 mTouchIconLoader.cancel(false);
1038 mTouchIconLoader = null;
1039 }
1040 // Have only one async task at a time.
1041 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001042 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001043 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001044 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001045 }
1046 }
1047
1048 @Override
1049 public void onShowCustomView(View view,
1050 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001051 Activity activity = mWebViewController.getActivity();
1052 if (activity != null) {
1053 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1054 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001055 }
1056
1057 @Override
1058 public void onShowCustomView(View view, int requestedOrientation,
1059 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001060 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001061 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001062 }
1063
1064 @Override
1065 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001066 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001067 }
1068
1069 /**
1070 * The origin has exceeded its database quota.
1071 * @param url the URL that exceeded the quota
1072 * @param databaseIdentifier the identifier of the database on which the
1073 * transaction that caused the quota overflow was run
1074 * @param currentQuota the current quota for the origin.
1075 * @param estimatedSize the estimated size of the database.
1076 * @param totalUsedQuota is the sum of all origins' quota.
1077 * @param quotaUpdater The callback to run when a decision to allow or
1078 * deny quota has been made. Don't forget to call this!
1079 */
1080 @Override
1081 public void onExceededDatabaseQuota(String url,
1082 String databaseIdentifier, long currentQuota, long estimatedSize,
1083 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001084 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001085 .onExceededDatabaseQuota(url, databaseIdentifier,
1086 currentQuota, estimatedSize, totalUsedQuota,
1087 quotaUpdater);
1088 }
1089
1090 /**
1091 * The Application Cache has exceeded its max size.
1092 * @param spaceNeeded is the amount of disk space that would be needed
1093 * in order for the last appcache operation to succeed.
1094 * @param totalUsedQuota is the sum of all origins' quota.
1095 * @param quotaUpdater A callback to inform the WebCore thread that a
1096 * new app cache size is available. This callback must always
1097 * be executed at some point to ensure that the sleeping
1098 * WebCore thread is woken up.
1099 */
1100 @Override
1101 public void onReachedMaxAppCacheSize(long spaceNeeded,
1102 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001103 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001104 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1105 quotaUpdater);
1106 }
1107
1108 /**
1109 * Instructs the browser to show a prompt to ask the user to set the
1110 * Geolocation permission state for the specified origin.
1111 * @param origin The origin for which Geolocation permissions are
1112 * requested.
1113 * @param callback The callback to call once the user has set the
1114 * Geolocation permission state.
1115 */
1116 @Override
1117 public void onGeolocationPermissionsShowPrompt(String origin,
1118 GeolocationPermissions.Callback callback) {
1119 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001120 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001121 }
1122 }
1123
1124 /**
1125 * Instructs the browser to hide the Geolocation permissions prompt.
1126 */
1127 @Override
1128 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001129 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001130 mGeolocationPermissionsPrompt.hide();
1131 }
1132 }
1133
Ben Murdoch65acc352009-11-19 18:16:04 +00001134 /* Adds a JavaScript error message to the system log and if the JS
1135 * console is enabled in the about:debug options, to that console
1136 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001137 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001138 */
1139 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001140 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001141 if (mInForeground) {
1142 // call getErrorConsole(true) so it will create one if needed
1143 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001144 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001145 if (mWebViewController.shouldShowErrorConsole()
1146 && errorConsole.getShowState() !=
1147 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001148 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1149 }
1150 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001151
Jeff Hamilton47654f42010-09-07 09:57:51 -05001152 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001153 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001154
Ben Murdochc42addf2010-01-28 15:19:59 +00001155 String message = "Console: " + consoleMessage.message() + " "
1156 + consoleMessage.sourceId() + ":"
1157 + consoleMessage.lineNumber();
1158
1159 switch (consoleMessage.messageLevel()) {
1160 case TIP:
1161 Log.v(CONSOLE_LOGTAG, message);
1162 break;
1163 case LOG:
1164 Log.i(CONSOLE_LOGTAG, message);
1165 break;
1166 case WARNING:
1167 Log.w(CONSOLE_LOGTAG, message);
1168 break;
1169 case ERROR:
1170 Log.e(CONSOLE_LOGTAG, message);
1171 break;
1172 case DEBUG:
1173 Log.d(CONSOLE_LOGTAG, message);
1174 break;
1175 }
1176
1177 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001178 }
1179
1180 /**
1181 * Ask the browser for an icon to represent a <video> element.
1182 * This icon will be used if the Web page did not specify a poster attribute.
1183 * @return Bitmap The icon or null if no such icon is available.
1184 */
1185 @Override
1186 public Bitmap getDefaultVideoPoster() {
1187 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001188 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001189 }
1190 return null;
1191 }
1192
1193 /**
1194 * Ask the host application for a custom progress view to show while
1195 * a <video> is loading.
1196 * @return View The progress view.
1197 */
1198 @Override
1199 public View getVideoLoadingProgressView() {
1200 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001201 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001202 }
1203 return null;
1204 }
1205
1206 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001207 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001208 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001209 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001210 } else {
1211 uploadMsg.onReceiveValue(null);
1212 }
1213 }
1214
1215 /**
1216 * Deliver a list of already-visited URLs
1217 */
1218 @Override
1219 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001220 mWebViewController.getVisitedHistory(callback);
1221 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001222
1223 @Override
1224 public void setupAutoFill(Message message) {
1225 // Prompt the user to set up their profile.
1226 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001227 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1228 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001229 Context.LAYOUT_INFLATER_SERVICE);
1230 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1231
1232 builder.setView(layout)
Ben Murdochb7e6f942011-07-08 13:00:21 +01001233 .setTitle(R.string.autofill_setup_dialog_title)
Ben Murdoch1d676b62011-01-17 12:54:24 +00001234 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1235 @Override
1236 public void onClick(DialogInterface dialog, int id) {
1237 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1238 R.id.setup_autofill_dialog_disable_autofill);
1239
1240 if (disableAutoFill.isChecked()) {
1241 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001242 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001243 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001244 R.string.autofill_setup_dialog_negative_toast,
1245 Toast.LENGTH_LONG).show();
1246 } else {
1247 // Take user to the AutoFill profile editor. When they return,
1248 // we will send the message that we pass here which will trigger
1249 // the form to get filled out with their new profile.
1250 mWebViewController.setupAutoFill(msg);
1251 }
1252 }
1253 })
1254 .setNegativeButton(R.string.cancel, null)
1255 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001256 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001257 };
1258
1259 // -------------------------------------------------------------------------
1260 // WebViewClient implementation for the sub window
1261 // -------------------------------------------------------------------------
1262
1263 // Subclass of WebViewClient used in subwindows to notify the main
1264 // WebViewClient of certain WebView activities.
1265 private static class SubWindowClient extends WebViewClient {
1266 // The main WebViewClient.
1267 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001268 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001269
Michael Kolb8233fac2010-10-26 16:08:53 -07001270 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001271 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001272 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001273 }
1274 @Override
1275 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1276 // Unlike the others, do not call mClient's version, which would
1277 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001278 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001279 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001280 }
1281 @Override
1282 public void doUpdateVisitedHistory(WebView view, String url,
1283 boolean isReload) {
1284 mClient.doUpdateVisitedHistory(view, url, isReload);
1285 }
1286 @Override
1287 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1288 return mClient.shouldOverrideUrlLoading(view, url);
1289 }
1290 @Override
1291 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1292 SslError error) {
1293 mClient.onReceivedSslError(view, handler, error);
1294 }
1295 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001296 public void onReceivedClientCertRequest(WebView view,
1297 ClientCertRequestHandler handler, String host_and_port) {
1298 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1299 }
1300 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001301 public void onReceivedHttpAuthRequest(WebView view,
1302 HttpAuthHandler handler, String host, String realm) {
1303 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1304 }
1305 @Override
1306 public void onFormResubmission(WebView view, Message dontResend,
1307 Message resend) {
1308 mClient.onFormResubmission(view, dontResend, resend);
1309 }
1310 @Override
1311 public void onReceivedError(WebView view, int errorCode,
1312 String description, String failingUrl) {
1313 mClient.onReceivedError(view, errorCode, description, failingUrl);
1314 }
1315 @Override
1316 public boolean shouldOverrideKeyEvent(WebView view,
1317 android.view.KeyEvent event) {
1318 return mClient.shouldOverrideKeyEvent(view, event);
1319 }
1320 @Override
1321 public void onUnhandledKeyEvent(WebView view,
1322 android.view.KeyEvent event) {
1323 mClient.onUnhandledKeyEvent(view, event);
1324 }
1325 }
1326
1327 // -------------------------------------------------------------------------
1328 // WebChromeClient implementation for the sub window
1329 // -------------------------------------------------------------------------
1330
1331 private class SubWindowChromeClient extends WebChromeClient {
1332 // The main WebChromeClient.
1333 private final WebChromeClient mClient;
1334
1335 SubWindowChromeClient(WebChromeClient client) {
1336 mClient = client;
1337 }
1338 @Override
1339 public void onProgressChanged(WebView view, int newProgress) {
1340 mClient.onProgressChanged(view, newProgress);
1341 }
1342 @Override
1343 public boolean onCreateWindow(WebView view, boolean dialog,
1344 boolean userGesture, android.os.Message resultMsg) {
1345 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1346 }
1347 @Override
1348 public void onCloseWindow(WebView window) {
1349 if (window != mSubView) {
1350 Log.e(LOGTAG, "Can't close the window");
1351 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001352 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001353 }
1354 }
1355
1356 // -------------------------------------------------------------------------
1357
Michael Kolb8233fac2010-10-26 16:08:53 -07001358 // TODO temporarily use activity here
1359 // remove later
1360
Grace Kloba22ac16e2009-10-07 18:00:23 -07001361 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001362 Tab(WebViewController wvcontroller, WebView w) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001363 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001364 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001365 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001366 mDataController = DataController.getInstance(mContext);
1367 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001368 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001369 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001370 mInForeground = false;
1371
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001372 mDownloadListener = new DownloadListener() {
1373 public void onDownloadStart(String url, String userAgent,
1374 String contentDisposition, String mimetype,
1375 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001376 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001377 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001378 }
1379 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001380 mWebBackForwardListClient = new WebBackForwardListClient() {
1381 @Override
1382 public void onNewHistoryItem(WebHistoryItem item) {
1383 if (isInVoiceSearchMode()) {
1384 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1385 }
1386 }
1387 @Override
1388 public void onIndexChanged(WebHistoryItem item, int index) {
1389 Object data = item.getCustomData();
1390 if (data != null && data instanceof Intent) {
1391 activateVoiceSearchMode((Intent) data);
1392 }
1393 }
1394 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001395
Grace Kloba22ac16e2009-10-07 18:00:23 -07001396 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001397 mCaptureWidth = mContext.getResources().getDimensionPixelSize(R.dimen.nav_tab_width);
1398 mCaptureHeight = mContext.getResources().getDimensionPixelSize(R.dimen.nav_tab_height);
1399 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1400 Bitmap.Config.RGB_565);
1401 mHandler = new Handler() {
1402 public void handleMessage(Message m) {
1403 Tab.this.capture();
1404 }
1405 };
1406
Grace Kloba22ac16e2009-10-07 18:00:23 -07001407 }
1408
Michael Kolb14612442011-06-24 13:06:29 -07001409 public void setController(WebViewController ctl) {
1410 mWebViewController = ctl;
1411 }
1412
Michael Kolbc831b632011-05-11 09:30:34 -07001413 public void setId(long id) {
1414 mId = id;
1415 }
1416
1417 public long getId() {
1418 return mId;
1419 }
1420
Grace Kloba22ac16e2009-10-07 18:00:23 -07001421 /**
1422 * Sets the WebView for this tab, correctly removing the old WebView from
1423 * the container view.
1424 */
1425 void setWebView(WebView w) {
1426 if (mMainView == w) {
1427 return;
1428 }
Michael Kolba713ec82010-11-29 17:27:06 -08001429
Grace Kloba22ac16e2009-10-07 18:00:23 -07001430 // If the WebView is changing, the page will be reloaded, so any ongoing
1431 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001432 if (mGeolocationPermissionsPrompt != null) {
1433 mGeolocationPermissionsPrompt.hide();
1434 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001435
Michael Kolba713ec82010-11-29 17:27:06 -08001436 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001437
1438 // set the new one
1439 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001440 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001441 if (mMainView != null) {
1442 mMainView.setWebViewClient(mWebViewClient);
1443 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001444 // Attach DownloadManager so that downloads can start in an active
1445 // or a non-active window. This can happen when going to a site that
1446 // does a redirect after a period of time. The user could have
1447 // switched to another tab while waiting for the download to start.
1448 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001449 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001450 mMainView.setPictureListener(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001451 }
1452 }
1453
1454 /**
1455 * Destroy the tab's main WebView and subWindow if any
1456 */
1457 void destroy() {
1458 if (mMainView != null) {
1459 dismissSubWindow();
John Reckef654f12011-07-12 16:42:08 -07001460 // Make sure the embedded title bar isn't still attached
1461 mMainView.setEmbeddedTitleBar(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001462 // save the WebView to call destroy() after detach it from the tab
1463 WebView webView = mMainView;
1464 setWebView(null);
1465 webView.destroy();
1466 }
1467 }
1468
1469 /**
1470 * Remove the tab from the parent
1471 */
1472 void removeFromTree() {
1473 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001474 if (mChildren != null) {
1475 for(Tab t : mChildren) {
1476 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001477 }
1478 }
1479 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001480 if (mParent != null) {
1481 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001482 }
1483 }
1484
1485 /**
1486 * Create a new subwindow unless a subwindow already exists.
1487 * @return True if a new subwindow was created. False if one already exists.
1488 */
1489 boolean createSubWindow() {
1490 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001491 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001492 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001493 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001494 mSubView.setWebChromeClient(new SubWindowChromeClient(
1495 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001496 // Set a different DownloadListener for the mSubView, since it will
1497 // just need to dismiss the mSubView, rather than close the Tab
1498 mSubView.setDownloadListener(new DownloadListener() {
1499 public void onDownloadStart(String url, String userAgent,
1500 String contentDisposition, String mimetype,
1501 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001502 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001503 contentDisposition, mimetype, contentLength);
1504 if (mSubView.copyBackForwardList().getSize() == 0) {
1505 // This subwindow was opened for the sole purpose of
1506 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001507 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001508 }
1509 }
1510 });
Michael Kolb14612442011-06-24 13:06:29 -07001511 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001512 return true;
1513 }
1514 return false;
1515 }
1516
1517 /**
1518 * Dismiss the subWindow for the tab.
1519 */
1520 void dismissSubWindow() {
1521 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001522 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001523 mSubView.destroy();
1524 mSubView = null;
1525 mSubViewContainer = null;
1526 }
1527 }
1528
Grace Kloba22ac16e2009-10-07 18:00:23 -07001529
1530 /**
1531 * Set the parent tab of this tab.
1532 */
Michael Kolbc831b632011-05-11 09:30:34 -07001533 void setParent(Tab parent) {
1534 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001535 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001536 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001537 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001538 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001539 if (mSavedState != null) {
1540 if (parent == null) {
1541 mSavedState.remove(PARENTTAB);
1542 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001543 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001544 }
1545 }
John Reckb0a86db2011-05-24 14:05:58 -07001546
1547 // Sync the WebView useragent with the parent
1548 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1549 != mSettings.hasDesktopUseragent(getWebView())) {
1550 mSettings.toggleDesktopUseragent(getWebView());
1551 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001552 }
1553
1554 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001555 * If this Tab was created through another Tab, then this method returns
1556 * that Tab.
1557 * @return the Tab parent or null
1558 */
1559 public Tab getParent() {
1560 return mParent;
1561 }
1562
1563 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001564 * When a Tab is created through the content of another Tab, then we
1565 * associate the Tabs.
1566 * @param child the Tab that was created from this Tab
1567 */
1568 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001569 if (mChildren == null) {
1570 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001571 }
Michael Kolbc831b632011-05-11 09:30:34 -07001572 mChildren.add(child);
1573 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001574 }
1575
Michael Kolbc831b632011-05-11 09:30:34 -07001576 Vector<Tab> getChildren() {
1577 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001578 }
1579
1580 void resume() {
1581 if (mMainView != null) {
1582 mMainView.onResume();
1583 if (mSubView != null) {
1584 mSubView.onResume();
1585 }
1586 }
1587 }
1588
1589 void pause() {
1590 if (mMainView != null) {
1591 mMainView.onPause();
1592 if (mSubView != null) {
1593 mSubView.onPause();
1594 }
1595 }
1596 }
1597
1598 void putInForeground() {
1599 mInForeground = true;
1600 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001601 Activity activity = mWebViewController.getActivity();
1602 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001603 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001604 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001605 }
1606 // Show the pending error dialog if the queue is not empty
1607 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1608 showError(mQueuedErrors.getFirst());
1609 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001610 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001611 }
1612
1613 void putInBackground() {
1614 mInForeground = false;
1615 pause();
1616 mMainView.setOnCreateContextMenuListener(null);
1617 if (mSubView != null) {
1618 mSubView.setOnCreateContextMenuListener(null);
1619 }
1620 }
1621
Michael Kolb8233fac2010-10-26 16:08:53 -07001622 boolean inForeground() {
1623 return mInForeground;
1624 }
1625
Grace Kloba22ac16e2009-10-07 18:00:23 -07001626 /**
1627 * Return the top window of this tab; either the subwindow if it is not
1628 * null or the main window.
1629 * @return The top window of this tab.
1630 */
1631 WebView getTopWindow() {
1632 if (mSubView != null) {
1633 return mSubView;
1634 }
1635 return mMainView;
1636 }
1637
1638 /**
1639 * Return the main window of this tab. Note: if a tab is freed in the
1640 * background, this can return null. It is only guaranteed to be
1641 * non-null for the current tab.
1642 * @return The main WebView of this tab.
1643 */
1644 WebView getWebView() {
1645 return mMainView;
1646 }
1647
Michael Kolba713ec82010-11-29 17:27:06 -08001648 void setViewContainer(View container) {
1649 mContainer = container;
1650 }
1651
Michael Kolb8233fac2010-10-26 16:08:53 -07001652 View getViewContainer() {
1653 return mContainer;
1654 }
1655
Grace Kloba22ac16e2009-10-07 18:00:23 -07001656 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001657 * Return whether private browsing is enabled for the main window of
1658 * this tab.
1659 * @return True if private browsing is enabled.
1660 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001661 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001662 WebView webView = getWebView();
1663 if (webView == null) {
1664 return false;
1665 }
1666 return webView.isPrivateBrowsingEnabled();
1667 }
1668
1669 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001670 * Return the subwindow of this tab or null if there is no subwindow.
1671 * @return The subwindow of this tab or null.
1672 */
1673 WebView getSubWebView() {
1674 return mSubView;
1675 }
1676
Michael Kolb1514bb72010-11-22 09:11:48 -08001677 void setSubWebView(WebView subView) {
1678 mSubView = subView;
1679 }
1680
Michael Kolb8233fac2010-10-26 16:08:53 -07001681 View getSubViewContainer() {
1682 return mSubViewContainer;
1683 }
1684
Michael Kolb1514bb72010-11-22 09:11:48 -08001685 void setSubViewContainer(View subViewContainer) {
1686 mSubViewContainer = subViewContainer;
1687 }
1688
Grace Kloba22ac16e2009-10-07 18:00:23 -07001689 /**
1690 * @return The geolocation permissions prompt for this tab.
1691 */
1692 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001693 if (mGeolocationPermissionsPrompt == null) {
1694 ViewStub stub = (ViewStub) mContainer
1695 .findViewById(R.id.geolocation_permissions_prompt);
1696 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1697 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001698 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001699 return mGeolocationPermissionsPrompt;
1700 }
1701
1702 /**
1703 * @return The application id string
1704 */
1705 String getAppId() {
1706 return mAppId;
1707 }
1708
1709 /**
1710 * Set the application id string
1711 * @param id
1712 */
1713 void setAppId(String id) {
1714 mAppId = id;
1715 }
1716
Grace Kloba22ac16e2009-10-07 18:00:23 -07001717 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001718 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001719 }
1720
John Reck49a603c2011-03-03 09:33:05 -08001721 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001722 if (mCurrentState.mOriginalUrl == null) {
1723 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001724 }
John Reckdb22ec42011-06-29 11:31:24 -07001725 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001726 }
1727
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728 /**
John Reck30c714c2010-12-16 17:30:34 -08001729 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001730 */
1731 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001732 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001733 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001734 }
John Reck30c714c2010-12-16 17:30:34 -08001735 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001736 }
1737
1738 /**
John Reck30c714c2010-12-16 17:30:34 -08001739 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001740 */
1741 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001742 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001743 }
1744
John Recke969cc52010-12-21 17:24:43 -08001745 public boolean isBookmarkedSite() {
1746 return mCurrentState.mIsBookmarkedSite;
1747 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001748
Grace Kloba22ac16e2009-10-07 18:00:23 -07001749 /**
1750 * Return the tab's error console. Creates the console if createIfNEcessary
1751 * is true and we haven't already created the console.
1752 * @param createIfNecessary Flag to indicate if the console should be
1753 * created if it has not been already.
1754 * @return The tab's error console, or null if one has not been created and
1755 * createIfNecessary is false.
1756 */
1757 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1758 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001759 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001760 mErrorConsole.setWebView(mMainView);
1761 }
1762 return mErrorConsole;
1763 }
1764
John Reck30c714c2010-12-16 17:30:34 -08001765 private void setLockIconType(LockIcon icon) {
1766 mCurrentState.mLockIcon = icon;
1767 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001768 }
1769
1770 /**
1771 * @return The tab's lock icon type.
1772 */
John Reck30c714c2010-12-16 17:30:34 -08001773 LockIcon getLockIconType() {
1774 return mCurrentState.mLockIcon;
1775 }
1776
1777 int getLoadProgress() {
1778 if (mInPageLoad) {
1779 return mPageLoadProgress;
1780 }
1781 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001782 }
1783
1784 /**
1785 * @return TRUE if onPageStarted is called while onPageFinished is not
1786 * called yet.
1787 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001788 boolean inPageLoad() {
1789 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001790 }
1791
1792 // force mInLoad to be false. This should only be called before closing the
1793 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001794 void clearInPageLoad() {
1795 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001796 }
1797
Grace Kloba22ac16e2009-10-07 18:00:23 -07001798 /**
John Reck30c714c2010-12-16 17:30:34 -08001799 * Get the cached saved state bundle.
1800 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001801 */
1802 Bundle getSavedState() {
1803 return mSavedState;
1804 }
1805
John Reckaed9c542011-05-27 16:08:53 -07001806 Bundle getSavedState(boolean saveImages) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07001807 if (saveImages && mCapture != null) {
John Reckaed9c542011-05-27 16:08:53 -07001808 Bundle b = new Bundle(mSavedState);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001809 b.putParcelable(SCREENSHOT, mCapture);
John Reckaed9c542011-05-27 16:08:53 -07001810 return b;
1811 }
1812 return mSavedState;
1813 }
1814
Grace Kloba22ac16e2009-10-07 18:00:23 -07001815 /**
1816 * Set the saved state.
1817 */
1818 void setSavedState(Bundle state) {
1819 mSavedState = state;
1820 }
1821
1822 /**
1823 * @return TRUE if succeed in saving the state.
1824 */
1825 boolean saveState() {
1826 // If the WebView is null it means we ran low on memory and we already
1827 // stored the saved state in mSavedState.
1828 if (mMainView == null) {
1829 return mSavedState != null;
1830 }
John Reck24f18262011-06-17 14:47:20 -07001831 // If the tab is the homepage or has no URL, don't save it
1832 String homepage = BrowserSettings.getInstance().getHomePage();
1833 if (TextUtils.equals(homepage, mCurrentState.mUrl)
1834 || TextUtils.isEmpty(mCurrentState.mUrl)) {
1835 return false;
1836 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001837
1838 mSavedState = new Bundle();
John Reck541f55a2011-06-07 16:34:43 -07001839 mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001840
Michael Kolbc831b632011-05-11 09:30:34 -07001841 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001842 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1843 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001844 if (mAppId != null) {
1845 mSavedState.putString(APPID, mAppId);
1846 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001847 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001848 if (mParent != null) {
1849 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001850 }
John Reckb0a86db2011-05-24 14:05:58 -07001851 mSavedState.putBoolean(USERAGENT,
1852 mSettings.hasDesktopUseragent(getWebView()));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001853 return true;
1854 }
1855
1856 /*
1857 * Restore the state of the tab.
1858 */
1859 boolean restoreState(Bundle b) {
1860 if (b == null) {
1861 return false;
1862 }
1863 // Restore the internal state even if the WebView fails to restore.
1864 // This will maintain the app id, original url and close-on-exit values.
1865 mSavedState = null;
Michael Kolbc831b632011-05-11 09:30:34 -07001866 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001867 mAppId = b.getString(APPID);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001868 final Bitmap sshot = b.getParcelable(SCREENSHOT);
1869 if (sshot != null) {
1870 mCapture = sshot;
1871 }
John Reckb0a86db2011-05-24 14:05:58 -07001872 if (b.getBoolean(USERAGENT)
1873 != mSettings.hasDesktopUseragent(getWebView())) {
1874 mSettings.toggleDesktopUseragent(getWebView());
1875 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001876
1877 final WebBackForwardList list = mMainView.restoreState(b);
1878 if (list == null) {
1879 return false;
1880 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001881 return true;
1882 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001883
Leon Scroggins1961ed22010-12-07 15:22:21 -05001884 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001885 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001886 }
1887
John Recke969cc52010-12-21 17:24:43 -08001888 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1889 = new DataController.OnQueryUrlIsBookmark() {
1890 @Override
1891 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1892 if (mCurrentState.mUrl.equals(url)) {
1893 mCurrentState.mIsBookmarkedSite = isBookmark;
1894 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1895 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001896 }
John Recke969cc52010-12-21 17:24:43 -08001897 };
Michael Kolb1acef692011-03-08 14:12:06 -08001898
Michael Kolbeb95db42011-03-03 10:38:40 -08001899 public void setScreenshot(Bitmap screenshot) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07001900 mCapture = screenshot;
Michael Kolbeb95db42011-03-03 10:38:40 -08001901 }
1902
1903 public Bitmap getScreenshot() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07001904 return mCapture;
Michael Kolbeb95db42011-03-03 10:38:40 -08001905 }
1906
John Reck541f55a2011-06-07 16:34:43 -07001907 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001908 return false;
1909 }
1910
John Reckd8c74522011-06-14 08:45:00 -07001911 public ContentValues createSnapshotValues() {
1912 if (mMainView == null) return null;
John Reck8cc92352011-07-06 17:41:52 -07001913 ByteArrayOutputStream bos = new ByteArrayOutputStream();
1914 try {
1915 GZIPOutputStream stream = new GZIPOutputStream(bos);
1916 if (!mMainView.saveViewState(stream)) {
1917 return null;
1918 }
1919 stream.flush();
1920 stream.close();
1921 } catch (Exception e) {
1922 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07001923 return null;
1924 }
John Reck8cc92352011-07-06 17:41:52 -07001925 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07001926 ContentValues values = new ContentValues();
1927 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1928 values.put(Snapshots.URL, mCurrentState.mUrl);
1929 values.put(Snapshots.VIEWSTATE, data);
1930 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001931 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1932 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
1933 Bitmap screenshot = Controller.createScreenshot(mMainView,
1934 Controller.getDesiredThumbnailWidth(mContext),
1935 Controller.getDesiredThumbnailHeight(mContext));
1936 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001937 return values;
John Reck541f55a2011-06-07 16:34:43 -07001938 }
1939
John Reck8cc92352011-07-06 17:41:52 -07001940 public byte[] compressBitmap(Bitmap bitmap) {
1941 if (bitmap == null) {
1942 return null;
1943 }
1944 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1945 bitmap.compress(CompressFormat.PNG, 100, stream);
1946 return stream.toByteArray();
1947 }
1948
John Reck26b18322011-06-21 13:08:58 -07001949 public void loadUrl(String url, Map<String, String> headers) {
1950 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001951 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001952 mWebViewController.onPageStarted(this, mMainView, null);
1953 mMainView.loadUrl(url, headers);
1954 }
1955 }
1956
Michael Kolb9ef259a2011-07-12 15:33:08 -07001957 protected void capture() {
1958 if (mMainView == null || mCapture == null) return;
1959 Canvas c = new Canvas(mCapture);
1960 final int left = mMainView.getScrollX();
1961 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
1962 c.translate(-left, -top);
1963 float scale = mCaptureWidth / (float) mMainView.getWidth();
1964 c.scale(scale, scale, left, top);
1965 mMainView.draw(c);
1966 }
1967
1968 @Override
1969 public void onNewPicture(WebView view, Picture picture) {
1970 //update screenshot
1971 if (!mHandler.hasMessages(MSG_CAPTURE)) {
1972 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
1973 }
1974 }
1975
John Reckef654f12011-07-12 16:42:08 -07001976 public boolean canGoBack() {
1977 return mMainView != null ? mMainView.canGoBack() : false;
1978 }
1979
1980 public boolean canGoForward() {
1981 return mMainView != null ? mMainView.canGoForward() : false;
1982 }
1983
1984 public void goBack() {
1985 if (mMainView != null) {
1986 mMainView.goBack();
1987 }
1988 }
1989
1990 public void goForward() {
1991 if (mMainView != null) {
1992 mMainView.goForward();
1993 }
1994 }
1995
Grace Kloba22ac16e2009-10-07 18:00:23 -07001996}