blob: 4089cac8ef2389e37fe81e040c23b2d118b9ce7d [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2007 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
19import android.content.Context;
Patrick Scott2ed6edb2009-04-22 10:07:45 -040020import android.graphics.Picture;
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.net.http.SslError;
22import android.os.Bundle;
23import android.os.Message;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024import android.util.Log;
25import android.view.Gravity;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29import android.view.View.OnClickListener;
30import android.webkit.HttpAuthHandler;
31import android.webkit.JsPromptResult;
32import android.webkit.JsResult;
33import android.webkit.SslErrorHandler;
34import android.webkit.WebBackForwardList;
35import android.webkit.WebChromeClient;
36import android.webkit.WebHistoryItem;
37import android.webkit.WebView;
38import android.webkit.WebViewClient;
39import android.widget.FrameLayout;
40import android.widget.ImageButton;
Steve Block2bc69912009-07-30 14:45:13 +010041import android.widget.LinearLayout;
The Android Open Source Project0c908882009-03-03 19:32:16 -080042
43import java.io.File;
Patrick Scott2ed6edb2009-04-22 10:07:45 -040044import java.io.FileInputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import java.util.ArrayList;
46import java.util.Vector;
47
48class TabControl {
49 // Log Tag
50 private static final String LOGTAG = "TabControl";
51 // Maximum number of tabs.
52 static final int MAX_TABS = 8;
53 // Static instance of an empty callback.
54 private static final WebViewClient mEmptyClient =
55 new WebViewClient();
56 // Instance of BackgroundChromeClient for background tabs.
57 private final BackgroundChromeClient mBackgroundChromeClient =
58 new BackgroundChromeClient();
59 // Private array of WebViews that are used as tabs.
60 private ArrayList<Tab> mTabs = new ArrayList<Tab>(MAX_TABS);
61 // Queue of most recently viewed tabs.
62 private ArrayList<Tab> mTabQueue = new ArrayList<Tab>(MAX_TABS);
63 // Current position in mTabs.
64 private int mCurrentTab = -1;
65 // A private instance of BrowserActivity to interface with when adding and
66 // switching between tabs.
67 private final BrowserActivity mActivity;
68 // Inflation service for making subwindows.
69 private final LayoutInflater mInflateService;
70 // Subclass of WebViewClient used in subwindows to notify the main
71 // WebViewClient of certain WebView activities.
Cary Clarka9771242009-08-11 16:42:26 -040072 private static class SubWindowClient extends WebViewClient {
The Android Open Source Project0c908882009-03-03 19:32:16 -080073 // The main WebViewClient.
74 private final WebViewClient mClient;
75
76 SubWindowClient(WebViewClient client) {
77 mClient = client;
78 }
79 @Override
80 public void doUpdateVisitedHistory(WebView view, String url,
81 boolean isReload) {
82 mClient.doUpdateVisitedHistory(view, url, isReload);
83 }
84 @Override
85 public boolean shouldOverrideUrlLoading(WebView view, String url) {
86 return mClient.shouldOverrideUrlLoading(view, url);
87 }
88 @Override
89 public void onReceivedSslError(WebView view, SslErrorHandler handler,
90 SslError error) {
91 mClient.onReceivedSslError(view, handler, error);
92 }
93 @Override
94 public void onReceivedHttpAuthRequest(WebView view,
95 HttpAuthHandler handler, String host, String realm) {
96 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
97 }
98 @Override
99 public void onFormResubmission(WebView view, Message dontResend,
100 Message resend) {
101 mClient.onFormResubmission(view, dontResend, resend);
102 }
103 @Override
104 public void onReceivedError(WebView view, int errorCode,
105 String description, String failingUrl) {
106 mClient.onReceivedError(view, errorCode, description, failingUrl);
107 }
Patrick Scotte9fc4492009-08-12 09:15:51 -0400108 @Override
109 public boolean shouldOverrideKeyEvent(WebView view,
110 android.view.KeyEvent event) {
111 return mClient.shouldOverrideKeyEvent(view, event);
112 }
113 @Override
114 public void onUnhandledKeyEvent(WebView view,
115 android.view.KeyEvent event) {
116 mClient.onUnhandledKeyEvent(view, event);
117 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118 }
119 // Subclass of WebChromeClient to display javascript dialogs.
120 private class SubWindowChromeClient extends WebChromeClient {
121 // This subwindow's tab.
122 private final Tab mTab;
123 // The main WebChromeClient.
124 private final WebChromeClient mClient;
125
126 SubWindowChromeClient(Tab t, WebChromeClient client) {
127 mTab = t;
128 mClient = client;
129 }
130 @Override
131 public void onProgressChanged(WebView view, int newProgress) {
132 mClient.onProgressChanged(view, newProgress);
133 }
134 @Override
135 public boolean onCreateWindow(WebView view, boolean dialog,
136 boolean userGesture, android.os.Message resultMsg) {
137 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
138 }
139 @Override
140 public void onCloseWindow(WebView window) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700141 if (Browser.DEBUG && window != mTab.mSubView) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800142 throw new AssertionError("Can't close the window");
143 }
144 mActivity.dismissSubWindow(mTab);
145 }
146 }
147 // Background WebChromeClient for focusing tabs
148 private class BackgroundChromeClient extends WebChromeClient {
149 @Override
150 public void onRequestFocus(WebView view) {
151 Tab t = getTabFromView(view);
152 if (t != getCurrentTab()) {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400153 mActivity.switchToTab(getTabIndex(t));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800154 }
155 }
156 }
157
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400158 // Extra saved information for displaying the tab in the picker.
159 public static class PickerData {
160 String mUrl;
161 String mTitle;
162 float mScale;
163 int mScrollX;
164 int mScrollY;
165 int mWidth;
166 Picture mPicture;
167 // This can be null. When a new picture comes in, this view should be
168 // invalidated to show the new picture.
169 FakeWebView mFakeWebView;
170 }
171
The Android Open Source Project0c908882009-03-03 19:32:16 -0800172 /**
173 * Private class for maintaining Tabs with a main WebView and a subwindow.
174 */
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400175 public class Tab implements WebView.PictureListener {
Steve Block2bc69912009-07-30 14:45:13 +0100176 // The Geolocation permissions prompt
177 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
178 private View mContainer;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800179 // Main WebView
180 private WebView mMainView;
181 // Subwindow WebView
182 private WebView mSubView;
183 // Subwindow container
184 private View mSubViewContainer;
185 // Subwindow callback
186 private SubWindowClient mSubViewClient;
187 // Subwindow chrome callback
188 private SubWindowChromeClient mSubViewChromeClient;
189 // Saved bundle for when we are running low on memory. It contains the
190 // information needed to restore the WebView if the user goes back to
191 // the tab.
192 private Bundle mSavedState;
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400193 // Data used when displaying the tab in the picker.
194 private PickerData mPickerData;
195
The Android Open Source Project0c908882009-03-03 19:32:16 -0800196 // Parent Tab. This is the Tab that created this Tab, or null
197 // if the Tab was created by the UI
198 private Tab mParentTab;
199 // Tab that constructed by this Tab. This is used when this
200 // Tab is destroyed, it clears all mParentTab values in the
201 // children.
202 private Vector<Tab> mChildTabs;
203
204 private Boolean mCloseOnExit;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700205 // Application identifier used to find tabs that another application
206 // wants to reuse.
207 private String mAppId;
208 // Keep the original url around to avoid killing the old WebView if the
209 // url has not changed.
210 private String mOriginalUrl;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800211
Ben Murdochbff2d602009-07-01 20:19:05 +0100212 private ErrorConsoleView mErrorConsole;
Grace Klobaeb6eef42009-09-15 17:56:32 -0700213 // the lock icon type and previous lock icon type for the tab
214 private int mSavedLockIconType;
215 private int mSavedPrevLockIconType;
Ben Murdochbff2d602009-07-01 20:19:05 +0100216
The Android Open Source Project0c908882009-03-03 19:32:16 -0800217 // Construct a new tab
Steve Block2bc69912009-07-30 14:45:13 +0100218 private Tab(WebView w, boolean closeOnExit, String appId, String url, Context context) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800219 mCloseOnExit = closeOnExit;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700220 mAppId = appId;
221 mOriginalUrl = url;
Grace Klobaeb6eef42009-09-15 17:56:32 -0700222 mSavedLockIconType = BrowserActivity.LOCK_ICON_UNSECURE;
223 mSavedPrevLockIconType = BrowserActivity.LOCK_ICON_UNSECURE;
Steve Block2bc69912009-07-30 14:45:13 +0100224
225 // The tab consists of a container view, which contains the main
226 // WebView, as well as any other UI elements associated with the tab.
227 //
228 // FIXME: Fix the interaction between this layout and the animation
229 // used when switching to and from the tab picker. This may not be
230 // required if the tab selection UI is redesigned.
231 LayoutInflater factory = LayoutInflater.from(context);
232 mContainer = factory.inflate(R.layout.tab, null);
233
234 mGeolocationPermissionsPrompt =
235 (GeolocationPermissionsPrompt) mContainer.findViewById(
236 R.id.geolocation_permissions_prompt);
237
238 setWebView(w);
239 }
240
241 /**
242 * Sets the WebView for this tab, correctly removing the old WebView
243 * from, and inserting the new WebView into, the container view.
244 */
245 public void setWebView(WebView w) {
246 if (mMainView == w) {
247 return;
248 }
249 // If the WebView is changing, the page will be reloaded, so any ongoing Geolocation
250 // permission requests are void.
251 mGeolocationPermissionsPrompt.hide();
252
253 FrameLayout wrapper = (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
254 if (mMainView != null) {
255 wrapper.removeView(mMainView);
256 }
257 mMainView = w;
258 if (mMainView != null) {
259 wrapper.addView(mMainView);
260 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800261 }
262
263 /**
264 * Return the top window of this tab; either the subwindow if it is not
265 * null or the main window.
266 * @return The top window of this tab.
267 */
268 public WebView getTopWindow() {
269 if (mSubView != null) {
270 return mSubView;
271 }
272 return mMainView;
273 }
274
275 /**
276 * Return the main window of this tab. Note: if a tab is freed in the
277 * background, this can return null. It is only guaranteed to be
278 * non-null for the current tab.
279 * @return The main WebView of this tab.
280 */
281 public WebView getWebView() {
282 return mMainView;
283 }
284
285 /**
Steve Block2bc69912009-07-30 14:45:13 +0100286 * @return The container for this tab.
287 */
288 public View getContainer() {
289 return mContainer;
290 }
291
292 /**
293 * @return The geolocation permissions prompt for this tab.
294 */
295 public GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
296 return mGeolocationPermissionsPrompt;
297 }
298
299 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800300 * Return the subwindow of this tab or null if there is no subwindow.
301 * @return The subwindow of this tab or null.
302 */
303 public WebView getSubWebView() {
304 return mSubView;
305 }
306
307 /**
308 * Return the subwindow container of this tab or null if there is no
309 * subwindow.
310 * @return The subwindow's container View.
311 */
312 public View getSubWebViewContainer() {
313 return mSubViewContainer;
314 }
315
316 /**
317 * Get the url of this tab. Valid after calling populatePickerData, but
318 * before calling wipePickerData, or if the webview has been destroyed.
319 *
320 * @return The WebView's url or null.
321 */
322 public String getUrl() {
Patrick Scott20abe042009-04-28 08:03:29 -0400323 if (mPickerData != null) {
324 return mPickerData.mUrl;
325 }
326 return null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800327 }
328
329 /**
330 * Get the title of this tab. Valid after calling populatePickerData,
331 * but before calling wipePickerData, or if the webview has been
332 * destroyed. If the url has no title, use the url instead.
333 *
334 * @return The WebView's title (or url) or null.
335 */
336 public String getTitle() {
Patrick Scott20abe042009-04-28 08:03:29 -0400337 if (mPickerData != null) {
338 return mPickerData.mTitle;
339 }
340 return null;
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400341 }
342
343 /**
344 * Returns the picker data.
345 */
346 public PickerData getPickerData() {
347 return mPickerData;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800348 }
349
350 private void setParentTab(Tab parent) {
351 mParentTab = parent;
352 // This tab may have been freed due to low memory. If that is the
353 // case, the parent tab index is already saved. If we are changing
354 // that index (most likely due to removing the parent tab) we must
355 // update the parent tab index in the saved Bundle.
356 if (mSavedState != null) {
357 if (parent == null) {
358 mSavedState.remove(PARENTTAB);
359 } else {
360 mSavedState.putInt(PARENTTAB, getTabIndex(parent));
361 }
362 }
363 }
364
365 /**
366 * When a Tab is created through the content of another Tab, then
367 * we associate the Tabs.
368 * @param child the Tab that was created from this Tab
369 */
370 public void addChildTab(Tab child) {
371 if (mChildTabs == null) {
372 mChildTabs = new Vector<Tab>();
373 }
374 mChildTabs.add(child);
375 child.setParentTab(this);
376 }
377
378 private void removeFromTree() {
379 // detach the children
380 if (mChildTabs != null) {
381 for(Tab t : mChildTabs) {
382 t.setParentTab(null);
383 }
384 }
385
386 // Find myself in my parent list
387 if (mParentTab != null) {
388 mParentTab.mChildTabs.remove(this);
389 }
390 }
391
392 /**
393 * If this Tab was created through another Tab, then this method
394 * returns that Tab.
395 * @return the Tab parent or null
396 */
397 public Tab getParentTab() {
398 return mParentTab;
399 }
400
401 /**
402 * Return whether this tab should be closed when it is backing out of
403 * the first page.
404 * @return TRUE if this tab should be closed when exit.
405 */
406 public boolean closeOnExit() {
407 return mCloseOnExit;
408 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400409
410 public void onNewPicture(WebView view, Picture p) {
411 if (mPickerData == null) {
412 return;
413 }
414
415 mPickerData.mPicture = p;
416 // Tell the FakeWebView to redraw.
417 if (mPickerData.mFakeWebView != null) {
418 mPickerData.mFakeWebView.invalidate();
419 }
420 }
Grace Klobaeb6eef42009-09-15 17:56:32 -0700421
422 void setLockIconType(int type) {
423 mSavedLockIconType = type;
424 }
425
426 int getLockIconType() {
427 return mSavedLockIconType;
428 }
429
430 void setPrevLockIconType(int type) {
431 mSavedPrevLockIconType = type;
432 }
433
434 int getPrevLockIconType() {
435 return mSavedPrevLockIconType;
436 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800437 };
438
439 // Directory to store thumbnails for each WebView.
440 private final File mThumbnailDir;
441
442 /**
443 * Construct a new TabControl object that interfaces with the given
444 * BrowserActivity instance.
445 * @param activity A BrowserActivity instance that TabControl will interface
446 * with.
447 */
448 TabControl(BrowserActivity activity) {
449 mActivity = activity;
450 mInflateService =
451 ((LayoutInflater) activity.getSystemService(
452 Context.LAYOUT_INFLATER_SERVICE));
453 mThumbnailDir = activity.getDir("thumbnails", 0);
454 }
455
456 File getThumbnailDir() {
457 return mThumbnailDir;
458 }
459
460 BrowserActivity getBrowserActivity() {
461 return mActivity;
462 }
463
464 /**
465 * Return the current tab's main WebView. This will always return the main
466 * WebView for a given tab and not a subwindow.
467 * @return The current tab's WebView.
468 */
469 WebView getCurrentWebView() {
470 Tab t = getTab(mCurrentTab);
471 if (t == null) {
472 return null;
473 }
474 return t.mMainView;
475 }
476
477 /**
Ben Murdochbff2d602009-07-01 20:19:05 +0100478 * Return the current tab's error console. Creates the console if createIfNEcessary
479 * is true and we haven't already created the console.
480 * @param createIfNecessary Flag to indicate if the console should be created if it has
481 * not been already.
482 * @return The current tab's error console, or null if one has not been created and
483 * createIfNecessary is false.
484 */
485 ErrorConsoleView getCurrentErrorConsole(boolean createIfNecessary) {
486 Tab t = getTab(mCurrentTab);
487 if (t == null) {
488 return null;
489 }
490
491 if (createIfNecessary && t.mErrorConsole == null) {
492 t.mErrorConsole = new ErrorConsoleView(mActivity);
493 t.mErrorConsole.setWebView(t.mMainView);
494 }
495
496 return t.mErrorConsole;
497 }
498
499 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800500 * Return the current tab's top-level WebView. This can return a subwindow
501 * if one exists.
502 * @return The top-level WebView of the current tab.
503 */
504 WebView getCurrentTopWebView() {
505 Tab t = getTab(mCurrentTab);
506 if (t == null) {
507 return null;
508 }
509 return t.mSubView != null ? t.mSubView : t.mMainView;
510 }
511
512 /**
513 * Return the current tab's subwindow if it exists.
514 * @return The subwindow of the current tab or null if it doesn't exist.
515 */
516 WebView getCurrentSubWindow() {
517 Tab t = getTab(mCurrentTab);
518 if (t == null) {
519 return null;
520 }
521 return t.mSubView;
522 }
523
524 /**
525 * Return the tab at the specified index.
526 * @return The Tab for the specified index or null if the tab does not
527 * exist.
528 */
529 Tab getTab(int index) {
530 if (index >= 0 && index < mTabs.size()) {
531 return mTabs.get(index);
532 }
533 return null;
534 }
535
536 /**
537 * Return the current tab.
538 * @return The current tab.
539 */
540 Tab getCurrentTab() {
541 return getTab(mCurrentTab);
542 }
543
544 /**
545 * Return the current tab index.
546 * @return The current tab index
547 */
548 int getCurrentIndex() {
549 return mCurrentTab;
550 }
551
552 /**
553 * Given a Tab, find it's index
554 * @param Tab to find
555 * @return index of Tab or -1 if not found
556 */
557 int getTabIndex(Tab tab) {
Patrick Scottae641ac2009-04-20 13:51:49 -0400558 if (tab == null) {
559 return -1;
560 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800561 return mTabs.indexOf(tab);
562 }
563
564 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700565 * Create a new tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800566 * @return The newly createTab or null if we have reached the maximum
567 * number of open tabs.
568 */
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700569 Tab createNewTab(boolean closeOnExit, String appId, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800570 int size = mTabs.size();
571 // Return false if we have maxed out on tabs
572 if (MAX_TABS == size) {
573 return null;
574 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700575 final WebView w = createNewWebView();
Steve Block2bc69912009-07-30 14:45:13 +0100576
The Android Open Source Project0c908882009-03-03 19:32:16 -0800577 // Create a new tab and add it to the tab list
Steve Block2bc69912009-07-30 14:45:13 +0100578 Tab t = new Tab(w, closeOnExit, appId, url, mActivity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800579 mTabs.add(t);
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700580 // Initially put the tab in the background.
581 putTabInBackground(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800582 return t;
583 }
584
585 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700586 * Create a new tab with default values for closeOnExit(false),
587 * appId(null), and url(null).
588 */
589 Tab createNewTab() {
590 return createNewTab(false, null, null);
591 }
592
593 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800594 * Remove the tab from the list. If the tab is the current tab shown, the
595 * last created tab will be shown.
596 * @param t The tab to be removed.
597 */
598 boolean removeTab(Tab t) {
599 if (t == null) {
600 return false;
601 }
602 // Only remove the tab if it is the current one.
603 if (getCurrentTab() == t) {
604 putTabInBackground(t);
605 }
606
607 // Only destroy the WebView if it still exists.
608 if (t.mMainView != null) {
609 // Take down the sub window.
610 dismissSubWindow(t);
611 // Remove the WebView's settings from the BrowserSettings list of
612 // observers.
613 BrowserSettings.getInstance().deleteObserver(
614 t.mMainView.getSettings());
615 // Destroy the main view and subview
616 t.mMainView.destroy();
Steve Block2bc69912009-07-30 14:45:13 +0100617 t.setWebView(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800618 }
619 // clear it's references to parent and children
620 t.removeFromTree();
621
622 // Remove it from our list of tabs.
623 mTabs.remove(t);
624
625 // The tab indices have shifted, update all the saved state so we point
626 // to the correct index.
627 for (Tab tab : mTabs) {
628 if (tab.mChildTabs != null) {
629 for (Tab child : tab.mChildTabs) {
630 child.setParentTab(tab);
631 }
632 }
633 }
634
635
636 // This tab may have been pushed in to the background and then closed.
637 // If the saved state contains a picture file, delete the file.
638 if (t.mSavedState != null) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400639 if (t.mSavedState.containsKey(CURRPICTURE)) {
640 new File(t.mSavedState.getString(CURRPICTURE)).delete();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800641 }
642 }
643
644 // Remove it from the queue of viewed tabs.
645 mTabQueue.remove(t);
646 mCurrentTab = -1;
647 return true;
648 }
649
650 /**
651 * Clear the back/forward list for all the current tabs.
652 */
653 void clearHistory() {
654 int size = getTabCount();
655 for (int i = 0; i < size; i++) {
656 Tab t = mTabs.get(i);
657 // TODO: if a tab is freed due to low memory, its history is not
658 // cleared here.
659 if (t.mMainView != null) {
660 t.mMainView.clearHistory();
661 }
662 if (t.mSubView != null) {
663 t.mSubView.clearHistory();
664 }
665 }
666 }
667
668 /**
669 * Destroy all the tabs and subwindows
670 */
671 void destroy() {
672 BrowserSettings s = BrowserSettings.getInstance();
673 for (Tab t : mTabs) {
674 if (t.mMainView != null) {
675 dismissSubWindow(t);
676 s.deleteObserver(t.mMainView.getSettings());
677 t.mMainView.destroy();
Steve Block2bc69912009-07-30 14:45:13 +0100678 t.setWebView(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800679 }
680 }
681 mTabs.clear();
682 mTabQueue.clear();
683 }
684
685 /**
686 * Returns the number of tabs created.
687 * @return The number of tabs created.
688 */
689 int getTabCount() {
690 return mTabs.size();
691 }
692
693 // Used for saving and restoring each Tab
694 private static final String WEBVIEW = "webview";
695 private static final String NUMTABS = "numTabs";
696 private static final String CURRTAB = "currentTab";
697 private static final String CURRURL = "currentUrl";
698 private static final String CURRTITLE = "currentTitle";
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400699 private static final String CURRWIDTH = "currentWidth";
700 private static final String CURRPICTURE = "currentPicture";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800701 private static final String CLOSEONEXIT = "closeonexit";
702 private static final String PARENTTAB = "parentTab";
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700703 private static final String APPID = "appid";
704 private static final String ORIGINALURL = "originalUrl";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800705
706 /**
707 * Save the state of all the Tabs.
708 * @param outState The Bundle to save the state to.
709 */
710 void saveState(Bundle outState) {
711 final int numTabs = getTabCount();
712 outState.putInt(NUMTABS, numTabs);
713 final int index = getCurrentIndex();
714 outState.putInt(CURRTAB, (index >= 0 && index < numTabs) ? index : 0);
715 for (int i = 0; i < numTabs; i++) {
716 final Tab t = getTab(i);
717 if (saveState(t)) {
718 outState.putBundle(WEBVIEW + i, t.mSavedState);
719 }
720 }
721 }
722
723 /**
724 * Restore the state of all the tabs.
725 * @param inState The saved state of all the tabs.
726 * @return True if there were previous tabs that were restored. False if
727 * there was no saved state or restoring the state failed.
728 */
729 boolean restoreState(Bundle inState) {
730 final int numTabs = (inState == null)
731 ? -1 : inState.getInt(NUMTABS, -1);
732 if (numTabs == -1) {
733 return false;
734 } else {
735 final int currentTab = inState.getInt(CURRTAB, -1);
736 for (int i = 0; i < numTabs; i++) {
737 if (i == currentTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700738 Tab t = createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800739 // Me must set the current tab before restoring the state
740 // so that all the client classes are set.
741 setCurrentTab(t);
742 if (!restoreState(inState.getBundle(WEBVIEW + i), t)) {
743 Log.w(LOGTAG, "Fail in restoreState, load home page.");
744 t.mMainView.loadUrl(BrowserSettings.getInstance()
745 .getHomePage());
746 }
747 } else {
748 // Create a new tab and don't restore the state yet, add it
749 // to the tab list
Steve Block2bc69912009-07-30 14:45:13 +0100750 Tab t = new Tab(null, false, null, null, mActivity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800751 t.mSavedState = inState.getBundle(WEBVIEW + i);
752 if (t.mSavedState != null) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400753 populatePickerDataFromSavedState(t);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700754 // Need to maintain the app id and original url so we
755 // can possibly reuse this tab.
756 t.mAppId = t.mSavedState.getString(APPID);
757 t.mOriginalUrl = t.mSavedState.getString(ORIGINALURL);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800758 }
759 mTabs.add(t);
760 mTabQueue.add(t);
761 }
762 }
763 // Rebuild the tree of tabs. Do this after all tabs have been
764 // created/restored so that the parent tab exists.
765 for (int i = 0; i < numTabs; i++) {
766 final Bundle b = inState.getBundle(WEBVIEW + i);
767 final Tab t = getTab(i);
768 if (b != null && t != null) {
769 final int parentIndex = b.getInt(PARENTTAB, -1);
770 if (parentIndex != -1) {
771 final Tab parent = getTab(parentIndex);
772 if (parent != null) {
773 parent.addChildTab(t);
774 }
775 }
776 }
777 }
778 }
779 return true;
780 }
781
782 /**
783 * Free the memory in this order, 1) free the background tab; 2) free the
784 * WebView cache;
785 */
786 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700787 if (getTabCount() == 0) return;
788
The Android Open Source Project0c908882009-03-03 19:32:16 -0800789 // free the least frequently used background tab
Patrick Scott2a67de42009-08-31 09:48:37 -0400790 Tab t = getLeastUsedTab(getCurrentTab());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800791 if (t != null) {
792 Log.w(LOGTAG, "Free a tab in the browser");
793 freeTab(t);
794 // force a gc
795 System.gc();
796 return;
797 }
798
Derek Sollenberger4433d032009-06-10 15:37:21 -0400799 // free the WebView's unused memory (this includes the cache)
800 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800801 WebView view = getCurrentWebView();
802 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400803 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800804 }
805 // force a gc
806 System.gc();
807 }
808
Patrick Scott2a67de42009-08-31 09:48:37 -0400809 private Tab getLeastUsedTab(Tab current) {
810 // Don't do anything if we only have 1 tab or if the current tab is
811 // null.
812 if (getTabCount() == 1 || current == null) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800813 return null;
814 }
815
816 // Rip through the queue starting at the beginning and teardown the
817 // next available tab.
818 Tab t = null;
819 int i = 0;
820 final int queueSize = mTabQueue.size();
821 if (queueSize == 0) {
822 return null;
823 }
824 do {
825 t = mTabQueue.get(i++);
Grace Kloba92c18a52009-07-31 23:48:32 -0700826 } while (i < queueSize
Patrick Scott2a67de42009-08-31 09:48:37 -0400827 && ((t != null && t.mMainView == null)
828 || t == current.mParentTab));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800829
Patrick Scottd068f802009-06-22 11:46:06 -0400830 // Don't do anything if the last remaining tab is the current one or if
831 // the last tab has been freed already.
Patrick Scott2a67de42009-08-31 09:48:37 -0400832 if (t == current || t.mMainView == null) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800833 return null;
834 }
835
836 return t;
837 }
838
839 private void freeTab(Tab t) {
840 // Store the WebView's state.
841 saveState(t);
842
843 // Tear down the tab.
844 dismissSubWindow(t);
845 // Remove the WebView's settings from the BrowserSettings list of
846 // observers.
847 BrowserSettings.getInstance().deleteObserver(t.mMainView.getSettings());
848 t.mMainView.destroy();
Steve Block2bc69912009-07-30 14:45:13 +0100849 t.setWebView(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800850 }
851
852 /**
853 * Create a new subwindow unless a subwindow already exists.
854 * @return True if a new subwindow was created. False if one already exists.
855 */
856 void createSubWindow() {
857 Tab t = getTab(mCurrentTab);
858 if (t != null && t.mSubView == null) {
859 final View v = mInflateService.inflate(R.layout.browser_subwindow, null);
860 final WebView w = (WebView) v.findViewById(R.id.webview);
861 w.setMapTrackballToArrowKeys(false); // use trackball directly
862 final SubWindowClient subClient =
863 new SubWindowClient(mActivity.getWebViewClient());
864 final SubWindowChromeClient subChromeClient =
865 new SubWindowChromeClient(t,
866 mActivity.getWebChromeClient());
867 w.setWebViewClient(subClient);
868 w.setWebChromeClient(subChromeClient);
869 w.setDownloadListener(mActivity);
870 w.setOnCreateContextMenuListener(mActivity);
871 final BrowserSettings s = BrowserSettings.getInstance();
872 s.addObserver(w.getSettings()).update(s, null);
873 t.mSubView = w;
874 t.mSubViewClient = subClient;
875 t.mSubViewChromeClient = subChromeClient;
876 // FIXME: I really hate having to know the name of the view
877 // containing the webview.
878 t.mSubViewContainer = v.findViewById(R.id.subwindow_container);
879 final ImageButton cancel =
880 (ImageButton) v.findViewById(R.id.subwindow_close);
881 cancel.setOnClickListener(new OnClickListener() {
882 public void onClick(View v) {
883 subChromeClient.onCloseWindow(w);
884 }
885 });
886 }
887 }
888
889 /**
890 * Show the tab that contains the given WebView.
891 * @param view The WebView used to find the tab.
892 */
893 Tab getTabFromView(WebView view) {
894 final int size = getTabCount();
895 for (int i = 0; i < size; i++) {
896 final Tab t = getTab(i);
897 if (t.mSubView == view || t.mMainView == view) {
898 return t;
899 }
900 }
901 return null;
902 }
903
904 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700905 * Return the tab with the matching application id.
906 * @param id The application identifier.
907 */
908 Tab getTabFromId(String id) {
909 if (id == null) {
910 return null;
911 }
912 final int size = getTabCount();
913 for (int i = 0; i < size; i++) {
914 final Tab t = getTab(i);
915 if (id.equals(t.mAppId)) {
916 return t;
917 }
918 }
919 return null;
920 }
921
Patrick Scottcd115892009-07-16 09:42:58 -0400922 // This method checks if a non-app tab (one created within the browser)
923 // matches the given url.
924 private boolean tabMatchesUrl(Tab t, String url) {
925 if (t.mAppId != null) {
926 return false;
927 } else if (t.mMainView == null) {
928 return false;
929 } else if (url.equals(t.mMainView.getUrl()) ||
930 url.equals(t.mMainView.getOriginalUrl())) {
931 return true;
932 }
933 return false;
934 }
935
936 /**
937 * Return the tab that has no app id associated with it and the url of the
938 * tab matches the given url.
939 * @param url The url to search for.
940 */
941 Tab findUnusedTabWithUrl(String url) {
942 if (url == null) {
943 return null;
944 }
945 // Check the current tab first.
946 Tab t = getCurrentTab();
947 if (t != null && tabMatchesUrl(t, url)) {
948 return t;
949 }
950 // Now check all the rest.
951 final int size = getTabCount();
952 for (int i = 0; i < size; i++) {
953 t = getTab(i);
954 if (tabMatchesUrl(t, url)) {
955 return t;
956 }
957 }
958 return null;
959 }
960
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700961 /**
962 * Recreate the main WebView of the given tab. Returns true if the WebView
963 * was deleted.
964 */
965 boolean recreateWebView(Tab t, String url) {
966 final WebView w = t.mMainView;
967 if (w != null) {
968 if (url != null && url.equals(t.mOriginalUrl)) {
969 // The original url matches the current url. Just go back to the
970 // first history item so we can load it faster than if we
971 // rebuilt the WebView.
972 final WebBackForwardList list = w.copyBackForwardList();
973 if (list != null) {
974 w.goBackOrForward(-list.getCurrentIndex());
975 w.clearHistory(); // maintains the current page.
976 return false;
977 }
978 }
979 // Remove the settings object from the global settings and destroy
980 // the WebView.
981 BrowserSettings.getInstance().deleteObserver(
982 t.mMainView.getSettings());
983 t.mMainView.destroy();
984 }
985 // Create a new WebView. If this tab is the current tab, we need to put
986 // back all the clients so force it to be the current tab.
Steve Block2bc69912009-07-30 14:45:13 +0100987 t.setWebView(createNewWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700988 if (getCurrentTab() == t) {
989 setCurrentTab(t, true);
990 }
991 // Clear the saved state except for the app id and close-on-exit
992 // values.
993 t.mSavedState = null;
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400994 t.mPickerData = null;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700995 // Save the new url in order to avoid deleting the WebView.
996 t.mOriginalUrl = url;
997 return true;
998 }
999
1000 /**
1001 * Creates a new WebView and registers it with the global settings.
1002 */
1003 private WebView createNewWebView() {
1004 // Create a new WebView
1005 WebView w = new WebView(mActivity);
1006 w.setMapTrackballToArrowKeys(false); // use trackball directly
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -07001007 // Enable the built-in zoom
1008 w.getSettings().setBuiltInZoomControls(true);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001009 // Add this WebView to the settings observer list and update the
1010 // settings
1011 final BrowserSettings s = BrowserSettings.getInstance();
1012 s.addObserver(w.getSettings()).update(s, null);
1013 return w;
1014 }
1015
1016 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001017 * Put the current tab in the background and set newTab as the current tab.
1018 * @param newTab The new tab. If newTab is null, the current tab is not
1019 * set.
1020 */
1021 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001022 return setCurrentTab(newTab, false);
1023 }
1024
Mike Reed7bfa63b2009-05-28 11:08:32 -04001025 /*package*/ void pauseCurrentTab() {
1026 Tab t = getCurrentTab();
1027 if (t != null) {
1028 t.mMainView.onPause();
1029 if (t.mSubView != null) {
1030 t.mSubView.onPause();
1031 }
1032 }
1033 }
1034
1035 /*package*/ void resumeCurrentTab() {
1036 Tab t = getCurrentTab();
1037 if (t != null) {
1038 t.mMainView.onResume();
1039 if (t.mSubView != null) {
1040 t.mSubView.onResume();
1041 }
1042 }
1043 }
1044
1045 private void putViewInForeground(WebView v, WebViewClient vc,
1046 WebChromeClient cc) {
1047 v.setWebViewClient(vc);
1048 v.setWebChromeClient(cc);
1049 v.setOnCreateContextMenuListener(mActivity);
1050 v.setDownloadListener(mActivity);
1051 v.onResume();
1052 }
1053
1054 private void putViewInBackground(WebView v) {
1055 // Set an empty callback so that default actions are not triggered.
1056 v.setWebViewClient(mEmptyClient);
1057 v.setWebChromeClient(mBackgroundChromeClient);
1058 v.setOnCreateContextMenuListener(null);
1059 // Leave the DownloadManager attached so that downloads can start in
1060 // a non-active window. This can happen when going to a site that does
1061 // a redirect after a period of time. The user could have switched to
1062 // another tab while waiting for the download to start.
1063 v.setDownloadListener(mActivity);
1064 v.onPause();
1065 }
1066
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001067 /**
1068 * If force is true, this method skips the check for newTab == current.
1069 */
1070 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001071 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001072 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001073 return true;
1074 }
1075 if (current != null) {
1076 // Remove the current WebView and the container of the subwindow
1077 putTabInBackground(current);
1078 }
1079
1080 if (newTab == null) {
1081 return false;
1082 }
1083
1084 // Move the newTab to the end of the queue
1085 int index = mTabQueue.indexOf(newTab);
1086 if (index != -1) {
1087 mTabQueue.remove(index);
1088 }
1089 mTabQueue.add(newTab);
1090
1091 WebView mainView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001092
1093 // Display the new current tab
1094 mCurrentTab = mTabs.indexOf(newTab);
1095 mainView = newTab.mMainView;
1096 boolean needRestore = (mainView == null);
1097 if (needRestore) {
1098 // Same work as in createNewTab() except don't do new Tab()
Steve Block2bc69912009-07-30 14:45:13 +01001099 mainView = createNewWebView();
1100 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001101 }
Mike Reed7bfa63b2009-05-28 11:08:32 -04001102 putViewInForeground(mainView, mActivity.getWebViewClient(),
1103 mActivity.getWebChromeClient());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001104 // Add the subwindow if it exists
1105 if (newTab.mSubViewContainer != null) {
Mike Reed7bfa63b2009-05-28 11:08:32 -04001106 putViewInForeground(newTab.mSubView, newTab.mSubViewClient,
1107 newTab.mSubViewChromeClient);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001108 }
1109 if (needRestore) {
1110 // Have to finish setCurrentTab work before calling restoreState
1111 if (!restoreState(newTab.mSavedState, newTab)) {
1112 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
1113 }
1114 }
1115 return true;
1116 }
1117
1118 /*
1119 * Put the tab in the background using all the empty/background clients.
1120 */
1121 private void putTabInBackground(Tab t) {
Mike Reed7bfa63b2009-05-28 11:08:32 -04001122 putViewInBackground(t.mMainView);
1123 if (t.mSubView != null) {
1124 putViewInBackground(t.mSubView);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001125 }
1126 }
1127
1128 /*
1129 * Dismiss the subwindow for the given tab.
1130 */
1131 void dismissSubWindow(Tab t) {
1132 if (t != null && t.mSubView != null) {
1133 BrowserSettings.getInstance().deleteObserver(
1134 t.mSubView.getSettings());
1135 t.mSubView.destroy();
1136 t.mSubView = null;
1137 t.mSubViewContainer = null;
1138 }
1139 }
1140
1141 /**
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001142 * Ensure that Tab t has data to display in the tab picker.
The Android Open Source Project0c908882009-03-03 19:32:16 -08001143 * @param t Tab to populate.
1144 */
1145 /* package */ void populatePickerData(Tab t) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001146 if (t == null) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001147 return;
1148 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001149
1150 // mMainView == null indicates that the tab has been freed.
1151 if (t.mMainView == null) {
1152 populatePickerDataFromSavedState(t);
1153 return;
1154 }
1155
The Android Open Source Project0c908882009-03-03 19:32:16 -08001156 // FIXME: The only place we cared about subwindow was for
1157 // bookmarking (i.e. not when saving state). Was this deliberate?
1158 final WebBackForwardList list = t.mMainView.copyBackForwardList();
1159 final WebHistoryItem item =
1160 list != null ? list.getCurrentItem() : null;
1161 populatePickerData(t, item);
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001162
1163 // This method is only called during the tab picker creation. At this
1164 // point we need to listen for new pictures since the WebView is still
1165 // active.
1166 final WebView w = t.getTopWindow();
1167 w.setPictureListener(t);
1168 // Capture the picture here instead of populatePickerData since it can
1169 // be called when saving the state of a tab.
1170 t.mPickerData.mPicture = w.capturePicture();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001171 }
1172
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001173 // Create the PickerData and populate it using the saved state of the tab.
1174 private void populatePickerDataFromSavedState(Tab t) {
1175 if (t.mSavedState == null) {
1176 return;
1177 }
1178
1179 final PickerData data = new PickerData();
1180 final Bundle state = t.mSavedState;
1181 data.mUrl = state.getString(CURRURL);
1182 data.mTitle = state.getString(CURRTITLE);
1183 data.mWidth = state.getInt(CURRWIDTH, 0);
1184 // XXX: These keys are from WebView.savePicture so if they change, this
1185 // will break.
1186 data.mScale = state.getFloat("scale", 1.0f);
1187 data.mScrollX = state.getInt("scrollX", 0);
1188 data.mScrollY = state.getInt("scrollY", 0);
1189
1190 if (state.containsKey(CURRPICTURE)) {
1191 final File f = new File(t.mSavedState.getString(CURRPICTURE));
1192 try {
1193 final FileInputStream in = new FileInputStream(f);
1194 data.mPicture = Picture.createFromStream(in);
1195 in.close();
1196 } catch (Exception ex) {
1197 // Ignore any problems with inflating the picture. We just
1198 // won't draw anything.
The Android Open Source Project0c908882009-03-03 19:32:16 -08001199 }
1200 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001201
1202 // Set the tab's picker data.
1203 t.mPickerData = data;
1204 }
1205
1206 // Populate the picker data using the given history item and the current
1207 // top WebView.
1208 private void populatePickerData(Tab t, WebHistoryItem item) {
1209 final PickerData data = new PickerData();
1210 if (item != null) {
1211 data.mUrl = item.getUrl();
1212 data.mTitle = item.getTitle();
1213 if (data.mTitle == null) {
1214 data.mTitle = data.mUrl;
1215 }
1216 }
1217 // We want to display the top window in the tab picker but use the url
1218 // and title of the main window.
1219 final WebView w = t.getTopWindow();
1220 data.mWidth = w.getWidth();
1221 data.mScale = w.getScale();
1222 data.mScrollX = w.getScrollX();
1223 data.mScrollY = w.getScrollY();
Patrick Scottb0e4fc72009-07-14 10:49:22 -04001224
1225 // Remember the old picture if possible.
1226 if (t.mPickerData != null) {
1227 data.mPicture = t.mPickerData.mPicture;
1228 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001229 t.mPickerData = data;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001230 }
1231
1232 /**
1233 * Clean up the data for all tabs.
1234 */
1235 /* package */ void wipeAllPickerData() {
1236 int size = getTabCount();
1237 for (int i = 0; i < size; i++) {
1238 final Tab t = getTab(i);
1239 if (t != null && t.mSavedState == null) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001240 t.mPickerData = null;
1241 }
1242 if (t.mMainView != null) {
1243 // Clear the picture listeners.
1244 t.mMainView.setPictureListener(null);
1245 if (t.mSubView != null) {
1246 t.mSubView.setPictureListener(null);
1247 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001248 }
1249 }
1250 }
1251
1252 /*
1253 * Save the state for an individual tab.
1254 */
1255 private boolean saveState(Tab t) {
1256 if (t != null) {
1257 final WebView w = t.mMainView;
1258 // If the WebView is null it means we ran low on memory and we
1259 // already stored the saved state in mSavedState.
1260 if (w == null) {
1261 return true;
1262 }
1263 final Bundle b = new Bundle();
1264 final WebBackForwardList list = w.saveState(b);
1265 if (list != null) {
1266 final File f = new File(mThumbnailDir, w.hashCode()
1267 + "_pic.save");
1268 if (w.savePicture(b, f)) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001269 b.putString(CURRPICTURE, f.getPath());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001270 }
1271 }
1272
1273 // Store some extra info for displaying the tab in the picker.
1274 final WebHistoryItem item =
1275 list != null ? list.getCurrentItem() : null;
1276 populatePickerData(t, item);
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001277
1278 // XXX: WebView.savePicture stores the scale and scroll positions
1279 // in the bundle so we don't have to do it here.
1280 final PickerData data = t.mPickerData;
1281 if (data.mUrl != null) {
1282 b.putString(CURRURL, data.mUrl);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001283 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001284 if (data.mTitle != null) {
1285 b.putString(CURRTITLE, data.mTitle);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001286 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001287 b.putInt(CURRWIDTH, data.mWidth);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001288 b.putBoolean(CLOSEONEXIT, t.mCloseOnExit);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001289 if (t.mAppId != null) {
1290 b.putString(APPID, t.mAppId);
1291 }
1292 if (t.mOriginalUrl != null) {
1293 b.putString(ORIGINALURL, t.mOriginalUrl);
1294 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001295
1296 // Remember the parent tab so the relationship can be restored.
1297 if (t.mParentTab != null) {
1298 b.putInt(PARENTTAB, getTabIndex(t.mParentTab));
1299 }
1300
1301 // Remember the saved state.
1302 t.mSavedState = b;
1303 return true;
1304 }
1305 return false;
1306 }
1307
1308 /*
1309 * Restore the state of the tab.
1310 */
1311 private boolean restoreState(Bundle b, Tab t) {
1312 if (b == null) {
1313 return false;
1314 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001315 // Restore the internal state even if the WebView fails to restore.
1316 // This will maintain the app id, original url and close-on-exit values.
1317 t.mSavedState = null;
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001318 t.mPickerData = null;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001319 t.mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1320 t.mAppId = b.getString(APPID);
1321 t.mOriginalUrl = b.getString(ORIGINALURL);
1322
The Android Open Source Project0c908882009-03-03 19:32:16 -08001323 final WebView w = t.mMainView;
1324 final WebBackForwardList list = w.restoreState(b);
1325 if (list == null) {
1326 return false;
1327 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001328 if (b.containsKey(CURRPICTURE)) {
1329 final File f = new File(b.getString(CURRPICTURE));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001330 w.restorePicture(b, f);
1331 f.delete();
1332 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001333 return true;
1334 }
1335}