blob: 911726cc02801bef8a7dc13f89a8f3a1b393fca7 [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 Reck30c714c2010-12-16 17:30:34 -080029import android.graphics.BitmapFactory;
John Reck8cc92352011-07-06 17:41:52 -070030import android.graphics.Bitmap.CompressFormat;
Grace Kloba22ac16e2009-10-07 18:00:23 -070031import android.net.Uri;
32import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070033import android.os.Bundle;
34import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000035import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070036import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070037import android.security.KeyChainAliasCallback;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050038import android.speech.RecognizerResultsIntent;
John Reck24f18262011-06-17 14:47:20 -070039import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070040import android.util.Log;
41import android.view.KeyEvent;
42import android.view.LayoutInflater;
43import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070044import android.view.ViewStub;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070045import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000046import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050047import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070048import android.webkit.GeolocationPermissions;
49import android.webkit.HttpAuthHandler;
50import android.webkit.SslErrorHandler;
51import android.webkit.URLUtil;
52import android.webkit.ValueCallback;
53import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050054import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070055import android.webkit.WebChromeClient;
56import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080057import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070058import android.webkit.WebStorage;
59import android.webkit.WebView;
60import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000061import android.widget.CheckBox;
Grace Kloba22ac16e2009-10-07 18:00:23 -070062import android.widget.LinearLayout;
63import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000064import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070065
John Reck541f55a2011-06-07 16:34:43 -070066import com.android.browser.homepages.HomeProvider;
John Reck8cc92352011-07-06 17:41:52 -070067import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070068import com.android.common.speech.LoggingEvents;
69
70import java.io.ByteArrayOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070071import java.util.ArrayList;
72import java.util.HashMap;
73import java.util.Iterator;
74import java.util.LinkedList;
75import java.util.Map;
76import java.util.Vector;
John Reck8cc92352011-07-06 17:41:52 -070077import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070078
Grace Kloba22ac16e2009-10-07 18:00:23 -070079/**
80 * Class for maintaining Tabs with a main WebView and a subwindow.
81 */
82class Tab {
Michael Kolb8233fac2010-10-26 16:08:53 -070083
Grace Kloba22ac16e2009-10-07 18:00:23 -070084 // Log Tag
85 private static final String LOGTAG = "Tab";
Ben Murdochc42addf2010-01-28 15:19:59 +000086 // Special case the logtag for messages for the Console to make it easier to
87 // filter them and match the logtag used for these messages in older versions
88 // of the browser.
89 private static final String CONSOLE_LOGTAG = "browser";
90
John Reck30c714c2010-12-16 17:30:34 -080091 public enum LockIcon {
92 LOCK_ICON_UNSECURE,
93 LOCK_ICON_SECURE,
94 LOCK_ICON_MIXED,
95 }
Michael Kolb8233fac2010-10-26 16:08:53 -070096
Michael Kolb14612442011-06-24 13:06:29 -070097 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -070098 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -070099
Michael Kolbc831b632011-05-11 09:30:34 -0700100 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700101 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700102
Grace Kloba22ac16e2009-10-07 18:00:23 -0700103 // The Geolocation permissions prompt
104 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
105 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800106 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700107 // Main WebView
108 private WebView mMainView;
109 // Subwindow container
110 private View mSubViewContainer;
111 // Subwindow WebView
112 private WebView mSubView;
113 // Saved bundle for when we are running low on memory. It contains the
114 // information needed to restore the WebView if the user goes back to the
115 // tab.
116 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700117 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
118 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700119 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700120 // Tab that constructed by this Tab. This is used when this Tab is
121 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700122 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700123 // If true, the tab is in the foreground of the current activity.
124 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700125 // If true, the tab is in page loading state (after onPageStarted,
126 // before onPageFinsihed)
127 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800128 // The last reported progress of the current page
129 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000130 // The time the load started, used to find load page time
131 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700132 // Application identifier used to find tabs that another application wants
133 // to reuse.
134 private String mAppId;
135 // Keep the original url around to avoid killing the old WebView if the url
136 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700137 // Error console for the tab
138 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500139 // The listener that gets invoked when a download is started from the
140 // mMainView
141 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500142 // Listener used to know when we move forward or back in the history list.
143 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800144 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500145 // State of the auto-login request.
146 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700147
148 // AsyncTask for downloading touch icons
149 DownloadTouchIcon mTouchIconLoader;
150
Michael Kolbeb95db42011-03-03 10:38:40 -0800151 private Bitmap mScreenshot;
John Reck35e9dd62011-04-25 09:01:54 -0700152 private BrowserSettings mSettings;
Michael Kolbeb95db42011-03-03 10:38:40 -0800153
John Reck30c714c2010-12-16 17:30:34 -0800154 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700155 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800156 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700157 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800158 String mTitle;
159 LockIcon mLockIcon;
160 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800161 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800162
163 PageState(Context c, boolean incognito) {
164 if (incognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700165 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800166 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800167 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700168 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800169 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800170 }
Justin Hodc6cbb72011-01-24 15:14:54 -0800171 mFavicon = BitmapFactory.decodeResource(
172 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800173 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
174 }
175
176 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reckdb22ec42011-06-29 11:31:24 -0700177 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800178 mTitle = null;
179 if (URLUtil.isHttpsUrl(url)) {
180 mLockIcon = LockIcon.LOCK_ICON_SECURE;
181 } else {
182 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
183 }
184 if (favicon != null) {
185 mFavicon = favicon;
186 } else {
Justin Hodc6cbb72011-01-24 15:14:54 -0800187 mFavicon = BitmapFactory.decodeResource(
188 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800189 }
190 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700191 }
192
John Reck30c714c2010-12-16 17:30:34 -0800193 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700194 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800195
Grace Kloba22ac16e2009-10-07 18:00:23 -0700196 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700197 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700198 static final String CURRURL = "currentUrl";
199 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700200 static final String PARENTTAB = "parentTab";
201 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700202 static final String INCOGNITO = "privateBrowsingEnabled";
Michael Kolbeb95db42011-03-03 10:38:40 -0800203 static final String SCREENSHOT = "screenshot";
John Reckb0a86db2011-05-24 14:05:58 -0700204 static final String USERAGENT = "useragent";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700205
206 // -------------------------------------------------------------------------
207
Leon Scroggins58d56c62010-01-28 15:12:40 -0500208 /**
209 * Private information regarding the latest voice search. If the Tab is not
210 * in voice search mode, this will be null.
211 */
212 private VoiceSearchData mVoiceSearchData;
213 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400214 * Remove voice search mode from this tab.
215 */
216 public void revertVoiceSearchMode() {
217 if (mVoiceSearchData != null) {
218 mVoiceSearchData = null;
219 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700220 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400221 }
222 }
223 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700224
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400225 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500226 * Return whether the tab is in voice search mode.
227 */
228 public boolean isInVoiceSearchMode() {
229 return mVoiceSearchData != null;
230 }
231 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400232 * Return true if the Tab is in voice search mode and the voice search
233 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500234 */
235 public boolean voiceSearchSourceIsGoogle() {
236 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
237 }
238 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500239 * Get the title to display for the current voice search page. If the Tab
240 * is not in voice search mode, return null.
241 */
242 public String getVoiceDisplayTitle() {
243 if (mVoiceSearchData == null) return null;
244 return mVoiceSearchData.mLastVoiceSearchTitle;
245 }
246 /**
247 * Get the latest array of voice search results, to be passed to the
248 * BrowserProvider. If the Tab is not in voice search mode, return null.
249 */
250 public ArrayList<String> getVoiceSearchResults() {
251 if (mVoiceSearchData == null) return null;
252 return mVoiceSearchData.mVoiceSearchResults;
253 }
254 /**
255 * Activate voice search mode.
256 * @param intent Intent which has the results to use, or an index into the
257 * results when reusing the old results.
258 */
259 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500260 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500261 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500262 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500263 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500264 ArrayList<String> urls = intent.getStringArrayListExtra(
265 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
266 ArrayList<String> htmls = intent.getStringArrayListExtra(
267 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
268 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
269 RecognizerResultsIntent
270 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500271 // This tab is now entering voice search mode for the first time, or
272 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500273 int size = results.size();
274 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500275 throw new AssertionError("improper extras passed in Intent");
276 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500277 if (htmls == null || htmls.size() != size || baseUrls == null ||
278 (baseUrls.size() != size && baseUrls.size() != 1)) {
279 // If either of these arrays are empty/incorrectly sized, ignore
280 // them.
281 htmls = null;
282 baseUrls = null;
283 }
284 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
285 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500286 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
287 RecognizerResultsIntent
288 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500289 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
290 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400291 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500292 }
293 String extraData = intent.getStringExtra(
294 SearchManager.EXTRA_DATA_KEY);
295 if (extraData != null) {
296 index = Integer.parseInt(extraData);
297 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
298 throw new AssertionError("index must be less than "
299 + "size of mVoiceSearchResults");
300 }
301 if (mVoiceSearchData.mSourceIsGoogle) {
302 Intent logIntent = new Intent(
303 LoggingEvents.ACTION_LOG_EVENT);
304 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
305 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
306 logIntent.putExtra(
307 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
308 index);
Michael Kolb14612442011-06-24 13:06:29 -0700309 mContext.sendBroadcast(logIntent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500310 }
311 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400312 // Copy the Intent, so that each history item will have its own
313 // Intent, with different (or none) extra data.
314 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
315 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
316 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500317 }
318 }
319 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500320 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500321 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700322 mWebViewController.activateVoiceSearchMode(
323 mVoiceSearchData.mLastVoiceSearchTitle,
324 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500325 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500326 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
327 // When index was found it was already ensured that it was valid
328 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
329 if (uriString != null) {
330 Uri dataUri = Uri.parse(uriString);
331 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
332 dataUri.getScheme())) {
333 // If there is only one base URL, use it. If there are
334 // more, there will be one for each index, so use the base
335 // URL corresponding to the index.
336 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
337 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
338 index : 0);
339 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
340 mMainView.loadDataWithBaseURL(baseUrl,
341 uriString.substring(RecognizerResultsIntent
342 .URI_SCHEME_INLINE.length() + 1), "text/html",
343 "utf-8", baseUrl);
344 return;
345 }
346 }
347 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500348 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500349 = mVoiceSearchData.mVoiceSearchUrls.get(index);
350 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700351 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500352 mVoiceSearchData.mLastVoiceSearchTitle);
353 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500354 Map<String, String> headers = null;
355 if (mVoiceSearchData.mHeaders != null) {
356 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
357 : index;
358 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
359 if (bundle != null && !bundle.isEmpty()) {
360 Iterator<String> iter = bundle.keySet().iterator();
361 headers = new HashMap<String, String>();
362 while (iter.hasNext()) {
363 String key = iter.next();
364 headers.put(key, bundle.getString(key));
365 }
366 }
367 }
368 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500369 }
370 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500371 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500372 ArrayList<String> urls, ArrayList<String> htmls,
373 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500374 mVoiceSearchResults = results;
375 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500376 mVoiceSearchHtmls = htmls;
377 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500378 }
379 /*
380 * ArrayList of suggestions to be displayed when opening the
381 * SearchManager
382 */
383 public ArrayList<String> mVoiceSearchResults;
384 /*
385 * ArrayList of urls, associated with the suggestions in
386 * mVoiceSearchResults.
387 */
388 public ArrayList<String> mVoiceSearchUrls;
389 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500390 * ArrayList holding content to load for each item in
391 * mVoiceSearchResults.
392 */
393 public ArrayList<String> mVoiceSearchHtmls;
394 /*
395 * ArrayList holding base urls for the items in mVoiceSearchResults.
396 * If non null, this will either have the same size as
397 * mVoiceSearchResults or have a size of 1, in which case all will use
398 * the same base url
399 */
400 public ArrayList<String> mVoiceSearchBaseUrls;
401 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500402 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500403 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500404 */
405 public String mLastVoiceSearchUrl;
406 /**
407 * The last title used for voice search. Needed to update the title bar
408 * when switching tabs.
409 */
410 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500411 /**
412 * Whether the Intent which turned on voice search mode contained the
413 * String signifying that Google was the source.
414 */
415 public boolean mSourceIsGoogle;
416 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500417 * List of headers to be passed into the WebView containing location
418 * information
419 */
420 public ArrayList<Bundle> mHeaders;
421 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500422 * The Intent used to invoke voice search. Placed on the
423 * WebHistoryItem so that when coming back to a previous voice search
424 * page we can again activate voice search.
425 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500426 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500427 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500428 * String used to identify Google as the source of voice search.
429 */
430 public static String SOURCE_IS_GOOGLE
431 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500432 }
433
Grace Kloba22ac16e2009-10-07 18:00:23 -0700434 // Container class for the next error dialog that needs to be displayed
435 private class ErrorDialog {
436 public final int mTitle;
437 public final String mDescription;
438 public final int mError;
439 ErrorDialog(int title, String desc, int error) {
440 mTitle = title;
441 mDescription = desc;
442 mError = error;
443 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700444 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700445
446 private void processNextError() {
447 if (mQueuedErrors == null) {
448 return;
449 }
450 // The first one is currently displayed so just remove it.
451 mQueuedErrors.removeFirst();
452 if (mQueuedErrors.size() == 0) {
453 mQueuedErrors = null;
454 return;
455 }
456 showError(mQueuedErrors.getFirst());
457 }
458
459 private DialogInterface.OnDismissListener mDialogListener =
460 new DialogInterface.OnDismissListener() {
461 public void onDismiss(DialogInterface d) {
462 processNextError();
463 }
464 };
465 private LinkedList<ErrorDialog> mQueuedErrors;
466
467 private void queueError(int err, String desc) {
468 if (mQueuedErrors == null) {
469 mQueuedErrors = new LinkedList<ErrorDialog>();
470 }
471 for (ErrorDialog d : mQueuedErrors) {
472 if (d.mError == err) {
473 // Already saw a similar error, ignore the new one.
474 return;
475 }
476 }
477 ErrorDialog errDialog = new ErrorDialog(
478 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
479 R.string.browserFrameFileErrorLabel :
480 R.string.browserFrameNetworkErrorLabel,
481 desc, err);
482 mQueuedErrors.addLast(errDialog);
483
484 // Show the dialog now if the queue was empty and it is in foreground
485 if (mQueuedErrors.size() == 1 && mInForeground) {
486 showError(errDialog);
487 }
488 }
489
490 private void showError(ErrorDialog errDialog) {
491 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700492 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700493 .setTitle(errDialog.mTitle)
494 .setMessage(errDialog.mDescription)
495 .setPositiveButton(R.string.ok, null)
496 .create();
497 d.setOnDismissListener(mDialogListener);
498 d.show();
499 }
500 }
501
502 // -------------------------------------------------------------------------
503 // WebViewClient implementation for the main WebView
504 // -------------------------------------------------------------------------
505
506 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500507 private Message mDontResend;
508 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700509 @Override
510 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700511 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800512 mPageLoadProgress = 0;
Michael Kolb14612442011-06-24 13:06:29 -0700513 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800514 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000515 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500516 if (mVoiceSearchData != null
517 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500518 if (mVoiceSearchData.mSourceIsGoogle) {
519 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
520 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700521 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500522 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400523 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500524 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700525
Grace Kloba22ac16e2009-10-07 18:00:23 -0700526
527 // If we start a touch icon load and then load a new page, we don't
528 // want to cancel the current touch icon loader. But, we do want to
529 // create a new one when the touch icon url is known.
530 if (mTouchIconLoader != null) {
531 mTouchIconLoader.mTab = null;
532 mTouchIconLoader = null;
533 }
534
535 // reset the error console
536 if (mErrorConsole != null) {
537 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700538 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700539 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
540 }
541 }
542
Patrick Scott92066772011-03-10 08:46:27 -0500543 // Cancel the auto-login process.
544 if (mDeviceAccountLogin != null) {
545 mDeviceAccountLogin.cancel();
546 mDeviceAccountLogin = null;
547 mWebViewController.hideAutoLogin(Tab.this);
548 }
549
Grace Kloba22ac16e2009-10-07 18:00:23 -0700550 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800551 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500552
John Recke969cc52010-12-21 17:24:43 -0800553 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700554 }
555
556 @Override
557 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800558 if (!isPrivateBrowsingEnabled()) {
559 LogTag.logPageFinishedLoading(
560 url, SystemClock.uptimeMillis() - mLoadStartTime);
561 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700562 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800563 // Sync state (in case of stop/timeout)
564 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800565 if (mCurrentState.mUrl == null) {
566 mCurrentState.mUrl = url != null ? url : "";
567 }
John Reckdb22ec42011-06-29 11:31:24 -0700568 mCurrentState.mOriginalUrl = view.getOriginalUrl();
John Reck30c714c2010-12-16 17:30:34 -0800569 mCurrentState.mTitle = view.getTitle();
570 mCurrentState.mFavicon = view.getFavicon();
571 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
572 // In case we stop when loading an HTTPS page from an HTTP page
573 // but before a provisional load occurred
574 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
575 }
John Reck324d4402011-01-11 16:56:42 -0800576 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700577 }
578
579 // return true if want to hijack the url to let another app to handle it
580 @Override
581 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400582 if (voiceSearchSourceIsGoogle()) {
583 // This method is called when the user clicks on a link.
584 // VoiceSearchMode is turned off when the user leaves the
585 // Google results page, so at this point the user must be on
586 // that page. If the user clicked a link on that page, assume
587 // that the voice search was effective, and broadcast an Intent
588 // so a receiver can take note of that fact.
589 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
590 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
591 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700592 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400593 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700594 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800595 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
596 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700597 } else {
598 return false;
599 }
600 }
601
602 /**
603 * Updates the lock icon. This method is called when we discover another
604 * resource to be loaded for this page (for example, javascript). While
605 * we update the icon type, we do not update the lock icon itself until
606 * we are done loading, it is slightly more secure this way.
607 */
608 @Override
609 public void onLoadResource(WebView view, String url) {
610 if (url != null && url.length() > 0) {
611 // It is only if the page claims to be secure that we may have
612 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800613 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700614 // If NOT a 'safe' url, change the lock to mixed content!
615 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
616 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800617 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700618 }
619 }
620 }
621 }
622
623 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700624 * Show a dialog informing the user of the network error reported by
625 * WebCore if it is in the foreground.
626 */
627 @Override
628 public void onReceivedError(WebView view, int errorCode,
629 String description, String failingUrl) {
630 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
631 errorCode != WebViewClient.ERROR_CONNECT &&
632 errorCode != WebViewClient.ERROR_BAD_URL &&
633 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
634 errorCode != WebViewClient.ERROR_FILE) {
635 queueError(errorCode, description);
636 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500637
638 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700639 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500640 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
641 + " " + description);
642 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700643 }
644
645 /**
646 * Check with the user if it is ok to resend POST data as the page they
647 * are trying to navigate to is the result of a POST.
648 */
649 @Override
650 public void onFormResubmission(WebView view, final Message dontResend,
651 final Message resend) {
652 if (!mInForeground) {
653 dontResend.sendToTarget();
654 return;
655 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500656 if (mDontResend != null) {
657 Log.w(LOGTAG, "onFormResubmission should not be called again "
658 + "while dialog is still up");
659 dontResend.sendToTarget();
660 return;
661 }
662 mDontResend = dontResend;
663 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700664 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700665 R.string.browserFrameFormResubmitLabel).setMessage(
666 R.string.browserFrameFormResubmitMessage)
667 .setPositiveButton(R.string.ok,
668 new DialogInterface.OnClickListener() {
669 public void onClick(DialogInterface dialog,
670 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500671 if (mResend != null) {
672 mResend.sendToTarget();
673 mResend = null;
674 mDontResend = null;
675 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700676 }
677 }).setNegativeButton(R.string.cancel,
678 new DialogInterface.OnClickListener() {
679 public void onClick(DialogInterface dialog,
680 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500681 if (mDontResend != null) {
682 mDontResend.sendToTarget();
683 mResend = null;
684 mDontResend = null;
685 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700686 }
687 }).setOnCancelListener(new OnCancelListener() {
688 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500689 if (mDontResend != null) {
690 mDontResend.sendToTarget();
691 mResend = null;
692 mDontResend = null;
693 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700694 }
695 }).show();
696 }
697
698 /**
699 * Insert the url into the visited history database.
700 * @param url The url to be inserted.
701 * @param isReload True if this url is being reloaded.
702 * FIXME: Not sure what to do when reloading the page.
703 */
704 @Override
705 public void doUpdateVisitedHistory(WebView view, String url,
706 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800707 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700708 }
709
710 /**
711 * Displays SSL error(s) dialog to the user.
712 */
713 @Override
714 public void onReceivedSslError(final WebView view,
715 final SslErrorHandler handler, final SslError error) {
716 if (!mInForeground) {
717 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800718 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700719 return;
720 }
John Reck35e9dd62011-04-25 09:01:54 -0700721 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700722 final LayoutInflater factory =
Michael Kolb14612442011-06-24 13:06:29 -0700723 LayoutInflater.from(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700724 final View warningsView =
725 factory.inflate(R.layout.ssl_warnings, null);
726 final LinearLayout placeholder =
727 (LinearLayout)warningsView.findViewById(R.id.placeholder);
728
729 if (error.hasError(SslError.SSL_UNTRUSTED)) {
730 LinearLayout ll = (LinearLayout)factory
731 .inflate(R.layout.ssl_warning, null);
732 ((TextView)ll.findViewById(R.id.warning))
733 .setText(R.string.ssl_untrusted);
734 placeholder.addView(ll);
735 }
736
737 if (error.hasError(SslError.SSL_IDMISMATCH)) {
738 LinearLayout ll = (LinearLayout)factory
739 .inflate(R.layout.ssl_warning, null);
740 ((TextView)ll.findViewById(R.id.warning))
741 .setText(R.string.ssl_mismatch);
742 placeholder.addView(ll);
743 }
744
745 if (error.hasError(SslError.SSL_EXPIRED)) {
746 LinearLayout ll = (LinearLayout)factory
747 .inflate(R.layout.ssl_warning, null);
748 ((TextView)ll.findViewById(R.id.warning))
749 .setText(R.string.ssl_expired);
750 placeholder.addView(ll);
751 }
752
753 if (error.hasError(SslError.SSL_NOTYETVALID)) {
754 LinearLayout ll = (LinearLayout)factory
755 .inflate(R.layout.ssl_warning, null);
756 ((TextView)ll.findViewById(R.id.warning))
757 .setText(R.string.ssl_not_yet_valid);
758 placeholder.addView(ll);
759 }
760
Michael Kolb14612442011-06-24 13:06:29 -0700761 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700762 R.string.security_warning).setIcon(
763 android.R.drawable.ic_dialog_alert).setView(
764 warningsView).setPositiveButton(R.string.ssl_continue,
765 new DialogInterface.OnClickListener() {
766 public void onClick(DialogInterface dialog,
767 int whichButton) {
768 handler.proceed();
769 }
770 }).setNeutralButton(R.string.view_certificate,
771 new DialogInterface.OnClickListener() {
772 public void onClick(DialogInterface dialog,
773 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700774 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700775 handler, error);
776 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000777 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700778 new DialogInterface.OnClickListener() {
779 public void onClick(DialogInterface dialog,
780 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800781 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700782 }
783 }).setOnCancelListener(
784 new DialogInterface.OnCancelListener() {
785 public void onCancel(DialogInterface dialog) {
786 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800787 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
788 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700789 }
790 }).show();
791 } else {
792 handler.proceed();
793 }
794 }
795
796 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700797 * Displays client certificate request to the user.
798 */
799 @Override
800 public void onReceivedClientCertRequest(final WebView view,
801 final ClientCertRequestHandler handler, final String host_and_port) {
802 if (!mInForeground) {
803 handler.ignore();
804 return;
805 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700806 int colon = host_and_port.lastIndexOf(':');
807 String host;
808 int port;
809 if (colon == -1) {
810 host = host_and_port;
811 port = -1;
812 } else {
813 String portString = host_and_port.substring(colon + 1);
814 try {
815 port = Integer.parseInt(portString);
816 host = host_and_port.substring(0, colon);
817 } catch (NumberFormatException e) {
818 host = host_and_port;
819 port = -1;
820 }
821 }
Michael Kolb14612442011-06-24 13:06:29 -0700822 KeyChain.choosePrivateKeyAlias(
823 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700824 @Override public void alias(String alias) {
825 if (alias == null) {
826 handler.cancel();
827 return;
828 }
Michael Kolb14612442011-06-24 13:06:29 -0700829 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700830 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700831 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700832 }
833
834 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700835 * Handles an HTTP authentication request.
836 *
837 * @param handler The authentication handler
838 * @param host The host
839 * @param realm The realm
840 */
841 @Override
842 public void onReceivedHttpAuthRequest(WebView view,
843 final HttpAuthHandler handler, final String host,
844 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700845 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700846 }
847
848 @Override
John Reck438bf462011-01-12 18:11:46 -0800849 public WebResourceResponse shouldInterceptRequest(WebView view,
850 String url) {
851 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700852 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800853 return res;
854 }
855
856 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700857 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
858 if (!mInForeground) {
859 return false;
860 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700861 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700862 }
863
864 @Override
865 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700866 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700867 return;
868 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700869 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700870 }
Patrick Scott92066772011-03-10 08:46:27 -0500871
872 @Override
873 public void onReceivedLoginRequest(WebView view, String realm,
874 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700875 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500876 .handleLogin(realm, account, args);
877 }
878
Grace Kloba22ac16e2009-10-07 18:00:23 -0700879 };
880
Patrick Scott92066772011-03-10 08:46:27 -0500881 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
882 // displayed.
883 void setDeviceAccountLogin(DeviceAccountLogin login) {
884 mDeviceAccountLogin = login;
885 }
886
887 // Returns non-null if the title bar should display the auto-login UI.
888 DeviceAccountLogin getDeviceAccountLogin() {
889 return mDeviceAccountLogin;
890 }
891
Grace Kloba22ac16e2009-10-07 18:00:23 -0700892 // -------------------------------------------------------------------------
893 // WebChromeClient implementation for the main WebView
894 // -------------------------------------------------------------------------
895
896 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
897 // Helper method to create a new tab or sub window.
898 private void createWindow(final boolean dialog, final Message msg) {
899 WebView.WebViewTransport transport =
900 (WebView.WebViewTransport) msg.obj;
901 if (dialog) {
902 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700903 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700904 transport.setWebView(mSubView);
905 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700906 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700907 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700908 transport.setWebView(newTab.getWebView());
909 }
910 msg.sendToTarget();
911 }
912
913 @Override
914 public boolean onCreateWindow(WebView view, final boolean dialog,
915 final boolean userGesture, final Message resultMsg) {
916 // only allow new window or sub window for the foreground case
917 if (!mInForeground) {
918 return false;
919 }
920 // Short-circuit if we can't create any more tabs or sub windows.
921 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700922 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700923 .setTitle(R.string.too_many_subwindows_dialog_title)
924 .setIcon(android.R.drawable.ic_dialog_alert)
925 .setMessage(R.string.too_many_subwindows_dialog_message)
926 .setPositiveButton(R.string.ok, null)
927 .show();
928 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700929 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700930 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700931 .setTitle(R.string.too_many_windows_dialog_title)
932 .setIcon(android.R.drawable.ic_dialog_alert)
933 .setMessage(R.string.too_many_windows_dialog_message)
934 .setPositiveButton(R.string.ok, null)
935 .show();
936 return false;
937 }
938
939 // Short-circuit if this was a user gesture.
940 if (userGesture) {
941 createWindow(dialog, resultMsg);
942 return true;
943 }
944
945 // Allow the popup and create the appropriate window.
946 final AlertDialog.OnClickListener allowListener =
947 new AlertDialog.OnClickListener() {
948 public void onClick(DialogInterface d,
949 int which) {
950 createWindow(dialog, resultMsg);
951 }
952 };
953
954 // Block the popup by returning a null WebView.
955 final AlertDialog.OnClickListener blockListener =
956 new AlertDialog.OnClickListener() {
957 public void onClick(DialogInterface d, int which) {
958 resultMsg.sendToTarget();
959 }
960 };
961
962 // Build a confirmation dialog to display to the user.
963 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700964 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700965 .setTitle(R.string.attention)
966 .setIcon(android.R.drawable.ic_dialog_alert)
967 .setMessage(R.string.popup_window_attempt)
968 .setPositiveButton(R.string.allow, allowListener)
969 .setNegativeButton(R.string.block, blockListener)
970 .setCancelable(false)
971 .create();
972
973 // Show the confirmation dialog.
974 d.show();
975 return true;
976 }
977
978 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500979 public void onRequestFocus(WebView view) {
980 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700981 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500982 }
983 }
984
985 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700986 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700987 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700988 // JavaScript can only close popup window.
989 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700990 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700991 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700992 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700993 }
994 }
995
996 @Override
997 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800998 mPageLoadProgress = newProgress;
999 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001000 }
1001
1002 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001003 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001004 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001005 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001006 }
1007
1008 @Override
1009 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001010 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001011 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001012 }
1013
1014 @Override
1015 public void onReceivedTouchIconUrl(WebView view, String url,
1016 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001017 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001018 // Let precomposed icons take precedence over non-composed
1019 // icons.
1020 if (precomposed && mTouchIconLoader != null) {
1021 mTouchIconLoader.cancel(false);
1022 mTouchIconLoader = null;
1023 }
1024 // Have only one async task at a time.
1025 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001026 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001027 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001028 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001029 }
1030 }
1031
1032 @Override
1033 public void onShowCustomView(View view,
1034 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001035 Activity activity = mWebViewController.getActivity();
1036 if (activity != null) {
1037 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1038 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001039 }
1040
1041 @Override
1042 public void onShowCustomView(View view, int requestedOrientation,
1043 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001045 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001046 }
1047
1048 @Override
1049 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001050 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001051 }
1052
1053 /**
1054 * The origin has exceeded its database quota.
1055 * @param url the URL that exceeded the quota
1056 * @param databaseIdentifier the identifier of the database on which the
1057 * transaction that caused the quota overflow was run
1058 * @param currentQuota the current quota for the origin.
1059 * @param estimatedSize the estimated size of the database.
1060 * @param totalUsedQuota is the sum of all origins' quota.
1061 * @param quotaUpdater The callback to run when a decision to allow or
1062 * deny quota has been made. Don't forget to call this!
1063 */
1064 @Override
1065 public void onExceededDatabaseQuota(String url,
1066 String databaseIdentifier, long currentQuota, long estimatedSize,
1067 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001068 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001069 .onExceededDatabaseQuota(url, databaseIdentifier,
1070 currentQuota, estimatedSize, totalUsedQuota,
1071 quotaUpdater);
1072 }
1073
1074 /**
1075 * The Application Cache has exceeded its max size.
1076 * @param spaceNeeded is the amount of disk space that would be needed
1077 * in order for the last appcache operation to succeed.
1078 * @param totalUsedQuota is the sum of all origins' quota.
1079 * @param quotaUpdater A callback to inform the WebCore thread that a
1080 * new app cache size is available. This callback must always
1081 * be executed at some point to ensure that the sleeping
1082 * WebCore thread is woken up.
1083 */
1084 @Override
1085 public void onReachedMaxAppCacheSize(long spaceNeeded,
1086 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001087 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001088 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1089 quotaUpdater);
1090 }
1091
1092 /**
1093 * Instructs the browser to show a prompt to ask the user to set the
1094 * Geolocation permission state for the specified origin.
1095 * @param origin The origin for which Geolocation permissions are
1096 * requested.
1097 * @param callback The callback to call once the user has set the
1098 * Geolocation permission state.
1099 */
1100 @Override
1101 public void onGeolocationPermissionsShowPrompt(String origin,
1102 GeolocationPermissions.Callback callback) {
1103 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001104 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001105 }
1106 }
1107
1108 /**
1109 * Instructs the browser to hide the Geolocation permissions prompt.
1110 */
1111 @Override
1112 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001113 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001114 mGeolocationPermissionsPrompt.hide();
1115 }
1116 }
1117
Ben Murdoch65acc352009-11-19 18:16:04 +00001118 /* Adds a JavaScript error message to the system log and if the JS
1119 * console is enabled in the about:debug options, to that console
1120 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001121 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001122 */
1123 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001124 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001125 if (mInForeground) {
1126 // call getErrorConsole(true) so it will create one if needed
1127 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001128 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001129 if (mWebViewController.shouldShowErrorConsole()
1130 && errorConsole.getShowState() !=
1131 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001132 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1133 }
1134 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001135
Jeff Hamilton47654f42010-09-07 09:57:51 -05001136 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001137 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001138
Ben Murdochc42addf2010-01-28 15:19:59 +00001139 String message = "Console: " + consoleMessage.message() + " "
1140 + consoleMessage.sourceId() + ":"
1141 + consoleMessage.lineNumber();
1142
1143 switch (consoleMessage.messageLevel()) {
1144 case TIP:
1145 Log.v(CONSOLE_LOGTAG, message);
1146 break;
1147 case LOG:
1148 Log.i(CONSOLE_LOGTAG, message);
1149 break;
1150 case WARNING:
1151 Log.w(CONSOLE_LOGTAG, message);
1152 break;
1153 case ERROR:
1154 Log.e(CONSOLE_LOGTAG, message);
1155 break;
1156 case DEBUG:
1157 Log.d(CONSOLE_LOGTAG, message);
1158 break;
1159 }
1160
1161 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001162 }
1163
1164 /**
1165 * Ask the browser for an icon to represent a <video> element.
1166 * This icon will be used if the Web page did not specify a poster attribute.
1167 * @return Bitmap The icon or null if no such icon is available.
1168 */
1169 @Override
1170 public Bitmap getDefaultVideoPoster() {
1171 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001172 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001173 }
1174 return null;
1175 }
1176
1177 /**
1178 * Ask the host application for a custom progress view to show while
1179 * a <video> is loading.
1180 * @return View The progress view.
1181 */
1182 @Override
1183 public View getVideoLoadingProgressView() {
1184 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001185 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001186 }
1187 return null;
1188 }
1189
1190 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001191 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001192 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001193 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001194 } else {
1195 uploadMsg.onReceiveValue(null);
1196 }
1197 }
1198
1199 /**
1200 * Deliver a list of already-visited URLs
1201 */
1202 @Override
1203 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001204 mWebViewController.getVisitedHistory(callback);
1205 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001206
1207 @Override
1208 public void setupAutoFill(Message message) {
1209 // Prompt the user to set up their profile.
1210 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001211 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1212 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001213 Context.LAYOUT_INFLATER_SERVICE);
1214 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1215
1216 builder.setView(layout)
1217 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1218 @Override
1219 public void onClick(DialogInterface dialog, int id) {
1220 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1221 R.id.setup_autofill_dialog_disable_autofill);
1222
1223 if (disableAutoFill.isChecked()) {
1224 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001225 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001226 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001227 R.string.autofill_setup_dialog_negative_toast,
1228 Toast.LENGTH_LONG).show();
1229 } else {
1230 // Take user to the AutoFill profile editor. When they return,
1231 // we will send the message that we pass here which will trigger
1232 // the form to get filled out with their new profile.
1233 mWebViewController.setupAutoFill(msg);
1234 }
1235 }
1236 })
1237 .setNegativeButton(R.string.cancel, null)
1238 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001239 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001240 };
1241
1242 // -------------------------------------------------------------------------
1243 // WebViewClient implementation for the sub window
1244 // -------------------------------------------------------------------------
1245
1246 // Subclass of WebViewClient used in subwindows to notify the main
1247 // WebViewClient of certain WebView activities.
1248 private static class SubWindowClient extends WebViewClient {
1249 // The main WebViewClient.
1250 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001251 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001252
Michael Kolb8233fac2010-10-26 16:08:53 -07001253 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001254 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001255 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001256 }
1257 @Override
1258 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1259 // Unlike the others, do not call mClient's version, which would
1260 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001261 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001262 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001263 }
1264 @Override
1265 public void doUpdateVisitedHistory(WebView view, String url,
1266 boolean isReload) {
1267 mClient.doUpdateVisitedHistory(view, url, isReload);
1268 }
1269 @Override
1270 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1271 return mClient.shouldOverrideUrlLoading(view, url);
1272 }
1273 @Override
1274 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1275 SslError error) {
1276 mClient.onReceivedSslError(view, handler, error);
1277 }
1278 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001279 public void onReceivedClientCertRequest(WebView view,
1280 ClientCertRequestHandler handler, String host_and_port) {
1281 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1282 }
1283 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001284 public void onReceivedHttpAuthRequest(WebView view,
1285 HttpAuthHandler handler, String host, String realm) {
1286 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1287 }
1288 @Override
1289 public void onFormResubmission(WebView view, Message dontResend,
1290 Message resend) {
1291 mClient.onFormResubmission(view, dontResend, resend);
1292 }
1293 @Override
1294 public void onReceivedError(WebView view, int errorCode,
1295 String description, String failingUrl) {
1296 mClient.onReceivedError(view, errorCode, description, failingUrl);
1297 }
1298 @Override
1299 public boolean shouldOverrideKeyEvent(WebView view,
1300 android.view.KeyEvent event) {
1301 return mClient.shouldOverrideKeyEvent(view, event);
1302 }
1303 @Override
1304 public void onUnhandledKeyEvent(WebView view,
1305 android.view.KeyEvent event) {
1306 mClient.onUnhandledKeyEvent(view, event);
1307 }
1308 }
1309
1310 // -------------------------------------------------------------------------
1311 // WebChromeClient implementation for the sub window
1312 // -------------------------------------------------------------------------
1313
1314 private class SubWindowChromeClient extends WebChromeClient {
1315 // The main WebChromeClient.
1316 private final WebChromeClient mClient;
1317
1318 SubWindowChromeClient(WebChromeClient client) {
1319 mClient = client;
1320 }
1321 @Override
1322 public void onProgressChanged(WebView view, int newProgress) {
1323 mClient.onProgressChanged(view, newProgress);
1324 }
1325 @Override
1326 public boolean onCreateWindow(WebView view, boolean dialog,
1327 boolean userGesture, android.os.Message resultMsg) {
1328 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1329 }
1330 @Override
1331 public void onCloseWindow(WebView window) {
1332 if (window != mSubView) {
1333 Log.e(LOGTAG, "Can't close the window");
1334 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001335 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001336 }
1337 }
1338
1339 // -------------------------------------------------------------------------
1340
Michael Kolb8233fac2010-10-26 16:08:53 -07001341 // TODO temporarily use activity here
1342 // remove later
1343
Grace Kloba22ac16e2009-10-07 18:00:23 -07001344 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001345 Tab(WebViewController wvcontroller, WebView w) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001346 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001347 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001348 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001349 mDataController = DataController.getInstance(mContext);
1350 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001351 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001352 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001353 mInForeground = false;
1354
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001355 mDownloadListener = new DownloadListener() {
1356 public void onDownloadStart(String url, String userAgent,
1357 String contentDisposition, String mimetype,
1358 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001359 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001360 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001361 }
1362 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001363 mWebBackForwardListClient = new WebBackForwardListClient() {
1364 @Override
1365 public void onNewHistoryItem(WebHistoryItem item) {
1366 if (isInVoiceSearchMode()) {
1367 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1368 }
1369 }
1370 @Override
1371 public void onIndexChanged(WebHistoryItem item, int index) {
1372 Object data = item.getCustomData();
1373 if (data != null && data instanceof Intent) {
1374 activateVoiceSearchMode((Intent) data);
1375 }
1376 }
1377 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001378
Grace Kloba22ac16e2009-10-07 18:00:23 -07001379 setWebView(w);
1380 }
1381
Michael Kolb14612442011-06-24 13:06:29 -07001382 public void setController(WebViewController ctl) {
1383 mWebViewController = ctl;
1384 }
1385
Michael Kolbc831b632011-05-11 09:30:34 -07001386 public void setId(long id) {
1387 mId = id;
1388 }
1389
1390 public long getId() {
1391 return mId;
1392 }
1393
Grace Kloba22ac16e2009-10-07 18:00:23 -07001394 /**
1395 * Sets the WebView for this tab, correctly removing the old WebView from
1396 * the container view.
1397 */
1398 void setWebView(WebView w) {
1399 if (mMainView == w) {
1400 return;
1401 }
Michael Kolba713ec82010-11-29 17:27:06 -08001402
Grace Kloba22ac16e2009-10-07 18:00:23 -07001403 // If the WebView is changing, the page will be reloaded, so any ongoing
1404 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001405 if (mGeolocationPermissionsPrompt != null) {
1406 mGeolocationPermissionsPrompt.hide();
1407 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001408
Michael Kolba713ec82010-11-29 17:27:06 -08001409 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001410
1411 // set the new one
1412 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001413 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001414 if (mMainView != null) {
1415 mMainView.setWebViewClient(mWebViewClient);
1416 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001417 // Attach DownloadManager so that downloads can start in an active
1418 // or a non-active window. This can happen when going to a site that
1419 // does a redirect after a period of time. The user could have
1420 // switched to another tab while waiting for the download to start.
1421 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001422 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001423 }
1424 }
1425
1426 /**
1427 * Destroy the tab's main WebView and subWindow if any
1428 */
1429 void destroy() {
1430 if (mMainView != null) {
1431 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001432 // save the WebView to call destroy() after detach it from the tab
1433 WebView webView = mMainView;
1434 setWebView(null);
1435 webView.destroy();
1436 }
1437 }
1438
1439 /**
1440 * Remove the tab from the parent
1441 */
1442 void removeFromTree() {
1443 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001444 if (mChildren != null) {
1445 for(Tab t : mChildren) {
1446 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001447 }
1448 }
1449 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001450 if (mParent != null) {
1451 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001452 }
1453 }
1454
1455 /**
1456 * Create a new subwindow unless a subwindow already exists.
1457 * @return True if a new subwindow was created. False if one already exists.
1458 */
1459 boolean createSubWindow() {
1460 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001461 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001462 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001463 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001464 mSubView.setWebChromeClient(new SubWindowChromeClient(
1465 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001466 // Set a different DownloadListener for the mSubView, since it will
1467 // just need to dismiss the mSubView, rather than close the Tab
1468 mSubView.setDownloadListener(new DownloadListener() {
1469 public void onDownloadStart(String url, String userAgent,
1470 String contentDisposition, String mimetype,
1471 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001472 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001473 contentDisposition, mimetype, contentLength);
1474 if (mSubView.copyBackForwardList().getSize() == 0) {
1475 // This subwindow was opened for the sole purpose of
1476 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001477 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001478 }
1479 }
1480 });
Michael Kolb14612442011-06-24 13:06:29 -07001481 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001482 return true;
1483 }
1484 return false;
1485 }
1486
1487 /**
1488 * Dismiss the subWindow for the tab.
1489 */
1490 void dismissSubWindow() {
1491 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001492 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001493 mSubView.destroy();
1494 mSubView = null;
1495 mSubViewContainer = null;
1496 }
1497 }
1498
Grace Kloba22ac16e2009-10-07 18:00:23 -07001499
1500 /**
1501 * Set the parent tab of this tab.
1502 */
Michael Kolbc831b632011-05-11 09:30:34 -07001503 void setParent(Tab parent) {
1504 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001505 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001506 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001507 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001508 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001509 if (mSavedState != null) {
1510 if (parent == null) {
1511 mSavedState.remove(PARENTTAB);
1512 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001513 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001514 }
1515 }
John Reckb0a86db2011-05-24 14:05:58 -07001516
1517 // Sync the WebView useragent with the parent
1518 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1519 != mSettings.hasDesktopUseragent(getWebView())) {
1520 mSettings.toggleDesktopUseragent(getWebView());
1521 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001522 }
1523
1524 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001525 * If this Tab was created through another Tab, then this method returns
1526 * that Tab.
1527 * @return the Tab parent or null
1528 */
1529 public Tab getParent() {
1530 return mParent;
1531 }
1532
1533 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001534 * When a Tab is created through the content of another Tab, then we
1535 * associate the Tabs.
1536 * @param child the Tab that was created from this Tab
1537 */
1538 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001539 if (mChildren == null) {
1540 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001541 }
Michael Kolbc831b632011-05-11 09:30:34 -07001542 mChildren.add(child);
1543 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001544 }
1545
Michael Kolbc831b632011-05-11 09:30:34 -07001546 Vector<Tab> getChildren() {
1547 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001548 }
1549
1550 void resume() {
1551 if (mMainView != null) {
1552 mMainView.onResume();
1553 if (mSubView != null) {
1554 mSubView.onResume();
1555 }
1556 }
1557 }
1558
1559 void pause() {
1560 if (mMainView != null) {
1561 mMainView.onPause();
1562 if (mSubView != null) {
1563 mSubView.onPause();
1564 }
1565 }
1566 }
1567
1568 void putInForeground() {
1569 mInForeground = true;
1570 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001571 Activity activity = mWebViewController.getActivity();
1572 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001573 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001574 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001575 }
1576 // Show the pending error dialog if the queue is not empty
1577 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1578 showError(mQueuedErrors.getFirst());
1579 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001580 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001581 }
1582
1583 void putInBackground() {
1584 mInForeground = false;
1585 pause();
1586 mMainView.setOnCreateContextMenuListener(null);
1587 if (mSubView != null) {
1588 mSubView.setOnCreateContextMenuListener(null);
1589 }
1590 }
1591
Michael Kolb8233fac2010-10-26 16:08:53 -07001592 boolean inForeground() {
1593 return mInForeground;
1594 }
1595
Grace Kloba22ac16e2009-10-07 18:00:23 -07001596 /**
1597 * Return the top window of this tab; either the subwindow if it is not
1598 * null or the main window.
1599 * @return The top window of this tab.
1600 */
1601 WebView getTopWindow() {
1602 if (mSubView != null) {
1603 return mSubView;
1604 }
1605 return mMainView;
1606 }
1607
1608 /**
1609 * Return the main window of this tab. Note: if a tab is freed in the
1610 * background, this can return null. It is only guaranteed to be
1611 * non-null for the current tab.
1612 * @return The main WebView of this tab.
1613 */
1614 WebView getWebView() {
1615 return mMainView;
1616 }
1617
Michael Kolba713ec82010-11-29 17:27:06 -08001618 void setViewContainer(View container) {
1619 mContainer = container;
1620 }
1621
Michael Kolb8233fac2010-10-26 16:08:53 -07001622 View getViewContainer() {
1623 return mContainer;
1624 }
1625
Grace Kloba22ac16e2009-10-07 18:00:23 -07001626 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001627 * Return whether private browsing is enabled for the main window of
1628 * this tab.
1629 * @return True if private browsing is enabled.
1630 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001631 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001632 WebView webView = getWebView();
1633 if (webView == null) {
1634 return false;
1635 }
1636 return webView.isPrivateBrowsingEnabled();
1637 }
1638
1639 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001640 * Return the subwindow of this tab or null if there is no subwindow.
1641 * @return The subwindow of this tab or null.
1642 */
1643 WebView getSubWebView() {
1644 return mSubView;
1645 }
1646
Michael Kolb1514bb72010-11-22 09:11:48 -08001647 void setSubWebView(WebView subView) {
1648 mSubView = subView;
1649 }
1650
Michael Kolb8233fac2010-10-26 16:08:53 -07001651 View getSubViewContainer() {
1652 return mSubViewContainer;
1653 }
1654
Michael Kolb1514bb72010-11-22 09:11:48 -08001655 void setSubViewContainer(View subViewContainer) {
1656 mSubViewContainer = subViewContainer;
1657 }
1658
Grace Kloba22ac16e2009-10-07 18:00:23 -07001659 /**
1660 * @return The geolocation permissions prompt for this tab.
1661 */
1662 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001663 if (mGeolocationPermissionsPrompt == null) {
1664 ViewStub stub = (ViewStub) mContainer
1665 .findViewById(R.id.geolocation_permissions_prompt);
1666 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1667 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001668 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001669 return mGeolocationPermissionsPrompt;
1670 }
1671
1672 /**
1673 * @return The application id string
1674 */
1675 String getAppId() {
1676 return mAppId;
1677 }
1678
1679 /**
1680 * Set the application id string
1681 * @param id
1682 */
1683 void setAppId(String id) {
1684 mAppId = id;
1685 }
1686
Grace Kloba22ac16e2009-10-07 18:00:23 -07001687 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001688 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001689 }
1690
John Reck49a603c2011-03-03 09:33:05 -08001691 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001692 if (mCurrentState.mOriginalUrl == null) {
1693 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001694 }
John Reckdb22ec42011-06-29 11:31:24 -07001695 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001696 }
1697
Grace Kloba22ac16e2009-10-07 18:00:23 -07001698 /**
John Reck30c714c2010-12-16 17:30:34 -08001699 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001700 */
1701 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001702 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001703 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 }
John Reck30c714c2010-12-16 17:30:34 -08001705 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001706 }
1707
1708 /**
John Reck30c714c2010-12-16 17:30:34 -08001709 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001710 */
1711 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001712 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001713 }
1714
John Recke969cc52010-12-21 17:24:43 -08001715 public boolean isBookmarkedSite() {
1716 return mCurrentState.mIsBookmarkedSite;
1717 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001718
Grace Kloba22ac16e2009-10-07 18:00:23 -07001719 /**
1720 * Return the tab's error console. Creates the console if createIfNEcessary
1721 * is true and we haven't already created the console.
1722 * @param createIfNecessary Flag to indicate if the console should be
1723 * created if it has not been already.
1724 * @return The tab's error console, or null if one has not been created and
1725 * createIfNecessary is false.
1726 */
1727 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1728 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001729 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001730 mErrorConsole.setWebView(mMainView);
1731 }
1732 return mErrorConsole;
1733 }
1734
John Reck30c714c2010-12-16 17:30:34 -08001735 private void setLockIconType(LockIcon icon) {
1736 mCurrentState.mLockIcon = icon;
1737 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738 }
1739
1740 /**
1741 * @return The tab's lock icon type.
1742 */
John Reck30c714c2010-12-16 17:30:34 -08001743 LockIcon getLockIconType() {
1744 return mCurrentState.mLockIcon;
1745 }
1746
1747 int getLoadProgress() {
1748 if (mInPageLoad) {
1749 return mPageLoadProgress;
1750 }
1751 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001752 }
1753
1754 /**
1755 * @return TRUE if onPageStarted is called while onPageFinished is not
1756 * called yet.
1757 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001758 boolean inPageLoad() {
1759 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001760 }
1761
1762 // force mInLoad to be false. This should only be called before closing the
1763 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001764 void clearInPageLoad() {
1765 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001766 }
1767
Grace Kloba22ac16e2009-10-07 18:00:23 -07001768 /**
John Reck30c714c2010-12-16 17:30:34 -08001769 * Get the cached saved state bundle.
1770 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001771 */
1772 Bundle getSavedState() {
1773 return mSavedState;
1774 }
1775
John Reckaed9c542011-05-27 16:08:53 -07001776 Bundle getSavedState(boolean saveImages) {
1777 if (saveImages && mScreenshot != null) {
1778 Bundle b = new Bundle(mSavedState);
1779 b.putParcelable(SCREENSHOT, mScreenshot);
1780 return b;
1781 }
1782 return mSavedState;
1783 }
1784
Grace Kloba22ac16e2009-10-07 18:00:23 -07001785 /**
1786 * Set the saved state.
1787 */
1788 void setSavedState(Bundle state) {
1789 mSavedState = state;
1790 }
1791
1792 /**
1793 * @return TRUE if succeed in saving the state.
1794 */
1795 boolean saveState() {
1796 // If the WebView is null it means we ran low on memory and we already
1797 // stored the saved state in mSavedState.
1798 if (mMainView == null) {
1799 return mSavedState != null;
1800 }
John Reck24f18262011-06-17 14:47:20 -07001801 // If the tab is the homepage or has no URL, don't save it
1802 String homepage = BrowserSettings.getInstance().getHomePage();
1803 if (TextUtils.equals(homepage, mCurrentState.mUrl)
1804 || TextUtils.isEmpty(mCurrentState.mUrl)) {
1805 return false;
1806 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001807
1808 mSavedState = new Bundle();
John Reck541f55a2011-06-07 16:34:43 -07001809 mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001810
Michael Kolbc831b632011-05-11 09:30:34 -07001811 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001812 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1813 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001814 if (mAppId != null) {
1815 mSavedState.putString(APPID, mAppId);
1816 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001817 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001818 if (mParent != null) {
1819 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001820 }
John Reckb0a86db2011-05-24 14:05:58 -07001821 mSavedState.putBoolean(USERAGENT,
1822 mSettings.hasDesktopUseragent(getWebView()));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001823 return true;
1824 }
1825
1826 /*
1827 * Restore the state of the tab.
1828 */
1829 boolean restoreState(Bundle b) {
1830 if (b == null) {
1831 return false;
1832 }
1833 // Restore the internal state even if the WebView fails to restore.
1834 // This will maintain the app id, original url and close-on-exit values.
1835 mSavedState = null;
Michael Kolbc831b632011-05-11 09:30:34 -07001836 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001837 mAppId = b.getString(APPID);
Michael Kolbeb95db42011-03-03 10:38:40 -08001838 mScreenshot = b.getParcelable(SCREENSHOT);
John Reckb0a86db2011-05-24 14:05:58 -07001839 if (b.getBoolean(USERAGENT)
1840 != mSettings.hasDesktopUseragent(getWebView())) {
1841 mSettings.toggleDesktopUseragent(getWebView());
1842 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001843
1844 final WebBackForwardList list = mMainView.restoreState(b);
1845 if (list == null) {
1846 return false;
1847 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001848 return true;
1849 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001850
Leon Scroggins1961ed22010-12-07 15:22:21 -05001851 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001852 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001853 }
1854
John Recke969cc52010-12-21 17:24:43 -08001855 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1856 = new DataController.OnQueryUrlIsBookmark() {
1857 @Override
1858 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1859 if (mCurrentState.mUrl.equals(url)) {
1860 mCurrentState.mIsBookmarkedSite = isBookmark;
1861 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1862 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001863 }
John Recke969cc52010-12-21 17:24:43 -08001864 };
Michael Kolb1acef692011-03-08 14:12:06 -08001865
Michael Kolbeb95db42011-03-03 10:38:40 -08001866 public void setScreenshot(Bitmap screenshot) {
1867 mScreenshot = screenshot;
1868 }
1869
1870 public Bitmap getScreenshot() {
1871 return mScreenshot;
1872 }
1873
John Reck541f55a2011-06-07 16:34:43 -07001874 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001875 return false;
1876 }
1877
John Reckd8c74522011-06-14 08:45:00 -07001878 public ContentValues createSnapshotValues() {
1879 if (mMainView == null) return null;
John Reck8cc92352011-07-06 17:41:52 -07001880 ByteArrayOutputStream bos = new ByteArrayOutputStream();
1881 try {
1882 GZIPOutputStream stream = new GZIPOutputStream(bos);
1883 if (!mMainView.saveViewState(stream)) {
1884 return null;
1885 }
1886 stream.flush();
1887 stream.close();
1888 } catch (Exception e) {
1889 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07001890 return null;
1891 }
John Reck8cc92352011-07-06 17:41:52 -07001892 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07001893 ContentValues values = new ContentValues();
1894 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1895 values.put(Snapshots.URL, mCurrentState.mUrl);
1896 values.put(Snapshots.VIEWSTATE, data);
1897 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001898 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1899 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
1900 Bitmap screenshot = Controller.createScreenshot(mMainView,
1901 Controller.getDesiredThumbnailWidth(mContext),
1902 Controller.getDesiredThumbnailHeight(mContext));
1903 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001904 return values;
John Reck541f55a2011-06-07 16:34:43 -07001905 }
1906
John Reck8cc92352011-07-06 17:41:52 -07001907 public byte[] compressBitmap(Bitmap bitmap) {
1908 if (bitmap == null) {
1909 return null;
1910 }
1911 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1912 bitmap.compress(CompressFormat.PNG, 100, stream);
1913 return stream.toByteArray();
1914 }
1915
John Reck26b18322011-06-21 13:08:58 -07001916 public void loadUrl(String url, Map<String, String> headers) {
1917 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001918 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001919 mWebViewController.onPageStarted(this, mMainView, null);
1920 mMainView.loadUrl(url, headers);
1921 }
1922 }
1923
Grace Kloba22ac16e2009-10-07 18:00:23 -07001924}