blob: 0d16b5945c5ffdcd9258dbfd90c695e6a34434ef [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080018
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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080021
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070022import org.codeaurora.swe.GeolocationPermissions;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080023import org.codeaurora.swe.WebView;
Enrico Ros1f5a0952014-11-18 20:15:48 -080024import org.codeaurora.swe.util.Observable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025
The Android Open Source Project0c908882009-03-03 19:32:16 -080026import java.util.ArrayList;
Elliott Slaughter3d6df162010-08-25 13:17:44 -070027import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080028import java.util.List;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import java.util.Vector;
30
31class TabControl {
32 // Log Tag
33 private static final String LOGTAG = "TabControl";
Michael Kolbc831b632011-05-11 09:30:34 -070034
John Reckd8c74522011-06-14 08:45:00 -070035 // next Tab ID, starting at 1
36 private static long sNextId = 1;
Michael Kolbc831b632011-05-11 09:30:34 -070037
38 private static final String POSITIONS = "positions";
39 private static final String CURRENT = "current";
40
John Reck8ee633f2011-08-09 16:00:35 -070041 public static interface OnThumbnailUpdatedListener {
42 void onThumbnailUpdated(Tab t);
43 }
44
The Android Open Source Project0c908882009-03-03 19:32:16 -080045 // Maximum number of tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070046 private int mMaxTabs;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047 // Private array of WebViews that are used as tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070048 private ArrayList<Tab> mTabs;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049 // Queue of most recently viewed tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070050 private ArrayList<Tab> mTabQueue;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051 // Current position in mTabs.
52 private int mCurrentTab = -1;
Michael Kolb8233fac2010-10-26 16:08:53 -070053 // the main browser controller
54 private final Controller mController;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070055 // number of incognito tabs
56 private int mNumIncognito = 0;
Michael Kolb8233fac2010-10-26 16:08:53 -070057
John Reck8ee633f2011-08-09 16:00:35 -070058 private OnThumbnailUpdatedListener mOnThumbnailUpdatedListener;
The Android Open Source Project0c908882009-03-03 19:32:16 -080059
Enrico Ros1f5a0952014-11-18 20:15:48 -080060 private Observable mTabCountObservable;
61
The Android Open Source Project0c908882009-03-03 19:32:16 -080062 /**
Michael Kolb8233fac2010-10-26 16:08:53 -070063 * Construct a new TabControl object
The Android Open Source Project0c908882009-03-03 19:32:16 -080064 */
Michael Kolb8233fac2010-10-26 16:08:53 -070065 TabControl(Controller controller) {
66 mController = controller;
Michael Kolb8233fac2010-10-26 16:08:53 -070067 mMaxTabs = mController.getMaxTabs();
Michael Kolb6e4653e2010-09-27 16:22:38 -070068 mTabs = new ArrayList<Tab>(mMaxTabs);
69 mTabQueue = new ArrayList<Tab>(mMaxTabs);
Enrico Ros1f5a0952014-11-18 20:15:48 -080070 mTabCountObservable = new Observable();
71 mTabCountObservable.set(0);
The Android Open Source Project0c908882009-03-03 19:32:16 -080072 }
73
John Reck52be4782011-08-26 15:37:29 -070074 synchronized static long getNextId() {
Michael Kolbc831b632011-05-11 09:30:34 -070075 return sNextId++;
76 }
77
Enrico Ros1f5a0952014-11-18 20:15:48 -080078 Observable getTabCountObservable() {
79 return mTabCountObservable;
80 }
81
Michael Kolb68775752010-08-19 12:42:01 -070082 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080083 * Return the current tab's main WebView. This will always return the main
84 * WebView for a given tab and not a subwindow.
85 * @return The current tab's WebView.
86 */
87 WebView getCurrentWebView() {
88 Tab t = getTab(mCurrentTab);
89 if (t == null) {
90 return null;
91 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070092 return t.getWebView();
Ben Murdochbff2d602009-07-01 20:19:05 +010093 }
94
95 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080096 * Return the current tab's top-level WebView. This can return a subwindow
97 * if one exists.
98 * @return The top-level WebView of the current tab.
99 */
100 WebView getCurrentTopWebView() {
101 Tab t = getTab(mCurrentTab);
102 if (t == null) {
103 return null;
104 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700105 return t.getTopWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800106 }
107
108 /**
109 * Return the current tab's subwindow if it exists.
110 * @return The subwindow of the current tab or null if it doesn't exist.
111 */
112 WebView getCurrentSubWindow() {
113 Tab t = getTab(mCurrentTab);
114 if (t == null) {
115 return null;
116 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700117 return t.getSubWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118 }
119
120 /**
Michael Kolb1bf23132010-11-19 12:55:12 -0800121 * return the list of tabs
122 */
123 List<Tab> getTabs() {
124 return mTabs;
125 }
126
127 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700128 * Return the tab at the specified position.
129 * @return The Tab for the specified position or null if the tab does not
The Android Open Source Project0c908882009-03-03 19:32:16 -0800130 * exist.
131 */
Michael Kolbc831b632011-05-11 09:30:34 -0700132 Tab getTab(int position) {
133 if (position >= 0 && position < mTabs.size()) {
134 return mTabs.get(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800135 }
136 return null;
137 }
138
139 /**
140 * Return the current tab.
141 * @return The current tab.
142 */
143 Tab getCurrentTab() {
144 return getTab(mCurrentTab);
145 }
146
147 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700148 * Return the current tab position.
149 * @return The current tab position
The Android Open Source Project0c908882009-03-03 19:32:16 -0800150 */
Michael Kolbc831b632011-05-11 09:30:34 -0700151 int getCurrentPosition() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800152 return mCurrentTab;
153 }
Michael Kolbfe251992010-07-08 15:41:55 -0700154
The Android Open Source Project0c908882009-03-03 19:32:16 -0800155 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700156 * Given a Tab, find it's position
The Android Open Source Project0c908882009-03-03 19:32:16 -0800157 * @param Tab to find
Michael Kolbc831b632011-05-11 09:30:34 -0700158 * @return position of Tab or -1 if not found
The Android Open Source Project0c908882009-03-03 19:32:16 -0800159 */
Michael Kolbc831b632011-05-11 09:30:34 -0700160 int getTabPosition(Tab tab) {
Patrick Scottae641ac2009-04-20 13:51:49 -0400161 if (tab == null) {
162 return -1;
163 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800164 return mTabs.indexOf(tab);
165 }
166
Grace Kloba22ac16e2009-10-07 18:00:23 -0700167 boolean canCreateNewTab() {
Narayan Kamathc3ad9ea2011-07-26 15:20:21 +0100168 return mMaxTabs > mTabs.size();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700169 }
170
The Android Open Source Project0c908882009-03-03 19:32:16 -0800171 /**
Elliott Slaughtere440a882010-08-20 13:54:45 -0700172 * Returns true if there are any incognito tabs open.
173 * @return True when any incognito tabs are open, false otherwise.
174 */
175 boolean hasAnyOpenIncognitoTabs() {
176 for (Tab tab : mTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700177 if (tab.getWebView() != null
178 && tab.getWebView().isPrivateBrowsingEnabled()) {
Elliott Slaughtere440a882010-08-20 13:54:45 -0700179 return true;
180 }
181 }
182 return false;
183 }
184
Michael Kolb14612442011-06-24 13:06:29 -0700185 void addPreloadedTab(Tab tab) {
Mathew Inwoode09305e2011-09-02 12:03:26 +0100186 for (Tab current : mTabs) {
187 if (current != null && current.getId() == tab.getId()) {
188 throw new IllegalStateException("Tab with id " + tab.getId() + " already exists: "
189 + current.toString());
190 }
191 }
Michael Kolb14612442011-06-24 13:06:29 -0700192 mTabs.add(tab);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800193 mTabCountObservable.set(mTabs.size());
Michael Kolb14612442011-06-24 13:06:29 -0700194 tab.setController(mController);
195 mController.onSetWebView(tab, tab.getWebView());
196 tab.putInBackground();
197 }
198
Elliott Slaughtere440a882010-08-20 13:54:45 -0700199 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700200 * Create a new tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800201 * @return The newly createTab or null if we have reached the maximum
202 * number of open tabs.
203 */
Michael Kolb7bcafde2011-05-09 13:55:59 -0700204 Tab createNewTab(boolean privateBrowsing) {
John Reck1cf4b792011-07-26 10:22:22 -0700205 return createNewTab(null, privateBrowsing);
206 }
207
208 Tab createNewTab(Bundle state, boolean privateBrowsing) {
209 int size = mTabs.size();
210 // Return false if we have maxed out on tabs
Narayan Kamathc3ad9ea2011-07-26 15:20:21 +0100211 if (!canCreateNewTab()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800212 return null;
213 }
Narayan Kamathc3ad9ea2011-07-26 15:20:21 +0100214
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700215 final WebView w = createNewWebView(privateBrowsing);
Steve Block2bc69912009-07-30 14:45:13 +0100216
The Android Open Source Project0c908882009-03-03 19:32:16 -0800217 // Create a new tab and add it to the tab list
John Reck1cf4b792011-07-26 10:22:22 -0700218 Tab t = new Tab(mController, w, state);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800219 mTabs.add(t);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800220 mTabCountObservable.set(mTabs.size());
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700221 if (privateBrowsing) {
222 mNumIncognito += 1;
223 }
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700224 // Initially put the tab in the background.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700225 t.putInBackground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800226 return t;
227 }
228
229 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700230 * Create a new tab with default values for closeOnExit(false),
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700231 * appId(null), url(null), and privateBrowsing(false).
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700232 */
233 Tab createNewTab() {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700234 return createNewTab(false);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700235 }
236
John Reckd8c74522011-06-14 08:45:00 -0700237 SnapshotTab createSnapshotTab(long snapshotId) {
John Reckd8c74522011-06-14 08:45:00 -0700238 SnapshotTab t = new SnapshotTab(mController, snapshotId);
John Reckd8c74522011-06-14 08:45:00 -0700239 mTabs.add(t);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800240 mTabCountObservable.set(mTabs.size());
John Reckd8c74522011-06-14 08:45:00 -0700241 return t;
242 }
243
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700244 /**
Leon Scrogginsfde97462010-01-11 13:06:21 -0500245 * Remove the parent child relationships from all tabs.
246 */
247 void removeParentChildRelationShips() {
248 for (Tab tab : mTabs) {
249 tab.removeFromTree();
250 }
251 }
252
253 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800254 * Remove the tab from the list. If the tab is the current tab shown, the
255 * last created tab will be shown.
256 * @param t The tab to be removed.
257 */
258 boolean removeTab(Tab t) {
259 if (t == null) {
260 return false;
261 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700262
Patrick Scottd944d4d2010-01-27 16:39:11 -0500263 // Grab the current tab before modifying the list.
264 Tab current = getCurrentTab();
265
266 // Remove t from our list of tabs.
267 mTabs.remove(t);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800268 mTabCountObservable.set(mTabs.size());
Patrick Scottd944d4d2010-01-27 16:39:11 -0500269
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700270 //Clear incognito geolocation state if this is the last incognito tab.
271 if (t.isPrivateBrowsingEnabled()) {
272 mNumIncognito -= 1;
273 if (mNumIncognito == 0) {
274 GeolocationPermissions.onIncognitoTabsRemoved();
275 }
276 }
277
Patrick Scottd944d4d2010-01-27 16:39:11 -0500278 // Put the tab in the background only if it is the current one.
279 if (current == t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700280 t.putInBackground();
281 mCurrentTab = -1;
Patrick Scottd944d4d2010-01-27 16:39:11 -0500282 } else {
283 // If a tab that is earlier in the list gets removed, the current
284 // index no longer points to the correct tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700285 mCurrentTab = getTabPosition(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800286 }
287
Grace Kloba22ac16e2009-10-07 18:00:23 -0700288 // destroy the tab
289 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800290 // clear it's references to parent and children
291 t.removeFromTree();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800292
The Android Open Source Project0c908882009-03-03 19:32:16 -0800293 // Remove it from the queue of viewed tabs.
294 mTabQueue.remove(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800295 return true;
296 }
297
298 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800299 * Destroy all the tabs and subwindows
300 */
301 void destroy() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800302 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700303 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800304 }
305 mTabs.clear();
306 mTabQueue.clear();
307 }
308
309 /**
310 * Returns the number of tabs created.
311 * @return The number of tabs created.
312 */
313 int getTabCount() {
314 return mTabs.size();
315 }
316
The Android Open Source Project0c908882009-03-03 19:32:16 -0800317 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700318 * save the tab state:
319 * current position
320 * position sorted array of tab ids
321 * for each tab id, save the tab state
322 * @param outState
John Reckaed9c542011-05-27 16:08:53 -0700323 * @param saveImages
The Android Open Source Project0c908882009-03-03 19:32:16 -0800324 */
John Reck1cf4b792011-07-26 10:22:22 -0700325 void saveState(Bundle outState) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800326 final int numTabs = getTabCount();
John Reck24f18262011-06-17 14:47:20 -0700327 if (numTabs == 0) {
328 return;
329 }
Michael Kolbc831b632011-05-11 09:30:34 -0700330 long[] ids = new long[numTabs];
331 int i = 0;
332 for (Tab tab : mTabs) {
John Reck1cf4b792011-07-26 10:22:22 -0700333 Bundle tabState = tab.saveState();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700334 if (tabState != null && tab.isPrivateBrowsingEnabled() == false) {
Michael Kolb3ae7f742011-07-13 15:18:25 -0700335 ids[i++] = tab.getId();
John Reck52be4782011-08-26 15:37:29 -0700336 String key = Long.toString(tab.getId());
337 if (outState.containsKey(key)) {
338 // Dump the tab state for debugging purposes
339 for (Tab dt : mTabs) {
340 Log.e(LOGTAG, dt.toString());
341 }
342 throw new IllegalStateException(
343 "Error saving state, duplicate tab ids!");
344 }
345 outState.putBundle(key, tabState);
Michael Kolb3ae7f742011-07-13 15:18:25 -0700346 } else {
347 ids[i++] = -1;
John Reck52be4782011-08-26 15:37:29 -0700348 // Since we won't be restoring the thumbnail, delete it
349 tab.deleteThumbnail();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800350 }
351 }
John Reck24f18262011-06-17 14:47:20 -0700352 if (!outState.isEmpty()) {
353 outState.putLongArray(POSITIONS, ids);
354 Tab current = getCurrentTab();
355 long cid = -1;
356 if (current != null) {
357 cid = current.getId();
358 }
359 outState.putLong(CURRENT, cid);
Michael Kolbdbf39812011-06-10 14:06:40 -0700360 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800361 }
362
363 /**
Patrick Scott7d50a932011-02-04 09:27:26 -0500364 * Check if the state can be restored. If the state can be restored, the
Michael Kolbc831b632011-05-11 09:30:34 -0700365 * current tab id is returned. This can be passed to restoreState below
Patrick Scott7d50a932011-02-04 09:27:26 -0500366 * in order to restore the correct tab. Otherwise, -1 is returned and the
367 * state cannot be restored.
368 */
Michael Kolbc831b632011-05-11 09:30:34 -0700369 long canRestoreState(Bundle inState, boolean restoreIncognitoTabs) {
370 final long[] ids = (inState == null) ? null : inState.getLongArray(POSITIONS);
371 if (ids == null) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500372 return -1;
373 }
Michael Kolbc831b632011-05-11 09:30:34 -0700374 final long oldcurrent = inState.getLong(CURRENT);
375 long current = -1;
Michael Kolb3ae7f742011-07-13 15:18:25 -0700376 if (restoreIncognitoTabs || (hasState(oldcurrent, inState) && !isIncognito(oldcurrent, inState))) {
John Reck1cf4b792011-07-26 10:22:22 -0700377 current = oldcurrent;
Patrick Scott7d50a932011-02-04 09:27:26 -0500378 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700379 // pick first non incognito tab
380 for (long id : ids) {
Michael Kolb3ae7f742011-07-13 15:18:25 -0700381 if (hasState(id, inState) && !isIncognito(id, inState)) {
Michael Kolbc831b632011-05-11 09:30:34 -0700382 current = id;
Patrick Scott7d50a932011-02-04 09:27:26 -0500383 break;
384 }
385 }
386 }
Michael Kolbc831b632011-05-11 09:30:34 -0700387 return current;
Patrick Scott7d50a932011-02-04 09:27:26 -0500388 }
389
Michael Kolb3ae7f742011-07-13 15:18:25 -0700390 private boolean hasState(long id, Bundle state) {
391 if (id == -1) return false;
392 Bundle tab = state.getBundle(Long.toString(id));
393 return ((tab != null) && !tab.isEmpty());
394 }
395
396 private boolean isIncognito(long id, Bundle state) {
397 Bundle tabstate = state.getBundle(Long.toString(id));
398 if ((tabstate != null) && !tabstate.isEmpty()) {
399 return tabstate.getBoolean(Tab.INCOGNITO);
400 }
401 return false;
402 }
403
Patrick Scott7d50a932011-02-04 09:27:26 -0500404 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800405 * Restore the state of all the tabs.
Michael Kolbc831b632011-05-11 09:30:34 -0700406 * @param currentId The tab id to restore.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800407 * @param inState The saved state of all the tabs.
Michael Kolb1bf23132010-11-19 12:55:12 -0800408 * @param restoreIncognitoTabs Restoring private browsing tabs
409 * @param restoreAll All webviews get restored, not just the current tab
410 * (this does not override handling of incognito tabs)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800411 */
Michael Kolbc831b632011-05-11 09:30:34 -0700412 void restoreState(Bundle inState, long currentId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500413 boolean restoreIncognitoTabs, boolean restoreAll) {
Michael Kolbc831b632011-05-11 09:30:34 -0700414 if (currentId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500415 return;
416 }
Michael Kolbc831b632011-05-11 09:30:34 -0700417 long[] ids = inState.getLongArray(POSITIONS);
418 long maxId = -Long.MAX_VALUE;
419 HashMap<Long, Tab> tabMap = new HashMap<Long, Tab>();
420 for (long id : ids) {
421 if (id > maxId) {
422 maxId = id;
423 }
424 final String idkey = Long.toString(id);
425 Bundle state = inState.getBundle(idkey);
John Reckd8c74522011-06-14 08:45:00 -0700426 if (state == null || state.isEmpty()) {
427 // Skip tab
428 continue;
429 } else if (!restoreIncognitoTabs
Michael Kolbc831b632011-05-11 09:30:34 -0700430 && state.getBoolean(Tab.INCOGNITO)) {
431 // ignore tab
432 } else if (id == currentId || restoreAll) {
John Reck1cf4b792011-07-26 10:22:22 -0700433 Tab t = createNewTab(state, false);
Narayan Kamath79e5d8d2011-07-20 14:12:38 +0100434 if (t == null) {
435 // We could "break" at this point, but we want
436 // sNextId to be set correctly.
437 continue;
438 }
Michael Kolbc831b632011-05-11 09:30:34 -0700439 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500440 // Me must set the current tab before restoring the state
441 // so that all the client classes are set.
Michael Kolbc831b632011-05-11 09:30:34 -0700442 if (id == currentId) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500443 setCurrentTab(t);
444 }
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700445 } else {
Patrick Scott7d50a932011-02-04 09:27:26 -0500446 // Create a new tab and don't restore the state yet, add it
447 // to the tab list
John Reck1cf4b792011-07-26 10:22:22 -0700448 Tab t = new Tab(mController, state);
Michael Kolbc831b632011-05-11 09:30:34 -0700449 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500450 mTabs.add(t);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800451 mTabCountObservable.set(mTabs.size());
Patrick Scott7d50a932011-02-04 09:27:26 -0500452 // added the tab to the front as they are not current
453 mTabQueue.add(0, t);
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700454 }
Michael Kolbc831b632011-05-11 09:30:34 -0700455 }
Narayan Kamath79e5d8d2011-07-20 14:12:38 +0100456
457 // make sure that there is no id overlap between the restored
458 // and new tabs
459 sNextId = maxId + 1;
460
John Reckd8c74522011-06-14 08:45:00 -0700461 if (mCurrentTab == -1) {
462 if (getTabCount() > 0) {
463 setCurrentTab(getTab(0));
John Reckd8c74522011-06-14 08:45:00 -0700464 }
465 }
Michael Kolbc831b632011-05-11 09:30:34 -0700466 // restore parent/child relationships
467 for (long id : ids) {
468 final Tab tab = tabMap.get(id);
469 final Bundle b = inState.getBundle(Long.toString(id));
470 if ((b != null) && (tab != null)) {
471 final long parentId = b.getLong(Tab.PARENTTAB, -1);
472 if (parentId != -1) {
473 final Tab parent = tabMap.get(parentId);
Patrick Scott7d50a932011-02-04 09:27:26 -0500474 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -0700475 parent.addChildTab(tab);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800476 }
477 }
478 }
479 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800480 }
481
482 /**
Grace Klobaf56f68d2010-04-11 22:53:42 -0700483 * Free the memory in this order, 1) free the background tabs; 2) free the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800484 * WebView cache;
485 */
486 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700487 if (getTabCount() == 0) return;
488
Grace Klobaf56f68d2010-04-11 22:53:42 -0700489 // free the least frequently used background tabs
490 Vector<Tab> tabs = getHalfLeastUsedTabs(getCurrentTab());
491 if (tabs.size() > 0) {
492 Log.w(LOGTAG, "Free " + tabs.size() + " tabs in the browser");
493 for (Tab t : tabs) {
494 // store the WebView's state.
495 t.saveState();
496 // destroy the tab
497 t.destroy();
498 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800499 return;
500 }
501
Derek Sollenberger4433d032009-06-10 15:37:21 -0400502 // free the WebView's unused memory (this includes the cache)
503 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800504 WebView view = getCurrentWebView();
505 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400506 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800507 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800508 }
509
Grace Klobaf56f68d2010-04-11 22:53:42 -0700510 private Vector<Tab> getHalfLeastUsedTabs(Tab current) {
511 Vector<Tab> tabsToGo = new Vector<Tab>();
512
Patrick Scott2a67de42009-08-31 09:48:37 -0400513 // Don't do anything if we only have 1 tab or if the current tab is
514 // null.
515 if (getTabCount() == 1 || current == null) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700516 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800517 }
518
Grace Klobaf56f68d2010-04-11 22:53:42 -0700519 if (mTabQueue.size() == 0) {
520 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800521 }
522
Grace Klobaf56f68d2010-04-11 22:53:42 -0700523 // Rip through the queue starting at the beginning and tear down half of
524 // available tabs which are not the current tab or the parent of the
525 // current tab.
526 int openTabCount = 0;
527 for (Tab t : mTabQueue) {
528 if (t != null && t.getWebView() != null) {
529 openTabCount++;
Michael Kolbc831b632011-05-11 09:30:34 -0700530 if (t != current && t != current.getParent()) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700531 tabsToGo.add(t);
532 }
533 }
534 }
535
536 openTabCount /= 2;
537 if (tabsToGo.size() > openTabCount) {
538 tabsToGo.setSize(openTabCount);
539 }
540
541 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800542 }
543
Michael Kolbff6a7482011-07-26 16:37:15 -0700544 Tab getLeastUsedTab(Tab current) {
545 if (getTabCount() == 1 || current == null) {
546 return null;
547 }
548 if (mTabQueue.size() == 0) {
549 return null;
550 }
551 // find a tab which is not the current tab or the parent of the
552 // current tab
553 for (Tab t : mTabQueue) {
554 if (t != null && t.getWebView() != null) {
555 if (t != current && t != current.getParent()) {
556 return t;
557 }
558 }
559 }
560 return null;
561 }
562
The Android Open Source Project0c908882009-03-03 19:32:16 -0800563 /**
564 * Show the tab that contains the given WebView.
565 * @param view The WebView used to find the tab.
566 */
567 Tab getTabFromView(WebView view) {
Michael Kolbc831b632011-05-11 09:30:34 -0700568 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700569 if (t.getSubWebView() == view || t.getWebView() == view) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800570 return t;
571 }
572 }
573 return null;
574 }
575
576 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700577 * Return the tab with the matching application id.
578 * @param id The application identifier.
579 */
Michael Kolbc831b632011-05-11 09:30:34 -0700580 Tab getTabFromAppId(String id) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700581 if (id == null) {
582 return null;
583 }
Michael Kolbc831b632011-05-11 09:30:34 -0700584 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700585 if (id.equals(t.getAppId())) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700586 return t;
587 }
588 }
589 return null;
590 }
591
Grace Kloba22ac16e2009-10-07 18:00:23 -0700592 /**
593 * Stop loading in all opened WebView including subWindows.
594 */
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700595 void stopAllLoading() {
Michael Kolbc831b632011-05-11 09:30:34 -0700596 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700597 final WebView webview = t.getWebView();
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700598 if (webview != null) {
599 webview.stopLoading();
600 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700601 final WebView subview = t.getSubWebView();
602 if (subview != null) {
Mattias Nilsson9727af82012-06-14 17:07:56 +0200603 subview.stopLoading();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700604 }
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700605 }
606 }
607
John Reckdb22ec42011-06-29 11:31:24 -0700608 // This method checks if a tab matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400609 private boolean tabMatchesUrl(Tab t, String url) {
John Reckdb22ec42011-06-29 11:31:24 -0700610 return url.equals(t.getUrl()) || url.equals(t.getOriginalUrl());
Patrick Scottcd115892009-07-16 09:42:58 -0400611 }
612
613 /**
John Reckdb22ec42011-06-29 11:31:24 -0700614 * Return the tab that matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400615 * @param url The url to search for.
616 */
John Reckdb22ec42011-06-29 11:31:24 -0700617 Tab findTabWithUrl(String url) {
Patrick Scottcd115892009-07-16 09:42:58 -0400618 if (url == null) {
619 return null;
620 }
621 // Check the current tab first.
John Reckdb22ec42011-06-29 11:31:24 -0700622 Tab currentTab = getCurrentTab();
623 if (currentTab != null && tabMatchesUrl(currentTab, url)) {
624 return currentTab;
Patrick Scottcd115892009-07-16 09:42:58 -0400625 }
626 // Now check all the rest.
Michael Kolbc831b632011-05-11 09:30:34 -0700627 for (Tab tab : mTabs) {
628 if (tabMatchesUrl(tab, url)) {
John Reckdb22ec42011-06-29 11:31:24 -0700629 return tab;
Patrick Scottcd115892009-07-16 09:42:58 -0400630 }
631 }
632 return null;
633 }
634
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700635 /**
John Reck30c714c2010-12-16 17:30:34 -0800636 * Recreate the main WebView of the given tab.
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700637 */
John Reck30c714c2010-12-16 17:30:34 -0800638 void recreateWebView(Tab t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700639 final WebView w = t.getWebView();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700640 if (w != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700641 t.destroy();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700642 }
643 // Create a new WebView. If this tab is the current tab, we need to put
644 // back all the clients so force it to be the current tab.
Panos Thomasa9a5a582014-03-18 19:20:08 -0700645 t.setWebView(createNewWebView(t.isPrivateBrowsingEnabled()), false);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700646 if (getCurrentTab() == t) {
647 setCurrentTab(t, true);
648 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700649 }
650
651 /**
652 * Creates a new WebView and registers it with the global settings.
653 */
654 private WebView createNewWebView() {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700655 return createNewWebView(false);
656 }
657
658 /**
659 * Creates a new WebView and registers it with the global settings.
660 * @param privateBrowsing When true, enables private browsing in the new
661 * WebView.
662 */
663 private WebView createNewWebView(boolean privateBrowsing) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700664 return mController.getWebViewFactory().createWebView(privateBrowsing);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700665 }
666
667 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800668 * Put the current tab in the background and set newTab as the current tab.
669 * @param newTab The new tab. If newTab is null, the current tab is not
670 * set.
671 */
672 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700673 return setCurrentTab(newTab, false);
674 }
675
676 /**
677 * If force is true, this method skips the check for newTab == current.
678 */
679 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800680 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700681 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800682 return true;
683 }
684 if (current != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700685 current.putInBackground();
686 mCurrentTab = -1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800687 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800688 if (newTab == null) {
689 return false;
690 }
691
692 // Move the newTab to the end of the queue
693 int index = mTabQueue.indexOf(newTab);
694 if (index != -1) {
695 mTabQueue.remove(index);
696 }
697 mTabQueue.add(newTab);
698
The Android Open Source Project0c908882009-03-03 19:32:16 -0800699 // Display the new current tab
700 mCurrentTab = mTabs.indexOf(newTab);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700701 WebView mainView = newTab.getWebView();
John Reckd8c74522011-06-14 08:45:00 -0700702 boolean needRestore = !newTab.isSnapshot() && (mainView == null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800703 if (needRestore) {
704 // Same work as in createNewTab() except don't do new Tab()
Panos Thomasa9a5a582014-03-18 19:20:08 -0700705 mainView = createNewWebView(newTab.isPrivateBrowsingEnabled());
Steve Block2bc69912009-07-30 14:45:13 +0100706 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800707 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700708 newTab.putInForeground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800709 return true;
710 }
Michael Kolbfe251992010-07-08 15:41:55 -0700711
John Reck8ee633f2011-08-09 16:00:35 -0700712 public void setOnThumbnailUpdatedListener(OnThumbnailUpdatedListener listener) {
713 mOnThumbnailUpdatedListener = listener;
714 for (Tab t : mTabs) {
715 WebView web = t.getWebView();
716 if (web != null) {
717 web.setPictureListener(listener != null ? t : null);
718 }
719 }
720 }
721
722 public OnThumbnailUpdatedListener getOnThumbnailUpdatedListener() {
723 return mOnThumbnailUpdatedListener;
724 }
725
The Android Open Source Project0c908882009-03-03 19:32:16 -0800726}