blob: 6473c886711fd522fd12b5e2bbbcdb36711e90f0 [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) {
Michael Kolbc831b632011-05-11 09:30:34 -0700300 if (tab.saveState()) {
Michael Kolb3ae7f742011-07-13 15:18:25 -0700301 ids[i++] = tab.getId();
John Reckaed9c542011-05-27 16:08:53 -0700302 outState.putBundle(Long.toString(tab.getId()),
303 tab.getSavedState(saveImages));
Michael Kolb3ae7f742011-07-13 15:18:25 -0700304 } else {
305 ids[i++] = -1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800306 }
307 }
John Reck24f18262011-06-17 14:47:20 -0700308 if (!outState.isEmpty()) {
309 outState.putLongArray(POSITIONS, ids);
310 Tab current = getCurrentTab();
311 long cid = -1;
312 if (current != null) {
313 cid = current.getId();
314 }
315 outState.putLong(CURRENT, cid);
Michael Kolbdbf39812011-06-10 14:06:40 -0700316 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800317 }
318
319 /**
Patrick Scott7d50a932011-02-04 09:27:26 -0500320 * Check if the state can be restored. If the state can be restored, the
Michael Kolbc831b632011-05-11 09:30:34 -0700321 * current tab id is returned. This can be passed to restoreState below
Patrick Scott7d50a932011-02-04 09:27:26 -0500322 * in order to restore the correct tab. Otherwise, -1 is returned and the
323 * state cannot be restored.
324 */
Michael Kolbc831b632011-05-11 09:30:34 -0700325 long canRestoreState(Bundle inState, boolean restoreIncognitoTabs) {
326 final long[] ids = (inState == null) ? null : inState.getLongArray(POSITIONS);
327 if (ids == null) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500328 return -1;
329 }
Michael Kolbc831b632011-05-11 09:30:34 -0700330 final long oldcurrent = inState.getLong(CURRENT);
331 long current = -1;
Michael Kolb3ae7f742011-07-13 15:18:25 -0700332 if (restoreIncognitoTabs || (hasState(oldcurrent, inState) && !isIncognito(oldcurrent, inState))) {
Michael Kolbc831b632011-05-11 09:30:34 -0700333 current = oldcurrent;
Patrick Scott7d50a932011-02-04 09:27:26 -0500334 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700335 // pick first non incognito tab
336 for (long id : ids) {
Michael Kolb3ae7f742011-07-13 15:18:25 -0700337 if (hasState(id, inState) && !isIncognito(id, inState)) {
Michael Kolbc831b632011-05-11 09:30:34 -0700338 current = id;
Patrick Scott7d50a932011-02-04 09:27:26 -0500339 break;
340 }
341 }
342 }
Michael Kolbc831b632011-05-11 09:30:34 -0700343 return current;
Patrick Scott7d50a932011-02-04 09:27:26 -0500344 }
345
Michael Kolb3ae7f742011-07-13 15:18:25 -0700346 private boolean hasState(long id, Bundle state) {
347 if (id == -1) return false;
348 Bundle tab = state.getBundle(Long.toString(id));
349 return ((tab != null) && !tab.isEmpty());
350 }
351
352 private boolean isIncognito(long id, Bundle state) {
353 Bundle tabstate = state.getBundle(Long.toString(id));
354 if ((tabstate != null) && !tabstate.isEmpty()) {
355 return tabstate.getBoolean(Tab.INCOGNITO);
356 }
357 return false;
358 }
359
Patrick Scott7d50a932011-02-04 09:27:26 -0500360 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800361 * Restore the state of all the tabs.
Michael Kolbc831b632011-05-11 09:30:34 -0700362 * @param currentId The tab id to restore.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800363 * @param inState The saved state of all the tabs.
Michael Kolb1bf23132010-11-19 12:55:12 -0800364 * @param restoreIncognitoTabs Restoring private browsing tabs
365 * @param restoreAll All webviews get restored, not just the current tab
366 * (this does not override handling of incognito tabs)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800367 * @return True if there were previous tabs that were restored. False if
368 * there was no saved state or restoring the state failed.
369 */
Michael Kolbc831b632011-05-11 09:30:34 -0700370 void restoreState(Bundle inState, long currentId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500371 boolean restoreIncognitoTabs, boolean restoreAll) {
Michael Kolbc831b632011-05-11 09:30:34 -0700372 if (currentId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500373 return;
374 }
Michael Kolbc831b632011-05-11 09:30:34 -0700375 long[] ids = inState.getLongArray(POSITIONS);
376 long maxId = -Long.MAX_VALUE;
377 HashMap<Long, Tab> tabMap = new HashMap<Long, Tab>();
378 for (long id : ids) {
379 if (id > maxId) {
380 maxId = id;
381 }
382 final String idkey = Long.toString(id);
383 Bundle state = inState.getBundle(idkey);
John Reckd8c74522011-06-14 08:45:00 -0700384 if (state == null || state.isEmpty()) {
385 // Skip tab
386 continue;
387 } else if (!restoreIncognitoTabs
Michael Kolbc831b632011-05-11 09:30:34 -0700388 && state.getBoolean(Tab.INCOGNITO)) {
389 // ignore tab
390 } else if (id == currentId || restoreAll) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500391 Tab t = createNewTab();
Narayan Kamath79e5d8d2011-07-20 14:12:38 +0100392 if (t == null) {
393 // We could "break" at this point, but we want
394 // sNextId to be set correctly.
395 continue;
396 }
Michael Kolbc831b632011-05-11 09:30:34 -0700397 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500398 // Me must set the current tab before restoring the state
399 // so that all the client classes are set.
Michael Kolbc831b632011-05-11 09:30:34 -0700400 if (id == currentId) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500401 setCurrentTab(t);
402 }
403 if (!t.restoreState(state)) {
404 Log.w(LOGTAG, "Fail in restoreState, load home page.");
405 t.getWebView().loadUrl(BrowserSettings.getInstance()
406 .getHomePage());
407 }
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700408 } else {
Patrick Scott7d50a932011-02-04 09:27:26 -0500409 // Create a new tab and don't restore the state yet, add it
410 // to the tab list
Michael Kolb7bcafde2011-05-09 13:55:59 -0700411 Tab t = new Tab(mController, null);
Michael Kolbc831b632011-05-11 09:30:34 -0700412 t.setId(id);
413 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500414 if (state != null) {
415 t.setSavedState(state);
416 // Need to maintain the app id and original url so we
417 // can possibly reuse this tab.
418 t.setAppId(state.getString(Tab.APPID));
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700419 }
Patrick Scott7d50a932011-02-04 09:27:26 -0500420 mTabs.add(t);
421 // added the tab to the front as they are not current
422 mTabQueue.add(0, t);
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700423 }
Michael Kolbc831b632011-05-11 09:30:34 -0700424 }
Narayan Kamath79e5d8d2011-07-20 14:12:38 +0100425
426 // make sure that there is no id overlap between the restored
427 // and new tabs
428 sNextId = maxId + 1;
429
John Reckd8c74522011-06-14 08:45:00 -0700430 if (mCurrentTab == -1) {
431 if (getTabCount() > 0) {
432 setCurrentTab(getTab(0));
433 } else {
434 Tab t = createNewTab();
435 setCurrentTab(t);
436 t.getWebView().loadUrl(BrowserSettings.getInstance()
437 .getHomePage());
438 }
439 }
Michael Kolbc831b632011-05-11 09:30:34 -0700440 // restore parent/child relationships
441 for (long id : ids) {
442 final Tab tab = tabMap.get(id);
443 final Bundle b = inState.getBundle(Long.toString(id));
444 if ((b != null) && (tab != null)) {
445 final long parentId = b.getLong(Tab.PARENTTAB, -1);
446 if (parentId != -1) {
447 final Tab parent = tabMap.get(parentId);
Patrick Scott7d50a932011-02-04 09:27:26 -0500448 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -0700449 parent.addChildTab(tab);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800450 }
451 }
452 }
453 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800454 }
455
456 /**
Grace Klobaf56f68d2010-04-11 22:53:42 -0700457 * Free the memory in this order, 1) free the background tabs; 2) free the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800458 * WebView cache;
459 */
460 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700461 if (getTabCount() == 0) return;
462
Grace Klobaf56f68d2010-04-11 22:53:42 -0700463 // free the least frequently used background tabs
464 Vector<Tab> tabs = getHalfLeastUsedTabs(getCurrentTab());
465 if (tabs.size() > 0) {
466 Log.w(LOGTAG, "Free " + tabs.size() + " tabs in the browser");
467 for (Tab t : tabs) {
468 // store the WebView's state.
469 t.saveState();
470 // destroy the tab
471 t.destroy();
472 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800473 return;
474 }
475
Derek Sollenberger4433d032009-06-10 15:37:21 -0400476 // free the WebView's unused memory (this includes the cache)
477 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800478 WebView view = getCurrentWebView();
479 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400480 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800481 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800482 }
483
Grace Klobaf56f68d2010-04-11 22:53:42 -0700484 private Vector<Tab> getHalfLeastUsedTabs(Tab current) {
485 Vector<Tab> tabsToGo = new Vector<Tab>();
486
Patrick Scott2a67de42009-08-31 09:48:37 -0400487 // Don't do anything if we only have 1 tab or if the current tab is
488 // null.
489 if (getTabCount() == 1 || current == null) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700490 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800491 }
492
Grace Klobaf56f68d2010-04-11 22:53:42 -0700493 if (mTabQueue.size() == 0) {
494 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800495 }
496
Grace Klobaf56f68d2010-04-11 22:53:42 -0700497 // Rip through the queue starting at the beginning and tear down half of
498 // available tabs which are not the current tab or the parent of the
499 // current tab.
500 int openTabCount = 0;
501 for (Tab t : mTabQueue) {
502 if (t != null && t.getWebView() != null) {
503 openTabCount++;
Michael Kolbc831b632011-05-11 09:30:34 -0700504 if (t != current && t != current.getParent()) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700505 tabsToGo.add(t);
506 }
507 }
508 }
509
510 openTabCount /= 2;
511 if (tabsToGo.size() > openTabCount) {
512 tabsToGo.setSize(openTabCount);
513 }
514
515 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800516 }
517
The Android Open Source Project0c908882009-03-03 19:32:16 -0800518 /**
519 * Show the tab that contains the given WebView.
520 * @param view The WebView used to find the tab.
521 */
522 Tab getTabFromView(WebView view) {
Michael Kolbc831b632011-05-11 09:30:34 -0700523 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700524 if (t.getSubWebView() == view || t.getWebView() == view) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800525 return t;
526 }
527 }
528 return null;
529 }
530
531 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700532 * Return the tab with the matching application id.
533 * @param id The application identifier.
534 */
Michael Kolbc831b632011-05-11 09:30:34 -0700535 Tab getTabFromAppId(String id) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700536 if (id == null) {
537 return null;
538 }
Michael Kolbc831b632011-05-11 09:30:34 -0700539 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700540 if (id.equals(t.getAppId())) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700541 return t;
542 }
543 }
544 return null;
545 }
546
Grace Kloba22ac16e2009-10-07 18:00:23 -0700547 /**
548 * Stop loading in all opened WebView including subWindows.
549 */
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700550 void stopAllLoading() {
Michael Kolbc831b632011-05-11 09:30:34 -0700551 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700552 final WebView webview = t.getWebView();
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700553 if (webview != null) {
554 webview.stopLoading();
555 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700556 final WebView subview = t.getSubWebView();
557 if (subview != null) {
558 webview.stopLoading();
559 }
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700560 }
561 }
562
John Reckdb22ec42011-06-29 11:31:24 -0700563 // This method checks if a tab matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400564 private boolean tabMatchesUrl(Tab t, String url) {
John Reckdb22ec42011-06-29 11:31:24 -0700565 return url.equals(t.getUrl()) || url.equals(t.getOriginalUrl());
Patrick Scottcd115892009-07-16 09:42:58 -0400566 }
567
568 /**
John Reckdb22ec42011-06-29 11:31:24 -0700569 * Return the tab that matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400570 * @param url The url to search for.
571 */
John Reckdb22ec42011-06-29 11:31:24 -0700572 Tab findTabWithUrl(String url) {
Patrick Scottcd115892009-07-16 09:42:58 -0400573 if (url == null) {
574 return null;
575 }
576 // Check the current tab first.
John Reckdb22ec42011-06-29 11:31:24 -0700577 Tab currentTab = getCurrentTab();
578 if (currentTab != null && tabMatchesUrl(currentTab, url)) {
579 return currentTab;
Patrick Scottcd115892009-07-16 09:42:58 -0400580 }
581 // Now check all the rest.
Michael Kolbc831b632011-05-11 09:30:34 -0700582 for (Tab tab : mTabs) {
583 if (tabMatchesUrl(tab, url)) {
John Reckdb22ec42011-06-29 11:31:24 -0700584 return tab;
Patrick Scottcd115892009-07-16 09:42:58 -0400585 }
586 }
587 return null;
588 }
589
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700590 /**
John Reck30c714c2010-12-16 17:30:34 -0800591 * Recreate the main WebView of the given tab.
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700592 */
John Reck30c714c2010-12-16 17:30:34 -0800593 void recreateWebView(Tab t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700594 final WebView w = t.getWebView();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700595 if (w != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700596 t.destroy();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700597 }
598 // Create a new WebView. If this tab is the current tab, we need to put
599 // back all the clients so force it to be the current tab.
Steve Block2bc69912009-07-30 14:45:13 +0100600 t.setWebView(createNewWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700601 if (getCurrentTab() == t) {
602 setCurrentTab(t, true);
603 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700604 // Clear the saved state and picker data
605 t.setSavedState(null);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700606 }
607
608 /**
609 * Creates a new WebView and registers it with the global settings.
610 */
611 private WebView createNewWebView() {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700612 return createNewWebView(false);
613 }
614
615 /**
616 * Creates a new WebView and registers it with the global settings.
617 * @param privateBrowsing When true, enables private browsing in the new
618 * WebView.
619 */
620 private WebView createNewWebView(boolean privateBrowsing) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700621 return mController.getWebViewFactory().createWebView(privateBrowsing);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700622 }
623
624 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800625 * Put the current tab in the background and set newTab as the current tab.
626 * @param newTab The new tab. If newTab is null, the current tab is not
627 * set.
628 */
629 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700630 return setCurrentTab(newTab, false);
631 }
632
633 /**
634 * If force is true, this method skips the check for newTab == current.
635 */
636 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800637 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700638 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800639 return true;
640 }
641 if (current != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700642 current.putInBackground();
643 mCurrentTab = -1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800644 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800645 if (newTab == null) {
646 return false;
647 }
648
649 // Move the newTab to the end of the queue
650 int index = mTabQueue.indexOf(newTab);
651 if (index != -1) {
652 mTabQueue.remove(index);
653 }
654 mTabQueue.add(newTab);
655
The Android Open Source Project0c908882009-03-03 19:32:16 -0800656 // Display the new current tab
657 mCurrentTab = mTabs.indexOf(newTab);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700658 WebView mainView = newTab.getWebView();
John Reckd8c74522011-06-14 08:45:00 -0700659 boolean needRestore = !newTab.isSnapshot() && (mainView == null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800660 if (needRestore) {
661 // Same work as in createNewTab() except don't do new Tab()
Steve Block2bc69912009-07-30 14:45:13 +0100662 mainView = createNewWebView();
663 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800664 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700665 newTab.putInForeground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800666 if (needRestore) {
667 // Have to finish setCurrentTab work before calling restoreState
Grace Kloba22ac16e2009-10-07 18:00:23 -0700668 if (!newTab.restoreState(newTab.getSavedState())) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800669 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
670 }
671 }
672 return true;
673 }
Michael Kolbfe251992010-07-08 15:41:55 -0700674
The Android Open Source Project0c908882009-03-03 19:32:16 -0800675}