blob: 07c9fa5cba3b2f69e88248ffba98201c5a0b3be2 [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
The Android Open Source Project0c908882009-03-03 19:32:16 -080019import android.os.Bundle;
The Android Open Source Project0c908882009-03-03 19:32:16 -080020import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.webkit.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080022
23import java.io.File;
24import java.util.ArrayList;
Elliott Slaughter3d6df162010-08-25 13:17:44 -070025import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080026import java.util.List;
The Android Open Source Project0c908882009-03-03 19:32:16 -080027import java.util.Vector;
28
29class TabControl {
30 // Log Tag
31 private static final String LOGTAG = "TabControl";
32 // Maximum number of tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070033 private int mMaxTabs;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034 // Private array of WebViews that are used as tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070035 private ArrayList<Tab> mTabs;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036 // Queue of most recently viewed tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070037 private ArrayList<Tab> mTabQueue;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038 // Current position in mTabs.
39 private int mCurrentTab = -1;
Michael Kolb8233fac2010-10-26 16:08:53 -070040 // the main browser controller
41 private final Controller mController;
42
The Android Open Source Project0c908882009-03-03 19:32:16 -080043 private final File mThumbnailDir;
44
45 /**
Michael Kolb8233fac2010-10-26 16:08:53 -070046 * Construct a new TabControl object
The Android Open Source Project0c908882009-03-03 19:32:16 -080047 */
Michael Kolb8233fac2010-10-26 16:08:53 -070048 TabControl(Controller controller) {
49 mController = controller;
50 mThumbnailDir = mController.getActivity()
51 .getDir("thumbnails", 0);
52 mMaxTabs = mController.getMaxTabs();
Michael Kolb6e4653e2010-09-27 16:22:38 -070053 mTabs = new ArrayList<Tab>(mMaxTabs);
54 mTabQueue = new ArrayList<Tab>(mMaxTabs);
The Android Open Source Project0c908882009-03-03 19:32:16 -080055 }
56
57 File getThumbnailDir() {
58 return mThumbnailDir;
59 }
60
Michael Kolb68775752010-08-19 12:42:01 -070061 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080062 * Return the current tab's main WebView. This will always return the main
63 * WebView for a given tab and not a subwindow.
64 * @return The current tab's WebView.
65 */
66 WebView getCurrentWebView() {
67 Tab t = getTab(mCurrentTab);
68 if (t == null) {
69 return null;
70 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070071 return t.getWebView();
Ben Murdochbff2d602009-07-01 20:19:05 +010072 }
73
74 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080075 * Return the current tab's top-level WebView. This can return a subwindow
76 * if one exists.
77 * @return The top-level WebView of the current tab.
78 */
79 WebView getCurrentTopWebView() {
80 Tab t = getTab(mCurrentTab);
81 if (t == null) {
82 return null;
83 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070084 return t.getTopWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -080085 }
86
87 /**
88 * Return the current tab's subwindow if it exists.
89 * @return The subwindow of the current tab or null if it doesn't exist.
90 */
91 WebView getCurrentSubWindow() {
92 Tab t = getTab(mCurrentTab);
93 if (t == null) {
94 return null;
95 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070096 return t.getSubWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -080097 }
98
99 /**
Michael Kolb1bf23132010-11-19 12:55:12 -0800100 * return the list of tabs
101 */
102 List<Tab> getTabs() {
103 return mTabs;
104 }
105
106 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800107 * Return the tab at the specified index.
108 * @return The Tab for the specified index or null if the tab does not
109 * exist.
110 */
111 Tab getTab(int index) {
112 if (index >= 0 && index < mTabs.size()) {
113 return mTabs.get(index);
114 }
115 return null;
116 }
117
118 /**
119 * Return the current tab.
120 * @return The current tab.
121 */
122 Tab getCurrentTab() {
123 return getTab(mCurrentTab);
124 }
125
126 /**
127 * Return the current tab index.
128 * @return The current tab index
129 */
130 int getCurrentIndex() {
131 return mCurrentTab;
132 }
Michael Kolbfe251992010-07-08 15:41:55 -0700133
The Android Open Source Project0c908882009-03-03 19:32:16 -0800134 /**
135 * Given a Tab, find it's index
136 * @param Tab to find
137 * @return index of Tab or -1 if not found
138 */
139 int getTabIndex(Tab tab) {
Patrick Scottae641ac2009-04-20 13:51:49 -0400140 if (tab == null) {
141 return -1;
142 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800143 return mTabs.indexOf(tab);
144 }
145
Grace Kloba22ac16e2009-10-07 18:00:23 -0700146 boolean canCreateNewTab() {
Michael Kolb6e4653e2010-09-27 16:22:38 -0700147 return mMaxTabs != mTabs.size();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700148 }
149
The Android Open Source Project0c908882009-03-03 19:32:16 -0800150 /**
Elliott Slaughtere440a882010-08-20 13:54:45 -0700151 * Returns true if there are any incognito tabs open.
152 * @return True when any incognito tabs are open, false otherwise.
153 */
154 boolean hasAnyOpenIncognitoTabs() {
155 for (Tab tab : mTabs) {
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700156 if (tab.getWebView() != null && tab.getWebView().isPrivateBrowsingEnabled()) {
Elliott Slaughtere440a882010-08-20 13:54:45 -0700157 return true;
158 }
159 }
160 return false;
161 }
162
163 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700164 * Create a new tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800165 * @return The newly createTab or null if we have reached the maximum
166 * number of open tabs.
167 */
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700168 Tab createNewTab(boolean closeOnExit, String appId, String url,
169 boolean privateBrowsing) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800170 int size = mTabs.size();
171 // Return false if we have maxed out on tabs
Michael Kolb6e4653e2010-09-27 16:22:38 -0700172 if (mMaxTabs == size) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800173 return null;
174 }
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700175 final WebView w = createNewWebView(privateBrowsing);
Steve Block2bc69912009-07-30 14:45:13 +0100176
The Android Open Source Project0c908882009-03-03 19:32:16 -0800177 // Create a new tab and add it to the tab list
Michael Kolb8233fac2010-10-26 16:08:53 -0700178 Tab t = new Tab(mController, w, closeOnExit, appId, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800179 mTabs.add(t);
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700180 // Initially put the tab in the background.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700181 t.putInBackground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800182 return t;
183 }
184
185 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700186 * Create a new tab with default values for closeOnExit(false),
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700187 * appId(null), url(null), and privateBrowsing(false).
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700188 */
189 Tab createNewTab() {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700190 return createNewTab(false, null, null, false);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700191 }
192
193 /**
Leon Scrogginsfde97462010-01-11 13:06:21 -0500194 * Remove the parent child relationships from all tabs.
195 */
196 void removeParentChildRelationShips() {
197 for (Tab tab : mTabs) {
198 tab.removeFromTree();
199 }
200 }
201
202 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800203 * Remove the tab from the list. If the tab is the current tab shown, the
204 * last created tab will be shown.
205 * @param t The tab to be removed.
206 */
207 boolean removeTab(Tab t) {
208 if (t == null) {
209 return false;
210 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700211
Patrick Scottd944d4d2010-01-27 16:39:11 -0500212 // Grab the current tab before modifying the list.
213 Tab current = getCurrentTab();
214
215 // Remove t from our list of tabs.
216 mTabs.remove(t);
217
218 // Put the tab in the background only if it is the current one.
219 if (current == t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700220 t.putInBackground();
221 mCurrentTab = -1;
Patrick Scottd944d4d2010-01-27 16:39:11 -0500222 } else {
223 // If a tab that is earlier in the list gets removed, the current
224 // index no longer points to the correct tab.
225 mCurrentTab = getTabIndex(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800226 }
227
Grace Kloba22ac16e2009-10-07 18:00:23 -0700228 // destroy the tab
229 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800230 // clear it's references to parent and children
231 t.removeFromTree();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800232
233 // The tab indices have shifted, update all the saved state so we point
234 // to the correct index.
235 for (Tab tab : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700236 Vector<Tab> children = tab.getChildTabs();
237 if (children != null) {
238 for (Tab child : children) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800239 child.setParentTab(tab);
240 }
241 }
242 }
243
The Android Open Source Project0c908882009-03-03 19:32:16 -0800244 // Remove it from the queue of viewed tabs.
245 mTabQueue.remove(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800246 return true;
247 }
248
249 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800250 * Destroy all the tabs and subwindows
251 */
252 void destroy() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800253 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700254 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800255 }
256 mTabs.clear();
257 mTabQueue.clear();
258 }
259
260 /**
261 * Returns the number of tabs created.
262 * @return The number of tabs created.
263 */
264 int getTabCount() {
265 return mTabs.size();
266 }
267
The Android Open Source Project0c908882009-03-03 19:32:16 -0800268
269 /**
270 * Save the state of all the Tabs.
271 * @param outState The Bundle to save the state to.
272 */
273 void saveState(Bundle outState) {
274 final int numTabs = getTabCount();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700275 outState.putInt(Tab.NUMTABS, numTabs);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800276 final int index = getCurrentIndex();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700277 outState.putInt(Tab.CURRTAB, (index >= 0 && index < numTabs) ? index : 0);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800278 for (int i = 0; i < numTabs; i++) {
279 final Tab t = getTab(i);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700280 if (t.saveState()) {
281 outState.putBundle(Tab.WEBVIEW + i, t.getSavedState());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800282 }
283 }
284 }
285
286 /**
Patrick Scott7d50a932011-02-04 09:27:26 -0500287 * Check if the state can be restored. If the state can be restored, the
288 * current tab index is returned. This can be passed to restoreState below
289 * in order to restore the correct tab. Otherwise, -1 is returned and the
290 * state cannot be restored.
291 */
292 int canRestoreState(Bundle inState, boolean restoreIncognitoTabs) {
293 final int numTabs = (inState == null)
294 ? - 1 : inState.getInt(Tab.NUMTABS, -1);
295 if (numTabs == -1) {
296 return -1;
297 }
298 final int oldCurrentTab = inState.getInt(Tab.CURRTAB, -1);
299
300 // Determine whether the saved current tab can be restored, and if not,
301 // which tab will take its place.
302 int currentTab = -1;
303 if (restoreIncognitoTabs ||
304 !inState.getBundle(Tab.WEBVIEW + oldCurrentTab)
305 .getBoolean(Tab.INCOGNITO)) {
306 currentTab = oldCurrentTab;
307 } else {
308 for (int i = 0; i < numTabs; i++) {
309 if (!inState.getBundle(Tab.WEBVIEW + i)
310 .getBoolean(Tab.INCOGNITO)) {
311 currentTab = i;
312 break;
313 }
314 }
315 }
316
317 return currentTab;
318 }
319
320 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800321 * Restore the state of all the tabs.
Patrick Scott7d50a932011-02-04 09:27:26 -0500322 * @param currentTab The tab index to restore.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800323 * @param inState The saved state of all the tabs.
Michael Kolb1bf23132010-11-19 12:55:12 -0800324 * @param restoreIncognitoTabs Restoring private browsing tabs
325 * @param restoreAll All webviews get restored, not just the current tab
326 * (this does not override handling of incognito tabs)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800327 * @return True if there were previous tabs that were restored. False if
328 * there was no saved state or restoring the state failed.
329 */
Patrick Scott7d50a932011-02-04 09:27:26 -0500330 void restoreState(Bundle inState, int currentTab,
331 boolean restoreIncognitoTabs, boolean restoreAll) {
332 if (currentTab == -1) {
333 return;
334 }
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700335
Patrick Scott7d50a932011-02-04 09:27:26 -0500336 // If currentTab is valid, numTabs must be present.
337 final int numTabs = inState.getInt(Tab.NUMTABS, -1);
338
339 // Map saved tab indices to new indices, in case any incognito tabs
340 // need to not be restored.
341 HashMap<Integer, Integer> originalTabIndices = new HashMap<Integer, Integer>();
342 originalTabIndices.put(-1, -1);
343 for (int i = 0; i < numTabs; i++) {
344 Bundle state = inState.getBundle(Tab.WEBVIEW + i);
345
346 if (!restoreIncognitoTabs && state != null && state.getBoolean(Tab.INCOGNITO)) {
347 originalTabIndices.put(i, -1);
348 } else if (i == currentTab || restoreAll) {
349 Tab t = createNewTab();
350 // Me must set the current tab before restoring the state
351 // so that all the client classes are set.
352 if (i == currentTab) {
353 setCurrentTab(t);
354 }
355 if (!t.restoreState(state)) {
356 Log.w(LOGTAG, "Fail in restoreState, load home page.");
357 t.getWebView().loadUrl(BrowserSettings.getInstance()
358 .getHomePage());
359 }
360 originalTabIndices.put(i, getTabCount() - 1);
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700361 } else {
Patrick Scott7d50a932011-02-04 09:27:26 -0500362 // Create a new tab and don't restore the state yet, add it
363 // to the tab list
364 Tab t = new Tab(mController, null, false, null, null);
365 if (state != null) {
366 t.setSavedState(state);
367 // Need to maintain the app id and original url so we
368 // can possibly reuse this tab.
369 t.setAppId(state.getString(Tab.APPID));
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700370 }
Patrick Scott7d50a932011-02-04 09:27:26 -0500371 mTabs.add(t);
372 // added the tab to the front as they are not current
373 mTabQueue.add(0, t);
374 originalTabIndices.put(i, getTabCount() - 1);
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700375 }
Patrick Scott7d50a932011-02-04 09:27:26 -0500376 }
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700377
Patrick Scott7d50a932011-02-04 09:27:26 -0500378 // Rebuild the tree of tabs. Do this after all tabs have been
379 // created/restored so that the parent tab exists.
380 for (int i = 0; i < numTabs; i++) {
381 final Bundle b = inState.getBundle(Tab.WEBVIEW + i);
382 final Tab t = getTab(i);
383 if (b != null && t != null) {
384 final Integer parentIndex = originalTabIndices.get(b.getInt(Tab.PARENTTAB, -1));
385 if (parentIndex != -1) {
386 final Tab parent = getTab(parentIndex);
387 if (parent != null) {
388 parent.addChildTab(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800389 }
390 }
391 }
392 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800393 }
394
395 /**
Grace Klobaf56f68d2010-04-11 22:53:42 -0700396 * Free the memory in this order, 1) free the background tabs; 2) free the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800397 * WebView cache;
398 */
399 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700400 if (getTabCount() == 0) return;
401
Grace Klobaf56f68d2010-04-11 22:53:42 -0700402 // free the least frequently used background tabs
403 Vector<Tab> tabs = getHalfLeastUsedTabs(getCurrentTab());
404 if (tabs.size() > 0) {
405 Log.w(LOGTAG, "Free " + tabs.size() + " tabs in the browser");
406 for (Tab t : tabs) {
407 // store the WebView's state.
408 t.saveState();
409 // destroy the tab
410 t.destroy();
411 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800412 return;
413 }
414
Derek Sollenberger4433d032009-06-10 15:37:21 -0400415 // free the WebView's unused memory (this includes the cache)
416 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800417 WebView view = getCurrentWebView();
418 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400419 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800420 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800421 }
422
Grace Klobaf56f68d2010-04-11 22:53:42 -0700423 private Vector<Tab> getHalfLeastUsedTabs(Tab current) {
424 Vector<Tab> tabsToGo = new Vector<Tab>();
425
Patrick Scott2a67de42009-08-31 09:48:37 -0400426 // Don't do anything if we only have 1 tab or if the current tab is
427 // null.
428 if (getTabCount() == 1 || current == null) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700429 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800430 }
431
Grace Klobaf56f68d2010-04-11 22:53:42 -0700432 if (mTabQueue.size() == 0) {
433 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800434 }
435
Grace Klobaf56f68d2010-04-11 22:53:42 -0700436 // Rip through the queue starting at the beginning and tear down half of
437 // available tabs which are not the current tab or the parent of the
438 // current tab.
439 int openTabCount = 0;
440 for (Tab t : mTabQueue) {
441 if (t != null && t.getWebView() != null) {
442 openTabCount++;
443 if (t != current && t != current.getParentTab()) {
444 tabsToGo.add(t);
445 }
446 }
447 }
448
449 openTabCount /= 2;
450 if (tabsToGo.size() > openTabCount) {
451 tabsToGo.setSize(openTabCount);
452 }
453
454 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800455 }
456
The Android Open Source Project0c908882009-03-03 19:32:16 -0800457 /**
458 * Show the tab that contains the given WebView.
459 * @param view The WebView used to find the tab.
460 */
461 Tab getTabFromView(WebView view) {
462 final int size = getTabCount();
463 for (int i = 0; i < size; i++) {
464 final Tab t = getTab(i);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700465 if (t.getSubWebView() == view || t.getWebView() == view) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800466 return t;
467 }
468 }
469 return null;
470 }
471
472 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700473 * Return the tab with the matching application id.
474 * @param id The application identifier.
475 */
476 Tab getTabFromId(String id) {
477 if (id == null) {
478 return null;
479 }
480 final int size = getTabCount();
481 for (int i = 0; i < size; i++) {
482 final Tab t = getTab(i);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700483 if (id.equals(t.getAppId())) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700484 return t;
485 }
486 }
487 return null;
488 }
489
Grace Kloba22ac16e2009-10-07 18:00:23 -0700490 /**
491 * Stop loading in all opened WebView including subWindows.
492 */
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700493 void stopAllLoading() {
494 final int size = getTabCount();
495 for (int i = 0; i < size; i++) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700496 final Tab t = getTab(i);
497 final WebView webview = t.getWebView();
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700498 if (webview != null) {
499 webview.stopLoading();
500 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700501 final WebView subview = t.getSubWebView();
502 if (subview != null) {
503 webview.stopLoading();
504 }
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700505 }
506 }
507
Patrick Scottcd115892009-07-16 09:42:58 -0400508 // This method checks if a non-app tab (one created within the browser)
509 // matches the given url.
510 private boolean tabMatchesUrl(Tab t, String url) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700511 if (t.getAppId() != null) {
Patrick Scottcd115892009-07-16 09:42:58 -0400512 return false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700513 }
514 WebView webview = t.getWebView();
515 if (webview == null) {
Patrick Scottcd115892009-07-16 09:42:58 -0400516 return false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700517 } else if (url.equals(webview.getUrl())
518 || url.equals(webview.getOriginalUrl())) {
Patrick Scottcd115892009-07-16 09:42:58 -0400519 return true;
520 }
521 return false;
522 }
523
524 /**
525 * Return the tab that has no app id associated with it and the url of the
526 * tab matches the given url.
527 * @param url The url to search for.
528 */
529 Tab findUnusedTabWithUrl(String url) {
530 if (url == null) {
531 return null;
532 }
533 // Check the current tab first.
534 Tab t = getCurrentTab();
535 if (t != null && tabMatchesUrl(t, url)) {
536 return t;
537 }
538 // Now check all the rest.
539 final int size = getTabCount();
540 for (int i = 0; i < size; i++) {
541 t = getTab(i);
542 if (tabMatchesUrl(t, url)) {
543 return t;
544 }
545 }
546 return null;
547 }
548
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700549 /**
John Reck30c714c2010-12-16 17:30:34 -0800550 * Recreate the main WebView of the given tab.
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700551 */
John Reck30c714c2010-12-16 17:30:34 -0800552 void recreateWebView(Tab t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700553 final WebView w = t.getWebView();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700554 if (w != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700555 t.destroy();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700556 }
557 // Create a new WebView. If this tab is the current tab, we need to put
558 // back all the clients so force it to be the current tab.
Steve Block2bc69912009-07-30 14:45:13 +0100559 t.setWebView(createNewWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700560 if (getCurrentTab() == t) {
561 setCurrentTab(t, true);
562 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700563 // Clear the saved state and picker data
564 t.setSavedState(null);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700565 }
566
567 /**
568 * Creates a new WebView and registers it with the global settings.
569 */
570 private WebView createNewWebView() {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700571 return createNewWebView(false);
572 }
573
574 /**
575 * Creates a new WebView and registers it with the global settings.
576 * @param privateBrowsing When true, enables private browsing in the new
577 * WebView.
578 */
579 private WebView createNewWebView(boolean privateBrowsing) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700580 return mController.getWebViewFactory().createWebView(privateBrowsing);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700581 }
582
583 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800584 * Put the current tab in the background and set newTab as the current tab.
585 * @param newTab The new tab. If newTab is null, the current tab is not
586 * set.
587 */
588 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700589 return setCurrentTab(newTab, false);
590 }
591
592 /**
593 * If force is true, this method skips the check for newTab == current.
594 */
595 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800596 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700597 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800598 return true;
599 }
600 if (current != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700601 current.putInBackground();
602 mCurrentTab = -1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800603 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800604 if (newTab == null) {
605 return false;
606 }
607
608 // Move the newTab to the end of the queue
609 int index = mTabQueue.indexOf(newTab);
610 if (index != -1) {
611 mTabQueue.remove(index);
612 }
613 mTabQueue.add(newTab);
614
The Android Open Source Project0c908882009-03-03 19:32:16 -0800615 // Display the new current tab
616 mCurrentTab = mTabs.indexOf(newTab);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700617 WebView mainView = newTab.getWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800618 boolean needRestore = (mainView == null);
619 if (needRestore) {
620 // Same work as in createNewTab() except don't do new Tab()
Steve Block2bc69912009-07-30 14:45:13 +0100621 mainView = createNewWebView();
622 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800623 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700624 newTab.putInForeground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800625 if (needRestore) {
626 // Have to finish setCurrentTab work before calling restoreState
Grace Kloba22ac16e2009-10-07 18:00:23 -0700627 if (!newTab.restoreState(newTab.getSavedState())) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800628 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
629 }
630 }
631 return true;
632 }
Michael Kolbfe251992010-07-08 15:41:55 -0700633
The Android Open Source Project0c908882009-03-03 19:32:16 -0800634}