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.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;
49 import java.io.FileInputStream;
50 import java.util.ArrayList;
51 import java.util.Vector;
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 =
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;
81 SubWindowClient(WebViewClient client) {
85 public void doUpdateVisitedHistory(WebView view, String url,
87 mClient.doUpdateVisitedHistory(view, url, isReload);
90 public boolean shouldOverrideUrlLoading(WebView view, String url) {
91 return mClient.shouldOverrideUrlLoading(view, url);
94 public void onReceivedSslError(WebView view, SslErrorHandler handler,
96 mClient.onReceivedSslError(view, handler, error);
99 public void onReceivedHttpAuthRequest(WebView view,
100 HttpAuthHandler handler, String host, String realm) {
101 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
104 public void onFormResubmission(WebView view, Message dontResend,
106 mClient.onFormResubmission(view, dontResend, resend);
109 public void onReceivedError(WebView view, int errorCode,
110 String description, String failingUrl) {
111 mClient.onReceivedError(view, errorCode, description, failingUrl);
114 public boolean shouldOverrideKeyEvent(WebView view,
115 android.view.KeyEvent event) {
116 return mClient.shouldOverrideKeyEvent(view, event);
119 public void onUnhandledKeyEvent(WebView view,
120 android.view.KeyEvent event) {
121 mClient.onUnhandledKeyEvent(view, event);
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;
131 SubWindowChromeClient(Tab t, WebChromeClient client) {
136 public void onProgressChanged(WebView view, int newProgress) {
137 mClient.onProgressChanged(view, newProgress);
140 public boolean onCreateWindow(WebView view, boolean dialog,
141 boolean userGesture, android.os.Message resultMsg) {
142 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
145 public void onCloseWindow(WebView window) {
146 if (Browser.DEBUG && window != mTab.mSubView) {
147 throw new AssertionError("Can't close the window");
149 mActivity.dismissSubWindow(mTab);
152 // Background WebChromeClient for focusing tabs
153 private class BackgroundChromeClient extends WebChromeClient {
155 public void onRequestFocus(WebView view) {
156 Tab t = getTabFromView(view);
157 if (t != getCurrentTab()) {
158 mActivity.switchToTab(getTabIndex(t));
163 // Extra saved information for displaying the tab in the picker.
164 public static class PickerData {
174 * Private class for maintaining Tabs with a main WebView and a subwindow.
177 // The Geolocation permissions prompt
178 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
179 private View mContainer;
181 private WebView mMainView;
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
193 private Bundle mSavedState;
194 // Data used when displaying the tab in the picker.
195 private PickerData mPickerData;
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
203 private Vector<Tab> mChildTabs;
205 private Boolean mCloseOnExit;
206 // Application identifier used to find tabs that another application
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;
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;
218 // Construct a new tab
219 private Tab(WebView w, boolean closeOnExit, String appId, String url, Context context) {
220 mCloseOnExit = closeOnExit;
223 mSavedLockIconType = BrowserActivity.LOCK_ICON_UNSECURE;
224 mSavedPrevLockIconType = BrowserActivity.LOCK_ICON_UNSECURE;
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);
231 mGeolocationPermissionsPrompt =
232 (GeolocationPermissionsPrompt) mContainer.findViewById(
233 R.id.geolocation_permissions_prompt);
239 * Sets the WebView for this tab, correctly removing the old WebView
240 * from the container view.
242 public void setWebView(WebView w) {
243 if (mMainView == w) {
246 // If the WebView is changing, the page will be reloaded, so any ongoing Geolocation
247 // permission requests are void.
248 mGeolocationPermissionsPrompt.hide();
250 // Just remove the old one.
251 FrameLayout wrapper =
252 (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
253 wrapper.removeView(mMainView);
258 * This method attaches both the WebView and any sub window to the
259 * given content view.
261 public void attachTabToContentView(ViewGroup content) {
262 if (mMainView == null) {
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);
276 * Remove the WebView and any sub window from the given content view.
278 public void removeTabFromContentView(ViewGroup content) {
279 if (mMainView == null) {
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);
294 * Attach the sub window to the content view.
296 public void attachSubWindow(ViewGroup content) {
297 if (mSubView != null) {
298 content.addView(mSubViewContainer,
299 BrowserActivity.COVER_SCREEN_PARAMS);
304 * Remove the sub window from the content view.
306 public void removeSubWindow(ViewGroup content) {
307 if (mSubView != null) {
308 content.removeView(mSubViewContainer);
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.
317 public WebView getTopWindow() {
318 if (mSubView != null) {
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.
330 public WebView getWebView() {
335 * @return The geolocation permissions prompt for this tab.
337 public GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
338 return mGeolocationPermissionsPrompt;
342 * Return the subwindow of this tab or null if there is no subwindow.
343 * @return The subwindow of this tab or null.
345 public WebView getSubWebView() {
350 * Get the url of this tab. Valid after calling populatePickerData, but
351 * before calling wipePickerData, or if the webview has been destroyed.
353 * @return The WebView's url or null.
355 public String getUrl() {
356 if (mPickerData != null) {
357 return mPickerData.mUrl;
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.
367 * @return The WebView's title (or url) or null.
369 public String getTitle() {
370 if (mPickerData != null) {
371 return mPickerData.mTitle;
376 public Bitmap getFavicon() {
377 if (mPickerData != null) {
378 return mPickerData.mFavicon;
383 private void setParentTab(Tab 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);
393 mSavedState.putInt(PARENTTAB, getTabIndex(parent));
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
403 public void addChildTab(Tab child) {
404 if (mChildTabs == null) {
405 mChildTabs = new Vector<Tab>();
407 mChildTabs.add(child);
408 child.setParentTab(this);
411 private void removeFromTree() {
412 // detach the children
413 if (mChildTabs != null) {
414 for(Tab t : mChildTabs) {
415 t.setParentTab(null);
419 // Find myself in my parent list
420 if (mParentTab != null) {
421 mParentTab.mChildTabs.remove(this);
426 * If this Tab was created through another Tab, then this method
428 * @return the Tab parent or null
430 public Tab getParentTab() {
435 * Return whether this tab should be closed when it is backing out of
437 * @return TRUE if this tab should be closed when exit.
439 public boolean closeOnExit() {
443 void setLockIconType(int type) {
444 mSavedLockIconType = type;
447 int getLockIconType() {
448 return mSavedLockIconType;
451 void setPrevLockIconType(int type) {
452 mSavedPrevLockIconType = type;
455 int getPrevLockIconType() {
456 return mSavedPrevLockIconType;
460 // Directory to store thumbnails for each WebView.
461 private final File mThumbnailDir;
464 * Construct a new TabControl object that interfaces with the given
465 * BrowserActivity instance.
466 * @param activity A BrowserActivity instance that TabControl will interface
469 TabControl(BrowserActivity activity) {
470 mActivity = activity;
472 ((LayoutInflater) activity.getSystemService(
473 Context.LAYOUT_INFLATER_SERVICE));
474 mThumbnailDir = activity.getDir("thumbnails", 0);
477 File getThumbnailDir() {
478 return mThumbnailDir;
481 BrowserActivity getBrowserActivity() {
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.
490 WebView getCurrentWebView() {
491 Tab t = getTab(mCurrentTab);
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
503 * @return The current tab's error console, or null if one has not been created and
504 * createIfNecessary is false.
506 ErrorConsoleView getCurrentErrorConsole(boolean createIfNecessary) {
507 Tab t = getTab(mCurrentTab);
512 if (createIfNecessary && t.mErrorConsole == null) {
513 t.mErrorConsole = new ErrorConsoleView(mActivity);
514 t.mErrorConsole.setWebView(t.mMainView);
517 return t.mErrorConsole;
521 * Return the current tab's top-level WebView. This can return a subwindow
523 * @return The top-level WebView of the current tab.
525 WebView getCurrentTopWebView() {
526 Tab t = getTab(mCurrentTab);
530 return t.mSubView != null ? t.mSubView : t.mMainView;
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.
537 WebView getCurrentSubWindow() {
538 Tab t = getTab(mCurrentTab);
546 * Return the tab at the specified index.
547 * @return The Tab for the specified index or null if the tab does not
550 Tab getTab(int index) {
551 if (index >= 0 && index < mTabs.size()) {
552 return mTabs.get(index);
558 * Return the current tab.
559 * @return The current tab.
561 Tab getCurrentTab() {
562 return getTab(mCurrentTab);
566 * Return the current tab index.
567 * @return The current tab index
569 int getCurrentIndex() {
574 * Given a Tab, find it's index
576 * @return index of Tab or -1 if not found
578 int getTabIndex(Tab tab) {
582 return mTabs.indexOf(tab);
587 * @return The newly createTab or null if we have reached the maximum
588 * number of open tabs.
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) {
596 final WebView w = createNewWebView();
598 // Create a new tab and add it to the tab list
599 Tab t = new Tab(w, closeOnExit, appId, url, mActivity);
601 // Initially put the tab in the background.
602 putTabInBackground(t);
607 * Create a new tab with default values for closeOnExit(false),
608 * appId(null), and url(null).
611 return createNewTab(false, null, null);
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.
619 boolean removeTab(Tab t) {
623 // Only remove the tab if it is the current one.
624 if (getCurrentTab() == t) {
625 putTabInBackground(t);
628 // Only destroy the WebView if it still exists.
629 if (t.mMainView != null) {
630 // Take down the sub window.
632 // Remove the WebView's settings from the BrowserSettings list of
634 BrowserSettings.getInstance().deleteObserver(
635 t.mMainView.getSettings());
636 WebView w = t.mMainView;
638 // Destroy the main view
641 // clear it's references to parent and children
644 // Remove it from our list of tabs.
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);
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();
666 // Remove it from the queue of viewed tabs.
673 * Clear the back/forward list for all the current tabs.
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
681 if (t.mMainView != null) {
682 t.mMainView.clearHistory();
684 if (t.mSubView != null) {
685 t.mSubView.clearHistory();
691 * Destroy all the tabs and subwindows
694 BrowserSettings s = BrowserSettings.getInstance();
695 for (Tab t : mTabs) {
696 if (t.mMainView != null) {
698 s.deleteObserver(t.mMainView.getSettings());
699 WebView w = t.mMainView;
709 * Returns the number of tabs created.
710 * @return The number of tabs created.
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";
729 * Save the state of all the Tabs.
730 * @param outState The Bundle to save the state to.
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);
740 outState.putBundle(WEBVIEW + i, t.mSavedState);
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.
751 boolean restoreState(Bundle inState) {
752 final int numTabs = (inState == null)
753 ? -1 : inState.getInt(NUMTABS, -1);
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.
764 if (!restoreState(inState.getBundle(WEBVIEW + i), t)) {
765 Log.w(LOGTAG, "Fail in restoreState, load home page.");
766 t.mMainView.loadUrl(BrowserSettings.getInstance()
770 // Create a new tab and don't restore the state yet, add it
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);
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);
805 * Free the memory in this order, 1) free the background tab; 2) free the
809 if (getTabCount() == 0) return;
811 // free the least frequently used background tab
812 Tab t = getLeastUsedTab(getCurrentTab());
814 Log.w(LOGTAG, "Free a tab in the browser");
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();
831 private Tab getLeastUsedTab(Tab current) {
832 // Don't do anything if we only have 1 tab or if the current tab is
834 if (getTabCount() == 1 || current == null) {
838 // Rip through the queue starting at the beginning and teardown the
839 // next available tab.
842 final int queueSize = mTabQueue.size();
843 if (queueSize == 0) {
847 t = mTabQueue.get(i++);
848 } while (i < queueSize
849 && ((t != null && t.mMainView == null)
850 || t == current.mParentTab));
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) {
861 private void freeTab(Tab t) {
862 // Store the WebView's state.
865 // Tear down the tab.
867 // Remove the WebView's settings from the BrowserSettings list of
869 BrowserSettings.getInstance().deleteObserver(t.mMainView.getSettings());
870 WebView w = t.mMainView;
876 * Create a new subwindow unless a subwindow already exists.
877 * @return True if a new subwindow was created. False if one already exists.
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);
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);
913 * Show the tab that contains the given WebView.
914 * @param view The WebView used to find the tab.
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) {
928 * Return the tab with the matching application id.
929 * @param id The application identifier.
931 Tab getTabFromId(String id) {
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)) {
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) {
950 } else if (t.mMainView == null) {
952 } else if (url.equals(t.mMainView.getUrl()) ||
953 url.equals(t.mMainView.getOriginalUrl())) {
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.
964 Tab findUnusedTabWithUrl(String url) {
968 // Check the current tab first.
969 Tab t = getCurrentTab();
970 if (t != null && tabMatchesUrl(t, url)) {
973 // Now check all the rest.
974 final int size = getTabCount();
975 for (int i = 0; i < size; i++) {
977 if (tabMatchesUrl(t, url)) {
985 * Recreate the main WebView of the given tab. Returns true if the WebView
988 boolean recreateWebView(Tab t, String url) {
989 final WebView w = t.mMainView;
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();
997 w.goBackOrForward(-list.getCurrentIndex());
998 w.clearHistory(); // maintains the current page.
1002 // Remove the settings object from the global settings and destroy
1004 BrowserSettings.getInstance().deleteObserver(
1005 t.mMainView.getSettings());
1006 t.mMainView.destroy();
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);
1014 // Clear the saved state except for the app id and close-on-exit
1016 t.mSavedState = null;
1017 t.mPickerData = null;
1018 // Save the new url in order to avoid deleting the WebView.
1019 t.mOriginalUrl = url;
1024 * Creates a new WebView and registers it with the global settings.
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
1036 final BrowserSettings s = BrowserSettings.getInstance();
1037 s.addObserver(w.getSettings()).update(s, null);
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);
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
1058 boolean setCurrentTab(Tab newTab) {
1059 return setCurrentTab(newTab, false);
1062 /*package*/ void pauseCurrentTab() {
1063 Tab t = getCurrentTab();
1065 t.mMainView.onPause();
1066 if (t.mSubView != null) {
1067 t.mSubView.onPause();
1072 /*package*/ void resumeCurrentTab() {
1073 Tab t = getCurrentTab();
1075 t.mMainView.onResume();
1076 if (t.mSubView != null) {
1077 t.mSubView.onResume();
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);
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);
1105 * If force is true, this method skips the check for newTab == current.
1107 private boolean setCurrentTab(Tab newTab, boolean force) {
1108 Tab current = getTab(mCurrentTab);
1109 if (current == newTab && !force) {
1112 if (current != null) {
1113 // Remove the current WebView and the container of the subwindow
1114 putTabInBackground(current);
1117 if (newTab == null) {
1121 // Move the newTab to the end of the queue
1122 int index = mTabQueue.indexOf(newTab);
1124 mTabQueue.remove(index);
1126 mTabQueue.add(newTab);
1130 // Display the new current tab
1131 mCurrentTab = mTabs.indexOf(newTab);
1132 mainView = newTab.mMainView;
1133 boolean needRestore = (mainView == null);
1135 // Same work as in createNewTab() except don't do new Tab()
1136 mainView = createNewWebView();
1137 newTab.setWebView(mainView);
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);
1147 // Have to finish setCurrentTab work before calling restoreState
1148 if (!restoreState(newTab.mSavedState, newTab)) {
1149 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
1156 * Put the tab in the background using all the empty/background clients.
1158 private void putTabInBackground(Tab t) {
1159 putViewInBackground(t.mMainView);
1160 if (t.mSubView != null) {
1161 putViewInBackground(t.mSubView);
1166 * Dismiss the subwindow for the given tab.
1168 void dismissSubWindow(Tab t) {
1169 if (t != null && t.mSubView != null) {
1170 BrowserSettings.getInstance().deleteObserver(
1171 t.mSubView.getSettings());
1172 t.mSubView.destroy();
1174 t.mSubViewContainer = null;
1179 * Ensure that Tab t has data to display in the tab picker.
1180 * @param t Tab to populate.
1182 /* package */ void populatePickerData(Tab t) {
1187 // mMainView == null indicates that the tab has been freed.
1188 if (t.mMainView == null) {
1189 populatePickerDataFromSavedState(t);
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);
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) {
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
1213 data.mScale = state.getFloat("scale", 1.0f);
1214 data.mScrollX = state.getInt("scrollX", 0);
1215 data.mScrollY = state.getInt("scrollY", 0);
1217 // Set the tab's picker data.
1218 t.mPickerData = data;
1221 // Populate the picker data using the given history item and the current
1223 private void populatePickerData(Tab t, WebHistoryItem item) {
1224 final PickerData data = new PickerData();
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;
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();
1240 t.mPickerData = data;
1244 * Clean up the data for all tabs.
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;
1257 * Save the state for an individual tab.
1259 private boolean saveState(Tab t) {
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.
1267 final Bundle b = new Bundle();
1268 final WebBackForwardList list = w.saveState(b);
1270 final File f = new File(mThumbnailDir, w.hashCode()
1272 if (w.savePicture(b, f)) {
1273 b.putString(CURRPICTURE, f.getPath());
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);
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);
1288 if (data.mTitle != null) {
1289 b.putString(CURRTITLE, data.mTitle);
1291 b.putBoolean(CLOSEONEXIT, t.mCloseOnExit);
1292 if (t.mAppId != null) {
1293 b.putString(APPID, t.mAppId);
1295 if (t.mOriginalUrl != null) {
1296 b.putString(ORIGINALURL, t.mOriginalUrl);
1299 // Remember the parent tab so the relationship can be restored.
1300 if (t.mParentTab != null) {
1301 b.putInt(PARENTTAB, getTabIndex(t.mParentTab));
1304 // Remember the saved state.
1312 * Restore the state of the tab.
1314 private boolean restoreState(Bundle b, Tab t) {
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);
1326 final WebView w = t.mMainView;
1327 final WebBackForwardList list = w.restoreState(b);
1331 if (b.containsKey(CURRPICTURE)) {
1332 final File f = new File(b.getString(CURRPICTURE));
1333 w.restorePicture(b, f);