blob: af9928a4c46c68c865e71404e6c695c568aab034 [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
Michael Kolb8233fac2010-10-26 16:08:53 -070019import com.android.browser.IntentHandler.UrlData;
20
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.os.Bundle;
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.webkit.WebBackForwardList;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024import android.webkit.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025
26import java.io.File;
27import java.util.ArrayList;
Elliott Slaughter3d6df162010-08-25 13:17:44 -070028import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080029import java.util.List;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import java.util.Vector;
31
32class TabControl {
33 // Log Tag
34 private static final String LOGTAG = "TabControl";
35 // Maximum number of tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070036 private int mMaxTabs;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037 // Private array of WebViews that are used as tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070038 private ArrayList<Tab> mTabs;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039 // Queue of most recently viewed tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070040 private ArrayList<Tab> mTabQueue;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041 // Current position in mTabs.
42 private int mCurrentTab = -1;
Michael Kolb8233fac2010-10-26 16:08:53 -070043 // the main browser controller
44 private final Controller mController;
45
The Android Open Source Project0c908882009-03-03 19:32:16 -080046 private final File mThumbnailDir;
47
48 /**
Michael Kolb8233fac2010-10-26 16:08:53 -070049 * Construct a new TabControl object
The Android Open Source Project0c908882009-03-03 19:32:16 -080050 */
Michael Kolb8233fac2010-10-26 16:08:53 -070051 TabControl(Controller controller) {
52 mController = controller;
53 mThumbnailDir = mController.getActivity()
54 .getDir("thumbnails", 0);
55 mMaxTabs = mController.getMaxTabs();
Michael Kolb6e4653e2010-09-27 16:22:38 -070056 mTabs = new ArrayList<Tab>(mMaxTabs);
57 mTabQueue = new ArrayList<Tab>(mMaxTabs);
The Android Open Source Project0c908882009-03-03 19:32:16 -080058 }
59
60 File getThumbnailDir() {
61 return mThumbnailDir;
62 }
63
Michael Kolb68775752010-08-19 12:42:01 -070064 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080065 * Return the current tab's main WebView. This will always return the main
66 * WebView for a given tab and not a subwindow.
67 * @return The current tab's WebView.
68 */
69 WebView getCurrentWebView() {
70 Tab t = getTab(mCurrentTab);
71 if (t == null) {
72 return null;
73 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070074 return t.getWebView();
Ben Murdochbff2d602009-07-01 20:19:05 +010075 }
76
77 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080078 * Return the current tab's top-level WebView. This can return a subwindow
79 * if one exists.
80 * @return The top-level WebView of the current tab.
81 */
82 WebView getCurrentTopWebView() {
83 Tab t = getTab(mCurrentTab);
84 if (t == null) {
85 return null;
86 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070087 return t.getTopWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -080088 }
89
90 /**
91 * Return the current tab's subwindow if it exists.
92 * @return The subwindow of the current tab or null if it doesn't exist.
93 */
94 WebView getCurrentSubWindow() {
95 Tab t = getTab(mCurrentTab);
96 if (t == null) {
97 return null;
98 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070099 return t.getSubWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800100 }
101
102 /**
Michael Kolb1bf23132010-11-19 12:55:12 -0800103 * return the list of tabs
104 */
105 List<Tab> getTabs() {
106 return mTabs;
107 }
108
109 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110 * Return the tab at the specified index.
111 * @return The Tab for the specified index or null if the tab does not
112 * exist.
113 */
114 Tab getTab(int index) {
115 if (index >= 0 && index < mTabs.size()) {
116 return mTabs.get(index);
117 }
118 return null;
119 }
120
121 /**
122 * Return the current tab.
123 * @return The current tab.
124 */
125 Tab getCurrentTab() {
126 return getTab(mCurrentTab);
127 }
128
129 /**
130 * Return the current tab index.
131 * @return The current tab index
132 */
133 int getCurrentIndex() {
134 return mCurrentTab;
135 }
Michael Kolbfe251992010-07-08 15:41:55 -0700136
The Android Open Source Project0c908882009-03-03 19:32:16 -0800137 /**
138 * Given a Tab, find it's index
139 * @param Tab to find
140 * @return index of Tab or -1 if not found
141 */
142 int getTabIndex(Tab tab) {
Patrick Scottae641ac2009-04-20 13:51:49 -0400143 if (tab == null) {
144 return -1;
145 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 return mTabs.indexOf(tab);
147 }
148
Grace Kloba22ac16e2009-10-07 18:00:23 -0700149 boolean canCreateNewTab() {
Michael Kolb6e4653e2010-09-27 16:22:38 -0700150 return mMaxTabs != mTabs.size();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700151 }
152
The Android Open Source Project0c908882009-03-03 19:32:16 -0800153 /**
Elliott Slaughtere440a882010-08-20 13:54:45 -0700154 * Returns true if there are any incognito tabs open.
155 * @return True when any incognito tabs are open, false otherwise.
156 */
157 boolean hasAnyOpenIncognitoTabs() {
158 for (Tab tab : mTabs) {
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700159 if (tab.getWebView() != null && tab.getWebView().isPrivateBrowsingEnabled()) {
Elliott Slaughtere440a882010-08-20 13:54:45 -0700160 return true;
161 }
162 }
163 return false;
164 }
165
166 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700167 * Create a new tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800168 * @return The newly createTab or null if we have reached the maximum
169 * number of open tabs.
170 */
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700171 Tab createNewTab(boolean closeOnExit, String appId, String url,
172 boolean privateBrowsing) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800173 int size = mTabs.size();
174 // Return false if we have maxed out on tabs
Michael Kolb6e4653e2010-09-27 16:22:38 -0700175 if (mMaxTabs == size) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800176 return null;
177 }
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700178 final WebView w = createNewWebView(privateBrowsing);
Steve Block2bc69912009-07-30 14:45:13 +0100179
The Android Open Source Project0c908882009-03-03 19:32:16 -0800180 // Create a new tab and add it to the tab list
Michael Kolb8233fac2010-10-26 16:08:53 -0700181 Tab t = new Tab(mController, w, closeOnExit, appId, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800182 mTabs.add(t);
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700183 // Initially put the tab in the background.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700184 t.putInBackground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800185 return t;
186 }
187
188 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700189 * Create a new tab with default values for closeOnExit(false),
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700190 * appId(null), url(null), and privateBrowsing(false).
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700191 */
192 Tab createNewTab() {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700193 return createNewTab(false, null, null, false);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700194 }
195
196 /**
Leon Scrogginsfde97462010-01-11 13:06:21 -0500197 * Remove the parent child relationships from all tabs.
198 */
199 void removeParentChildRelationShips() {
200 for (Tab tab : mTabs) {
201 tab.removeFromTree();
202 }
203 }
204
205 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800206 * Remove the tab from the list. If the tab is the current tab shown, the
207 * last created tab will be shown.
208 * @param t The tab to be removed.
209 */
210 boolean removeTab(Tab t) {
211 if (t == null) {
212 return false;
213 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700214
Patrick Scottd944d4d2010-01-27 16:39:11 -0500215 // Grab the current tab before modifying the list.
216 Tab current = getCurrentTab();
217
218 // Remove t from our list of tabs.
219 mTabs.remove(t);
220
221 // Put the tab in the background only if it is the current one.
222 if (current == t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700223 t.putInBackground();
224 mCurrentTab = -1;
Patrick Scottd944d4d2010-01-27 16:39:11 -0500225 } else {
226 // If a tab that is earlier in the list gets removed, the current
227 // index no longer points to the correct tab.
228 mCurrentTab = getTabIndex(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800229 }
230
Grace Kloba22ac16e2009-10-07 18:00:23 -0700231 // destroy the tab
232 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800233 // clear it's references to parent and children
234 t.removeFromTree();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800235
236 // The tab indices have shifted, update all the saved state so we point
237 // to the correct index.
238 for (Tab tab : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700239 Vector<Tab> children = tab.getChildTabs();
240 if (children != null) {
241 for (Tab child : children) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800242 child.setParentTab(tab);
243 }
244 }
245 }
246
The Android Open Source Project0c908882009-03-03 19:32:16 -0800247 // Remove it from the queue of viewed tabs.
248 mTabQueue.remove(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800249 return true;
250 }
251
252 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800253 * Destroy all the tabs and subwindows
254 */
255 void destroy() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800256 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700257 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800258 }
259 mTabs.clear();
260 mTabQueue.clear();
261 }
262
263 /**
264 * Returns the number of tabs created.
265 * @return The number of tabs created.
266 */
267 int getTabCount() {
268 return mTabs.size();
269 }
270
The Android Open Source Project0c908882009-03-03 19:32:16 -0800271
272 /**
273 * Save the state of all the Tabs.
274 * @param outState The Bundle to save the state to.
275 */
276 void saveState(Bundle outState) {
277 final int numTabs = getTabCount();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700278 outState.putInt(Tab.NUMTABS, numTabs);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800279 final int index = getCurrentIndex();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700280 outState.putInt(Tab.CURRTAB, (index >= 0 && index < numTabs) ? index : 0);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800281 for (int i = 0; i < numTabs; i++) {
282 final Tab t = getTab(i);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700283 if (t.saveState()) {
284 outState.putBundle(Tab.WEBVIEW + i, t.getSavedState());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800285 }
286 }
287 }
288
289 /**
Patrick Scott7d50a932011-02-04 09:27:26 -0500290 * Check if the state can be restored. If the state can be restored, the
291 * current tab index is returned. This can be passed to restoreState below
292 * in order to restore the correct tab. Otherwise, -1 is returned and the
293 * state cannot be restored.
294 */
295 int canRestoreState(Bundle inState, boolean restoreIncognitoTabs) {
296 final int numTabs = (inState == null)
297 ? - 1 : inState.getInt(Tab.NUMTABS, -1);
298 if (numTabs == -1) {
299 return -1;
300 }
301 final int oldCurrentTab = inState.getInt(Tab.CURRTAB, -1);
302
303 // Determine whether the saved current tab can be restored, and if not,
304 // which tab will take its place.
305 int currentTab = -1;
306 if (restoreIncognitoTabs ||
307 !inState.getBundle(Tab.WEBVIEW + oldCurrentTab)
308 .getBoolean(Tab.INCOGNITO)) {
309 currentTab = oldCurrentTab;
310 } else {
311 for (int i = 0; i < numTabs; i++) {
312 if (!inState.getBundle(Tab.WEBVIEW + i)
313 .getBoolean(Tab.INCOGNITO)) {
314 currentTab = i;
315 break;
316 }
317 }
318 }
319
320 return currentTab;
321 }
322
323 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800324 * Restore the state of all the tabs.
Patrick Scott7d50a932011-02-04 09:27:26 -0500325 * @param currentTab The tab index to restore.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800326 * @param inState The saved state of all the tabs.
Michael Kolb1bf23132010-11-19 12:55:12 -0800327 * @param restoreIncognitoTabs Restoring private browsing tabs
328 * @param restoreAll All webviews get restored, not just the current tab
329 * (this does not override handling of incognito tabs)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800330 * @return True if there were previous tabs that were restored. False if
331 * there was no saved state or restoring the state failed.
332 */
Patrick Scott7d50a932011-02-04 09:27:26 -0500333 void restoreState(Bundle inState, int currentTab,
334 boolean restoreIncognitoTabs, boolean restoreAll) {
335 if (currentTab == -1) {
336 return;
337 }
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700338
Patrick Scott7d50a932011-02-04 09:27:26 -0500339 // If currentTab is valid, numTabs must be present.
340 final int numTabs = inState.getInt(Tab.NUMTABS, -1);
341
342 // Map saved tab indices to new indices, in case any incognito tabs
343 // need to not be restored.
344 HashMap<Integer, Integer> originalTabIndices = new HashMap<Integer, Integer>();
345 originalTabIndices.put(-1, -1);
346 for (int i = 0; i < numTabs; i++) {
347 Bundle state = inState.getBundle(Tab.WEBVIEW + i);
348
349 if (!restoreIncognitoTabs && state != null && state.getBoolean(Tab.INCOGNITO)) {
350 originalTabIndices.put(i, -1);
351 } else if (i == currentTab || restoreAll) {
352 Tab t = createNewTab();
353 // Me must set the current tab before restoring the state
354 // so that all the client classes are set.
355 if (i == currentTab) {
356 setCurrentTab(t);
357 }
358 if (!t.restoreState(state)) {
359 Log.w(LOGTAG, "Fail in restoreState, load home page.");
360 t.getWebView().loadUrl(BrowserSettings.getInstance()
361 .getHomePage());
362 }
363 originalTabIndices.put(i, getTabCount() - 1);
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700364 } else {
Patrick Scott7d50a932011-02-04 09:27:26 -0500365 // Create a new tab and don't restore the state yet, add it
366 // to the tab list
367 Tab t = new Tab(mController, null, false, null, null);
368 if (state != null) {
369 t.setSavedState(state);
370 // Need to maintain the app id and original url so we
371 // can possibly reuse this tab.
372 t.setAppId(state.getString(Tab.APPID));
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700373 }
Patrick Scott7d50a932011-02-04 09:27:26 -0500374 mTabs.add(t);
375 // added the tab to the front as they are not current
376 mTabQueue.add(0, t);
377 originalTabIndices.put(i, getTabCount() - 1);
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700378 }
Patrick Scott7d50a932011-02-04 09:27:26 -0500379 }
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700380
Patrick Scott7d50a932011-02-04 09:27:26 -0500381 // Rebuild the tree of tabs. Do this after all tabs have been
382 // created/restored so that the parent tab exists.
383 for (int i = 0; i < numTabs; i++) {
384 final Bundle b = inState.getBundle(Tab.WEBVIEW + i);
385 final Tab t = getTab(i);
386 if (b != null && t != null) {
387 final Integer parentIndex = originalTabIndices.get(b.getInt(Tab.PARENTTAB, -1));
388 if (parentIndex != -1) {
389 final Tab parent = getTab(parentIndex);
390 if (parent != null) {
391 parent.addChildTab(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800392 }
393 }
394 }
395 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800396 }
397
398 /**
Grace Klobaf56f68d2010-04-11 22:53:42 -0700399 * Free the memory in this order, 1) free the background tabs; 2) free the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800400 * WebView cache;
401 */
402 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700403 if (getTabCount() == 0) return;
404
Grace Klobaf56f68d2010-04-11 22:53:42 -0700405 // free the least frequently used background tabs
406 Vector<Tab> tabs = getHalfLeastUsedTabs(getCurrentTab());
407 if (tabs.size() > 0) {
408 Log.w(LOGTAG, "Free " + tabs.size() + " tabs in the browser");
409 for (Tab t : tabs) {
410 // store the WebView's state.
411 t.saveState();
412 // destroy the tab
413 t.destroy();
414 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800415 return;
416 }
417
Derek Sollenberger4433d032009-06-10 15:37:21 -0400418 // free the WebView's unused memory (this includes the cache)
419 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800420 WebView view = getCurrentWebView();
421 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400422 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800423 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800424 }
425
Grace Klobaf56f68d2010-04-11 22:53:42 -0700426 private Vector<Tab> getHalfLeastUsedTabs(Tab current) {
427 Vector<Tab> tabsToGo = new Vector<Tab>();
428
Patrick Scott2a67de42009-08-31 09:48:37 -0400429 // Don't do anything if we only have 1 tab or if the current tab is
430 // null.
431 if (getTabCount() == 1 || current == null) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700432 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800433 }
434
Grace Klobaf56f68d2010-04-11 22:53:42 -0700435 if (mTabQueue.size() == 0) {
436 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800437 }
438
Grace Klobaf56f68d2010-04-11 22:53:42 -0700439 // Rip through the queue starting at the beginning and tear down half of
440 // available tabs which are not the current tab or the parent of the
441 // current tab.
442 int openTabCount = 0;
443 for (Tab t : mTabQueue) {
444 if (t != null && t.getWebView() != null) {
445 openTabCount++;
446 if (t != current && t != current.getParentTab()) {
447 tabsToGo.add(t);
448 }
449 }
450 }
451
452 openTabCount /= 2;
453 if (tabsToGo.size() > openTabCount) {
454 tabsToGo.setSize(openTabCount);
455 }
456
457 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800458 }
459
The Android Open Source Project0c908882009-03-03 19:32:16 -0800460 /**
461 * Show the tab that contains the given WebView.
462 * @param view The WebView used to find the tab.
463 */
464 Tab getTabFromView(WebView view) {
465 final int size = getTabCount();
466 for (int i = 0; i < size; i++) {
467 final Tab t = getTab(i);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700468 if (t.getSubWebView() == view || t.getWebView() == view) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800469 return t;
470 }
471 }
472 return null;
473 }
474
475 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700476 * Return the tab with the matching application id.
477 * @param id The application identifier.
478 */
479 Tab getTabFromId(String id) {
480 if (id == null) {
481 return null;
482 }
483 final int size = getTabCount();
484 for (int i = 0; i < size; i++) {
485 final Tab t = getTab(i);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700486 if (id.equals(t.getAppId())) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700487 return t;
488 }
489 }
490 return null;
491 }
492
Grace Kloba22ac16e2009-10-07 18:00:23 -0700493 /**
494 * Stop loading in all opened WebView including subWindows.
495 */
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700496 void stopAllLoading() {
497 final int size = getTabCount();
498 for (int i = 0; i < size; i++) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700499 final Tab t = getTab(i);
500 final WebView webview = t.getWebView();
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700501 if (webview != null) {
502 webview.stopLoading();
503 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700504 final WebView subview = t.getSubWebView();
505 if (subview != null) {
506 webview.stopLoading();
507 }
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700508 }
509 }
510
Patrick Scottcd115892009-07-16 09:42:58 -0400511 // This method checks if a non-app tab (one created within the browser)
512 // matches the given url.
513 private boolean tabMatchesUrl(Tab t, String url) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700514 if (t.getAppId() != null) {
Patrick Scottcd115892009-07-16 09:42:58 -0400515 return false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700516 }
517 WebView webview = t.getWebView();
518 if (webview == null) {
Patrick Scottcd115892009-07-16 09:42:58 -0400519 return false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700520 } else if (url.equals(webview.getUrl())
521 || url.equals(webview.getOriginalUrl())) {
Patrick Scottcd115892009-07-16 09:42:58 -0400522 return true;
523 }
524 return false;
525 }
526
527 /**
528 * Return the tab that has no app id associated with it and the url of the
529 * tab matches the given url.
530 * @param url The url to search for.
531 */
532 Tab findUnusedTabWithUrl(String url) {
533 if (url == null) {
534 return null;
535 }
536 // Check the current tab first.
537 Tab t = getCurrentTab();
538 if (t != null && tabMatchesUrl(t, url)) {
539 return t;
540 }
541 // Now check all the rest.
542 final int size = getTabCount();
543 for (int i = 0; i < size; i++) {
544 t = getTab(i);
545 if (tabMatchesUrl(t, url)) {
546 return t;
547 }
548 }
549 return null;
550 }
551
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700552 /**
John Reck30c714c2010-12-16 17:30:34 -0800553 * Recreate the main WebView of the given tab.
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700554 */
John Reck30c714c2010-12-16 17:30:34 -0800555 void recreateWebView(Tab t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700556 final WebView w = t.getWebView();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700557 if (w != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700558 t.destroy();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700559 }
560 // Create a new WebView. If this tab is the current tab, we need to put
561 // back all the clients so force it to be the current tab.
Steve Block2bc69912009-07-30 14:45:13 +0100562 t.setWebView(createNewWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700563 if (getCurrentTab() == t) {
564 setCurrentTab(t, true);
565 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700566 // Clear the saved state and picker data
567 t.setSavedState(null);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700568 }
569
570 /**
571 * Creates a new WebView and registers it with the global settings.
572 */
573 private WebView createNewWebView() {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700574 return createNewWebView(false);
575 }
576
577 /**
578 * Creates a new WebView and registers it with the global settings.
579 * @param privateBrowsing When true, enables private browsing in the new
580 * WebView.
581 */
582 private WebView createNewWebView(boolean privateBrowsing) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700583 return mController.getWebViewFactory().createWebView(privateBrowsing);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700584 }
585
586 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800587 * Put the current tab in the background and set newTab as the current tab.
588 * @param newTab The new tab. If newTab is null, the current tab is not
589 * set.
590 */
591 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700592 return setCurrentTab(newTab, false);
593 }
594
595 /**
596 * If force is true, this method skips the check for newTab == current.
597 */
598 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800599 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700600 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800601 return true;
602 }
603 if (current != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700604 current.putInBackground();
605 mCurrentTab = -1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800606 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800607 if (newTab == null) {
608 return false;
609 }
610
611 // Move the newTab to the end of the queue
612 int index = mTabQueue.indexOf(newTab);
613 if (index != -1) {
614 mTabQueue.remove(index);
615 }
616 mTabQueue.add(newTab);
617
The Android Open Source Project0c908882009-03-03 19:32:16 -0800618 // Display the new current tab
619 mCurrentTab = mTabs.indexOf(newTab);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700620 WebView mainView = newTab.getWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800621 boolean needRestore = (mainView == null);
622 if (needRestore) {
623 // Same work as in createNewTab() except don't do new Tab()
Steve Block2bc69912009-07-30 14:45:13 +0100624 mainView = createNewWebView();
625 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800626 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700627 newTab.putInForeground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800628 if (needRestore) {
629 // Have to finish setCurrentTab work before calling restoreState
Grace Kloba22ac16e2009-10-07 18:00:23 -0700630 if (!newTab.restoreState(newTab.getSavedState())) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800631 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
632 }
633 }
634 return true;
635 }
Michael Kolbfe251992010-07-08 15:41:55 -0700636
The Android Open Source Project0c908882009-03-03 19:32:16 -0800637}