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;
41 import android.widget.LinearLayout;
44 import java.io.FileInputStream;
45 import java.util.ArrayList;
46 import java.util.Vector;
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 =
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.
72 private static class SubWindowClient extends WebViewClient {
73 // The main WebViewClient.
74 private final WebViewClient mClient;
76 SubWindowClient(WebViewClient client) {
80 public void doUpdateVisitedHistory(WebView view, String url,
82 mClient.doUpdateVisitedHistory(view, url, isReload);
85 public boolean shouldOverrideUrlLoading(WebView view, String url) {
86 return mClient.shouldOverrideUrlLoading(view, url);
89 public void onReceivedSslError(WebView view, SslErrorHandler handler,
91 mClient.onReceivedSslError(view, handler, error);
94 public void onReceivedHttpAuthRequest(WebView view,
95 HttpAuthHandler handler, String host, String realm) {
96 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
99 public void onFormResubmission(WebView view, Message dontResend,
101 mClient.onFormResubmission(view, dontResend, resend);
104 public void onReceivedError(WebView view, int errorCode,
105 String description, String failingUrl) {
106 mClient.onReceivedError(view, errorCode, description, failingUrl);
109 public boolean shouldOverrideKeyEvent(WebView view,
110 android.view.KeyEvent event) {
111 return mClient.shouldOverrideKeyEvent(view, event);
114 public void onUnhandledKeyEvent(WebView view,
115 android.view.KeyEvent event) {
116 mClient.onUnhandledKeyEvent(view, event);
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;
126 SubWindowChromeClient(Tab t, WebChromeClient client) {
131 public void onProgressChanged(WebView view, int newProgress) {
132 mClient.onProgressChanged(view, newProgress);
135 public boolean onCreateWindow(WebView view, boolean dialog,
136 boolean userGesture, android.os.Message resultMsg) {
137 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
140 public void onCloseWindow(WebView window) {
141 if (Browser.DEBUG && window != mTab.mSubView) {
142 throw new AssertionError("Can't close the window");
144 mActivity.dismissSubWindow(mTab);
147 // Background WebChromeClient for focusing tabs
148 private class BackgroundChromeClient extends WebChromeClient {
150 public void onRequestFocus(WebView view) {
151 Tab t = getTabFromView(view);
152 if (t != getCurrentTab()) {
153 mActivity.showTab(t);
158 // Extra saved information for displaying the tab in the picker.
159 public static class PickerData {
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;
173 * Private class for maintaining Tabs with a main WebView and a subwindow.
175 public class Tab implements WebView.PictureListener {
176 // The Geolocation permissions prompt
177 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
178 private View mContainer;
180 private WebView mMainView;
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
192 private Bundle mSavedState;
193 // Data used when displaying the tab in the picker.
194 private PickerData mPickerData;
196 // 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
202 private Vector<Tab> mChildTabs;
204 private Boolean mCloseOnExit;
205 // Application identifier used to find tabs that another application
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;
212 private ErrorConsoleView mErrorConsole;
214 // Construct a new tab
215 private Tab(WebView w, boolean closeOnExit, String appId, String url, Context context) {
216 mCloseOnExit = closeOnExit;
220 // The tab consists of a container view, which contains the main
221 // WebView, as well as any other UI elements associated with the tab.
223 // FIXME: Fix the interaction between this layout and the animation
224 // used when switching to and from the tab picker. This may not be
225 // required if the tab selection UI is redesigned.
226 LayoutInflater factory = LayoutInflater.from(context);
227 mContainer = factory.inflate(R.layout.tab, null);
229 mGeolocationPermissionsPrompt =
230 (GeolocationPermissionsPrompt) mContainer.findViewById(
231 R.id.geolocation_permissions_prompt);
237 * Sets the WebView for this tab, correctly removing the old WebView
238 * from, and inserting the new WebView into, the container view.
240 public void setWebView(WebView w) {
241 if (mMainView == w) {
244 // If the WebView is changing, the page will be reloaded, so any ongoing Geolocation
245 // permission requests are void.
246 mGeolocationPermissionsPrompt.hide();
248 FrameLayout wrapper = (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
249 if (mMainView != null) {
250 wrapper.removeView(mMainView);
253 if (mMainView != null) {
254 wrapper.addView(mMainView);
259 * Return the top window of this tab; either the subwindow if it is not
260 * null or the main window.
261 * @return The top window of this tab.
263 public WebView getTopWindow() {
264 if (mSubView != null) {
271 * Return the main window of this tab. Note: if a tab is freed in the
272 * background, this can return null. It is only guaranteed to be
273 * non-null for the current tab.
274 * @return The main WebView of this tab.
276 public WebView getWebView() {
281 * @return The container for this tab.
283 public View getContainer() {
288 * @return The geolocation permissions prompt for this tab.
290 public GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
291 return mGeolocationPermissionsPrompt;
295 * Return the subwindow of this tab or null if there is no subwindow.
296 * @return The subwindow of this tab or null.
298 public WebView getSubWebView() {
303 * Return the subwindow container of this tab or null if there is no
305 * @return The subwindow's container View.
307 public View getSubWebViewContainer() {
308 return mSubViewContainer;
312 * Get the url of this tab. Valid after calling populatePickerData, but
313 * before calling wipePickerData, or if the webview has been destroyed.
315 * @return The WebView's url or null.
317 public String getUrl() {
318 if (mPickerData != null) {
319 return mPickerData.mUrl;
325 * Get the title of this tab. Valid after calling populatePickerData,
326 * but before calling wipePickerData, or if the webview has been
327 * destroyed. If the url has no title, use the url instead.
329 * @return The WebView's title (or url) or null.
331 public String getTitle() {
332 if (mPickerData != null) {
333 return mPickerData.mTitle;
339 * Returns the picker data.
341 public PickerData getPickerData() {
345 private void setParentTab(Tab parent) {
347 // This tab may have been freed due to low memory. If that is the
348 // case, the parent tab index is already saved. If we are changing
349 // that index (most likely due to removing the parent tab) we must
350 // update the parent tab index in the saved Bundle.
351 if (mSavedState != null) {
352 if (parent == null) {
353 mSavedState.remove(PARENTTAB);
355 mSavedState.putInt(PARENTTAB, getTabIndex(parent));
361 * When a Tab is created through the content of another Tab, then
362 * we associate the Tabs.
363 * @param child the Tab that was created from this Tab
365 public void addChildTab(Tab child) {
366 if (mChildTabs == null) {
367 mChildTabs = new Vector<Tab>();
369 mChildTabs.add(child);
370 child.setParentTab(this);
373 private void removeFromTree() {
374 // detach the children
375 if (mChildTabs != null) {
376 for(Tab t : mChildTabs) {
377 t.setParentTab(null);
381 // Find myself in my parent list
382 if (mParentTab != null) {
383 mParentTab.mChildTabs.remove(this);
388 * If this Tab was created through another Tab, then this method
390 * @return the Tab parent or null
392 public Tab getParentTab() {
397 * Return whether this tab should be closed when it is backing out of
399 * @return TRUE if this tab should be closed when exit.
401 public boolean closeOnExit() {
405 public void onNewPicture(WebView view, Picture p) {
406 if (mPickerData == null) {
410 mPickerData.mPicture = p;
411 // Tell the FakeWebView to redraw.
412 if (mPickerData.mFakeWebView != null) {
413 mPickerData.mFakeWebView.invalidate();
418 // Directory to store thumbnails for each WebView.
419 private final File mThumbnailDir;
422 * Construct a new TabControl object that interfaces with the given
423 * BrowserActivity instance.
424 * @param activity A BrowserActivity instance that TabControl will interface
427 TabControl(BrowserActivity activity) {
428 mActivity = activity;
430 ((LayoutInflater) activity.getSystemService(
431 Context.LAYOUT_INFLATER_SERVICE));
432 mThumbnailDir = activity.getDir("thumbnails", 0);
435 File getThumbnailDir() {
436 return mThumbnailDir;
439 BrowserActivity getBrowserActivity() {
444 * Return the current tab's main WebView. This will always return the main
445 * WebView for a given tab and not a subwindow.
446 * @return The current tab's WebView.
448 WebView getCurrentWebView() {
449 Tab t = getTab(mCurrentTab);
457 * Return the current tab's error console. Creates the console if createIfNEcessary
458 * is true and we haven't already created the console.
459 * @param createIfNecessary Flag to indicate if the console should be created if it has
461 * @return The current tab's error console, or null if one has not been created and
462 * createIfNecessary is false.
464 ErrorConsoleView getCurrentErrorConsole(boolean createIfNecessary) {
465 Tab t = getTab(mCurrentTab);
470 if (createIfNecessary && t.mErrorConsole == null) {
471 t.mErrorConsole = new ErrorConsoleView(mActivity);
472 t.mErrorConsole.setWebView(t.mMainView);
475 return t.mErrorConsole;
479 * Return the current tab's top-level WebView. This can return a subwindow
481 * @return The top-level WebView of the current tab.
483 WebView getCurrentTopWebView() {
484 Tab t = getTab(mCurrentTab);
488 return t.mSubView != null ? t.mSubView : t.mMainView;
492 * Return the current tab's subwindow if it exists.
493 * @return The subwindow of the current tab or null if it doesn't exist.
495 WebView getCurrentSubWindow() {
496 Tab t = getTab(mCurrentTab);
504 * Return the tab at the specified index.
505 * @return The Tab for the specified index or null if the tab does not
508 Tab getTab(int index) {
509 if (index >= 0 && index < mTabs.size()) {
510 return mTabs.get(index);
516 * Return the current tab.
517 * @return The current tab.
519 Tab getCurrentTab() {
520 return getTab(mCurrentTab);
524 * Return the current tab index.
525 * @return The current tab index
527 int getCurrentIndex() {
532 * Given a Tab, find it's index
534 * @return index of Tab or -1 if not found
536 int getTabIndex(Tab tab) {
540 return mTabs.indexOf(tab);
545 * @return The newly createTab or null if we have reached the maximum
546 * number of open tabs.
548 Tab createNewTab(boolean closeOnExit, String appId, String url) {
549 int size = mTabs.size();
550 // Return false if we have maxed out on tabs
551 if (MAX_TABS == size) {
554 final WebView w = createNewWebView();
556 // Create a new tab and add it to the tab list
557 Tab t = new Tab(w, closeOnExit, appId, url, mActivity);
559 // Initially put the tab in the background.
560 putTabInBackground(t);
565 * Create a new tab with default values for closeOnExit(false),
566 * appId(null), and url(null).
569 return createNewTab(false, null, null);
573 * Remove the tab from the list. If the tab is the current tab shown, the
574 * last created tab will be shown.
575 * @param t The tab to be removed.
577 boolean removeTab(Tab t) {
581 // Only remove the tab if it is the current one.
582 if (getCurrentTab() == t) {
583 putTabInBackground(t);
586 // Only destroy the WebView if it still exists.
587 if (t.mMainView != null) {
588 // Take down the sub window.
590 // Remove the WebView's settings from the BrowserSettings list of
592 BrowserSettings.getInstance().deleteObserver(
593 t.mMainView.getSettings());
594 // Destroy the main view and subview
595 t.mMainView.destroy();
598 // clear it's references to parent and children
601 // Remove it from our list of tabs.
604 // The tab indices have shifted, update all the saved state so we point
605 // to the correct index.
606 for (Tab tab : mTabs) {
607 if (tab.mChildTabs != null) {
608 for (Tab child : tab.mChildTabs) {
609 child.setParentTab(tab);
615 // This tab may have been pushed in to the background and then closed.
616 // If the saved state contains a picture file, delete the file.
617 if (t.mSavedState != null) {
618 if (t.mSavedState.containsKey(CURRPICTURE)) {
619 new File(t.mSavedState.getString(CURRPICTURE)).delete();
623 // Remove it from the queue of viewed tabs.
630 * Clear the back/forward list for all the current tabs.
632 void clearHistory() {
633 int size = getTabCount();
634 for (int i = 0; i < size; i++) {
635 Tab t = mTabs.get(i);
636 // TODO: if a tab is freed due to low memory, its history is not
638 if (t.mMainView != null) {
639 t.mMainView.clearHistory();
641 if (t.mSubView != null) {
642 t.mSubView.clearHistory();
648 * Destroy all the tabs and subwindows
651 BrowserSettings s = BrowserSettings.getInstance();
652 for (Tab t : mTabs) {
653 if (t.mMainView != null) {
655 s.deleteObserver(t.mMainView.getSettings());
656 t.mMainView.destroy();
665 * Returns the number of tabs created.
666 * @return The number of tabs created.
672 // Used for saving and restoring each Tab
673 private static final String WEBVIEW = "webview";
674 private static final String NUMTABS = "numTabs";
675 private static final String CURRTAB = "currentTab";
676 private static final String CURRURL = "currentUrl";
677 private static final String CURRTITLE = "currentTitle";
678 private static final String CURRWIDTH = "currentWidth";
679 private static final String CURRPICTURE = "currentPicture";
680 private static final String CLOSEONEXIT = "closeonexit";
681 private static final String PARENTTAB = "parentTab";
682 private static final String APPID = "appid";
683 private static final String ORIGINALURL = "originalUrl";
686 * Save the state of all the Tabs.
687 * @param outState The Bundle to save the state to.
689 void saveState(Bundle outState) {
690 final int numTabs = getTabCount();
691 outState.putInt(NUMTABS, numTabs);
692 final int index = getCurrentIndex();
693 outState.putInt(CURRTAB, (index >= 0 && index < numTabs) ? index : 0);
694 for (int i = 0; i < numTabs; i++) {
695 final Tab t = getTab(i);
697 outState.putBundle(WEBVIEW + i, t.mSavedState);
703 * Restore the state of all the tabs.
704 * @param inState The saved state of all the tabs.
705 * @return True if there were previous tabs that were restored. False if
706 * there was no saved state or restoring the state failed.
708 boolean restoreState(Bundle inState) {
709 final int numTabs = (inState == null)
710 ? -1 : inState.getInt(NUMTABS, -1);
714 final int currentTab = inState.getInt(CURRTAB, -1);
715 for (int i = 0; i < numTabs; i++) {
716 if (i == currentTab) {
717 Tab t = createNewTab();
718 // Me must set the current tab before restoring the state
719 // so that all the client classes are set.
721 if (!restoreState(inState.getBundle(WEBVIEW + i), t)) {
722 Log.w(LOGTAG, "Fail in restoreState, load home page.");
723 t.mMainView.loadUrl(BrowserSettings.getInstance()
727 // Create a new tab and don't restore the state yet, add it
729 Tab t = new Tab(null, false, null, null, mActivity);
730 t.mSavedState = inState.getBundle(WEBVIEW + i);
731 if (t.mSavedState != null) {
732 populatePickerDataFromSavedState(t);
733 // Need to maintain the app id and original url so we
734 // can possibly reuse this tab.
735 t.mAppId = t.mSavedState.getString(APPID);
736 t.mOriginalUrl = t.mSavedState.getString(ORIGINALURL);
742 // Rebuild the tree of tabs. Do this after all tabs have been
743 // created/restored so that the parent tab exists.
744 for (int i = 0; i < numTabs; i++) {
745 final Bundle b = inState.getBundle(WEBVIEW + i);
746 final Tab t = getTab(i);
747 if (b != null && t != null) {
748 final int parentIndex = b.getInt(PARENTTAB, -1);
749 if (parentIndex != -1) {
750 final Tab parent = getTab(parentIndex);
751 if (parent != null) {
752 parent.addChildTab(t);
762 * Free the memory in this order, 1) free the background tab; 2) free the
766 if (getTabCount() == 0) return;
768 // free the least frequently used background tab
769 Tab t = getLeastUsedTab(getCurrentTab().getParentTab());
771 Log.w(LOGTAG, "Free a tab in the browser");
778 // free the WebView's unused memory (this includes the cache)
779 Log.w(LOGTAG, "Free WebView's unused memory and cache");
780 WebView view = getCurrentWebView();
788 private Tab getLeastUsedTab(Tab currentParent) {
789 // Don't do anything if we only have 1 tab.
790 if (getTabCount() == 1) {
794 // Rip through the queue starting at the beginning and teardown the
795 // next available tab.
798 final int queueSize = mTabQueue.size();
799 if (queueSize == 0) {
803 t = mTabQueue.get(i++);
804 } while (i < queueSize
805 && ((t != null && t.mMainView == null) || t == currentParent));
807 // Don't do anything if the last remaining tab is the current one or if
808 // the last tab has been freed already.
809 if (t == getCurrentTab() || t.mMainView == null) {
816 private void freeTab(Tab t) {
817 // Store the WebView's state.
820 // Tear down the tab.
822 // Remove the WebView's settings from the BrowserSettings list of
824 BrowserSettings.getInstance().deleteObserver(t.mMainView.getSettings());
825 t.mMainView.destroy();
830 * Create a new subwindow unless a subwindow already exists.
831 * @return True if a new subwindow was created. False if one already exists.
833 void createSubWindow() {
834 Tab t = getTab(mCurrentTab);
835 if (t != null && t.mSubView == null) {
836 final View v = mInflateService.inflate(R.layout.browser_subwindow, null);
837 final WebView w = (WebView) v.findViewById(R.id.webview);
838 w.setMapTrackballToArrowKeys(false); // use trackball directly
839 final SubWindowClient subClient =
840 new SubWindowClient(mActivity.getWebViewClient());
841 final SubWindowChromeClient subChromeClient =
842 new SubWindowChromeClient(t,
843 mActivity.getWebChromeClient());
844 w.setWebViewClient(subClient);
845 w.setWebChromeClient(subChromeClient);
846 w.setDownloadListener(mActivity);
847 w.setOnCreateContextMenuListener(mActivity);
848 final BrowserSettings s = BrowserSettings.getInstance();
849 s.addObserver(w.getSettings()).update(s, null);
851 t.mSubViewClient = subClient;
852 t.mSubViewChromeClient = subChromeClient;
853 // FIXME: I really hate having to know the name of the view
854 // containing the webview.
855 t.mSubViewContainer = v.findViewById(R.id.subwindow_container);
856 final ImageButton cancel =
857 (ImageButton) v.findViewById(R.id.subwindow_close);
858 cancel.setOnClickListener(new OnClickListener() {
859 public void onClick(View v) {
860 subChromeClient.onCloseWindow(w);
867 * Show the tab that contains the given WebView.
868 * @param view The WebView used to find the tab.
870 Tab getTabFromView(WebView view) {
871 final int size = getTabCount();
872 for (int i = 0; i < size; i++) {
873 final Tab t = getTab(i);
874 if (t.mSubView == view || t.mMainView == view) {
882 * Return the tab with the matching application id.
883 * @param id The application identifier.
885 Tab getTabFromId(String id) {
889 final int size = getTabCount();
890 for (int i = 0; i < size; i++) {
891 final Tab t = getTab(i);
892 if (id.equals(t.mAppId)) {
899 // This method checks if a non-app tab (one created within the browser)
900 // matches the given url.
901 private boolean tabMatchesUrl(Tab t, String url) {
902 if (t.mAppId != null) {
904 } else if (t.mMainView == null) {
906 } else if (url.equals(t.mMainView.getUrl()) ||
907 url.equals(t.mMainView.getOriginalUrl())) {
914 * Return the tab that has no app id associated with it and the url of the
915 * tab matches the given url.
916 * @param url The url to search for.
918 Tab findUnusedTabWithUrl(String url) {
922 // Check the current tab first.
923 Tab t = getCurrentTab();
924 if (t != null && tabMatchesUrl(t, url)) {
927 // Now check all the rest.
928 final int size = getTabCount();
929 for (int i = 0; i < size; i++) {
931 if (tabMatchesUrl(t, url)) {
939 * Recreate the main WebView of the given tab. Returns true if the WebView
942 boolean recreateWebView(Tab t, String url) {
943 final WebView w = t.mMainView;
945 if (url != null && url.equals(t.mOriginalUrl)) {
946 // The original url matches the current url. Just go back to the
947 // first history item so we can load it faster than if we
948 // rebuilt the WebView.
949 final WebBackForwardList list = w.copyBackForwardList();
951 w.goBackOrForward(-list.getCurrentIndex());
952 w.clearHistory(); // maintains the current page.
956 // Remove the settings object from the global settings and destroy
958 BrowserSettings.getInstance().deleteObserver(
959 t.mMainView.getSettings());
960 t.mMainView.destroy();
962 // Create a new WebView. If this tab is the current tab, we need to put
963 // back all the clients so force it to be the current tab.
964 t.setWebView(createNewWebView());
965 if (getCurrentTab() == t) {
966 setCurrentTab(t, true);
968 // Clear the saved state except for the app id and close-on-exit
970 t.mSavedState = null;
971 t.mPickerData = null;
972 // Save the new url in order to avoid deleting the WebView.
973 t.mOriginalUrl = url;
978 * Creates a new WebView and registers it with the global settings.
980 private WebView createNewWebView() {
981 // Create a new WebView
982 WebView w = new WebView(mActivity);
983 w.setMapTrackballToArrowKeys(false); // use trackball directly
984 // Enable the built-in zoom
985 w.getSettings().setBuiltInZoomControls(true);
986 // Add this WebView to the settings observer list and update the
988 final BrowserSettings s = BrowserSettings.getInstance();
989 s.addObserver(w.getSettings()).update(s, null);
994 * Put the current tab in the background and set newTab as the current tab.
995 * @param newTab The new tab. If newTab is null, the current tab is not
998 boolean setCurrentTab(Tab newTab) {
999 return setCurrentTab(newTab, false);
1002 /*package*/ void pauseCurrentTab() {
1003 Tab t = getCurrentTab();
1005 t.mMainView.onPause();
1006 if (t.mSubView != null) {
1007 t.mSubView.onPause();
1012 /*package*/ void resumeCurrentTab() {
1013 Tab t = getCurrentTab();
1015 t.mMainView.onResume();
1016 if (t.mSubView != null) {
1017 t.mSubView.onResume();
1022 private void putViewInForeground(WebView v, WebViewClient vc,
1023 WebChromeClient cc) {
1024 v.setWebViewClient(vc);
1025 v.setWebChromeClient(cc);
1026 v.setOnCreateContextMenuListener(mActivity);
1027 v.setDownloadListener(mActivity);
1031 private void putViewInBackground(WebView v) {
1032 // Set an empty callback so that default actions are not triggered.
1033 v.setWebViewClient(mEmptyClient);
1034 v.setWebChromeClient(mBackgroundChromeClient);
1035 v.setOnCreateContextMenuListener(null);
1036 // Leave the DownloadManager attached so that downloads can start in
1037 // a non-active window. This can happen when going to a site that does
1038 // a redirect after a period of time. The user could have switched to
1039 // another tab while waiting for the download to start.
1040 v.setDownloadListener(mActivity);
1045 * If force is true, this method skips the check for newTab == current.
1047 private boolean setCurrentTab(Tab newTab, boolean force) {
1048 Tab current = getTab(mCurrentTab);
1049 if (current == newTab && !force) {
1052 if (current != null) {
1053 // Remove the current WebView and the container of the subwindow
1054 putTabInBackground(current);
1057 if (newTab == null) {
1061 // Move the newTab to the end of the queue
1062 int index = mTabQueue.indexOf(newTab);
1064 mTabQueue.remove(index);
1066 mTabQueue.add(newTab);
1070 // Display the new current tab
1071 mCurrentTab = mTabs.indexOf(newTab);
1072 mainView = newTab.mMainView;
1073 boolean needRestore = (mainView == null);
1075 // Same work as in createNewTab() except don't do new Tab()
1076 mainView = createNewWebView();
1077 newTab.setWebView(mainView);
1079 putViewInForeground(mainView, mActivity.getWebViewClient(),
1080 mActivity.getWebChromeClient());
1081 // Add the subwindow if it exists
1082 if (newTab.mSubViewContainer != null) {
1083 putViewInForeground(newTab.mSubView, newTab.mSubViewClient,
1084 newTab.mSubViewChromeClient);
1087 // Have to finish setCurrentTab work before calling restoreState
1088 if (!restoreState(newTab.mSavedState, newTab)) {
1089 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
1096 * Put the tab in the background using all the empty/background clients.
1098 private void putTabInBackground(Tab t) {
1099 putViewInBackground(t.mMainView);
1100 if (t.mSubView != null) {
1101 putViewInBackground(t.mSubView);
1106 * Dismiss the subwindow for the given tab.
1108 void dismissSubWindow(Tab t) {
1109 if (t != null && t.mSubView != null) {
1110 BrowserSettings.getInstance().deleteObserver(
1111 t.mSubView.getSettings());
1112 t.mSubView.destroy();
1114 t.mSubViewContainer = null;
1119 * Ensure that Tab t has data to display in the tab picker.
1120 * @param t Tab to populate.
1122 /* package */ void populatePickerData(Tab t) {
1127 // mMainView == null indicates that the tab has been freed.
1128 if (t.mMainView == null) {
1129 populatePickerDataFromSavedState(t);
1133 // FIXME: The only place we cared about subwindow was for
1134 // bookmarking (i.e. not when saving state). Was this deliberate?
1135 final WebBackForwardList list = t.mMainView.copyBackForwardList();
1136 final WebHistoryItem item =
1137 list != null ? list.getCurrentItem() : null;
1138 populatePickerData(t, item);
1140 // This method is only called during the tab picker creation. At this
1141 // point we need to listen for new pictures since the WebView is still
1143 final WebView w = t.getTopWindow();
1144 w.setPictureListener(t);
1145 // Capture the picture here instead of populatePickerData since it can
1146 // be called when saving the state of a tab.
1147 t.mPickerData.mPicture = w.capturePicture();
1150 // Create the PickerData and populate it using the saved state of the tab.
1151 private void populatePickerDataFromSavedState(Tab t) {
1152 if (t.mSavedState == null) {
1156 final PickerData data = new PickerData();
1157 final Bundle state = t.mSavedState;
1158 data.mUrl = state.getString(CURRURL);
1159 data.mTitle = state.getString(CURRTITLE);
1160 data.mWidth = state.getInt(CURRWIDTH, 0);
1161 // XXX: These keys are from WebView.savePicture so if they change, this
1163 data.mScale = state.getFloat("scale", 1.0f);
1164 data.mScrollX = state.getInt("scrollX", 0);
1165 data.mScrollY = state.getInt("scrollY", 0);
1167 if (state.containsKey(CURRPICTURE)) {
1168 final File f = new File(t.mSavedState.getString(CURRPICTURE));
1170 final FileInputStream in = new FileInputStream(f);
1171 data.mPicture = Picture.createFromStream(in);
1173 } catch (Exception ex) {
1174 // Ignore any problems with inflating the picture. We just
1175 // won't draw anything.
1179 // Set the tab's picker data.
1180 t.mPickerData = data;
1183 // Populate the picker data using the given history item and the current
1185 private void populatePickerData(Tab t, WebHistoryItem item) {
1186 final PickerData data = new PickerData();
1188 data.mUrl = item.getUrl();
1189 data.mTitle = item.getTitle();
1190 if (data.mTitle == null) {
1191 data.mTitle = data.mUrl;
1194 // We want to display the top window in the tab picker but use the url
1195 // and title of the main window.
1196 final WebView w = t.getTopWindow();
1197 data.mWidth = w.getWidth();
1198 data.mScale = w.getScale();
1199 data.mScrollX = w.getScrollX();
1200 data.mScrollY = w.getScrollY();
1202 // Remember the old picture if possible.
1203 if (t.mPickerData != null) {
1204 data.mPicture = t.mPickerData.mPicture;
1206 t.mPickerData = data;
1210 * Clean up the data for all tabs.
1212 /* package */ void wipeAllPickerData() {
1213 int size = getTabCount();
1214 for (int i = 0; i < size; i++) {
1215 final Tab t = getTab(i);
1216 if (t != null && t.mSavedState == null) {
1217 t.mPickerData = null;
1219 if (t.mMainView != null) {
1220 // Clear the picture listeners.
1221 t.mMainView.setPictureListener(null);
1222 if (t.mSubView != null) {
1223 t.mSubView.setPictureListener(null);
1230 * Save the state for an individual tab.
1232 private boolean saveState(Tab t) {
1234 final WebView w = t.mMainView;
1235 // If the WebView is null it means we ran low on memory and we
1236 // already stored the saved state in mSavedState.
1240 final Bundle b = new Bundle();
1241 final WebBackForwardList list = w.saveState(b);
1243 final File f = new File(mThumbnailDir, w.hashCode()
1245 if (w.savePicture(b, f)) {
1246 b.putString(CURRPICTURE, f.getPath());
1250 // Store some extra info for displaying the tab in the picker.
1251 final WebHistoryItem item =
1252 list != null ? list.getCurrentItem() : null;
1253 populatePickerData(t, item);
1255 // XXX: WebView.savePicture stores the scale and scroll positions
1256 // in the bundle so we don't have to do it here.
1257 final PickerData data = t.mPickerData;
1258 if (data.mUrl != null) {
1259 b.putString(CURRURL, data.mUrl);
1261 if (data.mTitle != null) {
1262 b.putString(CURRTITLE, data.mTitle);
1264 b.putInt(CURRWIDTH, data.mWidth);
1265 b.putBoolean(CLOSEONEXIT, t.mCloseOnExit);
1266 if (t.mAppId != null) {
1267 b.putString(APPID, t.mAppId);
1269 if (t.mOriginalUrl != null) {
1270 b.putString(ORIGINALURL, t.mOriginalUrl);
1273 // Remember the parent tab so the relationship can be restored.
1274 if (t.mParentTab != null) {
1275 b.putInt(PARENTTAB, getTabIndex(t.mParentTab));
1278 // Remember the saved state.
1286 * Restore the state of the tab.
1288 private boolean restoreState(Bundle b, Tab t) {
1292 // Restore the internal state even if the WebView fails to restore.
1293 // This will maintain the app id, original url and close-on-exit values.
1294 t.mSavedState = null;
1295 t.mPickerData = null;
1296 t.mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1297 t.mAppId = b.getString(APPID);
1298 t.mOriginalUrl = b.getString(ORIGINALURL);
1300 final WebView w = t.mMainView;
1301 final WebBackForwardList list = w.restoreState(b);
1305 if (b.containsKey(CURRPICTURE)) {
1306 final File f = new File(b.getString(CURRPICTURE));
1307 w.restorePicture(b, f);