blob: 3730ce1b53c699c407ab67fc99d911b939eeacd3 [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;
Patrick Scott2ed6edb2009-04-22 10:07:45 -040020import android.graphics.Picture;
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.net.http.SslError;
22import android.os.Bundle;
23import android.os.Message;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024import 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;
Patrick Scott2ed6edb2009-04-22 10:07:45 -040043import java.io.FileInputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044import java.util.ArrayList;
45import java.util.Vector;
46
47class TabControl {
48 // Log Tag
49 private static final String LOGTAG = "TabControl";
50 // Maximum number of tabs.
51 static final int MAX_TABS = 8;
52 // Static instance of an empty callback.
53 private static final WebViewClient mEmptyClient =
54 new WebViewClient();
55 // Instance of BackgroundChromeClient for background tabs.
56 private final BackgroundChromeClient mBackgroundChromeClient =
57 new BackgroundChromeClient();
58 // Private array of WebViews that are used as tabs.
59 private ArrayList<Tab> mTabs = new ArrayList<Tab>(MAX_TABS);
60 // Queue of most recently viewed tabs.
61 private ArrayList<Tab> mTabQueue = new ArrayList<Tab>(MAX_TABS);
62 // Current position in mTabs.
63 private int mCurrentTab = -1;
64 // A private instance of BrowserActivity to interface with when adding and
65 // switching between tabs.
66 private final BrowserActivity mActivity;
67 // Inflation service for making subwindows.
68 private final LayoutInflater mInflateService;
69 // Subclass of WebViewClient used in subwindows to notify the main
70 // WebViewClient of certain WebView activities.
71 private class SubWindowClient extends WebViewClient {
72 // The main WebViewClient.
73 private final WebViewClient mClient;
74
75 SubWindowClient(WebViewClient client) {
76 mClient = client;
77 }
78 @Override
79 public void doUpdateVisitedHistory(WebView view, String url,
80 boolean isReload) {
81 mClient.doUpdateVisitedHistory(view, url, isReload);
82 }
83 @Override
84 public boolean shouldOverrideUrlLoading(WebView view, String url) {
85 return mClient.shouldOverrideUrlLoading(view, url);
86 }
87 @Override
88 public void onReceivedSslError(WebView view, SslErrorHandler handler,
89 SslError error) {
90 mClient.onReceivedSslError(view, handler, error);
91 }
92 @Override
93 public void onReceivedHttpAuthRequest(WebView view,
94 HttpAuthHandler handler, String host, String realm) {
95 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
96 }
97 @Override
98 public void onFormResubmission(WebView view, Message dontResend,
99 Message resend) {
100 mClient.onFormResubmission(view, dontResend, resend);
101 }
102 @Override
103 public void onReceivedError(WebView view, int errorCode,
104 String description, String failingUrl) {
105 mClient.onReceivedError(view, errorCode, description, failingUrl);
106 }
107 }
108 // Subclass of WebChromeClient to display javascript dialogs.
109 private class SubWindowChromeClient extends WebChromeClient {
110 // This subwindow's tab.
111 private final Tab mTab;
112 // The main WebChromeClient.
113 private final WebChromeClient mClient;
114
115 SubWindowChromeClient(Tab t, WebChromeClient client) {
116 mTab = t;
117 mClient = client;
118 }
119 @Override
120 public void onProgressChanged(WebView view, int newProgress) {
121 mClient.onProgressChanged(view, newProgress);
122 }
123 @Override
124 public boolean onCreateWindow(WebView view, boolean dialog,
125 boolean userGesture, android.os.Message resultMsg) {
126 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
127 }
128 @Override
129 public void onCloseWindow(WebView window) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700130 if (Browser.DEBUG && window != mTab.mSubView) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800131 throw new AssertionError("Can't close the window");
132 }
133 mActivity.dismissSubWindow(mTab);
134 }
135 }
136 // Background WebChromeClient for focusing tabs
137 private class BackgroundChromeClient extends WebChromeClient {
138 @Override
139 public void onRequestFocus(WebView view) {
140 Tab t = getTabFromView(view);
141 if (t != getCurrentTab()) {
142 mActivity.showTab(t);
143 }
144 }
145 }
146
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400147 // Extra saved information for displaying the tab in the picker.
148 public static class PickerData {
149 String mUrl;
150 String mTitle;
151 float mScale;
152 int mScrollX;
153 int mScrollY;
154 int mWidth;
155 Picture mPicture;
156 // This can be null. When a new picture comes in, this view should be
157 // invalidated to show the new picture.
158 FakeWebView mFakeWebView;
159 }
160
The Android Open Source Project0c908882009-03-03 19:32:16 -0800161 /**
162 * Private class for maintaining Tabs with a main WebView and a subwindow.
163 */
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400164 public class Tab implements WebView.PictureListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800165 // Main WebView
166 private WebView mMainView;
167 // Subwindow WebView
168 private WebView mSubView;
169 // Subwindow container
170 private View mSubViewContainer;
171 // Subwindow callback
172 private SubWindowClient mSubViewClient;
173 // Subwindow chrome callback
174 private SubWindowChromeClient mSubViewChromeClient;
175 // Saved bundle for when we are running low on memory. It contains the
176 // information needed to restore the WebView if the user goes back to
177 // the tab.
178 private Bundle mSavedState;
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400179 // Data used when displaying the tab in the picker.
180 private PickerData mPickerData;
181
The Android Open Source Project0c908882009-03-03 19:32:16 -0800182 // Parent Tab. This is the Tab that created this Tab, or null
183 // if the Tab was created by the UI
184 private Tab mParentTab;
185 // Tab that constructed by this Tab. This is used when this
186 // Tab is destroyed, it clears all mParentTab values in the
187 // children.
188 private Vector<Tab> mChildTabs;
189
190 private Boolean mCloseOnExit;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700191 // Application identifier used to find tabs that another application
192 // wants to reuse.
193 private String mAppId;
194 // Keep the original url around to avoid killing the old WebView if the
195 // url has not changed.
196 private String mOriginalUrl;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800197
198 // Construct a new tab
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700199 private Tab(WebView w, boolean closeOnExit, String appId, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800200 mMainView = w;
201 mCloseOnExit = closeOnExit;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700202 mAppId = appId;
203 mOriginalUrl = url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800204 }
205
206 /**
207 * Return the top window of this tab; either the subwindow if it is not
208 * null or the main window.
209 * @return The top window of this tab.
210 */
211 public WebView getTopWindow() {
212 if (mSubView != null) {
213 return mSubView;
214 }
215 return mMainView;
216 }
217
218 /**
219 * Return the main window of this tab. Note: if a tab is freed in the
220 * background, this can return null. It is only guaranteed to be
221 * non-null for the current tab.
222 * @return The main WebView of this tab.
223 */
224 public WebView getWebView() {
225 return mMainView;
226 }
227
228 /**
229 * Return the subwindow of this tab or null if there is no subwindow.
230 * @return The subwindow of this tab or null.
231 */
232 public WebView getSubWebView() {
233 return mSubView;
234 }
235
236 /**
237 * Return the subwindow container of this tab or null if there is no
238 * subwindow.
239 * @return The subwindow's container View.
240 */
241 public View getSubWebViewContainer() {
242 return mSubViewContainer;
243 }
244
245 /**
246 * Get the url of this tab. Valid after calling populatePickerData, but
247 * before calling wipePickerData, or if the webview has been destroyed.
248 *
249 * @return The WebView's url or null.
250 */
251 public String getUrl() {
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400252 return mPickerData.mUrl;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800253 }
254
255 /**
256 * Get the title of this tab. Valid after calling populatePickerData,
257 * but before calling wipePickerData, or if the webview has been
258 * destroyed. If the url has no title, use the url instead.
259 *
260 * @return The WebView's title (or url) or null.
261 */
262 public String getTitle() {
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400263 return mPickerData.mTitle;
264 }
265
266 /**
267 * Returns the picker data.
268 */
269 public PickerData getPickerData() {
270 return mPickerData;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800271 }
272
273 private void setParentTab(Tab parent) {
274 mParentTab = parent;
275 // This tab may have been freed due to low memory. If that is the
276 // case, the parent tab index is already saved. If we are changing
277 // that index (most likely due to removing the parent tab) we must
278 // update the parent tab index in the saved Bundle.
279 if (mSavedState != null) {
280 if (parent == null) {
281 mSavedState.remove(PARENTTAB);
282 } else {
283 mSavedState.putInt(PARENTTAB, getTabIndex(parent));
284 }
285 }
286 }
287
288 /**
289 * When a Tab is created through the content of another Tab, then
290 * we associate the Tabs.
291 * @param child the Tab that was created from this Tab
292 */
293 public void addChildTab(Tab child) {
294 if (mChildTabs == null) {
295 mChildTabs = new Vector<Tab>();
296 }
297 mChildTabs.add(child);
298 child.setParentTab(this);
299 }
300
301 private void removeFromTree() {
302 // detach the children
303 if (mChildTabs != null) {
304 for(Tab t : mChildTabs) {
305 t.setParentTab(null);
306 }
307 }
308
309 // Find myself in my parent list
310 if (mParentTab != null) {
311 mParentTab.mChildTabs.remove(this);
312 }
313 }
314
315 /**
316 * If this Tab was created through another Tab, then this method
317 * returns that Tab.
318 * @return the Tab parent or null
319 */
320 public Tab getParentTab() {
321 return mParentTab;
322 }
323
324 /**
325 * Return whether this tab should be closed when it is backing out of
326 * the first page.
327 * @return TRUE if this tab should be closed when exit.
328 */
329 public boolean closeOnExit() {
330 return mCloseOnExit;
331 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400332
333 public void onNewPicture(WebView view, Picture p) {
334 if (mPickerData == null) {
335 return;
336 }
337
338 mPickerData.mPicture = p;
339 // Tell the FakeWebView to redraw.
340 if (mPickerData.mFakeWebView != null) {
341 mPickerData.mFakeWebView.invalidate();
342 }
343 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800344 };
345
346 // Directory to store thumbnails for each WebView.
347 private final File mThumbnailDir;
348
349 /**
350 * Construct a new TabControl object that interfaces with the given
351 * BrowserActivity instance.
352 * @param activity A BrowserActivity instance that TabControl will interface
353 * with.
354 */
355 TabControl(BrowserActivity activity) {
356 mActivity = activity;
357 mInflateService =
358 ((LayoutInflater) activity.getSystemService(
359 Context.LAYOUT_INFLATER_SERVICE));
360 mThumbnailDir = activity.getDir("thumbnails", 0);
361 }
362
363 File getThumbnailDir() {
364 return mThumbnailDir;
365 }
366
367 BrowserActivity getBrowserActivity() {
368 return mActivity;
369 }
370
371 /**
372 * Return the current tab's main WebView. This will always return the main
373 * WebView for a given tab and not a subwindow.
374 * @return The current tab's WebView.
375 */
376 WebView getCurrentWebView() {
377 Tab t = getTab(mCurrentTab);
378 if (t == null) {
379 return null;
380 }
381 return t.mMainView;
382 }
383
384 /**
385 * Return the current tab's top-level WebView. This can return a subwindow
386 * if one exists.
387 * @return The top-level WebView of the current tab.
388 */
389 WebView getCurrentTopWebView() {
390 Tab t = getTab(mCurrentTab);
391 if (t == null) {
392 return null;
393 }
394 return t.mSubView != null ? t.mSubView : t.mMainView;
395 }
396
397 /**
398 * Return the current tab's subwindow if it exists.
399 * @return The subwindow of the current tab or null if it doesn't exist.
400 */
401 WebView getCurrentSubWindow() {
402 Tab t = getTab(mCurrentTab);
403 if (t == null) {
404 return null;
405 }
406 return t.mSubView;
407 }
408
409 /**
410 * Return the tab at the specified index.
411 * @return The Tab for the specified index or null if the tab does not
412 * exist.
413 */
414 Tab getTab(int index) {
415 if (index >= 0 && index < mTabs.size()) {
416 return mTabs.get(index);
417 }
418 return null;
419 }
420
421 /**
422 * Return the current tab.
423 * @return The current tab.
424 */
425 Tab getCurrentTab() {
426 return getTab(mCurrentTab);
427 }
428
429 /**
430 * Return the current tab index.
431 * @return The current tab index
432 */
433 int getCurrentIndex() {
434 return mCurrentTab;
435 }
436
437 /**
438 * Given a Tab, find it's index
439 * @param Tab to find
440 * @return index of Tab or -1 if not found
441 */
442 int getTabIndex(Tab tab) {
Patrick Scottae641ac2009-04-20 13:51:49 -0400443 if (tab == null) {
444 return -1;
445 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800446 return mTabs.indexOf(tab);
447 }
448
449 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700450 * Create a new tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800451 * @return The newly createTab or null if we have reached the maximum
452 * number of open tabs.
453 */
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700454 Tab createNewTab(boolean closeOnExit, String appId, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800455 int size = mTabs.size();
456 // Return false if we have maxed out on tabs
457 if (MAX_TABS == size) {
458 return null;
459 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700460 final WebView w = createNewWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800461 // Create a new tab and add it to the tab list
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700462 Tab t = new Tab(w, closeOnExit, appId, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800463 mTabs.add(t);
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700464 // Initially put the tab in the background.
465 putTabInBackground(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800466 return t;
467 }
468
469 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700470 * Create a new tab with default values for closeOnExit(false),
471 * appId(null), and url(null).
472 */
473 Tab createNewTab() {
474 return createNewTab(false, null, null);
475 }
476
477 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800478 * Remove the tab from the list. If the tab is the current tab shown, the
479 * last created tab will be shown.
480 * @param t The tab to be removed.
481 */
482 boolean removeTab(Tab t) {
483 if (t == null) {
484 return false;
485 }
486 // Only remove the tab if it is the current one.
487 if (getCurrentTab() == t) {
488 putTabInBackground(t);
489 }
490
491 // Only destroy the WebView if it still exists.
492 if (t.mMainView != null) {
493 // Take down the sub window.
494 dismissSubWindow(t);
495 // Remove the WebView's settings from the BrowserSettings list of
496 // observers.
497 BrowserSettings.getInstance().deleteObserver(
498 t.mMainView.getSettings());
499 // Destroy the main view and subview
500 t.mMainView.destroy();
501 t.mMainView = null;
502 }
503 // clear it's references to parent and children
504 t.removeFromTree();
505
506 // Remove it from our list of tabs.
507 mTabs.remove(t);
508
509 // The tab indices have shifted, update all the saved state so we point
510 // to the correct index.
511 for (Tab tab : mTabs) {
512 if (tab.mChildTabs != null) {
513 for (Tab child : tab.mChildTabs) {
514 child.setParentTab(tab);
515 }
516 }
517 }
518
519
520 // This tab may have been pushed in to the background and then closed.
521 // If the saved state contains a picture file, delete the file.
522 if (t.mSavedState != null) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400523 if (t.mSavedState.containsKey(CURRPICTURE)) {
524 new File(t.mSavedState.getString(CURRPICTURE)).delete();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800525 }
526 }
527
528 // Remove it from the queue of viewed tabs.
529 mTabQueue.remove(t);
530 mCurrentTab = -1;
531 return true;
532 }
533
534 /**
535 * Clear the back/forward list for all the current tabs.
536 */
537 void clearHistory() {
538 int size = getTabCount();
539 for (int i = 0; i < size; i++) {
540 Tab t = mTabs.get(i);
541 // TODO: if a tab is freed due to low memory, its history is not
542 // cleared here.
543 if (t.mMainView != null) {
544 t.mMainView.clearHistory();
545 }
546 if (t.mSubView != null) {
547 t.mSubView.clearHistory();
548 }
549 }
550 }
551
552 /**
553 * Destroy all the tabs and subwindows
554 */
555 void destroy() {
556 BrowserSettings s = BrowserSettings.getInstance();
557 for (Tab t : mTabs) {
558 if (t.mMainView != null) {
559 dismissSubWindow(t);
560 s.deleteObserver(t.mMainView.getSettings());
561 t.mMainView.destroy();
562 t.mMainView = null;
563 }
564 }
565 mTabs.clear();
566 mTabQueue.clear();
567 }
568
569 /**
570 * Returns the number of tabs created.
571 * @return The number of tabs created.
572 */
573 int getTabCount() {
574 return mTabs.size();
575 }
576
577 // Used for saving and restoring each Tab
578 private static final String WEBVIEW = "webview";
579 private static final String NUMTABS = "numTabs";
580 private static final String CURRTAB = "currentTab";
581 private static final String CURRURL = "currentUrl";
582 private static final String CURRTITLE = "currentTitle";
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400583 private static final String CURRWIDTH = "currentWidth";
584 private static final String CURRPICTURE = "currentPicture";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800585 private static final String CLOSEONEXIT = "closeonexit";
586 private static final String PARENTTAB = "parentTab";
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700587 private static final String APPID = "appid";
588 private static final String ORIGINALURL = "originalUrl";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800589
590 /**
591 * Save the state of all the Tabs.
592 * @param outState The Bundle to save the state to.
593 */
594 void saveState(Bundle outState) {
595 final int numTabs = getTabCount();
596 outState.putInt(NUMTABS, numTabs);
597 final int index = getCurrentIndex();
598 outState.putInt(CURRTAB, (index >= 0 && index < numTabs) ? index : 0);
599 for (int i = 0; i < numTabs; i++) {
600 final Tab t = getTab(i);
601 if (saveState(t)) {
602 outState.putBundle(WEBVIEW + i, t.mSavedState);
603 }
604 }
605 }
606
607 /**
608 * Restore the state of all the tabs.
609 * @param inState The saved state of all the tabs.
610 * @return True if there were previous tabs that were restored. False if
611 * there was no saved state or restoring the state failed.
612 */
613 boolean restoreState(Bundle inState) {
614 final int numTabs = (inState == null)
615 ? -1 : inState.getInt(NUMTABS, -1);
616 if (numTabs == -1) {
617 return false;
618 } else {
619 final int currentTab = inState.getInt(CURRTAB, -1);
620 for (int i = 0; i < numTabs; i++) {
621 if (i == currentTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700622 Tab t = createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800623 // Me must set the current tab before restoring the state
624 // so that all the client classes are set.
625 setCurrentTab(t);
626 if (!restoreState(inState.getBundle(WEBVIEW + i), t)) {
627 Log.w(LOGTAG, "Fail in restoreState, load home page.");
628 t.mMainView.loadUrl(BrowserSettings.getInstance()
629 .getHomePage());
630 }
631 } else {
632 // Create a new tab and don't restore the state yet, add it
633 // to the tab list
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700634 Tab t = new Tab(null, false, null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800635 t.mSavedState = inState.getBundle(WEBVIEW + i);
636 if (t.mSavedState != null) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400637 populatePickerDataFromSavedState(t);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700638 // Need to maintain the app id and original url so we
639 // can possibly reuse this tab.
640 t.mAppId = t.mSavedState.getString(APPID);
641 t.mOriginalUrl = t.mSavedState.getString(ORIGINALURL);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800642 }
643 mTabs.add(t);
644 mTabQueue.add(t);
645 }
646 }
647 // Rebuild the tree of tabs. Do this after all tabs have been
648 // created/restored so that the parent tab exists.
649 for (int i = 0; i < numTabs; i++) {
650 final Bundle b = inState.getBundle(WEBVIEW + i);
651 final Tab t = getTab(i);
652 if (b != null && t != null) {
653 final int parentIndex = b.getInt(PARENTTAB, -1);
654 if (parentIndex != -1) {
655 final Tab parent = getTab(parentIndex);
656 if (parent != null) {
657 parent.addChildTab(t);
658 }
659 }
660 }
661 }
662 }
663 return true;
664 }
665
666 /**
667 * Free the memory in this order, 1) free the background tab; 2) free the
668 * WebView cache;
669 */
670 void freeMemory() {
671 // free the least frequently used background tab
672 Tab t = getLeastUsedTab();
673 if (t != null) {
674 Log.w(LOGTAG, "Free a tab in the browser");
675 freeTab(t);
676 // force a gc
677 System.gc();
678 return;
679 }
680
681 // free the WebView cache
682 Log.w(LOGTAG, "Free WebView cache");
683 WebView view = getCurrentWebView();
684 if (view != null) {
685 view.clearCache(false);
686 }
687 // force a gc
688 System.gc();
689 }
690
691 private Tab getLeastUsedTab() {
692 // Don't do anything if we only have 1 tab.
693 if (getTabCount() == 1) {
694 return null;
695 }
696
697 // Rip through the queue starting at the beginning and teardown the
698 // next available tab.
699 Tab t = null;
700 int i = 0;
701 final int queueSize = mTabQueue.size();
702 if (queueSize == 0) {
703 return null;
704 }
705 do {
706 t = mTabQueue.get(i++);
707 } while (i < queueSize && t != null && t.mMainView == null);
708
709 // Don't do anything if the last remaining tab is the current one.
710 if (t == getCurrentTab()) {
711 return null;
712 }
713
714 return t;
715 }
716
717 private void freeTab(Tab t) {
718 // Store the WebView's state.
719 saveState(t);
720
721 // Tear down the tab.
722 dismissSubWindow(t);
723 // Remove the WebView's settings from the BrowserSettings list of
724 // observers.
725 BrowserSettings.getInstance().deleteObserver(t.mMainView.getSettings());
726 t.mMainView.destroy();
727 t.mMainView = null;
728 }
729
730 /**
731 * Create a new subwindow unless a subwindow already exists.
732 * @return True if a new subwindow was created. False if one already exists.
733 */
734 void createSubWindow() {
735 Tab t = getTab(mCurrentTab);
736 if (t != null && t.mSubView == null) {
737 final View v = mInflateService.inflate(R.layout.browser_subwindow, null);
738 final WebView w = (WebView) v.findViewById(R.id.webview);
739 w.setMapTrackballToArrowKeys(false); // use trackball directly
740 final SubWindowClient subClient =
741 new SubWindowClient(mActivity.getWebViewClient());
742 final SubWindowChromeClient subChromeClient =
743 new SubWindowChromeClient(t,
744 mActivity.getWebChromeClient());
745 w.setWebViewClient(subClient);
746 w.setWebChromeClient(subChromeClient);
747 w.setDownloadListener(mActivity);
748 w.setOnCreateContextMenuListener(mActivity);
749 final BrowserSettings s = BrowserSettings.getInstance();
750 s.addObserver(w.getSettings()).update(s, null);
751 t.mSubView = w;
752 t.mSubViewClient = subClient;
753 t.mSubViewChromeClient = subChromeClient;
754 // FIXME: I really hate having to know the name of the view
755 // containing the webview.
756 t.mSubViewContainer = v.findViewById(R.id.subwindow_container);
757 final ImageButton cancel =
758 (ImageButton) v.findViewById(R.id.subwindow_close);
759 cancel.setOnClickListener(new OnClickListener() {
760 public void onClick(View v) {
761 subChromeClient.onCloseWindow(w);
762 }
763 });
764 }
765 }
766
767 /**
768 * Show the tab that contains the given WebView.
769 * @param view The WebView used to find the tab.
770 */
771 Tab getTabFromView(WebView view) {
772 final int size = getTabCount();
773 for (int i = 0; i < size; i++) {
774 final Tab t = getTab(i);
775 if (t.mSubView == view || t.mMainView == view) {
776 return t;
777 }
778 }
779 return null;
780 }
781
782 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700783 * Return the tab with the matching application id.
784 * @param id The application identifier.
785 */
786 Tab getTabFromId(String id) {
787 if (id == null) {
788 return null;
789 }
790 final int size = getTabCount();
791 for (int i = 0; i < size; i++) {
792 final Tab t = getTab(i);
793 if (id.equals(t.mAppId)) {
794 return t;
795 }
796 }
797 return null;
798 }
799
800 /**
801 * Recreate the main WebView of the given tab. Returns true if the WebView
802 * was deleted.
803 */
804 boolean recreateWebView(Tab t, String url) {
805 final WebView w = t.mMainView;
806 if (w != null) {
807 if (url != null && url.equals(t.mOriginalUrl)) {
808 // The original url matches the current url. Just go back to the
809 // first history item so we can load it faster than if we
810 // rebuilt the WebView.
811 final WebBackForwardList list = w.copyBackForwardList();
812 if (list != null) {
813 w.goBackOrForward(-list.getCurrentIndex());
814 w.clearHistory(); // maintains the current page.
815 return false;
816 }
817 }
818 // Remove the settings object from the global settings and destroy
819 // the WebView.
820 BrowserSettings.getInstance().deleteObserver(
821 t.mMainView.getSettings());
822 t.mMainView.destroy();
823 }
824 // Create a new WebView. If this tab is the current tab, we need to put
825 // back all the clients so force it to be the current tab.
826 t.mMainView = createNewWebView();
827 if (getCurrentTab() == t) {
828 setCurrentTab(t, true);
829 }
830 // Clear the saved state except for the app id and close-on-exit
831 // values.
832 t.mSavedState = null;
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400833 t.mPickerData = null;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700834 // Save the new url in order to avoid deleting the WebView.
835 t.mOriginalUrl = url;
836 return true;
837 }
838
839 /**
840 * Creates a new WebView and registers it with the global settings.
841 */
842 private WebView createNewWebView() {
843 // Create a new WebView
844 WebView w = new WebView(mActivity);
845 w.setMapTrackballToArrowKeys(false); // use trackball directly
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700846 // Enable the built-in zoom
847 w.getSettings().setBuiltInZoomControls(true);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700848 // Add this WebView to the settings observer list and update the
849 // settings
850 final BrowserSettings s = BrowserSettings.getInstance();
851 s.addObserver(w.getSettings()).update(s, null);
852 return w;
853 }
854
855 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800856 * Put the current tab in the background and set newTab as the current tab.
857 * @param newTab The new tab. If newTab is null, the current tab is not
858 * set.
859 */
860 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700861 return setCurrentTab(newTab, false);
862 }
863
864 /**
865 * If force is true, this method skips the check for newTab == current.
866 */
867 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800868 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700869 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800870 return true;
871 }
872 if (current != null) {
873 // Remove the current WebView and the container of the subwindow
874 putTabInBackground(current);
875 }
876
877 if (newTab == null) {
878 return false;
879 }
880
881 // Move the newTab to the end of the queue
882 int index = mTabQueue.indexOf(newTab);
883 if (index != -1) {
884 mTabQueue.remove(index);
885 }
886 mTabQueue.add(newTab);
887
888 WebView mainView;
889 WebView subView;
890
891 // Display the new current tab
892 mCurrentTab = mTabs.indexOf(newTab);
893 mainView = newTab.mMainView;
894 boolean needRestore = (mainView == null);
895 if (needRestore) {
896 // Same work as in createNewTab() except don't do new Tab()
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700897 newTab.mMainView = mainView = createNewWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800898 }
899 mainView.setWebViewClient(mActivity.getWebViewClient());
900 mainView.setWebChromeClient(mActivity.getWebChromeClient());
901 mainView.setOnCreateContextMenuListener(mActivity);
902 mainView.setDownloadListener(mActivity);
903 // Add the subwindow if it exists
904 if (newTab.mSubViewContainer != null) {
905 subView = newTab.mSubView;
906 subView.setWebViewClient(newTab.mSubViewClient);
907 subView.setWebChromeClient(newTab.mSubViewChromeClient);
908 subView.setOnCreateContextMenuListener(mActivity);
909 subView.setDownloadListener(mActivity);
910 }
911 if (needRestore) {
912 // Have to finish setCurrentTab work before calling restoreState
913 if (!restoreState(newTab.mSavedState, newTab)) {
914 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
915 }
916 }
917 return true;
918 }
919
920 /*
921 * Put the tab in the background using all the empty/background clients.
922 */
923 private void putTabInBackground(Tab t) {
924 WebView mainView = t.mMainView;
925 // Set an empty callback so that default actions are not triggered.
926 mainView.setWebViewClient(mEmptyClient);
927 mainView.setWebChromeClient(mBackgroundChromeClient);
928 mainView.setOnCreateContextMenuListener(null);
929 // Leave the DownloadManager attached so that downloads can start in
930 // a non-active window. This can happen when going to a site that does
931 // a redirect after a period of time. The user could have switched to
932 // another tab while waiting for the download to start.
933 mainView.setDownloadListener(mActivity);
934 WebView subView = t.mSubView;
935 if (subView != null) {
936 // Set an empty callback so that default actions are not triggered.
937 subView.setWebViewClient(mEmptyClient);
938 subView.setWebChromeClient(mBackgroundChromeClient);
939 subView.setOnCreateContextMenuListener(null);
940 subView.setDownloadListener(mActivity);
941 }
942 }
943
944 /*
945 * Dismiss the subwindow for the given tab.
946 */
947 void dismissSubWindow(Tab t) {
948 if (t != null && t.mSubView != null) {
949 BrowserSettings.getInstance().deleteObserver(
950 t.mSubView.getSettings());
951 t.mSubView.destroy();
952 t.mSubView = null;
953 t.mSubViewContainer = null;
954 }
955 }
956
957 /**
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400958 * Ensure that Tab t has data to display in the tab picker.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800959 * @param t Tab to populate.
960 */
961 /* package */ void populatePickerData(Tab t) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400962 if (t == null) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800963 return;
964 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400965
966 // mMainView == null indicates that the tab has been freed.
967 if (t.mMainView == null) {
968 populatePickerDataFromSavedState(t);
969 return;
970 }
971
The Android Open Source Project0c908882009-03-03 19:32:16 -0800972 // FIXME: The only place we cared about subwindow was for
973 // bookmarking (i.e. not when saving state). Was this deliberate?
974 final WebBackForwardList list = t.mMainView.copyBackForwardList();
975 final WebHistoryItem item =
976 list != null ? list.getCurrentItem() : null;
977 populatePickerData(t, item);
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400978
979 // This method is only called during the tab picker creation. At this
980 // point we need to listen for new pictures since the WebView is still
981 // active.
982 final WebView w = t.getTopWindow();
983 w.setPictureListener(t);
984 // Capture the picture here instead of populatePickerData since it can
985 // be called when saving the state of a tab.
986 t.mPickerData.mPicture = w.capturePicture();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800987 }
988
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400989 // Create the PickerData and populate it using the saved state of the tab.
990 private void populatePickerDataFromSavedState(Tab t) {
991 if (t.mSavedState == null) {
992 return;
993 }
994
995 final PickerData data = new PickerData();
996 final Bundle state = t.mSavedState;
997 data.mUrl = state.getString(CURRURL);
998 data.mTitle = state.getString(CURRTITLE);
999 data.mWidth = state.getInt(CURRWIDTH, 0);
1000 // XXX: These keys are from WebView.savePicture so if they change, this
1001 // will break.
1002 data.mScale = state.getFloat("scale", 1.0f);
1003 data.mScrollX = state.getInt("scrollX", 0);
1004 data.mScrollY = state.getInt("scrollY", 0);
1005
1006 if (state.containsKey(CURRPICTURE)) {
1007 final File f = new File(t.mSavedState.getString(CURRPICTURE));
1008 try {
1009 final FileInputStream in = new FileInputStream(f);
1010 data.mPicture = Picture.createFromStream(in);
1011 in.close();
1012 } catch (Exception ex) {
1013 // Ignore any problems with inflating the picture. We just
1014 // won't draw anything.
The Android Open Source Project0c908882009-03-03 19:32:16 -08001015 }
1016 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001017
1018 // Set the tab's picker data.
1019 t.mPickerData = data;
1020 }
1021
1022 // Populate the picker data using the given history item and the current
1023 // top WebView.
1024 private void populatePickerData(Tab t, WebHistoryItem item) {
1025 final PickerData data = new PickerData();
1026 if (item != null) {
1027 data.mUrl = item.getUrl();
1028 data.mTitle = item.getTitle();
1029 if (data.mTitle == null) {
1030 data.mTitle = data.mUrl;
1031 }
1032 }
1033 // We want to display the top window in the tab picker but use the url
1034 // and title of the main window.
1035 final WebView w = t.getTopWindow();
1036 data.mWidth = w.getWidth();
1037 data.mScale = w.getScale();
1038 data.mScrollX = w.getScrollX();
1039 data.mScrollY = w.getScrollY();
1040 t.mPickerData = data;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001041 }
1042
1043 /**
1044 * Clean up the data for all tabs.
1045 */
1046 /* package */ void wipeAllPickerData() {
1047 int size = getTabCount();
1048 for (int i = 0; i < size; i++) {
1049 final Tab t = getTab(i);
1050 if (t != null && t.mSavedState == null) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001051 t.mPickerData = null;
1052 }
1053 if (t.mMainView != null) {
1054 // Clear the picture listeners.
1055 t.mMainView.setPictureListener(null);
1056 if (t.mSubView != null) {
1057 t.mSubView.setPictureListener(null);
1058 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001059 }
1060 }
1061 }
1062
1063 /*
1064 * Save the state for an individual tab.
1065 */
1066 private boolean saveState(Tab t) {
1067 if (t != null) {
1068 final WebView w = t.mMainView;
1069 // If the WebView is null it means we ran low on memory and we
1070 // already stored the saved state in mSavedState.
1071 if (w == null) {
1072 return true;
1073 }
1074 final Bundle b = new Bundle();
1075 final WebBackForwardList list = w.saveState(b);
1076 if (list != null) {
1077 final File f = new File(mThumbnailDir, w.hashCode()
1078 + "_pic.save");
1079 if (w.savePicture(b, f)) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001080 b.putString(CURRPICTURE, f.getPath());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001081 }
1082 }
1083
1084 // Store some extra info for displaying the tab in the picker.
1085 final WebHistoryItem item =
1086 list != null ? list.getCurrentItem() : null;
1087 populatePickerData(t, item);
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001088
1089 // XXX: WebView.savePicture stores the scale and scroll positions
1090 // in the bundle so we don't have to do it here.
1091 final PickerData data = t.mPickerData;
1092 if (data.mUrl != null) {
1093 b.putString(CURRURL, data.mUrl);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001094 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001095 if (data.mTitle != null) {
1096 b.putString(CURRTITLE, data.mTitle);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001097 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001098 b.putInt(CURRWIDTH, data.mWidth);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001099 b.putBoolean(CLOSEONEXIT, t.mCloseOnExit);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001100 if (t.mAppId != null) {
1101 b.putString(APPID, t.mAppId);
1102 }
1103 if (t.mOriginalUrl != null) {
1104 b.putString(ORIGINALURL, t.mOriginalUrl);
1105 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001106
1107 // Remember the parent tab so the relationship can be restored.
1108 if (t.mParentTab != null) {
1109 b.putInt(PARENTTAB, getTabIndex(t.mParentTab));
1110 }
1111
1112 // Remember the saved state.
1113 t.mSavedState = b;
1114 return true;
1115 }
1116 return false;
1117 }
1118
1119 /*
1120 * Restore the state of the tab.
1121 */
1122 private boolean restoreState(Bundle b, Tab t) {
1123 if (b == null) {
1124 return false;
1125 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001126 // Restore the internal state even if the WebView fails to restore.
1127 // This will maintain the app id, original url and close-on-exit values.
1128 t.mSavedState = null;
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001129 t.mPickerData = null;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001130 t.mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1131 t.mAppId = b.getString(APPID);
1132 t.mOriginalUrl = b.getString(ORIGINALURL);
1133
The Android Open Source Project0c908882009-03-03 19:32:16 -08001134 final WebView w = t.mMainView;
1135 final WebBackForwardList list = w.restoreState(b);
1136 if (list == null) {
1137 return false;
1138 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001139 if (b.containsKey(CURRPICTURE)) {
1140 final File f = new File(b.getString(CURRPICTURE));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001141 w.restorePicture(b, f);
1142 f.delete();
1143 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001144 return true;
1145 }
1146}