blob: 932a811695de4172e7c34d6be1ee8f5106819ae3 [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
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import java.util.ArrayList;
Elliott Slaughter3d6df162010-08-25 13:17:44 -070024import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080025import java.util.List;
The Android Open Source Project0c908882009-03-03 19:32:16 -080026import java.util.Vector;
27
28class TabControl {
29 // Log Tag
30 private static final String LOGTAG = "TabControl";
Michael Kolbc831b632011-05-11 09:30:34 -070031
John Reckd8c74522011-06-14 08:45:00 -070032 // next Tab ID, starting at 1
33 private static long sNextId = 1;
Michael Kolbc831b632011-05-11 09:30:34 -070034
35 private static final String POSITIONS = "positions";
36 private static final String CURRENT = "current";
37
John Reck8ee633f2011-08-09 16:00:35 -070038 public static interface OnThumbnailUpdatedListener {
39 void onThumbnailUpdated(Tab t);
40 }
41
The Android Open Source Project0c908882009-03-03 19:32:16 -080042 // Maximum number of tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070043 private int mMaxTabs;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044 // Private array of WebViews that are used as tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070045 private ArrayList<Tab> mTabs;
The Android Open Source Project0c908882009-03-03 19:32:16 -080046 // Queue of most recently viewed tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070047 private ArrayList<Tab> mTabQueue;
The Android Open Source Project0c908882009-03-03 19:32:16 -080048 // Current position in mTabs.
49 private int mCurrentTab = -1;
Michael Kolb8233fac2010-10-26 16:08:53 -070050 // the main browser controller
51 private final Controller mController;
52
John Reck8ee633f2011-08-09 16:00:35 -070053 private OnThumbnailUpdatedListener mOnThumbnailUpdatedListener;
The Android Open Source Project0c908882009-03-03 19:32:16 -080054
55 /**
Michael Kolb8233fac2010-10-26 16:08:53 -070056 * Construct a new TabControl object
The Android Open Source Project0c908882009-03-03 19:32:16 -080057 */
Michael Kolb8233fac2010-10-26 16:08:53 -070058 TabControl(Controller controller) {
59 mController = controller;
Michael Kolb8233fac2010-10-26 16:08:53 -070060 mMaxTabs = mController.getMaxTabs();
Michael Kolb6e4653e2010-09-27 16:22:38 -070061 mTabs = new ArrayList<Tab>(mMaxTabs);
62 mTabQueue = new ArrayList<Tab>(mMaxTabs);
The Android Open Source Project0c908882009-03-03 19:32:16 -080063 }
64
John Reck52be4782011-08-26 15:37:29 -070065 synchronized static long getNextId() {
Michael Kolbc831b632011-05-11 09:30:34 -070066 return sNextId++;
67 }
68
Michael Kolb68775752010-08-19 12:42:01 -070069 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080070 * Return the current tab's main WebView. This will always return the main
71 * WebView for a given tab and not a subwindow.
72 * @return The current tab's WebView.
73 */
74 WebView getCurrentWebView() {
75 Tab t = getTab(mCurrentTab);
76 if (t == null) {
77 return null;
78 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070079 return t.getWebView();
Ben Murdochbff2d602009-07-01 20:19:05 +010080 }
81
82 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080083 * Return the current tab's top-level WebView. This can return a subwindow
84 * if one exists.
85 * @return The top-level WebView of the current tab.
86 */
87 WebView getCurrentTopWebView() {
88 Tab t = getTab(mCurrentTab);
89 if (t == null) {
90 return null;
91 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070092 return t.getTopWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -080093 }
94
95 /**
96 * Return the current tab's subwindow if it exists.
97 * @return The subwindow of the current tab or null if it doesn't exist.
98 */
99 WebView getCurrentSubWindow() {
100 Tab t = getTab(mCurrentTab);
101 if (t == null) {
102 return null;
103 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700104 return t.getSubWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800105 }
106
107 /**
Michael Kolb1bf23132010-11-19 12:55:12 -0800108 * return the list of tabs
109 */
110 List<Tab> getTabs() {
111 return mTabs;
112 }
113
114 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700115 * Return the tab at the specified position.
116 * @return The Tab for the specified position or null if the tab does not
The Android Open Source Project0c908882009-03-03 19:32:16 -0800117 * exist.
118 */
Michael Kolbc831b632011-05-11 09:30:34 -0700119 Tab getTab(int position) {
120 if (position >= 0 && position < mTabs.size()) {
121 return mTabs.get(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800122 }
123 return null;
124 }
125
126 /**
127 * Return the current tab.
128 * @return The current tab.
129 */
130 Tab getCurrentTab() {
131 return getTab(mCurrentTab);
132 }
133
134 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700135 * Return the current tab position.
136 * @return The current tab position
The Android Open Source Project0c908882009-03-03 19:32:16 -0800137 */
Michael Kolbc831b632011-05-11 09:30:34 -0700138 int getCurrentPosition() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800139 return mCurrentTab;
140 }
Michael Kolbfe251992010-07-08 15:41:55 -0700141
The Android Open Source Project0c908882009-03-03 19:32:16 -0800142 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700143 * Given a Tab, find it's position
The Android Open Source Project0c908882009-03-03 19:32:16 -0800144 * @param Tab to find
Michael Kolbc831b632011-05-11 09:30:34 -0700145 * @return position of Tab or -1 if not found
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 */
Michael Kolbc831b632011-05-11 09:30:34 -0700147 int getTabPosition(Tab tab) {
Patrick Scottae641ac2009-04-20 13:51:49 -0400148 if (tab == null) {
149 return -1;
150 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800151 return mTabs.indexOf(tab);
152 }
153
Grace Kloba22ac16e2009-10-07 18:00:23 -0700154 boolean canCreateNewTab() {
Narayan Kamathc3ad9ea2011-07-26 15:20:21 +0100155 return mMaxTabs > mTabs.size();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700156 }
157
The Android Open Source Project0c908882009-03-03 19:32:16 -0800158 /**
Elliott Slaughtere440a882010-08-20 13:54:45 -0700159 * Returns true if there are any incognito tabs open.
160 * @return True when any incognito tabs are open, false otherwise.
161 */
162 boolean hasAnyOpenIncognitoTabs() {
163 for (Tab tab : mTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700164 if (tab.getWebView() != null
165 && tab.getWebView().isPrivateBrowsingEnabled()) {
Elliott Slaughtere440a882010-08-20 13:54:45 -0700166 return true;
167 }
168 }
169 return false;
170 }
171
Michael Kolb14612442011-06-24 13:06:29 -0700172 void addPreloadedTab(Tab tab) {
Michael Kolb14612442011-06-24 13:06:29 -0700173 mTabs.add(tab);
174 tab.setController(mController);
175 mController.onSetWebView(tab, tab.getWebView());
176 tab.putInBackground();
177 }
178
Elliott Slaughtere440a882010-08-20 13:54:45 -0700179 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700180 * Create a new tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800181 * @return The newly createTab or null if we have reached the maximum
182 * number of open tabs.
183 */
Michael Kolb7bcafde2011-05-09 13:55:59 -0700184 Tab createNewTab(boolean privateBrowsing) {
John Reck1cf4b792011-07-26 10:22:22 -0700185 return createNewTab(null, privateBrowsing);
186 }
187
188 Tab createNewTab(Bundle state, boolean privateBrowsing) {
189 int size = mTabs.size();
190 // Return false if we have maxed out on tabs
Narayan Kamathc3ad9ea2011-07-26 15:20:21 +0100191 if (!canCreateNewTab()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800192 return null;
193 }
Narayan Kamathc3ad9ea2011-07-26 15:20:21 +0100194
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700195 final WebView w = createNewWebView(privateBrowsing);
Steve Block2bc69912009-07-30 14:45:13 +0100196
The Android Open Source Project0c908882009-03-03 19:32:16 -0800197 // Create a new tab and add it to the tab list
John Reck1cf4b792011-07-26 10:22:22 -0700198 Tab t = new Tab(mController, w, state);
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);
John Reckd8c74522011-06-14 08:45:00 -0700215 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 Reck1cf4b792011-07-26 10:22:22 -0700291 void saveState(Bundle outState) {
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) {
John Reck1cf4b792011-07-26 10:22:22 -0700299 Bundle tabState = tab.saveState();
300 if (tabState != null) {
Michael Kolb3ae7f742011-07-13 15:18:25 -0700301 ids[i++] = tab.getId();
John Reck52be4782011-08-26 15:37:29 -0700302 String key = Long.toString(tab.getId());
303 if (outState.containsKey(key)) {
304 // Dump the tab state for debugging purposes
305 for (Tab dt : mTabs) {
306 Log.e(LOGTAG, dt.toString());
307 }
308 throw new IllegalStateException(
309 "Error saving state, duplicate tab ids!");
310 }
311 outState.putBundle(key, tabState);
Michael Kolb3ae7f742011-07-13 15:18:25 -0700312 } else {
313 ids[i++] = -1;
John Reck52be4782011-08-26 15:37:29 -0700314 // Since we won't be restoring the thumbnail, delete it
315 tab.deleteThumbnail();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800316 }
317 }
John Reck24f18262011-06-17 14:47:20 -0700318 if (!outState.isEmpty()) {
319 outState.putLongArray(POSITIONS, ids);
320 Tab current = getCurrentTab();
321 long cid = -1;
322 if (current != null) {
323 cid = current.getId();
324 }
325 outState.putLong(CURRENT, cid);
Michael Kolbdbf39812011-06-10 14:06:40 -0700326 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800327 }
328
329 /**
Patrick Scott7d50a932011-02-04 09:27:26 -0500330 * Check if the state can be restored. If the state can be restored, the
Michael Kolbc831b632011-05-11 09:30:34 -0700331 * current tab id is returned. This can be passed to restoreState below
Patrick Scott7d50a932011-02-04 09:27:26 -0500332 * in order to restore the correct tab. Otherwise, -1 is returned and the
333 * state cannot be restored.
334 */
Michael Kolbc831b632011-05-11 09:30:34 -0700335 long canRestoreState(Bundle inState, boolean restoreIncognitoTabs) {
336 final long[] ids = (inState == null) ? null : inState.getLongArray(POSITIONS);
337 if (ids == null) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500338 return -1;
339 }
Michael Kolbc831b632011-05-11 09:30:34 -0700340 final long oldcurrent = inState.getLong(CURRENT);
341 long current = -1;
Michael Kolb3ae7f742011-07-13 15:18:25 -0700342 if (restoreIncognitoTabs || (hasState(oldcurrent, inState) && !isIncognito(oldcurrent, inState))) {
John Reck1cf4b792011-07-26 10:22:22 -0700343 current = oldcurrent;
Patrick Scott7d50a932011-02-04 09:27:26 -0500344 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700345 // pick first non incognito tab
346 for (long id : ids) {
Michael Kolb3ae7f742011-07-13 15:18:25 -0700347 if (hasState(id, inState) && !isIncognito(id, inState)) {
Michael Kolbc831b632011-05-11 09:30:34 -0700348 current = id;
Patrick Scott7d50a932011-02-04 09:27:26 -0500349 break;
350 }
351 }
352 }
Michael Kolbc831b632011-05-11 09:30:34 -0700353 return current;
Patrick Scott7d50a932011-02-04 09:27:26 -0500354 }
355
Michael Kolb3ae7f742011-07-13 15:18:25 -0700356 private boolean hasState(long id, Bundle state) {
357 if (id == -1) return false;
358 Bundle tab = state.getBundle(Long.toString(id));
359 return ((tab != null) && !tab.isEmpty());
360 }
361
362 private boolean isIncognito(long id, Bundle state) {
363 Bundle tabstate = state.getBundle(Long.toString(id));
364 if ((tabstate != null) && !tabstate.isEmpty()) {
365 return tabstate.getBoolean(Tab.INCOGNITO);
366 }
367 return false;
368 }
369
Patrick Scott7d50a932011-02-04 09:27:26 -0500370 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800371 * Restore the state of all the tabs.
Michael Kolbc831b632011-05-11 09:30:34 -0700372 * @param currentId The tab id to restore.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800373 * @param inState The saved state of all the tabs.
Michael Kolb1bf23132010-11-19 12:55:12 -0800374 * @param restoreIncognitoTabs Restoring private browsing tabs
375 * @param restoreAll All webviews get restored, not just the current tab
376 * (this does not override handling of incognito tabs)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800377 */
Michael Kolbc831b632011-05-11 09:30:34 -0700378 void restoreState(Bundle inState, long currentId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500379 boolean restoreIncognitoTabs, boolean restoreAll) {
Michael Kolbc831b632011-05-11 09:30:34 -0700380 if (currentId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500381 return;
382 }
Michael Kolbc831b632011-05-11 09:30:34 -0700383 long[] ids = inState.getLongArray(POSITIONS);
384 long maxId = -Long.MAX_VALUE;
385 HashMap<Long, Tab> tabMap = new HashMap<Long, Tab>();
386 for (long id : ids) {
387 if (id > maxId) {
388 maxId = id;
389 }
390 final String idkey = Long.toString(id);
391 Bundle state = inState.getBundle(idkey);
John Reckd8c74522011-06-14 08:45:00 -0700392 if (state == null || state.isEmpty()) {
393 // Skip tab
394 continue;
395 } else if (!restoreIncognitoTabs
Michael Kolbc831b632011-05-11 09:30:34 -0700396 && state.getBoolean(Tab.INCOGNITO)) {
397 // ignore tab
398 } else if (id == currentId || restoreAll) {
John Reck1cf4b792011-07-26 10:22:22 -0700399 Tab t = createNewTab(state, false);
Narayan Kamath79e5d8d2011-07-20 14:12:38 +0100400 if (t == null) {
401 // We could "break" at this point, but we want
402 // sNextId to be set correctly.
403 continue;
404 }
Michael Kolbc831b632011-05-11 09:30:34 -0700405 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500406 // Me must set the current tab before restoring the state
407 // so that all the client classes are set.
Michael Kolbc831b632011-05-11 09:30:34 -0700408 if (id == currentId) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500409 setCurrentTab(t);
410 }
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700411 } else {
Patrick Scott7d50a932011-02-04 09:27:26 -0500412 // Create a new tab and don't restore the state yet, add it
413 // to the tab list
John Reck1cf4b792011-07-26 10:22:22 -0700414 Tab t = new Tab(mController, state);
Michael Kolbc831b632011-05-11 09:30:34 -0700415 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500416 mTabs.add(t);
417 // added the tab to the front as they are not current
418 mTabQueue.add(0, t);
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700419 }
Michael Kolbc831b632011-05-11 09:30:34 -0700420 }
Narayan Kamath79e5d8d2011-07-20 14:12:38 +0100421
422 // make sure that there is no id overlap between the restored
423 // and new tabs
424 sNextId = maxId + 1;
425
John Reckd8c74522011-06-14 08:45:00 -0700426 if (mCurrentTab == -1) {
427 if (getTabCount() > 0) {
428 setCurrentTab(getTab(0));
John Reckd8c74522011-06-14 08:45:00 -0700429 }
430 }
Michael Kolbc831b632011-05-11 09:30:34 -0700431 // restore parent/child relationships
432 for (long id : ids) {
433 final Tab tab = tabMap.get(id);
434 final Bundle b = inState.getBundle(Long.toString(id));
435 if ((b != null) && (tab != null)) {
436 final long parentId = b.getLong(Tab.PARENTTAB, -1);
437 if (parentId != -1) {
438 final Tab parent = tabMap.get(parentId);
Patrick Scott7d50a932011-02-04 09:27:26 -0500439 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -0700440 parent.addChildTab(tab);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800441 }
442 }
443 }
444 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800445 }
446
447 /**
Grace Klobaf56f68d2010-04-11 22:53:42 -0700448 * Free the memory in this order, 1) free the background tabs; 2) free the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800449 * WebView cache;
450 */
451 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700452 if (getTabCount() == 0) return;
453
Grace Klobaf56f68d2010-04-11 22:53:42 -0700454 // free the least frequently used background tabs
455 Vector<Tab> tabs = getHalfLeastUsedTabs(getCurrentTab());
456 if (tabs.size() > 0) {
457 Log.w(LOGTAG, "Free " + tabs.size() + " tabs in the browser");
458 for (Tab t : tabs) {
459 // store the WebView's state.
460 t.saveState();
461 // destroy the tab
462 t.destroy();
463 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800464 return;
465 }
466
Derek Sollenberger4433d032009-06-10 15:37:21 -0400467 // free the WebView's unused memory (this includes the cache)
468 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800469 WebView view = getCurrentWebView();
470 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400471 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800472 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800473 }
474
Grace Klobaf56f68d2010-04-11 22:53:42 -0700475 private Vector<Tab> getHalfLeastUsedTabs(Tab current) {
476 Vector<Tab> tabsToGo = new Vector<Tab>();
477
Patrick Scott2a67de42009-08-31 09:48:37 -0400478 // Don't do anything if we only have 1 tab or if the current tab is
479 // null.
480 if (getTabCount() == 1 || current == null) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700481 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800482 }
483
Grace Klobaf56f68d2010-04-11 22:53:42 -0700484 if (mTabQueue.size() == 0) {
485 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800486 }
487
Grace Klobaf56f68d2010-04-11 22:53:42 -0700488 // Rip through the queue starting at the beginning and tear down half of
489 // available tabs which are not the current tab or the parent of the
490 // current tab.
491 int openTabCount = 0;
492 for (Tab t : mTabQueue) {
493 if (t != null && t.getWebView() != null) {
494 openTabCount++;
Michael Kolbc831b632011-05-11 09:30:34 -0700495 if (t != current && t != current.getParent()) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700496 tabsToGo.add(t);
497 }
498 }
499 }
500
501 openTabCount /= 2;
502 if (tabsToGo.size() > openTabCount) {
503 tabsToGo.setSize(openTabCount);
504 }
505
506 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800507 }
508
Michael Kolbff6a7482011-07-26 16:37:15 -0700509 Tab getLeastUsedTab(Tab current) {
510 if (getTabCount() == 1 || current == null) {
511 return null;
512 }
513 if (mTabQueue.size() == 0) {
514 return null;
515 }
516 // find a tab which is not the current tab or the parent of the
517 // current tab
518 for (Tab t : mTabQueue) {
519 if (t != null && t.getWebView() != null) {
520 if (t != current && t != current.getParent()) {
521 return t;
522 }
523 }
524 }
525 return null;
526 }
527
The Android Open Source Project0c908882009-03-03 19:32:16 -0800528 /**
529 * Show the tab that contains the given WebView.
530 * @param view The WebView used to find the tab.
531 */
532 Tab getTabFromView(WebView view) {
Michael Kolbc831b632011-05-11 09:30:34 -0700533 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700534 if (t.getSubWebView() == view || t.getWebView() == view) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800535 return t;
536 }
537 }
538 return null;
539 }
540
541 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700542 * Return the tab with the matching application id.
543 * @param id The application identifier.
544 */
Michael Kolbc831b632011-05-11 09:30:34 -0700545 Tab getTabFromAppId(String id) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700546 if (id == null) {
547 return null;
548 }
Michael Kolbc831b632011-05-11 09:30:34 -0700549 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700550 if (id.equals(t.getAppId())) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700551 return t;
552 }
553 }
554 return null;
555 }
556
Grace Kloba22ac16e2009-10-07 18:00:23 -0700557 /**
558 * Stop loading in all opened WebView including subWindows.
559 */
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700560 void stopAllLoading() {
Michael Kolbc831b632011-05-11 09:30:34 -0700561 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700562 final WebView webview = t.getWebView();
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700563 if (webview != null) {
564 webview.stopLoading();
565 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700566 final WebView subview = t.getSubWebView();
567 if (subview != null) {
568 webview.stopLoading();
569 }
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700570 }
571 }
572
John Reckdb22ec42011-06-29 11:31:24 -0700573 // This method checks if a tab matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400574 private boolean tabMatchesUrl(Tab t, String url) {
John Reckdb22ec42011-06-29 11:31:24 -0700575 return url.equals(t.getUrl()) || url.equals(t.getOriginalUrl());
Patrick Scottcd115892009-07-16 09:42:58 -0400576 }
577
578 /**
John Reckdb22ec42011-06-29 11:31:24 -0700579 * Return the tab that matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400580 * @param url The url to search for.
581 */
John Reckdb22ec42011-06-29 11:31:24 -0700582 Tab findTabWithUrl(String url) {
Patrick Scottcd115892009-07-16 09:42:58 -0400583 if (url == null) {
584 return null;
585 }
586 // Check the current tab first.
John Reckdb22ec42011-06-29 11:31:24 -0700587 Tab currentTab = getCurrentTab();
588 if (currentTab != null && tabMatchesUrl(currentTab, url)) {
589 return currentTab;
Patrick Scottcd115892009-07-16 09:42:58 -0400590 }
591 // Now check all the rest.
Michael Kolbc831b632011-05-11 09:30:34 -0700592 for (Tab tab : mTabs) {
593 if (tabMatchesUrl(tab, url)) {
John Reckdb22ec42011-06-29 11:31:24 -0700594 return tab;
Patrick Scottcd115892009-07-16 09:42:58 -0400595 }
596 }
597 return null;
598 }
599
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700600 /**
John Reck30c714c2010-12-16 17:30:34 -0800601 * Recreate the main WebView of the given tab.
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700602 */
John Reck30c714c2010-12-16 17:30:34 -0800603 void recreateWebView(Tab t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700604 final WebView w = t.getWebView();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700605 if (w != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700606 t.destroy();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700607 }
608 // Create a new WebView. If this tab is the current tab, we need to put
609 // back all the clients so force it to be the current tab.
Steve Block2bc69912009-07-30 14:45:13 +0100610 t.setWebView(createNewWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700611 if (getCurrentTab() == t) {
612 setCurrentTab(t, true);
613 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700614 }
615
616 /**
617 * Creates a new WebView and registers it with the global settings.
618 */
619 private WebView createNewWebView() {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700620 return createNewWebView(false);
621 }
622
623 /**
624 * Creates a new WebView and registers it with the global settings.
625 * @param privateBrowsing When true, enables private browsing in the new
626 * WebView.
627 */
628 private WebView createNewWebView(boolean privateBrowsing) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700629 return mController.getWebViewFactory().createWebView(privateBrowsing);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700630 }
631
632 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800633 * Put the current tab in the background and set newTab as the current tab.
634 * @param newTab The new tab. If newTab is null, the current tab is not
635 * set.
636 */
637 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700638 return setCurrentTab(newTab, false);
639 }
640
641 /**
642 * If force is true, this method skips the check for newTab == current.
643 */
644 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800645 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700646 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800647 return true;
648 }
649 if (current != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700650 current.putInBackground();
651 mCurrentTab = -1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800652 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800653 if (newTab == null) {
654 return false;
655 }
656
657 // Move the newTab to the end of the queue
658 int index = mTabQueue.indexOf(newTab);
659 if (index != -1) {
660 mTabQueue.remove(index);
661 }
662 mTabQueue.add(newTab);
663
The Android Open Source Project0c908882009-03-03 19:32:16 -0800664 // Display the new current tab
665 mCurrentTab = mTabs.indexOf(newTab);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700666 WebView mainView = newTab.getWebView();
John Reckd8c74522011-06-14 08:45:00 -0700667 boolean needRestore = !newTab.isSnapshot() && (mainView == null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800668 if (needRestore) {
669 // Same work as in createNewTab() except don't do new Tab()
Steve Block2bc69912009-07-30 14:45:13 +0100670 mainView = createNewWebView();
671 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800672 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700673 newTab.putInForeground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674 return true;
675 }
Michael Kolbfe251992010-07-08 15:41:55 -0700676
John Reck8ee633f2011-08-09 16:00:35 -0700677 public void setOnThumbnailUpdatedListener(OnThumbnailUpdatedListener listener) {
678 mOnThumbnailUpdatedListener = listener;
679 for (Tab t : mTabs) {
680 WebView web = t.getWebView();
681 if (web != null) {
682 web.setPictureListener(listener != null ? t : null);
683 }
684 }
685 }
686
687 public OnThumbnailUpdatedListener getOnThumbnailUpdatedListener() {
688 return mOnThumbnailUpdatedListener;
689 }
690
The Android Open Source Project0c908882009-03-03 19:32:16 -0800691}