blob: 0cbef695b6b202e3d521e42c9c10e6f1bb89ce2d [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;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700177 // Application identifier used to find tabs that another application
178 // wants to reuse.
179 private String mAppId;
180 // Keep the original url around to avoid killing the old WebView if the
181 // url has not changed.
182 private String mOriginalUrl;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800183
184 // Construct a new tab
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700185 private Tab(WebView w, boolean closeOnExit, String appId, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186 mMainView = w;
187 mCloseOnExit = closeOnExit;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700188 mAppId = appId;
189 mOriginalUrl = url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 }
191
192 /**
193 * Return the top window of this tab; either the subwindow if it is not
194 * null or the main window.
195 * @return The top window of this tab.
196 */
197 public WebView getTopWindow() {
198 if (mSubView != null) {
199 return mSubView;
200 }
201 return mMainView;
202 }
203
204 /**
205 * Return the main window of this tab. Note: if a tab is freed in the
206 * background, this can return null. It is only guaranteed to be
207 * non-null for the current tab.
208 * @return The main WebView of this tab.
209 */
210 public WebView getWebView() {
211 return mMainView;
212 }
213
214 /**
215 * Return the subwindow of this tab or null if there is no subwindow.
216 * @return The subwindow of this tab or null.
217 */
218 public WebView getSubWebView() {
219 return mSubView;
220 }
221
222 /**
223 * Return the subwindow container of this tab or null if there is no
224 * subwindow.
225 * @return The subwindow's container View.
226 */
227 public View getSubWebViewContainer() {
228 return mSubViewContainer;
229 }
230
231 /**
232 * Get the url of this tab. Valid after calling populatePickerData, but
233 * before calling wipePickerData, or if the webview has been destroyed.
234 *
235 * @return The WebView's url or null.
236 */
237 public String getUrl() {
238 return mUrl;
239 }
240
241 /**
242 * Get the title of this tab. Valid after calling populatePickerData,
243 * but before calling wipePickerData, or if the webview has been
244 * destroyed. If the url has no title, use the url instead.
245 *
246 * @return The WebView's title (or url) or null.
247 */
248 public String getTitle() {
249 return mTitle;
250 }
251
252 private void setParentTab(Tab parent) {
253 mParentTab = parent;
254 // This tab may have been freed due to low memory. If that is the
255 // case, the parent tab index is already saved. If we are changing
256 // that index (most likely due to removing the parent tab) we must
257 // update the parent tab index in the saved Bundle.
258 if (mSavedState != null) {
259 if (parent == null) {
260 mSavedState.remove(PARENTTAB);
261 } else {
262 mSavedState.putInt(PARENTTAB, getTabIndex(parent));
263 }
264 }
265 }
266
267 /**
268 * When a Tab is created through the content of another Tab, then
269 * we associate the Tabs.
270 * @param child the Tab that was created from this Tab
271 */
272 public void addChildTab(Tab child) {
273 if (mChildTabs == null) {
274 mChildTabs = new Vector<Tab>();
275 }
276 mChildTabs.add(child);
277 child.setParentTab(this);
278 }
279
280 private void removeFromTree() {
281 // detach the children
282 if (mChildTabs != null) {
283 for(Tab t : mChildTabs) {
284 t.setParentTab(null);
285 }
286 }
287
288 // Find myself in my parent list
289 if (mParentTab != null) {
290 mParentTab.mChildTabs.remove(this);
291 }
292 }
293
294 /**
295 * If this Tab was created through another Tab, then this method
296 * returns that Tab.
297 * @return the Tab parent or null
298 */
299 public Tab getParentTab() {
300 return mParentTab;
301 }
302
303 /**
304 * Return whether this tab should be closed when it is backing out of
305 * the first page.
306 * @return TRUE if this tab should be closed when exit.
307 */
308 public boolean closeOnExit() {
309 return mCloseOnExit;
310 }
311 };
312
313 // Directory to store thumbnails for each WebView.
314 private final File mThumbnailDir;
315
316 /**
317 * Construct a new TabControl object that interfaces with the given
318 * BrowserActivity instance.
319 * @param activity A BrowserActivity instance that TabControl will interface
320 * with.
321 */
322 TabControl(BrowserActivity activity) {
323 mActivity = activity;
324 mInflateService =
325 ((LayoutInflater) activity.getSystemService(
326 Context.LAYOUT_INFLATER_SERVICE));
327 mThumbnailDir = activity.getDir("thumbnails", 0);
328 }
329
330 File getThumbnailDir() {
331 return mThumbnailDir;
332 }
333
334 BrowserActivity getBrowserActivity() {
335 return mActivity;
336 }
337
338 /**
339 * Return the current tab's main WebView. This will always return the main
340 * WebView for a given tab and not a subwindow.
341 * @return The current tab's WebView.
342 */
343 WebView getCurrentWebView() {
344 Tab t = getTab(mCurrentTab);
345 if (t == null) {
346 return null;
347 }
348 return t.mMainView;
349 }
350
351 /**
352 * Return the current tab's top-level WebView. This can return a subwindow
353 * if one exists.
354 * @return The top-level WebView of the current tab.
355 */
356 WebView getCurrentTopWebView() {
357 Tab t = getTab(mCurrentTab);
358 if (t == null) {
359 return null;
360 }
361 return t.mSubView != null ? t.mSubView : t.mMainView;
362 }
363
364 /**
365 * Return the current tab's subwindow if it exists.
366 * @return The subwindow of the current tab or null if it doesn't exist.
367 */
368 WebView getCurrentSubWindow() {
369 Tab t = getTab(mCurrentTab);
370 if (t == null) {
371 return null;
372 }
373 return t.mSubView;
374 }
375
376 /**
377 * Return the tab at the specified index.
378 * @return The Tab for the specified index or null if the tab does not
379 * exist.
380 */
381 Tab getTab(int index) {
382 if (index >= 0 && index < mTabs.size()) {
383 return mTabs.get(index);
384 }
385 return null;
386 }
387
388 /**
389 * Return the current tab.
390 * @return The current tab.
391 */
392 Tab getCurrentTab() {
393 return getTab(mCurrentTab);
394 }
395
396 /**
397 * Return the current tab index.
398 * @return The current tab index
399 */
400 int getCurrentIndex() {
401 return mCurrentTab;
402 }
403
404 /**
405 * Given a Tab, find it's index
406 * @param Tab to find
407 * @return index of Tab or -1 if not found
408 */
409 int getTabIndex(Tab tab) {
Patrick Scottae641ac2009-04-20 13:51:49 -0400410 if (tab == null) {
411 return -1;
412 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800413 return mTabs.indexOf(tab);
414 }
415
416 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700417 * Create a new tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800418 * @return The newly createTab or null if we have reached the maximum
419 * number of open tabs.
420 */
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700421 Tab createNewTab(boolean closeOnExit, String appId, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800422 int size = mTabs.size();
423 // Return false if we have maxed out on tabs
424 if (MAX_TABS == size) {
425 return null;
426 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700427 final WebView w = createNewWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800428 // Create a new tab and add it to the tab list
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700429 Tab t = new Tab(w, closeOnExit, appId, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800430 mTabs.add(t);
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700431 // Initially put the tab in the background.
432 putTabInBackground(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800433 return t;
434 }
435
436 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700437 * Create a new tab with default values for closeOnExit(false),
438 * appId(null), and url(null).
439 */
440 Tab createNewTab() {
441 return createNewTab(false, null, null);
442 }
443
444 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800445 * Remove the tab from the list. If the tab is the current tab shown, the
446 * last created tab will be shown.
447 * @param t The tab to be removed.
448 */
449 boolean removeTab(Tab t) {
450 if (t == null) {
451 return false;
452 }
453 // Only remove the tab if it is the current one.
454 if (getCurrentTab() == t) {
455 putTabInBackground(t);
456 }
457
458 // Only destroy the WebView if it still exists.
459 if (t.mMainView != null) {
460 // Take down the sub window.
461 dismissSubWindow(t);
462 // Remove the WebView's settings from the BrowserSettings list of
463 // observers.
464 BrowserSettings.getInstance().deleteObserver(
465 t.mMainView.getSettings());
466 // Destroy the main view and subview
467 t.mMainView.destroy();
468 t.mMainView = null;
469 }
470 // clear it's references to parent and children
471 t.removeFromTree();
472
473 // Remove it from our list of tabs.
474 mTabs.remove(t);
475
476 // The tab indices have shifted, update all the saved state so we point
477 // to the correct index.
478 for (Tab tab : mTabs) {
479 if (tab.mChildTabs != null) {
480 for (Tab child : tab.mChildTabs) {
481 child.setParentTab(tab);
482 }
483 }
484 }
485
486
487 // This tab may have been pushed in to the background and then closed.
488 // If the saved state contains a picture file, delete the file.
489 if (t.mSavedState != null) {
490 if (t.mSavedState.containsKey("picture")) {
491 new File(t.mSavedState.getString("picture")).delete();
492 }
493 }
494
495 // Remove it from the queue of viewed tabs.
496 mTabQueue.remove(t);
497 mCurrentTab = -1;
498 return true;
499 }
500
501 /**
502 * Clear the back/forward list for all the current tabs.
503 */
504 void clearHistory() {
505 int size = getTabCount();
506 for (int i = 0; i < size; i++) {
507 Tab t = mTabs.get(i);
508 // TODO: if a tab is freed due to low memory, its history is not
509 // cleared here.
510 if (t.mMainView != null) {
511 t.mMainView.clearHistory();
512 }
513 if (t.mSubView != null) {
514 t.mSubView.clearHistory();
515 }
516 }
517 }
518
519 /**
520 * Destroy all the tabs and subwindows
521 */
522 void destroy() {
523 BrowserSettings s = BrowserSettings.getInstance();
524 for (Tab t : mTabs) {
525 if (t.mMainView != null) {
526 dismissSubWindow(t);
527 s.deleteObserver(t.mMainView.getSettings());
528 t.mMainView.destroy();
529 t.mMainView = null;
530 }
531 }
532 mTabs.clear();
533 mTabQueue.clear();
534 }
535
536 /**
537 * Returns the number of tabs created.
538 * @return The number of tabs created.
539 */
540 int getTabCount() {
541 return mTabs.size();
542 }
543
544 // Used for saving and restoring each Tab
545 private static final String WEBVIEW = "webview";
546 private static final String NUMTABS = "numTabs";
547 private static final String CURRTAB = "currentTab";
548 private static final String CURRURL = "currentUrl";
549 private static final String CURRTITLE = "currentTitle";
550 private static final String CLOSEONEXIT = "closeonexit";
551 private static final String PARENTTAB = "parentTab";
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700552 private static final String APPID = "appid";
553 private static final String ORIGINALURL = "originalUrl";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800554
555 /**
556 * Save the state of all the Tabs.
557 * @param outState The Bundle to save the state to.
558 */
559 void saveState(Bundle outState) {
560 final int numTabs = getTabCount();
561 outState.putInt(NUMTABS, numTabs);
562 final int index = getCurrentIndex();
563 outState.putInt(CURRTAB, (index >= 0 && index < numTabs) ? index : 0);
564 for (int i = 0; i < numTabs; i++) {
565 final Tab t = getTab(i);
566 if (saveState(t)) {
567 outState.putBundle(WEBVIEW + i, t.mSavedState);
568 }
569 }
570 }
571
572 /**
573 * Restore the state of all the tabs.
574 * @param inState The saved state of all the tabs.
575 * @return True if there were previous tabs that were restored. False if
576 * there was no saved state or restoring the state failed.
577 */
578 boolean restoreState(Bundle inState) {
579 final int numTabs = (inState == null)
580 ? -1 : inState.getInt(NUMTABS, -1);
581 if (numTabs == -1) {
582 return false;
583 } else {
584 final int currentTab = inState.getInt(CURRTAB, -1);
585 for (int i = 0; i < numTabs; i++) {
586 if (i == currentTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700587 Tab t = createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800588 // Me must set the current tab before restoring the state
589 // so that all the client classes are set.
590 setCurrentTab(t);
591 if (!restoreState(inState.getBundle(WEBVIEW + i), t)) {
592 Log.w(LOGTAG, "Fail in restoreState, load home page.");
593 t.mMainView.loadUrl(BrowserSettings.getInstance()
594 .getHomePage());
595 }
596 } else {
597 // Create a new tab and don't restore the state yet, add it
598 // to the tab list
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700599 Tab t = new Tab(null, false, null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800600 t.mSavedState = inState.getBundle(WEBVIEW + i);
601 if (t.mSavedState != null) {
602 t.mUrl = t.mSavedState.getString(CURRURL);
603 t.mTitle = t.mSavedState.getString(CURRTITLE);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700604 // Need to maintain the app id and original url so we
605 // can possibly reuse this tab.
606 t.mAppId = t.mSavedState.getString(APPID);
607 t.mOriginalUrl = t.mSavedState.getString(ORIGINALURL);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800608 }
609 mTabs.add(t);
610 mTabQueue.add(t);
611 }
612 }
613 // Rebuild the tree of tabs. Do this after all tabs have been
614 // created/restored so that the parent tab exists.
615 for (int i = 0; i < numTabs; i++) {
616 final Bundle b = inState.getBundle(WEBVIEW + i);
617 final Tab t = getTab(i);
618 if (b != null && t != null) {
619 final int parentIndex = b.getInt(PARENTTAB, -1);
620 if (parentIndex != -1) {
621 final Tab parent = getTab(parentIndex);
622 if (parent != null) {
623 parent.addChildTab(t);
624 }
625 }
626 }
627 }
628 }
629 return true;
630 }
631
632 /**
633 * Free the memory in this order, 1) free the background tab; 2) free the
634 * WebView cache;
635 */
636 void freeMemory() {
637 // free the least frequently used background tab
638 Tab t = getLeastUsedTab();
639 if (t != null) {
640 Log.w(LOGTAG, "Free a tab in the browser");
641 freeTab(t);
642 // force a gc
643 System.gc();
644 return;
645 }
646
647 // free the WebView cache
648 Log.w(LOGTAG, "Free WebView cache");
649 WebView view = getCurrentWebView();
650 if (view != null) {
651 view.clearCache(false);
652 }
653 // force a gc
654 System.gc();
655 }
656
657 private Tab getLeastUsedTab() {
658 // Don't do anything if we only have 1 tab.
659 if (getTabCount() == 1) {
660 return null;
661 }
662
663 // Rip through the queue starting at the beginning and teardown the
664 // next available tab.
665 Tab t = null;
666 int i = 0;
667 final int queueSize = mTabQueue.size();
668 if (queueSize == 0) {
669 return null;
670 }
671 do {
672 t = mTabQueue.get(i++);
673 } while (i < queueSize && t != null && t.mMainView == null);
674
675 // Don't do anything if the last remaining tab is the current one.
676 if (t == getCurrentTab()) {
677 return null;
678 }
679
680 return t;
681 }
682
683 private void freeTab(Tab t) {
684 // Store the WebView's state.
685 saveState(t);
686
687 // Tear down the tab.
688 dismissSubWindow(t);
689 // Remove the WebView's settings from the BrowserSettings list of
690 // observers.
691 BrowserSettings.getInstance().deleteObserver(t.mMainView.getSettings());
692 t.mMainView.destroy();
693 t.mMainView = null;
694 }
695
696 /**
697 * Create a new subwindow unless a subwindow already exists.
698 * @return True if a new subwindow was created. False if one already exists.
699 */
700 void createSubWindow() {
701 Tab t = getTab(mCurrentTab);
702 if (t != null && t.mSubView == null) {
703 final View v = mInflateService.inflate(R.layout.browser_subwindow, null);
704 final WebView w = (WebView) v.findViewById(R.id.webview);
705 w.setMapTrackballToArrowKeys(false); // use trackball directly
706 final SubWindowClient subClient =
707 new SubWindowClient(mActivity.getWebViewClient());
708 final SubWindowChromeClient subChromeClient =
709 new SubWindowChromeClient(t,
710 mActivity.getWebChromeClient());
711 w.setWebViewClient(subClient);
712 w.setWebChromeClient(subChromeClient);
713 w.setDownloadListener(mActivity);
714 w.setOnCreateContextMenuListener(mActivity);
715 final BrowserSettings s = BrowserSettings.getInstance();
716 s.addObserver(w.getSettings()).update(s, null);
717 t.mSubView = w;
718 t.mSubViewClient = subClient;
719 t.mSubViewChromeClient = subChromeClient;
720 // FIXME: I really hate having to know the name of the view
721 // containing the webview.
722 t.mSubViewContainer = v.findViewById(R.id.subwindow_container);
723 final ImageButton cancel =
724 (ImageButton) v.findViewById(R.id.subwindow_close);
725 cancel.setOnClickListener(new OnClickListener() {
726 public void onClick(View v) {
727 subChromeClient.onCloseWindow(w);
728 }
729 });
730 }
731 }
732
733 /**
734 * Show the tab that contains the given WebView.
735 * @param view The WebView used to find the tab.
736 */
737 Tab getTabFromView(WebView view) {
738 final int size = getTabCount();
739 for (int i = 0; i < size; i++) {
740 final Tab t = getTab(i);
741 if (t.mSubView == view || t.mMainView == view) {
742 return t;
743 }
744 }
745 return null;
746 }
747
748 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700749 * Return the tab with the matching application id.
750 * @param id The application identifier.
751 */
752 Tab getTabFromId(String id) {
753 if (id == null) {
754 return null;
755 }
756 final int size = getTabCount();
757 for (int i = 0; i < size; i++) {
758 final Tab t = getTab(i);
759 if (id.equals(t.mAppId)) {
760 return t;
761 }
762 }
763 return null;
764 }
765
766 /**
767 * Recreate the main WebView of the given tab. Returns true if the WebView
768 * was deleted.
769 */
770 boolean recreateWebView(Tab t, String url) {
771 final WebView w = t.mMainView;
772 if (w != null) {
773 if (url != null && url.equals(t.mOriginalUrl)) {
774 // The original url matches the current url. Just go back to the
775 // first history item so we can load it faster than if we
776 // rebuilt the WebView.
777 final WebBackForwardList list = w.copyBackForwardList();
778 if (list != null) {
779 w.goBackOrForward(-list.getCurrentIndex());
780 w.clearHistory(); // maintains the current page.
781 return false;
782 }
783 }
784 // Remove the settings object from the global settings and destroy
785 // the WebView.
786 BrowserSettings.getInstance().deleteObserver(
787 t.mMainView.getSettings());
788 t.mMainView.destroy();
789 }
790 // Create a new WebView. If this tab is the current tab, we need to put
791 // back all the clients so force it to be the current tab.
792 t.mMainView = createNewWebView();
793 if (getCurrentTab() == t) {
794 setCurrentTab(t, true);
795 }
796 // Clear the saved state except for the app id and close-on-exit
797 // values.
798 t.mSavedState = null;
799 t.mUrl = null;
800 t.mTitle = null;
801 // Save the new url in order to avoid deleting the WebView.
802 t.mOriginalUrl = url;
803 return true;
804 }
805
806 /**
807 * Creates a new WebView and registers it with the global settings.
808 */
809 private WebView createNewWebView() {
810 // Create a new WebView
811 WebView w = new WebView(mActivity);
812 w.setMapTrackballToArrowKeys(false); // use trackball directly
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700813 // Enable the built-in zoom
814 w.getSettings().setBuiltInZoomControls(true);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700815 // Add this WebView to the settings observer list and update the
816 // settings
817 final BrowserSettings s = BrowserSettings.getInstance();
818 s.addObserver(w.getSettings()).update(s, null);
819 return w;
820 }
821
822 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800823 * Put the current tab in the background and set newTab as the current tab.
824 * @param newTab The new tab. If newTab is null, the current tab is not
825 * set.
826 */
827 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700828 return setCurrentTab(newTab, false);
829 }
830
831 /**
832 * If force is true, this method skips the check for newTab == current.
833 */
834 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800835 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700836 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800837 return true;
838 }
839 if (current != null) {
840 // Remove the current WebView and the container of the subwindow
841 putTabInBackground(current);
842 }
843
844 if (newTab == null) {
845 return false;
846 }
847
848 // Move the newTab to the end of the queue
849 int index = mTabQueue.indexOf(newTab);
850 if (index != -1) {
851 mTabQueue.remove(index);
852 }
853 mTabQueue.add(newTab);
854
855 WebView mainView;
856 WebView subView;
857
858 // Display the new current tab
859 mCurrentTab = mTabs.indexOf(newTab);
860 mainView = newTab.mMainView;
861 boolean needRestore = (mainView == null);
862 if (needRestore) {
863 // Same work as in createNewTab() except don't do new Tab()
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700864 newTab.mMainView = mainView = createNewWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800865 }
866 mainView.setWebViewClient(mActivity.getWebViewClient());
867 mainView.setWebChromeClient(mActivity.getWebChromeClient());
868 mainView.setOnCreateContextMenuListener(mActivity);
869 mainView.setDownloadListener(mActivity);
870 // Add the subwindow if it exists
871 if (newTab.mSubViewContainer != null) {
872 subView = newTab.mSubView;
873 subView.setWebViewClient(newTab.mSubViewClient);
874 subView.setWebChromeClient(newTab.mSubViewChromeClient);
875 subView.setOnCreateContextMenuListener(mActivity);
876 subView.setDownloadListener(mActivity);
877 }
878 if (needRestore) {
879 // Have to finish setCurrentTab work before calling restoreState
880 if (!restoreState(newTab.mSavedState, newTab)) {
881 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
882 }
883 }
884 return true;
885 }
886
887 /*
888 * Put the tab in the background using all the empty/background clients.
889 */
890 private void putTabInBackground(Tab t) {
891 WebView mainView = t.mMainView;
892 // Set an empty callback so that default actions are not triggered.
893 mainView.setWebViewClient(mEmptyClient);
894 mainView.setWebChromeClient(mBackgroundChromeClient);
895 mainView.setOnCreateContextMenuListener(null);
896 // Leave the DownloadManager attached so that downloads can start in
897 // a non-active window. This can happen when going to a site that does
898 // a redirect after a period of time. The user could have switched to
899 // another tab while waiting for the download to start.
900 mainView.setDownloadListener(mActivity);
901 WebView subView = t.mSubView;
902 if (subView != null) {
903 // Set an empty callback so that default actions are not triggered.
904 subView.setWebViewClient(mEmptyClient);
905 subView.setWebChromeClient(mBackgroundChromeClient);
906 subView.setOnCreateContextMenuListener(null);
907 subView.setDownloadListener(mActivity);
908 }
909 }
910
911 /*
912 * Dismiss the subwindow for the given tab.
913 */
914 void dismissSubWindow(Tab t) {
915 if (t != null && t.mSubView != null) {
916 BrowserSettings.getInstance().deleteObserver(
917 t.mSubView.getSettings());
918 t.mSubView.destroy();
919 t.mSubView = null;
920 t.mSubViewContainer = null;
921 }
922 }
923
924 /**
925 * Ensure that Tab t has a title, url, and favicon.
926 * @param t Tab to populate.
927 */
928 /* package */ void populatePickerData(Tab t) {
929 if (t == null || t.mMainView == null) {
930 return;
931 }
932 // FIXME: The only place we cared about subwindow was for
933 // bookmarking (i.e. not when saving state). Was this deliberate?
934 final WebBackForwardList list = t.mMainView.copyBackForwardList();
935 final WebHistoryItem item =
936 list != null ? list.getCurrentItem() : null;
937 populatePickerData(t, item);
938 }
939
940 // Populate the picker data
941 private void populatePickerData(Tab t, WebHistoryItem item) {
942 if (item != null) {
943 t.mUrl = item.getUrl();
944 t.mTitle = item.getTitle();
945 if (t.mTitle == null) {
946 t.mTitle = t.mUrl;
947 }
948 }
949 }
950
951 /**
952 * Clean up the data for all tabs.
953 */
954 /* package */ void wipeAllPickerData() {
955 int size = getTabCount();
956 for (int i = 0; i < size; i++) {
957 final Tab t = getTab(i);
958 if (t != null && t.mSavedState == null) {
959 t.mUrl = null;
960 t.mTitle = null;
961 }
962 }
963 }
964
965 /*
966 * Save the state for an individual tab.
967 */
968 private boolean saveState(Tab t) {
969 if (t != null) {
970 final WebView w = t.mMainView;
971 // If the WebView is null it means we ran low on memory and we
972 // already stored the saved state in mSavedState.
973 if (w == null) {
974 return true;
975 }
976 final Bundle b = new Bundle();
977 final WebBackForwardList list = w.saveState(b);
978 if (list != null) {
979 final File f = new File(mThumbnailDir, w.hashCode()
980 + "_pic.save");
981 if (w.savePicture(b, f)) {
982 b.putString("picture", f.getPath());
983 }
984 }
985
986 // Store some extra info for displaying the tab in the picker.
987 final WebHistoryItem item =
988 list != null ? list.getCurrentItem() : null;
989 populatePickerData(t, item);
990 if (t.mUrl != null) {
991 b.putString(CURRURL, t.mUrl);
992 }
993 if (t.mTitle != null) {
994 b.putString(CURRTITLE, t.mTitle);
995 }
996 b.putBoolean(CLOSEONEXIT, t.mCloseOnExit);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700997 if (t.mAppId != null) {
998 b.putString(APPID, t.mAppId);
999 }
1000 if (t.mOriginalUrl != null) {
1001 b.putString(ORIGINALURL, t.mOriginalUrl);
1002 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001003
1004 // Remember the parent tab so the relationship can be restored.
1005 if (t.mParentTab != null) {
1006 b.putInt(PARENTTAB, getTabIndex(t.mParentTab));
1007 }
1008
1009 // Remember the saved state.
1010 t.mSavedState = b;
1011 return true;
1012 }
1013 return false;
1014 }
1015
1016 /*
1017 * Restore the state of the tab.
1018 */
1019 private boolean restoreState(Bundle b, Tab t) {
1020 if (b == null) {
1021 return false;
1022 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001023 // Restore the internal state even if the WebView fails to restore.
1024 // This will maintain the app id, original url and close-on-exit values.
1025 t.mSavedState = null;
1026 t.mUrl = null;
1027 t.mTitle = null;
1028 t.mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1029 t.mAppId = b.getString(APPID);
1030 t.mOriginalUrl = b.getString(ORIGINALURL);
1031
The Android Open Source Project0c908882009-03-03 19:32:16 -08001032 final WebView w = t.mMainView;
1033 final WebBackForwardList list = w.restoreState(b);
1034 if (list == null) {
1035 return false;
1036 }
1037 if (b.containsKey("picture")) {
1038 final File f = new File(b.getString("picture"));
1039 w.restorePicture(b, f);
1040 f.delete();
1041 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001042 return true;
1043 }
1044}