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