blob: 6e4bae2e32b00318df13811b3cbca58d8b7f4d42 [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;
Steve Block2bc69912009-07-30 14:45:13 +010041import android.widget.LinearLayout;
The Android Open Source Project0c908882009-03-03 19:32:16 -080042
43import java.io.File;
Patrick Scott2ed6edb2009-04-22 10:07:45 -040044import java.io.FileInputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import java.util.ArrayList;
46import java.util.Vector;
47
48class TabControl {
49 // Log Tag
50 private static final String LOGTAG = "TabControl";
51 // Maximum number of tabs.
52 static final int MAX_TABS = 8;
53 // Static instance of an empty callback.
54 private static final WebViewClient mEmptyClient =
55 new WebViewClient();
56 // Instance of BackgroundChromeClient for background tabs.
57 private final BackgroundChromeClient mBackgroundChromeClient =
58 new BackgroundChromeClient();
59 // Private array of WebViews that are used as tabs.
60 private ArrayList<Tab> mTabs = new ArrayList<Tab>(MAX_TABS);
61 // Queue of most recently viewed tabs.
62 private ArrayList<Tab> mTabQueue = new ArrayList<Tab>(MAX_TABS);
63 // Current position in mTabs.
64 private int mCurrentTab = -1;
65 // A private instance of BrowserActivity to interface with when adding and
66 // switching between tabs.
67 private final BrowserActivity mActivity;
68 // Inflation service for making subwindows.
69 private final LayoutInflater mInflateService;
70 // Subclass of WebViewClient used in subwindows to notify the main
71 // WebViewClient of certain WebView activities.
Cary Clarka9771242009-08-11 16:42:26 -040072 private static class SubWindowClient extends WebViewClient {
The Android Open Source Project0c908882009-03-03 19:32:16 -080073 // The main WebViewClient.
74 private final WebViewClient mClient;
75
76 SubWindowClient(WebViewClient client) {
77 mClient = client;
78 }
79 @Override
80 public void doUpdateVisitedHistory(WebView view, String url,
81 boolean isReload) {
82 mClient.doUpdateVisitedHistory(view, url, isReload);
83 }
84 @Override
85 public boolean shouldOverrideUrlLoading(WebView view, String url) {
86 return mClient.shouldOverrideUrlLoading(view, url);
87 }
88 @Override
89 public void onReceivedSslError(WebView view, SslErrorHandler handler,
90 SslError error) {
91 mClient.onReceivedSslError(view, handler, error);
92 }
93 @Override
94 public void onReceivedHttpAuthRequest(WebView view,
95 HttpAuthHandler handler, String host, String realm) {
96 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
97 }
98 @Override
99 public void onFormResubmission(WebView view, Message dontResend,
100 Message resend) {
101 mClient.onFormResubmission(view, dontResend, resend);
102 }
103 @Override
104 public void onReceivedError(WebView view, int errorCode,
105 String description, String failingUrl) {
106 mClient.onReceivedError(view, errorCode, description, failingUrl);
107 }
Patrick Scotte9fc4492009-08-12 09:15:51 -0400108 @Override
109 public boolean shouldOverrideKeyEvent(WebView view,
110 android.view.KeyEvent event) {
111 return mClient.shouldOverrideKeyEvent(view, event);
112 }
113 @Override
114 public void onUnhandledKeyEvent(WebView view,
115 android.view.KeyEvent event) {
116 mClient.onUnhandledKeyEvent(view, event);
117 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118 }
119 // Subclass of WebChromeClient to display javascript dialogs.
120 private class SubWindowChromeClient extends WebChromeClient {
121 // This subwindow's tab.
122 private final Tab mTab;
123 // The main WebChromeClient.
124 private final WebChromeClient mClient;
125
126 SubWindowChromeClient(Tab t, WebChromeClient client) {
127 mTab = t;
128 mClient = client;
129 }
130 @Override
131 public void onProgressChanged(WebView view, int newProgress) {
132 mClient.onProgressChanged(view, newProgress);
133 }
134 @Override
135 public boolean onCreateWindow(WebView view, boolean dialog,
136 boolean userGesture, android.os.Message resultMsg) {
137 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
138 }
139 @Override
140 public void onCloseWindow(WebView window) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700141 if (Browser.DEBUG && window != mTab.mSubView) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800142 throw new AssertionError("Can't close the window");
143 }
144 mActivity.dismissSubWindow(mTab);
145 }
146 }
147 // Background WebChromeClient for focusing tabs
148 private class BackgroundChromeClient extends WebChromeClient {
149 @Override
150 public void onRequestFocus(WebView view) {
151 Tab t = getTabFromView(view);
152 if (t != getCurrentTab()) {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400153 mActivity.switchToTab(getTabIndex(t));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800154 }
155 }
156 }
157
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400158 // Extra saved information for displaying the tab in the picker.
159 public static class PickerData {
160 String mUrl;
161 String mTitle;
162 float mScale;
163 int mScrollX;
164 int mScrollY;
165 int mWidth;
166 Picture mPicture;
167 // This can be null. When a new picture comes in, this view should be
168 // invalidated to show the new picture.
169 FakeWebView mFakeWebView;
170 }
171
The Android Open Source Project0c908882009-03-03 19:32:16 -0800172 /**
173 * Private class for maintaining Tabs with a main WebView and a subwindow.
174 */
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400175 public class Tab implements WebView.PictureListener {
Steve Block2bc69912009-07-30 14:45:13 +0100176 // The Geolocation permissions prompt
177 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
178 private View mContainer;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800179 // Main WebView
180 private WebView mMainView;
181 // Subwindow WebView
182 private WebView mSubView;
183 // Subwindow container
184 private View mSubViewContainer;
185 // Subwindow callback
186 private SubWindowClient mSubViewClient;
187 // Subwindow chrome callback
188 private SubWindowChromeClient mSubViewChromeClient;
189 // Saved bundle for when we are running low on memory. It contains the
190 // information needed to restore the WebView if the user goes back to
191 // the tab.
192 private Bundle mSavedState;
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400193 // Data used when displaying the tab in the picker.
194 private PickerData mPickerData;
195
The Android Open Source Project0c908882009-03-03 19:32:16 -0800196 // Parent Tab. This is the Tab that created this Tab, or null
197 // if the Tab was created by the UI
198 private Tab mParentTab;
199 // Tab that constructed by this Tab. This is used when this
200 // Tab is destroyed, it clears all mParentTab values in the
201 // children.
202 private Vector<Tab> mChildTabs;
203
204 private Boolean mCloseOnExit;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700205 // Application identifier used to find tabs that another application
206 // wants to reuse.
207 private String mAppId;
208 // Keep the original url around to avoid killing the old WebView if the
209 // url has not changed.
210 private String mOriginalUrl;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800211
Ben Murdochbff2d602009-07-01 20:19:05 +0100212 private ErrorConsoleView mErrorConsole;
Grace Klobaeb6eef42009-09-15 17:56:32 -0700213 // the lock icon type and previous lock icon type for the tab
214 private int mSavedLockIconType;
215 private int mSavedPrevLockIconType;
Ben Murdochbff2d602009-07-01 20:19:05 +0100216
The Android Open Source Project0c908882009-03-03 19:32:16 -0800217 // Construct a new tab
Steve Block2bc69912009-07-30 14:45:13 +0100218 private Tab(WebView w, boolean closeOnExit, String appId, String url, Context context) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800219 mCloseOnExit = closeOnExit;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700220 mAppId = appId;
221 mOriginalUrl = url;
Grace Klobaeb6eef42009-09-15 17:56:32 -0700222 mSavedLockIconType = BrowserActivity.LOCK_ICON_UNSECURE;
223 mSavedPrevLockIconType = BrowserActivity.LOCK_ICON_UNSECURE;
Steve Block2bc69912009-07-30 14:45:13 +0100224
225 // The tab consists of a container view, which contains the main
226 // WebView, as well as any other UI elements associated with the tab.
Steve Block2bc69912009-07-30 14:45:13 +0100227 LayoutInflater factory = LayoutInflater.from(context);
228 mContainer = factory.inflate(R.layout.tab, null);
229
230 mGeolocationPermissionsPrompt =
231 (GeolocationPermissionsPrompt) mContainer.findViewById(
232 R.id.geolocation_permissions_prompt);
233
234 setWebView(w);
235 }
236
237 /**
238 * Sets the WebView for this tab, correctly removing the old WebView
Patrick Scottd0119532009-09-17 08:00:31 -0400239 * from the container view.
Steve Block2bc69912009-07-30 14:45:13 +0100240 */
241 public void setWebView(WebView w) {
242 if (mMainView == w) {
243 return;
244 }
245 // If the WebView is changing, the page will be reloaded, so any ongoing Geolocation
246 // permission requests are void.
247 mGeolocationPermissionsPrompt.hide();
248
Patrick Scottd0119532009-09-17 08:00:31 -0400249 // Just remove the old one.
250 FrameLayout wrapper =
251 (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
252 wrapper.removeView(mMainView);
Steve Block2bc69912009-07-30 14:45:13 +0100253 mMainView = w;
Patrick Scottd0119532009-09-17 08:00:31 -0400254 }
255
256 /**
257 * This method attaches both the WebView and any sub window to the
258 * given content view.
259 */
260 public void attachTabToContentView(ViewGroup content) {
261 if (mMainView == null) {
262 return;
263 }
264
265 // Attach the WebView to the container and then attach the
266 // container to the content view.
267 FrameLayout wrapper =
268 (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
269 wrapper.addView(mMainView);
270 content.addView(mContainer, BrowserActivity.COVER_SCREEN_PARAMS);
271 attachSubWindow(content);
272 }
273
274 /**
275 * Remove the WebView and any sub window from the given content view.
276 */
277 public void removeTabFromContentView(ViewGroup content) {
278 if (mMainView == null) {
279 return;
280 }
281
282 // Remove the container from the content and then remove the
283 // WebView from the container. This will trigger a focus change
284 // needed by WebView.
285 FrameLayout wrapper =
286 (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
287 wrapper.removeView(mMainView);
288 content.removeView(mContainer);
289 removeSubWindow(content);
290 }
291
292 /**
293 * Attach the sub window to the content view.
294 */
295 public void attachSubWindow(ViewGroup content) {
296 if (mSubView != null) {
297 content.addView(mSubViewContainer,
298 BrowserActivity.COVER_SCREEN_PARAMS);
299 }
300 }
301
302 /**
303 * Remove the sub window from the content view.
304 */
305 public void removeSubWindow(ViewGroup content) {
306 if (mSubView != null) {
307 content.removeView(mSubViewContainer);
Steve Block2bc69912009-07-30 14:45:13 +0100308 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800309 }
310
311 /**
312 * Return the top window of this tab; either the subwindow if it is not
313 * null or the main window.
314 * @return The top window of this tab.
315 */
316 public WebView getTopWindow() {
317 if (mSubView != null) {
318 return mSubView;
319 }
320 return mMainView;
321 }
322
323 /**
324 * Return the main window of this tab. Note: if a tab is freed in the
325 * background, this can return null. It is only guaranteed to be
326 * non-null for the current tab.
327 * @return The main WebView of this tab.
328 */
329 public WebView getWebView() {
330 return mMainView;
331 }
332
333 /**
Steve Block2bc69912009-07-30 14:45:13 +0100334 * @return The geolocation permissions prompt for this tab.
335 */
336 public GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
337 return mGeolocationPermissionsPrompt;
338 }
339
340 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800341 * Return the subwindow of this tab or null if there is no subwindow.
342 * @return The subwindow of this tab or null.
343 */
344 public WebView getSubWebView() {
345 return mSubView;
346 }
347
348 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800349 * Get the url of this tab. Valid after calling populatePickerData, but
350 * before calling wipePickerData, or if the webview has been destroyed.
351 *
352 * @return The WebView's url or null.
353 */
354 public String getUrl() {
Patrick Scott20abe042009-04-28 08:03:29 -0400355 if (mPickerData != null) {
356 return mPickerData.mUrl;
357 }
358 return null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800359 }
360
361 /**
362 * Get the title of this tab. Valid after calling populatePickerData,
363 * but before calling wipePickerData, or if the webview has been
364 * destroyed. If the url has no title, use the url instead.
365 *
366 * @return The WebView's title (or url) or null.
367 */
368 public String getTitle() {
Patrick Scott20abe042009-04-28 08:03:29 -0400369 if (mPickerData != null) {
370 return mPickerData.mTitle;
371 }
372 return null;
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400373 }
374
375 /**
376 * Returns the picker data.
377 */
378 public PickerData getPickerData() {
379 return mPickerData;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800380 }
381
382 private void setParentTab(Tab parent) {
383 mParentTab = parent;
384 // This tab may have been freed due to low memory. If that is the
385 // case, the parent tab index is already saved. If we are changing
386 // that index (most likely due to removing the parent tab) we must
387 // update the parent tab index in the saved Bundle.
388 if (mSavedState != null) {
389 if (parent == null) {
390 mSavedState.remove(PARENTTAB);
391 } else {
392 mSavedState.putInt(PARENTTAB, getTabIndex(parent));
393 }
394 }
395 }
396
397 /**
398 * When a Tab is created through the content of another Tab, then
399 * we associate the Tabs.
400 * @param child the Tab that was created from this Tab
401 */
402 public void addChildTab(Tab child) {
403 if (mChildTabs == null) {
404 mChildTabs = new Vector<Tab>();
405 }
406 mChildTabs.add(child);
407 child.setParentTab(this);
408 }
409
410 private void removeFromTree() {
411 // detach the children
412 if (mChildTabs != null) {
413 for(Tab t : mChildTabs) {
414 t.setParentTab(null);
415 }
416 }
417
418 // Find myself in my parent list
419 if (mParentTab != null) {
420 mParentTab.mChildTabs.remove(this);
421 }
422 }
423
424 /**
425 * If this Tab was created through another Tab, then this method
426 * returns that Tab.
427 * @return the Tab parent or null
428 */
429 public Tab getParentTab() {
430 return mParentTab;
431 }
432
433 /**
434 * Return whether this tab should be closed when it is backing out of
435 * the first page.
436 * @return TRUE if this tab should be closed when exit.
437 */
438 public boolean closeOnExit() {
439 return mCloseOnExit;
440 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400441
442 public void onNewPicture(WebView view, Picture p) {
443 if (mPickerData == null) {
444 return;
445 }
446
447 mPickerData.mPicture = p;
448 // Tell the FakeWebView to redraw.
449 if (mPickerData.mFakeWebView != null) {
450 mPickerData.mFakeWebView.invalidate();
451 }
452 }
Grace Klobaeb6eef42009-09-15 17:56:32 -0700453
454 void setLockIconType(int type) {
455 mSavedLockIconType = type;
456 }
457
458 int getLockIconType() {
459 return mSavedLockIconType;
460 }
461
462 void setPrevLockIconType(int type) {
463 mSavedPrevLockIconType = type;
464 }
465
466 int getPrevLockIconType() {
467 return mSavedPrevLockIconType;
468 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800469 };
470
471 // Directory to store thumbnails for each WebView.
472 private final File mThumbnailDir;
473
474 /**
475 * Construct a new TabControl object that interfaces with the given
476 * BrowserActivity instance.
477 * @param activity A BrowserActivity instance that TabControl will interface
478 * with.
479 */
480 TabControl(BrowserActivity activity) {
481 mActivity = activity;
482 mInflateService =
483 ((LayoutInflater) activity.getSystemService(
484 Context.LAYOUT_INFLATER_SERVICE));
485 mThumbnailDir = activity.getDir("thumbnails", 0);
486 }
487
488 File getThumbnailDir() {
489 return mThumbnailDir;
490 }
491
492 BrowserActivity getBrowserActivity() {
493 return mActivity;
494 }
495
496 /**
497 * Return the current tab's main WebView. This will always return the main
498 * WebView for a given tab and not a subwindow.
499 * @return The current tab's WebView.
500 */
501 WebView getCurrentWebView() {
502 Tab t = getTab(mCurrentTab);
503 if (t == null) {
504 return null;
505 }
506 return t.mMainView;
507 }
508
509 /**
Ben Murdochbff2d602009-07-01 20:19:05 +0100510 * Return the current tab's error console. Creates the console if createIfNEcessary
511 * is true and we haven't already created the console.
512 * @param createIfNecessary Flag to indicate if the console should be created if it has
513 * not been already.
514 * @return The current tab's error console, or null if one has not been created and
515 * createIfNecessary is false.
516 */
517 ErrorConsoleView getCurrentErrorConsole(boolean createIfNecessary) {
518 Tab t = getTab(mCurrentTab);
519 if (t == null) {
520 return null;
521 }
522
523 if (createIfNecessary && t.mErrorConsole == null) {
524 t.mErrorConsole = new ErrorConsoleView(mActivity);
525 t.mErrorConsole.setWebView(t.mMainView);
526 }
527
528 return t.mErrorConsole;
529 }
530
531 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800532 * Return the current tab's top-level WebView. This can return a subwindow
533 * if one exists.
534 * @return The top-level WebView of the current tab.
535 */
536 WebView getCurrentTopWebView() {
537 Tab t = getTab(mCurrentTab);
538 if (t == null) {
539 return null;
540 }
541 return t.mSubView != null ? t.mSubView : t.mMainView;
542 }
543
544 /**
545 * Return the current tab's subwindow if it exists.
546 * @return The subwindow of the current tab or null if it doesn't exist.
547 */
548 WebView getCurrentSubWindow() {
549 Tab t = getTab(mCurrentTab);
550 if (t == null) {
551 return null;
552 }
553 return t.mSubView;
554 }
555
556 /**
557 * Return the tab at the specified index.
558 * @return The Tab for the specified index or null if the tab does not
559 * exist.
560 */
561 Tab getTab(int index) {
562 if (index >= 0 && index < mTabs.size()) {
563 return mTabs.get(index);
564 }
565 return null;
566 }
567
568 /**
569 * Return the current tab.
570 * @return The current tab.
571 */
572 Tab getCurrentTab() {
573 return getTab(mCurrentTab);
574 }
575
576 /**
577 * Return the current tab index.
578 * @return The current tab index
579 */
580 int getCurrentIndex() {
581 return mCurrentTab;
582 }
583
584 /**
585 * Given a Tab, find it's index
586 * @param Tab to find
587 * @return index of Tab or -1 if not found
588 */
589 int getTabIndex(Tab tab) {
Patrick Scottae641ac2009-04-20 13:51:49 -0400590 if (tab == null) {
591 return -1;
592 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800593 return mTabs.indexOf(tab);
594 }
595
596 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700597 * Create a new tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800598 * @return The newly createTab or null if we have reached the maximum
599 * number of open tabs.
600 */
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700601 Tab createNewTab(boolean closeOnExit, String appId, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800602 int size = mTabs.size();
603 // Return false if we have maxed out on tabs
604 if (MAX_TABS == size) {
605 return null;
606 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700607 final WebView w = createNewWebView();
Steve Block2bc69912009-07-30 14:45:13 +0100608
The Android Open Source Project0c908882009-03-03 19:32:16 -0800609 // Create a new tab and add it to the tab list
Steve Block2bc69912009-07-30 14:45:13 +0100610 Tab t = new Tab(w, closeOnExit, appId, url, mActivity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800611 mTabs.add(t);
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700612 // Initially put the tab in the background.
613 putTabInBackground(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800614 return t;
615 }
616
617 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700618 * Create a new tab with default values for closeOnExit(false),
619 * appId(null), and url(null).
620 */
621 Tab createNewTab() {
622 return createNewTab(false, null, null);
623 }
624
625 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800626 * Remove the tab from the list. If the tab is the current tab shown, the
627 * last created tab will be shown.
628 * @param t The tab to be removed.
629 */
630 boolean removeTab(Tab t) {
631 if (t == null) {
632 return false;
633 }
634 // Only remove the tab if it is the current one.
635 if (getCurrentTab() == t) {
636 putTabInBackground(t);
637 }
638
639 // Only destroy the WebView if it still exists.
640 if (t.mMainView != null) {
641 // Take down the sub window.
642 dismissSubWindow(t);
643 // Remove the WebView's settings from the BrowserSettings list of
644 // observers.
645 BrowserSettings.getInstance().deleteObserver(
646 t.mMainView.getSettings());
Patrick Scottd0119532009-09-17 08:00:31 -0400647 WebView w = t.mMainView;
Steve Block2bc69912009-07-30 14:45:13 +0100648 t.setWebView(null);
Patrick Scottd0119532009-09-17 08:00:31 -0400649 // Destroy the main view
650 w.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800651 }
652 // clear it's references to parent and children
653 t.removeFromTree();
654
655 // Remove it from our list of tabs.
656 mTabs.remove(t);
657
658 // The tab indices have shifted, update all the saved state so we point
659 // to the correct index.
660 for (Tab tab : mTabs) {
661 if (tab.mChildTabs != null) {
662 for (Tab child : tab.mChildTabs) {
663 child.setParentTab(tab);
664 }
665 }
666 }
667
668
669 // This tab may have been pushed in to the background and then closed.
670 // If the saved state contains a picture file, delete the file.
671 if (t.mSavedState != null) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400672 if (t.mSavedState.containsKey(CURRPICTURE)) {
673 new File(t.mSavedState.getString(CURRPICTURE)).delete();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674 }
675 }
676
677 // Remove it from the queue of viewed tabs.
678 mTabQueue.remove(t);
679 mCurrentTab = -1;
680 return true;
681 }
682
683 /**
684 * Clear the back/forward list for all the current tabs.
685 */
686 void clearHistory() {
687 int size = getTabCount();
688 for (int i = 0; i < size; i++) {
689 Tab t = mTabs.get(i);
690 // TODO: if a tab is freed due to low memory, its history is not
691 // cleared here.
692 if (t.mMainView != null) {
693 t.mMainView.clearHistory();
694 }
695 if (t.mSubView != null) {
696 t.mSubView.clearHistory();
697 }
698 }
699 }
700
701 /**
702 * Destroy all the tabs and subwindows
703 */
704 void destroy() {
705 BrowserSettings s = BrowserSettings.getInstance();
706 for (Tab t : mTabs) {
707 if (t.mMainView != null) {
708 dismissSubWindow(t);
709 s.deleteObserver(t.mMainView.getSettings());
Patrick Scottd0119532009-09-17 08:00:31 -0400710 WebView w = t.mMainView;
Steve Block2bc69912009-07-30 14:45:13 +0100711 t.setWebView(null);
Patrick Scottd0119532009-09-17 08:00:31 -0400712 w.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800713 }
714 }
715 mTabs.clear();
716 mTabQueue.clear();
717 }
718
719 /**
720 * Returns the number of tabs created.
721 * @return The number of tabs created.
722 */
723 int getTabCount() {
724 return mTabs.size();
725 }
726
727 // Used for saving and restoring each Tab
728 private static final String WEBVIEW = "webview";
729 private static final String NUMTABS = "numTabs";
730 private static final String CURRTAB = "currentTab";
731 private static final String CURRURL = "currentUrl";
732 private static final String CURRTITLE = "currentTitle";
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400733 private static final String CURRWIDTH = "currentWidth";
734 private static final String CURRPICTURE = "currentPicture";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800735 private static final String CLOSEONEXIT = "closeonexit";
736 private static final String PARENTTAB = "parentTab";
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700737 private static final String APPID = "appid";
738 private static final String ORIGINALURL = "originalUrl";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800739
740 /**
741 * Save the state of all the Tabs.
742 * @param outState The Bundle to save the state to.
743 */
744 void saveState(Bundle outState) {
745 final int numTabs = getTabCount();
746 outState.putInt(NUMTABS, numTabs);
747 final int index = getCurrentIndex();
748 outState.putInt(CURRTAB, (index >= 0 && index < numTabs) ? index : 0);
749 for (int i = 0; i < numTabs; i++) {
750 final Tab t = getTab(i);
751 if (saveState(t)) {
752 outState.putBundle(WEBVIEW + i, t.mSavedState);
753 }
754 }
755 }
756
757 /**
758 * Restore the state of all the tabs.
759 * @param inState The saved state of all the tabs.
760 * @return True if there were previous tabs that were restored. False if
761 * there was no saved state or restoring the state failed.
762 */
763 boolean restoreState(Bundle inState) {
764 final int numTabs = (inState == null)
765 ? -1 : inState.getInt(NUMTABS, -1);
766 if (numTabs == -1) {
767 return false;
768 } else {
769 final int currentTab = inState.getInt(CURRTAB, -1);
770 for (int i = 0; i < numTabs; i++) {
771 if (i == currentTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700772 Tab t = createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800773 // Me must set the current tab before restoring the state
774 // so that all the client classes are set.
775 setCurrentTab(t);
776 if (!restoreState(inState.getBundle(WEBVIEW + i), t)) {
777 Log.w(LOGTAG, "Fail in restoreState, load home page.");
778 t.mMainView.loadUrl(BrowserSettings.getInstance()
779 .getHomePage());
780 }
781 } else {
782 // Create a new tab and don't restore the state yet, add it
783 // to the tab list
Steve Block2bc69912009-07-30 14:45:13 +0100784 Tab t = new Tab(null, false, null, null, mActivity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800785 t.mSavedState = inState.getBundle(WEBVIEW + i);
786 if (t.mSavedState != null) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400787 populatePickerDataFromSavedState(t);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700788 // Need to maintain the app id and original url so we
789 // can possibly reuse this tab.
790 t.mAppId = t.mSavedState.getString(APPID);
791 t.mOriginalUrl = t.mSavedState.getString(ORIGINALURL);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800792 }
793 mTabs.add(t);
794 mTabQueue.add(t);
795 }
796 }
797 // Rebuild the tree of tabs. Do this after all tabs have been
798 // created/restored so that the parent tab exists.
799 for (int i = 0; i < numTabs; i++) {
800 final Bundle b = inState.getBundle(WEBVIEW + i);
801 final Tab t = getTab(i);
802 if (b != null && t != null) {
803 final int parentIndex = b.getInt(PARENTTAB, -1);
804 if (parentIndex != -1) {
805 final Tab parent = getTab(parentIndex);
806 if (parent != null) {
807 parent.addChildTab(t);
808 }
809 }
810 }
811 }
812 }
813 return true;
814 }
815
816 /**
817 * Free the memory in this order, 1) free the background tab; 2) free the
818 * WebView cache;
819 */
820 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700821 if (getTabCount() == 0) return;
822
The Android Open Source Project0c908882009-03-03 19:32:16 -0800823 // free the least frequently used background tab
Patrick Scott2a67de42009-08-31 09:48:37 -0400824 Tab t = getLeastUsedTab(getCurrentTab());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800825 if (t != null) {
826 Log.w(LOGTAG, "Free a tab in the browser");
827 freeTab(t);
828 // force a gc
829 System.gc();
830 return;
831 }
832
Derek Sollenberger4433d032009-06-10 15:37:21 -0400833 // free the WebView's unused memory (this includes the cache)
834 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800835 WebView view = getCurrentWebView();
836 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400837 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800838 }
839 // force a gc
840 System.gc();
841 }
842
Patrick Scott2a67de42009-08-31 09:48:37 -0400843 private Tab getLeastUsedTab(Tab current) {
844 // Don't do anything if we only have 1 tab or if the current tab is
845 // null.
846 if (getTabCount() == 1 || current == null) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800847 return null;
848 }
849
850 // Rip through the queue starting at the beginning and teardown the
851 // next available tab.
852 Tab t = null;
853 int i = 0;
854 final int queueSize = mTabQueue.size();
855 if (queueSize == 0) {
856 return null;
857 }
858 do {
859 t = mTabQueue.get(i++);
Grace Kloba92c18a52009-07-31 23:48:32 -0700860 } while (i < queueSize
Patrick Scott2a67de42009-08-31 09:48:37 -0400861 && ((t != null && t.mMainView == null)
862 || t == current.mParentTab));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800863
Patrick Scottd068f802009-06-22 11:46:06 -0400864 // Don't do anything if the last remaining tab is the current one or if
865 // the last tab has been freed already.
Patrick Scott2a67de42009-08-31 09:48:37 -0400866 if (t == current || t.mMainView == null) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800867 return null;
868 }
869
870 return t;
871 }
872
873 private void freeTab(Tab t) {
874 // Store the WebView's state.
875 saveState(t);
876
877 // Tear down the tab.
878 dismissSubWindow(t);
879 // Remove the WebView's settings from the BrowserSettings list of
880 // observers.
881 BrowserSettings.getInstance().deleteObserver(t.mMainView.getSettings());
Patrick Scottd0119532009-09-17 08:00:31 -0400882 WebView w = t.mMainView;
Steve Block2bc69912009-07-30 14:45:13 +0100883 t.setWebView(null);
Patrick Scottd0119532009-09-17 08:00:31 -0400884 w.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800885 }
886
887 /**
888 * Create a new subwindow unless a subwindow already exists.
889 * @return True if a new subwindow was created. False if one already exists.
890 */
891 void createSubWindow() {
892 Tab t = getTab(mCurrentTab);
893 if (t != null && t.mSubView == null) {
894 final View v = mInflateService.inflate(R.layout.browser_subwindow, null);
895 final WebView w = (WebView) v.findViewById(R.id.webview);
896 w.setMapTrackballToArrowKeys(false); // use trackball directly
897 final SubWindowClient subClient =
898 new SubWindowClient(mActivity.getWebViewClient());
899 final SubWindowChromeClient subChromeClient =
900 new SubWindowChromeClient(t,
901 mActivity.getWebChromeClient());
902 w.setWebViewClient(subClient);
903 w.setWebChromeClient(subChromeClient);
904 w.setDownloadListener(mActivity);
905 w.setOnCreateContextMenuListener(mActivity);
906 final BrowserSettings s = BrowserSettings.getInstance();
907 s.addObserver(w.getSettings()).update(s, null);
908 t.mSubView = w;
909 t.mSubViewClient = subClient;
910 t.mSubViewChromeClient = subChromeClient;
911 // FIXME: I really hate having to know the name of the view
912 // containing the webview.
913 t.mSubViewContainer = v.findViewById(R.id.subwindow_container);
914 final ImageButton cancel =
915 (ImageButton) v.findViewById(R.id.subwindow_close);
916 cancel.setOnClickListener(new OnClickListener() {
917 public void onClick(View v) {
918 subChromeClient.onCloseWindow(w);
919 }
920 });
921 }
922 }
923
924 /**
925 * Show the tab that contains the given WebView.
926 * @param view The WebView used to find the tab.
927 */
928 Tab getTabFromView(WebView view) {
929 final int size = getTabCount();
930 for (int i = 0; i < size; i++) {
931 final Tab t = getTab(i);
932 if (t.mSubView == view || t.mMainView == view) {
933 return t;
934 }
935 }
936 return null;
937 }
938
939 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700940 * Return the tab with the matching application id.
941 * @param id The application identifier.
942 */
943 Tab getTabFromId(String id) {
944 if (id == null) {
945 return null;
946 }
947 final int size = getTabCount();
948 for (int i = 0; i < size; i++) {
949 final Tab t = getTab(i);
950 if (id.equals(t.mAppId)) {
951 return t;
952 }
953 }
954 return null;
955 }
956
Patrick Scottcd115892009-07-16 09:42:58 -0400957 // This method checks if a non-app tab (one created within the browser)
958 // matches the given url.
959 private boolean tabMatchesUrl(Tab t, String url) {
960 if (t.mAppId != null) {
961 return false;
962 } else if (t.mMainView == null) {
963 return false;
964 } else if (url.equals(t.mMainView.getUrl()) ||
965 url.equals(t.mMainView.getOriginalUrl())) {
966 return true;
967 }
968 return false;
969 }
970
971 /**
972 * Return the tab that has no app id associated with it and the url of the
973 * tab matches the given url.
974 * @param url The url to search for.
975 */
976 Tab findUnusedTabWithUrl(String url) {
977 if (url == null) {
978 return null;
979 }
980 // Check the current tab first.
981 Tab t = getCurrentTab();
982 if (t != null && tabMatchesUrl(t, url)) {
983 return t;
984 }
985 // Now check all the rest.
986 final int size = getTabCount();
987 for (int i = 0; i < size; i++) {
988 t = getTab(i);
989 if (tabMatchesUrl(t, url)) {
990 return t;
991 }
992 }
993 return null;
994 }
995
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700996 /**
997 * Recreate the main WebView of the given tab. Returns true if the WebView
998 * was deleted.
999 */
1000 boolean recreateWebView(Tab t, String url) {
1001 final WebView w = t.mMainView;
1002 if (w != null) {
1003 if (url != null && url.equals(t.mOriginalUrl)) {
1004 // The original url matches the current url. Just go back to the
1005 // first history item so we can load it faster than if we
1006 // rebuilt the WebView.
1007 final WebBackForwardList list = w.copyBackForwardList();
1008 if (list != null) {
1009 w.goBackOrForward(-list.getCurrentIndex());
1010 w.clearHistory(); // maintains the current page.
1011 return false;
1012 }
1013 }
1014 // Remove the settings object from the global settings and destroy
1015 // the WebView.
1016 BrowserSettings.getInstance().deleteObserver(
1017 t.mMainView.getSettings());
1018 t.mMainView.destroy();
1019 }
1020 // Create a new WebView. If this tab is the current tab, we need to put
1021 // back all the clients so force it to be the current tab.
Steve Block2bc69912009-07-30 14:45:13 +01001022 t.setWebView(createNewWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001023 if (getCurrentTab() == t) {
1024 setCurrentTab(t, true);
1025 }
1026 // Clear the saved state except for the app id and close-on-exit
1027 // values.
1028 t.mSavedState = null;
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001029 t.mPickerData = null;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001030 // Save the new url in order to avoid deleting the WebView.
1031 t.mOriginalUrl = url;
1032 return true;
1033 }
1034
1035 /**
1036 * Creates a new WebView and registers it with the global settings.
1037 */
1038 private WebView createNewWebView() {
1039 // Create a new WebView
1040 WebView w = new WebView(mActivity);
1041 w.setMapTrackballToArrowKeys(false); // use trackball directly
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -07001042 // Enable the built-in zoom
1043 w.getSettings().setBuiltInZoomControls(true);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001044 // Add this WebView to the settings observer list and update the
1045 // settings
1046 final BrowserSettings s = BrowserSettings.getInstance();
1047 s.addObserver(w.getSettings()).update(s, null);
1048 return w;
1049 }
1050
1051 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001052 * Put the current tab in the background and set newTab as the current tab.
1053 * @param newTab The new tab. If newTab is null, the current tab is not
1054 * set.
1055 */
1056 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001057 return setCurrentTab(newTab, false);
1058 }
1059
Mike Reed7bfa63b2009-05-28 11:08:32 -04001060 /*package*/ void pauseCurrentTab() {
1061 Tab t = getCurrentTab();
1062 if (t != null) {
1063 t.mMainView.onPause();
1064 if (t.mSubView != null) {
1065 t.mSubView.onPause();
1066 }
1067 }
1068 }
1069
1070 /*package*/ void resumeCurrentTab() {
1071 Tab t = getCurrentTab();
1072 if (t != null) {
1073 t.mMainView.onResume();
1074 if (t.mSubView != null) {
1075 t.mSubView.onResume();
1076 }
1077 }
1078 }
1079
1080 private void putViewInForeground(WebView v, WebViewClient vc,
1081 WebChromeClient cc) {
1082 v.setWebViewClient(vc);
1083 v.setWebChromeClient(cc);
1084 v.setOnCreateContextMenuListener(mActivity);
1085 v.setDownloadListener(mActivity);
1086 v.onResume();
1087 }
1088
1089 private void putViewInBackground(WebView v) {
1090 // Set an empty callback so that default actions are not triggered.
1091 v.setWebViewClient(mEmptyClient);
1092 v.setWebChromeClient(mBackgroundChromeClient);
1093 v.setOnCreateContextMenuListener(null);
1094 // Leave the DownloadManager attached so that downloads can start in
1095 // a non-active window. This can happen when going to a site that does
1096 // a redirect after a period of time. The user could have switched to
1097 // another tab while waiting for the download to start.
1098 v.setDownloadListener(mActivity);
1099 v.onPause();
1100 }
1101
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001102 /**
1103 * If force is true, this method skips the check for newTab == current.
1104 */
1105 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001106 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001107 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001108 return true;
1109 }
1110 if (current != null) {
1111 // Remove the current WebView and the container of the subwindow
1112 putTabInBackground(current);
1113 }
1114
1115 if (newTab == null) {
1116 return false;
1117 }
1118
1119 // Move the newTab to the end of the queue
1120 int index = mTabQueue.indexOf(newTab);
1121 if (index != -1) {
1122 mTabQueue.remove(index);
1123 }
1124 mTabQueue.add(newTab);
1125
1126 WebView mainView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001127
1128 // Display the new current tab
1129 mCurrentTab = mTabs.indexOf(newTab);
1130 mainView = newTab.mMainView;
1131 boolean needRestore = (mainView == null);
1132 if (needRestore) {
1133 // Same work as in createNewTab() except don't do new Tab()
Steve Block2bc69912009-07-30 14:45:13 +01001134 mainView = createNewWebView();
1135 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001136 }
Mike Reed7bfa63b2009-05-28 11:08:32 -04001137 putViewInForeground(mainView, mActivity.getWebViewClient(),
1138 mActivity.getWebChromeClient());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001139 // Add the subwindow if it exists
1140 if (newTab.mSubViewContainer != null) {
Mike Reed7bfa63b2009-05-28 11:08:32 -04001141 putViewInForeground(newTab.mSubView, newTab.mSubViewClient,
1142 newTab.mSubViewChromeClient);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001143 }
1144 if (needRestore) {
1145 // Have to finish setCurrentTab work before calling restoreState
1146 if (!restoreState(newTab.mSavedState, newTab)) {
1147 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
1148 }
1149 }
1150 return true;
1151 }
1152
1153 /*
1154 * Put the tab in the background using all the empty/background clients.
1155 */
1156 private void putTabInBackground(Tab t) {
Mike Reed7bfa63b2009-05-28 11:08:32 -04001157 putViewInBackground(t.mMainView);
1158 if (t.mSubView != null) {
1159 putViewInBackground(t.mSubView);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001160 }
1161 }
1162
1163 /*
1164 * Dismiss the subwindow for the given tab.
1165 */
1166 void dismissSubWindow(Tab t) {
1167 if (t != null && t.mSubView != null) {
1168 BrowserSettings.getInstance().deleteObserver(
1169 t.mSubView.getSettings());
1170 t.mSubView.destroy();
1171 t.mSubView = null;
1172 t.mSubViewContainer = null;
1173 }
1174 }
1175
1176 /**
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001177 * Ensure that Tab t has data to display in the tab picker.
The Android Open Source Project0c908882009-03-03 19:32:16 -08001178 * @param t Tab to populate.
1179 */
1180 /* package */ void populatePickerData(Tab t) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001181 if (t == null) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001182 return;
1183 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001184
1185 // mMainView == null indicates that the tab has been freed.
1186 if (t.mMainView == null) {
1187 populatePickerDataFromSavedState(t);
1188 return;
1189 }
1190
The Android Open Source Project0c908882009-03-03 19:32:16 -08001191 // FIXME: The only place we cared about subwindow was for
1192 // bookmarking (i.e. not when saving state). Was this deliberate?
1193 final WebBackForwardList list = t.mMainView.copyBackForwardList();
1194 final WebHistoryItem item =
1195 list != null ? list.getCurrentItem() : null;
1196 populatePickerData(t, item);
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001197
1198 // This method is only called during the tab picker creation. At this
1199 // point we need to listen for new pictures since the WebView is still
1200 // active.
1201 final WebView w = t.getTopWindow();
1202 w.setPictureListener(t);
1203 // Capture the picture here instead of populatePickerData since it can
1204 // be called when saving the state of a tab.
1205 t.mPickerData.mPicture = w.capturePicture();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001206 }
1207
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001208 // Create the PickerData and populate it using the saved state of the tab.
1209 private void populatePickerDataFromSavedState(Tab t) {
1210 if (t.mSavedState == null) {
1211 return;
1212 }
1213
1214 final PickerData data = new PickerData();
1215 final Bundle state = t.mSavedState;
1216 data.mUrl = state.getString(CURRURL);
1217 data.mTitle = state.getString(CURRTITLE);
1218 data.mWidth = state.getInt(CURRWIDTH, 0);
1219 // XXX: These keys are from WebView.savePicture so if they change, this
1220 // will break.
1221 data.mScale = state.getFloat("scale", 1.0f);
1222 data.mScrollX = state.getInt("scrollX", 0);
1223 data.mScrollY = state.getInt("scrollY", 0);
1224
1225 if (state.containsKey(CURRPICTURE)) {
1226 final File f = new File(t.mSavedState.getString(CURRPICTURE));
1227 try {
1228 final FileInputStream in = new FileInputStream(f);
1229 data.mPicture = Picture.createFromStream(in);
1230 in.close();
1231 } catch (Exception ex) {
1232 // Ignore any problems with inflating the picture. We just
1233 // won't draw anything.
The Android Open Source Project0c908882009-03-03 19:32:16 -08001234 }
1235 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001236
1237 // Set the tab's picker data.
1238 t.mPickerData = data;
1239 }
1240
1241 // Populate the picker data using the given history item and the current
1242 // top WebView.
1243 private void populatePickerData(Tab t, WebHistoryItem item) {
1244 final PickerData data = new PickerData();
1245 if (item != null) {
1246 data.mUrl = item.getUrl();
1247 data.mTitle = item.getTitle();
1248 if (data.mTitle == null) {
1249 data.mTitle = data.mUrl;
1250 }
1251 }
1252 // We want to display the top window in the tab picker but use the url
1253 // and title of the main window.
1254 final WebView w = t.getTopWindow();
1255 data.mWidth = w.getWidth();
1256 data.mScale = w.getScale();
1257 data.mScrollX = w.getScrollX();
1258 data.mScrollY = w.getScrollY();
Patrick Scottb0e4fc72009-07-14 10:49:22 -04001259
1260 // Remember the old picture if possible.
1261 if (t.mPickerData != null) {
1262 data.mPicture = t.mPickerData.mPicture;
1263 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001264 t.mPickerData = data;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001265 }
1266
1267 /**
1268 * Clean up the data for all tabs.
1269 */
1270 /* package */ void wipeAllPickerData() {
1271 int size = getTabCount();
1272 for (int i = 0; i < size; i++) {
1273 final Tab t = getTab(i);
1274 if (t != null && t.mSavedState == null) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001275 t.mPickerData = null;
1276 }
1277 if (t.mMainView != null) {
1278 // Clear the picture listeners.
1279 t.mMainView.setPictureListener(null);
1280 if (t.mSubView != null) {
1281 t.mSubView.setPictureListener(null);
1282 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001283 }
1284 }
1285 }
1286
1287 /*
1288 * Save the state for an individual tab.
1289 */
1290 private boolean saveState(Tab t) {
1291 if (t != null) {
1292 final WebView w = t.mMainView;
1293 // If the WebView is null it means we ran low on memory and we
1294 // already stored the saved state in mSavedState.
1295 if (w == null) {
1296 return true;
1297 }
1298 final Bundle b = new Bundle();
1299 final WebBackForwardList list = w.saveState(b);
1300 if (list != null) {
1301 final File f = new File(mThumbnailDir, w.hashCode()
1302 + "_pic.save");
1303 if (w.savePicture(b, f)) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001304 b.putString(CURRPICTURE, f.getPath());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001305 }
1306 }
1307
1308 // Store some extra info for displaying the tab in the picker.
1309 final WebHistoryItem item =
1310 list != null ? list.getCurrentItem() : null;
1311 populatePickerData(t, item);
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001312
1313 // XXX: WebView.savePicture stores the scale and scroll positions
1314 // in the bundle so we don't have to do it here.
1315 final PickerData data = t.mPickerData;
1316 if (data.mUrl != null) {
1317 b.putString(CURRURL, data.mUrl);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001318 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001319 if (data.mTitle != null) {
1320 b.putString(CURRTITLE, data.mTitle);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001321 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001322 b.putInt(CURRWIDTH, data.mWidth);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001323 b.putBoolean(CLOSEONEXIT, t.mCloseOnExit);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001324 if (t.mAppId != null) {
1325 b.putString(APPID, t.mAppId);
1326 }
1327 if (t.mOriginalUrl != null) {
1328 b.putString(ORIGINALURL, t.mOriginalUrl);
1329 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001330
1331 // Remember the parent tab so the relationship can be restored.
1332 if (t.mParentTab != null) {
1333 b.putInt(PARENTTAB, getTabIndex(t.mParentTab));
1334 }
1335
1336 // Remember the saved state.
1337 t.mSavedState = b;
1338 return true;
1339 }
1340 return false;
1341 }
1342
1343 /*
1344 * Restore the state of the tab.
1345 */
1346 private boolean restoreState(Bundle b, Tab t) {
1347 if (b == null) {
1348 return false;
1349 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001350 // Restore the internal state even if the WebView fails to restore.
1351 // This will maintain the app id, original url and close-on-exit values.
1352 t.mSavedState = null;
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001353 t.mPickerData = null;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001354 t.mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1355 t.mAppId = b.getString(APPID);
1356 t.mOriginalUrl = b.getString(ORIGINALURL);
1357
The Android Open Source Project0c908882009-03-03 19:32:16 -08001358 final WebView w = t.mMainView;
1359 final WebBackForwardList list = w.restoreState(b);
1360 if (list == null) {
1361 return false;
1362 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001363 if (b.containsKey(CURRPICTURE)) {
1364 final File f = new File(b.getString(CURRPICTURE));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001365 w.restorePicture(b, f);
1366 f.delete();
1367 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001368 return true;
1369 }
1370}