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