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