2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.browser;
19 import android.content.Context;
20 import android.graphics.Picture;
21 import android.net.http.SslError;
22 import android.os.Bundle;
23 import android.os.Message;
24 import android.util.Log;
25 import android.view.Gravity;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.view.View.OnClickListener;
30 import android.webkit.HttpAuthHandler;
31 import android.webkit.JsPromptResult;
32 import android.webkit.JsResult;
33 import android.webkit.SslErrorHandler;
34 import android.webkit.WebBackForwardList;
35 import android.webkit.WebChromeClient;
36 import android.webkit.WebHistoryItem;
37 import android.webkit.WebView;
38 import android.webkit.WebViewClient;
39 import android.widget.FrameLayout;
40 import android.widget.ImageButton;
43 import java.io.FileInputStream;
44 import java.util.ArrayList;
45 import java.util.Vector;
49 private static final String LOGTAG = "TabControl";
50 // Maximum number of tabs.
51 static final int MAX_TABS = 8;
52 // Static instance of an empty callback.
53 private static final WebViewClient mEmptyClient =
55 // Instance of BackgroundChromeClient for background tabs.
56 private final BackgroundChromeClient mBackgroundChromeClient =
57 new BackgroundChromeClient();
58 // Private array of WebViews that are used as tabs.
59 private ArrayList<Tab> mTabs = new ArrayList<Tab>(MAX_TABS);
60 // Queue of most recently viewed tabs.
61 private ArrayList<Tab> mTabQueue = new ArrayList<Tab>(MAX_TABS);
62 // Current position in mTabs.
63 private int mCurrentTab = -1;
64 // A private instance of BrowserActivity to interface with when adding and
65 // switching between tabs.
66 private final BrowserActivity mActivity;
67 // Inflation service for making subwindows.
68 private final LayoutInflater mInflateService;
69 // Subclass of WebViewClient used in subwindows to notify the main
70 // WebViewClient of certain WebView activities.
71 private class SubWindowClient extends WebViewClient {
72 // The main WebViewClient.
73 private final WebViewClient mClient;
75 SubWindowClient(WebViewClient client) {
79 public void doUpdateVisitedHistory(WebView view, String url,
81 mClient.doUpdateVisitedHistory(view, url, isReload);
84 public boolean shouldOverrideUrlLoading(WebView view, String url) {
85 return mClient.shouldOverrideUrlLoading(view, url);
88 public void onReceivedSslError(WebView view, SslErrorHandler handler,
90 mClient.onReceivedSslError(view, handler, error);
93 public void onReceivedHttpAuthRequest(WebView view,
94 HttpAuthHandler handler, String host, String realm) {
95 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
98 public void onFormResubmission(WebView view, Message dontResend,
100 mClient.onFormResubmission(view, dontResend, resend);
103 public void onReceivedError(WebView view, int errorCode,
104 String description, String failingUrl) {
105 mClient.onReceivedError(view, errorCode, description, failingUrl);
108 // Subclass of WebChromeClient to display javascript dialogs.
109 private class SubWindowChromeClient extends WebChromeClient {
110 // This subwindow's tab.
111 private final Tab mTab;
112 // The main WebChromeClient.
113 private final WebChromeClient mClient;
115 SubWindowChromeClient(Tab t, WebChromeClient client) {
120 public void onProgressChanged(WebView view, int newProgress) {
121 mClient.onProgressChanged(view, newProgress);
124 public boolean onCreateWindow(WebView view, boolean dialog,
125 boolean userGesture, android.os.Message resultMsg) {
126 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
129 public void onCloseWindow(WebView window) {
130 if (Browser.DEBUG && window != mTab.mSubView) {
131 throw new AssertionError("Can't close the window");
133 mActivity.dismissSubWindow(mTab);
136 // Background WebChromeClient for focusing tabs
137 private class BackgroundChromeClient extends WebChromeClient {
139 public void onRequestFocus(WebView view) {
140 Tab t = getTabFromView(view);
141 if (t != getCurrentTab()) {
142 mActivity.showTab(t);
147 // Extra saved information for displaying the tab in the picker.
148 public static class PickerData {
156 // This can be null. When a new picture comes in, this view should be
157 // invalidated to show the new picture.
158 FakeWebView mFakeWebView;
162 * Private class for maintaining Tabs with a main WebView and a subwindow.
164 public class Tab implements WebView.PictureListener {
166 private WebView mMainView;
168 private WebView mSubView;
169 // Subwindow container
170 private View mSubViewContainer;
171 // Subwindow callback
172 private SubWindowClient mSubViewClient;
173 // Subwindow chrome callback
174 private SubWindowChromeClient mSubViewChromeClient;
175 // Saved bundle for when we are running low on memory. It contains the
176 // information needed to restore the WebView if the user goes back to
178 private Bundle mSavedState;
179 // Data used when displaying the tab in the picker.
180 private PickerData mPickerData;
182 // Parent Tab. This is the Tab that created this Tab, or null
183 // if the Tab was created by the UI
184 private Tab mParentTab;
185 // Tab that constructed by this Tab. This is used when this
186 // Tab is destroyed, it clears all mParentTab values in the
188 private Vector<Tab> mChildTabs;
190 private Boolean mCloseOnExit;
191 // Application identifier used to find tabs that another application
193 private String mAppId;
194 // Keep the original url around to avoid killing the old WebView if the
195 // url has not changed.
196 private String mOriginalUrl;
198 // Construct a new tab
199 private Tab(WebView w, boolean closeOnExit, String appId, String url) {
201 mCloseOnExit = closeOnExit;
207 * Return the top window of this tab; either the subwindow if it is not
208 * null or the main window.
209 * @return The top window of this tab.
211 public WebView getTopWindow() {
212 if (mSubView != null) {
219 * Return the main window of this tab. Note: if a tab is freed in the
220 * background, this can return null. It is only guaranteed to be
221 * non-null for the current tab.
222 * @return The main WebView of this tab.
224 public WebView getWebView() {
229 * Return the subwindow of this tab or null if there is no subwindow.
230 * @return The subwindow of this tab or null.
232 public WebView getSubWebView() {
237 * Return the subwindow container of this tab or null if there is no
239 * @return The subwindow's container View.
241 public View getSubWebViewContainer() {
242 return mSubViewContainer;
246 * Get the url of this tab. Valid after calling populatePickerData, but
247 * before calling wipePickerData, or if the webview has been destroyed.
249 * @return The WebView's url or null.
251 public String getUrl() {
252 if (mPickerData != null) {
253 return mPickerData.mUrl;
259 * Get the title of this tab. Valid after calling populatePickerData,
260 * but before calling wipePickerData, or if the webview has been
261 * destroyed. If the url has no title, use the url instead.
263 * @return The WebView's title (or url) or null.
265 public String getTitle() {
266 if (mPickerData != null) {
267 return mPickerData.mTitle;
273 * Returns the picker data.
275 public PickerData getPickerData() {
279 private void setParentTab(Tab parent) {
281 // This tab may have been freed due to low memory. If that is the
282 // case, the parent tab index is already saved. If we are changing
283 // that index (most likely due to removing the parent tab) we must
284 // update the parent tab index in the saved Bundle.
285 if (mSavedState != null) {
286 if (parent == null) {
287 mSavedState.remove(PARENTTAB);
289 mSavedState.putInt(PARENTTAB, getTabIndex(parent));
295 * When a Tab is created through the content of another Tab, then
296 * we associate the Tabs.
297 * @param child the Tab that was created from this Tab
299 public void addChildTab(Tab child) {
300 if (mChildTabs == null) {
301 mChildTabs = new Vector<Tab>();
303 mChildTabs.add(child);
304 child.setParentTab(this);
307 private void removeFromTree() {
308 // detach the children
309 if (mChildTabs != null) {
310 for(Tab t : mChildTabs) {
311 t.setParentTab(null);
315 // Find myself in my parent list
316 if (mParentTab != null) {
317 mParentTab.mChildTabs.remove(this);
322 * If this Tab was created through another Tab, then this method
324 * @return the Tab parent or null
326 public Tab getParentTab() {
331 * Return whether this tab should be closed when it is backing out of
333 * @return TRUE if this tab should be closed when exit.
335 public boolean closeOnExit() {
339 public void onNewPicture(WebView view, Picture p) {
340 if (mPickerData == null) {
344 mPickerData.mPicture = p;
345 // Tell the FakeWebView to redraw.
346 if (mPickerData.mFakeWebView != null) {
347 mPickerData.mFakeWebView.invalidate();
352 // Directory to store thumbnails for each WebView.
353 private final File mThumbnailDir;
356 * Construct a new TabControl object that interfaces with the given
357 * BrowserActivity instance.
358 * @param activity A BrowserActivity instance that TabControl will interface
361 TabControl(BrowserActivity activity) {
362 mActivity = activity;
364 ((LayoutInflater) activity.getSystemService(
365 Context.LAYOUT_INFLATER_SERVICE));
366 mThumbnailDir = activity.getDir("thumbnails", 0);
369 File getThumbnailDir() {
370 return mThumbnailDir;
373 BrowserActivity getBrowserActivity() {
378 * Return the current tab's main WebView. This will always return the main
379 * WebView for a given tab and not a subwindow.
380 * @return The current tab's WebView.
382 WebView getCurrentWebView() {
383 Tab t = getTab(mCurrentTab);
391 * Return the current tab's top-level WebView. This can return a subwindow
393 * @return The top-level WebView of the current tab.
395 WebView getCurrentTopWebView() {
396 Tab t = getTab(mCurrentTab);
400 return t.mSubView != null ? t.mSubView : t.mMainView;
404 * Return the current tab's subwindow if it exists.
405 * @return The subwindow of the current tab or null if it doesn't exist.
407 WebView getCurrentSubWindow() {
408 Tab t = getTab(mCurrentTab);
416 * Return the tab at the specified index.
417 * @return The Tab for the specified index or null if the tab does not
420 Tab getTab(int index) {
421 if (index >= 0 && index < mTabs.size()) {
422 return mTabs.get(index);
428 * Return the current tab.
429 * @return The current tab.
431 Tab getCurrentTab() {
432 return getTab(mCurrentTab);
436 * Return the current tab index.
437 * @return The current tab index
439 int getCurrentIndex() {
444 * Given a Tab, find it's index
446 * @return index of Tab or -1 if not found
448 int getTabIndex(Tab tab) {
452 return mTabs.indexOf(tab);
457 * @return The newly createTab or null if we have reached the maximum
458 * number of open tabs.
460 Tab createNewTab(boolean closeOnExit, String appId, String url) {
461 int size = mTabs.size();
462 // Return false if we have maxed out on tabs
463 if (MAX_TABS == size) {
466 final WebView w = createNewWebView();
467 // Create a new tab and add it to the tab list
468 Tab t = new Tab(w, closeOnExit, appId, url);
470 // Initially put the tab in the background.
471 putTabInBackground(t);
476 * Create a new tab with default values for closeOnExit(false),
477 * appId(null), and url(null).
480 return createNewTab(false, null, null);
484 * Remove the tab from the list. If the tab is the current tab shown, the
485 * last created tab will be shown.
486 * @param t The tab to be removed.
488 boolean removeTab(Tab t) {
492 // Only remove the tab if it is the current one.
493 if (getCurrentTab() == t) {
494 putTabInBackground(t);
497 // Only destroy the WebView if it still exists.
498 if (t.mMainView != null) {
499 // Take down the sub window.
501 // Remove the WebView's settings from the BrowserSettings list of
503 BrowserSettings.getInstance().deleteObserver(
504 t.mMainView.getSettings());
505 // Destroy the main view and subview
506 t.mMainView.destroy();
509 // clear it's references to parent and children
512 // Remove it from our list of tabs.
515 // The tab indices have shifted, update all the saved state so we point
516 // to the correct index.
517 for (Tab tab : mTabs) {
518 if (tab.mChildTabs != null) {
519 for (Tab child : tab.mChildTabs) {
520 child.setParentTab(tab);
526 // This tab may have been pushed in to the background and then closed.
527 // If the saved state contains a picture file, delete the file.
528 if (t.mSavedState != null) {
529 if (t.mSavedState.containsKey(CURRPICTURE)) {
530 new File(t.mSavedState.getString(CURRPICTURE)).delete();
534 // Remove it from the queue of viewed tabs.
541 * Clear the back/forward list for all the current tabs.
543 void clearHistory() {
544 int size = getTabCount();
545 for (int i = 0; i < size; i++) {
546 Tab t = mTabs.get(i);
547 // TODO: if a tab is freed due to low memory, its history is not
549 if (t.mMainView != null) {
550 t.mMainView.clearHistory();
552 if (t.mSubView != null) {
553 t.mSubView.clearHistory();
559 * Destroy all the tabs and subwindows
562 BrowserSettings s = BrowserSettings.getInstance();
563 for (Tab t : mTabs) {
564 if (t.mMainView != null) {
566 s.deleteObserver(t.mMainView.getSettings());
567 t.mMainView.destroy();
576 * Returns the number of tabs created.
577 * @return The number of tabs created.
583 // Used for saving and restoring each Tab
584 private static final String WEBVIEW = "webview";
585 private static final String NUMTABS = "numTabs";
586 private static final String CURRTAB = "currentTab";
587 private static final String CURRURL = "currentUrl";
588 private static final String CURRTITLE = "currentTitle";
589 private static final String CURRWIDTH = "currentWidth";
590 private static final String CURRPICTURE = "currentPicture";
591 private static final String CLOSEONEXIT = "closeonexit";
592 private static final String PARENTTAB = "parentTab";
593 private static final String APPID = "appid";
594 private static final String ORIGINALURL = "originalUrl";
597 * Save the state of all the Tabs.
598 * @param outState The Bundle to save the state to.
600 void saveState(Bundle outState) {
601 final int numTabs = getTabCount();
602 outState.putInt(NUMTABS, numTabs);
603 final int index = getCurrentIndex();
604 outState.putInt(CURRTAB, (index >= 0 && index < numTabs) ? index : 0);
605 for (int i = 0; i < numTabs; i++) {
606 final Tab t = getTab(i);
608 outState.putBundle(WEBVIEW + i, t.mSavedState);
614 * Restore the state of all the tabs.
615 * @param inState The saved state of all the tabs.
616 * @return True if there were previous tabs that were restored. False if
617 * there was no saved state or restoring the state failed.
619 boolean restoreState(Bundle inState) {
620 final int numTabs = (inState == null)
621 ? -1 : inState.getInt(NUMTABS, -1);
625 final int currentTab = inState.getInt(CURRTAB, -1);
626 for (int i = 0; i < numTabs; i++) {
627 if (i == currentTab) {
628 Tab t = createNewTab();
629 // Me must set the current tab before restoring the state
630 // so that all the client classes are set.
632 if (!restoreState(inState.getBundle(WEBVIEW + i), t)) {
633 Log.w(LOGTAG, "Fail in restoreState, load home page.");
634 t.mMainView.loadUrl(BrowserSettings.getInstance()
638 // Create a new tab and don't restore the state yet, add it
640 Tab t = new Tab(null, false, null, null);
641 t.mSavedState = inState.getBundle(WEBVIEW + i);
642 if (t.mSavedState != null) {
643 populatePickerDataFromSavedState(t);
644 // Need to maintain the app id and original url so we
645 // can possibly reuse this tab.
646 t.mAppId = t.mSavedState.getString(APPID);
647 t.mOriginalUrl = t.mSavedState.getString(ORIGINALURL);
653 // Rebuild the tree of tabs. Do this after all tabs have been
654 // created/restored so that the parent tab exists.
655 for (int i = 0; i < numTabs; i++) {
656 final Bundle b = inState.getBundle(WEBVIEW + i);
657 final Tab t = getTab(i);
658 if (b != null && t != null) {
659 final int parentIndex = b.getInt(PARENTTAB, -1);
660 if (parentIndex != -1) {
661 final Tab parent = getTab(parentIndex);
662 if (parent != null) {
663 parent.addChildTab(t);
673 * Free the memory in this order, 1) free the background tab; 2) free the
677 // free the least frequently used background tab
678 Tab t = getLeastUsedTab();
680 Log.w(LOGTAG, "Free a tab in the browser");
687 // free the WebView's unused memory (this includes the cache)
688 Log.w(LOGTAG, "Free WebView's unused memory and cache");
689 WebView view = getCurrentWebView();
697 private Tab getLeastUsedTab() {
698 // Don't do anything if we only have 1 tab.
699 if (getTabCount() == 1) {
703 // Rip through the queue starting at the beginning and teardown the
704 // next available tab.
707 final int queueSize = mTabQueue.size();
708 if (queueSize == 0) {
712 t = mTabQueue.get(i++);
713 } while (i < queueSize && t != null && t.mMainView == null);
715 // Don't do anything if the last remaining tab is the current one or if
716 // the last tab has been freed already.
717 if (t == getCurrentTab() || t.mMainView == null) {
724 private void freeTab(Tab t) {
725 // Store the WebView's state.
728 // Tear down the tab.
730 // Remove the WebView's settings from the BrowserSettings list of
732 BrowserSettings.getInstance().deleteObserver(t.mMainView.getSettings());
733 t.mMainView.destroy();
738 * Create a new subwindow unless a subwindow already exists.
739 * @return True if a new subwindow was created. False if one already exists.
741 void createSubWindow() {
742 Tab t = getTab(mCurrentTab);
743 if (t != null && t.mSubView == null) {
744 final View v = mInflateService.inflate(R.layout.browser_subwindow, null);
745 final WebView w = (WebView) v.findViewById(R.id.webview);
746 w.setMapTrackballToArrowKeys(false); // use trackball directly
747 final SubWindowClient subClient =
748 new SubWindowClient(mActivity.getWebViewClient());
749 final SubWindowChromeClient subChromeClient =
750 new SubWindowChromeClient(t,
751 mActivity.getWebChromeClient());
752 w.setWebViewClient(subClient);
753 w.setWebChromeClient(subChromeClient);
754 w.setDownloadListener(mActivity);
755 w.setOnCreateContextMenuListener(mActivity);
756 final BrowserSettings s = BrowserSettings.getInstance();
757 s.addObserver(w.getSettings()).update(s, null);
759 t.mSubViewClient = subClient;
760 t.mSubViewChromeClient = subChromeClient;
761 // FIXME: I really hate having to know the name of the view
762 // containing the webview.
763 t.mSubViewContainer = v.findViewById(R.id.subwindow_container);
764 final ImageButton cancel =
765 (ImageButton) v.findViewById(R.id.subwindow_close);
766 cancel.setOnClickListener(new OnClickListener() {
767 public void onClick(View v) {
768 subChromeClient.onCloseWindow(w);
775 * Show the tab that contains the given WebView.
776 * @param view The WebView used to find the tab.
778 Tab getTabFromView(WebView view) {
779 final int size = getTabCount();
780 for (int i = 0; i < size; i++) {
781 final Tab t = getTab(i);
782 if (t.mSubView == view || t.mMainView == view) {
790 * Return the tab with the matching application id.
791 * @param id The application identifier.
793 Tab getTabFromId(String id) {
797 final int size = getTabCount();
798 for (int i = 0; i < size; i++) {
799 final Tab t = getTab(i);
800 if (id.equals(t.mAppId)) {
808 * Recreate the main WebView of the given tab. Returns true if the WebView
811 boolean recreateWebView(Tab t, String url) {
812 final WebView w = t.mMainView;
814 if (url != null && url.equals(t.mOriginalUrl)) {
815 // The original url matches the current url. Just go back to the
816 // first history item so we can load it faster than if we
817 // rebuilt the WebView.
818 final WebBackForwardList list = w.copyBackForwardList();
820 w.goBackOrForward(-list.getCurrentIndex());
821 w.clearHistory(); // maintains the current page.
825 // Remove the settings object from the global settings and destroy
827 BrowserSettings.getInstance().deleteObserver(
828 t.mMainView.getSettings());
829 t.mMainView.destroy();
831 // Create a new WebView. If this tab is the current tab, we need to put
832 // back all the clients so force it to be the current tab.
833 t.mMainView = createNewWebView();
834 if (getCurrentTab() == t) {
835 setCurrentTab(t, true);
837 // Clear the saved state except for the app id and close-on-exit
839 t.mSavedState = null;
840 t.mPickerData = null;
841 // Save the new url in order to avoid deleting the WebView.
842 t.mOriginalUrl = url;
847 * Creates a new WebView and registers it with the global settings.
849 private WebView createNewWebView() {
850 // Create a new WebView
851 WebView w = new WebView(mActivity);
852 w.setMapTrackballToArrowKeys(false); // use trackball directly
853 // Enable the built-in zoom
854 w.getSettings().setBuiltInZoomControls(true);
855 // Add this WebView to the settings observer list and update the
857 final BrowserSettings s = BrowserSettings.getInstance();
858 s.addObserver(w.getSettings()).update(s, null);
863 * Put the current tab in the background and set newTab as the current tab.
864 * @param newTab The new tab. If newTab is null, the current tab is not
867 boolean setCurrentTab(Tab newTab) {
868 return setCurrentTab(newTab, false);
871 /*package*/ void pauseCurrentTab() {
872 Tab t = getCurrentTab();
874 t.mMainView.onPause();
875 if (t.mSubView != null) {
876 t.mSubView.onPause();
881 /*package*/ void resumeCurrentTab() {
882 Tab t = getCurrentTab();
884 t.mMainView.onResume();
885 if (t.mSubView != null) {
886 t.mSubView.onResume();
891 private void putViewInForeground(WebView v, WebViewClient vc,
892 WebChromeClient cc) {
893 v.setWebViewClient(vc);
894 v.setWebChromeClient(cc);
895 v.setOnCreateContextMenuListener(mActivity);
896 v.setDownloadListener(mActivity);
900 private void putViewInBackground(WebView v) {
901 // Set an empty callback so that default actions are not triggered.
902 v.setWebViewClient(mEmptyClient);
903 v.setWebChromeClient(mBackgroundChromeClient);
904 v.setOnCreateContextMenuListener(null);
905 // Leave the DownloadManager attached so that downloads can start in
906 // a non-active window. This can happen when going to a site that does
907 // a redirect after a period of time. The user could have switched to
908 // another tab while waiting for the download to start.
909 v.setDownloadListener(mActivity);
914 * If force is true, this method skips the check for newTab == current.
916 private boolean setCurrentTab(Tab newTab, boolean force) {
917 Tab current = getTab(mCurrentTab);
918 if (current == newTab && !force) {
921 if (current != null) {
922 // Remove the current WebView and the container of the subwindow
923 putTabInBackground(current);
926 if (newTab == null) {
930 // Move the newTab to the end of the queue
931 int index = mTabQueue.indexOf(newTab);
933 mTabQueue.remove(index);
935 mTabQueue.add(newTab);
939 // Display the new current tab
940 mCurrentTab = mTabs.indexOf(newTab);
941 mainView = newTab.mMainView;
942 boolean needRestore = (mainView == null);
944 // Same work as in createNewTab() except don't do new Tab()
945 newTab.mMainView = mainView = createNewWebView();
947 putViewInForeground(mainView, mActivity.getWebViewClient(),
948 mActivity.getWebChromeClient());
949 // Add the subwindow if it exists
950 if (newTab.mSubViewContainer != null) {
951 putViewInForeground(newTab.mSubView, newTab.mSubViewClient,
952 newTab.mSubViewChromeClient);
955 // Have to finish setCurrentTab work before calling restoreState
956 if (!restoreState(newTab.mSavedState, newTab)) {
957 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
964 * Put the tab in the background using all the empty/background clients.
966 private void putTabInBackground(Tab t) {
967 putViewInBackground(t.mMainView);
968 if (t.mSubView != null) {
969 putViewInBackground(t.mSubView);
974 * Dismiss the subwindow for the given tab.
976 void dismissSubWindow(Tab t) {
977 if (t != null && t.mSubView != null) {
978 BrowserSettings.getInstance().deleteObserver(
979 t.mSubView.getSettings());
980 t.mSubView.destroy();
982 t.mSubViewContainer = null;
987 * Ensure that Tab t has data to display in the tab picker.
988 * @param t Tab to populate.
990 /* package */ void populatePickerData(Tab t) {
995 // mMainView == null indicates that the tab has been freed.
996 if (t.mMainView == null) {
997 populatePickerDataFromSavedState(t);
1001 // FIXME: The only place we cared about subwindow was for
1002 // bookmarking (i.e. not when saving state). Was this deliberate?
1003 final WebBackForwardList list = t.mMainView.copyBackForwardList();
1004 final WebHistoryItem item =
1005 list != null ? list.getCurrentItem() : null;
1006 populatePickerData(t, item);
1008 // This method is only called during the tab picker creation. At this
1009 // point we need to listen for new pictures since the WebView is still
1011 final WebView w = t.getTopWindow();
1012 w.setPictureListener(t);
1013 // Capture the picture here instead of populatePickerData since it can
1014 // be called when saving the state of a tab.
1015 t.mPickerData.mPicture = w.capturePicture();
1018 // Create the PickerData and populate it using the saved state of the tab.
1019 private void populatePickerDataFromSavedState(Tab t) {
1020 if (t.mSavedState == null) {
1024 final PickerData data = new PickerData();
1025 final Bundle state = t.mSavedState;
1026 data.mUrl = state.getString(CURRURL);
1027 data.mTitle = state.getString(CURRTITLE);
1028 data.mWidth = state.getInt(CURRWIDTH, 0);
1029 // XXX: These keys are from WebView.savePicture so if they change, this
1031 data.mScale = state.getFloat("scale", 1.0f);
1032 data.mScrollX = state.getInt("scrollX", 0);
1033 data.mScrollY = state.getInt("scrollY", 0);
1035 if (state.containsKey(CURRPICTURE)) {
1036 final File f = new File(t.mSavedState.getString(CURRPICTURE));
1038 final FileInputStream in = new FileInputStream(f);
1039 data.mPicture = Picture.createFromStream(in);
1041 } catch (Exception ex) {
1042 // Ignore any problems with inflating the picture. We just
1043 // won't draw anything.
1047 // Set the tab's picker data.
1048 t.mPickerData = data;
1051 // Populate the picker data using the given history item and the current
1053 private void populatePickerData(Tab t, WebHistoryItem item) {
1054 final PickerData data = new PickerData();
1056 data.mUrl = item.getUrl();
1057 data.mTitle = item.getTitle();
1058 if (data.mTitle == null) {
1059 data.mTitle = data.mUrl;
1062 // We want to display the top window in the tab picker but use the url
1063 // and title of the main window.
1064 final WebView w = t.getTopWindow();
1065 data.mWidth = w.getWidth();
1066 data.mScale = w.getScale();
1067 data.mScrollX = w.getScrollX();
1068 data.mScrollY = w.getScrollY();
1069 t.mPickerData = data;
1073 * Clean up the data for all tabs.
1075 /* package */ void wipeAllPickerData() {
1076 int size = getTabCount();
1077 for (int i = 0; i < size; i++) {
1078 final Tab t = getTab(i);
1079 if (t != null && t.mSavedState == null) {
1080 t.mPickerData = null;
1082 if (t.mMainView != null) {
1083 // Clear the picture listeners.
1084 t.mMainView.setPictureListener(null);
1085 if (t.mSubView != null) {
1086 t.mSubView.setPictureListener(null);
1093 * Save the state for an individual tab.
1095 private boolean saveState(Tab t) {
1097 final WebView w = t.mMainView;
1098 // If the WebView is null it means we ran low on memory and we
1099 // already stored the saved state in mSavedState.
1103 final Bundle b = new Bundle();
1104 final WebBackForwardList list = w.saveState(b);
1106 final File f = new File(mThumbnailDir, w.hashCode()
1108 if (w.savePicture(b, f)) {
1109 b.putString(CURRPICTURE, f.getPath());
1113 // Store some extra info for displaying the tab in the picker.
1114 final WebHistoryItem item =
1115 list != null ? list.getCurrentItem() : null;
1116 populatePickerData(t, item);
1118 // XXX: WebView.savePicture stores the scale and scroll positions
1119 // in the bundle so we don't have to do it here.
1120 final PickerData data = t.mPickerData;
1121 if (data.mUrl != null) {
1122 b.putString(CURRURL, data.mUrl);
1124 if (data.mTitle != null) {
1125 b.putString(CURRTITLE, data.mTitle);
1127 b.putInt(CURRWIDTH, data.mWidth);
1128 b.putBoolean(CLOSEONEXIT, t.mCloseOnExit);
1129 if (t.mAppId != null) {
1130 b.putString(APPID, t.mAppId);
1132 if (t.mOriginalUrl != null) {
1133 b.putString(ORIGINALURL, t.mOriginalUrl);
1136 // Remember the parent tab so the relationship can be restored.
1137 if (t.mParentTab != null) {
1138 b.putInt(PARENTTAB, getTabIndex(t.mParentTab));
1141 // Remember the saved state.
1149 * Restore the state of the tab.
1151 private boolean restoreState(Bundle b, Tab t) {
1155 // Restore the internal state even if the WebView fails to restore.
1156 // This will maintain the app id, original url and close-on-exit values.
1157 t.mSavedState = null;
1158 t.mPickerData = null;
1159 t.mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1160 t.mAppId = b.getString(APPID);
1161 t.mOriginalUrl = b.getString(ORIGINALURL);
1163 final WebView w = t.mMainView;
1164 final WebBackForwardList list = w.restoreState(b);
1168 if (b.containsKey(CURRPICTURE)) {
1169 final File f = new File(b.getString(CURRPICTURE));
1170 w.restorePicture(b, f);