blob: 993cd11384d3a43bf53a4c0abe0852f346ecb548 [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) {
Mathew Inwoode09305e2011-09-02 12:03:26 +0100173 for (Tab current : mTabs) {
174 if (current != null && current.getId() == tab.getId()) {
175 throw new IllegalStateException("Tab with id " + tab.getId() + " already exists: "
176 + current.toString());
177 }
178 }
Michael Kolb14612442011-06-24 13:06:29 -0700179 mTabs.add(tab);
180 tab.setController(mController);
181 mController.onSetWebView(tab, tab.getWebView());
182 tab.putInBackground();
183 }
184
Elliott Slaughtere440a882010-08-20 13:54:45 -0700185 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700186 * Create a new tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800187 * @return The newly createTab or null if we have reached the maximum
188 * number of open tabs.
189 */
Michael Kolb7bcafde2011-05-09 13:55:59 -0700190 Tab createNewTab(boolean privateBrowsing) {
John Reck1cf4b792011-07-26 10:22:22 -0700191 return createNewTab(null, privateBrowsing);
192 }
193
194 Tab createNewTab(Bundle state, boolean privateBrowsing) {
195 int size = mTabs.size();
196 // Return false if we have maxed out on tabs
Narayan Kamathc3ad9ea2011-07-26 15:20:21 +0100197 if (!canCreateNewTab()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800198 return null;
199 }
Narayan Kamathc3ad9ea2011-07-26 15:20:21 +0100200
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700201 final WebView w = createNewWebView(privateBrowsing);
Steve Block2bc69912009-07-30 14:45:13 +0100202
The Android Open Source Project0c908882009-03-03 19:32:16 -0800203 // Create a new tab and add it to the tab list
John Reck1cf4b792011-07-26 10:22:22 -0700204 Tab t = new Tab(mController, w, state);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800205 mTabs.add(t);
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700206 // Initially put the tab in the background.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700207 t.putInBackground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800208 return t;
209 }
210
211 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700212 * Create a new tab with default values for closeOnExit(false),
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700213 * appId(null), url(null), and privateBrowsing(false).
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700214 */
215 Tab createNewTab() {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700216 return createNewTab(false);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700217 }
218
John Reckd8c74522011-06-14 08:45:00 -0700219 SnapshotTab createSnapshotTab(long snapshotId) {
John Reckd8c74522011-06-14 08:45:00 -0700220 SnapshotTab t = new SnapshotTab(mController, snapshotId);
John Reckd8c74522011-06-14 08:45:00 -0700221 mTabs.add(t);
222 return t;
223 }
224
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700225 /**
Leon Scrogginsfde97462010-01-11 13:06:21 -0500226 * Remove the parent child relationships from all tabs.
227 */
228 void removeParentChildRelationShips() {
229 for (Tab tab : mTabs) {
230 tab.removeFromTree();
231 }
232 }
233
234 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800235 * Remove the tab from the list. If the tab is the current tab shown, the
236 * last created tab will be shown.
237 * @param t The tab to be removed.
238 */
239 boolean removeTab(Tab t) {
240 if (t == null) {
241 return false;
242 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700243
Patrick Scottd944d4d2010-01-27 16:39:11 -0500244 // Grab the current tab before modifying the list.
245 Tab current = getCurrentTab();
246
247 // Remove t from our list of tabs.
248 mTabs.remove(t);
249
250 // Put the tab in the background only if it is the current one.
251 if (current == t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700252 t.putInBackground();
253 mCurrentTab = -1;
Patrick Scottd944d4d2010-01-27 16:39:11 -0500254 } else {
255 // If a tab that is earlier in the list gets removed, the current
256 // index no longer points to the correct tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700257 mCurrentTab = getTabPosition(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800258 }
259
Grace Kloba22ac16e2009-10-07 18:00:23 -0700260 // destroy the tab
261 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800262 // clear it's references to parent and children
263 t.removeFromTree();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800264
The Android Open Source Project0c908882009-03-03 19:32:16 -0800265 // Remove it from the queue of viewed tabs.
266 mTabQueue.remove(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800267 return true;
268 }
269
270 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800271 * Destroy all the tabs and subwindows
272 */
273 void destroy() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800274 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700275 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800276 }
277 mTabs.clear();
278 mTabQueue.clear();
279 }
280
281 /**
282 * Returns the number of tabs created.
283 * @return The number of tabs created.
284 */
285 int getTabCount() {
286 return mTabs.size();
287 }
288
The Android Open Source Project0c908882009-03-03 19:32:16 -0800289 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700290 * save the tab state:
291 * current position
292 * position sorted array of tab ids
293 * for each tab id, save the tab state
294 * @param outState
John Reckaed9c542011-05-27 16:08:53 -0700295 * @param saveImages
The Android Open Source Project0c908882009-03-03 19:32:16 -0800296 */
John Reck1cf4b792011-07-26 10:22:22 -0700297 void saveState(Bundle outState) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800298 final int numTabs = getTabCount();
John Reck24f18262011-06-17 14:47:20 -0700299 if (numTabs == 0) {
300 return;
301 }
Michael Kolbc831b632011-05-11 09:30:34 -0700302 long[] ids = new long[numTabs];
303 int i = 0;
304 for (Tab tab : mTabs) {
John Reck1cf4b792011-07-26 10:22:22 -0700305 Bundle tabState = tab.saveState();
306 if (tabState != null) {
Michael Kolb3ae7f742011-07-13 15:18:25 -0700307 ids[i++] = tab.getId();
John Reck52be4782011-08-26 15:37:29 -0700308 String key = Long.toString(tab.getId());
309 if (outState.containsKey(key)) {
310 // Dump the tab state for debugging purposes
311 for (Tab dt : mTabs) {
312 Log.e(LOGTAG, dt.toString());
313 }
314 throw new IllegalStateException(
315 "Error saving state, duplicate tab ids!");
316 }
317 outState.putBundle(key, tabState);
Michael Kolb3ae7f742011-07-13 15:18:25 -0700318 } else {
319 ids[i++] = -1;
John Reck52be4782011-08-26 15:37:29 -0700320 // Since we won't be restoring the thumbnail, delete it
321 tab.deleteThumbnail();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800322 }
323 }
John Reck24f18262011-06-17 14:47:20 -0700324 if (!outState.isEmpty()) {
325 outState.putLongArray(POSITIONS, ids);
326 Tab current = getCurrentTab();
327 long cid = -1;
328 if (current != null) {
329 cid = current.getId();
330 }
331 outState.putLong(CURRENT, cid);
Michael Kolbdbf39812011-06-10 14:06:40 -0700332 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800333 }
334
335 /**
Patrick Scott7d50a932011-02-04 09:27:26 -0500336 * Check if the state can be restored. If the state can be restored, the
Michael Kolbc831b632011-05-11 09:30:34 -0700337 * current tab id is returned. This can be passed to restoreState below
Patrick Scott7d50a932011-02-04 09:27:26 -0500338 * in order to restore the correct tab. Otherwise, -1 is returned and the
339 * state cannot be restored.
340 */
Michael Kolbc831b632011-05-11 09:30:34 -0700341 long canRestoreState(Bundle inState, boolean restoreIncognitoTabs) {
342 final long[] ids = (inState == null) ? null : inState.getLongArray(POSITIONS);
343 if (ids == null) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500344 return -1;
345 }
Michael Kolbc831b632011-05-11 09:30:34 -0700346 final long oldcurrent = inState.getLong(CURRENT);
347 long current = -1;
Michael Kolb3ae7f742011-07-13 15:18:25 -0700348 if (restoreIncognitoTabs || (hasState(oldcurrent, inState) && !isIncognito(oldcurrent, inState))) {
John Reck1cf4b792011-07-26 10:22:22 -0700349 current = oldcurrent;
Patrick Scott7d50a932011-02-04 09:27:26 -0500350 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700351 // pick first non incognito tab
352 for (long id : ids) {
Michael Kolb3ae7f742011-07-13 15:18:25 -0700353 if (hasState(id, inState) && !isIncognito(id, inState)) {
Michael Kolbc831b632011-05-11 09:30:34 -0700354 current = id;
Patrick Scott7d50a932011-02-04 09:27:26 -0500355 break;
356 }
357 }
358 }
Michael Kolbc831b632011-05-11 09:30:34 -0700359 return current;
Patrick Scott7d50a932011-02-04 09:27:26 -0500360 }
361
Michael Kolb3ae7f742011-07-13 15:18:25 -0700362 private boolean hasState(long id, Bundle state) {
363 if (id == -1) return false;
364 Bundle tab = state.getBundle(Long.toString(id));
365 return ((tab != null) && !tab.isEmpty());
366 }
367
368 private boolean isIncognito(long id, Bundle state) {
369 Bundle tabstate = state.getBundle(Long.toString(id));
370 if ((tabstate != null) && !tabstate.isEmpty()) {
371 return tabstate.getBoolean(Tab.INCOGNITO);
372 }
373 return false;
374 }
375
Patrick Scott7d50a932011-02-04 09:27:26 -0500376 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800377 * Restore the state of all the tabs.
Michael Kolbc831b632011-05-11 09:30:34 -0700378 * @param currentId The tab id to restore.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800379 * @param inState The saved state of all the tabs.
Michael Kolb1bf23132010-11-19 12:55:12 -0800380 * @param restoreIncognitoTabs Restoring private browsing tabs
381 * @param restoreAll All webviews get restored, not just the current tab
382 * (this does not override handling of incognito tabs)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800383 */
Michael Kolbc831b632011-05-11 09:30:34 -0700384 void restoreState(Bundle inState, long currentId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500385 boolean restoreIncognitoTabs, boolean restoreAll) {
Michael Kolbc831b632011-05-11 09:30:34 -0700386 if (currentId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500387 return;
388 }
Michael Kolbc831b632011-05-11 09:30:34 -0700389 long[] ids = inState.getLongArray(POSITIONS);
390 long maxId = -Long.MAX_VALUE;
391 HashMap<Long, Tab> tabMap = new HashMap<Long, Tab>();
392 for (long id : ids) {
393 if (id > maxId) {
394 maxId = id;
395 }
396 final String idkey = Long.toString(id);
397 Bundle state = inState.getBundle(idkey);
John Reckd8c74522011-06-14 08:45:00 -0700398 if (state == null || state.isEmpty()) {
399 // Skip tab
400 continue;
401 } else if (!restoreIncognitoTabs
Michael Kolbc831b632011-05-11 09:30:34 -0700402 && state.getBoolean(Tab.INCOGNITO)) {
403 // ignore tab
404 } else if (id == currentId || restoreAll) {
John Reck1cf4b792011-07-26 10:22:22 -0700405 Tab t = createNewTab(state, false);
Narayan Kamath79e5d8d2011-07-20 14:12:38 +0100406 if (t == null) {
407 // We could "break" at this point, but we want
408 // sNextId to be set correctly.
409 continue;
410 }
Michael Kolbc831b632011-05-11 09:30:34 -0700411 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500412 // Me must set the current tab before restoring the state
413 // so that all the client classes are set.
Michael Kolbc831b632011-05-11 09:30:34 -0700414 if (id == currentId) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500415 setCurrentTab(t);
416 }
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700417 } else {
Patrick Scott7d50a932011-02-04 09:27:26 -0500418 // Create a new tab and don't restore the state yet, add it
419 // to the tab list
John Reck1cf4b792011-07-26 10:22:22 -0700420 Tab t = new Tab(mController, state);
Michael Kolbc831b632011-05-11 09:30:34 -0700421 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500422 mTabs.add(t);
423 // added the tab to the front as they are not current
424 mTabQueue.add(0, t);
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700425 }
Michael Kolbc831b632011-05-11 09:30:34 -0700426 }
Narayan Kamath79e5d8d2011-07-20 14:12:38 +0100427
428 // make sure that there is no id overlap between the restored
429 // and new tabs
430 sNextId = maxId + 1;
431
John Reckd8c74522011-06-14 08:45:00 -0700432 if (mCurrentTab == -1) {
433 if (getTabCount() > 0) {
434 setCurrentTab(getTab(0));
John Reckd8c74522011-06-14 08:45:00 -0700435 }
436 }
Michael Kolbc831b632011-05-11 09:30:34 -0700437 // restore parent/child relationships
438 for (long id : ids) {
439 final Tab tab = tabMap.get(id);
440 final Bundle b = inState.getBundle(Long.toString(id));
441 if ((b != null) && (tab != null)) {
442 final long parentId = b.getLong(Tab.PARENTTAB, -1);
443 if (parentId != -1) {
444 final Tab parent = tabMap.get(parentId);
Patrick Scott7d50a932011-02-04 09:27:26 -0500445 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -0700446 parent.addChildTab(tab);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800447 }
448 }
449 }
450 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800451 }
452
453 /**
Grace Klobaf56f68d2010-04-11 22:53:42 -0700454 * Free the memory in this order, 1) free the background tabs; 2) free the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800455 * WebView cache;
456 */
457 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700458 if (getTabCount() == 0) return;
459
Grace Klobaf56f68d2010-04-11 22:53:42 -0700460 // free the least frequently used background tabs
461 Vector<Tab> tabs = getHalfLeastUsedTabs(getCurrentTab());
462 if (tabs.size() > 0) {
463 Log.w(LOGTAG, "Free " + tabs.size() + " tabs in the browser");
464 for (Tab t : tabs) {
465 // store the WebView's state.
466 t.saveState();
467 // destroy the tab
468 t.destroy();
469 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800470 return;
471 }
472
Derek Sollenberger4433d032009-06-10 15:37:21 -0400473 // free the WebView's unused memory (this includes the cache)
474 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800475 WebView view = getCurrentWebView();
476 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400477 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800478 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800479 }
480
Grace Klobaf56f68d2010-04-11 22:53:42 -0700481 private Vector<Tab> getHalfLeastUsedTabs(Tab current) {
482 Vector<Tab> tabsToGo = new Vector<Tab>();
483
Patrick Scott2a67de42009-08-31 09:48:37 -0400484 // Don't do anything if we only have 1 tab or if the current tab is
485 // null.
486 if (getTabCount() == 1 || current == null) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700487 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800488 }
489
Grace Klobaf56f68d2010-04-11 22:53:42 -0700490 if (mTabQueue.size() == 0) {
491 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800492 }
493
Grace Klobaf56f68d2010-04-11 22:53:42 -0700494 // Rip through the queue starting at the beginning and tear down half of
495 // available tabs which are not the current tab or the parent of the
496 // current tab.
497 int openTabCount = 0;
498 for (Tab t : mTabQueue) {
499 if (t != null && t.getWebView() != null) {
500 openTabCount++;
Michael Kolbc831b632011-05-11 09:30:34 -0700501 if (t != current && t != current.getParent()) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700502 tabsToGo.add(t);
503 }
504 }
505 }
506
507 openTabCount /= 2;
508 if (tabsToGo.size() > openTabCount) {
509 tabsToGo.setSize(openTabCount);
510 }
511
512 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800513 }
514
Michael Kolbff6a7482011-07-26 16:37:15 -0700515 Tab getLeastUsedTab(Tab current) {
516 if (getTabCount() == 1 || current == null) {
517 return null;
518 }
519 if (mTabQueue.size() == 0) {
520 return null;
521 }
522 // find a tab which is not the current tab or the parent of the
523 // current tab
524 for (Tab t : mTabQueue) {
525 if (t != null && t.getWebView() != null) {
526 if (t != current && t != current.getParent()) {
527 return t;
528 }
529 }
530 }
531 return null;
532 }
533
The Android Open Source Project0c908882009-03-03 19:32:16 -0800534 /**
535 * Show the tab that contains the given WebView.
536 * @param view The WebView used to find the tab.
537 */
538 Tab getTabFromView(WebView view) {
Michael Kolbc831b632011-05-11 09:30:34 -0700539 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700540 if (t.getSubWebView() == view || t.getWebView() == view) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800541 return t;
542 }
543 }
544 return null;
545 }
546
547 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700548 * Return the tab with the matching application id.
549 * @param id The application identifier.
550 */
Michael Kolbc831b632011-05-11 09:30:34 -0700551 Tab getTabFromAppId(String id) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700552 if (id == null) {
553 return null;
554 }
Michael Kolbc831b632011-05-11 09:30:34 -0700555 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700556 if (id.equals(t.getAppId())) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700557 return t;
558 }
559 }
560 return null;
561 }
562
Grace Kloba22ac16e2009-10-07 18:00:23 -0700563 /**
564 * Stop loading in all opened WebView including subWindows.
565 */
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700566 void stopAllLoading() {
Michael Kolbc831b632011-05-11 09:30:34 -0700567 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700568 final WebView webview = t.getWebView();
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700569 if (webview != null) {
570 webview.stopLoading();
571 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700572 final WebView subview = t.getSubWebView();
573 if (subview != null) {
574 webview.stopLoading();
575 }
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700576 }
577 }
578
John Reckdb22ec42011-06-29 11:31:24 -0700579 // This method checks if a tab matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400580 private boolean tabMatchesUrl(Tab t, String url) {
John Reckdb22ec42011-06-29 11:31:24 -0700581 return url.equals(t.getUrl()) || url.equals(t.getOriginalUrl());
Patrick Scottcd115892009-07-16 09:42:58 -0400582 }
583
584 /**
John Reckdb22ec42011-06-29 11:31:24 -0700585 * Return the tab that matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400586 * @param url The url to search for.
587 */
John Reckdb22ec42011-06-29 11:31:24 -0700588 Tab findTabWithUrl(String url) {
Patrick Scottcd115892009-07-16 09:42:58 -0400589 if (url == null) {
590 return null;
591 }
592 // Check the current tab first.
John Reckdb22ec42011-06-29 11:31:24 -0700593 Tab currentTab = getCurrentTab();
594 if (currentTab != null && tabMatchesUrl(currentTab, url)) {
595 return currentTab;
Patrick Scottcd115892009-07-16 09:42:58 -0400596 }
597 // Now check all the rest.
Michael Kolbc831b632011-05-11 09:30:34 -0700598 for (Tab tab : mTabs) {
599 if (tabMatchesUrl(tab, url)) {
John Reckdb22ec42011-06-29 11:31:24 -0700600 return tab;
Patrick Scottcd115892009-07-16 09:42:58 -0400601 }
602 }
603 return null;
604 }
605
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700606 /**
John Reck30c714c2010-12-16 17:30:34 -0800607 * Recreate the main WebView of the given tab.
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700608 */
John Reck30c714c2010-12-16 17:30:34 -0800609 void recreateWebView(Tab t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700610 final WebView w = t.getWebView();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700611 if (w != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700612 t.destroy();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700613 }
614 // Create a new WebView. If this tab is the current tab, we need to put
615 // back all the clients so force it to be the current tab.
Michael Kolb91911a22012-01-17 11:21:25 -0800616 t.setWebView(createNewWebView(), false);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700617 if (getCurrentTab() == t) {
618 setCurrentTab(t, true);
619 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700620 }
621
622 /**
623 * Creates a new WebView and registers it with the global settings.
624 */
625 private WebView createNewWebView() {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700626 return createNewWebView(false);
627 }
628
629 /**
630 * Creates a new WebView and registers it with the global settings.
631 * @param privateBrowsing When true, enables private browsing in the new
632 * WebView.
633 */
634 private WebView createNewWebView(boolean privateBrowsing) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 return mController.getWebViewFactory().createWebView(privateBrowsing);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700636 }
637
638 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800639 * Put the current tab in the background and set newTab as the current tab.
640 * @param newTab The new tab. If newTab is null, the current tab is not
641 * set.
642 */
643 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700644 return setCurrentTab(newTab, false);
645 }
646
647 /**
648 * If force is true, this method skips the check for newTab == current.
649 */
650 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800651 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700652 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800653 return true;
654 }
655 if (current != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700656 current.putInBackground();
657 mCurrentTab = -1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800658 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800659 if (newTab == null) {
660 return false;
661 }
662
663 // Move the newTab to the end of the queue
664 int index = mTabQueue.indexOf(newTab);
665 if (index != -1) {
666 mTabQueue.remove(index);
667 }
668 mTabQueue.add(newTab);
669
The Android Open Source Project0c908882009-03-03 19:32:16 -0800670 // Display the new current tab
671 mCurrentTab = mTabs.indexOf(newTab);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700672 WebView mainView = newTab.getWebView();
John Reckd8c74522011-06-14 08:45:00 -0700673 boolean needRestore = !newTab.isSnapshot() && (mainView == null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674 if (needRestore) {
675 // Same work as in createNewTab() except don't do new Tab()
Steve Block2bc69912009-07-30 14:45:13 +0100676 mainView = createNewWebView();
677 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800678 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700679 newTab.putInForeground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800680 return true;
681 }
Michael Kolbfe251992010-07-08 15:41:55 -0700682
John Reck8ee633f2011-08-09 16:00:35 -0700683 public void setOnThumbnailUpdatedListener(OnThumbnailUpdatedListener listener) {
684 mOnThumbnailUpdatedListener = listener;
685 for (Tab t : mTabs) {
686 WebView web = t.getWebView();
687 if (web != null) {
688 web.setPictureListener(listener != null ? t : null);
689 }
690 }
691 }
692
693 public OnThumbnailUpdatedListener getOnThumbnailUpdatedListener() {
694 return mOnThumbnailUpdatedListener;
695 }
696
The Android Open Source Project0c908882009-03-03 19:32:16 -0800697}