blob: 1be2016acc33c18fc30e594a8bffe5b9bd2562e1 [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();
Michael Kolbc831b632011-05-11 09:30:34 -0700392 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500393 // Me must set the current tab before restoring the state
394 // so that all the client classes are set.
Michael Kolbc831b632011-05-11 09:30:34 -0700395 if (id == currentId) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500396 setCurrentTab(t);
397 }
398 if (!t.restoreState(state)) {
399 Log.w(LOGTAG, "Fail in restoreState, load home page.");
400 t.getWebView().loadUrl(BrowserSettings.getInstance()
401 .getHomePage());
402 }
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700403 } else {
Patrick Scott7d50a932011-02-04 09:27:26 -0500404 // Create a new tab and don't restore the state yet, add it
405 // to the tab list
Michael Kolb7bcafde2011-05-09 13:55:59 -0700406 Tab t = new Tab(mController, null);
Michael Kolbc831b632011-05-11 09:30:34 -0700407 t.setId(id);
408 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500409 if (state != null) {
410 t.setSavedState(state);
411 // Need to maintain the app id and original url so we
412 // can possibly reuse this tab.
413 t.setAppId(state.getString(Tab.APPID));
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700414 }
Patrick Scott7d50a932011-02-04 09:27:26 -0500415 mTabs.add(t);
416 // added the tab to the front as they are not current
417 mTabQueue.add(0, t);
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700418 }
Michael Kolbc831b632011-05-11 09:30:34 -0700419 // make sure that there is no id overlap between the restored
420 // and new tabs
421 sNextId = maxId + 1;
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700422
Michael Kolbc831b632011-05-11 09:30:34 -0700423 }
John Reckd8c74522011-06-14 08:45:00 -0700424 if (mCurrentTab == -1) {
425 if (getTabCount() > 0) {
426 setCurrentTab(getTab(0));
427 } else {
428 Tab t = createNewTab();
429 setCurrentTab(t);
430 t.getWebView().loadUrl(BrowserSettings.getInstance()
431 .getHomePage());
432 }
433 }
Michael Kolbc831b632011-05-11 09:30:34 -0700434 // restore parent/child relationships
435 for (long id : ids) {
436 final Tab tab = tabMap.get(id);
437 final Bundle b = inState.getBundle(Long.toString(id));
438 if ((b != null) && (tab != null)) {
439 final long parentId = b.getLong(Tab.PARENTTAB, -1);
440 if (parentId != -1) {
441 final Tab parent = tabMap.get(parentId);
Patrick Scott7d50a932011-02-04 09:27:26 -0500442 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -0700443 parent.addChildTab(tab);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800444 }
445 }
446 }
447 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800448 }
449
450 /**
Grace Klobaf56f68d2010-04-11 22:53:42 -0700451 * Free the memory in this order, 1) free the background tabs; 2) free the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800452 * WebView cache;
453 */
454 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700455 if (getTabCount() == 0) return;
456
Grace Klobaf56f68d2010-04-11 22:53:42 -0700457 // free the least frequently used background tabs
458 Vector<Tab> tabs = getHalfLeastUsedTabs(getCurrentTab());
459 if (tabs.size() > 0) {
460 Log.w(LOGTAG, "Free " + tabs.size() + " tabs in the browser");
461 for (Tab t : tabs) {
462 // store the WebView's state.
463 t.saveState();
464 // destroy the tab
465 t.destroy();
466 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800467 return;
468 }
469
Derek Sollenberger4433d032009-06-10 15:37:21 -0400470 // free the WebView's unused memory (this includes the cache)
471 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800472 WebView view = getCurrentWebView();
473 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400474 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800475 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800476 }
477
Grace Klobaf56f68d2010-04-11 22:53:42 -0700478 private Vector<Tab> getHalfLeastUsedTabs(Tab current) {
479 Vector<Tab> tabsToGo = new Vector<Tab>();
480
Patrick Scott2a67de42009-08-31 09:48:37 -0400481 // Don't do anything if we only have 1 tab or if the current tab is
482 // null.
483 if (getTabCount() == 1 || current == null) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700484 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800485 }
486
Grace Klobaf56f68d2010-04-11 22:53:42 -0700487 if (mTabQueue.size() == 0) {
488 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800489 }
490
Grace Klobaf56f68d2010-04-11 22:53:42 -0700491 // Rip through the queue starting at the beginning and tear down half of
492 // available tabs which are not the current tab or the parent of the
493 // current tab.
494 int openTabCount = 0;
495 for (Tab t : mTabQueue) {
496 if (t != null && t.getWebView() != null) {
497 openTabCount++;
Michael Kolbc831b632011-05-11 09:30:34 -0700498 if (t != current && t != current.getParent()) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700499 tabsToGo.add(t);
500 }
501 }
502 }
503
504 openTabCount /= 2;
505 if (tabsToGo.size() > openTabCount) {
506 tabsToGo.setSize(openTabCount);
507 }
508
509 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800510 }
511
The Android Open Source Project0c908882009-03-03 19:32:16 -0800512 /**
513 * Show the tab that contains the given WebView.
514 * @param view The WebView used to find the tab.
515 */
516 Tab getTabFromView(WebView view) {
Michael Kolbc831b632011-05-11 09:30:34 -0700517 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700518 if (t.getSubWebView() == view || t.getWebView() == view) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800519 return t;
520 }
521 }
522 return null;
523 }
524
525 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700526 * Return the tab with the matching application id.
527 * @param id The application identifier.
528 */
Michael Kolbc831b632011-05-11 09:30:34 -0700529 Tab getTabFromAppId(String id) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700530 if (id == null) {
531 return null;
532 }
Michael Kolbc831b632011-05-11 09:30:34 -0700533 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700534 if (id.equals(t.getAppId())) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700535 return t;
536 }
537 }
538 return null;
539 }
540
Grace Kloba22ac16e2009-10-07 18:00:23 -0700541 /**
542 * Stop loading in all opened WebView including subWindows.
543 */
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700544 void stopAllLoading() {
Michael Kolbc831b632011-05-11 09:30:34 -0700545 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700546 final WebView webview = t.getWebView();
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700547 if (webview != null) {
548 webview.stopLoading();
549 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700550 final WebView subview = t.getSubWebView();
551 if (subview != null) {
552 webview.stopLoading();
553 }
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700554 }
555 }
556
John Reckdb22ec42011-06-29 11:31:24 -0700557 // This method checks if a tab matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400558 private boolean tabMatchesUrl(Tab t, String url) {
John Reckdb22ec42011-06-29 11:31:24 -0700559 return url.equals(t.getUrl()) || url.equals(t.getOriginalUrl());
Patrick Scottcd115892009-07-16 09:42:58 -0400560 }
561
562 /**
John Reckdb22ec42011-06-29 11:31:24 -0700563 * Return the tab that matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400564 * @param url The url to search for.
565 */
John Reckdb22ec42011-06-29 11:31:24 -0700566 Tab findTabWithUrl(String url) {
Patrick Scottcd115892009-07-16 09:42:58 -0400567 if (url == null) {
568 return null;
569 }
570 // Check the current tab first.
John Reckdb22ec42011-06-29 11:31:24 -0700571 Tab currentTab = getCurrentTab();
572 if (currentTab != null && tabMatchesUrl(currentTab, url)) {
573 return currentTab;
Patrick Scottcd115892009-07-16 09:42:58 -0400574 }
575 // Now check all the rest.
Michael Kolbc831b632011-05-11 09:30:34 -0700576 for (Tab tab : mTabs) {
577 if (tabMatchesUrl(tab, url)) {
John Reckdb22ec42011-06-29 11:31:24 -0700578 return tab;
Patrick Scottcd115892009-07-16 09:42:58 -0400579 }
580 }
581 return null;
582 }
583
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700584 /**
John Reck30c714c2010-12-16 17:30:34 -0800585 * Recreate the main WebView of the given tab.
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700586 */
John Reck30c714c2010-12-16 17:30:34 -0800587 void recreateWebView(Tab t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700588 final WebView w = t.getWebView();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700589 if (w != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700590 t.destroy();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700591 }
592 // Create a new WebView. If this tab is the current tab, we need to put
593 // back all the clients so force it to be the current tab.
Steve Block2bc69912009-07-30 14:45:13 +0100594 t.setWebView(createNewWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700595 if (getCurrentTab() == t) {
596 setCurrentTab(t, true);
597 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700598 // Clear the saved state and picker data
599 t.setSavedState(null);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700600 }
601
602 /**
603 * Creates a new WebView and registers it with the global settings.
604 */
605 private WebView createNewWebView() {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700606 return createNewWebView(false);
607 }
608
609 /**
610 * Creates a new WebView and registers it with the global settings.
611 * @param privateBrowsing When true, enables private browsing in the new
612 * WebView.
613 */
614 private WebView createNewWebView(boolean privateBrowsing) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700615 return mController.getWebViewFactory().createWebView(privateBrowsing);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700616 }
617
618 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800619 * Put the current tab in the background and set newTab as the current tab.
620 * @param newTab The new tab. If newTab is null, the current tab is not
621 * set.
622 */
623 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700624 return setCurrentTab(newTab, false);
625 }
626
627 /**
628 * If force is true, this method skips the check for newTab == current.
629 */
630 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800631 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700632 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800633 return true;
634 }
635 if (current != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700636 current.putInBackground();
637 mCurrentTab = -1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800638 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800639 if (newTab == null) {
640 return false;
641 }
642
643 // Move the newTab to the end of the queue
644 int index = mTabQueue.indexOf(newTab);
645 if (index != -1) {
646 mTabQueue.remove(index);
647 }
648 mTabQueue.add(newTab);
649
The Android Open Source Project0c908882009-03-03 19:32:16 -0800650 // Display the new current tab
651 mCurrentTab = mTabs.indexOf(newTab);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700652 WebView mainView = newTab.getWebView();
John Reckd8c74522011-06-14 08:45:00 -0700653 boolean needRestore = !newTab.isSnapshot() && (mainView == null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800654 if (needRestore) {
655 // Same work as in createNewTab() except don't do new Tab()
Steve Block2bc69912009-07-30 14:45:13 +0100656 mainView = createNewWebView();
657 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800658 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700659 newTab.putInForeground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800660 if (needRestore) {
661 // Have to finish setCurrentTab work before calling restoreState
Grace Kloba22ac16e2009-10-07 18:00:23 -0700662 if (!newTab.restoreState(newTab.getSavedState())) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800663 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
664 }
665 }
666 return true;
667 }
Michael Kolbfe251992010-07-08 15:41:55 -0700668
The Android Open Source Project0c908882009-03-03 19:32:16 -0800669}