OSDN Git Service

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