blob: a1ebd722f4bd8ce355c505af6ed471ef6b32d609 [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070022import com.android.browser.reflect.ReflectHelper;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080023
24import org.codeaurora.swe.WebView;
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;
55
John Reck8ee633f2011-08-09 16:00:35 -070056 private OnThumbnailUpdatedListener mOnThumbnailUpdatedListener;
The Android Open Source Project0c908882009-03-03 19:32:16 -080057
58 /**
Michael Kolb8233fac2010-10-26 16:08:53 -070059 * Construct a new TabControl object
The Android Open Source Project0c908882009-03-03 19:32:16 -080060 */
Michael Kolb8233fac2010-10-26 16:08:53 -070061 TabControl(Controller controller) {
62 mController = controller;
Michael Kolb8233fac2010-10-26 16:08:53 -070063 mMaxTabs = mController.getMaxTabs();
Michael Kolb6e4653e2010-09-27 16:22:38 -070064 mTabs = new ArrayList<Tab>(mMaxTabs);
65 mTabQueue = new ArrayList<Tab>(mMaxTabs);
The Android Open Source Project0c908882009-03-03 19:32:16 -080066 }
67
John Reck52be4782011-08-26 15:37:29 -070068 synchronized static long getNextId() {
Michael Kolbc831b632011-05-11 09:30:34 -070069 return sNextId++;
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() {
Narayan Kamathc3ad9ea2011-07-26 15:20:21 +0100158 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) {
Mathew Inwoode09305e2011-09-02 12:03:26 +0100176 for (Tab current : mTabs) {
177 if (current != null && current.getId() == tab.getId()) {
178 throw new IllegalStateException("Tab with id " + tab.getId() + " already exists: "
179 + current.toString());
180 }
181 }
Michael Kolb14612442011-06-24 13:06:29 -0700182 mTabs.add(tab);
183 tab.setController(mController);
184 mController.onSetWebView(tab, tab.getWebView());
185 tab.putInBackground();
186 }
187
Elliott Slaughtere440a882010-08-20 13:54:45 -0700188 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700189 * Create a new tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 * @return The newly createTab or null if we have reached the maximum
191 * number of open tabs.
192 */
Michael Kolb7bcafde2011-05-09 13:55:59 -0700193 Tab createNewTab(boolean privateBrowsing) {
John Reck1cf4b792011-07-26 10:22:22 -0700194 return createNewTab(null, privateBrowsing);
195 }
196
197 Tab createNewTab(Bundle state, boolean privateBrowsing) {
198 int size = mTabs.size();
199 // Return false if we have maxed out on tabs
Narayan Kamathc3ad9ea2011-07-26 15:20:21 +0100200 if (!canCreateNewTab()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800201 return null;
202 }
Narayan Kamathc3ad9ea2011-07-26 15:20:21 +0100203
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700204 final WebView w = createNewWebView(privateBrowsing);
Steve Block2bc69912009-07-30 14:45:13 +0100205
The Android Open Source Project0c908882009-03-03 19:32:16 -0800206 // Create a new tab and add it to the tab list
John Reck1cf4b792011-07-26 10:22:22 -0700207 Tab t = new Tab(mController, w, state);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800208 mTabs.add(t);
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700209 // Initially put the tab in the background.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700210 t.putInBackground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800211 return t;
212 }
213
214 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700215 * Create a new tab with default values for closeOnExit(false),
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700216 * appId(null), url(null), and privateBrowsing(false).
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700217 */
218 Tab createNewTab() {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700219 return createNewTab(false);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700220 }
221
John Reckd8c74522011-06-14 08:45:00 -0700222 SnapshotTab createSnapshotTab(long snapshotId) {
John Reckd8c74522011-06-14 08:45:00 -0700223 SnapshotTab t = new SnapshotTab(mController, snapshotId);
John Reckd8c74522011-06-14 08:45:00 -0700224 mTabs.add(t);
225 return t;
226 }
227
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700228 /**
Leon Scrogginsfde97462010-01-11 13:06:21 -0500229 * Remove the parent child relationships from all tabs.
230 */
231 void removeParentChildRelationShips() {
232 for (Tab tab : mTabs) {
233 tab.removeFromTree();
234 }
235 }
236
237 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800238 * Remove the tab from the list. If the tab is the current tab shown, the
239 * last created tab will be shown.
240 * @param t The tab to be removed.
241 */
242 boolean removeTab(Tab t) {
243 if (t == null) {
244 return false;
245 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700246
Patrick Scottd944d4d2010-01-27 16:39:11 -0500247 // Grab the current tab before modifying the list.
248 Tab current = getCurrentTab();
249
250 // Remove t from our list of tabs.
251 mTabs.remove(t);
252
253 // Put the tab in the background only if it is the current one.
254 if (current == t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700255 t.putInBackground();
256 mCurrentTab = -1;
Patrick Scottd944d4d2010-01-27 16:39:11 -0500257 } else {
258 // If a tab that is earlier in the list gets removed, the current
259 // index no longer points to the correct tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700260 mCurrentTab = getTabPosition(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800261 }
262
Grace Kloba22ac16e2009-10-07 18:00:23 -0700263 // destroy the tab
264 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800265 // clear it's references to parent and children
266 t.removeFromTree();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800267
The Android Open Source Project0c908882009-03-03 19:32:16 -0800268 // Remove it from the queue of viewed tabs.
269 mTabQueue.remove(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800270 return true;
271 }
272
273 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800274 * Destroy all the tabs and subwindows
275 */
276 void destroy() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800277 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700278 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800279 }
280 mTabs.clear();
281 mTabQueue.clear();
282 }
283
284 /**
285 * Returns the number of tabs created.
286 * @return The number of tabs created.
287 */
288 int getTabCount() {
289 return mTabs.size();
290 }
291
The Android Open Source Project0c908882009-03-03 19:32:16 -0800292 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700293 * save the tab state:
294 * current position
295 * position sorted array of tab ids
296 * for each tab id, save the tab state
297 * @param outState
John Reckaed9c542011-05-27 16:08:53 -0700298 * @param saveImages
The Android Open Source Project0c908882009-03-03 19:32:16 -0800299 */
John Reck1cf4b792011-07-26 10:22:22 -0700300 void saveState(Bundle outState) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800301 final int numTabs = getTabCount();
John Reck24f18262011-06-17 14:47:20 -0700302 if (numTabs == 0) {
303 return;
304 }
Michael Kolbc831b632011-05-11 09:30:34 -0700305 long[] ids = new long[numTabs];
306 int i = 0;
307 for (Tab tab : mTabs) {
John Reck1cf4b792011-07-26 10:22:22 -0700308 Bundle tabState = tab.saveState();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800309 if (tabState != null && tab.getWebView() != null
310 && tab.getWebView().isPrivateBrowsingEnabled() == false) {
Michael Kolb3ae7f742011-07-13 15:18:25 -0700311 ids[i++] = tab.getId();
John Reck52be4782011-08-26 15:37:29 -0700312 String key = Long.toString(tab.getId());
313 if (outState.containsKey(key)) {
314 // Dump the tab state for debugging purposes
315 for (Tab dt : mTabs) {
316 Log.e(LOGTAG, dt.toString());
317 }
318 throw new IllegalStateException(
319 "Error saving state, duplicate tab ids!");
320 }
321 outState.putBundle(key, tabState);
Michael Kolb3ae7f742011-07-13 15:18:25 -0700322 } else {
323 ids[i++] = -1;
John Reck52be4782011-08-26 15:37:29 -0700324 // Since we won't be restoring the thumbnail, delete it
325 tab.deleteThumbnail();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800326 }
327 }
John Reck24f18262011-06-17 14:47:20 -0700328 if (!outState.isEmpty()) {
329 outState.putLongArray(POSITIONS, ids);
330 Tab current = getCurrentTab();
331 long cid = -1;
332 if (current != null) {
333 cid = current.getId();
334 }
335 outState.putLong(CURRENT, cid);
Michael Kolbdbf39812011-06-10 14:06:40 -0700336 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800337 }
338
339 /**
Patrick Scott7d50a932011-02-04 09:27:26 -0500340 * Check if the state can be restored. If the state can be restored, the
Michael Kolbc831b632011-05-11 09:30:34 -0700341 * current tab id is returned. This can be passed to restoreState below
Patrick Scott7d50a932011-02-04 09:27:26 -0500342 * in order to restore the correct tab. Otherwise, -1 is returned and the
343 * state cannot be restored.
344 */
Michael Kolbc831b632011-05-11 09:30:34 -0700345 long canRestoreState(Bundle inState, boolean restoreIncognitoTabs) {
346 final long[] ids = (inState == null) ? null : inState.getLongArray(POSITIONS);
347 if (ids == null) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500348 return -1;
349 }
Michael Kolbc831b632011-05-11 09:30:34 -0700350 final long oldcurrent = inState.getLong(CURRENT);
351 long current = -1;
Michael Kolb3ae7f742011-07-13 15:18:25 -0700352 if (restoreIncognitoTabs || (hasState(oldcurrent, inState) && !isIncognito(oldcurrent, inState))) {
John Reck1cf4b792011-07-26 10:22:22 -0700353 current = oldcurrent;
Patrick Scott7d50a932011-02-04 09:27:26 -0500354 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700355 // pick first non incognito tab
356 for (long id : ids) {
Michael Kolb3ae7f742011-07-13 15:18:25 -0700357 if (hasState(id, inState) && !isIncognito(id, inState)) {
Michael Kolbc831b632011-05-11 09:30:34 -0700358 current = id;
Patrick Scott7d50a932011-02-04 09:27:26 -0500359 break;
360 }
361 }
362 }
Michael Kolbc831b632011-05-11 09:30:34 -0700363 return current;
Patrick Scott7d50a932011-02-04 09:27:26 -0500364 }
365
Michael Kolb3ae7f742011-07-13 15:18:25 -0700366 private boolean hasState(long id, Bundle state) {
367 if (id == -1) return false;
368 Bundle tab = state.getBundle(Long.toString(id));
369 return ((tab != null) && !tab.isEmpty());
370 }
371
372 private boolean isIncognito(long id, Bundle state) {
373 Bundle tabstate = state.getBundle(Long.toString(id));
374 if ((tabstate != null) && !tabstate.isEmpty()) {
375 return tabstate.getBoolean(Tab.INCOGNITO);
376 }
377 return false;
378 }
379
Patrick Scott7d50a932011-02-04 09:27:26 -0500380 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800381 * Restore the state of all the tabs.
Michael Kolbc831b632011-05-11 09:30:34 -0700382 * @param currentId The tab id to restore.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800383 * @param inState The saved state of all the tabs.
Michael Kolb1bf23132010-11-19 12:55:12 -0800384 * @param restoreIncognitoTabs Restoring private browsing tabs
385 * @param restoreAll All webviews get restored, not just the current tab
386 * (this does not override handling of incognito tabs)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800387 */
Michael Kolbc831b632011-05-11 09:30:34 -0700388 void restoreState(Bundle inState, long currentId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500389 boolean restoreIncognitoTabs, boolean restoreAll) {
Michael Kolbc831b632011-05-11 09:30:34 -0700390 if (currentId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500391 return;
392 }
Michael Kolbc831b632011-05-11 09:30:34 -0700393 long[] ids = inState.getLongArray(POSITIONS);
394 long maxId = -Long.MAX_VALUE;
395 HashMap<Long, Tab> tabMap = new HashMap<Long, Tab>();
396 for (long id : ids) {
397 if (id > maxId) {
398 maxId = id;
399 }
400 final String idkey = Long.toString(id);
401 Bundle state = inState.getBundle(idkey);
John Reckd8c74522011-06-14 08:45:00 -0700402 if (state == null || state.isEmpty()) {
403 // Skip tab
404 continue;
405 } else if (!restoreIncognitoTabs
Michael Kolbc831b632011-05-11 09:30:34 -0700406 && state.getBoolean(Tab.INCOGNITO)) {
407 // ignore tab
408 } else if (id == currentId || restoreAll) {
John Reck1cf4b792011-07-26 10:22:22 -0700409 Tab t = createNewTab(state, false);
Narayan Kamath79e5d8d2011-07-20 14:12:38 +0100410 if (t == null) {
411 // We could "break" at this point, but we want
412 // sNextId to be set correctly.
413 continue;
414 }
qqzhou8c5b0a32013-07-22 15:31:03 +0800415
416 // add for carrier homepage feature
417 // If the webview restore successfully, add javascript interface again.
418 WebView view = t.getWebView();
419 if (view != null) {
Vivek Sekhar6f4f82a2014-03-21 19:24:51 -0700420 String browserRes = mController.getActivity().getApplicationContext()
421 .getResources().getString(R.string.config_carrier_resource);
qqzhou8c5b0a32013-07-22 15:31:03 +0800422 if ("ct".equals(browserRes)) {
423 view.getSettings().setJavaScriptEnabled(true);
424 if (mController.getActivity() instanceof BrowserActivity) {
425 view.addJavascriptInterface(mController.getActivity(),
426 "default_homepage");
427 }
428 }
429 }
430
Michael Kolbc831b632011-05-11 09:30:34 -0700431 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500432 // Me must set the current tab before restoring the state
433 // so that all the client classes are set.
Michael Kolbc831b632011-05-11 09:30:34 -0700434 if (id == currentId) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500435 setCurrentTab(t);
436 }
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700437 } else {
Patrick Scott7d50a932011-02-04 09:27:26 -0500438 // Create a new tab and don't restore the state yet, add it
439 // to the tab list
John Reck1cf4b792011-07-26 10:22:22 -0700440 Tab t = new Tab(mController, state);
Michael Kolbc831b632011-05-11 09:30:34 -0700441 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500442 mTabs.add(t);
443 // added the tab to the front as they are not current
444 mTabQueue.add(0, t);
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700445 }
Michael Kolbc831b632011-05-11 09:30:34 -0700446 }
Narayan Kamath79e5d8d2011-07-20 14:12:38 +0100447
448 // make sure that there is no id overlap between the restored
449 // and new tabs
450 sNextId = maxId + 1;
451
John Reckd8c74522011-06-14 08:45:00 -0700452 if (mCurrentTab == -1) {
453 if (getTabCount() > 0) {
454 setCurrentTab(getTab(0));
John Reckd8c74522011-06-14 08:45:00 -0700455 }
456 }
Michael Kolbc831b632011-05-11 09:30:34 -0700457 // restore parent/child relationships
458 for (long id : ids) {
459 final Tab tab = tabMap.get(id);
460 final Bundle b = inState.getBundle(Long.toString(id));
461 if ((b != null) && (tab != null)) {
462 final long parentId = b.getLong(Tab.PARENTTAB, -1);
463 if (parentId != -1) {
464 final Tab parent = tabMap.get(parentId);
Patrick Scott7d50a932011-02-04 09:27:26 -0500465 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -0700466 parent.addChildTab(tab);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800467 }
468 }
469 }
470 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800471 }
472
473 /**
Grace Klobaf56f68d2010-04-11 22:53:42 -0700474 * Free the memory in this order, 1) free the background tabs; 2) free the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800475 * WebView cache;
476 */
477 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700478 if (getTabCount() == 0) return;
479
Grace Klobaf56f68d2010-04-11 22:53:42 -0700480 // free the least frequently used background tabs
481 Vector<Tab> tabs = getHalfLeastUsedTabs(getCurrentTab());
482 if (tabs.size() > 0) {
483 Log.w(LOGTAG, "Free " + tabs.size() + " tabs in the browser");
484 for (Tab t : tabs) {
485 // store the WebView's state.
486 t.saveState();
487 // destroy the tab
488 t.destroy();
489 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800490 return;
491 }
492
Derek Sollenberger4433d032009-06-10 15:37:21 -0400493 // free the WebView's unused memory (this includes the cache)
494 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800495 WebView view = getCurrentWebView();
496 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400497 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800498 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800499 }
500
Grace Klobaf56f68d2010-04-11 22:53:42 -0700501 private Vector<Tab> getHalfLeastUsedTabs(Tab current) {
502 Vector<Tab> tabsToGo = new Vector<Tab>();
503
Patrick Scott2a67de42009-08-31 09:48:37 -0400504 // Don't do anything if we only have 1 tab or if the current tab is
505 // null.
506 if (getTabCount() == 1 || current == null) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700507 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800508 }
509
Grace Klobaf56f68d2010-04-11 22:53:42 -0700510 if (mTabQueue.size() == 0) {
511 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800512 }
513
Grace Klobaf56f68d2010-04-11 22:53:42 -0700514 // Rip through the queue starting at the beginning and tear down half of
515 // available tabs which are not the current tab or the parent of the
516 // current tab.
517 int openTabCount = 0;
518 for (Tab t : mTabQueue) {
519 if (t != null && t.getWebView() != null) {
520 openTabCount++;
Michael Kolbc831b632011-05-11 09:30:34 -0700521 if (t != current && t != current.getParent()) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700522 tabsToGo.add(t);
523 }
524 }
525 }
526
527 openTabCount /= 2;
528 if (tabsToGo.size() > openTabCount) {
529 tabsToGo.setSize(openTabCount);
530 }
531
532 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800533 }
534
Michael Kolbff6a7482011-07-26 16:37:15 -0700535 Tab getLeastUsedTab(Tab current) {
536 if (getTabCount() == 1 || current == null) {
537 return null;
538 }
539 if (mTabQueue.size() == 0) {
540 return null;
541 }
542 // find a tab which is not the current tab or the parent of the
543 // current tab
544 for (Tab t : mTabQueue) {
545 if (t != null && t.getWebView() != null) {
546 if (t != current && t != current.getParent()) {
547 return t;
548 }
549 }
550 }
551 return null;
552 }
553
The Android Open Source Project0c908882009-03-03 19:32:16 -0800554 /**
555 * Show the tab that contains the given WebView.
556 * @param view The WebView used to find the tab.
557 */
558 Tab getTabFromView(WebView view) {
Michael Kolbc831b632011-05-11 09:30:34 -0700559 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700560 if (t.getSubWebView() == view || t.getWebView() == view) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800561 return t;
562 }
563 }
564 return null;
565 }
566
567 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700568 * Return the tab with the matching application id.
569 * @param id The application identifier.
570 */
Michael Kolbc831b632011-05-11 09:30:34 -0700571 Tab getTabFromAppId(String id) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700572 if (id == null) {
573 return null;
574 }
Michael Kolbc831b632011-05-11 09:30:34 -0700575 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700576 if (id.equals(t.getAppId())) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700577 return t;
578 }
579 }
580 return null;
581 }
582
Grace Kloba22ac16e2009-10-07 18:00:23 -0700583 /**
584 * Stop loading in all opened WebView including subWindows.
585 */
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700586 void stopAllLoading() {
Michael Kolbc831b632011-05-11 09:30:34 -0700587 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700588 final WebView webview = t.getWebView();
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700589 if (webview != null) {
590 webview.stopLoading();
591 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700592 final WebView subview = t.getSubWebView();
593 if (subview != null) {
Mattias Nilsson9727af82012-06-14 17:07:56 +0200594 subview.stopLoading();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700595 }
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700596 }
597 }
598
John Reckdb22ec42011-06-29 11:31:24 -0700599 // This method checks if a tab matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400600 private boolean tabMatchesUrl(Tab t, String url) {
John Reckdb22ec42011-06-29 11:31:24 -0700601 return url.equals(t.getUrl()) || url.equals(t.getOriginalUrl());
Patrick Scottcd115892009-07-16 09:42:58 -0400602 }
603
604 /**
John Reckdb22ec42011-06-29 11:31:24 -0700605 * Return the tab that matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400606 * @param url The url to search for.
607 */
John Reckdb22ec42011-06-29 11:31:24 -0700608 Tab findTabWithUrl(String url) {
Patrick Scottcd115892009-07-16 09:42:58 -0400609 if (url == null) {
610 return null;
611 }
612 // Check the current tab first.
John Reckdb22ec42011-06-29 11:31:24 -0700613 Tab currentTab = getCurrentTab();
614 if (currentTab != null && tabMatchesUrl(currentTab, url)) {
615 return currentTab;
Patrick Scottcd115892009-07-16 09:42:58 -0400616 }
617 // Now check all the rest.
Michael Kolbc831b632011-05-11 09:30:34 -0700618 for (Tab tab : mTabs) {
619 if (tabMatchesUrl(tab, url)) {
John Reckdb22ec42011-06-29 11:31:24 -0700620 return tab;
Patrick Scottcd115892009-07-16 09:42:58 -0400621 }
622 }
623 return null;
624 }
625
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700626 /**
John Reck30c714c2010-12-16 17:30:34 -0800627 * Recreate the main WebView of the given tab.
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700628 */
John Reck30c714c2010-12-16 17:30:34 -0800629 void recreateWebView(Tab t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700630 final WebView w = t.getWebView();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700631 if (w != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700632 t.destroy();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700633 }
634 // Create a new WebView. If this tab is the current tab, we need to put
635 // back all the clients so force it to be the current tab.
Panos Thomasa9a5a582014-03-18 19:20:08 -0700636 t.setWebView(createNewWebView(t.isPrivateBrowsingEnabled()), false);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700637 if (getCurrentTab() == t) {
638 setCurrentTab(t, true);
639 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700640 }
641
642 /**
643 * Creates a new WebView and registers it with the global settings.
644 */
645 private WebView createNewWebView() {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700646 return createNewWebView(false);
647 }
648
649 /**
650 * Creates a new WebView and registers it with the global settings.
651 * @param privateBrowsing When true, enables private browsing in the new
652 * WebView.
653 */
654 private WebView createNewWebView(boolean privateBrowsing) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700655 return mController.getWebViewFactory().createWebView(privateBrowsing);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700656 }
657
658 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800659 * Put the current tab in the background and set newTab as the current tab.
660 * @param newTab The new tab. If newTab is null, the current tab is not
661 * set.
662 */
663 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700664 return setCurrentTab(newTab, false);
665 }
666
667 /**
668 * If force is true, this method skips the check for newTab == current.
669 */
670 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800671 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700672 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800673 return true;
674 }
675 if (current != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700676 current.putInBackground();
677 mCurrentTab = -1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800678 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800679 if (newTab == null) {
680 return false;
681 }
682
683 // Move the newTab to the end of the queue
684 int index = mTabQueue.indexOf(newTab);
685 if (index != -1) {
686 mTabQueue.remove(index);
687 }
688 mTabQueue.add(newTab);
689
The Android Open Source Project0c908882009-03-03 19:32:16 -0800690 // Display the new current tab
691 mCurrentTab = mTabs.indexOf(newTab);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700692 WebView mainView = newTab.getWebView();
John Reckd8c74522011-06-14 08:45:00 -0700693 boolean needRestore = !newTab.isSnapshot() && (mainView == null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800694 if (needRestore) {
695 // Same work as in createNewTab() except don't do new Tab()
Panos Thomasa9a5a582014-03-18 19:20:08 -0700696 mainView = createNewWebView(newTab.isPrivateBrowsingEnabled());
Steve Block2bc69912009-07-30 14:45:13 +0100697 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800698 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700699 newTab.putInForeground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800700 return true;
701 }
Michael Kolbfe251992010-07-08 15:41:55 -0700702
John Reck8ee633f2011-08-09 16:00:35 -0700703 public void setOnThumbnailUpdatedListener(OnThumbnailUpdatedListener listener) {
704 mOnThumbnailUpdatedListener = listener;
705 for (Tab t : mTabs) {
706 WebView web = t.getWebView();
707 if (web != null) {
708 web.setPictureListener(listener != null ? t : null);
709 }
710 }
711 }
712
713 public OnThumbnailUpdatedListener getOnThumbnailUpdatedListener() {
714 return mOnThumbnailUpdatedListener;
715 }
716
The Android Open Source Project0c908882009-03-03 19:32:16 -0800717}