blob: 037a757c7bcae7615e938e4cc00c568d54136877 [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;
213
The Android Open Source Project0c908882009-03-03 19:32:16 -0800214 // Construct a new tab
Steve Block2bc69912009-07-30 14:45:13 +0100215 private Tab(WebView w, boolean closeOnExit, String appId, String url, Context context) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800216 mCloseOnExit = closeOnExit;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700217 mAppId = appId;
218 mOriginalUrl = url;
Steve Block2bc69912009-07-30 14:45:13 +0100219
220 // The tab consists of a container view, which contains the main
221 // WebView, as well as any other UI elements associated with the tab.
222 //
223 // FIXME: Fix the interaction between this layout and the animation
224 // used when switching to and from the tab picker. This may not be
225 // required if the tab selection UI is redesigned.
226 LayoutInflater factory = LayoutInflater.from(context);
227 mContainer = factory.inflate(R.layout.tab, null);
228
229 mGeolocationPermissionsPrompt =
230 (GeolocationPermissionsPrompt) mContainer.findViewById(
231 R.id.geolocation_permissions_prompt);
232
233 setWebView(w);
234 }
235
236 /**
237 * Sets the WebView for this tab, correctly removing the old WebView
238 * from, and inserting the new WebView into, the container view.
239 */
240 public void setWebView(WebView w) {
241 if (mMainView == w) {
242 return;
243 }
244 // If the WebView is changing, the page will be reloaded, so any ongoing Geolocation
245 // permission requests are void.
246 mGeolocationPermissionsPrompt.hide();
247
248 FrameLayout wrapper = (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
249 if (mMainView != null) {
250 wrapper.removeView(mMainView);
251 }
252 mMainView = w;
253 if (mMainView != null) {
254 wrapper.addView(mMainView);
255 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800256 }
257
258 /**
259 * Return the top window of this tab; either the subwindow if it is not
260 * null or the main window.
261 * @return The top window of this tab.
262 */
263 public WebView getTopWindow() {
264 if (mSubView != null) {
265 return mSubView;
266 }
267 return mMainView;
268 }
269
270 /**
271 * Return the main window of this tab. Note: if a tab is freed in the
272 * background, this can return null. It is only guaranteed to be
273 * non-null for the current tab.
274 * @return The main WebView of this tab.
275 */
276 public WebView getWebView() {
277 return mMainView;
278 }
279
280 /**
Steve Block2bc69912009-07-30 14:45:13 +0100281 * @return The container for this tab.
282 */
283 public View getContainer() {
284 return mContainer;
285 }
286
287 /**
288 * @return The geolocation permissions prompt for this tab.
289 */
290 public GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
291 return mGeolocationPermissionsPrompt;
292 }
293
294 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800295 * Return the subwindow of this tab or null if there is no subwindow.
296 * @return The subwindow of this tab or null.
297 */
298 public WebView getSubWebView() {
299 return mSubView;
300 }
301
302 /**
303 * Return the subwindow container of this tab or null if there is no
304 * subwindow.
305 * @return The subwindow's container View.
306 */
307 public View getSubWebViewContainer() {
308 return mSubViewContainer;
309 }
310
311 /**
312 * Get the url of this tab. Valid after calling populatePickerData, but
313 * before calling wipePickerData, or if the webview has been destroyed.
314 *
315 * @return The WebView's url or null.
316 */
317 public String getUrl() {
Patrick Scott20abe042009-04-28 08:03:29 -0400318 if (mPickerData != null) {
319 return mPickerData.mUrl;
320 }
321 return null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800322 }
323
324 /**
325 * Get the title of this tab. Valid after calling populatePickerData,
326 * but before calling wipePickerData, or if the webview has been
327 * destroyed. If the url has no title, use the url instead.
328 *
329 * @return The WebView's title (or url) or null.
330 */
331 public String getTitle() {
Patrick Scott20abe042009-04-28 08:03:29 -0400332 if (mPickerData != null) {
333 return mPickerData.mTitle;
334 }
335 return null;
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400336 }
337
338 /**
339 * Returns the picker data.
340 */
341 public PickerData getPickerData() {
342 return mPickerData;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800343 }
344
345 private void setParentTab(Tab parent) {
346 mParentTab = parent;
347 // This tab may have been freed due to low memory. If that is the
348 // case, the parent tab index is already saved. If we are changing
349 // that index (most likely due to removing the parent tab) we must
350 // update the parent tab index in the saved Bundle.
351 if (mSavedState != null) {
352 if (parent == null) {
353 mSavedState.remove(PARENTTAB);
354 } else {
355 mSavedState.putInt(PARENTTAB, getTabIndex(parent));
356 }
357 }
358 }
359
360 /**
361 * When a Tab is created through the content of another Tab, then
362 * we associate the Tabs.
363 * @param child the Tab that was created from this Tab
364 */
365 public void addChildTab(Tab child) {
366 if (mChildTabs == null) {
367 mChildTabs = new Vector<Tab>();
368 }
369 mChildTabs.add(child);
370 child.setParentTab(this);
371 }
372
373 private void removeFromTree() {
374 // detach the children
375 if (mChildTabs != null) {
376 for(Tab t : mChildTabs) {
377 t.setParentTab(null);
378 }
379 }
380
381 // Find myself in my parent list
382 if (mParentTab != null) {
383 mParentTab.mChildTabs.remove(this);
384 }
385 }
386
387 /**
388 * If this Tab was created through another Tab, then this method
389 * returns that Tab.
390 * @return the Tab parent or null
391 */
392 public Tab getParentTab() {
393 return mParentTab;
394 }
395
396 /**
397 * Return whether this tab should be closed when it is backing out of
398 * the first page.
399 * @return TRUE if this tab should be closed when exit.
400 */
401 public boolean closeOnExit() {
402 return mCloseOnExit;
403 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400404
405 public void onNewPicture(WebView view, Picture p) {
406 if (mPickerData == null) {
407 return;
408 }
409
410 mPickerData.mPicture = p;
411 // Tell the FakeWebView to redraw.
412 if (mPickerData.mFakeWebView != null) {
413 mPickerData.mFakeWebView.invalidate();
414 }
415 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800416 };
417
418 // Directory to store thumbnails for each WebView.
419 private final File mThumbnailDir;
420
421 /**
422 * Construct a new TabControl object that interfaces with the given
423 * BrowserActivity instance.
424 * @param activity A BrowserActivity instance that TabControl will interface
425 * with.
426 */
427 TabControl(BrowserActivity activity) {
428 mActivity = activity;
429 mInflateService =
430 ((LayoutInflater) activity.getSystemService(
431 Context.LAYOUT_INFLATER_SERVICE));
432 mThumbnailDir = activity.getDir("thumbnails", 0);
433 }
434
435 File getThumbnailDir() {
436 return mThumbnailDir;
437 }
438
439 BrowserActivity getBrowserActivity() {
440 return mActivity;
441 }
442
443 /**
444 * Return the current tab's main WebView. This will always return the main
445 * WebView for a given tab and not a subwindow.
446 * @return The current tab's WebView.
447 */
448 WebView getCurrentWebView() {
449 Tab t = getTab(mCurrentTab);
450 if (t == null) {
451 return null;
452 }
453 return t.mMainView;
454 }
455
456 /**
Ben Murdochbff2d602009-07-01 20:19:05 +0100457 * Return the current tab's error console. Creates the console if createIfNEcessary
458 * is true and we haven't already created the console.
459 * @param createIfNecessary Flag to indicate if the console should be created if it has
460 * not been already.
461 * @return The current tab's error console, or null if one has not been created and
462 * createIfNecessary is false.
463 */
464 ErrorConsoleView getCurrentErrorConsole(boolean createIfNecessary) {
465 Tab t = getTab(mCurrentTab);
466 if (t == null) {
467 return null;
468 }
469
470 if (createIfNecessary && t.mErrorConsole == null) {
471 t.mErrorConsole = new ErrorConsoleView(mActivity);
472 t.mErrorConsole.setWebView(t.mMainView);
473 }
474
475 return t.mErrorConsole;
476 }
477
478 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800479 * Return the current tab's top-level WebView. This can return a subwindow
480 * if one exists.
481 * @return The top-level WebView of the current tab.
482 */
483 WebView getCurrentTopWebView() {
484 Tab t = getTab(mCurrentTab);
485 if (t == null) {
486 return null;
487 }
488 return t.mSubView != null ? t.mSubView : t.mMainView;
489 }
490
491 /**
492 * Return the current tab's subwindow if it exists.
493 * @return The subwindow of the current tab or null if it doesn't exist.
494 */
495 WebView getCurrentSubWindow() {
496 Tab t = getTab(mCurrentTab);
497 if (t == null) {
498 return null;
499 }
500 return t.mSubView;
501 }
502
503 /**
504 * Return the tab at the specified index.
505 * @return The Tab for the specified index or null if the tab does not
506 * exist.
507 */
508 Tab getTab(int index) {
509 if (index >= 0 && index < mTabs.size()) {
510 return mTabs.get(index);
511 }
512 return null;
513 }
514
515 /**
516 * Return the current tab.
517 * @return The current tab.
518 */
519 Tab getCurrentTab() {
520 return getTab(mCurrentTab);
521 }
522
523 /**
524 * Return the current tab index.
525 * @return The current tab index
526 */
527 int getCurrentIndex() {
528 return mCurrentTab;
529 }
530
531 /**
532 * Given a Tab, find it's index
533 * @param Tab to find
534 * @return index of Tab or -1 if not found
535 */
536 int getTabIndex(Tab tab) {
Patrick Scottae641ac2009-04-20 13:51:49 -0400537 if (tab == null) {
538 return -1;
539 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800540 return mTabs.indexOf(tab);
541 }
542
543 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700544 * Create a new tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800545 * @return The newly createTab or null if we have reached the maximum
546 * number of open tabs.
547 */
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700548 Tab createNewTab(boolean closeOnExit, String appId, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800549 int size = mTabs.size();
550 // Return false if we have maxed out on tabs
551 if (MAX_TABS == size) {
552 return null;
553 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700554 final WebView w = createNewWebView();
Steve Block2bc69912009-07-30 14:45:13 +0100555
The Android Open Source Project0c908882009-03-03 19:32:16 -0800556 // Create a new tab and add it to the tab list
Steve Block2bc69912009-07-30 14:45:13 +0100557 Tab t = new Tab(w, closeOnExit, appId, url, mActivity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800558 mTabs.add(t);
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700559 // Initially put the tab in the background.
560 putTabInBackground(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800561 return t;
562 }
563
564 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700565 * Create a new tab with default values for closeOnExit(false),
566 * appId(null), and url(null).
567 */
568 Tab createNewTab() {
569 return createNewTab(false, null, null);
570 }
571
572 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800573 * Remove the tab from the list. If the tab is the current tab shown, the
574 * last created tab will be shown.
575 * @param t The tab to be removed.
576 */
577 boolean removeTab(Tab t) {
578 if (t == null) {
579 return false;
580 }
581 // Only remove the tab if it is the current one.
582 if (getCurrentTab() == t) {
583 putTabInBackground(t);
584 }
585
586 // Only destroy the WebView if it still exists.
587 if (t.mMainView != null) {
588 // Take down the sub window.
589 dismissSubWindow(t);
590 // Remove the WebView's settings from the BrowserSettings list of
591 // observers.
592 BrowserSettings.getInstance().deleteObserver(
593 t.mMainView.getSettings());
594 // Destroy the main view and subview
595 t.mMainView.destroy();
Steve Block2bc69912009-07-30 14:45:13 +0100596 t.setWebView(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800597 }
598 // clear it's references to parent and children
599 t.removeFromTree();
600
601 // Remove it from our list of tabs.
602 mTabs.remove(t);
603
604 // The tab indices have shifted, update all the saved state so we point
605 // to the correct index.
606 for (Tab tab : mTabs) {
607 if (tab.mChildTabs != null) {
608 for (Tab child : tab.mChildTabs) {
609 child.setParentTab(tab);
610 }
611 }
612 }
613
614
615 // This tab may have been pushed in to the background and then closed.
616 // If the saved state contains a picture file, delete the file.
617 if (t.mSavedState != null) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400618 if (t.mSavedState.containsKey(CURRPICTURE)) {
619 new File(t.mSavedState.getString(CURRPICTURE)).delete();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800620 }
621 }
622
623 // Remove it from the queue of viewed tabs.
624 mTabQueue.remove(t);
625 mCurrentTab = -1;
626 return true;
627 }
628
629 /**
630 * Clear the back/forward list for all the current tabs.
631 */
632 void clearHistory() {
633 int size = getTabCount();
634 for (int i = 0; i < size; i++) {
635 Tab t = mTabs.get(i);
636 // TODO: if a tab is freed due to low memory, its history is not
637 // cleared here.
638 if (t.mMainView != null) {
639 t.mMainView.clearHistory();
640 }
641 if (t.mSubView != null) {
642 t.mSubView.clearHistory();
643 }
644 }
645 }
646
647 /**
648 * Destroy all the tabs and subwindows
649 */
650 void destroy() {
651 BrowserSettings s = BrowserSettings.getInstance();
652 for (Tab t : mTabs) {
653 if (t.mMainView != null) {
654 dismissSubWindow(t);
655 s.deleteObserver(t.mMainView.getSettings());
656 t.mMainView.destroy();
Steve Block2bc69912009-07-30 14:45:13 +0100657 t.setWebView(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800658 }
659 }
660 mTabs.clear();
661 mTabQueue.clear();
662 }
663
664 /**
665 * Returns the number of tabs created.
666 * @return The number of tabs created.
667 */
668 int getTabCount() {
669 return mTabs.size();
670 }
671
672 // Used for saving and restoring each Tab
673 private static final String WEBVIEW = "webview";
674 private static final String NUMTABS = "numTabs";
675 private static final String CURRTAB = "currentTab";
676 private static final String CURRURL = "currentUrl";
677 private static final String CURRTITLE = "currentTitle";
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400678 private static final String CURRWIDTH = "currentWidth";
679 private static final String CURRPICTURE = "currentPicture";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800680 private static final String CLOSEONEXIT = "closeonexit";
681 private static final String PARENTTAB = "parentTab";
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700682 private static final String APPID = "appid";
683 private static final String ORIGINALURL = "originalUrl";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800684
685 /**
686 * Save the state of all the Tabs.
687 * @param outState The Bundle to save the state to.
688 */
689 void saveState(Bundle outState) {
690 final int numTabs = getTabCount();
691 outState.putInt(NUMTABS, numTabs);
692 final int index = getCurrentIndex();
693 outState.putInt(CURRTAB, (index >= 0 && index < numTabs) ? index : 0);
694 for (int i = 0; i < numTabs; i++) {
695 final Tab t = getTab(i);
696 if (saveState(t)) {
697 outState.putBundle(WEBVIEW + i, t.mSavedState);
698 }
699 }
700 }
701
702 /**
703 * Restore the state of all the tabs.
704 * @param inState The saved state of all the tabs.
705 * @return True if there were previous tabs that were restored. False if
706 * there was no saved state or restoring the state failed.
707 */
708 boolean restoreState(Bundle inState) {
709 final int numTabs = (inState == null)
710 ? -1 : inState.getInt(NUMTABS, -1);
711 if (numTabs == -1) {
712 return false;
713 } else {
714 final int currentTab = inState.getInt(CURRTAB, -1);
715 for (int i = 0; i < numTabs; i++) {
716 if (i == currentTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700717 Tab t = createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800718 // Me must set the current tab before restoring the state
719 // so that all the client classes are set.
720 setCurrentTab(t);
721 if (!restoreState(inState.getBundle(WEBVIEW + i), t)) {
722 Log.w(LOGTAG, "Fail in restoreState, load home page.");
723 t.mMainView.loadUrl(BrowserSettings.getInstance()
724 .getHomePage());
725 }
726 } else {
727 // Create a new tab and don't restore the state yet, add it
728 // to the tab list
Steve Block2bc69912009-07-30 14:45:13 +0100729 Tab t = new Tab(null, false, null, null, mActivity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800730 t.mSavedState = inState.getBundle(WEBVIEW + i);
731 if (t.mSavedState != null) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400732 populatePickerDataFromSavedState(t);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700733 // Need to maintain the app id and original url so we
734 // can possibly reuse this tab.
735 t.mAppId = t.mSavedState.getString(APPID);
736 t.mOriginalUrl = t.mSavedState.getString(ORIGINALURL);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800737 }
738 mTabs.add(t);
739 mTabQueue.add(t);
740 }
741 }
742 // Rebuild the tree of tabs. Do this after all tabs have been
743 // created/restored so that the parent tab exists.
744 for (int i = 0; i < numTabs; i++) {
745 final Bundle b = inState.getBundle(WEBVIEW + i);
746 final Tab t = getTab(i);
747 if (b != null && t != null) {
748 final int parentIndex = b.getInt(PARENTTAB, -1);
749 if (parentIndex != -1) {
750 final Tab parent = getTab(parentIndex);
751 if (parent != null) {
752 parent.addChildTab(t);
753 }
754 }
755 }
756 }
757 }
758 return true;
759 }
760
761 /**
762 * Free the memory in this order, 1) free the background tab; 2) free the
763 * WebView cache;
764 */
765 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700766 if (getTabCount() == 0) return;
767
The Android Open Source Project0c908882009-03-03 19:32:16 -0800768 // free the least frequently used background tab
Patrick Scott2a67de42009-08-31 09:48:37 -0400769 Tab t = getLeastUsedTab(getCurrentTab());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800770 if (t != null) {
771 Log.w(LOGTAG, "Free a tab in the browser");
772 freeTab(t);
773 // force a gc
774 System.gc();
775 return;
776 }
777
Derek Sollenberger4433d032009-06-10 15:37:21 -0400778 // free the WebView's unused memory (this includes the cache)
779 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800780 WebView view = getCurrentWebView();
781 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400782 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800783 }
784 // force a gc
785 System.gc();
786 }
787
Patrick Scott2a67de42009-08-31 09:48:37 -0400788 private Tab getLeastUsedTab(Tab current) {
789 // Don't do anything if we only have 1 tab or if the current tab is
790 // null.
791 if (getTabCount() == 1 || current == null) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800792 return null;
793 }
794
795 // Rip through the queue starting at the beginning and teardown the
796 // next available tab.
797 Tab t = null;
798 int i = 0;
799 final int queueSize = mTabQueue.size();
800 if (queueSize == 0) {
801 return null;
802 }
803 do {
804 t = mTabQueue.get(i++);
Grace Kloba92c18a52009-07-31 23:48:32 -0700805 } while (i < queueSize
Patrick Scott2a67de42009-08-31 09:48:37 -0400806 && ((t != null && t.mMainView == null)
807 || t == current.mParentTab));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800808
Patrick Scottd068f802009-06-22 11:46:06 -0400809 // Don't do anything if the last remaining tab is the current one or if
810 // the last tab has been freed already.
Patrick Scott2a67de42009-08-31 09:48:37 -0400811 if (t == current || t.mMainView == null) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800812 return null;
813 }
814
815 return t;
816 }
817
818 private void freeTab(Tab t) {
819 // Store the WebView's state.
820 saveState(t);
821
822 // Tear down the tab.
823 dismissSubWindow(t);
824 // Remove the WebView's settings from the BrowserSettings list of
825 // observers.
826 BrowserSettings.getInstance().deleteObserver(t.mMainView.getSettings());
827 t.mMainView.destroy();
Steve Block2bc69912009-07-30 14:45:13 +0100828 t.setWebView(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800829 }
830
831 /**
832 * Create a new subwindow unless a subwindow already exists.
833 * @return True if a new subwindow was created. False if one already exists.
834 */
835 void createSubWindow() {
836 Tab t = getTab(mCurrentTab);
837 if (t != null && t.mSubView == null) {
838 final View v = mInflateService.inflate(R.layout.browser_subwindow, null);
839 final WebView w = (WebView) v.findViewById(R.id.webview);
840 w.setMapTrackballToArrowKeys(false); // use trackball directly
841 final SubWindowClient subClient =
842 new SubWindowClient(mActivity.getWebViewClient());
843 final SubWindowChromeClient subChromeClient =
844 new SubWindowChromeClient(t,
845 mActivity.getWebChromeClient());
846 w.setWebViewClient(subClient);
847 w.setWebChromeClient(subChromeClient);
848 w.setDownloadListener(mActivity);
849 w.setOnCreateContextMenuListener(mActivity);
850 final BrowserSettings s = BrowserSettings.getInstance();
851 s.addObserver(w.getSettings()).update(s, null);
852 t.mSubView = w;
853 t.mSubViewClient = subClient;
854 t.mSubViewChromeClient = subChromeClient;
855 // FIXME: I really hate having to know the name of the view
856 // containing the webview.
857 t.mSubViewContainer = v.findViewById(R.id.subwindow_container);
858 final ImageButton cancel =
859 (ImageButton) v.findViewById(R.id.subwindow_close);
860 cancel.setOnClickListener(new OnClickListener() {
861 public void onClick(View v) {
862 subChromeClient.onCloseWindow(w);
863 }
864 });
865 }
866 }
867
868 /**
869 * Show the tab that contains the given WebView.
870 * @param view The WebView used to find the tab.
871 */
872 Tab getTabFromView(WebView view) {
873 final int size = getTabCount();
874 for (int i = 0; i < size; i++) {
875 final Tab t = getTab(i);
876 if (t.mSubView == view || t.mMainView == view) {
877 return t;
878 }
879 }
880 return null;
881 }
882
883 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700884 * Return the tab with the matching application id.
885 * @param id The application identifier.
886 */
887 Tab getTabFromId(String id) {
888 if (id == null) {
889 return null;
890 }
891 final int size = getTabCount();
892 for (int i = 0; i < size; i++) {
893 final Tab t = getTab(i);
894 if (id.equals(t.mAppId)) {
895 return t;
896 }
897 }
898 return null;
899 }
900
Patrick Scottcd115892009-07-16 09:42:58 -0400901 // This method checks if a non-app tab (one created within the browser)
902 // matches the given url.
903 private boolean tabMatchesUrl(Tab t, String url) {
904 if (t.mAppId != null) {
905 return false;
906 } else if (t.mMainView == null) {
907 return false;
908 } else if (url.equals(t.mMainView.getUrl()) ||
909 url.equals(t.mMainView.getOriginalUrl())) {
910 return true;
911 }
912 return false;
913 }
914
915 /**
916 * Return the tab that has no app id associated with it and the url of the
917 * tab matches the given url.
918 * @param url The url to search for.
919 */
920 Tab findUnusedTabWithUrl(String url) {
921 if (url == null) {
922 return null;
923 }
924 // Check the current tab first.
925 Tab t = getCurrentTab();
926 if (t != null && tabMatchesUrl(t, url)) {
927 return t;
928 }
929 // Now check all the rest.
930 final int size = getTabCount();
931 for (int i = 0; i < size; i++) {
932 t = getTab(i);
933 if (tabMatchesUrl(t, url)) {
934 return t;
935 }
936 }
937 return null;
938 }
939
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700940 /**
941 * Recreate the main WebView of the given tab. Returns true if the WebView
942 * was deleted.
943 */
944 boolean recreateWebView(Tab t, String url) {
945 final WebView w = t.mMainView;
946 if (w != null) {
947 if (url != null && url.equals(t.mOriginalUrl)) {
948 // The original url matches the current url. Just go back to the
949 // first history item so we can load it faster than if we
950 // rebuilt the WebView.
951 final WebBackForwardList list = w.copyBackForwardList();
952 if (list != null) {
953 w.goBackOrForward(-list.getCurrentIndex());
954 w.clearHistory(); // maintains the current page.
955 return false;
956 }
957 }
958 // Remove the settings object from the global settings and destroy
959 // the WebView.
960 BrowserSettings.getInstance().deleteObserver(
961 t.mMainView.getSettings());
962 t.mMainView.destroy();
963 }
964 // Create a new WebView. If this tab is the current tab, we need to put
965 // back all the clients so force it to be the current tab.
Steve Block2bc69912009-07-30 14:45:13 +0100966 t.setWebView(createNewWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700967 if (getCurrentTab() == t) {
968 setCurrentTab(t, true);
969 }
970 // Clear the saved state except for the app id and close-on-exit
971 // values.
972 t.mSavedState = null;
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400973 t.mPickerData = null;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700974 // Save the new url in order to avoid deleting the WebView.
975 t.mOriginalUrl = url;
976 return true;
977 }
978
979 /**
980 * Creates a new WebView and registers it with the global settings.
981 */
982 private WebView createNewWebView() {
983 // Create a new WebView
984 WebView w = new WebView(mActivity);
985 w.setMapTrackballToArrowKeys(false); // use trackball directly
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700986 // Enable the built-in zoom
987 w.getSettings().setBuiltInZoomControls(true);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700988 // Add this WebView to the settings observer list and update the
989 // settings
990 final BrowserSettings s = BrowserSettings.getInstance();
991 s.addObserver(w.getSettings()).update(s, null);
992 return w;
993 }
994
995 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800996 * Put the current tab in the background and set newTab as the current tab.
997 * @param newTab The new tab. If newTab is null, the current tab is not
998 * set.
999 */
1000 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001001 return setCurrentTab(newTab, false);
1002 }
1003
Mike Reed7bfa63b2009-05-28 11:08:32 -04001004 /*package*/ void pauseCurrentTab() {
1005 Tab t = getCurrentTab();
1006 if (t != null) {
1007 t.mMainView.onPause();
1008 if (t.mSubView != null) {
1009 t.mSubView.onPause();
1010 }
1011 }
1012 }
1013
1014 /*package*/ void resumeCurrentTab() {
1015 Tab t = getCurrentTab();
1016 if (t != null) {
1017 t.mMainView.onResume();
1018 if (t.mSubView != null) {
1019 t.mSubView.onResume();
1020 }
1021 }
1022 }
1023
1024 private void putViewInForeground(WebView v, WebViewClient vc,
1025 WebChromeClient cc) {
1026 v.setWebViewClient(vc);
1027 v.setWebChromeClient(cc);
1028 v.setOnCreateContextMenuListener(mActivity);
1029 v.setDownloadListener(mActivity);
1030 v.onResume();
1031 }
1032
1033 private void putViewInBackground(WebView v) {
1034 // Set an empty callback so that default actions are not triggered.
1035 v.setWebViewClient(mEmptyClient);
1036 v.setWebChromeClient(mBackgroundChromeClient);
1037 v.setOnCreateContextMenuListener(null);
1038 // Leave the DownloadManager attached so that downloads can start in
1039 // a non-active window. This can happen when going to a site that does
1040 // a redirect after a period of time. The user could have switched to
1041 // another tab while waiting for the download to start.
1042 v.setDownloadListener(mActivity);
1043 v.onPause();
1044 }
1045
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001046 /**
1047 * If force is true, this method skips the check for newTab == current.
1048 */
1049 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001050 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001051 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001052 return true;
1053 }
1054 if (current != null) {
1055 // Remove the current WebView and the container of the subwindow
1056 putTabInBackground(current);
1057 }
1058
1059 if (newTab == null) {
1060 return false;
1061 }
1062
1063 // Move the newTab to the end of the queue
1064 int index = mTabQueue.indexOf(newTab);
1065 if (index != -1) {
1066 mTabQueue.remove(index);
1067 }
1068 mTabQueue.add(newTab);
1069
1070 WebView mainView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001071
1072 // Display the new current tab
1073 mCurrentTab = mTabs.indexOf(newTab);
1074 mainView = newTab.mMainView;
1075 boolean needRestore = (mainView == null);
1076 if (needRestore) {
1077 // Same work as in createNewTab() except don't do new Tab()
Steve Block2bc69912009-07-30 14:45:13 +01001078 mainView = createNewWebView();
1079 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001080 }
Mike Reed7bfa63b2009-05-28 11:08:32 -04001081 putViewInForeground(mainView, mActivity.getWebViewClient(),
1082 mActivity.getWebChromeClient());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001083 // Add the subwindow if it exists
1084 if (newTab.mSubViewContainer != null) {
Mike Reed7bfa63b2009-05-28 11:08:32 -04001085 putViewInForeground(newTab.mSubView, newTab.mSubViewClient,
1086 newTab.mSubViewChromeClient);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001087 }
1088 if (needRestore) {
1089 // Have to finish setCurrentTab work before calling restoreState
1090 if (!restoreState(newTab.mSavedState, newTab)) {
1091 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
1092 }
1093 }
1094 return true;
1095 }
1096
1097 /*
1098 * Put the tab in the background using all the empty/background clients.
1099 */
1100 private void putTabInBackground(Tab t) {
Mike Reed7bfa63b2009-05-28 11:08:32 -04001101 putViewInBackground(t.mMainView);
1102 if (t.mSubView != null) {
1103 putViewInBackground(t.mSubView);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001104 }
1105 }
1106
1107 /*
1108 * Dismiss the subwindow for the given tab.
1109 */
1110 void dismissSubWindow(Tab t) {
1111 if (t != null && t.mSubView != null) {
1112 BrowserSettings.getInstance().deleteObserver(
1113 t.mSubView.getSettings());
1114 t.mSubView.destroy();
1115 t.mSubView = null;
1116 t.mSubViewContainer = null;
1117 }
1118 }
1119
1120 /**
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001121 * Ensure that Tab t has data to display in the tab picker.
The Android Open Source Project0c908882009-03-03 19:32:16 -08001122 * @param t Tab to populate.
1123 */
1124 /* package */ void populatePickerData(Tab t) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001125 if (t == null) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001126 return;
1127 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001128
1129 // mMainView == null indicates that the tab has been freed.
1130 if (t.mMainView == null) {
1131 populatePickerDataFromSavedState(t);
1132 return;
1133 }
1134
The Android Open Source Project0c908882009-03-03 19:32:16 -08001135 // FIXME: The only place we cared about subwindow was for
1136 // bookmarking (i.e. not when saving state). Was this deliberate?
1137 final WebBackForwardList list = t.mMainView.copyBackForwardList();
1138 final WebHistoryItem item =
1139 list != null ? list.getCurrentItem() : null;
1140 populatePickerData(t, item);
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001141
1142 // This method is only called during the tab picker creation. At this
1143 // point we need to listen for new pictures since the WebView is still
1144 // active.
1145 final WebView w = t.getTopWindow();
1146 w.setPictureListener(t);
1147 // Capture the picture here instead of populatePickerData since it can
1148 // be called when saving the state of a tab.
1149 t.mPickerData.mPicture = w.capturePicture();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001150 }
1151
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001152 // Create the PickerData and populate it using the saved state of the tab.
1153 private void populatePickerDataFromSavedState(Tab t) {
1154 if (t.mSavedState == null) {
1155 return;
1156 }
1157
1158 final PickerData data = new PickerData();
1159 final Bundle state = t.mSavedState;
1160 data.mUrl = state.getString(CURRURL);
1161 data.mTitle = state.getString(CURRTITLE);
1162 data.mWidth = state.getInt(CURRWIDTH, 0);
1163 // XXX: These keys are from WebView.savePicture so if they change, this
1164 // will break.
1165 data.mScale = state.getFloat("scale", 1.0f);
1166 data.mScrollX = state.getInt("scrollX", 0);
1167 data.mScrollY = state.getInt("scrollY", 0);
1168
1169 if (state.containsKey(CURRPICTURE)) {
1170 final File f = new File(t.mSavedState.getString(CURRPICTURE));
1171 try {
1172 final FileInputStream in = new FileInputStream(f);
1173 data.mPicture = Picture.createFromStream(in);
1174 in.close();
1175 } catch (Exception ex) {
1176 // Ignore any problems with inflating the picture. We just
1177 // won't draw anything.
The Android Open Source Project0c908882009-03-03 19:32:16 -08001178 }
1179 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001180
1181 // Set the tab's picker data.
1182 t.mPickerData = data;
1183 }
1184
1185 // Populate the picker data using the given history item and the current
1186 // top WebView.
1187 private void populatePickerData(Tab t, WebHistoryItem item) {
1188 final PickerData data = new PickerData();
1189 if (item != null) {
1190 data.mUrl = item.getUrl();
1191 data.mTitle = item.getTitle();
1192 if (data.mTitle == null) {
1193 data.mTitle = data.mUrl;
1194 }
1195 }
1196 // We want to display the top window in the tab picker but use the url
1197 // and title of the main window.
1198 final WebView w = t.getTopWindow();
1199 data.mWidth = w.getWidth();
1200 data.mScale = w.getScale();
1201 data.mScrollX = w.getScrollX();
1202 data.mScrollY = w.getScrollY();
Patrick Scottb0e4fc72009-07-14 10:49:22 -04001203
1204 // Remember the old picture if possible.
1205 if (t.mPickerData != null) {
1206 data.mPicture = t.mPickerData.mPicture;
1207 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001208 t.mPickerData = data;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001209 }
1210
1211 /**
1212 * Clean up the data for all tabs.
1213 */
1214 /* package */ void wipeAllPickerData() {
1215 int size = getTabCount();
1216 for (int i = 0; i < size; i++) {
1217 final Tab t = getTab(i);
1218 if (t != null && t.mSavedState == null) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001219 t.mPickerData = null;
1220 }
1221 if (t.mMainView != null) {
1222 // Clear the picture listeners.
1223 t.mMainView.setPictureListener(null);
1224 if (t.mSubView != null) {
1225 t.mSubView.setPictureListener(null);
1226 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001227 }
1228 }
1229 }
1230
1231 /*
1232 * Save the state for an individual tab.
1233 */
1234 private boolean saveState(Tab t) {
1235 if (t != null) {
1236 final WebView w = t.mMainView;
1237 // If the WebView is null it means we ran low on memory and we
1238 // already stored the saved state in mSavedState.
1239 if (w == null) {
1240 return true;
1241 }
1242 final Bundle b = new Bundle();
1243 final WebBackForwardList list = w.saveState(b);
1244 if (list != null) {
1245 final File f = new File(mThumbnailDir, w.hashCode()
1246 + "_pic.save");
1247 if (w.savePicture(b, f)) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001248 b.putString(CURRPICTURE, f.getPath());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001249 }
1250 }
1251
1252 // Store some extra info for displaying the tab in the picker.
1253 final WebHistoryItem item =
1254 list != null ? list.getCurrentItem() : null;
1255 populatePickerData(t, item);
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001256
1257 // XXX: WebView.savePicture stores the scale and scroll positions
1258 // in the bundle so we don't have to do it here.
1259 final PickerData data = t.mPickerData;
1260 if (data.mUrl != null) {
1261 b.putString(CURRURL, data.mUrl);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001262 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001263 if (data.mTitle != null) {
1264 b.putString(CURRTITLE, data.mTitle);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001265 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001266 b.putInt(CURRWIDTH, data.mWidth);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001267 b.putBoolean(CLOSEONEXIT, t.mCloseOnExit);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001268 if (t.mAppId != null) {
1269 b.putString(APPID, t.mAppId);
1270 }
1271 if (t.mOriginalUrl != null) {
1272 b.putString(ORIGINALURL, t.mOriginalUrl);
1273 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001274
1275 // Remember the parent tab so the relationship can be restored.
1276 if (t.mParentTab != null) {
1277 b.putInt(PARENTTAB, getTabIndex(t.mParentTab));
1278 }
1279
1280 // Remember the saved state.
1281 t.mSavedState = b;
1282 return true;
1283 }
1284 return false;
1285 }
1286
1287 /*
1288 * Restore the state of the tab.
1289 */
1290 private boolean restoreState(Bundle b, Tab t) {
1291 if (b == null) {
1292 return false;
1293 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001294 // Restore the internal state even if the WebView fails to restore.
1295 // This will maintain the app id, original url and close-on-exit values.
1296 t.mSavedState = null;
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001297 t.mPickerData = null;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001298 t.mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1299 t.mAppId = b.getString(APPID);
1300 t.mOriginalUrl = b.getString(ORIGINALURL);
1301
The Android Open Source Project0c908882009-03-03 19:32:16 -08001302 final WebView w = t.mMainView;
1303 final WebBackForwardList list = w.restoreState(b);
1304 if (list == null) {
1305 return false;
1306 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001307 if (b.containsKey(CURRPICTURE)) {
1308 final File f = new File(b.getString(CURRPICTURE));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001309 w.restorePicture(b, f);
1310 f.delete();
1311 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001312 return true;
1313 }
1314}