blob: 62b574df4857b31405bf1466b3c4ee9ccda61175 [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 Recka1696282011-07-08 14:10:37 -0700558 if (!mInPageLoad) {
559 // In page navigation links (www.something.com#footer) will
560 // trigger an onPageFinished which we don't care about.
561 return;
562 }
John Reck5b691842010-11-29 11:21:13 -0800563 if (!isPrivateBrowsingEnabled()) {
564 LogTag.logPageFinishedLoading(
565 url, SystemClock.uptimeMillis() - mLoadStartTime);
566 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700567 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800568 // Sync state (in case of stop/timeout)
569 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800570 if (mCurrentState.mUrl == null) {
571 mCurrentState.mUrl = url != null ? url : "";
572 }
John Reckdb22ec42011-06-29 11:31:24 -0700573 mCurrentState.mOriginalUrl = view.getOriginalUrl();
John Reck30c714c2010-12-16 17:30:34 -0800574 mCurrentState.mTitle = view.getTitle();
575 mCurrentState.mFavicon = view.getFavicon();
576 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
577 // In case we stop when loading an HTTPS page from an HTTP page
578 // but before a provisional load occurred
579 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
580 }
John Reck324d4402011-01-11 16:56:42 -0800581 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700582 }
583
584 // return true if want to hijack the url to let another app to handle it
585 @Override
586 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400587 if (voiceSearchSourceIsGoogle()) {
588 // This method is called when the user clicks on a link.
589 // VoiceSearchMode is turned off when the user leaves the
590 // Google results page, so at this point the user must be on
591 // that page. If the user clicked a link on that page, assume
592 // that the voice search was effective, and broadcast an Intent
593 // so a receiver can take note of that fact.
594 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
595 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
596 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700597 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400598 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700599 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800600 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
601 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700602 } else {
603 return false;
604 }
605 }
606
607 /**
608 * Updates the lock icon. This method is called when we discover another
609 * resource to be loaded for this page (for example, javascript). While
610 * we update the icon type, we do not update the lock icon itself until
611 * we are done loading, it is slightly more secure this way.
612 */
613 @Override
614 public void onLoadResource(WebView view, String url) {
615 if (url != null && url.length() > 0) {
616 // It is only if the page claims to be secure that we may have
617 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800618 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700619 // If NOT a 'safe' url, change the lock to mixed content!
620 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
621 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800622 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700623 }
624 }
625 }
626 }
627
628 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700629 * Show a dialog informing the user of the network error reported by
630 * WebCore if it is in the foreground.
631 */
632 @Override
633 public void onReceivedError(WebView view, int errorCode,
634 String description, String failingUrl) {
635 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
636 errorCode != WebViewClient.ERROR_CONNECT &&
637 errorCode != WebViewClient.ERROR_BAD_URL &&
638 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
639 errorCode != WebViewClient.ERROR_FILE) {
640 queueError(errorCode, description);
641 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500642
643 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700644 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500645 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
646 + " " + description);
647 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700648 }
649
650 /**
651 * Check with the user if it is ok to resend POST data as the page they
652 * are trying to navigate to is the result of a POST.
653 */
654 @Override
655 public void onFormResubmission(WebView view, final Message dontResend,
656 final Message resend) {
657 if (!mInForeground) {
658 dontResend.sendToTarget();
659 return;
660 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500661 if (mDontResend != null) {
662 Log.w(LOGTAG, "onFormResubmission should not be called again "
663 + "while dialog is still up");
664 dontResend.sendToTarget();
665 return;
666 }
667 mDontResend = dontResend;
668 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700669 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700670 R.string.browserFrameFormResubmitLabel).setMessage(
671 R.string.browserFrameFormResubmitMessage)
672 .setPositiveButton(R.string.ok,
673 new DialogInterface.OnClickListener() {
674 public void onClick(DialogInterface dialog,
675 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500676 if (mResend != null) {
677 mResend.sendToTarget();
678 mResend = null;
679 mDontResend = null;
680 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700681 }
682 }).setNegativeButton(R.string.cancel,
683 new DialogInterface.OnClickListener() {
684 public void onClick(DialogInterface dialog,
685 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500686 if (mDontResend != null) {
687 mDontResend.sendToTarget();
688 mResend = null;
689 mDontResend = null;
690 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700691 }
692 }).setOnCancelListener(new OnCancelListener() {
693 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500694 if (mDontResend != null) {
695 mDontResend.sendToTarget();
696 mResend = null;
697 mDontResend = null;
698 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700699 }
700 }).show();
701 }
702
703 /**
704 * Insert the url into the visited history database.
705 * @param url The url to be inserted.
706 * @param isReload True if this url is being reloaded.
707 * FIXME: Not sure what to do when reloading the page.
708 */
709 @Override
710 public void doUpdateVisitedHistory(WebView view, String url,
711 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800712 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700713 }
714
715 /**
716 * Displays SSL error(s) dialog to the user.
717 */
718 @Override
719 public void onReceivedSslError(final WebView view,
720 final SslErrorHandler handler, final SslError error) {
721 if (!mInForeground) {
722 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800723 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700724 return;
725 }
John Reck35e9dd62011-04-25 09:01:54 -0700726 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700727 final LayoutInflater factory =
Michael Kolb14612442011-06-24 13:06:29 -0700728 LayoutInflater.from(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700729 final View warningsView =
730 factory.inflate(R.layout.ssl_warnings, null);
731 final LinearLayout placeholder =
732 (LinearLayout)warningsView.findViewById(R.id.placeholder);
733
734 if (error.hasError(SslError.SSL_UNTRUSTED)) {
735 LinearLayout ll = (LinearLayout)factory
736 .inflate(R.layout.ssl_warning, null);
737 ((TextView)ll.findViewById(R.id.warning))
738 .setText(R.string.ssl_untrusted);
739 placeholder.addView(ll);
740 }
741
742 if (error.hasError(SslError.SSL_IDMISMATCH)) {
743 LinearLayout ll = (LinearLayout)factory
744 .inflate(R.layout.ssl_warning, null);
745 ((TextView)ll.findViewById(R.id.warning))
746 .setText(R.string.ssl_mismatch);
747 placeholder.addView(ll);
748 }
749
750 if (error.hasError(SslError.SSL_EXPIRED)) {
751 LinearLayout ll = (LinearLayout)factory
752 .inflate(R.layout.ssl_warning, null);
753 ((TextView)ll.findViewById(R.id.warning))
754 .setText(R.string.ssl_expired);
755 placeholder.addView(ll);
756 }
757
758 if (error.hasError(SslError.SSL_NOTYETVALID)) {
759 LinearLayout ll = (LinearLayout)factory
760 .inflate(R.layout.ssl_warning, null);
761 ((TextView)ll.findViewById(R.id.warning))
762 .setText(R.string.ssl_not_yet_valid);
763 placeholder.addView(ll);
764 }
765
Michael Kolb14612442011-06-24 13:06:29 -0700766 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700767 R.string.security_warning).setIcon(
768 android.R.drawable.ic_dialog_alert).setView(
769 warningsView).setPositiveButton(R.string.ssl_continue,
770 new DialogInterface.OnClickListener() {
771 public void onClick(DialogInterface dialog,
772 int whichButton) {
773 handler.proceed();
774 }
775 }).setNeutralButton(R.string.view_certificate,
776 new DialogInterface.OnClickListener() {
777 public void onClick(DialogInterface dialog,
778 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700779 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700780 handler, error);
781 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000782 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700783 new DialogInterface.OnClickListener() {
784 public void onClick(DialogInterface dialog,
785 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800786 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700787 }
788 }).setOnCancelListener(
789 new DialogInterface.OnCancelListener() {
790 public void onCancel(DialogInterface dialog) {
791 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800792 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
793 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700794 }
795 }).show();
796 } else {
797 handler.proceed();
798 }
799 }
800
801 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700802 * Displays client certificate request to the user.
803 */
804 @Override
805 public void onReceivedClientCertRequest(final WebView view,
806 final ClientCertRequestHandler handler, final String host_and_port) {
807 if (!mInForeground) {
808 handler.ignore();
809 return;
810 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700811 int colon = host_and_port.lastIndexOf(':');
812 String host;
813 int port;
814 if (colon == -1) {
815 host = host_and_port;
816 port = -1;
817 } else {
818 String portString = host_and_port.substring(colon + 1);
819 try {
820 port = Integer.parseInt(portString);
821 host = host_and_port.substring(0, colon);
822 } catch (NumberFormatException e) {
823 host = host_and_port;
824 port = -1;
825 }
826 }
Michael Kolb14612442011-06-24 13:06:29 -0700827 KeyChain.choosePrivateKeyAlias(
828 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700829 @Override public void alias(String alias) {
830 if (alias == null) {
831 handler.cancel();
832 return;
833 }
Michael Kolb14612442011-06-24 13:06:29 -0700834 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700835 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700836 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700837 }
838
839 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700840 * Handles an HTTP authentication request.
841 *
842 * @param handler The authentication handler
843 * @param host The host
844 * @param realm The realm
845 */
846 @Override
847 public void onReceivedHttpAuthRequest(WebView view,
848 final HttpAuthHandler handler, final String host,
849 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700850 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700851 }
852
853 @Override
John Reck438bf462011-01-12 18:11:46 -0800854 public WebResourceResponse shouldInterceptRequest(WebView view,
855 String url) {
856 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700857 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800858 return res;
859 }
860
861 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700862 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
863 if (!mInForeground) {
864 return false;
865 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700866 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700867 }
868
869 @Override
870 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700871 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700872 return;
873 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700874 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700875 }
Patrick Scott92066772011-03-10 08:46:27 -0500876
877 @Override
878 public void onReceivedLoginRequest(WebView view, String realm,
879 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700880 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500881 .handleLogin(realm, account, args);
882 }
883
Grace Kloba22ac16e2009-10-07 18:00:23 -0700884 };
885
Patrick Scott92066772011-03-10 08:46:27 -0500886 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
887 // displayed.
888 void setDeviceAccountLogin(DeviceAccountLogin login) {
889 mDeviceAccountLogin = login;
890 }
891
892 // Returns non-null if the title bar should display the auto-login UI.
893 DeviceAccountLogin getDeviceAccountLogin() {
894 return mDeviceAccountLogin;
895 }
896
Grace Kloba22ac16e2009-10-07 18:00:23 -0700897 // -------------------------------------------------------------------------
898 // WebChromeClient implementation for the main WebView
899 // -------------------------------------------------------------------------
900
901 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
902 // Helper method to create a new tab or sub window.
903 private void createWindow(final boolean dialog, final Message msg) {
904 WebView.WebViewTransport transport =
905 (WebView.WebViewTransport) msg.obj;
906 if (dialog) {
907 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700908 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700909 transport.setWebView(mSubView);
910 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700911 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700912 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700913 transport.setWebView(newTab.getWebView());
914 }
915 msg.sendToTarget();
916 }
917
918 @Override
919 public boolean onCreateWindow(WebView view, final boolean dialog,
920 final boolean userGesture, final Message resultMsg) {
921 // only allow new window or sub window for the foreground case
922 if (!mInForeground) {
923 return false;
924 }
925 // Short-circuit if we can't create any more tabs or sub windows.
926 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700927 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700928 .setTitle(R.string.too_many_subwindows_dialog_title)
929 .setIcon(android.R.drawable.ic_dialog_alert)
930 .setMessage(R.string.too_many_subwindows_dialog_message)
931 .setPositiveButton(R.string.ok, null)
932 .show();
933 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700934 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700935 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700936 .setTitle(R.string.too_many_windows_dialog_title)
937 .setIcon(android.R.drawable.ic_dialog_alert)
938 .setMessage(R.string.too_many_windows_dialog_message)
939 .setPositiveButton(R.string.ok, null)
940 .show();
941 return false;
942 }
943
944 // Short-circuit if this was a user gesture.
945 if (userGesture) {
946 createWindow(dialog, resultMsg);
947 return true;
948 }
949
950 // Allow the popup and create the appropriate window.
951 final AlertDialog.OnClickListener allowListener =
952 new AlertDialog.OnClickListener() {
953 public void onClick(DialogInterface d,
954 int which) {
955 createWindow(dialog, resultMsg);
956 }
957 };
958
959 // Block the popup by returning a null WebView.
960 final AlertDialog.OnClickListener blockListener =
961 new AlertDialog.OnClickListener() {
962 public void onClick(DialogInterface d, int which) {
963 resultMsg.sendToTarget();
964 }
965 };
966
967 // Build a confirmation dialog to display to the user.
968 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700969 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700970 .setTitle(R.string.attention)
971 .setIcon(android.R.drawable.ic_dialog_alert)
972 .setMessage(R.string.popup_window_attempt)
973 .setPositiveButton(R.string.allow, allowListener)
974 .setNegativeButton(R.string.block, blockListener)
975 .setCancelable(false)
976 .create();
977
978 // Show the confirmation dialog.
979 d.show();
980 return true;
981 }
982
983 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500984 public void onRequestFocus(WebView view) {
985 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700986 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500987 }
988 }
989
990 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700991 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700992 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700993 // JavaScript can only close popup window.
994 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700995 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700996 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700997 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700998 }
999 }
1000
1001 @Override
1002 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001003 mPageLoadProgress = newProgress;
1004 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001005 }
1006
1007 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001008 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001009 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001010 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001011 }
1012
1013 @Override
1014 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001015 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001016 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001017 }
1018
1019 @Override
1020 public void onReceivedTouchIconUrl(WebView view, String url,
1021 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001022 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001023 // Let precomposed icons take precedence over non-composed
1024 // icons.
1025 if (precomposed && mTouchIconLoader != null) {
1026 mTouchIconLoader.cancel(false);
1027 mTouchIconLoader = null;
1028 }
1029 // Have only one async task at a time.
1030 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001031 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001032 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001033 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001034 }
1035 }
1036
1037 @Override
1038 public void onShowCustomView(View view,
1039 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001040 Activity activity = mWebViewController.getActivity();
1041 if (activity != null) {
1042 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1043 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001044 }
1045
1046 @Override
1047 public void onShowCustomView(View view, int requestedOrientation,
1048 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001049 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001050 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001051 }
1052
1053 @Override
1054 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001055 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001056 }
1057
1058 /**
1059 * The origin has exceeded its database quota.
1060 * @param url the URL that exceeded the quota
1061 * @param databaseIdentifier the identifier of the database on which the
1062 * transaction that caused the quota overflow was run
1063 * @param currentQuota the current quota for the origin.
1064 * @param estimatedSize the estimated size of the database.
1065 * @param totalUsedQuota is the sum of all origins' quota.
1066 * @param quotaUpdater The callback to run when a decision to allow or
1067 * deny quota has been made. Don't forget to call this!
1068 */
1069 @Override
1070 public void onExceededDatabaseQuota(String url,
1071 String databaseIdentifier, long currentQuota, long estimatedSize,
1072 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001073 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001074 .onExceededDatabaseQuota(url, databaseIdentifier,
1075 currentQuota, estimatedSize, totalUsedQuota,
1076 quotaUpdater);
1077 }
1078
1079 /**
1080 * The Application Cache has exceeded its max size.
1081 * @param spaceNeeded is the amount of disk space that would be needed
1082 * in order for the last appcache operation to succeed.
1083 * @param totalUsedQuota is the sum of all origins' quota.
1084 * @param quotaUpdater A callback to inform the WebCore thread that a
1085 * new app cache size is available. This callback must always
1086 * be executed at some point to ensure that the sleeping
1087 * WebCore thread is woken up.
1088 */
1089 @Override
1090 public void onReachedMaxAppCacheSize(long spaceNeeded,
1091 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001092 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001093 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1094 quotaUpdater);
1095 }
1096
1097 /**
1098 * Instructs the browser to show a prompt to ask the user to set the
1099 * Geolocation permission state for the specified origin.
1100 * @param origin The origin for which Geolocation permissions are
1101 * requested.
1102 * @param callback The callback to call once the user has set the
1103 * Geolocation permission state.
1104 */
1105 @Override
1106 public void onGeolocationPermissionsShowPrompt(String origin,
1107 GeolocationPermissions.Callback callback) {
1108 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001109 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001110 }
1111 }
1112
1113 /**
1114 * Instructs the browser to hide the Geolocation permissions prompt.
1115 */
1116 @Override
1117 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001118 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001119 mGeolocationPermissionsPrompt.hide();
1120 }
1121 }
1122
Ben Murdoch65acc352009-11-19 18:16:04 +00001123 /* Adds a JavaScript error message to the system log and if the JS
1124 * console is enabled in the about:debug options, to that console
1125 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001126 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001127 */
1128 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001129 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001130 if (mInForeground) {
1131 // call getErrorConsole(true) so it will create one if needed
1132 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001133 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001134 if (mWebViewController.shouldShowErrorConsole()
1135 && errorConsole.getShowState() !=
1136 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001137 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1138 }
1139 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001140
Jeff Hamilton47654f42010-09-07 09:57:51 -05001141 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001142 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001143
Ben Murdochc42addf2010-01-28 15:19:59 +00001144 String message = "Console: " + consoleMessage.message() + " "
1145 + consoleMessage.sourceId() + ":"
1146 + consoleMessage.lineNumber();
1147
1148 switch (consoleMessage.messageLevel()) {
1149 case TIP:
1150 Log.v(CONSOLE_LOGTAG, message);
1151 break;
1152 case LOG:
1153 Log.i(CONSOLE_LOGTAG, message);
1154 break;
1155 case WARNING:
1156 Log.w(CONSOLE_LOGTAG, message);
1157 break;
1158 case ERROR:
1159 Log.e(CONSOLE_LOGTAG, message);
1160 break;
1161 case DEBUG:
1162 Log.d(CONSOLE_LOGTAG, message);
1163 break;
1164 }
1165
1166 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001167 }
1168
1169 /**
1170 * Ask the browser for an icon to represent a <video> element.
1171 * This icon will be used if the Web page did not specify a poster attribute.
1172 * @return Bitmap The icon or null if no such icon is available.
1173 */
1174 @Override
1175 public Bitmap getDefaultVideoPoster() {
1176 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001177 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001178 }
1179 return null;
1180 }
1181
1182 /**
1183 * Ask the host application for a custom progress view to show while
1184 * a <video> is loading.
1185 * @return View The progress view.
1186 */
1187 @Override
1188 public View getVideoLoadingProgressView() {
1189 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001190 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001191 }
1192 return null;
1193 }
1194
1195 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001196 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001197 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001198 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001199 } else {
1200 uploadMsg.onReceiveValue(null);
1201 }
1202 }
1203
1204 /**
1205 * Deliver a list of already-visited URLs
1206 */
1207 @Override
1208 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001209 mWebViewController.getVisitedHistory(callback);
1210 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001211
1212 @Override
1213 public void setupAutoFill(Message message) {
1214 // Prompt the user to set up their profile.
1215 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001216 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1217 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001218 Context.LAYOUT_INFLATER_SERVICE);
1219 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1220
1221 builder.setView(layout)
Ben Murdochb7e6f942011-07-08 13:00:21 +01001222 .setTitle(R.string.autofill_setup_dialog_title)
Ben Murdoch1d676b62011-01-17 12:54:24 +00001223 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1224 @Override
1225 public void onClick(DialogInterface dialog, int id) {
1226 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1227 R.id.setup_autofill_dialog_disable_autofill);
1228
1229 if (disableAutoFill.isChecked()) {
1230 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001231 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001232 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001233 R.string.autofill_setup_dialog_negative_toast,
1234 Toast.LENGTH_LONG).show();
1235 } else {
1236 // Take user to the AutoFill profile editor. When they return,
1237 // we will send the message that we pass here which will trigger
1238 // the form to get filled out with their new profile.
1239 mWebViewController.setupAutoFill(msg);
1240 }
1241 }
1242 })
1243 .setNegativeButton(R.string.cancel, null)
1244 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001245 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001246 };
1247
1248 // -------------------------------------------------------------------------
1249 // WebViewClient implementation for the sub window
1250 // -------------------------------------------------------------------------
1251
1252 // Subclass of WebViewClient used in subwindows to notify the main
1253 // WebViewClient of certain WebView activities.
1254 private static class SubWindowClient extends WebViewClient {
1255 // The main WebViewClient.
1256 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001257 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001258
Michael Kolb8233fac2010-10-26 16:08:53 -07001259 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001260 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001261 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001262 }
1263 @Override
1264 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1265 // Unlike the others, do not call mClient's version, which would
1266 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001267 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001268 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001269 }
1270 @Override
1271 public void doUpdateVisitedHistory(WebView view, String url,
1272 boolean isReload) {
1273 mClient.doUpdateVisitedHistory(view, url, isReload);
1274 }
1275 @Override
1276 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1277 return mClient.shouldOverrideUrlLoading(view, url);
1278 }
1279 @Override
1280 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1281 SslError error) {
1282 mClient.onReceivedSslError(view, handler, error);
1283 }
1284 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001285 public void onReceivedClientCertRequest(WebView view,
1286 ClientCertRequestHandler handler, String host_and_port) {
1287 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1288 }
1289 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001290 public void onReceivedHttpAuthRequest(WebView view,
1291 HttpAuthHandler handler, String host, String realm) {
1292 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1293 }
1294 @Override
1295 public void onFormResubmission(WebView view, Message dontResend,
1296 Message resend) {
1297 mClient.onFormResubmission(view, dontResend, resend);
1298 }
1299 @Override
1300 public void onReceivedError(WebView view, int errorCode,
1301 String description, String failingUrl) {
1302 mClient.onReceivedError(view, errorCode, description, failingUrl);
1303 }
1304 @Override
1305 public boolean shouldOverrideKeyEvent(WebView view,
1306 android.view.KeyEvent event) {
1307 return mClient.shouldOverrideKeyEvent(view, event);
1308 }
1309 @Override
1310 public void onUnhandledKeyEvent(WebView view,
1311 android.view.KeyEvent event) {
1312 mClient.onUnhandledKeyEvent(view, event);
1313 }
1314 }
1315
1316 // -------------------------------------------------------------------------
1317 // WebChromeClient implementation for the sub window
1318 // -------------------------------------------------------------------------
1319
1320 private class SubWindowChromeClient extends WebChromeClient {
1321 // The main WebChromeClient.
1322 private final WebChromeClient mClient;
1323
1324 SubWindowChromeClient(WebChromeClient client) {
1325 mClient = client;
1326 }
1327 @Override
1328 public void onProgressChanged(WebView view, int newProgress) {
1329 mClient.onProgressChanged(view, newProgress);
1330 }
1331 @Override
1332 public boolean onCreateWindow(WebView view, boolean dialog,
1333 boolean userGesture, android.os.Message resultMsg) {
1334 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1335 }
1336 @Override
1337 public void onCloseWindow(WebView window) {
1338 if (window != mSubView) {
1339 Log.e(LOGTAG, "Can't close the window");
1340 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001341 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001342 }
1343 }
1344
1345 // -------------------------------------------------------------------------
1346
Michael Kolb8233fac2010-10-26 16:08:53 -07001347 // TODO temporarily use activity here
1348 // remove later
1349
Grace Kloba22ac16e2009-10-07 18:00:23 -07001350 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001351 Tab(WebViewController wvcontroller, WebView w) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001352 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001353 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001354 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001355 mDataController = DataController.getInstance(mContext);
1356 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001357 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001358 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001359 mInForeground = false;
1360
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001361 mDownloadListener = new DownloadListener() {
1362 public void onDownloadStart(String url, String userAgent,
1363 String contentDisposition, String mimetype,
1364 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001365 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001366 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001367 }
1368 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001369 mWebBackForwardListClient = new WebBackForwardListClient() {
1370 @Override
1371 public void onNewHistoryItem(WebHistoryItem item) {
1372 if (isInVoiceSearchMode()) {
1373 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1374 }
1375 }
1376 @Override
1377 public void onIndexChanged(WebHistoryItem item, int index) {
1378 Object data = item.getCustomData();
1379 if (data != null && data instanceof Intent) {
1380 activateVoiceSearchMode((Intent) data);
1381 }
1382 }
1383 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001384
Grace Kloba22ac16e2009-10-07 18:00:23 -07001385 setWebView(w);
1386 }
1387
Michael Kolb14612442011-06-24 13:06:29 -07001388 public void setController(WebViewController ctl) {
1389 mWebViewController = ctl;
1390 }
1391
Michael Kolbc831b632011-05-11 09:30:34 -07001392 public void setId(long id) {
1393 mId = id;
1394 }
1395
1396 public long getId() {
1397 return mId;
1398 }
1399
Grace Kloba22ac16e2009-10-07 18:00:23 -07001400 /**
1401 * Sets the WebView for this tab, correctly removing the old WebView from
1402 * the container view.
1403 */
1404 void setWebView(WebView w) {
1405 if (mMainView == w) {
1406 return;
1407 }
Michael Kolba713ec82010-11-29 17:27:06 -08001408
Grace Kloba22ac16e2009-10-07 18:00:23 -07001409 // If the WebView is changing, the page will be reloaded, so any ongoing
1410 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001411 if (mGeolocationPermissionsPrompt != null) {
1412 mGeolocationPermissionsPrompt.hide();
1413 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001414
Michael Kolba713ec82010-11-29 17:27:06 -08001415 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001416
1417 // set the new one
1418 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001419 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001420 if (mMainView != null) {
1421 mMainView.setWebViewClient(mWebViewClient);
1422 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001423 // Attach DownloadManager so that downloads can start in an active
1424 // or a non-active window. This can happen when going to a site that
1425 // does a redirect after a period of time. The user could have
1426 // switched to another tab while waiting for the download to start.
1427 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001428 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001429 }
1430 }
1431
1432 /**
1433 * Destroy the tab's main WebView and subWindow if any
1434 */
1435 void destroy() {
1436 if (mMainView != null) {
1437 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001438 // save the WebView to call destroy() after detach it from the tab
1439 WebView webView = mMainView;
1440 setWebView(null);
1441 webView.destroy();
1442 }
1443 }
1444
1445 /**
1446 * Remove the tab from the parent
1447 */
1448 void removeFromTree() {
1449 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001450 if (mChildren != null) {
1451 for(Tab t : mChildren) {
1452 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001453 }
1454 }
1455 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001456 if (mParent != null) {
1457 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001458 }
1459 }
1460
1461 /**
1462 * Create a new subwindow unless a subwindow already exists.
1463 * @return True if a new subwindow was created. False if one already exists.
1464 */
1465 boolean createSubWindow() {
1466 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001467 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001468 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001469 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001470 mSubView.setWebChromeClient(new SubWindowChromeClient(
1471 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001472 // Set a different DownloadListener for the mSubView, since it will
1473 // just need to dismiss the mSubView, rather than close the Tab
1474 mSubView.setDownloadListener(new DownloadListener() {
1475 public void onDownloadStart(String url, String userAgent,
1476 String contentDisposition, String mimetype,
1477 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001478 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001479 contentDisposition, mimetype, contentLength);
1480 if (mSubView.copyBackForwardList().getSize() == 0) {
1481 // This subwindow was opened for the sole purpose of
1482 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001483 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001484 }
1485 }
1486 });
Michael Kolb14612442011-06-24 13:06:29 -07001487 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001488 return true;
1489 }
1490 return false;
1491 }
1492
1493 /**
1494 * Dismiss the subWindow for the tab.
1495 */
1496 void dismissSubWindow() {
1497 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001498 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001499 mSubView.destroy();
1500 mSubView = null;
1501 mSubViewContainer = null;
1502 }
1503 }
1504
Grace Kloba22ac16e2009-10-07 18:00:23 -07001505
1506 /**
1507 * Set the parent tab of this tab.
1508 */
Michael Kolbc831b632011-05-11 09:30:34 -07001509 void setParent(Tab parent) {
1510 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001511 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001512 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001513 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001514 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001515 if (mSavedState != null) {
1516 if (parent == null) {
1517 mSavedState.remove(PARENTTAB);
1518 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001519 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001520 }
1521 }
John Reckb0a86db2011-05-24 14:05:58 -07001522
1523 // Sync the WebView useragent with the parent
1524 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1525 != mSettings.hasDesktopUseragent(getWebView())) {
1526 mSettings.toggleDesktopUseragent(getWebView());
1527 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001528 }
1529
1530 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001531 * If this Tab was created through another Tab, then this method returns
1532 * that Tab.
1533 * @return the Tab parent or null
1534 */
1535 public Tab getParent() {
1536 return mParent;
1537 }
1538
1539 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001540 * When a Tab is created through the content of another Tab, then we
1541 * associate the Tabs.
1542 * @param child the Tab that was created from this Tab
1543 */
1544 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001545 if (mChildren == null) {
1546 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001547 }
Michael Kolbc831b632011-05-11 09:30:34 -07001548 mChildren.add(child);
1549 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001550 }
1551
Michael Kolbc831b632011-05-11 09:30:34 -07001552 Vector<Tab> getChildren() {
1553 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001554 }
1555
1556 void resume() {
1557 if (mMainView != null) {
1558 mMainView.onResume();
1559 if (mSubView != null) {
1560 mSubView.onResume();
1561 }
1562 }
1563 }
1564
1565 void pause() {
1566 if (mMainView != null) {
1567 mMainView.onPause();
1568 if (mSubView != null) {
1569 mSubView.onPause();
1570 }
1571 }
1572 }
1573
1574 void putInForeground() {
1575 mInForeground = true;
1576 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001577 Activity activity = mWebViewController.getActivity();
1578 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001579 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001580 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001581 }
1582 // Show the pending error dialog if the queue is not empty
1583 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1584 showError(mQueuedErrors.getFirst());
1585 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001586 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001587 }
1588
1589 void putInBackground() {
1590 mInForeground = false;
1591 pause();
1592 mMainView.setOnCreateContextMenuListener(null);
1593 if (mSubView != null) {
1594 mSubView.setOnCreateContextMenuListener(null);
1595 }
1596 }
1597
Michael Kolb8233fac2010-10-26 16:08:53 -07001598 boolean inForeground() {
1599 return mInForeground;
1600 }
1601
Grace Kloba22ac16e2009-10-07 18:00:23 -07001602 /**
1603 * Return the top window of this tab; either the subwindow if it is not
1604 * null or the main window.
1605 * @return The top window of this tab.
1606 */
1607 WebView getTopWindow() {
1608 if (mSubView != null) {
1609 return mSubView;
1610 }
1611 return mMainView;
1612 }
1613
1614 /**
1615 * Return the main window of this tab. Note: if a tab is freed in the
1616 * background, this can return null. It is only guaranteed to be
1617 * non-null for the current tab.
1618 * @return The main WebView of this tab.
1619 */
1620 WebView getWebView() {
1621 return mMainView;
1622 }
1623
Michael Kolba713ec82010-11-29 17:27:06 -08001624 void setViewContainer(View container) {
1625 mContainer = container;
1626 }
1627
Michael Kolb8233fac2010-10-26 16:08:53 -07001628 View getViewContainer() {
1629 return mContainer;
1630 }
1631
Grace Kloba22ac16e2009-10-07 18:00:23 -07001632 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001633 * Return whether private browsing is enabled for the main window of
1634 * this tab.
1635 * @return True if private browsing is enabled.
1636 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001637 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001638 WebView webView = getWebView();
1639 if (webView == null) {
1640 return false;
1641 }
1642 return webView.isPrivateBrowsingEnabled();
1643 }
1644
1645 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001646 * Return the subwindow of this tab or null if there is no subwindow.
1647 * @return The subwindow of this tab or null.
1648 */
1649 WebView getSubWebView() {
1650 return mSubView;
1651 }
1652
Michael Kolb1514bb72010-11-22 09:11:48 -08001653 void setSubWebView(WebView subView) {
1654 mSubView = subView;
1655 }
1656
Michael Kolb8233fac2010-10-26 16:08:53 -07001657 View getSubViewContainer() {
1658 return mSubViewContainer;
1659 }
1660
Michael Kolb1514bb72010-11-22 09:11:48 -08001661 void setSubViewContainer(View subViewContainer) {
1662 mSubViewContainer = subViewContainer;
1663 }
1664
Grace Kloba22ac16e2009-10-07 18:00:23 -07001665 /**
1666 * @return The geolocation permissions prompt for this tab.
1667 */
1668 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001669 if (mGeolocationPermissionsPrompt == null) {
1670 ViewStub stub = (ViewStub) mContainer
1671 .findViewById(R.id.geolocation_permissions_prompt);
1672 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1673 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001674 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001675 return mGeolocationPermissionsPrompt;
1676 }
1677
1678 /**
1679 * @return The application id string
1680 */
1681 String getAppId() {
1682 return mAppId;
1683 }
1684
1685 /**
1686 * Set the application id string
1687 * @param id
1688 */
1689 void setAppId(String id) {
1690 mAppId = id;
1691 }
1692
Grace Kloba22ac16e2009-10-07 18:00:23 -07001693 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001694 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001695 }
1696
John Reck49a603c2011-03-03 09:33:05 -08001697 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001698 if (mCurrentState.mOriginalUrl == null) {
1699 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001700 }
John Reckdb22ec42011-06-29 11:31:24 -07001701 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001702 }
1703
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 /**
John Reck30c714c2010-12-16 17:30:34 -08001705 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001706 */
1707 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001708 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001709 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001710 }
John Reck30c714c2010-12-16 17:30:34 -08001711 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001712 }
1713
1714 /**
John Reck30c714c2010-12-16 17:30:34 -08001715 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001716 */
1717 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001718 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001719 }
1720
John Recke969cc52010-12-21 17:24:43 -08001721 public boolean isBookmarkedSite() {
1722 return mCurrentState.mIsBookmarkedSite;
1723 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001724
Grace Kloba22ac16e2009-10-07 18:00:23 -07001725 /**
1726 * Return the tab's error console. Creates the console if createIfNEcessary
1727 * is true and we haven't already created the console.
1728 * @param createIfNecessary Flag to indicate if the console should be
1729 * created if it has not been already.
1730 * @return The tab's error console, or null if one has not been created and
1731 * createIfNecessary is false.
1732 */
1733 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1734 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001735 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001736 mErrorConsole.setWebView(mMainView);
1737 }
1738 return mErrorConsole;
1739 }
1740
John Reck30c714c2010-12-16 17:30:34 -08001741 private void setLockIconType(LockIcon icon) {
1742 mCurrentState.mLockIcon = icon;
1743 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001744 }
1745
1746 /**
1747 * @return The tab's lock icon type.
1748 */
John Reck30c714c2010-12-16 17:30:34 -08001749 LockIcon getLockIconType() {
1750 return mCurrentState.mLockIcon;
1751 }
1752
1753 int getLoadProgress() {
1754 if (mInPageLoad) {
1755 return mPageLoadProgress;
1756 }
1757 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001758 }
1759
1760 /**
1761 * @return TRUE if onPageStarted is called while onPageFinished is not
1762 * called yet.
1763 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001764 boolean inPageLoad() {
1765 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001766 }
1767
1768 // force mInLoad to be false. This should only be called before closing the
1769 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001770 void clearInPageLoad() {
1771 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001772 }
1773
Grace Kloba22ac16e2009-10-07 18:00:23 -07001774 /**
John Reck30c714c2010-12-16 17:30:34 -08001775 * Get the cached saved state bundle.
1776 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001777 */
1778 Bundle getSavedState() {
1779 return mSavedState;
1780 }
1781
John Reckaed9c542011-05-27 16:08:53 -07001782 Bundle getSavedState(boolean saveImages) {
1783 if (saveImages && mScreenshot != null) {
1784 Bundle b = new Bundle(mSavedState);
1785 b.putParcelable(SCREENSHOT, mScreenshot);
1786 return b;
1787 }
1788 return mSavedState;
1789 }
1790
Grace Kloba22ac16e2009-10-07 18:00:23 -07001791 /**
1792 * Set the saved state.
1793 */
1794 void setSavedState(Bundle state) {
1795 mSavedState = state;
1796 }
1797
1798 /**
1799 * @return TRUE if succeed in saving the state.
1800 */
1801 boolean saveState() {
1802 // If the WebView is null it means we ran low on memory and we already
1803 // stored the saved state in mSavedState.
1804 if (mMainView == null) {
1805 return mSavedState != null;
1806 }
John Reck24f18262011-06-17 14:47:20 -07001807 // If the tab is the homepage or has no URL, don't save it
1808 String homepage = BrowserSettings.getInstance().getHomePage();
1809 if (TextUtils.equals(homepage, mCurrentState.mUrl)
1810 || TextUtils.isEmpty(mCurrentState.mUrl)) {
1811 return false;
1812 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001813
1814 mSavedState = new Bundle();
John Reck541f55a2011-06-07 16:34:43 -07001815 mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001816
Michael Kolbc831b632011-05-11 09:30:34 -07001817 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001818 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1819 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001820 if (mAppId != null) {
1821 mSavedState.putString(APPID, mAppId);
1822 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001823 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001824 if (mParent != null) {
1825 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001826 }
John Reckb0a86db2011-05-24 14:05:58 -07001827 mSavedState.putBoolean(USERAGENT,
1828 mSettings.hasDesktopUseragent(getWebView()));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001829 return true;
1830 }
1831
1832 /*
1833 * Restore the state of the tab.
1834 */
1835 boolean restoreState(Bundle b) {
1836 if (b == null) {
1837 return false;
1838 }
1839 // Restore the internal state even if the WebView fails to restore.
1840 // This will maintain the app id, original url and close-on-exit values.
1841 mSavedState = null;
Michael Kolbc831b632011-05-11 09:30:34 -07001842 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001843 mAppId = b.getString(APPID);
Michael Kolbeb95db42011-03-03 10:38:40 -08001844 mScreenshot = b.getParcelable(SCREENSHOT);
John Reckb0a86db2011-05-24 14:05:58 -07001845 if (b.getBoolean(USERAGENT)
1846 != mSettings.hasDesktopUseragent(getWebView())) {
1847 mSettings.toggleDesktopUseragent(getWebView());
1848 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001849
1850 final WebBackForwardList list = mMainView.restoreState(b);
1851 if (list == null) {
1852 return false;
1853 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001854 return true;
1855 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001856
Leon Scroggins1961ed22010-12-07 15:22:21 -05001857 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001858 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001859 }
1860
John Recke969cc52010-12-21 17:24:43 -08001861 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1862 = new DataController.OnQueryUrlIsBookmark() {
1863 @Override
1864 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1865 if (mCurrentState.mUrl.equals(url)) {
1866 mCurrentState.mIsBookmarkedSite = isBookmark;
1867 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1868 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001869 }
John Recke969cc52010-12-21 17:24:43 -08001870 };
Michael Kolb1acef692011-03-08 14:12:06 -08001871
Michael Kolbeb95db42011-03-03 10:38:40 -08001872 public void setScreenshot(Bitmap screenshot) {
1873 mScreenshot = screenshot;
1874 }
1875
1876 public Bitmap getScreenshot() {
1877 return mScreenshot;
1878 }
1879
John Reck541f55a2011-06-07 16:34:43 -07001880 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001881 return false;
1882 }
1883
John Reckd8c74522011-06-14 08:45:00 -07001884 public ContentValues createSnapshotValues() {
1885 if (mMainView == null) return null;
John Reck8cc92352011-07-06 17:41:52 -07001886 ByteArrayOutputStream bos = new ByteArrayOutputStream();
1887 try {
1888 GZIPOutputStream stream = new GZIPOutputStream(bos);
1889 if (!mMainView.saveViewState(stream)) {
1890 return null;
1891 }
1892 stream.flush();
1893 stream.close();
1894 } catch (Exception e) {
1895 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07001896 return null;
1897 }
John Reck8cc92352011-07-06 17:41:52 -07001898 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07001899 ContentValues values = new ContentValues();
1900 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1901 values.put(Snapshots.URL, mCurrentState.mUrl);
1902 values.put(Snapshots.VIEWSTATE, data);
1903 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001904 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1905 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
1906 Bitmap screenshot = Controller.createScreenshot(mMainView,
1907 Controller.getDesiredThumbnailWidth(mContext),
1908 Controller.getDesiredThumbnailHeight(mContext));
1909 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001910 return values;
John Reck541f55a2011-06-07 16:34:43 -07001911 }
1912
John Reck8cc92352011-07-06 17:41:52 -07001913 public byte[] compressBitmap(Bitmap bitmap) {
1914 if (bitmap == null) {
1915 return null;
1916 }
1917 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1918 bitmap.compress(CompressFormat.PNG, 100, stream);
1919 return stream.toByteArray();
1920 }
1921
John Reck26b18322011-06-21 13:08:58 -07001922 public void loadUrl(String url, Map<String, String> headers) {
1923 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001924 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001925 mWebViewController.onPageStarted(this, mMainView, null);
1926 mMainView.loadUrl(url, headers);
1927 }
1928 }
1929
Grace Kloba22ac16e2009-10-07 18:00:23 -07001930}