blob: 57e55c4039f047a36bc1e02b5b474c85fb4f8ae2 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
19import android.content.Context;
20import android.net.http.SslError;
21import android.os.Bundle;
22import android.os.Message;
23import android.util.Config;
24import android.util.Log;
25import android.view.Gravity;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29import android.view.View.OnClickListener;
30import android.webkit.HttpAuthHandler;
31import android.webkit.JsPromptResult;
32import android.webkit.JsResult;
33import android.webkit.SslErrorHandler;
34import android.webkit.WebBackForwardList;
35import android.webkit.WebChromeClient;
36import android.webkit.WebHistoryItem;
37import android.webkit.WebView;
38import android.webkit.WebViewClient;
39import android.widget.FrameLayout;
40import android.widget.ImageButton;
41
42import java.io.File;
43import java.util.ArrayList;
44import java.util.Vector;
45
46class TabControl {
47 // Log Tag
48 private static final String LOGTAG = "TabControl";
49 // Maximum number of tabs.
50 static final int MAX_TABS = 8;
51 // Static instance of an empty callback.
52 private static final WebViewClient mEmptyClient =
53 new WebViewClient();
54 // Instance of BackgroundChromeClient for background tabs.
55 private final BackgroundChromeClient mBackgroundChromeClient =
56 new BackgroundChromeClient();
57 // Private array of WebViews that are used as tabs.
58 private ArrayList<Tab> mTabs = new ArrayList<Tab>(MAX_TABS);
59 // Queue of most recently viewed tabs.
60 private ArrayList<Tab> mTabQueue = new ArrayList<Tab>(MAX_TABS);
61 // Current position in mTabs.
62 private int mCurrentTab = -1;
63 // A private instance of BrowserActivity to interface with when adding and
64 // switching between tabs.
65 private final BrowserActivity mActivity;
66 // Inflation service for making subwindows.
67 private final LayoutInflater mInflateService;
68 // Subclass of WebViewClient used in subwindows to notify the main
69 // WebViewClient of certain WebView activities.
70 private class SubWindowClient extends WebViewClient {
71 // The main WebViewClient.
72 private final WebViewClient mClient;
73
74 SubWindowClient(WebViewClient client) {
75 mClient = client;
76 }
77 @Override
78 public void doUpdateVisitedHistory(WebView view, String url,
79 boolean isReload) {
80 mClient.doUpdateVisitedHistory(view, url, isReload);
81 }
82 @Override
83 public boolean shouldOverrideUrlLoading(WebView view, String url) {
84 return mClient.shouldOverrideUrlLoading(view, url);
85 }
86 @Override
87 public void onReceivedSslError(WebView view, SslErrorHandler handler,
88 SslError error) {
89 mClient.onReceivedSslError(view, handler, error);
90 }
91 @Override
92 public void onReceivedHttpAuthRequest(WebView view,
93 HttpAuthHandler handler, String host, String realm) {
94 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
95 }
96 @Override
97 public void onFormResubmission(WebView view, Message dontResend,
98 Message resend) {
99 mClient.onFormResubmission(view, dontResend, resend);
100 }
101 @Override
102 public void onReceivedError(WebView view, int errorCode,
103 String description, String failingUrl) {
104 mClient.onReceivedError(view, errorCode, description, failingUrl);
105 }
106 }
107 // Subclass of WebChromeClient to display javascript dialogs.
108 private class SubWindowChromeClient extends WebChromeClient {
109 // This subwindow's tab.
110 private final Tab mTab;
111 // The main WebChromeClient.
112 private final WebChromeClient mClient;
113
114 SubWindowChromeClient(Tab t, WebChromeClient client) {
115 mTab = t;
116 mClient = client;
117 }
118 @Override
119 public void onProgressChanged(WebView view, int newProgress) {
120 mClient.onProgressChanged(view, newProgress);
121 }
122 @Override
123 public boolean onCreateWindow(WebView view, boolean dialog,
124 boolean userGesture, android.os.Message resultMsg) {
125 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
126 }
127 @Override
128 public void onCloseWindow(WebView window) {
129 if (Config.DEBUG && window != mTab.mSubView) {
130 throw new AssertionError("Can't close the window");
131 }
132 mActivity.dismissSubWindow(mTab);
133 }
134 }
135 // Background WebChromeClient for focusing tabs
136 private class BackgroundChromeClient extends WebChromeClient {
137 @Override
138 public void onRequestFocus(WebView view) {
139 Tab t = getTabFromView(view);
140 if (t != getCurrentTab()) {
141 mActivity.showTab(t);
142 }
143 }
144 }
145
146 /**
147 * Private class for maintaining Tabs with a main WebView and a subwindow.
148 */
149 public class Tab {
150 // Main WebView
151 private WebView mMainView;
152 // Subwindow WebView
153 private WebView mSubView;
154 // Subwindow container
155 private View mSubViewContainer;
156 // Subwindow callback
157 private SubWindowClient mSubViewClient;
158 // Subwindow chrome callback
159 private SubWindowChromeClient mSubViewChromeClient;
160 // Saved bundle for when we are running low on memory. It contains the
161 // information needed to restore the WebView if the user goes back to
162 // the tab.
163 private Bundle mSavedState;
164 // Extra saved information for displaying the tab in the picker.
165 private String mUrl;
166 private String mTitle;
167
168 // Parent Tab. This is the Tab that created this Tab, or null
169 // if the Tab was created by the UI
170 private Tab mParentTab;
171 // Tab that constructed by this Tab. This is used when this
172 // Tab is destroyed, it clears all mParentTab values in the
173 // children.
174 private Vector<Tab> mChildTabs;
175
176 private Boolean mCloseOnExit;
177
178 // Construct a new tab
179 private Tab(WebView w, boolean closeOnExit) {
180 mMainView = w;
181 mCloseOnExit = closeOnExit;
182 }
183
184 /**
185 * Return the top window of this tab; either the subwindow if it is not
186 * null or the main window.
187 * @return The top window of this tab.
188 */
189 public WebView getTopWindow() {
190 if (mSubView != null) {
191 return mSubView;
192 }
193 return mMainView;
194 }
195
196 /**
197 * Return the main window of this tab. Note: if a tab is freed in the
198 * background, this can return null. It is only guaranteed to be
199 * non-null for the current tab.
200 * @return The main WebView of this tab.
201 */
202 public WebView getWebView() {
203 return mMainView;
204 }
205
206 /**
207 * Return the subwindow of this tab or null if there is no subwindow.
208 * @return The subwindow of this tab or null.
209 */
210 public WebView getSubWebView() {
211 return mSubView;
212 }
213
214 /**
215 * Return the subwindow container of this tab or null if there is no
216 * subwindow.
217 * @return The subwindow's container View.
218 */
219 public View getSubWebViewContainer() {
220 return mSubViewContainer;
221 }
222
223 /**
224 * Get the url of this tab. Valid after calling populatePickerData, but
225 * before calling wipePickerData, or if the webview has been destroyed.
226 *
227 * @return The WebView's url or null.
228 */
229 public String getUrl() {
230 return mUrl;
231 }
232
233 /**
234 * Get the title of this tab. Valid after calling populatePickerData,
235 * but before calling wipePickerData, or if the webview has been
236 * destroyed. If the url has no title, use the url instead.
237 *
238 * @return The WebView's title (or url) or null.
239 */
240 public String getTitle() {
241 return mTitle;
242 }
243
244 private void setParentTab(Tab parent) {
245 mParentTab = parent;
246 // This tab may have been freed due to low memory. If that is the
247 // case, the parent tab index is already saved. If we are changing
248 // that index (most likely due to removing the parent tab) we must
249 // update the parent tab index in the saved Bundle.
250 if (mSavedState != null) {
251 if (parent == null) {
252 mSavedState.remove(PARENTTAB);
253 } else {
254 mSavedState.putInt(PARENTTAB, getTabIndex(parent));
255 }
256 }
257 }
258
259 /**
260 * When a Tab is created through the content of another Tab, then
261 * we associate the Tabs.
262 * @param child the Tab that was created from this Tab
263 */
264 public void addChildTab(Tab child) {
265 if (mChildTabs == null) {
266 mChildTabs = new Vector<Tab>();
267 }
268 mChildTabs.add(child);
269 child.setParentTab(this);
270 }
271
272 private void removeFromTree() {
273 // detach the children
274 if (mChildTabs != null) {
275 for(Tab t : mChildTabs) {
276 t.setParentTab(null);
277 }
278 }
279
280 // Find myself in my parent list
281 if (mParentTab != null) {
282 mParentTab.mChildTabs.remove(this);
283 }
284 }
285
286 /**
287 * If this Tab was created through another Tab, then this method
288 * returns that Tab.
289 * @return the Tab parent or null
290 */
291 public Tab getParentTab() {
292 return mParentTab;
293 }
294
295 /**
296 * Return whether this tab should be closed when it is backing out of
297 * the first page.
298 * @return TRUE if this tab should be closed when exit.
299 */
300 public boolean closeOnExit() {
301 return mCloseOnExit;
302 }
303 };
304
305 // Directory to store thumbnails for each WebView.
306 private final File mThumbnailDir;
307
308 /**
309 * Construct a new TabControl object that interfaces with the given
310 * BrowserActivity instance.
311 * @param activity A BrowserActivity instance that TabControl will interface
312 * with.
313 */
314 TabControl(BrowserActivity activity) {
315 mActivity = activity;
316 mInflateService =
317 ((LayoutInflater) activity.getSystemService(
318 Context.LAYOUT_INFLATER_SERVICE));
319 mThumbnailDir = activity.getDir("thumbnails", 0);
320 }
321
322 File getThumbnailDir() {
323 return mThumbnailDir;
324 }
325
326 BrowserActivity getBrowserActivity() {
327 return mActivity;
328 }
329
330 /**
331 * Return the current tab's main WebView. This will always return the main
332 * WebView for a given tab and not a subwindow.
333 * @return The current tab's WebView.
334 */
335 WebView getCurrentWebView() {
336 Tab t = getTab(mCurrentTab);
337 if (t == null) {
338 return null;
339 }
340 return t.mMainView;
341 }
342
343 /**
344 * Return the current tab's top-level WebView. This can return a subwindow
345 * if one exists.
346 * @return The top-level WebView of the current tab.
347 */
348 WebView getCurrentTopWebView() {
349 Tab t = getTab(mCurrentTab);
350 if (t == null) {
351 return null;
352 }
353 return t.mSubView != null ? t.mSubView : t.mMainView;
354 }
355
356 /**
357 * Return the current tab's subwindow if it exists.
358 * @return The subwindow of the current tab or null if it doesn't exist.
359 */
360 WebView getCurrentSubWindow() {
361 Tab t = getTab(mCurrentTab);
362 if (t == null) {
363 return null;
364 }
365 return t.mSubView;
366 }
367
368 /**
369 * Return the tab at the specified index.
370 * @return The Tab for the specified index or null if the tab does not
371 * exist.
372 */
373 Tab getTab(int index) {
374 if (index >= 0 && index < mTabs.size()) {
375 return mTabs.get(index);
376 }
377 return null;
378 }
379
380 /**
381 * Return the current tab.
382 * @return The current tab.
383 */
384 Tab getCurrentTab() {
385 return getTab(mCurrentTab);
386 }
387
388 /**
389 * Return the current tab index.
390 * @return The current tab index
391 */
392 int getCurrentIndex() {
393 return mCurrentTab;
394 }
395
396 /**
397 * Given a Tab, find it's index
398 * @param Tab to find
399 * @return index of Tab or -1 if not found
400 */
401 int getTabIndex(Tab tab) {
402 return mTabs.indexOf(tab);
403 }
404
405 /**
406 * Create a new tab and display the new tab immediately.
407 * @return The newly createTab or null if we have reached the maximum
408 * number of open tabs.
409 */
410 Tab createNewTab(boolean closeOnExit) {
411 int size = mTabs.size();
412 // Return false if we have maxed out on tabs
413 if (MAX_TABS == size) {
414 return null;
415 }
416 // Create a new WebView
417 WebView w = new WebView(mActivity);
418 w.setMapTrackballToArrowKeys(false); // use trackball directly
419 // Add this WebView to the settings observer list and update the
420 // settings
421 final BrowserSettings s = BrowserSettings.getInstance();
422 s.addObserver(w.getSettings()).update(s, null);
423 // Create a new tab and add it to the tab list
424 Tab t = new Tab(w, closeOnExit);
425 mTabs.add(t);
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700426 // Initially put the tab in the background.
427 putTabInBackground(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800428 return t;
429 }
430
431 /**
432 * Remove the tab from the list. If the tab is the current tab shown, the
433 * last created tab will be shown.
434 * @param t The tab to be removed.
435 */
436 boolean removeTab(Tab t) {
437 if (t == null) {
438 return false;
439 }
440 // Only remove the tab if it is the current one.
441 if (getCurrentTab() == t) {
442 putTabInBackground(t);
443 }
444
445 // Only destroy the WebView if it still exists.
446 if (t.mMainView != null) {
447 // Take down the sub window.
448 dismissSubWindow(t);
449 // Remove the WebView's settings from the BrowserSettings list of
450 // observers.
451 BrowserSettings.getInstance().deleteObserver(
452 t.mMainView.getSettings());
453 // Destroy the main view and subview
454 t.mMainView.destroy();
455 t.mMainView = null;
456 }
457 // clear it's references to parent and children
458 t.removeFromTree();
459
460 // Remove it from our list of tabs.
461 mTabs.remove(t);
462
463 // The tab indices have shifted, update all the saved state so we point
464 // to the correct index.
465 for (Tab tab : mTabs) {
466 if (tab.mChildTabs != null) {
467 for (Tab child : tab.mChildTabs) {
468 child.setParentTab(tab);
469 }
470 }
471 }
472
473
474 // This tab may have been pushed in to the background and then closed.
475 // If the saved state contains a picture file, delete the file.
476 if (t.mSavedState != null) {
477 if (t.mSavedState.containsKey("picture")) {
478 new File(t.mSavedState.getString("picture")).delete();
479 }
480 }
481
482 // Remove it from the queue of viewed tabs.
483 mTabQueue.remove(t);
484 mCurrentTab = -1;
485 return true;
486 }
487
488 /**
489 * Clear the back/forward list for all the current tabs.
490 */
491 void clearHistory() {
492 int size = getTabCount();
493 for (int i = 0; i < size; i++) {
494 Tab t = mTabs.get(i);
495 // TODO: if a tab is freed due to low memory, its history is not
496 // cleared here.
497 if (t.mMainView != null) {
498 t.mMainView.clearHistory();
499 }
500 if (t.mSubView != null) {
501 t.mSubView.clearHistory();
502 }
503 }
504 }
505
506 /**
507 * Destroy all the tabs and subwindows
508 */
509 void destroy() {
510 BrowserSettings s = BrowserSettings.getInstance();
511 for (Tab t : mTabs) {
512 if (t.mMainView != null) {
513 dismissSubWindow(t);
514 s.deleteObserver(t.mMainView.getSettings());
515 t.mMainView.destroy();
516 t.mMainView = null;
517 }
518 }
519 mTabs.clear();
520 mTabQueue.clear();
521 }
522
523 /**
524 * Returns the number of tabs created.
525 * @return The number of tabs created.
526 */
527 int getTabCount() {
528 return mTabs.size();
529 }
530
531 // Used for saving and restoring each Tab
532 private static final String WEBVIEW = "webview";
533 private static final String NUMTABS = "numTabs";
534 private static final String CURRTAB = "currentTab";
535 private static final String CURRURL = "currentUrl";
536 private static final String CURRTITLE = "currentTitle";
537 private static final String CLOSEONEXIT = "closeonexit";
538 private static final String PARENTTAB = "parentTab";
539
540 /**
541 * Save the state of all the Tabs.
542 * @param outState The Bundle to save the state to.
543 */
544 void saveState(Bundle outState) {
545 final int numTabs = getTabCount();
546 outState.putInt(NUMTABS, numTabs);
547 final int index = getCurrentIndex();
548 outState.putInt(CURRTAB, (index >= 0 && index < numTabs) ? index : 0);
549 for (int i = 0; i < numTabs; i++) {
550 final Tab t = getTab(i);
551 if (saveState(t)) {
552 outState.putBundle(WEBVIEW + i, t.mSavedState);
553 }
554 }
555 }
556
557 /**
558 * Restore the state of all the tabs.
559 * @param inState The saved state of all the tabs.
560 * @return True if there were previous tabs that were restored. False if
561 * there was no saved state or restoring the state failed.
562 */
563 boolean restoreState(Bundle inState) {
564 final int numTabs = (inState == null)
565 ? -1 : inState.getInt(NUMTABS, -1);
566 if (numTabs == -1) {
567 return false;
568 } else {
569 final int currentTab = inState.getInt(CURRTAB, -1);
570 for (int i = 0; i < numTabs; i++) {
571 if (i == currentTab) {
572 Tab t = createNewTab(false);
573 // Me must set the current tab before restoring the state
574 // so that all the client classes are set.
575 setCurrentTab(t);
576 if (!restoreState(inState.getBundle(WEBVIEW + i), t)) {
577 Log.w(LOGTAG, "Fail in restoreState, load home page.");
578 t.mMainView.loadUrl(BrowserSettings.getInstance()
579 .getHomePage());
580 }
581 } else {
582 // Create a new tab and don't restore the state yet, add it
583 // to the tab list
584 Tab t = new Tab(null, false);
585 t.mSavedState = inState.getBundle(WEBVIEW + i);
586 if (t.mSavedState != null) {
587 t.mUrl = t.mSavedState.getString(CURRURL);
588 t.mTitle = t.mSavedState.getString(CURRTITLE);
589 }
590 mTabs.add(t);
591 mTabQueue.add(t);
592 }
593 }
594 // Rebuild the tree of tabs. Do this after all tabs have been
595 // created/restored so that the parent tab exists.
596 for (int i = 0; i < numTabs; i++) {
597 final Bundle b = inState.getBundle(WEBVIEW + i);
598 final Tab t = getTab(i);
599 if (b != null && t != null) {
600 final int parentIndex = b.getInt(PARENTTAB, -1);
601 if (parentIndex != -1) {
602 final Tab parent = getTab(parentIndex);
603 if (parent != null) {
604 parent.addChildTab(t);
605 }
606 }
607 }
608 }
609 }
610 return true;
611 }
612
613 /**
614 * Free the memory in this order, 1) free the background tab; 2) free the
615 * WebView cache;
616 */
617 void freeMemory() {
618 // free the least frequently used background tab
619 Tab t = getLeastUsedTab();
620 if (t != null) {
621 Log.w(LOGTAG, "Free a tab in the browser");
622 freeTab(t);
623 // force a gc
624 System.gc();
625 return;
626 }
627
628 // free the WebView cache
629 Log.w(LOGTAG, "Free WebView cache");
630 WebView view = getCurrentWebView();
631 if (view != null) {
632 view.clearCache(false);
633 }
634 // force a gc
635 System.gc();
636 }
637
638 private Tab getLeastUsedTab() {
639 // Don't do anything if we only have 1 tab.
640 if (getTabCount() == 1) {
641 return null;
642 }
643
644 // Rip through the queue starting at the beginning and teardown the
645 // next available tab.
646 Tab t = null;
647 int i = 0;
648 final int queueSize = mTabQueue.size();
649 if (queueSize == 0) {
650 return null;
651 }
652 do {
653 t = mTabQueue.get(i++);
654 } while (i < queueSize && t != null && t.mMainView == null);
655
656 // Don't do anything if the last remaining tab is the current one.
657 if (t == getCurrentTab()) {
658 return null;
659 }
660
661 return t;
662 }
663
664 private void freeTab(Tab t) {
665 // Store the WebView's state.
666 saveState(t);
667
668 // Tear down the tab.
669 dismissSubWindow(t);
670 // Remove the WebView's settings from the BrowserSettings list of
671 // observers.
672 BrowserSettings.getInstance().deleteObserver(t.mMainView.getSettings());
673 t.mMainView.destroy();
674 t.mMainView = null;
675 }
676
677 /**
678 * Create a new subwindow unless a subwindow already exists.
679 * @return True if a new subwindow was created. False if one already exists.
680 */
681 void createSubWindow() {
682 Tab t = getTab(mCurrentTab);
683 if (t != null && t.mSubView == null) {
684 final View v = mInflateService.inflate(R.layout.browser_subwindow, null);
685 final WebView w = (WebView) v.findViewById(R.id.webview);
686 w.setMapTrackballToArrowKeys(false); // use trackball directly
687 final SubWindowClient subClient =
688 new SubWindowClient(mActivity.getWebViewClient());
689 final SubWindowChromeClient subChromeClient =
690 new SubWindowChromeClient(t,
691 mActivity.getWebChromeClient());
692 w.setWebViewClient(subClient);
693 w.setWebChromeClient(subChromeClient);
694 w.setDownloadListener(mActivity);
695 w.setOnCreateContextMenuListener(mActivity);
696 final BrowserSettings s = BrowserSettings.getInstance();
697 s.addObserver(w.getSettings()).update(s, null);
698 t.mSubView = w;
699 t.mSubViewClient = subClient;
700 t.mSubViewChromeClient = subChromeClient;
701 // FIXME: I really hate having to know the name of the view
702 // containing the webview.
703 t.mSubViewContainer = v.findViewById(R.id.subwindow_container);
704 final ImageButton cancel =
705 (ImageButton) v.findViewById(R.id.subwindow_close);
706 cancel.setOnClickListener(new OnClickListener() {
707 public void onClick(View v) {
708 subChromeClient.onCloseWindow(w);
709 }
710 });
711 }
712 }
713
714 /**
715 * Show the tab that contains the given WebView.
716 * @param view The WebView used to find the tab.
717 */
718 Tab getTabFromView(WebView view) {
719 final int size = getTabCount();
720 for (int i = 0; i < size; i++) {
721 final Tab t = getTab(i);
722 if (t.mSubView == view || t.mMainView == view) {
723 return t;
724 }
725 }
726 return null;
727 }
728
729 /**
730 * Put the current tab in the background and set newTab as the current tab.
731 * @param newTab The new tab. If newTab is null, the current tab is not
732 * set.
733 */
734 boolean setCurrentTab(Tab newTab) {
735 Tab current = getTab(mCurrentTab);
736 if (current == newTab) {
737 return true;
738 }
739 if (current != null) {
740 // Remove the current WebView and the container of the subwindow
741 putTabInBackground(current);
742 }
743
744 if (newTab == null) {
745 return false;
746 }
747
748 // Move the newTab to the end of the queue
749 int index = mTabQueue.indexOf(newTab);
750 if (index != -1) {
751 mTabQueue.remove(index);
752 }
753 mTabQueue.add(newTab);
754
755 WebView mainView;
756 WebView subView;
757
758 // Display the new current tab
759 mCurrentTab = mTabs.indexOf(newTab);
760 mainView = newTab.mMainView;
761 boolean needRestore = (mainView == null);
762 if (needRestore) {
763 // Same work as in createNewTab() except don't do new Tab()
764 newTab.mMainView = mainView = new WebView(mActivity);
765 mainView.setMapTrackballToArrowKeys(false); // use t-ball directly
766
767 // Add this WebView to the settings observer list and update the
768 // settings
769 final BrowserSettings s = BrowserSettings.getInstance();
770 s.addObserver(mainView.getSettings()).update(s, null);
771 }
772 mainView.setWebViewClient(mActivity.getWebViewClient());
773 mainView.setWebChromeClient(mActivity.getWebChromeClient());
774 mainView.setOnCreateContextMenuListener(mActivity);
775 mainView.setDownloadListener(mActivity);
776 // Add the subwindow if it exists
777 if (newTab.mSubViewContainer != null) {
778 subView = newTab.mSubView;
779 subView.setWebViewClient(newTab.mSubViewClient);
780 subView.setWebChromeClient(newTab.mSubViewChromeClient);
781 subView.setOnCreateContextMenuListener(mActivity);
782 subView.setDownloadListener(mActivity);
783 }
784 if (needRestore) {
785 // Have to finish setCurrentTab work before calling restoreState
786 if (!restoreState(newTab.mSavedState, newTab)) {
787 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
788 }
789 }
790 return true;
791 }
792
793 /*
794 * Put the tab in the background using all the empty/background clients.
795 */
796 private void putTabInBackground(Tab t) {
797 WebView mainView = t.mMainView;
798 // Set an empty callback so that default actions are not triggered.
799 mainView.setWebViewClient(mEmptyClient);
800 mainView.setWebChromeClient(mBackgroundChromeClient);
801 mainView.setOnCreateContextMenuListener(null);
802 // Leave the DownloadManager attached so that downloads can start in
803 // a non-active window. This can happen when going to a site that does
804 // a redirect after a period of time. The user could have switched to
805 // another tab while waiting for the download to start.
806 mainView.setDownloadListener(mActivity);
807 WebView subView = t.mSubView;
808 if (subView != null) {
809 // Set an empty callback so that default actions are not triggered.
810 subView.setWebViewClient(mEmptyClient);
811 subView.setWebChromeClient(mBackgroundChromeClient);
812 subView.setOnCreateContextMenuListener(null);
813 subView.setDownloadListener(mActivity);
814 }
815 }
816
817 /*
818 * Dismiss the subwindow for the given tab.
819 */
820 void dismissSubWindow(Tab t) {
821 if (t != null && t.mSubView != null) {
822 BrowserSettings.getInstance().deleteObserver(
823 t.mSubView.getSettings());
824 t.mSubView.destroy();
825 t.mSubView = null;
826 t.mSubViewContainer = null;
827 }
828 }
829
830 /**
831 * Ensure that Tab t has a title, url, and favicon.
832 * @param t Tab to populate.
833 */
834 /* package */ void populatePickerData(Tab t) {
835 if (t == null || t.mMainView == null) {
836 return;
837 }
838 // FIXME: The only place we cared about subwindow was for
839 // bookmarking (i.e. not when saving state). Was this deliberate?
840 final WebBackForwardList list = t.mMainView.copyBackForwardList();
841 final WebHistoryItem item =
842 list != null ? list.getCurrentItem() : null;
843 populatePickerData(t, item);
844 }
845
846 // Populate the picker data
847 private void populatePickerData(Tab t, WebHistoryItem item) {
848 if (item != null) {
849 t.mUrl = item.getUrl();
850 t.mTitle = item.getTitle();
851 if (t.mTitle == null) {
852 t.mTitle = t.mUrl;
853 }
854 }
855 }
856
857 /**
858 * Clean up the data for all tabs.
859 */
860 /* package */ void wipeAllPickerData() {
861 int size = getTabCount();
862 for (int i = 0; i < size; i++) {
863 final Tab t = getTab(i);
864 if (t != null && t.mSavedState == null) {
865 t.mUrl = null;
866 t.mTitle = null;
867 }
868 }
869 }
870
871 /*
872 * Save the state for an individual tab.
873 */
874 private boolean saveState(Tab t) {
875 if (t != null) {
876 final WebView w = t.mMainView;
877 // If the WebView is null it means we ran low on memory and we
878 // already stored the saved state in mSavedState.
879 if (w == null) {
880 return true;
881 }
882 final Bundle b = new Bundle();
883 final WebBackForwardList list = w.saveState(b);
884 if (list != null) {
885 final File f = new File(mThumbnailDir, w.hashCode()
886 + "_pic.save");
887 if (w.savePicture(b, f)) {
888 b.putString("picture", f.getPath());
889 }
890 }
891
892 // Store some extra info for displaying the tab in the picker.
893 final WebHistoryItem item =
894 list != null ? list.getCurrentItem() : null;
895 populatePickerData(t, item);
896 if (t.mUrl != null) {
897 b.putString(CURRURL, t.mUrl);
898 }
899 if (t.mTitle != null) {
900 b.putString(CURRTITLE, t.mTitle);
901 }
902 b.putBoolean(CLOSEONEXIT, t.mCloseOnExit);
903
904 // Remember the parent tab so the relationship can be restored.
905 if (t.mParentTab != null) {
906 b.putInt(PARENTTAB, getTabIndex(t.mParentTab));
907 }
908
909 // Remember the saved state.
910 t.mSavedState = b;
911 return true;
912 }
913 return false;
914 }
915
916 /*
917 * Restore the state of the tab.
918 */
919 private boolean restoreState(Bundle b, Tab t) {
920 if (b == null) {
921 return false;
922 }
923 final WebView w = t.mMainView;
924 final WebBackForwardList list = w.restoreState(b);
925 if (list == null) {
926 return false;
927 }
928 if (b.containsKey("picture")) {
929 final File f = new File(b.getString("picture"));
930 w.restorePicture(b, f);
931 f.delete();
932 }
933 t.mSavedState = null;
934 t.mUrl = null;
935 t.mTitle = null;
936 t.mCloseOnExit = b.getBoolean(CLOSEONEXIT);
937 return true;
938 }
939}