blob: f8687a89d1cf855b471b872d60cb3a93858e0468 [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;
Grace Kloba22ac16e2009-10-07 18:00:23 -070030import android.net.Uri;
31import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070032import android.os.Bundle;
33import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000034import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070035import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070036import android.security.KeyChainAliasCallback;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050037import android.speech.RecognizerResultsIntent;
John Reck24f18262011-06-17 14:47:20 -070038import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.util.Log;
40import android.view.KeyEvent;
41import android.view.LayoutInflater;
42import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070043import android.view.ViewStub;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070044import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000045import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050046import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070047import android.webkit.GeolocationPermissions;
48import android.webkit.HttpAuthHandler;
49import android.webkit.SslErrorHandler;
50import android.webkit.URLUtil;
51import android.webkit.ValueCallback;
52import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050053import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070054import android.webkit.WebChromeClient;
55import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080056import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070057import android.webkit.WebStorage;
58import android.webkit.WebView;
59import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000060import android.widget.CheckBox;
Grace Kloba22ac16e2009-10-07 18:00:23 -070061import android.widget.LinearLayout;
62import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000063import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070064
John Reck541f55a2011-06-07 16:34:43 -070065import com.android.browser.homepages.HomeProvider;
John Reckd8c74522011-06-14 08:45:00 -070066import com.android.browser.provider.BrowserProvider2.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070067import com.android.common.speech.LoggingEvents;
68
69import java.io.ByteArrayOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070070import java.util.ArrayList;
71import java.util.HashMap;
72import java.util.Iterator;
73import java.util.LinkedList;
74import java.util.Map;
75import java.util.Vector;
76
Grace Kloba22ac16e2009-10-07 18:00:23 -070077/**
78 * Class for maintaining Tabs with a main WebView and a subwindow.
79 */
80class Tab {
Michael Kolb8233fac2010-10-26 16:08:53 -070081
Grace Kloba22ac16e2009-10-07 18:00:23 -070082 // Log Tag
83 private static final String LOGTAG = "Tab";
Ben Murdochc42addf2010-01-28 15:19:59 +000084 // Special case the logtag for messages for the Console to make it easier to
85 // filter them and match the logtag used for these messages in older versions
86 // of the browser.
87 private static final String CONSOLE_LOGTAG = "browser";
88
John Reck30c714c2010-12-16 17:30:34 -080089 public enum LockIcon {
90 LOCK_ICON_UNSECURE,
91 LOCK_ICON_SECURE,
92 LOCK_ICON_MIXED,
93 }
Michael Kolb8233fac2010-10-26 16:08:53 -070094
Michael Kolb14612442011-06-24 13:06:29 -070095 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -070096 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -070097
Michael Kolbc831b632011-05-11 09:30:34 -070098 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -070099 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700100
Grace Kloba22ac16e2009-10-07 18:00:23 -0700101 // The Geolocation permissions prompt
102 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
103 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800104 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700105 // Main WebView
106 private WebView mMainView;
107 // Subwindow container
108 private View mSubViewContainer;
109 // Subwindow WebView
110 private WebView mSubView;
111 // Saved bundle for when we are running low on memory. It contains the
112 // information needed to restore the WebView if the user goes back to the
113 // tab.
114 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700115 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
116 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700117 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700118 // Tab that constructed by this Tab. This is used when this Tab is
119 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700120 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700121 // If true, the tab is in the foreground of the current activity.
122 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700123 // If true, the tab is in page loading state (after onPageStarted,
124 // before onPageFinsihed)
125 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800126 // The last reported progress of the current page
127 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000128 // The time the load started, used to find load page time
129 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700130 // Application identifier used to find tabs that another application wants
131 // to reuse.
132 private String mAppId;
133 // Keep the original url around to avoid killing the old WebView if the url
134 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700135 // Error console for the tab
136 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500137 // The listener that gets invoked when a download is started from the
138 // mMainView
139 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500140 // Listener used to know when we move forward or back in the history list.
141 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800142 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500143 // State of the auto-login request.
144 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700145
146 // AsyncTask for downloading touch icons
147 DownloadTouchIcon mTouchIconLoader;
148
Michael Kolbeb95db42011-03-03 10:38:40 -0800149 private Bitmap mScreenshot;
John Reck35e9dd62011-04-25 09:01:54 -0700150 private BrowserSettings mSettings;
Michael Kolbeb95db42011-03-03 10:38:40 -0800151
John Reck30c714c2010-12-16 17:30:34 -0800152 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700153 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800154 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700155 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800156 String mTitle;
157 LockIcon mLockIcon;
158 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800159 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800160
161 PageState(Context c, boolean incognito) {
162 if (incognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700163 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800164 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800165 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700166 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800167 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800168 }
Justin Hodc6cbb72011-01-24 15:14:54 -0800169 mFavicon = BitmapFactory.decodeResource(
170 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800171 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
172 }
173
174 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reckdb22ec42011-06-29 11:31:24 -0700175 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800176 mTitle = null;
177 if (URLUtil.isHttpsUrl(url)) {
178 mLockIcon = LockIcon.LOCK_ICON_SECURE;
179 } else {
180 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
181 }
182 if (favicon != null) {
183 mFavicon = favicon;
184 } else {
Justin Hodc6cbb72011-01-24 15:14:54 -0800185 mFavicon = BitmapFactory.decodeResource(
186 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800187 }
188 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700189 }
190
John Reck30c714c2010-12-16 17:30:34 -0800191 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700192 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800193
Grace Kloba22ac16e2009-10-07 18:00:23 -0700194 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700195 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700196 static final String CURRURL = "currentUrl";
197 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700198 static final String PARENTTAB = "parentTab";
199 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700200 static final String INCOGNITO = "privateBrowsingEnabled";
Michael Kolbeb95db42011-03-03 10:38:40 -0800201 static final String SCREENSHOT = "screenshot";
John Reckb0a86db2011-05-24 14:05:58 -0700202 static final String USERAGENT = "useragent";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700203
204 // -------------------------------------------------------------------------
205
Leon Scroggins58d56c62010-01-28 15:12:40 -0500206 /**
207 * Private information regarding the latest voice search. If the Tab is not
208 * in voice search mode, this will be null.
209 */
210 private VoiceSearchData mVoiceSearchData;
211 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400212 * Remove voice search mode from this tab.
213 */
214 public void revertVoiceSearchMode() {
215 if (mVoiceSearchData != null) {
216 mVoiceSearchData = null;
217 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700218 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400219 }
220 }
221 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700222
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400223 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500224 * Return whether the tab is in voice search mode.
225 */
226 public boolean isInVoiceSearchMode() {
227 return mVoiceSearchData != null;
228 }
229 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400230 * Return true if the Tab is in voice search mode and the voice search
231 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500232 */
233 public boolean voiceSearchSourceIsGoogle() {
234 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
235 }
236 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500237 * Get the title to display for the current voice search page. If the Tab
238 * is not in voice search mode, return null.
239 */
240 public String getVoiceDisplayTitle() {
241 if (mVoiceSearchData == null) return null;
242 return mVoiceSearchData.mLastVoiceSearchTitle;
243 }
244 /**
245 * Get the latest array of voice search results, to be passed to the
246 * BrowserProvider. If the Tab is not in voice search mode, return null.
247 */
248 public ArrayList<String> getVoiceSearchResults() {
249 if (mVoiceSearchData == null) return null;
250 return mVoiceSearchData.mVoiceSearchResults;
251 }
252 /**
253 * Activate voice search mode.
254 * @param intent Intent which has the results to use, or an index into the
255 * results when reusing the old results.
256 */
257 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500258 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500259 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500260 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500261 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500262 ArrayList<String> urls = intent.getStringArrayListExtra(
263 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
264 ArrayList<String> htmls = intent.getStringArrayListExtra(
265 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
266 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
267 RecognizerResultsIntent
268 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500269 // This tab is now entering voice search mode for the first time, or
270 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500271 int size = results.size();
272 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500273 throw new AssertionError("improper extras passed in Intent");
274 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500275 if (htmls == null || htmls.size() != size || baseUrls == null ||
276 (baseUrls.size() != size && baseUrls.size() != 1)) {
277 // If either of these arrays are empty/incorrectly sized, ignore
278 // them.
279 htmls = null;
280 baseUrls = null;
281 }
282 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
283 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500284 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
285 RecognizerResultsIntent
286 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500287 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
288 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400289 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500290 }
291 String extraData = intent.getStringExtra(
292 SearchManager.EXTRA_DATA_KEY);
293 if (extraData != null) {
294 index = Integer.parseInt(extraData);
295 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
296 throw new AssertionError("index must be less than "
297 + "size of mVoiceSearchResults");
298 }
299 if (mVoiceSearchData.mSourceIsGoogle) {
300 Intent logIntent = new Intent(
301 LoggingEvents.ACTION_LOG_EVENT);
302 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
303 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
304 logIntent.putExtra(
305 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
306 index);
Michael Kolb14612442011-06-24 13:06:29 -0700307 mContext.sendBroadcast(logIntent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500308 }
309 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400310 // Copy the Intent, so that each history item will have its own
311 // Intent, with different (or none) extra data.
312 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
313 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
314 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500315 }
316 }
317 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500318 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500319 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700320 mWebViewController.activateVoiceSearchMode(
321 mVoiceSearchData.mLastVoiceSearchTitle,
322 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500323 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500324 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
325 // When index was found it was already ensured that it was valid
326 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
327 if (uriString != null) {
328 Uri dataUri = Uri.parse(uriString);
329 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
330 dataUri.getScheme())) {
331 // If there is only one base URL, use it. If there are
332 // more, there will be one for each index, so use the base
333 // URL corresponding to the index.
334 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
335 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
336 index : 0);
337 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
338 mMainView.loadDataWithBaseURL(baseUrl,
339 uriString.substring(RecognizerResultsIntent
340 .URI_SCHEME_INLINE.length() + 1), "text/html",
341 "utf-8", baseUrl);
342 return;
343 }
344 }
345 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500346 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500347 = mVoiceSearchData.mVoiceSearchUrls.get(index);
348 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700349 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500350 mVoiceSearchData.mLastVoiceSearchTitle);
351 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500352 Map<String, String> headers = null;
353 if (mVoiceSearchData.mHeaders != null) {
354 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
355 : index;
356 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
357 if (bundle != null && !bundle.isEmpty()) {
358 Iterator<String> iter = bundle.keySet().iterator();
359 headers = new HashMap<String, String>();
360 while (iter.hasNext()) {
361 String key = iter.next();
362 headers.put(key, bundle.getString(key));
363 }
364 }
365 }
366 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500367 }
368 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500369 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500370 ArrayList<String> urls, ArrayList<String> htmls,
371 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500372 mVoiceSearchResults = results;
373 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500374 mVoiceSearchHtmls = htmls;
375 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500376 }
377 /*
378 * ArrayList of suggestions to be displayed when opening the
379 * SearchManager
380 */
381 public ArrayList<String> mVoiceSearchResults;
382 /*
383 * ArrayList of urls, associated with the suggestions in
384 * mVoiceSearchResults.
385 */
386 public ArrayList<String> mVoiceSearchUrls;
387 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500388 * ArrayList holding content to load for each item in
389 * mVoiceSearchResults.
390 */
391 public ArrayList<String> mVoiceSearchHtmls;
392 /*
393 * ArrayList holding base urls for the items in mVoiceSearchResults.
394 * If non null, this will either have the same size as
395 * mVoiceSearchResults or have a size of 1, in which case all will use
396 * the same base url
397 */
398 public ArrayList<String> mVoiceSearchBaseUrls;
399 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500400 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500401 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500402 */
403 public String mLastVoiceSearchUrl;
404 /**
405 * The last title used for voice search. Needed to update the title bar
406 * when switching tabs.
407 */
408 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500409 /**
410 * Whether the Intent which turned on voice search mode contained the
411 * String signifying that Google was the source.
412 */
413 public boolean mSourceIsGoogle;
414 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500415 * List of headers to be passed into the WebView containing location
416 * information
417 */
418 public ArrayList<Bundle> mHeaders;
419 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500420 * The Intent used to invoke voice search. Placed on the
421 * WebHistoryItem so that when coming back to a previous voice search
422 * page we can again activate voice search.
423 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500424 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500425 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500426 * String used to identify Google as the source of voice search.
427 */
428 public static String SOURCE_IS_GOOGLE
429 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500430 }
431
Grace Kloba22ac16e2009-10-07 18:00:23 -0700432 // Container class for the next error dialog that needs to be displayed
433 private class ErrorDialog {
434 public final int mTitle;
435 public final String mDescription;
436 public final int mError;
437 ErrorDialog(int title, String desc, int error) {
438 mTitle = title;
439 mDescription = desc;
440 mError = error;
441 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700442 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700443
444 private void processNextError() {
445 if (mQueuedErrors == null) {
446 return;
447 }
448 // The first one is currently displayed so just remove it.
449 mQueuedErrors.removeFirst();
450 if (mQueuedErrors.size() == 0) {
451 mQueuedErrors = null;
452 return;
453 }
454 showError(mQueuedErrors.getFirst());
455 }
456
457 private DialogInterface.OnDismissListener mDialogListener =
458 new DialogInterface.OnDismissListener() {
459 public void onDismiss(DialogInterface d) {
460 processNextError();
461 }
462 };
463 private LinkedList<ErrorDialog> mQueuedErrors;
464
465 private void queueError(int err, String desc) {
466 if (mQueuedErrors == null) {
467 mQueuedErrors = new LinkedList<ErrorDialog>();
468 }
469 for (ErrorDialog d : mQueuedErrors) {
470 if (d.mError == err) {
471 // Already saw a similar error, ignore the new one.
472 return;
473 }
474 }
475 ErrorDialog errDialog = new ErrorDialog(
476 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
477 R.string.browserFrameFileErrorLabel :
478 R.string.browserFrameNetworkErrorLabel,
479 desc, err);
480 mQueuedErrors.addLast(errDialog);
481
482 // Show the dialog now if the queue was empty and it is in foreground
483 if (mQueuedErrors.size() == 1 && mInForeground) {
484 showError(errDialog);
485 }
486 }
487
488 private void showError(ErrorDialog errDialog) {
489 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700490 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700491 .setTitle(errDialog.mTitle)
492 .setMessage(errDialog.mDescription)
493 .setPositiveButton(R.string.ok, null)
494 .create();
495 d.setOnDismissListener(mDialogListener);
496 d.show();
497 }
498 }
499
500 // -------------------------------------------------------------------------
501 // WebViewClient implementation for the main WebView
502 // -------------------------------------------------------------------------
503
504 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500505 private Message mDontResend;
506 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700507 @Override
508 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700509 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800510 mPageLoadProgress = 0;
Michael Kolb14612442011-06-24 13:06:29 -0700511 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800512 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000513 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500514 if (mVoiceSearchData != null
515 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500516 if (mVoiceSearchData.mSourceIsGoogle) {
517 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
518 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700519 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500520 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400521 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500522 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700523
Grace Kloba22ac16e2009-10-07 18:00:23 -0700524
525 // If we start a touch icon load and then load a new page, we don't
526 // want to cancel the current touch icon loader. But, we do want to
527 // create a new one when the touch icon url is known.
528 if (mTouchIconLoader != null) {
529 mTouchIconLoader.mTab = null;
530 mTouchIconLoader = null;
531 }
532
533 // reset the error console
534 if (mErrorConsole != null) {
535 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700536 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700537 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
538 }
539 }
540
Patrick Scott92066772011-03-10 08:46:27 -0500541 // Cancel the auto-login process.
542 if (mDeviceAccountLogin != null) {
543 mDeviceAccountLogin.cancel();
544 mDeviceAccountLogin = null;
545 mWebViewController.hideAutoLogin(Tab.this);
546 }
547
Grace Kloba22ac16e2009-10-07 18:00:23 -0700548 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800549 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500550
John Recke969cc52010-12-21 17:24:43 -0800551 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700552 }
553
554 @Override
555 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800556 if (!isPrivateBrowsingEnabled()) {
557 LogTag.logPageFinishedLoading(
558 url, SystemClock.uptimeMillis() - mLoadStartTime);
559 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700560 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800561 // Sync state (in case of stop/timeout)
562 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800563 if (mCurrentState.mUrl == null) {
564 mCurrentState.mUrl = url != null ? url : "";
565 }
John Reckdb22ec42011-06-29 11:31:24 -0700566 mCurrentState.mOriginalUrl = view.getOriginalUrl();
John Reck30c714c2010-12-16 17:30:34 -0800567 mCurrentState.mTitle = view.getTitle();
568 mCurrentState.mFavicon = view.getFavicon();
569 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
570 // In case we stop when loading an HTTPS page from an HTTP page
571 // but before a provisional load occurred
572 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
573 }
John Reck324d4402011-01-11 16:56:42 -0800574 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700575 }
576
577 // return true if want to hijack the url to let another app to handle it
578 @Override
579 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400580 if (voiceSearchSourceIsGoogle()) {
581 // This method is called when the user clicks on a link.
582 // VoiceSearchMode is turned off when the user leaves the
583 // Google results page, so at this point the user must be on
584 // that page. If the user clicked a link on that page, assume
585 // that the voice search was effective, and broadcast an Intent
586 // so a receiver can take note of that fact.
587 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
588 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
589 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700590 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400591 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700592 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800593 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
594 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700595 } else {
596 return false;
597 }
598 }
599
600 /**
601 * Updates the lock icon. This method is called when we discover another
602 * resource to be loaded for this page (for example, javascript). While
603 * we update the icon type, we do not update the lock icon itself until
604 * we are done loading, it is slightly more secure this way.
605 */
606 @Override
607 public void onLoadResource(WebView view, String url) {
608 if (url != null && url.length() > 0) {
609 // It is only if the page claims to be secure that we may have
610 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800611 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700612 // If NOT a 'safe' url, change the lock to mixed content!
613 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
614 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800615 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700616 }
617 }
618 }
619 }
620
621 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700622 * Show a dialog informing the user of the network error reported by
623 * WebCore if it is in the foreground.
624 */
625 @Override
626 public void onReceivedError(WebView view, int errorCode,
627 String description, String failingUrl) {
628 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
629 errorCode != WebViewClient.ERROR_CONNECT &&
630 errorCode != WebViewClient.ERROR_BAD_URL &&
631 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
632 errorCode != WebViewClient.ERROR_FILE) {
633 queueError(errorCode, description);
634 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500635
636 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700637 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500638 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
639 + " " + description);
640 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700641 }
642
643 /**
644 * Check with the user if it is ok to resend POST data as the page they
645 * are trying to navigate to is the result of a POST.
646 */
647 @Override
648 public void onFormResubmission(WebView view, final Message dontResend,
649 final Message resend) {
650 if (!mInForeground) {
651 dontResend.sendToTarget();
652 return;
653 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500654 if (mDontResend != null) {
655 Log.w(LOGTAG, "onFormResubmission should not be called again "
656 + "while dialog is still up");
657 dontResend.sendToTarget();
658 return;
659 }
660 mDontResend = dontResend;
661 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700662 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700663 R.string.browserFrameFormResubmitLabel).setMessage(
664 R.string.browserFrameFormResubmitMessage)
665 .setPositiveButton(R.string.ok,
666 new DialogInterface.OnClickListener() {
667 public void onClick(DialogInterface dialog,
668 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500669 if (mResend != null) {
670 mResend.sendToTarget();
671 mResend = null;
672 mDontResend = null;
673 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700674 }
675 }).setNegativeButton(R.string.cancel,
676 new DialogInterface.OnClickListener() {
677 public void onClick(DialogInterface dialog,
678 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500679 if (mDontResend != null) {
680 mDontResend.sendToTarget();
681 mResend = null;
682 mDontResend = null;
683 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700684 }
685 }).setOnCancelListener(new OnCancelListener() {
686 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500687 if (mDontResend != null) {
688 mDontResend.sendToTarget();
689 mResend = null;
690 mDontResend = null;
691 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700692 }
693 }).show();
694 }
695
696 /**
697 * Insert the url into the visited history database.
698 * @param url The url to be inserted.
699 * @param isReload True if this url is being reloaded.
700 * FIXME: Not sure what to do when reloading the page.
701 */
702 @Override
703 public void doUpdateVisitedHistory(WebView view, String url,
704 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800705 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700706 }
707
708 /**
709 * Displays SSL error(s) dialog to the user.
710 */
711 @Override
712 public void onReceivedSslError(final WebView view,
713 final SslErrorHandler handler, final SslError error) {
714 if (!mInForeground) {
715 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800716 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700717 return;
718 }
John Reck35e9dd62011-04-25 09:01:54 -0700719 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700720 final LayoutInflater factory =
Michael Kolb14612442011-06-24 13:06:29 -0700721 LayoutInflater.from(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700722 final View warningsView =
723 factory.inflate(R.layout.ssl_warnings, null);
724 final LinearLayout placeholder =
725 (LinearLayout)warningsView.findViewById(R.id.placeholder);
726
727 if (error.hasError(SslError.SSL_UNTRUSTED)) {
728 LinearLayout ll = (LinearLayout)factory
729 .inflate(R.layout.ssl_warning, null);
730 ((TextView)ll.findViewById(R.id.warning))
731 .setText(R.string.ssl_untrusted);
732 placeholder.addView(ll);
733 }
734
735 if (error.hasError(SslError.SSL_IDMISMATCH)) {
736 LinearLayout ll = (LinearLayout)factory
737 .inflate(R.layout.ssl_warning, null);
738 ((TextView)ll.findViewById(R.id.warning))
739 .setText(R.string.ssl_mismatch);
740 placeholder.addView(ll);
741 }
742
743 if (error.hasError(SslError.SSL_EXPIRED)) {
744 LinearLayout ll = (LinearLayout)factory
745 .inflate(R.layout.ssl_warning, null);
746 ((TextView)ll.findViewById(R.id.warning))
747 .setText(R.string.ssl_expired);
748 placeholder.addView(ll);
749 }
750
751 if (error.hasError(SslError.SSL_NOTYETVALID)) {
752 LinearLayout ll = (LinearLayout)factory
753 .inflate(R.layout.ssl_warning, null);
754 ((TextView)ll.findViewById(R.id.warning))
755 .setText(R.string.ssl_not_yet_valid);
756 placeholder.addView(ll);
757 }
758
Michael Kolb14612442011-06-24 13:06:29 -0700759 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700760 R.string.security_warning).setIcon(
761 android.R.drawable.ic_dialog_alert).setView(
762 warningsView).setPositiveButton(R.string.ssl_continue,
763 new DialogInterface.OnClickListener() {
764 public void onClick(DialogInterface dialog,
765 int whichButton) {
766 handler.proceed();
767 }
768 }).setNeutralButton(R.string.view_certificate,
769 new DialogInterface.OnClickListener() {
770 public void onClick(DialogInterface dialog,
771 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700772 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700773 handler, error);
774 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000775 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700776 new DialogInterface.OnClickListener() {
777 public void onClick(DialogInterface dialog,
778 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800779 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700780 }
781 }).setOnCancelListener(
782 new DialogInterface.OnCancelListener() {
783 public void onCancel(DialogInterface dialog) {
784 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800785 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
786 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700787 }
788 }).show();
789 } else {
790 handler.proceed();
791 }
792 }
793
794 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700795 * Displays client certificate request to the user.
796 */
797 @Override
798 public void onReceivedClientCertRequest(final WebView view,
799 final ClientCertRequestHandler handler, final String host_and_port) {
800 if (!mInForeground) {
801 handler.ignore();
802 return;
803 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700804 int colon = host_and_port.lastIndexOf(':');
805 String host;
806 int port;
807 if (colon == -1) {
808 host = host_and_port;
809 port = -1;
810 } else {
811 String portString = host_and_port.substring(colon + 1);
812 try {
813 port = Integer.parseInt(portString);
814 host = host_and_port.substring(0, colon);
815 } catch (NumberFormatException e) {
816 host = host_and_port;
817 port = -1;
818 }
819 }
Michael Kolb14612442011-06-24 13:06:29 -0700820 KeyChain.choosePrivateKeyAlias(
821 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700822 @Override public void alias(String alias) {
823 if (alias == null) {
824 handler.cancel();
825 return;
826 }
Michael Kolb14612442011-06-24 13:06:29 -0700827 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700828 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700829 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700830 }
831
832 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700833 * Handles an HTTP authentication request.
834 *
835 * @param handler The authentication handler
836 * @param host The host
837 * @param realm The realm
838 */
839 @Override
840 public void onReceivedHttpAuthRequest(WebView view,
841 final HttpAuthHandler handler, final String host,
842 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700843 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700844 }
845
846 @Override
John Reck438bf462011-01-12 18:11:46 -0800847 public WebResourceResponse shouldInterceptRequest(WebView view,
848 String url) {
849 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700850 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800851 return res;
852 }
853
854 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700855 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
856 if (!mInForeground) {
857 return false;
858 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700859 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700860 }
861
862 @Override
863 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700865 return;
866 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700867 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700868 }
Patrick Scott92066772011-03-10 08:46:27 -0500869
870 @Override
871 public void onReceivedLoginRequest(WebView view, String realm,
872 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700873 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500874 .handleLogin(realm, account, args);
875 }
876
Grace Kloba22ac16e2009-10-07 18:00:23 -0700877 };
878
Patrick Scott92066772011-03-10 08:46:27 -0500879 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
880 // displayed.
881 void setDeviceAccountLogin(DeviceAccountLogin login) {
882 mDeviceAccountLogin = login;
883 }
884
885 // Returns non-null if the title bar should display the auto-login UI.
886 DeviceAccountLogin getDeviceAccountLogin() {
887 return mDeviceAccountLogin;
888 }
889
Grace Kloba22ac16e2009-10-07 18:00:23 -0700890 // -------------------------------------------------------------------------
891 // WebChromeClient implementation for the main WebView
892 // -------------------------------------------------------------------------
893
894 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
895 // Helper method to create a new tab or sub window.
896 private void createWindow(final boolean dialog, final Message msg) {
897 WebView.WebViewTransport transport =
898 (WebView.WebViewTransport) msg.obj;
899 if (dialog) {
900 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700901 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700902 transport.setWebView(mSubView);
903 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700904 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700905 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700906 transport.setWebView(newTab.getWebView());
907 }
908 msg.sendToTarget();
909 }
910
911 @Override
912 public boolean onCreateWindow(WebView view, final boolean dialog,
913 final boolean userGesture, final Message resultMsg) {
914 // only allow new window or sub window for the foreground case
915 if (!mInForeground) {
916 return false;
917 }
918 // Short-circuit if we can't create any more tabs or sub windows.
919 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700920 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700921 .setTitle(R.string.too_many_subwindows_dialog_title)
922 .setIcon(android.R.drawable.ic_dialog_alert)
923 .setMessage(R.string.too_many_subwindows_dialog_message)
924 .setPositiveButton(R.string.ok, null)
925 .show();
926 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700927 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700928 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700929 .setTitle(R.string.too_many_windows_dialog_title)
930 .setIcon(android.R.drawable.ic_dialog_alert)
931 .setMessage(R.string.too_many_windows_dialog_message)
932 .setPositiveButton(R.string.ok, null)
933 .show();
934 return false;
935 }
936
937 // Short-circuit if this was a user gesture.
938 if (userGesture) {
939 createWindow(dialog, resultMsg);
940 return true;
941 }
942
943 // Allow the popup and create the appropriate window.
944 final AlertDialog.OnClickListener allowListener =
945 new AlertDialog.OnClickListener() {
946 public void onClick(DialogInterface d,
947 int which) {
948 createWindow(dialog, resultMsg);
949 }
950 };
951
952 // Block the popup by returning a null WebView.
953 final AlertDialog.OnClickListener blockListener =
954 new AlertDialog.OnClickListener() {
955 public void onClick(DialogInterface d, int which) {
956 resultMsg.sendToTarget();
957 }
958 };
959
960 // Build a confirmation dialog to display to the user.
961 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700962 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700963 .setTitle(R.string.attention)
964 .setIcon(android.R.drawable.ic_dialog_alert)
965 .setMessage(R.string.popup_window_attempt)
966 .setPositiveButton(R.string.allow, allowListener)
967 .setNegativeButton(R.string.block, blockListener)
968 .setCancelable(false)
969 .create();
970
971 // Show the confirmation dialog.
972 d.show();
973 return true;
974 }
975
976 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500977 public void onRequestFocus(WebView view) {
978 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700979 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500980 }
981 }
982
983 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700984 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700985 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700986 // JavaScript can only close popup window.
987 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700988 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700989 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700990 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700991 }
992 }
993
994 @Override
995 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800996 mPageLoadProgress = newProgress;
997 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700998 }
999
1000 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001001 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001002 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001003 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001004 }
1005
1006 @Override
1007 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001008 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001009 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001010 }
1011
1012 @Override
1013 public void onReceivedTouchIconUrl(WebView view, String url,
1014 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001015 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001016 // Let precomposed icons take precedence over non-composed
1017 // icons.
1018 if (precomposed && mTouchIconLoader != null) {
1019 mTouchIconLoader.cancel(false);
1020 mTouchIconLoader = null;
1021 }
1022 // Have only one async task at a time.
1023 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001024 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001025 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001026 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001027 }
1028 }
1029
1030 @Override
1031 public void onShowCustomView(View view,
1032 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001033 Activity activity = mWebViewController.getActivity();
1034 if (activity != null) {
1035 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1036 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001037 }
1038
1039 @Override
1040 public void onShowCustomView(View view, int requestedOrientation,
1041 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001042 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001043 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001044 }
1045
1046 @Override
1047 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001048 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001049 }
1050
1051 /**
1052 * The origin has exceeded its database quota.
1053 * @param url the URL that exceeded the quota
1054 * @param databaseIdentifier the identifier of the database on which the
1055 * transaction that caused the quota overflow was run
1056 * @param currentQuota the current quota for the origin.
1057 * @param estimatedSize the estimated size of the database.
1058 * @param totalUsedQuota is the sum of all origins' quota.
1059 * @param quotaUpdater The callback to run when a decision to allow or
1060 * deny quota has been made. Don't forget to call this!
1061 */
1062 @Override
1063 public void onExceededDatabaseQuota(String url,
1064 String databaseIdentifier, long currentQuota, long estimatedSize,
1065 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001066 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001067 .onExceededDatabaseQuota(url, databaseIdentifier,
1068 currentQuota, estimatedSize, totalUsedQuota,
1069 quotaUpdater);
1070 }
1071
1072 /**
1073 * The Application Cache has exceeded its max size.
1074 * @param spaceNeeded is the amount of disk space that would be needed
1075 * in order for the last appcache operation to succeed.
1076 * @param totalUsedQuota is the sum of all origins' quota.
1077 * @param quotaUpdater A callback to inform the WebCore thread that a
1078 * new app cache size is available. This callback must always
1079 * be executed at some point to ensure that the sleeping
1080 * WebCore thread is woken up.
1081 */
1082 @Override
1083 public void onReachedMaxAppCacheSize(long spaceNeeded,
1084 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001085 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001086 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1087 quotaUpdater);
1088 }
1089
1090 /**
1091 * Instructs the browser to show a prompt to ask the user to set the
1092 * Geolocation permission state for the specified origin.
1093 * @param origin The origin for which Geolocation permissions are
1094 * requested.
1095 * @param callback The callback to call once the user has set the
1096 * Geolocation permission state.
1097 */
1098 @Override
1099 public void onGeolocationPermissionsShowPrompt(String origin,
1100 GeolocationPermissions.Callback callback) {
1101 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001102 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001103 }
1104 }
1105
1106 /**
1107 * Instructs the browser to hide the Geolocation permissions prompt.
1108 */
1109 @Override
1110 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001111 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001112 mGeolocationPermissionsPrompt.hide();
1113 }
1114 }
1115
Ben Murdoch65acc352009-11-19 18:16:04 +00001116 /* Adds a JavaScript error message to the system log and if the JS
1117 * console is enabled in the about:debug options, to that console
1118 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001119 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001120 */
1121 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001122 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001123 if (mInForeground) {
1124 // call getErrorConsole(true) so it will create one if needed
1125 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001126 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001127 if (mWebViewController.shouldShowErrorConsole()
1128 && errorConsole.getShowState() !=
1129 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001130 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1131 }
1132 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001133
Jeff Hamilton47654f42010-09-07 09:57:51 -05001134 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001135 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001136
Ben Murdochc42addf2010-01-28 15:19:59 +00001137 String message = "Console: " + consoleMessage.message() + " "
1138 + consoleMessage.sourceId() + ":"
1139 + consoleMessage.lineNumber();
1140
1141 switch (consoleMessage.messageLevel()) {
1142 case TIP:
1143 Log.v(CONSOLE_LOGTAG, message);
1144 break;
1145 case LOG:
1146 Log.i(CONSOLE_LOGTAG, message);
1147 break;
1148 case WARNING:
1149 Log.w(CONSOLE_LOGTAG, message);
1150 break;
1151 case ERROR:
1152 Log.e(CONSOLE_LOGTAG, message);
1153 break;
1154 case DEBUG:
1155 Log.d(CONSOLE_LOGTAG, message);
1156 break;
1157 }
1158
1159 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001160 }
1161
1162 /**
1163 * Ask the browser for an icon to represent a <video> element.
1164 * This icon will be used if the Web page did not specify a poster attribute.
1165 * @return Bitmap The icon or null if no such icon is available.
1166 */
1167 @Override
1168 public Bitmap getDefaultVideoPoster() {
1169 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001170 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001171 }
1172 return null;
1173 }
1174
1175 /**
1176 * Ask the host application for a custom progress view to show while
1177 * a <video> is loading.
1178 * @return View The progress view.
1179 */
1180 @Override
1181 public View getVideoLoadingProgressView() {
1182 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001183 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001184 }
1185 return null;
1186 }
1187
1188 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001189 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001190 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001191 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001192 } else {
1193 uploadMsg.onReceiveValue(null);
1194 }
1195 }
1196
1197 /**
1198 * Deliver a list of already-visited URLs
1199 */
1200 @Override
1201 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001202 mWebViewController.getVisitedHistory(callback);
1203 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001204
1205 @Override
1206 public void setupAutoFill(Message message) {
1207 // Prompt the user to set up their profile.
1208 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001209 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1210 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001211 Context.LAYOUT_INFLATER_SERVICE);
1212 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1213
1214 builder.setView(layout)
1215 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1216 @Override
1217 public void onClick(DialogInterface dialog, int id) {
1218 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1219 R.id.setup_autofill_dialog_disable_autofill);
1220
1221 if (disableAutoFill.isChecked()) {
1222 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001223 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001224 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001225 R.string.autofill_setup_dialog_negative_toast,
1226 Toast.LENGTH_LONG).show();
1227 } else {
1228 // Take user to the AutoFill profile editor. When they return,
1229 // we will send the message that we pass here which will trigger
1230 // the form to get filled out with their new profile.
1231 mWebViewController.setupAutoFill(msg);
1232 }
1233 }
1234 })
1235 .setNegativeButton(R.string.cancel, null)
1236 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001237 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001238 };
1239
1240 // -------------------------------------------------------------------------
1241 // WebViewClient implementation for the sub window
1242 // -------------------------------------------------------------------------
1243
1244 // Subclass of WebViewClient used in subwindows to notify the main
1245 // WebViewClient of certain WebView activities.
1246 private static class SubWindowClient extends WebViewClient {
1247 // The main WebViewClient.
1248 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001249 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001250
Michael Kolb8233fac2010-10-26 16:08:53 -07001251 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001252 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001253 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001254 }
1255 @Override
1256 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1257 // Unlike the others, do not call mClient's version, which would
1258 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001259 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001260 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001261 }
1262 @Override
1263 public void doUpdateVisitedHistory(WebView view, String url,
1264 boolean isReload) {
1265 mClient.doUpdateVisitedHistory(view, url, isReload);
1266 }
1267 @Override
1268 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1269 return mClient.shouldOverrideUrlLoading(view, url);
1270 }
1271 @Override
1272 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1273 SslError error) {
1274 mClient.onReceivedSslError(view, handler, error);
1275 }
1276 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001277 public void onReceivedClientCertRequest(WebView view,
1278 ClientCertRequestHandler handler, String host_and_port) {
1279 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1280 }
1281 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001282 public void onReceivedHttpAuthRequest(WebView view,
1283 HttpAuthHandler handler, String host, String realm) {
1284 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1285 }
1286 @Override
1287 public void onFormResubmission(WebView view, Message dontResend,
1288 Message resend) {
1289 mClient.onFormResubmission(view, dontResend, resend);
1290 }
1291 @Override
1292 public void onReceivedError(WebView view, int errorCode,
1293 String description, String failingUrl) {
1294 mClient.onReceivedError(view, errorCode, description, failingUrl);
1295 }
1296 @Override
1297 public boolean shouldOverrideKeyEvent(WebView view,
1298 android.view.KeyEvent event) {
1299 return mClient.shouldOverrideKeyEvent(view, event);
1300 }
1301 @Override
1302 public void onUnhandledKeyEvent(WebView view,
1303 android.view.KeyEvent event) {
1304 mClient.onUnhandledKeyEvent(view, event);
1305 }
1306 }
1307
1308 // -------------------------------------------------------------------------
1309 // WebChromeClient implementation for the sub window
1310 // -------------------------------------------------------------------------
1311
1312 private class SubWindowChromeClient extends WebChromeClient {
1313 // The main WebChromeClient.
1314 private final WebChromeClient mClient;
1315
1316 SubWindowChromeClient(WebChromeClient client) {
1317 mClient = client;
1318 }
1319 @Override
1320 public void onProgressChanged(WebView view, int newProgress) {
1321 mClient.onProgressChanged(view, newProgress);
1322 }
1323 @Override
1324 public boolean onCreateWindow(WebView view, boolean dialog,
1325 boolean userGesture, android.os.Message resultMsg) {
1326 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1327 }
1328 @Override
1329 public void onCloseWindow(WebView window) {
1330 if (window != mSubView) {
1331 Log.e(LOGTAG, "Can't close the window");
1332 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001333 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001334 }
1335 }
1336
1337 // -------------------------------------------------------------------------
1338
Michael Kolb8233fac2010-10-26 16:08:53 -07001339 // TODO temporarily use activity here
1340 // remove later
1341
Grace Kloba22ac16e2009-10-07 18:00:23 -07001342 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001343 Tab(WebViewController wvcontroller, WebView w) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001344 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001345 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001346 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001347 mDataController = DataController.getInstance(mContext);
1348 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001349 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001350 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001351 mInForeground = false;
1352
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001353 mDownloadListener = new DownloadListener() {
1354 public void onDownloadStart(String url, String userAgent,
1355 String contentDisposition, String mimetype,
1356 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001357 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001358 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001359 }
1360 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001361 mWebBackForwardListClient = new WebBackForwardListClient() {
1362 @Override
1363 public void onNewHistoryItem(WebHistoryItem item) {
1364 if (isInVoiceSearchMode()) {
1365 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1366 }
1367 }
1368 @Override
1369 public void onIndexChanged(WebHistoryItem item, int index) {
1370 Object data = item.getCustomData();
1371 if (data != null && data instanceof Intent) {
1372 activateVoiceSearchMode((Intent) data);
1373 }
1374 }
1375 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001376
Grace Kloba22ac16e2009-10-07 18:00:23 -07001377 setWebView(w);
1378 }
1379
Michael Kolb14612442011-06-24 13:06:29 -07001380 public void setController(WebViewController ctl) {
1381 mWebViewController = ctl;
1382 }
1383
Michael Kolbc831b632011-05-11 09:30:34 -07001384 public void setId(long id) {
1385 mId = id;
1386 }
1387
1388 public long getId() {
1389 return mId;
1390 }
1391
Grace Kloba22ac16e2009-10-07 18:00:23 -07001392 /**
1393 * Sets the WebView for this tab, correctly removing the old WebView from
1394 * the container view.
1395 */
1396 void setWebView(WebView w) {
1397 if (mMainView == w) {
1398 return;
1399 }
Michael Kolba713ec82010-11-29 17:27:06 -08001400
Grace Kloba22ac16e2009-10-07 18:00:23 -07001401 // If the WebView is changing, the page will be reloaded, so any ongoing
1402 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001403 if (mGeolocationPermissionsPrompt != null) {
1404 mGeolocationPermissionsPrompt.hide();
1405 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001406
Michael Kolba713ec82010-11-29 17:27:06 -08001407 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001408
1409 // set the new one
1410 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001411 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001412 if (mMainView != null) {
1413 mMainView.setWebViewClient(mWebViewClient);
1414 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001415 // Attach DownloadManager so that downloads can start in an active
1416 // or a non-active window. This can happen when going to a site that
1417 // does a redirect after a period of time. The user could have
1418 // switched to another tab while waiting for the download to start.
1419 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001420 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001421 }
1422 }
1423
1424 /**
1425 * Destroy the tab's main WebView and subWindow if any
1426 */
1427 void destroy() {
1428 if (mMainView != null) {
1429 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001430 // save the WebView to call destroy() after detach it from the tab
1431 WebView webView = mMainView;
1432 setWebView(null);
1433 webView.destroy();
1434 }
1435 }
1436
1437 /**
1438 * Remove the tab from the parent
1439 */
1440 void removeFromTree() {
1441 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001442 if (mChildren != null) {
1443 for(Tab t : mChildren) {
1444 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001445 }
1446 }
1447 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001448 if (mParent != null) {
1449 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001450 }
1451 }
1452
1453 /**
1454 * Create a new subwindow unless a subwindow already exists.
1455 * @return True if a new subwindow was created. False if one already exists.
1456 */
1457 boolean createSubWindow() {
1458 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001459 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001460 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001461 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001462 mSubView.setWebChromeClient(new SubWindowChromeClient(
1463 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001464 // Set a different DownloadListener for the mSubView, since it will
1465 // just need to dismiss the mSubView, rather than close the Tab
1466 mSubView.setDownloadListener(new DownloadListener() {
1467 public void onDownloadStart(String url, String userAgent,
1468 String contentDisposition, String mimetype,
1469 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001470 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001471 contentDisposition, mimetype, contentLength);
1472 if (mSubView.copyBackForwardList().getSize() == 0) {
1473 // This subwindow was opened for the sole purpose of
1474 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001475 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001476 }
1477 }
1478 });
Michael Kolb14612442011-06-24 13:06:29 -07001479 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001480 return true;
1481 }
1482 return false;
1483 }
1484
1485 /**
1486 * Dismiss the subWindow for the tab.
1487 */
1488 void dismissSubWindow() {
1489 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001490 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001491 mSubView.destroy();
1492 mSubView = null;
1493 mSubViewContainer = null;
1494 }
1495 }
1496
Grace Kloba22ac16e2009-10-07 18:00:23 -07001497
1498 /**
1499 * Set the parent tab of this tab.
1500 */
Michael Kolbc831b632011-05-11 09:30:34 -07001501 void setParent(Tab parent) {
1502 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001503 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001504 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001505 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001506 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001507 if (mSavedState != null) {
1508 if (parent == null) {
1509 mSavedState.remove(PARENTTAB);
1510 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001511 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001512 }
1513 }
John Reckb0a86db2011-05-24 14:05:58 -07001514
1515 // Sync the WebView useragent with the parent
1516 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1517 != mSettings.hasDesktopUseragent(getWebView())) {
1518 mSettings.toggleDesktopUseragent(getWebView());
1519 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001520 }
1521
1522 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001523 * If this Tab was created through another Tab, then this method returns
1524 * that Tab.
1525 * @return the Tab parent or null
1526 */
1527 public Tab getParent() {
1528 return mParent;
1529 }
1530
1531 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001532 * When a Tab is created through the content of another Tab, then we
1533 * associate the Tabs.
1534 * @param child the Tab that was created from this Tab
1535 */
1536 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001537 if (mChildren == null) {
1538 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001539 }
Michael Kolbc831b632011-05-11 09:30:34 -07001540 mChildren.add(child);
1541 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001542 }
1543
Michael Kolbc831b632011-05-11 09:30:34 -07001544 Vector<Tab> getChildren() {
1545 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001546 }
1547
1548 void resume() {
1549 if (mMainView != null) {
1550 mMainView.onResume();
1551 if (mSubView != null) {
1552 mSubView.onResume();
1553 }
1554 }
1555 }
1556
1557 void pause() {
1558 if (mMainView != null) {
1559 mMainView.onPause();
1560 if (mSubView != null) {
1561 mSubView.onPause();
1562 }
1563 }
1564 }
1565
1566 void putInForeground() {
1567 mInForeground = true;
1568 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001569 Activity activity = mWebViewController.getActivity();
1570 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001571 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001572 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001573 }
1574 // Show the pending error dialog if the queue is not empty
1575 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1576 showError(mQueuedErrors.getFirst());
1577 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001578 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001579 }
1580
1581 void putInBackground() {
1582 mInForeground = false;
1583 pause();
1584 mMainView.setOnCreateContextMenuListener(null);
1585 if (mSubView != null) {
1586 mSubView.setOnCreateContextMenuListener(null);
1587 }
1588 }
1589
Michael Kolb8233fac2010-10-26 16:08:53 -07001590 boolean inForeground() {
1591 return mInForeground;
1592 }
1593
Grace Kloba22ac16e2009-10-07 18:00:23 -07001594 /**
1595 * Return the top window of this tab; either the subwindow if it is not
1596 * null or the main window.
1597 * @return The top window of this tab.
1598 */
1599 WebView getTopWindow() {
1600 if (mSubView != null) {
1601 return mSubView;
1602 }
1603 return mMainView;
1604 }
1605
1606 /**
1607 * Return the main window of this tab. Note: if a tab is freed in the
1608 * background, this can return null. It is only guaranteed to be
1609 * non-null for the current tab.
1610 * @return The main WebView of this tab.
1611 */
1612 WebView getWebView() {
1613 return mMainView;
1614 }
1615
Michael Kolba713ec82010-11-29 17:27:06 -08001616 void setViewContainer(View container) {
1617 mContainer = container;
1618 }
1619
Michael Kolb8233fac2010-10-26 16:08:53 -07001620 View getViewContainer() {
1621 return mContainer;
1622 }
1623
Grace Kloba22ac16e2009-10-07 18:00:23 -07001624 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001625 * Return whether private browsing is enabled for the main window of
1626 * this tab.
1627 * @return True if private browsing is enabled.
1628 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001629 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001630 WebView webView = getWebView();
1631 if (webView == null) {
1632 return false;
1633 }
1634 return webView.isPrivateBrowsingEnabled();
1635 }
1636
1637 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001638 * Return the subwindow of this tab or null if there is no subwindow.
1639 * @return The subwindow of this tab or null.
1640 */
1641 WebView getSubWebView() {
1642 return mSubView;
1643 }
1644
Michael Kolb1514bb72010-11-22 09:11:48 -08001645 void setSubWebView(WebView subView) {
1646 mSubView = subView;
1647 }
1648
Michael Kolb8233fac2010-10-26 16:08:53 -07001649 View getSubViewContainer() {
1650 return mSubViewContainer;
1651 }
1652
Michael Kolb1514bb72010-11-22 09:11:48 -08001653 void setSubViewContainer(View subViewContainer) {
1654 mSubViewContainer = subViewContainer;
1655 }
1656
Grace Kloba22ac16e2009-10-07 18:00:23 -07001657 /**
1658 * @return The geolocation permissions prompt for this tab.
1659 */
1660 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001661 if (mGeolocationPermissionsPrompt == null) {
1662 ViewStub stub = (ViewStub) mContainer
1663 .findViewById(R.id.geolocation_permissions_prompt);
1664 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1665 .inflate();
1666 mGeolocationPermissionsPrompt.init();
1667 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001668 return mGeolocationPermissionsPrompt;
1669 }
1670
1671 /**
1672 * @return The application id string
1673 */
1674 String getAppId() {
1675 return mAppId;
1676 }
1677
1678 /**
1679 * Set the application id string
1680 * @param id
1681 */
1682 void setAppId(String id) {
1683 mAppId = id;
1684 }
1685
Grace Kloba22ac16e2009-10-07 18:00:23 -07001686 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001687 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001688 }
1689
John Reck49a603c2011-03-03 09:33:05 -08001690 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001691 if (mCurrentState.mOriginalUrl == null) {
1692 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001693 }
John Reckdb22ec42011-06-29 11:31:24 -07001694 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001695 }
1696
Grace Kloba22ac16e2009-10-07 18:00:23 -07001697 /**
John Reck30c714c2010-12-16 17:30:34 -08001698 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001699 */
1700 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001701 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001702 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001703 }
John Reck30c714c2010-12-16 17:30:34 -08001704 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001705 }
1706
1707 /**
John Reck30c714c2010-12-16 17:30:34 -08001708 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001709 */
1710 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001711 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001712 }
1713
John Recke969cc52010-12-21 17:24:43 -08001714 public boolean isBookmarkedSite() {
1715 return mCurrentState.mIsBookmarkedSite;
1716 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001717
Grace Kloba22ac16e2009-10-07 18:00:23 -07001718 /**
1719 * Return the tab's error console. Creates the console if createIfNEcessary
1720 * is true and we haven't already created the console.
1721 * @param createIfNecessary Flag to indicate if the console should be
1722 * created if it has not been already.
1723 * @return The tab's error console, or null if one has not been created and
1724 * createIfNecessary is false.
1725 */
1726 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1727 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001728 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001729 mErrorConsole.setWebView(mMainView);
1730 }
1731 return mErrorConsole;
1732 }
1733
John Reck30c714c2010-12-16 17:30:34 -08001734 private void setLockIconType(LockIcon icon) {
1735 mCurrentState.mLockIcon = icon;
1736 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001737 }
1738
1739 /**
1740 * @return The tab's lock icon type.
1741 */
John Reck30c714c2010-12-16 17:30:34 -08001742 LockIcon getLockIconType() {
1743 return mCurrentState.mLockIcon;
1744 }
1745
1746 int getLoadProgress() {
1747 if (mInPageLoad) {
1748 return mPageLoadProgress;
1749 }
1750 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 }
1752
1753 /**
1754 * @return TRUE if onPageStarted is called while onPageFinished is not
1755 * called yet.
1756 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001757 boolean inPageLoad() {
1758 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001759 }
1760
1761 // force mInLoad to be false. This should only be called before closing the
1762 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001763 void clearInPageLoad() {
1764 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001765 }
1766
Grace Kloba22ac16e2009-10-07 18:00:23 -07001767 /**
John Reck30c714c2010-12-16 17:30:34 -08001768 * Get the cached saved state bundle.
1769 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001770 */
1771 Bundle getSavedState() {
1772 return mSavedState;
1773 }
1774
John Reckaed9c542011-05-27 16:08:53 -07001775 Bundle getSavedState(boolean saveImages) {
1776 if (saveImages && mScreenshot != null) {
1777 Bundle b = new Bundle(mSavedState);
1778 b.putParcelable(SCREENSHOT, mScreenshot);
1779 return b;
1780 }
1781 return mSavedState;
1782 }
1783
Grace Kloba22ac16e2009-10-07 18:00:23 -07001784 /**
1785 * Set the saved state.
1786 */
1787 void setSavedState(Bundle state) {
1788 mSavedState = state;
1789 }
1790
1791 /**
1792 * @return TRUE if succeed in saving the state.
1793 */
1794 boolean saveState() {
1795 // If the WebView is null it means we ran low on memory and we already
1796 // stored the saved state in mSavedState.
1797 if (mMainView == null) {
1798 return mSavedState != null;
1799 }
John Reck24f18262011-06-17 14:47:20 -07001800 // If the tab is the homepage or has no URL, don't save it
1801 String homepage = BrowserSettings.getInstance().getHomePage();
1802 if (TextUtils.equals(homepage, mCurrentState.mUrl)
1803 || TextUtils.isEmpty(mCurrentState.mUrl)) {
1804 return false;
1805 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001806
1807 mSavedState = new Bundle();
John Reck541f55a2011-06-07 16:34:43 -07001808 mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001809
Michael Kolbc831b632011-05-11 09:30:34 -07001810 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001811 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1812 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001813 if (mAppId != null) {
1814 mSavedState.putString(APPID, mAppId);
1815 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001816 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001817 if (mParent != null) {
1818 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001819 }
John Reckb0a86db2011-05-24 14:05:58 -07001820 mSavedState.putBoolean(USERAGENT,
1821 mSettings.hasDesktopUseragent(getWebView()));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001822 return true;
1823 }
1824
1825 /*
1826 * Restore the state of the tab.
1827 */
1828 boolean restoreState(Bundle b) {
1829 if (b == null) {
1830 return false;
1831 }
1832 // Restore the internal state even if the WebView fails to restore.
1833 // This will maintain the app id, original url and close-on-exit values.
1834 mSavedState = null;
Michael Kolbc831b632011-05-11 09:30:34 -07001835 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001836 mAppId = b.getString(APPID);
Michael Kolbeb95db42011-03-03 10:38:40 -08001837 mScreenshot = b.getParcelable(SCREENSHOT);
John Reckb0a86db2011-05-24 14:05:58 -07001838 if (b.getBoolean(USERAGENT)
1839 != mSettings.hasDesktopUseragent(getWebView())) {
1840 mSettings.toggleDesktopUseragent(getWebView());
1841 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001842
1843 final WebBackForwardList list = mMainView.restoreState(b);
1844 if (list == null) {
1845 return false;
1846 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001847 return true;
1848 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001849
Leon Scroggins1961ed22010-12-07 15:22:21 -05001850 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001851 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001852 }
1853
John Recke969cc52010-12-21 17:24:43 -08001854 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1855 = new DataController.OnQueryUrlIsBookmark() {
1856 @Override
1857 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1858 if (mCurrentState.mUrl.equals(url)) {
1859 mCurrentState.mIsBookmarkedSite = isBookmark;
1860 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1861 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001862 }
John Recke969cc52010-12-21 17:24:43 -08001863 };
Michael Kolb1acef692011-03-08 14:12:06 -08001864
Michael Kolbeb95db42011-03-03 10:38:40 -08001865 public void setScreenshot(Bitmap screenshot) {
1866 mScreenshot = screenshot;
1867 }
1868
1869 public Bitmap getScreenshot() {
1870 return mScreenshot;
1871 }
1872
John Reck541f55a2011-06-07 16:34:43 -07001873 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001874 return false;
1875 }
1876
John Reckd8c74522011-06-14 08:45:00 -07001877 public ContentValues createSnapshotValues() {
1878 if (mMainView == null) return null;
1879 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1880 if (!mMainView.saveViewState(stream)) {
John Reck541f55a2011-06-07 16:34:43 -07001881 return null;
1882 }
John Reckd8c74522011-06-14 08:45:00 -07001883 byte[] data = stream.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07001884 ContentValues values = new ContentValues();
1885 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1886 values.put(Snapshots.URL, mCurrentState.mUrl);
1887 values.put(Snapshots.VIEWSTATE, data);
1888 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
1889 return values;
John Reck541f55a2011-06-07 16:34:43 -07001890 }
1891
John Reck26b18322011-06-21 13:08:58 -07001892 public void loadUrl(String url, Map<String, String> headers) {
1893 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001894 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001895 mWebViewController.onPageStarted(this, mMainView, null);
1896 mMainView.loadUrl(url, headers);
1897 }
1898 }
1899
Grace Kloba22ac16e2009-10-07 18:00:23 -07001900}