blob: 1df4b7489ee755a8d57b78ca5e2ee784fceef3de [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 }
108 }
109 // Subclass of WebChromeClient to display javascript dialogs.
110 private class SubWindowChromeClient extends WebChromeClient {
111 // This subwindow's tab.
112 private final Tab mTab;
113 // The main WebChromeClient.
114 private final WebChromeClient mClient;
115
116 SubWindowChromeClient(Tab t, WebChromeClient client) {
117 mTab = t;
118 mClient = client;
119 }
120 @Override
121 public void onProgressChanged(WebView view, int newProgress) {
122 mClient.onProgressChanged(view, newProgress);
123 }
124 @Override
125 public boolean onCreateWindow(WebView view, boolean dialog,
126 boolean userGesture, android.os.Message resultMsg) {
127 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
128 }
129 @Override
130 public void onCloseWindow(WebView window) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700131 if (Browser.DEBUG && window != mTab.mSubView) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800132 throw new AssertionError("Can't close the window");
133 }
134 mActivity.dismissSubWindow(mTab);
135 }
136 }
137 // Background WebChromeClient for focusing tabs
138 private class BackgroundChromeClient extends WebChromeClient {
139 @Override
140 public void onRequestFocus(WebView view) {
141 Tab t = getTabFromView(view);
142 if (t != getCurrentTab()) {
143 mActivity.showTab(t);
144 }
145 }
146 }
147
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400148 // Extra saved information for displaying the tab in the picker.
149 public static class PickerData {
150 String mUrl;
151 String mTitle;
152 float mScale;
153 int mScrollX;
154 int mScrollY;
155 int mWidth;
156 Picture mPicture;
157 // This can be null. When a new picture comes in, this view should be
158 // invalidated to show the new picture.
159 FakeWebView mFakeWebView;
160 }
161
The Android Open Source Project0c908882009-03-03 19:32:16 -0800162 /**
163 * Private class for maintaining Tabs with a main WebView and a subwindow.
164 */
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400165 public class Tab implements WebView.PictureListener {
Steve Block2bc69912009-07-30 14:45:13 +0100166 // The Geolocation permissions prompt
167 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
168 private View mContainer;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800169 // Main WebView
170 private WebView mMainView;
171 // Subwindow WebView
172 private WebView mSubView;
173 // Subwindow container
174 private View mSubViewContainer;
175 // Subwindow callback
176 private SubWindowClient mSubViewClient;
177 // Subwindow chrome callback
178 private SubWindowChromeClient mSubViewChromeClient;
179 // Saved bundle for when we are running low on memory. It contains the
180 // information needed to restore the WebView if the user goes back to
181 // the tab.
182 private Bundle mSavedState;
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400183 // Data used when displaying the tab in the picker.
184 private PickerData mPickerData;
185
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186 // Parent Tab. This is the Tab that created this Tab, or null
187 // if the Tab was created by the UI
188 private Tab mParentTab;
189 // Tab that constructed by this Tab. This is used when this
190 // Tab is destroyed, it clears all mParentTab values in the
191 // children.
192 private Vector<Tab> mChildTabs;
193
194 private Boolean mCloseOnExit;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700195 // Application identifier used to find tabs that another application
196 // wants to reuse.
197 private String mAppId;
198 // Keep the original url around to avoid killing the old WebView if the
199 // url has not changed.
200 private String mOriginalUrl;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800201
Ben Murdochbff2d602009-07-01 20:19:05 +0100202 private ErrorConsoleView mErrorConsole;
203
The Android Open Source Project0c908882009-03-03 19:32:16 -0800204 // Construct a new tab
Steve Block2bc69912009-07-30 14:45:13 +0100205 private Tab(WebView w, boolean closeOnExit, String appId, String url, Context context) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800206 mCloseOnExit = closeOnExit;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700207 mAppId = appId;
208 mOriginalUrl = url;
Steve Block2bc69912009-07-30 14:45:13 +0100209
210 // The tab consists of a container view, which contains the main
211 // WebView, as well as any other UI elements associated with the tab.
212 //
213 // FIXME: Fix the interaction between this layout and the animation
214 // used when switching to and from the tab picker. This may not be
215 // required if the tab selection UI is redesigned.
216 LayoutInflater factory = LayoutInflater.from(context);
217 mContainer = factory.inflate(R.layout.tab, null);
218
219 mGeolocationPermissionsPrompt =
220 (GeolocationPermissionsPrompt) mContainer.findViewById(
221 R.id.geolocation_permissions_prompt);
222
223 setWebView(w);
224 }
225
226 /**
227 * Sets the WebView for this tab, correctly removing the old WebView
228 * from, and inserting the new WebView into, the container view.
229 */
230 public void setWebView(WebView w) {
231 if (mMainView == w) {
232 return;
233 }
234 // If the WebView is changing, the page will be reloaded, so any ongoing Geolocation
235 // permission requests are void.
236 mGeolocationPermissionsPrompt.hide();
237
238 FrameLayout wrapper = (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
239 if (mMainView != null) {
240 wrapper.removeView(mMainView);
241 }
242 mMainView = w;
243 if (mMainView != null) {
244 wrapper.addView(mMainView);
245 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800246 }
247
248 /**
249 * Return the top window of this tab; either the subwindow if it is not
250 * null or the main window.
251 * @return The top window of this tab.
252 */
253 public WebView getTopWindow() {
254 if (mSubView != null) {
255 return mSubView;
256 }
257 return mMainView;
258 }
259
260 /**
261 * Return the main window of this tab. Note: if a tab is freed in the
262 * background, this can return null. It is only guaranteed to be
263 * non-null for the current tab.
264 * @return The main WebView of this tab.
265 */
266 public WebView getWebView() {
267 return mMainView;
268 }
269
270 /**
Steve Block2bc69912009-07-30 14:45:13 +0100271 * @return The container for this tab.
272 */
273 public View getContainer() {
274 return mContainer;
275 }
276
277 /**
278 * @return The geolocation permissions prompt for this tab.
279 */
280 public GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
281 return mGeolocationPermissionsPrompt;
282 }
283
284 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800285 * Return the subwindow of this tab or null if there is no subwindow.
286 * @return The subwindow of this tab or null.
287 */
288 public WebView getSubWebView() {
289 return mSubView;
290 }
291
292 /**
293 * Return the subwindow container of this tab or null if there is no
294 * subwindow.
295 * @return The subwindow's container View.
296 */
297 public View getSubWebViewContainer() {
298 return mSubViewContainer;
299 }
300
301 /**
302 * Get the url of this tab. Valid after calling populatePickerData, but
303 * before calling wipePickerData, or if the webview has been destroyed.
304 *
305 * @return The WebView's url or null.
306 */
307 public String getUrl() {
Patrick Scott20abe042009-04-28 08:03:29 -0400308 if (mPickerData != null) {
309 return mPickerData.mUrl;
310 }
311 return null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800312 }
313
314 /**
315 * Get the title of this tab. Valid after calling populatePickerData,
316 * but before calling wipePickerData, or if the webview has been
317 * destroyed. If the url has no title, use the url instead.
318 *
319 * @return The WebView's title (or url) or null.
320 */
321 public String getTitle() {
Patrick Scott20abe042009-04-28 08:03:29 -0400322 if (mPickerData != null) {
323 return mPickerData.mTitle;
324 }
325 return null;
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400326 }
327
328 /**
329 * Returns the picker data.
330 */
331 public PickerData getPickerData() {
332 return mPickerData;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800333 }
334
335 private void setParentTab(Tab parent) {
336 mParentTab = parent;
337 // This tab may have been freed due to low memory. If that is the
338 // case, the parent tab index is already saved. If we are changing
339 // that index (most likely due to removing the parent tab) we must
340 // update the parent tab index in the saved Bundle.
341 if (mSavedState != null) {
342 if (parent == null) {
343 mSavedState.remove(PARENTTAB);
344 } else {
345 mSavedState.putInt(PARENTTAB, getTabIndex(parent));
346 }
347 }
348 }
349
350 /**
351 * When a Tab is created through the content of another Tab, then
352 * we associate the Tabs.
353 * @param child the Tab that was created from this Tab
354 */
355 public void addChildTab(Tab child) {
356 if (mChildTabs == null) {
357 mChildTabs = new Vector<Tab>();
358 }
359 mChildTabs.add(child);
360 child.setParentTab(this);
361 }
362
363 private void removeFromTree() {
364 // detach the children
365 if (mChildTabs != null) {
366 for(Tab t : mChildTabs) {
367 t.setParentTab(null);
368 }
369 }
370
371 // Find myself in my parent list
372 if (mParentTab != null) {
373 mParentTab.mChildTabs.remove(this);
374 }
375 }
376
377 /**
378 * If this Tab was created through another Tab, then this method
379 * returns that Tab.
380 * @return the Tab parent or null
381 */
382 public Tab getParentTab() {
383 return mParentTab;
384 }
385
386 /**
387 * Return whether this tab should be closed when it is backing out of
388 * the first page.
389 * @return TRUE if this tab should be closed when exit.
390 */
391 public boolean closeOnExit() {
392 return mCloseOnExit;
393 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400394
395 public void onNewPicture(WebView view, Picture p) {
396 if (mPickerData == null) {
397 return;
398 }
399
400 mPickerData.mPicture = p;
401 // Tell the FakeWebView to redraw.
402 if (mPickerData.mFakeWebView != null) {
403 mPickerData.mFakeWebView.invalidate();
404 }
405 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800406 };
407
408 // Directory to store thumbnails for each WebView.
409 private final File mThumbnailDir;
410
411 /**
412 * Construct a new TabControl object that interfaces with the given
413 * BrowserActivity instance.
414 * @param activity A BrowserActivity instance that TabControl will interface
415 * with.
416 */
417 TabControl(BrowserActivity activity) {
418 mActivity = activity;
419 mInflateService =
420 ((LayoutInflater) activity.getSystemService(
421 Context.LAYOUT_INFLATER_SERVICE));
422 mThumbnailDir = activity.getDir("thumbnails", 0);
423 }
424
425 File getThumbnailDir() {
426 return mThumbnailDir;
427 }
428
429 BrowserActivity getBrowserActivity() {
430 return mActivity;
431 }
432
433 /**
434 * Return the current tab's main WebView. This will always return the main
435 * WebView for a given tab and not a subwindow.
436 * @return The current tab's WebView.
437 */
438 WebView getCurrentWebView() {
439 Tab t = getTab(mCurrentTab);
440 if (t == null) {
441 return null;
442 }
443 return t.mMainView;
444 }
445
446 /**
Ben Murdochbff2d602009-07-01 20:19:05 +0100447 * Return the current tab's error console. Creates the console if createIfNEcessary
448 * is true and we haven't already created the console.
449 * @param createIfNecessary Flag to indicate if the console should be created if it has
450 * not been already.
451 * @return The current tab's error console, or null if one has not been created and
452 * createIfNecessary is false.
453 */
454 ErrorConsoleView getCurrentErrorConsole(boolean createIfNecessary) {
455 Tab t = getTab(mCurrentTab);
456 if (t == null) {
457 return null;
458 }
459
460 if (createIfNecessary && t.mErrorConsole == null) {
461 t.mErrorConsole = new ErrorConsoleView(mActivity);
462 t.mErrorConsole.setWebView(t.mMainView);
463 }
464
465 return t.mErrorConsole;
466 }
467
468 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800469 * Return the current tab's top-level WebView. This can return a subwindow
470 * if one exists.
471 * @return The top-level WebView of the current tab.
472 */
473 WebView getCurrentTopWebView() {
474 Tab t = getTab(mCurrentTab);
475 if (t == null) {
476 return null;
477 }
478 return t.mSubView != null ? t.mSubView : t.mMainView;
479 }
480
481 /**
482 * Return the current tab's subwindow if it exists.
483 * @return The subwindow of the current tab or null if it doesn't exist.
484 */
485 WebView getCurrentSubWindow() {
486 Tab t = getTab(mCurrentTab);
487 if (t == null) {
488 return null;
489 }
490 return t.mSubView;
491 }
492
493 /**
494 * Return the tab at the specified index.
495 * @return The Tab for the specified index or null if the tab does not
496 * exist.
497 */
498 Tab getTab(int index) {
499 if (index >= 0 && index < mTabs.size()) {
500 return mTabs.get(index);
501 }
502 return null;
503 }
504
505 /**
506 * Return the current tab.
507 * @return The current tab.
508 */
509 Tab getCurrentTab() {
510 return getTab(mCurrentTab);
511 }
512
513 /**
514 * Return the current tab index.
515 * @return The current tab index
516 */
517 int getCurrentIndex() {
518 return mCurrentTab;
519 }
520
521 /**
522 * Given a Tab, find it's index
523 * @param Tab to find
524 * @return index of Tab or -1 if not found
525 */
526 int getTabIndex(Tab tab) {
Patrick Scottae641ac2009-04-20 13:51:49 -0400527 if (tab == null) {
528 return -1;
529 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800530 return mTabs.indexOf(tab);
531 }
532
533 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700534 * Create a new tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800535 * @return The newly createTab or null if we have reached the maximum
536 * number of open tabs.
537 */
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700538 Tab createNewTab(boolean closeOnExit, String appId, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800539 int size = mTabs.size();
540 // Return false if we have maxed out on tabs
541 if (MAX_TABS == size) {
542 return null;
543 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700544 final WebView w = createNewWebView();
Steve Block2bc69912009-07-30 14:45:13 +0100545
The Android Open Source Project0c908882009-03-03 19:32:16 -0800546 // Create a new tab and add it to the tab list
Steve Block2bc69912009-07-30 14:45:13 +0100547 Tab t = new Tab(w, closeOnExit, appId, url, mActivity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800548 mTabs.add(t);
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700549 // Initially put the tab in the background.
550 putTabInBackground(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800551 return t;
552 }
553
554 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700555 * Create a new tab with default values for closeOnExit(false),
556 * appId(null), and url(null).
557 */
558 Tab createNewTab() {
559 return createNewTab(false, null, null);
560 }
561
562 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800563 * Remove the tab from the list. If the tab is the current tab shown, the
564 * last created tab will be shown.
565 * @param t The tab to be removed.
566 */
567 boolean removeTab(Tab t) {
568 if (t == null) {
569 return false;
570 }
571 // Only remove the tab if it is the current one.
572 if (getCurrentTab() == t) {
573 putTabInBackground(t);
574 }
575
576 // Only destroy the WebView if it still exists.
577 if (t.mMainView != null) {
578 // Take down the sub window.
579 dismissSubWindow(t);
580 // Remove the WebView's settings from the BrowserSettings list of
581 // observers.
582 BrowserSettings.getInstance().deleteObserver(
583 t.mMainView.getSettings());
584 // Destroy the main view and subview
585 t.mMainView.destroy();
Steve Block2bc69912009-07-30 14:45:13 +0100586 t.setWebView(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800587 }
588 // clear it's references to parent and children
589 t.removeFromTree();
590
591 // Remove it from our list of tabs.
592 mTabs.remove(t);
593
594 // The tab indices have shifted, update all the saved state so we point
595 // to the correct index.
596 for (Tab tab : mTabs) {
597 if (tab.mChildTabs != null) {
598 for (Tab child : tab.mChildTabs) {
599 child.setParentTab(tab);
600 }
601 }
602 }
603
604
605 // This tab may have been pushed in to the background and then closed.
606 // If the saved state contains a picture file, delete the file.
607 if (t.mSavedState != null) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400608 if (t.mSavedState.containsKey(CURRPICTURE)) {
609 new File(t.mSavedState.getString(CURRPICTURE)).delete();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800610 }
611 }
612
613 // Remove it from the queue of viewed tabs.
614 mTabQueue.remove(t);
615 mCurrentTab = -1;
616 return true;
617 }
618
619 /**
620 * Clear the back/forward list for all the current tabs.
621 */
622 void clearHistory() {
623 int size = getTabCount();
624 for (int i = 0; i < size; i++) {
625 Tab t = mTabs.get(i);
626 // TODO: if a tab is freed due to low memory, its history is not
627 // cleared here.
628 if (t.mMainView != null) {
629 t.mMainView.clearHistory();
630 }
631 if (t.mSubView != null) {
632 t.mSubView.clearHistory();
633 }
634 }
635 }
636
637 /**
638 * Destroy all the tabs and subwindows
639 */
640 void destroy() {
641 BrowserSettings s = BrowserSettings.getInstance();
642 for (Tab t : mTabs) {
643 if (t.mMainView != null) {
644 dismissSubWindow(t);
645 s.deleteObserver(t.mMainView.getSettings());
646 t.mMainView.destroy();
Steve Block2bc69912009-07-30 14:45:13 +0100647 t.setWebView(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800648 }
649 }
650 mTabs.clear();
651 mTabQueue.clear();
652 }
653
654 /**
655 * Returns the number of tabs created.
656 * @return The number of tabs created.
657 */
658 int getTabCount() {
659 return mTabs.size();
660 }
661
662 // Used for saving and restoring each Tab
663 private static final String WEBVIEW = "webview";
664 private static final String NUMTABS = "numTabs";
665 private static final String CURRTAB = "currentTab";
666 private static final String CURRURL = "currentUrl";
667 private static final String CURRTITLE = "currentTitle";
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400668 private static final String CURRWIDTH = "currentWidth";
669 private static final String CURRPICTURE = "currentPicture";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800670 private static final String CLOSEONEXIT = "closeonexit";
671 private static final String PARENTTAB = "parentTab";
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700672 private static final String APPID = "appid";
673 private static final String ORIGINALURL = "originalUrl";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674
675 /**
676 * Save the state of all the Tabs.
677 * @param outState The Bundle to save the state to.
678 */
679 void saveState(Bundle outState) {
680 final int numTabs = getTabCount();
681 outState.putInt(NUMTABS, numTabs);
682 final int index = getCurrentIndex();
683 outState.putInt(CURRTAB, (index >= 0 && index < numTabs) ? index : 0);
684 for (int i = 0; i < numTabs; i++) {
685 final Tab t = getTab(i);
686 if (saveState(t)) {
687 outState.putBundle(WEBVIEW + i, t.mSavedState);
688 }
689 }
690 }
691
692 /**
693 * Restore the state of all the tabs.
694 * @param inState The saved state of all the tabs.
695 * @return True if there were previous tabs that were restored. False if
696 * there was no saved state or restoring the state failed.
697 */
698 boolean restoreState(Bundle inState) {
699 final int numTabs = (inState == null)
700 ? -1 : inState.getInt(NUMTABS, -1);
701 if (numTabs == -1) {
702 return false;
703 } else {
704 final int currentTab = inState.getInt(CURRTAB, -1);
705 for (int i = 0; i < numTabs; i++) {
706 if (i == currentTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700707 Tab t = createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800708 // Me must set the current tab before restoring the state
709 // so that all the client classes are set.
710 setCurrentTab(t);
711 if (!restoreState(inState.getBundle(WEBVIEW + i), t)) {
712 Log.w(LOGTAG, "Fail in restoreState, load home page.");
713 t.mMainView.loadUrl(BrowserSettings.getInstance()
714 .getHomePage());
715 }
716 } else {
717 // Create a new tab and don't restore the state yet, add it
718 // to the tab list
Steve Block2bc69912009-07-30 14:45:13 +0100719 Tab t = new Tab(null, false, null, null, mActivity);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800720 t.mSavedState = inState.getBundle(WEBVIEW + i);
721 if (t.mSavedState != null) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400722 populatePickerDataFromSavedState(t);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700723 // Need to maintain the app id and original url so we
724 // can possibly reuse this tab.
725 t.mAppId = t.mSavedState.getString(APPID);
726 t.mOriginalUrl = t.mSavedState.getString(ORIGINALURL);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800727 }
728 mTabs.add(t);
729 mTabQueue.add(t);
730 }
731 }
732 // Rebuild the tree of tabs. Do this after all tabs have been
733 // created/restored so that the parent tab exists.
734 for (int i = 0; i < numTabs; i++) {
735 final Bundle b = inState.getBundle(WEBVIEW + i);
736 final Tab t = getTab(i);
737 if (b != null && t != null) {
738 final int parentIndex = b.getInt(PARENTTAB, -1);
739 if (parentIndex != -1) {
740 final Tab parent = getTab(parentIndex);
741 if (parent != null) {
742 parent.addChildTab(t);
743 }
744 }
745 }
746 }
747 }
748 return true;
749 }
750
751 /**
752 * Free the memory in this order, 1) free the background tab; 2) free the
753 * WebView cache;
754 */
755 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700756 if (getTabCount() == 0) return;
757
The Android Open Source Project0c908882009-03-03 19:32:16 -0800758 // free the least frequently used background tab
Grace Kloba92c18a52009-07-31 23:48:32 -0700759 Tab t = getLeastUsedTab(getCurrentTab().getParentTab());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800760 if (t != null) {
761 Log.w(LOGTAG, "Free a tab in the browser");
762 freeTab(t);
763 // force a gc
764 System.gc();
765 return;
766 }
767
Derek Sollenberger4433d032009-06-10 15:37:21 -0400768 // free the WebView's unused memory (this includes the cache)
769 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800770 WebView view = getCurrentWebView();
771 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400772 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800773 }
774 // force a gc
775 System.gc();
776 }
777
Grace Kloba92c18a52009-07-31 23:48:32 -0700778 private Tab getLeastUsedTab(Tab currentParent) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800779 // Don't do anything if we only have 1 tab.
780 if (getTabCount() == 1) {
781 return null;
782 }
783
784 // Rip through the queue starting at the beginning and teardown the
785 // next available tab.
786 Tab t = null;
787 int i = 0;
788 final int queueSize = mTabQueue.size();
789 if (queueSize == 0) {
790 return null;
791 }
792 do {
793 t = mTabQueue.get(i++);
Grace Kloba92c18a52009-07-31 23:48:32 -0700794 } while (i < queueSize
795 && ((t != null && t.mMainView == null) || t == currentParent));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800796
Patrick Scottd068f802009-06-22 11:46:06 -0400797 // Don't do anything if the last remaining tab is the current one or if
798 // the last tab has been freed already.
799 if (t == getCurrentTab() || t.mMainView == null) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800800 return null;
801 }
802
803 return t;
804 }
805
806 private void freeTab(Tab t) {
807 // Store the WebView's state.
808 saveState(t);
809
810 // Tear down the tab.
811 dismissSubWindow(t);
812 // Remove the WebView's settings from the BrowserSettings list of
813 // observers.
814 BrowserSettings.getInstance().deleteObserver(t.mMainView.getSettings());
815 t.mMainView.destroy();
Steve Block2bc69912009-07-30 14:45:13 +0100816 t.setWebView(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800817 }
818
819 /**
820 * Create a new subwindow unless a subwindow already exists.
821 * @return True if a new subwindow was created. False if one already exists.
822 */
823 void createSubWindow() {
824 Tab t = getTab(mCurrentTab);
825 if (t != null && t.mSubView == null) {
826 final View v = mInflateService.inflate(R.layout.browser_subwindow, null);
827 final WebView w = (WebView) v.findViewById(R.id.webview);
828 w.setMapTrackballToArrowKeys(false); // use trackball directly
829 final SubWindowClient subClient =
830 new SubWindowClient(mActivity.getWebViewClient());
831 final SubWindowChromeClient subChromeClient =
832 new SubWindowChromeClient(t,
833 mActivity.getWebChromeClient());
834 w.setWebViewClient(subClient);
835 w.setWebChromeClient(subChromeClient);
836 w.setDownloadListener(mActivity);
837 w.setOnCreateContextMenuListener(mActivity);
838 final BrowserSettings s = BrowserSettings.getInstance();
839 s.addObserver(w.getSettings()).update(s, null);
840 t.mSubView = w;
841 t.mSubViewClient = subClient;
842 t.mSubViewChromeClient = subChromeClient;
843 // FIXME: I really hate having to know the name of the view
844 // containing the webview.
845 t.mSubViewContainer = v.findViewById(R.id.subwindow_container);
846 final ImageButton cancel =
847 (ImageButton) v.findViewById(R.id.subwindow_close);
848 cancel.setOnClickListener(new OnClickListener() {
849 public void onClick(View v) {
850 subChromeClient.onCloseWindow(w);
851 }
852 });
853 }
854 }
855
856 /**
857 * Show the tab that contains the given WebView.
858 * @param view The WebView used to find the tab.
859 */
860 Tab getTabFromView(WebView view) {
861 final int size = getTabCount();
862 for (int i = 0; i < size; i++) {
863 final Tab t = getTab(i);
864 if (t.mSubView == view || t.mMainView == view) {
865 return t;
866 }
867 }
868 return null;
869 }
870
871 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700872 * Return the tab with the matching application id.
873 * @param id The application identifier.
874 */
875 Tab getTabFromId(String id) {
876 if (id == null) {
877 return null;
878 }
879 final int size = getTabCount();
880 for (int i = 0; i < size; i++) {
881 final Tab t = getTab(i);
882 if (id.equals(t.mAppId)) {
883 return t;
884 }
885 }
886 return null;
887 }
888
Patrick Scottcd115892009-07-16 09:42:58 -0400889 // This method checks if a non-app tab (one created within the browser)
890 // matches the given url.
891 private boolean tabMatchesUrl(Tab t, String url) {
892 if (t.mAppId != null) {
893 return false;
894 } else if (t.mMainView == null) {
895 return false;
896 } else if (url.equals(t.mMainView.getUrl()) ||
897 url.equals(t.mMainView.getOriginalUrl())) {
898 return true;
899 }
900 return false;
901 }
902
903 /**
904 * Return the tab that has no app id associated with it and the url of the
905 * tab matches the given url.
906 * @param url The url to search for.
907 */
908 Tab findUnusedTabWithUrl(String url) {
909 if (url == null) {
910 return null;
911 }
912 // Check the current tab first.
913 Tab t = getCurrentTab();
914 if (t != null && tabMatchesUrl(t, url)) {
915 return t;
916 }
917 // Now check all the rest.
918 final int size = getTabCount();
919 for (int i = 0; i < size; i++) {
920 t = getTab(i);
921 if (tabMatchesUrl(t, url)) {
922 return t;
923 }
924 }
925 return null;
926 }
927
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700928 /**
929 * Recreate the main WebView of the given tab. Returns true if the WebView
930 * was deleted.
931 */
932 boolean recreateWebView(Tab t, String url) {
933 final WebView w = t.mMainView;
934 if (w != null) {
935 if (url != null && url.equals(t.mOriginalUrl)) {
936 // The original url matches the current url. Just go back to the
937 // first history item so we can load it faster than if we
938 // rebuilt the WebView.
939 final WebBackForwardList list = w.copyBackForwardList();
940 if (list != null) {
941 w.goBackOrForward(-list.getCurrentIndex());
942 w.clearHistory(); // maintains the current page.
943 return false;
944 }
945 }
946 // Remove the settings object from the global settings and destroy
947 // the WebView.
948 BrowserSettings.getInstance().deleteObserver(
949 t.mMainView.getSettings());
950 t.mMainView.destroy();
951 }
952 // Create a new WebView. If this tab is the current tab, we need to put
953 // back all the clients so force it to be the current tab.
Steve Block2bc69912009-07-30 14:45:13 +0100954 t.setWebView(createNewWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700955 if (getCurrentTab() == t) {
956 setCurrentTab(t, true);
957 }
958 // Clear the saved state except for the app id and close-on-exit
959 // values.
960 t.mSavedState = null;
Patrick Scott2ed6edb2009-04-22 10:07:45 -0400961 t.mPickerData = null;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700962 // Save the new url in order to avoid deleting the WebView.
963 t.mOriginalUrl = url;
964 return true;
965 }
966
967 /**
968 * Creates a new WebView and registers it with the global settings.
969 */
970 private WebView createNewWebView() {
971 // Create a new WebView
972 WebView w = new WebView(mActivity);
973 w.setMapTrackballToArrowKeys(false); // use trackball directly
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700974 // Enable the built-in zoom
975 w.getSettings().setBuiltInZoomControls(true);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700976 // Add this WebView to the settings observer list and update the
977 // settings
978 final BrowserSettings s = BrowserSettings.getInstance();
979 s.addObserver(w.getSettings()).update(s, null);
980 return w;
981 }
982
983 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800984 * Put the current tab in the background and set newTab as the current tab.
985 * @param newTab The new tab. If newTab is null, the current tab is not
986 * set.
987 */
988 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700989 return setCurrentTab(newTab, false);
990 }
991
Mike Reed7bfa63b2009-05-28 11:08:32 -0400992 /*package*/ void pauseCurrentTab() {
993 Tab t = getCurrentTab();
994 if (t != null) {
995 t.mMainView.onPause();
996 if (t.mSubView != null) {
997 t.mSubView.onPause();
998 }
999 }
1000 }
1001
1002 /*package*/ void resumeCurrentTab() {
1003 Tab t = getCurrentTab();
1004 if (t != null) {
1005 t.mMainView.onResume();
1006 if (t.mSubView != null) {
1007 t.mSubView.onResume();
1008 }
1009 }
1010 }
1011
1012 private void putViewInForeground(WebView v, WebViewClient vc,
1013 WebChromeClient cc) {
1014 v.setWebViewClient(vc);
1015 v.setWebChromeClient(cc);
1016 v.setOnCreateContextMenuListener(mActivity);
1017 v.setDownloadListener(mActivity);
1018 v.onResume();
1019 }
1020
1021 private void putViewInBackground(WebView v) {
1022 // Set an empty callback so that default actions are not triggered.
1023 v.setWebViewClient(mEmptyClient);
1024 v.setWebChromeClient(mBackgroundChromeClient);
1025 v.setOnCreateContextMenuListener(null);
1026 // Leave the DownloadManager attached so that downloads can start in
1027 // a non-active window. This can happen when going to a site that does
1028 // a redirect after a period of time. The user could have switched to
1029 // another tab while waiting for the download to start.
1030 v.setDownloadListener(mActivity);
1031 v.onPause();
1032 }
1033
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001034 /**
1035 * If force is true, this method skips the check for newTab == current.
1036 */
1037 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001038 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001039 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001040 return true;
1041 }
1042 if (current != null) {
1043 // Remove the current WebView and the container of the subwindow
1044 putTabInBackground(current);
1045 }
1046
1047 if (newTab == null) {
1048 return false;
1049 }
1050
1051 // Move the newTab to the end of the queue
1052 int index = mTabQueue.indexOf(newTab);
1053 if (index != -1) {
1054 mTabQueue.remove(index);
1055 }
1056 mTabQueue.add(newTab);
1057
1058 WebView mainView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001059
1060 // Display the new current tab
1061 mCurrentTab = mTabs.indexOf(newTab);
1062 mainView = newTab.mMainView;
1063 boolean needRestore = (mainView == null);
1064 if (needRestore) {
1065 // Same work as in createNewTab() except don't do new Tab()
Steve Block2bc69912009-07-30 14:45:13 +01001066 mainView = createNewWebView();
1067 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001068 }
Mike Reed7bfa63b2009-05-28 11:08:32 -04001069 putViewInForeground(mainView, mActivity.getWebViewClient(),
1070 mActivity.getWebChromeClient());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001071 // Add the subwindow if it exists
1072 if (newTab.mSubViewContainer != null) {
Mike Reed7bfa63b2009-05-28 11:08:32 -04001073 putViewInForeground(newTab.mSubView, newTab.mSubViewClient,
1074 newTab.mSubViewChromeClient);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001075 }
1076 if (needRestore) {
1077 // Have to finish setCurrentTab work before calling restoreState
1078 if (!restoreState(newTab.mSavedState, newTab)) {
1079 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
1080 }
1081 }
1082 return true;
1083 }
1084
1085 /*
1086 * Put the tab in the background using all the empty/background clients.
1087 */
1088 private void putTabInBackground(Tab t) {
Mike Reed7bfa63b2009-05-28 11:08:32 -04001089 putViewInBackground(t.mMainView);
1090 if (t.mSubView != null) {
1091 putViewInBackground(t.mSubView);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001092 }
1093 }
1094
1095 /*
1096 * Dismiss the subwindow for the given tab.
1097 */
1098 void dismissSubWindow(Tab t) {
1099 if (t != null && t.mSubView != null) {
1100 BrowserSettings.getInstance().deleteObserver(
1101 t.mSubView.getSettings());
1102 t.mSubView.destroy();
1103 t.mSubView = null;
1104 t.mSubViewContainer = null;
1105 }
1106 }
1107
1108 /**
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001109 * Ensure that Tab t has data to display in the tab picker.
The Android Open Source Project0c908882009-03-03 19:32:16 -08001110 * @param t Tab to populate.
1111 */
1112 /* package */ void populatePickerData(Tab t) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001113 if (t == null) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001114 return;
1115 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001116
1117 // mMainView == null indicates that the tab has been freed.
1118 if (t.mMainView == null) {
1119 populatePickerDataFromSavedState(t);
1120 return;
1121 }
1122
The Android Open Source Project0c908882009-03-03 19:32:16 -08001123 // FIXME: The only place we cared about subwindow was for
1124 // bookmarking (i.e. not when saving state). Was this deliberate?
1125 final WebBackForwardList list = t.mMainView.copyBackForwardList();
1126 final WebHistoryItem item =
1127 list != null ? list.getCurrentItem() : null;
1128 populatePickerData(t, item);
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001129
1130 // This method is only called during the tab picker creation. At this
1131 // point we need to listen for new pictures since the WebView is still
1132 // active.
1133 final WebView w = t.getTopWindow();
1134 w.setPictureListener(t);
1135 // Capture the picture here instead of populatePickerData since it can
1136 // be called when saving the state of a tab.
1137 t.mPickerData.mPicture = w.capturePicture();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001138 }
1139
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001140 // Create the PickerData and populate it using the saved state of the tab.
1141 private void populatePickerDataFromSavedState(Tab t) {
1142 if (t.mSavedState == null) {
1143 return;
1144 }
1145
1146 final PickerData data = new PickerData();
1147 final Bundle state = t.mSavedState;
1148 data.mUrl = state.getString(CURRURL);
1149 data.mTitle = state.getString(CURRTITLE);
1150 data.mWidth = state.getInt(CURRWIDTH, 0);
1151 // XXX: These keys are from WebView.savePicture so if they change, this
1152 // will break.
1153 data.mScale = state.getFloat("scale", 1.0f);
1154 data.mScrollX = state.getInt("scrollX", 0);
1155 data.mScrollY = state.getInt("scrollY", 0);
1156
1157 if (state.containsKey(CURRPICTURE)) {
1158 final File f = new File(t.mSavedState.getString(CURRPICTURE));
1159 try {
1160 final FileInputStream in = new FileInputStream(f);
1161 data.mPicture = Picture.createFromStream(in);
1162 in.close();
1163 } catch (Exception ex) {
1164 // Ignore any problems with inflating the picture. We just
1165 // won't draw anything.
The Android Open Source Project0c908882009-03-03 19:32:16 -08001166 }
1167 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001168
1169 // Set the tab's picker data.
1170 t.mPickerData = data;
1171 }
1172
1173 // Populate the picker data using the given history item and the current
1174 // top WebView.
1175 private void populatePickerData(Tab t, WebHistoryItem item) {
1176 final PickerData data = new PickerData();
1177 if (item != null) {
1178 data.mUrl = item.getUrl();
1179 data.mTitle = item.getTitle();
1180 if (data.mTitle == null) {
1181 data.mTitle = data.mUrl;
1182 }
1183 }
1184 // We want to display the top window in the tab picker but use the url
1185 // and title of the main window.
1186 final WebView w = t.getTopWindow();
1187 data.mWidth = w.getWidth();
1188 data.mScale = w.getScale();
1189 data.mScrollX = w.getScrollX();
1190 data.mScrollY = w.getScrollY();
Patrick Scottb0e4fc72009-07-14 10:49:22 -04001191
1192 // Remember the old picture if possible.
1193 if (t.mPickerData != null) {
1194 data.mPicture = t.mPickerData.mPicture;
1195 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001196 t.mPickerData = data;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001197 }
1198
1199 /**
1200 * Clean up the data for all tabs.
1201 */
1202 /* package */ void wipeAllPickerData() {
1203 int size = getTabCount();
1204 for (int i = 0; i < size; i++) {
1205 final Tab t = getTab(i);
1206 if (t != null && t.mSavedState == null) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001207 t.mPickerData = null;
1208 }
1209 if (t.mMainView != null) {
1210 // Clear the picture listeners.
1211 t.mMainView.setPictureListener(null);
1212 if (t.mSubView != null) {
1213 t.mSubView.setPictureListener(null);
1214 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001215 }
1216 }
1217 }
1218
1219 /*
1220 * Save the state for an individual tab.
1221 */
1222 private boolean saveState(Tab t) {
1223 if (t != null) {
1224 final WebView w = t.mMainView;
1225 // If the WebView is null it means we ran low on memory and we
1226 // already stored the saved state in mSavedState.
1227 if (w == null) {
1228 return true;
1229 }
1230 final Bundle b = new Bundle();
1231 final WebBackForwardList list = w.saveState(b);
1232 if (list != null) {
1233 final File f = new File(mThumbnailDir, w.hashCode()
1234 + "_pic.save");
1235 if (w.savePicture(b, f)) {
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001236 b.putString(CURRPICTURE, f.getPath());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001237 }
1238 }
1239
1240 // Store some extra info for displaying the tab in the picker.
1241 final WebHistoryItem item =
1242 list != null ? list.getCurrentItem() : null;
1243 populatePickerData(t, item);
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001244
1245 // XXX: WebView.savePicture stores the scale and scroll positions
1246 // in the bundle so we don't have to do it here.
1247 final PickerData data = t.mPickerData;
1248 if (data.mUrl != null) {
1249 b.putString(CURRURL, data.mUrl);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001250 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001251 if (data.mTitle != null) {
1252 b.putString(CURRTITLE, data.mTitle);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001253 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001254 b.putInt(CURRWIDTH, data.mWidth);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001255 b.putBoolean(CLOSEONEXIT, t.mCloseOnExit);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001256 if (t.mAppId != null) {
1257 b.putString(APPID, t.mAppId);
1258 }
1259 if (t.mOriginalUrl != null) {
1260 b.putString(ORIGINALURL, t.mOriginalUrl);
1261 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001262
1263 // Remember the parent tab so the relationship can be restored.
1264 if (t.mParentTab != null) {
1265 b.putInt(PARENTTAB, getTabIndex(t.mParentTab));
1266 }
1267
1268 // Remember the saved state.
1269 t.mSavedState = b;
1270 return true;
1271 }
1272 return false;
1273 }
1274
1275 /*
1276 * Restore the state of the tab.
1277 */
1278 private boolean restoreState(Bundle b, Tab t) {
1279 if (b == null) {
1280 return false;
1281 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001282 // Restore the internal state even if the WebView fails to restore.
1283 // This will maintain the app id, original url and close-on-exit values.
1284 t.mSavedState = null;
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001285 t.mPickerData = null;
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001286 t.mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1287 t.mAppId = b.getString(APPID);
1288 t.mOriginalUrl = b.getString(ORIGINALURL);
1289
The Android Open Source Project0c908882009-03-03 19:32:16 -08001290 final WebView w = t.mMainView;
1291 final WebBackForwardList list = w.restoreState(b);
1292 if (list == null) {
1293 return false;
1294 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -04001295 if (b.containsKey(CURRPICTURE)) {
1296 final File f = new File(b.getString(CURRPICTURE));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001297 w.restorePicture(b, f);
1298 f.delete();
1299 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001300 return true;
1301 }
1302}