blob: 69610bf44da61d6de613a7e7148c191430c6f922 [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;
The Android Open Source Projectb7775e12009-02-10 15:44:04 -080020import android.net.http.SslError;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070021import android.os.Bundle;
The Android Open Source Projectb7775e12009-02-10 15:44:04 -080022import android.os.Message;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070023import 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;
The Android Open Source Projectb7775e12009-02-10 15:44:04 -080030import android.webkit.HttpAuthHandler;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070031import android.webkit.JsPromptResult;
32import android.webkit.JsResult;
The Android Open Source Projectb7775e12009-02-10 15:44:04 -080033import android.webkit.SslErrorHandler;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070034import 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
The Android Open Source Projected217d92008-12-17 18:05:52 -080042import java.io.File;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070043import 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 }
The Android Open Source Projectb7775e12009-02-10 15:44:04 -080086 @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 }
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700106 }
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 }
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700134 }
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
The Android Open Source Projected217d92008-12-17 18:05:52 -0800176 private Boolean mCloseOnExit;
177
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700178 // Construct a new tab
The Android Open Source Projected217d92008-12-17 18:05:52 -0800179 private Tab(WebView w, boolean closeOnExit) {
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700180 mMainView = w;
The Android Open Source Projected217d92008-12-17 18:05:52 -0800181 mCloseOnExit = closeOnExit;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700182 }
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;
The Android Open Source Projected217d92008-12-17 18:05:52 -0800246 // 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 }
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700257 }
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 }
The Android Open Source Projected217d92008-12-17 18:05:52 -0800294
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 }
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700303 };
304
The Android Open Source Projected217d92008-12-17 18:05:52 -0800305 // Directory to store thumbnails for each WebView.
306 private final File mThumbnailDir;
307
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700308 /**
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));
The Android Open Source Projected217d92008-12-17 18:05:52 -0800319 mThumbnailDir = activity.getDir("thumbnails", 0);
320 }
321
322 File getThumbnailDir() {
323 return mThumbnailDir;
324 }
325
326 BrowserActivity getBrowserActivity() {
327 return mActivity;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700328 }
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 */
The Android Open Source Projected217d92008-12-17 18:05:52 -0800410 Tab createNewTab(boolean closeOnExit) {
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700411 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
The Android Open Source Projected217d92008-12-17 18:05:52 -0800424 Tab t = new Tab(w, closeOnExit);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700425 mTabs.add(t);
426 return t;
427 }
428
429 /**
430 * Remove the tab from the list. If the tab is the current tab shown, the
431 * last created tab will be shown.
432 * @param t The tab to be removed.
433 */
434 boolean removeTab(Tab t) {
435 if (t == null) {
436 return false;
437 }
438 // Only remove the tab if it is the current one.
439 if (getCurrentTab() == t) {
440 putTabInBackground(t);
441 }
442
443 // Only destroy the WebView if it still exists.
444 if (t.mMainView != null) {
445 // Take down the sub window.
446 dismissSubWindow(t);
447 // Remove the WebView's settings from the BrowserSettings list of
448 // observers.
449 BrowserSettings.getInstance().deleteObserver(
450 t.mMainView.getSettings());
451 // Destroy the main view and subview
452 t.mMainView.destroy();
453 t.mMainView = null;
454 }
455 // clear it's references to parent and children
456 t.removeFromTree();
457
458 // Remove it from our list of tabs.
459 mTabs.remove(t);
The Android Open Source Projected217d92008-12-17 18:05:52 -0800460
461 // The tab indices have shifted, update all the saved state so we point
462 // to the correct index.
463 for (Tab tab : mTabs) {
464 if (tab.mChildTabs != null) {
465 for (Tab child : tab.mChildTabs) {
466 child.setParentTab(tab);
467 }
468 }
469 }
470
471
472 // This tab may have been pushed in to the background and then closed.
473 // If the saved state contains a picture file, delete the file.
474 if (t.mSavedState != null) {
475 if (t.mSavedState.containsKey("picture")) {
476 new File(t.mSavedState.getString("picture")).delete();
477 }
478 }
479
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700480 // Remove it from the queue of viewed tabs.
481 mTabQueue.remove(t);
482 mCurrentTab = -1;
483 return true;
484 }
485
486 /**
487 * Clear the back/forward list for all the current tabs.
488 */
489 void clearHistory() {
490 int size = getTabCount();
491 for (int i = 0; i < size; i++) {
492 Tab t = mTabs.get(i);
493 // TODO: if a tab is freed due to low memory, its history is not
494 // cleared here.
495 if (t.mMainView != null) {
496 t.mMainView.clearHistory();
497 }
498 if (t.mSubView != null) {
499 t.mSubView.clearHistory();
500 }
501 }
502 }
503
504 /**
505 * Destroy all the tabs and subwindows
506 */
507 void destroy() {
508 BrowserSettings s = BrowserSettings.getInstance();
509 for (Tab t : mTabs) {
510 if (t.mMainView != null) {
511 dismissSubWindow(t);
512 s.deleteObserver(t.mMainView.getSettings());
513 t.mMainView.destroy();
514 t.mMainView = null;
515 }
516 }
517 mTabs.clear();
518 mTabQueue.clear();
519 }
520
521 /**
522 * Returns the number of tabs created.
523 * @return The number of tabs created.
524 */
525 int getTabCount() {
526 return mTabs.size();
527 }
528
529 // Used for saving and restoring each Tab
530 private static final String WEBVIEW = "webview";
531 private static final String NUMTABS = "numTabs";
532 private static final String CURRTAB = "currentTab";
533 private static final String CURRURL = "currentUrl";
534 private static final String CURRTITLE = "currentTitle";
The Android Open Source Projected217d92008-12-17 18:05:52 -0800535 private static final String CLOSEONEXIT = "closeonexit";
536 private static final String PARENTTAB = "parentTab";
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700537
538 /**
539 * Save the state of all the Tabs.
540 * @param outState The Bundle to save the state to.
541 */
542 void saveState(Bundle outState) {
543 final int numTabs = getTabCount();
544 outState.putInt(NUMTABS, numTabs);
545 final int index = getCurrentIndex();
546 outState.putInt(CURRTAB, (index >= 0 && index < numTabs) ? index : 0);
547 for (int i = 0; i < numTabs; i++) {
548 final Tab t = getTab(i);
549 if (saveState(t)) {
550 outState.putBundle(WEBVIEW + i, t.mSavedState);
551 }
552 }
553 }
554
555 /**
556 * Restore the state of all the tabs.
557 * @param inState The saved state of all the tabs.
558 * @return True if there were previous tabs that were restored. False if
559 * there was no saved state or restoring the state failed.
560 */
561 boolean restoreState(Bundle inState) {
562 final int numTabs = (inState == null)
563 ? -1 : inState.getInt(NUMTABS, -1);
564 if (numTabs == -1) {
565 return false;
566 } else {
567 final int currentTab = inState.getInt(CURRTAB, -1);
568 for (int i = 0; i < numTabs; i++) {
569 if (i == currentTab) {
The Android Open Source Projected217d92008-12-17 18:05:52 -0800570 Tab t = createNewTab(false);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700571 // Me must set the current tab before restoring the state
572 // so that all the client classes are set.
573 setCurrentTab(t);
574 if (!restoreState(inState.getBundle(WEBVIEW + i), t)) {
575 Log.w(LOGTAG, "Fail in restoreState, load home page.");
576 t.mMainView.loadUrl(BrowserSettings.getInstance()
577 .getHomePage());
578 }
579 } else {
580 // Create a new tab and don't restore the state yet, add it
581 // to the tab list
The Android Open Source Projected217d92008-12-17 18:05:52 -0800582 Tab t = new Tab(null, false);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700583 t.mSavedState = inState.getBundle(WEBVIEW + i);
584 if (t.mSavedState != null) {
585 t.mUrl = t.mSavedState.getString(CURRURL);
586 t.mTitle = t.mSavedState.getString(CURRTITLE);
587 }
588 mTabs.add(t);
589 mTabQueue.add(t);
590 }
591 }
The Android Open Source Projected217d92008-12-17 18:05:52 -0800592 // Rebuild the tree of tabs. Do this after all tabs have been
593 // created/restored so that the parent tab exists.
594 for (int i = 0; i < numTabs; i++) {
595 final Bundle b = inState.getBundle(WEBVIEW + i);
596 final Tab t = getTab(i);
597 if (b != null && t != null) {
598 final int parentIndex = b.getInt(PARENTTAB, -1);
599 if (parentIndex != -1) {
600 final Tab parent = getTab(parentIndex);
601 if (parent != null) {
602 parent.addChildTab(t);
603 }
604 }
605 }
606 }
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700607 }
608 return true;
609 }
610
611 /**
612 * Free the memory in this order, 1) free the background tab; 2) free the
613 * WebView cache;
614 */
615 void freeMemory() {
616 // free the least frequently used background tab
617 Tab t = getLeastUsedTab();
618 if (t != null) {
619 Log.w(LOGTAG, "Free a tab in the browser");
620 freeTab(t);
621 // force a gc
622 System.gc();
623 return;
624 }
625
626 // free the WebView cache
627 Log.w(LOGTAG, "Free WebView cache");
628 WebView view = getCurrentWebView();
629 view.clearCache(false);
630 // force a gc
631 System.gc();
632 }
633
634 private Tab getLeastUsedTab() {
635 // Don't do anything if we only have 1 tab.
636 if (getTabCount() == 1) {
637 return null;
638 }
639
640 // Rip through the queue starting at the beginning and teardown the
641 // next available tab.
642 Tab t = null;
643 int i = 0;
644 final int queueSize = mTabQueue.size();
645 if (queueSize == 0) {
646 return null;
647 }
648 do {
649 t = mTabQueue.get(i++);
650 } while (i < queueSize && t != null && t.mMainView == null);
651
652 // Don't do anything if the last remaining tab is the current one.
653 if (t == getCurrentTab()) {
654 return null;
655 }
656
657 return t;
658 }
659
660 private void freeTab(Tab t) {
661 // Store the WebView's state.
662 saveState(t);
663
664 // Tear down the tab.
665 dismissSubWindow(t);
666 // Remove the WebView's settings from the BrowserSettings list of
667 // observers.
668 BrowserSettings.getInstance().deleteObserver(t.mMainView.getSettings());
669 t.mMainView.destroy();
670 t.mMainView = null;
671 }
672
673 /**
674 * Create a new subwindow unless a subwindow already exists.
675 * @return True if a new subwindow was created. False if one already exists.
676 */
677 void createSubWindow() {
678 Tab t = getTab(mCurrentTab);
679 if (t != null && t.mSubView == null) {
680 final View v = mInflateService.inflate(R.layout.browser_subwindow, null);
681 final WebView w = (WebView) v.findViewById(R.id.webview);
682 w.setMapTrackballToArrowKeys(false); // use trackball directly
683 final SubWindowClient subClient =
684 new SubWindowClient(mActivity.getWebViewClient());
685 final SubWindowChromeClient subChromeClient =
686 new SubWindowChromeClient(t,
687 mActivity.getWebChromeClient());
688 w.setWebViewClient(subClient);
689 w.setWebChromeClient(subChromeClient);
690 w.setDownloadListener(mActivity);
691 w.setOnCreateContextMenuListener(mActivity);
692 final BrowserSettings s = BrowserSettings.getInstance();
693 s.addObserver(w.getSettings()).update(s, null);
694 t.mSubView = w;
695 t.mSubViewClient = subClient;
696 t.mSubViewChromeClient = subChromeClient;
697 // FIXME: I really hate having to know the name of the view
698 // containing the webview.
699 t.mSubViewContainer = v.findViewById(R.id.subwindow_container);
700 final ImageButton cancel =
701 (ImageButton) v.findViewById(R.id.subwindow_close);
702 cancel.setOnClickListener(new OnClickListener() {
703 public void onClick(View v) {
704 subChromeClient.onCloseWindow(w);
705 }
706 });
707 }
708 }
709
710 /**
711 * Show the tab that contains the given WebView.
712 * @param view The WebView used to find the tab.
713 */
714 Tab getTabFromView(WebView view) {
715 final int size = getTabCount();
716 for (int i = 0; i < size; i++) {
717 final Tab t = getTab(i);
718 if (t.mSubView == view || t.mMainView == view) {
719 return t;
720 }
721 }
722 return null;
723 }
724
725 /**
726 * Put the current tab in the background and set newTab as the current tab.
727 * @param newTab The new tab. If newTab is null, the current tab is not
728 * set.
729 */
730 boolean setCurrentTab(Tab newTab) {
731 Tab current = getTab(mCurrentTab);
732 if (current == newTab) {
733 return true;
734 }
735 if (current != null) {
736 // Remove the current WebView and the container of the subwindow
737 putTabInBackground(current);
738 }
739
740 if (newTab == null) {
741 return false;
742 }
743
744 // Move the newTab to the end of the queue
745 int index = mTabQueue.indexOf(newTab);
746 if (index != -1) {
747 mTabQueue.remove(index);
748 }
749 mTabQueue.add(newTab);
750
751 WebView mainView;
752 WebView subView;
753
754 // Display the new current tab
755 mCurrentTab = mTabs.indexOf(newTab);
756 mainView = newTab.mMainView;
757 boolean needRestore = (mainView == null);
758 if (needRestore) {
759 // Same work as in createNewTab() except don't do new Tab()
760 newTab.mMainView = mainView = new WebView(mActivity);
761 mainView.setMapTrackballToArrowKeys(false); // use t-ball directly
762
763 // Add this WebView to the settings observer list and update the
764 // settings
765 final BrowserSettings s = BrowserSettings.getInstance();
766 s.addObserver(mainView.getSettings()).update(s, null);
767 }
768 mainView.setWebViewClient(mActivity.getWebViewClient());
769 mainView.setWebChromeClient(mActivity.getWebChromeClient());
770 mainView.setOnCreateContextMenuListener(mActivity);
771 mainView.setDownloadListener(mActivity);
772 // Add the subwindow if it exists
773 if (newTab.mSubViewContainer != null) {
774 subView = newTab.mSubView;
775 subView.setWebViewClient(newTab.mSubViewClient);
776 subView.setWebChromeClient(newTab.mSubViewChromeClient);
777 subView.setOnCreateContextMenuListener(mActivity);
778 subView.setDownloadListener(mActivity);
779 }
780 if (needRestore) {
781 // Have to finish setCurrentTab work before calling restoreState
782 if (!restoreState(newTab.mSavedState, newTab)) {
783 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
784 }
785 }
786 return true;
787 }
788
789 /*
790 * Put the tab in the background using all the empty/background clients.
791 */
792 private void putTabInBackground(Tab t) {
793 WebView mainView = t.mMainView;
794 // Set an empty callback so that default actions are not triggered.
795 mainView.setWebViewClient(mEmptyClient);
796 mainView.setWebChromeClient(mBackgroundChromeClient);
797 mainView.setOnCreateContextMenuListener(null);
798 // Leave the DownloadManager attached so that downloads can start in
799 // a non-active window. This can happen when going to a site that does
800 // a redirect after a period of time. The user could have switched to
801 // another tab while waiting for the download to start.
802 mainView.setDownloadListener(mActivity);
803 WebView subView = t.mSubView;
804 if (subView != null) {
805 // Set an empty callback so that default actions are not triggered.
806 subView.setWebViewClient(mEmptyClient);
807 subView.setWebChromeClient(mBackgroundChromeClient);
808 subView.setOnCreateContextMenuListener(null);
809 subView.setDownloadListener(mActivity);
810 }
811 }
812
813 /*
814 * Dismiss the subwindow for the given tab.
815 */
816 void dismissSubWindow(Tab t) {
817 if (t != null && t.mSubView != null) {
818 BrowserSettings.getInstance().deleteObserver(
819 t.mSubView.getSettings());
820 t.mSubView.destroy();
821 t.mSubView = null;
822 t.mSubViewContainer = null;
823 }
824 }
825
826 /**
827 * Ensure that Tab t has a title, url, and favicon.
828 * @param t Tab to populate.
829 */
830 /* package */ void populatePickerData(Tab t) {
831 if (t == null || t.mMainView == null) {
832 return;
833 }
834 // FIXME: The only place we cared about subwindow was for
835 // bookmarking (i.e. not when saving state). Was this deliberate?
836 final WebBackForwardList list = t.mMainView.copyBackForwardList();
837 final WebHistoryItem item =
838 list != null ? list.getCurrentItem() : null;
839 populatePickerData(t, item);
840 }
841
842 // Populate the picker data
843 private void populatePickerData(Tab t, WebHistoryItem item) {
844 if (item != null) {
845 t.mUrl = item.getUrl();
846 t.mTitle = item.getTitle();
847 if (t.mTitle == null) {
848 t.mTitle = t.mUrl;
849 }
850 }
851 }
852
853 /**
854 * Clean up the data for all tabs.
855 */
856 /* package */ void wipeAllPickerData() {
857 int size = getTabCount();
858 for (int i = 0; i < size; i++) {
859 final Tab t = getTab(i);
860 if (t != null && t.mSavedState == null) {
861 t.mUrl = null;
862 t.mTitle = null;
863 }
864 }
865 }
866
867 /*
868 * Save the state for an individual tab.
869 */
870 private boolean saveState(Tab t) {
871 if (t != null) {
872 final WebView w = t.mMainView;
873 // If the WebView is null it means we ran low on memory and we
874 // already stored the saved state in mSavedState.
875 if (w == null) {
876 return true;
877 }
878 final Bundle b = new Bundle();
879 final WebBackForwardList list = w.saveState(b);
The Android Open Source Projected217d92008-12-17 18:05:52 -0800880 if (list != null) {
881 final File f = new File(mThumbnailDir, w.hashCode()
882 + "_pic.save");
883 if (w.savePicture(b, f)) {
884 b.putString("picture", f.getPath());
885 }
886 }
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700887
888 // Store some extra info for displaying the tab in the picker.
The Android Open Source Projected217d92008-12-17 18:05:52 -0800889 final WebHistoryItem item =
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700890 list != null ? list.getCurrentItem() : null;
891 populatePickerData(t, item);
892 if (t.mUrl != null) {
893 b.putString(CURRURL, t.mUrl);
894 }
895 if (t.mTitle != null) {
896 b.putString(CURRTITLE, t.mTitle);
897 }
The Android Open Source Projected217d92008-12-17 18:05:52 -0800898 b.putBoolean(CLOSEONEXIT, t.mCloseOnExit);
899
900 // Remember the parent tab so the relationship can be restored.
901 if (t.mParentTab != null) {
902 b.putInt(PARENTTAB, getTabIndex(t.mParentTab));
903 }
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700904
905 // Remember the saved state.
906 t.mSavedState = b;
907 return true;
908 }
909 return false;
910 }
911
912 /*
913 * Restore the state of the tab.
914 */
915 private boolean restoreState(Bundle b, Tab t) {
916 if (b == null) {
917 return false;
918 }
919 final WebView w = t.mMainView;
920 final WebBackForwardList list = w.restoreState(b);
921 if (list == null) {
922 return false;
923 }
The Android Open Source Projected217d92008-12-17 18:05:52 -0800924 if (b.containsKey("picture")) {
925 final File f = new File(b.getString("picture"));
926 w.restorePicture(b, f);
927 f.delete();
928 }
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700929 t.mSavedState = null;
930 t.mUrl = null;
931 t.mTitle = null;
The Android Open Source Projected217d92008-12-17 18:05:52 -0800932 t.mCloseOnExit = b.getBoolean(CLOSEONEXIT);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700933 return true;
934 }
935}