blob: 5f3995ff333bc50d424007cc47a0e3f91cd7f50d [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";
Michael Kolbc831b632011-05-11 09:30:34 -070032
John Reckd8c74522011-06-14 08:45:00 -070033 // next Tab ID, starting at 1
34 private static long sNextId = 1;
Michael Kolbc831b632011-05-11 09:30:34 -070035
36 private static final String POSITIONS = "positions";
37 private static final String CURRENT = "current";
38
The Android Open Source Project0c908882009-03-03 19:32:16 -080039 // Maximum number of tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070040 private int mMaxTabs;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041 // Private array of WebViews that are used as tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070042 private ArrayList<Tab> mTabs;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043 // Queue of most recently viewed tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070044 private ArrayList<Tab> mTabQueue;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045 // Current position in mTabs.
46 private int mCurrentTab = -1;
Michael Kolb8233fac2010-10-26 16:08:53 -070047 // the main browser controller
48 private final Controller mController;
49
The Android Open Source Project0c908882009-03-03 19:32:16 -080050 private final File mThumbnailDir;
51
52 /**
Michael Kolb8233fac2010-10-26 16:08:53 -070053 * Construct a new TabControl object
The Android Open Source Project0c908882009-03-03 19:32:16 -080054 */
Michael Kolb8233fac2010-10-26 16:08:53 -070055 TabControl(Controller controller) {
56 mController = controller;
57 mThumbnailDir = mController.getActivity()
58 .getDir("thumbnails", 0);
59 mMaxTabs = mController.getMaxTabs();
Michael Kolb6e4653e2010-09-27 16:22:38 -070060 mTabs = new ArrayList<Tab>(mMaxTabs);
61 mTabQueue = new ArrayList<Tab>(mMaxTabs);
The Android Open Source Project0c908882009-03-03 19:32:16 -080062 }
63
Michael Kolbc831b632011-05-11 09:30:34 -070064 static long getNextId() {
65 return sNextId++;
66 }
67
The Android Open Source Project0c908882009-03-03 19:32:16 -080068 File getThumbnailDir() {
69 return mThumbnailDir;
70 }
71
Michael Kolb68775752010-08-19 12:42:01 -070072 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080073 * Return the current tab's main WebView. This will always return the main
74 * WebView for a given tab and not a subwindow.
75 * @return The current tab's WebView.
76 */
77 WebView getCurrentWebView() {
78 Tab t = getTab(mCurrentTab);
79 if (t == null) {
80 return null;
81 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070082 return t.getWebView();
Ben Murdochbff2d602009-07-01 20:19:05 +010083 }
84
85 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080086 * Return the current tab's top-level WebView. This can return a subwindow
87 * if one exists.
88 * @return The top-level WebView of the current tab.
89 */
90 WebView getCurrentTopWebView() {
91 Tab t = getTab(mCurrentTab);
92 if (t == null) {
93 return null;
94 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070095 return t.getTopWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -080096 }
97
98 /**
99 * Return the current tab's subwindow if it exists.
100 * @return The subwindow of the current tab or null if it doesn't exist.
101 */
102 WebView getCurrentSubWindow() {
103 Tab t = getTab(mCurrentTab);
104 if (t == null) {
105 return null;
106 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700107 return t.getSubWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800108 }
109
110 /**
Michael Kolb1bf23132010-11-19 12:55:12 -0800111 * return the list of tabs
112 */
113 List<Tab> getTabs() {
114 return mTabs;
115 }
116
117 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700118 * Return the tab at the specified position.
119 * @return The Tab for the specified position or null if the tab does not
The Android Open Source Project0c908882009-03-03 19:32:16 -0800120 * exist.
121 */
Michael Kolbc831b632011-05-11 09:30:34 -0700122 Tab getTab(int position) {
123 if (position >= 0 && position < mTabs.size()) {
124 return mTabs.get(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800125 }
126 return null;
127 }
128
129 /**
130 * Return the current tab.
131 * @return The current tab.
132 */
133 Tab getCurrentTab() {
134 return getTab(mCurrentTab);
135 }
136
137 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700138 * Return the current tab position.
139 * @return The current tab position
The Android Open Source Project0c908882009-03-03 19:32:16 -0800140 */
Michael Kolbc831b632011-05-11 09:30:34 -0700141 int getCurrentPosition() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800142 return mCurrentTab;
143 }
Michael Kolbfe251992010-07-08 15:41:55 -0700144
The Android Open Source Project0c908882009-03-03 19:32:16 -0800145 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700146 * Given a Tab, find it's position
The Android Open Source Project0c908882009-03-03 19:32:16 -0800147 * @param Tab to find
Michael Kolbc831b632011-05-11 09:30:34 -0700148 * @return position of Tab or -1 if not found
The Android Open Source Project0c908882009-03-03 19:32:16 -0800149 */
Michael Kolbc831b632011-05-11 09:30:34 -0700150 int getTabPosition(Tab tab) {
Patrick Scottae641ac2009-04-20 13:51:49 -0400151 if (tab == null) {
152 return -1;
153 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800154 return mTabs.indexOf(tab);
155 }
156
Grace Kloba22ac16e2009-10-07 18:00:23 -0700157 boolean canCreateNewTab() {
Michael Kolb6e4653e2010-09-27 16:22:38 -0700158 return mMaxTabs != mTabs.size();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700159 }
160
The Android Open Source Project0c908882009-03-03 19:32:16 -0800161 /**
Elliott Slaughtere440a882010-08-20 13:54:45 -0700162 * Returns true if there are any incognito tabs open.
163 * @return True when any incognito tabs are open, false otherwise.
164 */
165 boolean hasAnyOpenIncognitoTabs() {
166 for (Tab tab : mTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700167 if (tab.getWebView() != null
168 && tab.getWebView().isPrivateBrowsingEnabled()) {
Elliott Slaughtere440a882010-08-20 13:54:45 -0700169 return true;
170 }
171 }
172 return false;
173 }
174
Michael Kolb14612442011-06-24 13:06:29 -0700175 void addPreloadedTab(Tab tab) {
176 tab.setId(getNextId());
177 mTabs.add(tab);
178 tab.setController(mController);
179 mController.onSetWebView(tab, tab.getWebView());
180 tab.putInBackground();
181 }
182
Elliott Slaughtere440a882010-08-20 13:54:45 -0700183 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700184 * Create a new tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800185 * @return The newly createTab or null if we have reached the maximum
186 * number of open tabs.
187 */
Michael Kolb7bcafde2011-05-09 13:55:59 -0700188 Tab createNewTab(boolean privateBrowsing) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800189 int size = mTabs.size();
190 // Return false if we have maxed out on tabs
Michael Kolb6e4653e2010-09-27 16:22:38 -0700191 if (mMaxTabs == size) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800192 return null;
193 }
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700194 final WebView w = createNewWebView(privateBrowsing);
Steve Block2bc69912009-07-30 14:45:13 +0100195
The Android Open Source Project0c908882009-03-03 19:32:16 -0800196 // Create a new tab and add it to the tab list
Michael Kolb7bcafde2011-05-09 13:55:59 -0700197 Tab t = new Tab(mController, w);
Michael Kolbc831b632011-05-11 09:30:34 -0700198 t.setId(getNextId());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800199 mTabs.add(t);
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700200 // Initially put the tab in the background.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700201 t.putInBackground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800202 return t;
203 }
204
205 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700206 * Create a new tab with default values for closeOnExit(false),
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700207 * appId(null), url(null), and privateBrowsing(false).
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700208 */
209 Tab createNewTab() {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700210 return createNewTab(false);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700211 }
212
John Reckd8c74522011-06-14 08:45:00 -0700213 SnapshotTab createSnapshotTab(long snapshotId) {
John Reckd8c74522011-06-14 08:45:00 -0700214 SnapshotTab t = new SnapshotTab(mController, snapshotId);
215 t.setId(getNextId());
216 mTabs.add(t);
217 return t;
218 }
219
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700220 /**
Leon Scrogginsfde97462010-01-11 13:06:21 -0500221 * Remove the parent child relationships from all tabs.
222 */
223 void removeParentChildRelationShips() {
224 for (Tab tab : mTabs) {
225 tab.removeFromTree();
226 }
227 }
228
229 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800230 * Remove the tab from the list. If the tab is the current tab shown, the
231 * last created tab will be shown.
232 * @param t The tab to be removed.
233 */
234 boolean removeTab(Tab t) {
235 if (t == null) {
236 return false;
237 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700238
Patrick Scottd944d4d2010-01-27 16:39:11 -0500239 // Grab the current tab before modifying the list.
240 Tab current = getCurrentTab();
241
242 // Remove t from our list of tabs.
243 mTabs.remove(t);
244
245 // Put the tab in the background only if it is the current one.
246 if (current == t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700247 t.putInBackground();
248 mCurrentTab = -1;
Patrick Scottd944d4d2010-01-27 16:39:11 -0500249 } else {
250 // If a tab that is earlier in the list gets removed, the current
251 // index no longer points to the correct tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700252 mCurrentTab = getTabPosition(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800253 }
254
Grace Kloba22ac16e2009-10-07 18:00:23 -0700255 // destroy the tab
256 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800257 // clear it's references to parent and children
258 t.removeFromTree();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800259
The Android Open Source Project0c908882009-03-03 19:32:16 -0800260 // Remove it from the queue of viewed tabs.
261 mTabQueue.remove(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800262 return true;
263 }
264
265 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800266 * Destroy all the tabs and subwindows
267 */
268 void destroy() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800269 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700270 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800271 }
272 mTabs.clear();
273 mTabQueue.clear();
274 }
275
276 /**
277 * Returns the number of tabs created.
278 * @return The number of tabs created.
279 */
280 int getTabCount() {
281 return mTabs.size();
282 }
283
The Android Open Source Project0c908882009-03-03 19:32:16 -0800284 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700285 * save the tab state:
286 * current position
287 * position sorted array of tab ids
288 * for each tab id, save the tab state
289 * @param outState
John Reckaed9c542011-05-27 16:08:53 -0700290 * @param saveImages
The Android Open Source Project0c908882009-03-03 19:32:16 -0800291 */
John Reckaed9c542011-05-27 16:08:53 -0700292 void saveState(Bundle outState, boolean saveImages) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800293 final int numTabs = getTabCount();
John Reck24f18262011-06-17 14:47:20 -0700294 if (numTabs == 0) {
295 return;
296 }
Michael Kolbc831b632011-05-11 09:30:34 -0700297 long[] ids = new long[numTabs];
298 int i = 0;
299 for (Tab tab : mTabs) {
300 ids[i++] = tab.getId();
301 if (tab.saveState()) {
John Reckaed9c542011-05-27 16:08:53 -0700302 outState.putBundle(Long.toString(tab.getId()),
303 tab.getSavedState(saveImages));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800304 }
305 }
John Reck24f18262011-06-17 14:47:20 -0700306 if (!outState.isEmpty()) {
307 outState.putLongArray(POSITIONS, ids);
308 Tab current = getCurrentTab();
309 long cid = -1;
310 if (current != null) {
311 cid = current.getId();
312 }
313 outState.putLong(CURRENT, cid);
Michael Kolbdbf39812011-06-10 14:06:40 -0700314 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800315 }
316
317 /**
Patrick Scott7d50a932011-02-04 09:27:26 -0500318 * Check if the state can be restored. If the state can be restored, the
Michael Kolbc831b632011-05-11 09:30:34 -0700319 * current tab id is returned. This can be passed to restoreState below
Patrick Scott7d50a932011-02-04 09:27:26 -0500320 * in order to restore the correct tab. Otherwise, -1 is returned and the
321 * state cannot be restored.
322 */
Michael Kolbc831b632011-05-11 09:30:34 -0700323 long canRestoreState(Bundle inState, boolean restoreIncognitoTabs) {
324 final long[] ids = (inState == null) ? null : inState.getLongArray(POSITIONS);
325 if (ids == null) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500326 return -1;
327 }
Michael Kolbc831b632011-05-11 09:30:34 -0700328 final long oldcurrent = inState.getLong(CURRENT);
329 long current = -1;
Patrick Scott7d50a932011-02-04 09:27:26 -0500330 if (restoreIncognitoTabs ||
Michael Kolbc831b632011-05-11 09:30:34 -0700331 !inState.getBundle(Long.toString(oldcurrent)).getBoolean(Tab.INCOGNITO)) {
332 current = oldcurrent;
Patrick Scott7d50a932011-02-04 09:27:26 -0500333 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700334 // pick first non incognito tab
335 for (long id : ids) {
336 if (!inState.getBundle(Long.toString(id)).getBoolean(Tab.INCOGNITO)) {
337 current = id;
Patrick Scott7d50a932011-02-04 09:27:26 -0500338 break;
339 }
340 }
341 }
Michael Kolbc831b632011-05-11 09:30:34 -0700342 return current;
Patrick Scott7d50a932011-02-04 09:27:26 -0500343 }
344
345 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800346 * Restore the state of all the tabs.
Michael Kolbc831b632011-05-11 09:30:34 -0700347 * @param currentId The tab id to restore.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800348 * @param inState The saved state of all the tabs.
Michael Kolb1bf23132010-11-19 12:55:12 -0800349 * @param restoreIncognitoTabs Restoring private browsing tabs
350 * @param restoreAll All webviews get restored, not just the current tab
351 * (this does not override handling of incognito tabs)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800352 * @return True if there were previous tabs that were restored. False if
353 * there was no saved state or restoring the state failed.
354 */
Michael Kolbc831b632011-05-11 09:30:34 -0700355 void restoreState(Bundle inState, long currentId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500356 boolean restoreIncognitoTabs, boolean restoreAll) {
Michael Kolbc831b632011-05-11 09:30:34 -0700357 if (currentId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500358 return;
359 }
Michael Kolbc831b632011-05-11 09:30:34 -0700360 long[] ids = inState.getLongArray(POSITIONS);
361 long maxId = -Long.MAX_VALUE;
362 HashMap<Long, Tab> tabMap = new HashMap<Long, Tab>();
363 for (long id : ids) {
364 if (id > maxId) {
365 maxId = id;
366 }
367 final String idkey = Long.toString(id);
368 Bundle state = inState.getBundle(idkey);
John Reckd8c74522011-06-14 08:45:00 -0700369 if (state == null || state.isEmpty()) {
370 // Skip tab
371 continue;
372 } else if (!restoreIncognitoTabs
Michael Kolbc831b632011-05-11 09:30:34 -0700373 && state.getBoolean(Tab.INCOGNITO)) {
374 // ignore tab
375 } else if (id == currentId || restoreAll) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500376 Tab t = createNewTab();
Michael Kolbc831b632011-05-11 09:30:34 -0700377 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500378 // Me must set the current tab before restoring the state
379 // so that all the client classes are set.
Michael Kolbc831b632011-05-11 09:30:34 -0700380 if (id == currentId) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500381 setCurrentTab(t);
382 }
383 if (!t.restoreState(state)) {
384 Log.w(LOGTAG, "Fail in restoreState, load home page.");
385 t.getWebView().loadUrl(BrowserSettings.getInstance()
386 .getHomePage());
387 }
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700388 } else {
Patrick Scott7d50a932011-02-04 09:27:26 -0500389 // Create a new tab and don't restore the state yet, add it
390 // to the tab list
Michael Kolb7bcafde2011-05-09 13:55:59 -0700391 Tab t = new Tab(mController, null);
Michael Kolbc831b632011-05-11 09:30:34 -0700392 t.setId(id);
393 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500394 if (state != null) {
395 t.setSavedState(state);
396 // Need to maintain the app id and original url so we
397 // can possibly reuse this tab.
398 t.setAppId(state.getString(Tab.APPID));
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700399 }
Patrick Scott7d50a932011-02-04 09:27:26 -0500400 mTabs.add(t);
401 // added the tab to the front as they are not current
402 mTabQueue.add(0, t);
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700403 }
Michael Kolbc831b632011-05-11 09:30:34 -0700404 // make sure that there is no id overlap between the restored
405 // and new tabs
406 sNextId = maxId + 1;
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700407
Michael Kolbc831b632011-05-11 09:30:34 -0700408 }
John Reckd8c74522011-06-14 08:45:00 -0700409 if (mCurrentTab == -1) {
410 if (getTabCount() > 0) {
411 setCurrentTab(getTab(0));
412 } else {
413 Tab t = createNewTab();
414 setCurrentTab(t);
415 t.getWebView().loadUrl(BrowserSettings.getInstance()
416 .getHomePage());
417 }
418 }
Michael Kolbc831b632011-05-11 09:30:34 -0700419 // restore parent/child relationships
420 for (long id : ids) {
421 final Tab tab = tabMap.get(id);
422 final Bundle b = inState.getBundle(Long.toString(id));
423 if ((b != null) && (tab != null)) {
424 final long parentId = b.getLong(Tab.PARENTTAB, -1);
425 if (parentId != -1) {
426 final Tab parent = tabMap.get(parentId);
Patrick Scott7d50a932011-02-04 09:27:26 -0500427 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -0700428 parent.addChildTab(tab);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800429 }
430 }
431 }
432 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800433 }
434
435 /**
Grace Klobaf56f68d2010-04-11 22:53:42 -0700436 * Free the memory in this order, 1) free the background tabs; 2) free the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800437 * WebView cache;
438 */
439 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700440 if (getTabCount() == 0) return;
441
Grace Klobaf56f68d2010-04-11 22:53:42 -0700442 // free the least frequently used background tabs
443 Vector<Tab> tabs = getHalfLeastUsedTabs(getCurrentTab());
444 if (tabs.size() > 0) {
445 Log.w(LOGTAG, "Free " + tabs.size() + " tabs in the browser");
446 for (Tab t : tabs) {
447 // store the WebView's state.
448 t.saveState();
449 // destroy the tab
450 t.destroy();
451 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800452 return;
453 }
454
Derek Sollenberger4433d032009-06-10 15:37:21 -0400455 // free the WebView's unused memory (this includes the cache)
456 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800457 WebView view = getCurrentWebView();
458 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400459 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800460 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800461 }
462
Grace Klobaf56f68d2010-04-11 22:53:42 -0700463 private Vector<Tab> getHalfLeastUsedTabs(Tab current) {
464 Vector<Tab> tabsToGo = new Vector<Tab>();
465
Patrick Scott2a67de42009-08-31 09:48:37 -0400466 // Don't do anything if we only have 1 tab or if the current tab is
467 // null.
468 if (getTabCount() == 1 || current == null) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700469 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800470 }
471
Grace Klobaf56f68d2010-04-11 22:53:42 -0700472 if (mTabQueue.size() == 0) {
473 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800474 }
475
Grace Klobaf56f68d2010-04-11 22:53:42 -0700476 // Rip through the queue starting at the beginning and tear down half of
477 // available tabs which are not the current tab or the parent of the
478 // current tab.
479 int openTabCount = 0;
480 for (Tab t : mTabQueue) {
481 if (t != null && t.getWebView() != null) {
482 openTabCount++;
Michael Kolbc831b632011-05-11 09:30:34 -0700483 if (t != current && t != current.getParent()) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700484 tabsToGo.add(t);
485 }
486 }
487 }
488
489 openTabCount /= 2;
490 if (tabsToGo.size() > openTabCount) {
491 tabsToGo.setSize(openTabCount);
492 }
493
494 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800495 }
496
The Android Open Source Project0c908882009-03-03 19:32:16 -0800497 /**
498 * Show the tab that contains the given WebView.
499 * @param view The WebView used to find the tab.
500 */
501 Tab getTabFromView(WebView view) {
Michael Kolbc831b632011-05-11 09:30:34 -0700502 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700503 if (t.getSubWebView() == view || t.getWebView() == view) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800504 return t;
505 }
506 }
507 return null;
508 }
509
510 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700511 * Return the tab with the matching application id.
512 * @param id The application identifier.
513 */
Michael Kolbc831b632011-05-11 09:30:34 -0700514 Tab getTabFromAppId(String id) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700515 if (id == null) {
516 return null;
517 }
Michael Kolbc831b632011-05-11 09:30:34 -0700518 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700519 if (id.equals(t.getAppId())) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700520 return t;
521 }
522 }
523 return null;
524 }
525
Grace Kloba22ac16e2009-10-07 18:00:23 -0700526 /**
527 * Stop loading in all opened WebView including subWindows.
528 */
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700529 void stopAllLoading() {
Michael Kolbc831b632011-05-11 09:30:34 -0700530 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700531 final WebView webview = t.getWebView();
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700532 if (webview != null) {
533 webview.stopLoading();
534 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700535 final WebView subview = t.getSubWebView();
536 if (subview != null) {
537 webview.stopLoading();
538 }
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700539 }
540 }
541
John Reckdb22ec42011-06-29 11:31:24 -0700542 // This method checks if a tab matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400543 private boolean tabMatchesUrl(Tab t, String url) {
John Reckdb22ec42011-06-29 11:31:24 -0700544 return url.equals(t.getUrl()) || url.equals(t.getOriginalUrl());
Patrick Scottcd115892009-07-16 09:42:58 -0400545 }
546
547 /**
John Reckdb22ec42011-06-29 11:31:24 -0700548 * Return the tab that matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400549 * @param url The url to search for.
550 */
John Reckdb22ec42011-06-29 11:31:24 -0700551 Tab findTabWithUrl(String url) {
Patrick Scottcd115892009-07-16 09:42:58 -0400552 if (url == null) {
553 return null;
554 }
555 // Check the current tab first.
John Reckdb22ec42011-06-29 11:31:24 -0700556 Tab currentTab = getCurrentTab();
557 if (currentTab != null && tabMatchesUrl(currentTab, url)) {
558 return currentTab;
Patrick Scottcd115892009-07-16 09:42:58 -0400559 }
560 // Now check all the rest.
Michael Kolbc831b632011-05-11 09:30:34 -0700561 for (Tab tab : mTabs) {
562 if (tabMatchesUrl(tab, url)) {
John Reckdb22ec42011-06-29 11:31:24 -0700563 return tab;
Patrick Scottcd115892009-07-16 09:42:58 -0400564 }
565 }
566 return null;
567 }
568
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700569 /**
John Reck30c714c2010-12-16 17:30:34 -0800570 * Recreate the main WebView of the given tab.
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700571 */
John Reck30c714c2010-12-16 17:30:34 -0800572 void recreateWebView(Tab t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700573 final WebView w = t.getWebView();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700574 if (w != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700575 t.destroy();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700576 }
577 // Create a new WebView. If this tab is the current tab, we need to put
578 // back all the clients so force it to be the current tab.
Steve Block2bc69912009-07-30 14:45:13 +0100579 t.setWebView(createNewWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700580 if (getCurrentTab() == t) {
581 setCurrentTab(t, true);
582 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700583 // Clear the saved state and picker data
584 t.setSavedState(null);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700585 }
586
587 /**
588 * Creates a new WebView and registers it with the global settings.
589 */
590 private WebView createNewWebView() {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700591 return createNewWebView(false);
592 }
593
594 /**
595 * Creates a new WebView and registers it with the global settings.
596 * @param privateBrowsing When true, enables private browsing in the new
597 * WebView.
598 */
599 private WebView createNewWebView(boolean privateBrowsing) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700600 return mController.getWebViewFactory().createWebView(privateBrowsing);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700601 }
602
603 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800604 * Put the current tab in the background and set newTab as the current tab.
605 * @param newTab The new tab. If newTab is null, the current tab is not
606 * set.
607 */
608 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700609 return setCurrentTab(newTab, false);
610 }
611
612 /**
613 * If force is true, this method skips the check for newTab == current.
614 */
615 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800616 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700617 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800618 return true;
619 }
620 if (current != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700621 current.putInBackground();
622 mCurrentTab = -1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800623 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800624 if (newTab == null) {
625 return false;
626 }
627
628 // Move the newTab to the end of the queue
629 int index = mTabQueue.indexOf(newTab);
630 if (index != -1) {
631 mTabQueue.remove(index);
632 }
633 mTabQueue.add(newTab);
634
The Android Open Source Project0c908882009-03-03 19:32:16 -0800635 // Display the new current tab
636 mCurrentTab = mTabs.indexOf(newTab);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700637 WebView mainView = newTab.getWebView();
John Reckd8c74522011-06-14 08:45:00 -0700638 boolean needRestore = !newTab.isSnapshot() && (mainView == null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800639 if (needRestore) {
640 // Same work as in createNewTab() except don't do new Tab()
Steve Block2bc69912009-07-30 14:45:13 +0100641 mainView = createNewWebView();
642 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800643 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700644 newTab.putInForeground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800645 if (needRestore) {
646 // Have to finish setCurrentTab work before calling restoreState
Grace Kloba22ac16e2009-10-07 18:00:23 -0700647 if (!newTab.restoreState(newTab.getSavedState())) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800648 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
649 }
650 }
651 return true;
652 }
Michael Kolbfe251992010-07-08 15:41:55 -0700653
The Android Open Source Project0c908882009-03-03 19:32:16 -0800654}