blob: cd8da2ed115838a41ebfad8817ade4cd01ca918f [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() {
Narayan Kamathc3ad9ea2011-07-26 15:20:21 +0100158 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) {
Narayan Kamathc3ad9ea2011-07-26 15:20:21 +0100189 if (!canCreateNewTab()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 return null;
191 }
Narayan Kamathc3ad9ea2011-07-26 15:20:21 +0100192
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700193 final WebView w = createNewWebView(privateBrowsing);
Steve Block2bc69912009-07-30 14:45:13 +0100194
The Android Open Source Project0c908882009-03-03 19:32:16 -0800195 // Create a new tab and add it to the tab list
Michael Kolb7bcafde2011-05-09 13:55:59 -0700196 Tab t = new Tab(mController, w);
Michael Kolbc831b632011-05-11 09:30:34 -0700197 t.setId(getNextId());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800198 mTabs.add(t);
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700199 // Initially put the tab in the background.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700200 t.putInBackground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800201 return t;
202 }
203
204 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700205 * Create a new tab with default values for closeOnExit(false),
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700206 * appId(null), url(null), and privateBrowsing(false).
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700207 */
208 Tab createNewTab() {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700209 return createNewTab(false);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700210 }
211
John Reckd8c74522011-06-14 08:45:00 -0700212 SnapshotTab createSnapshotTab(long snapshotId) {
John Reckd8c74522011-06-14 08:45:00 -0700213 SnapshotTab t = new SnapshotTab(mController, snapshotId);
214 t.setId(getNextId());
215 mTabs.add(t);
216 return t;
217 }
218
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700219 /**
Leon Scrogginsfde97462010-01-11 13:06:21 -0500220 * Remove the parent child relationships from all tabs.
221 */
222 void removeParentChildRelationShips() {
223 for (Tab tab : mTabs) {
224 tab.removeFromTree();
225 }
226 }
227
228 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800229 * Remove the tab from the list. If the tab is the current tab shown, the
230 * last created tab will be shown.
231 * @param t The tab to be removed.
232 */
233 boolean removeTab(Tab t) {
234 if (t == null) {
235 return false;
236 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700237
Patrick Scottd944d4d2010-01-27 16:39:11 -0500238 // Grab the current tab before modifying the list.
239 Tab current = getCurrentTab();
240
241 // Remove t from our list of tabs.
242 mTabs.remove(t);
243
244 // Put the tab in the background only if it is the current one.
245 if (current == t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700246 t.putInBackground();
247 mCurrentTab = -1;
Patrick Scottd944d4d2010-01-27 16:39:11 -0500248 } else {
249 // If a tab that is earlier in the list gets removed, the current
250 // index no longer points to the correct tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700251 mCurrentTab = getTabPosition(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800252 }
253
Grace Kloba22ac16e2009-10-07 18:00:23 -0700254 // destroy the tab
255 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800256 // clear it's references to parent and children
257 t.removeFromTree();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800258
The Android Open Source Project0c908882009-03-03 19:32:16 -0800259 // Remove it from the queue of viewed tabs.
260 mTabQueue.remove(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800261 return true;
262 }
263
264 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800265 * Destroy all the tabs and subwindows
266 */
267 void destroy() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800268 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700269 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800270 }
271 mTabs.clear();
272 mTabQueue.clear();
273 }
274
275 /**
276 * Returns the number of tabs created.
277 * @return The number of tabs created.
278 */
279 int getTabCount() {
280 return mTabs.size();
281 }
282
The Android Open Source Project0c908882009-03-03 19:32:16 -0800283 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700284 * save the tab state:
285 * current position
286 * position sorted array of tab ids
287 * for each tab id, save the tab state
288 * @param outState
John Reckaed9c542011-05-27 16:08:53 -0700289 * @param saveImages
The Android Open Source Project0c908882009-03-03 19:32:16 -0800290 */
John Reckaed9c542011-05-27 16:08:53 -0700291 void saveState(Bundle outState, boolean saveImages) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800292 final int numTabs = getTabCount();
John Reck24f18262011-06-17 14:47:20 -0700293 if (numTabs == 0) {
294 return;
295 }
Michael Kolbc831b632011-05-11 09:30:34 -0700296 long[] ids = new long[numTabs];
297 int i = 0;
298 for (Tab tab : mTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700299 if (tab.saveState()) {
Michael Kolb3ae7f742011-07-13 15:18:25 -0700300 ids[i++] = tab.getId();
John Reckaed9c542011-05-27 16:08:53 -0700301 outState.putBundle(Long.toString(tab.getId()),
302 tab.getSavedState(saveImages));
Michael Kolb3ae7f742011-07-13 15:18:25 -0700303 } else {
304 ids[i++] = -1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800305 }
306 }
John Reck24f18262011-06-17 14:47:20 -0700307 if (!outState.isEmpty()) {
308 outState.putLongArray(POSITIONS, ids);
309 Tab current = getCurrentTab();
310 long cid = -1;
311 if (current != null) {
312 cid = current.getId();
313 }
314 outState.putLong(CURRENT, cid);
Michael Kolbdbf39812011-06-10 14:06:40 -0700315 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800316 }
317
318 /**
Patrick Scott7d50a932011-02-04 09:27:26 -0500319 * Check if the state can be restored. If the state can be restored, the
Michael Kolbc831b632011-05-11 09:30:34 -0700320 * current tab id is returned. This can be passed to restoreState below
Patrick Scott7d50a932011-02-04 09:27:26 -0500321 * in order to restore the correct tab. Otherwise, -1 is returned and the
322 * state cannot be restored.
323 */
Michael Kolbc831b632011-05-11 09:30:34 -0700324 long canRestoreState(Bundle inState, boolean restoreIncognitoTabs) {
325 final long[] ids = (inState == null) ? null : inState.getLongArray(POSITIONS);
326 if (ids == null) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500327 return -1;
328 }
Michael Kolbc831b632011-05-11 09:30:34 -0700329 final long oldcurrent = inState.getLong(CURRENT);
330 long current = -1;
Michael Kolb3ae7f742011-07-13 15:18:25 -0700331 if (restoreIncognitoTabs || (hasState(oldcurrent, inState) && !isIncognito(oldcurrent, inState))) {
Michael Kolbc831b632011-05-11 09:30:34 -0700332 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) {
Michael Kolb3ae7f742011-07-13 15:18:25 -0700336 if (hasState(id, inState) && !isIncognito(id, inState)) {
Michael Kolbc831b632011-05-11 09:30:34 -0700337 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
Michael Kolb3ae7f742011-07-13 15:18:25 -0700345 private boolean hasState(long id, Bundle state) {
346 if (id == -1) return false;
347 Bundle tab = state.getBundle(Long.toString(id));
348 return ((tab != null) && !tab.isEmpty());
349 }
350
351 private boolean isIncognito(long id, Bundle state) {
352 Bundle tabstate = state.getBundle(Long.toString(id));
353 if ((tabstate != null) && !tabstate.isEmpty()) {
354 return tabstate.getBoolean(Tab.INCOGNITO);
355 }
356 return false;
357 }
358
Patrick Scott7d50a932011-02-04 09:27:26 -0500359 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800360 * Restore the state of all the tabs.
Michael Kolbc831b632011-05-11 09:30:34 -0700361 * @param currentId The tab id to restore.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800362 * @param inState The saved state of all the tabs.
Michael Kolb1bf23132010-11-19 12:55:12 -0800363 * @param restoreIncognitoTabs Restoring private browsing tabs
364 * @param restoreAll All webviews get restored, not just the current tab
365 * (this does not override handling of incognito tabs)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800366 * @return True if there were previous tabs that were restored. False if
367 * there was no saved state or restoring the state failed.
368 */
Michael Kolbc831b632011-05-11 09:30:34 -0700369 void restoreState(Bundle inState, long currentId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500370 boolean restoreIncognitoTabs, boolean restoreAll) {
Michael Kolbc831b632011-05-11 09:30:34 -0700371 if (currentId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500372 return;
373 }
Michael Kolbc831b632011-05-11 09:30:34 -0700374 long[] ids = inState.getLongArray(POSITIONS);
375 long maxId = -Long.MAX_VALUE;
376 HashMap<Long, Tab> tabMap = new HashMap<Long, Tab>();
377 for (long id : ids) {
378 if (id > maxId) {
379 maxId = id;
380 }
381 final String idkey = Long.toString(id);
382 Bundle state = inState.getBundle(idkey);
John Reckd8c74522011-06-14 08:45:00 -0700383 if (state == null || state.isEmpty()) {
384 // Skip tab
385 continue;
386 } else if (!restoreIncognitoTabs
Michael Kolbc831b632011-05-11 09:30:34 -0700387 && state.getBoolean(Tab.INCOGNITO)) {
388 // ignore tab
389 } else if (id == currentId || restoreAll) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500390 Tab t = createNewTab();
Narayan Kamath79e5d8d2011-07-20 14:12:38 +0100391 if (t == null) {
392 // We could "break" at this point, but we want
393 // sNextId to be set correctly.
394 continue;
395 }
Michael Kolbc831b632011-05-11 09:30:34 -0700396 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500397 // Me must set the current tab before restoring the state
398 // so that all the client classes are set.
Michael Kolbc831b632011-05-11 09:30:34 -0700399 if (id == currentId) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500400 setCurrentTab(t);
401 }
402 if (!t.restoreState(state)) {
403 Log.w(LOGTAG, "Fail in restoreState, load home page.");
404 t.getWebView().loadUrl(BrowserSettings.getInstance()
405 .getHomePage());
406 }
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700407 } else {
Patrick Scott7d50a932011-02-04 09:27:26 -0500408 // Create a new tab and don't restore the state yet, add it
409 // to the tab list
Michael Kolb7bcafde2011-05-09 13:55:59 -0700410 Tab t = new Tab(mController, null);
Michael Kolbc831b632011-05-11 09:30:34 -0700411 t.setId(id);
412 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500413 if (state != null) {
414 t.setSavedState(state);
415 // Need to maintain the app id and original url so we
416 // can possibly reuse this tab.
417 t.setAppId(state.getString(Tab.APPID));
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700418 }
Patrick Scott7d50a932011-02-04 09:27:26 -0500419 mTabs.add(t);
420 // added the tab to the front as they are not current
421 mTabQueue.add(0, t);
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700422 }
Michael Kolbc831b632011-05-11 09:30:34 -0700423 }
Narayan Kamath79e5d8d2011-07-20 14:12:38 +0100424
425 // make sure that there is no id overlap between the restored
426 // and new tabs
427 sNextId = maxId + 1;
428
John Reckd8c74522011-06-14 08:45:00 -0700429 if (mCurrentTab == -1) {
430 if (getTabCount() > 0) {
431 setCurrentTab(getTab(0));
432 } else {
433 Tab t = createNewTab();
434 setCurrentTab(t);
435 t.getWebView().loadUrl(BrowserSettings.getInstance()
436 .getHomePage());
437 }
438 }
Michael Kolbc831b632011-05-11 09:30:34 -0700439 // restore parent/child relationships
440 for (long id : ids) {
441 final Tab tab = tabMap.get(id);
442 final Bundle b = inState.getBundle(Long.toString(id));
443 if ((b != null) && (tab != null)) {
444 final long parentId = b.getLong(Tab.PARENTTAB, -1);
445 if (parentId != -1) {
446 final Tab parent = tabMap.get(parentId);
Patrick Scott7d50a932011-02-04 09:27:26 -0500447 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -0700448 parent.addChildTab(tab);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800449 }
450 }
451 }
452 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800453 }
454
455 /**
Grace Klobaf56f68d2010-04-11 22:53:42 -0700456 * Free the memory in this order, 1) free the background tabs; 2) free the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800457 * WebView cache;
458 */
459 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700460 if (getTabCount() == 0) return;
461
Grace Klobaf56f68d2010-04-11 22:53:42 -0700462 // free the least frequently used background tabs
463 Vector<Tab> tabs = getHalfLeastUsedTabs(getCurrentTab());
464 if (tabs.size() > 0) {
465 Log.w(LOGTAG, "Free " + tabs.size() + " tabs in the browser");
466 for (Tab t : tabs) {
467 // store the WebView's state.
468 t.saveState();
469 // destroy the tab
470 t.destroy();
471 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800472 return;
473 }
474
Derek Sollenberger4433d032009-06-10 15:37:21 -0400475 // free the WebView's unused memory (this includes the cache)
476 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800477 WebView view = getCurrentWebView();
478 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400479 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800480 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800481 }
482
Grace Klobaf56f68d2010-04-11 22:53:42 -0700483 private Vector<Tab> getHalfLeastUsedTabs(Tab current) {
484 Vector<Tab> tabsToGo = new Vector<Tab>();
485
Patrick Scott2a67de42009-08-31 09:48:37 -0400486 // Don't do anything if we only have 1 tab or if the current tab is
487 // null.
488 if (getTabCount() == 1 || current == null) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700489 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800490 }
491
Grace Klobaf56f68d2010-04-11 22:53:42 -0700492 if (mTabQueue.size() == 0) {
493 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800494 }
495
Grace Klobaf56f68d2010-04-11 22:53:42 -0700496 // Rip through the queue starting at the beginning and tear down half of
497 // available tabs which are not the current tab or the parent of the
498 // current tab.
499 int openTabCount = 0;
500 for (Tab t : mTabQueue) {
501 if (t != null && t.getWebView() != null) {
502 openTabCount++;
Michael Kolbc831b632011-05-11 09:30:34 -0700503 if (t != current && t != current.getParent()) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700504 tabsToGo.add(t);
505 }
506 }
507 }
508
509 openTabCount /= 2;
510 if (tabsToGo.size() > openTabCount) {
511 tabsToGo.setSize(openTabCount);
512 }
513
514 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800515 }
516
Michael Kolbff6a7482011-07-26 16:37:15 -0700517 Tab getLeastUsedTab(Tab current) {
518 if (getTabCount() == 1 || current == null) {
519 return null;
520 }
521 if (mTabQueue.size() == 0) {
522 return null;
523 }
524 // find a tab which is not the current tab or the parent of the
525 // current tab
526 for (Tab t : mTabQueue) {
527 if (t != null && t.getWebView() != null) {
528 if (t != current && t != current.getParent()) {
529 return t;
530 }
531 }
532 }
533 return null;
534 }
535
The Android Open Source Project0c908882009-03-03 19:32:16 -0800536 /**
537 * Show the tab that contains the given WebView.
538 * @param view The WebView used to find the tab.
539 */
540 Tab getTabFromView(WebView view) {
Michael Kolbc831b632011-05-11 09:30:34 -0700541 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700542 if (t.getSubWebView() == view || t.getWebView() == view) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800543 return t;
544 }
545 }
546 return null;
547 }
548
549 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700550 * Return the tab with the matching application id.
551 * @param id The application identifier.
552 */
Michael Kolbc831b632011-05-11 09:30:34 -0700553 Tab getTabFromAppId(String id) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700554 if (id == null) {
555 return null;
556 }
Michael Kolbc831b632011-05-11 09:30:34 -0700557 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700558 if (id.equals(t.getAppId())) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700559 return t;
560 }
561 }
562 return null;
563 }
564
Grace Kloba22ac16e2009-10-07 18:00:23 -0700565 /**
566 * Stop loading in all opened WebView including subWindows.
567 */
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700568 void stopAllLoading() {
Michael Kolbc831b632011-05-11 09:30:34 -0700569 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700570 final WebView webview = t.getWebView();
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700571 if (webview != null) {
572 webview.stopLoading();
573 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700574 final WebView subview = t.getSubWebView();
575 if (subview != null) {
576 webview.stopLoading();
577 }
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700578 }
579 }
580
John Reckdb22ec42011-06-29 11:31:24 -0700581 // This method checks if a tab matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400582 private boolean tabMatchesUrl(Tab t, String url) {
John Reckdb22ec42011-06-29 11:31:24 -0700583 return url.equals(t.getUrl()) || url.equals(t.getOriginalUrl());
Patrick Scottcd115892009-07-16 09:42:58 -0400584 }
585
586 /**
John Reckdb22ec42011-06-29 11:31:24 -0700587 * Return the tab that matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400588 * @param url The url to search for.
589 */
John Reckdb22ec42011-06-29 11:31:24 -0700590 Tab findTabWithUrl(String url) {
Patrick Scottcd115892009-07-16 09:42:58 -0400591 if (url == null) {
592 return null;
593 }
594 // Check the current tab first.
John Reckdb22ec42011-06-29 11:31:24 -0700595 Tab currentTab = getCurrentTab();
596 if (currentTab != null && tabMatchesUrl(currentTab, url)) {
597 return currentTab;
Patrick Scottcd115892009-07-16 09:42:58 -0400598 }
599 // Now check all the rest.
Michael Kolbc831b632011-05-11 09:30:34 -0700600 for (Tab tab : mTabs) {
601 if (tabMatchesUrl(tab, url)) {
John Reckdb22ec42011-06-29 11:31:24 -0700602 return tab;
Patrick Scottcd115892009-07-16 09:42:58 -0400603 }
604 }
605 return null;
606 }
607
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700608 /**
John Reck30c714c2010-12-16 17:30:34 -0800609 * Recreate the main WebView of the given tab.
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700610 */
John Reck30c714c2010-12-16 17:30:34 -0800611 void recreateWebView(Tab t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700612 final WebView w = t.getWebView();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700613 if (w != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700614 t.destroy();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700615 }
616 // Create a new WebView. If this tab is the current tab, we need to put
617 // back all the clients so force it to be the current tab.
Steve Block2bc69912009-07-30 14:45:13 +0100618 t.setWebView(createNewWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700619 if (getCurrentTab() == t) {
620 setCurrentTab(t, true);
621 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700622 // Clear the saved state and picker data
623 t.setSavedState(null);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700624 }
625
626 /**
627 * Creates a new WebView and registers it with the global settings.
628 */
629 private WebView createNewWebView() {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700630 return createNewWebView(false);
631 }
632
633 /**
634 * Creates a new WebView and registers it with the global settings.
635 * @param privateBrowsing When true, enables private browsing in the new
636 * WebView.
637 */
638 private WebView createNewWebView(boolean privateBrowsing) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 return mController.getWebViewFactory().createWebView(privateBrowsing);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700640 }
641
642 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800643 * Put the current tab in the background and set newTab as the current tab.
644 * @param newTab The new tab. If newTab is null, the current tab is not
645 * set.
646 */
647 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700648 return setCurrentTab(newTab, false);
649 }
650
651 /**
652 * If force is true, this method skips the check for newTab == current.
653 */
654 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800655 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700656 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800657 return true;
658 }
659 if (current != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700660 current.putInBackground();
661 mCurrentTab = -1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800662 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800663 if (newTab == null) {
664 return false;
665 }
666
667 // Move the newTab to the end of the queue
668 int index = mTabQueue.indexOf(newTab);
669 if (index != -1) {
670 mTabQueue.remove(index);
671 }
672 mTabQueue.add(newTab);
673
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674 // Display the new current tab
675 mCurrentTab = mTabs.indexOf(newTab);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700676 WebView mainView = newTab.getWebView();
John Reckd8c74522011-06-14 08:45:00 -0700677 boolean needRestore = !newTab.isSnapshot() && (mainView == null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800678 if (needRestore) {
679 // Same work as in createNewTab() except don't do new Tab()
Steve Block2bc69912009-07-30 14:45:13 +0100680 mainView = createNewWebView();
681 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800682 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700683 newTab.putInForeground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800684 if (needRestore) {
685 // Have to finish setCurrentTab work before calling restoreState
Grace Kloba22ac16e2009-10-07 18:00:23 -0700686 if (!newTab.restoreState(newTab.getSavedState())) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800687 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
688 }
689 }
690 return true;
691 }
Michael Kolbfe251992010-07-08 15:41:55 -0700692
The Android Open Source Project0c908882009-03-03 19:32:16 -0800693}