blob: 6566ac84be67f6654273a17cc2b7c7c331ab5d96 [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
John Reckd8c74522011-06-14 08:45:00 -070019import android.content.ContentResolver;
20import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.os.Bundle;
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.webkit.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024
John Reckd8c74522011-06-14 08:45:00 -070025import com.android.browser.provider.BrowserProvider2.Snapshots;
26
The Android Open Source Project0c908882009-03-03 19:32:16 -080027import java.io.File;
28import java.util.ArrayList;
Elliott Slaughter3d6df162010-08-25 13:17:44 -070029import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080030import java.util.List;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import java.util.Vector;
32
33class TabControl {
34 // Log Tag
35 private static final String LOGTAG = "TabControl";
Michael Kolbc831b632011-05-11 09:30:34 -070036
John Reckd8c74522011-06-14 08:45:00 -070037 // next Tab ID, starting at 1
38 private static long sNextId = 1;
Michael Kolbc831b632011-05-11 09:30:34 -070039
40 private static final String POSITIONS = "positions";
41 private static final String CURRENT = "current";
42
The Android Open Source Project0c908882009-03-03 19:32:16 -080043 // Maximum number of tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070044 private int mMaxTabs;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045 // Private array of WebViews that are used as tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070046 private ArrayList<Tab> mTabs;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047 // Queue of most recently viewed tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070048 private ArrayList<Tab> mTabQueue;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049 // Current position in mTabs.
50 private int mCurrentTab = -1;
Michael Kolb8233fac2010-10-26 16:08:53 -070051 // the main browser controller
52 private final Controller mController;
53
The Android Open Source Project0c908882009-03-03 19:32:16 -080054 private final File mThumbnailDir;
55
56 /**
Michael Kolb8233fac2010-10-26 16:08:53 -070057 * Construct a new TabControl object
The Android Open Source Project0c908882009-03-03 19:32:16 -080058 */
Michael Kolb8233fac2010-10-26 16:08:53 -070059 TabControl(Controller controller) {
60 mController = controller;
61 mThumbnailDir = mController.getActivity()
62 .getDir("thumbnails", 0);
63 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
Michael Kolbc831b632011-05-11 09:30:34 -070068 static long getNextId() {
69 return sNextId++;
70 }
71
The Android Open Source Project0c908882009-03-03 19:32:16 -080072 File getThumbnailDir() {
73 return mThumbnailDir;
74 }
75
Michael Kolb68775752010-08-19 12:42:01 -070076 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080077 * Return the current tab's main WebView. This will always return the main
78 * WebView for a given tab and not a subwindow.
79 * @return The current tab's WebView.
80 */
81 WebView getCurrentWebView() {
82 Tab t = getTab(mCurrentTab);
83 if (t == null) {
84 return null;
85 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070086 return t.getWebView();
Ben Murdochbff2d602009-07-01 20:19:05 +010087 }
88
89 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080090 * Return the current tab's top-level WebView. This can return a subwindow
91 * if one exists.
92 * @return The top-level WebView of the current tab.
93 */
94 WebView getCurrentTopWebView() {
95 Tab t = getTab(mCurrentTab);
96 if (t == null) {
97 return null;
98 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070099 return t.getTopWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800100 }
101
102 /**
103 * Return the current tab's subwindow if it exists.
104 * @return The subwindow of the current tab or null if it doesn't exist.
105 */
106 WebView getCurrentSubWindow() {
107 Tab t = getTab(mCurrentTab);
108 if (t == null) {
109 return null;
110 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700111 return t.getSubWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800112 }
113
114 /**
Michael Kolb1bf23132010-11-19 12:55:12 -0800115 * return the list of tabs
116 */
117 List<Tab> getTabs() {
118 return mTabs;
119 }
120
121 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700122 * Return the tab at the specified position.
123 * @return The Tab for the specified position or null if the tab does not
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124 * exist.
125 */
Michael Kolbc831b632011-05-11 09:30:34 -0700126 Tab getTab(int position) {
127 if (position >= 0 && position < mTabs.size()) {
128 return mTabs.get(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800129 }
130 return null;
131 }
132
133 /**
134 * Return the current tab.
135 * @return The current tab.
136 */
137 Tab getCurrentTab() {
138 return getTab(mCurrentTab);
139 }
140
141 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700142 * Return the current tab position.
143 * @return The current tab position
The Android Open Source Project0c908882009-03-03 19:32:16 -0800144 */
Michael Kolbc831b632011-05-11 09:30:34 -0700145 int getCurrentPosition() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 return mCurrentTab;
147 }
Michael Kolbfe251992010-07-08 15:41:55 -0700148
The Android Open Source Project0c908882009-03-03 19:32:16 -0800149 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700150 * Given a Tab, find it's position
The Android Open Source Project0c908882009-03-03 19:32:16 -0800151 * @param Tab to find
Michael Kolbc831b632011-05-11 09:30:34 -0700152 * @return position of Tab or -1 if not found
The Android Open Source Project0c908882009-03-03 19:32:16 -0800153 */
Michael Kolbc831b632011-05-11 09:30:34 -0700154 int getTabPosition(Tab tab) {
Patrick Scottae641ac2009-04-20 13:51:49 -0400155 if (tab == null) {
156 return -1;
157 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800158 return mTabs.indexOf(tab);
159 }
160
Grace Kloba22ac16e2009-10-07 18:00:23 -0700161 boolean canCreateNewTab() {
Michael Kolb6e4653e2010-09-27 16:22:38 -0700162 return mMaxTabs != mTabs.size();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700163 }
164
The Android Open Source Project0c908882009-03-03 19:32:16 -0800165 /**
Elliott Slaughtere440a882010-08-20 13:54:45 -0700166 * Returns true if there are any incognito tabs open.
167 * @return True when any incognito tabs are open, false otherwise.
168 */
169 boolean hasAnyOpenIncognitoTabs() {
170 for (Tab tab : mTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700171 if (tab.getWebView() != null
172 && tab.getWebView().isPrivateBrowsingEnabled()) {
Elliott Slaughtere440a882010-08-20 13:54:45 -0700173 return true;
174 }
175 }
176 return false;
177 }
178
179 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700180 * Create a new tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800181 * @return The newly createTab or null if we have reached the maximum
182 * number of open tabs.
183 */
Michael Kolb7bcafde2011-05-09 13:55:59 -0700184 Tab createNewTab(boolean privateBrowsing) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800185 int size = mTabs.size();
186 // Return false if we have maxed out on tabs
Michael Kolb6e4653e2010-09-27 16:22:38 -0700187 if (mMaxTabs == size) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800188 return null;
189 }
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700190 final WebView w = createNewWebView(privateBrowsing);
Steve Block2bc69912009-07-30 14:45:13 +0100191
The Android Open Source Project0c908882009-03-03 19:32:16 -0800192 // Create a new tab and add it to the tab list
Michael Kolb7bcafde2011-05-09 13:55:59 -0700193 Tab t = new Tab(mController, w);
Michael Kolbc831b632011-05-11 09:30:34 -0700194 t.setId(getNextId());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800195 mTabs.add(t);
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700196 // Initially put the tab in the background.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700197 t.putInBackground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800198 return t;
199 }
200
201 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700202 * Create a new tab with default values for closeOnExit(false),
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700203 * appId(null), url(null), and privateBrowsing(false).
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700204 */
205 Tab createNewTab() {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700206 return createNewTab(false);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700207 }
208
John Reckd8c74522011-06-14 08:45:00 -0700209 SnapshotTab createSnapshotTab(long snapshotId) {
210 // TODO: Don't count this against the limit
211 SnapshotTab t = new SnapshotTab(mController, snapshotId);
212 t.setId(getNextId());
213 mTabs.add(t);
214 return t;
215 }
216
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700217 /**
Leon Scrogginsfde97462010-01-11 13:06:21 -0500218 * Remove the parent child relationships from all tabs.
219 */
220 void removeParentChildRelationShips() {
221 for (Tab tab : mTabs) {
222 tab.removeFromTree();
223 }
224 }
225
226 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800227 * Remove the tab from the list. If the tab is the current tab shown, the
228 * last created tab will be shown.
229 * @param t The tab to be removed.
230 */
231 boolean removeTab(Tab t) {
232 if (t == null) {
233 return false;
234 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700235
Patrick Scottd944d4d2010-01-27 16:39:11 -0500236 // Grab the current tab before modifying the list.
237 Tab current = getCurrentTab();
238
239 // Remove t from our list of tabs.
240 mTabs.remove(t);
241
242 // Put the tab in the background only if it is the current one.
243 if (current == t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700244 t.putInBackground();
245 mCurrentTab = -1;
Patrick Scottd944d4d2010-01-27 16:39:11 -0500246 } else {
247 // If a tab that is earlier in the list gets removed, the current
248 // index no longer points to the correct tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700249 mCurrentTab = getTabPosition(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800250 }
251
Grace Kloba22ac16e2009-10-07 18:00:23 -0700252 // destroy the tab
253 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800254 // clear it's references to parent and children
255 t.removeFromTree();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800256
The Android Open Source Project0c908882009-03-03 19:32:16 -0800257 // Remove it from the queue of viewed tabs.
258 mTabQueue.remove(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800259 return true;
260 }
261
262 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800263 * Destroy all the tabs and subwindows
264 */
265 void destroy() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800266 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700267 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800268 }
269 mTabs.clear();
270 mTabQueue.clear();
271 }
272
273 /**
274 * Returns the number of tabs created.
275 * @return The number of tabs created.
276 */
277 int getTabCount() {
278 return mTabs.size();
279 }
280
The Android Open Source Project0c908882009-03-03 19:32:16 -0800281 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700282 * save the tab state:
283 * current position
284 * position sorted array of tab ids
285 * for each tab id, save the tab state
286 * @param outState
John Reckaed9c542011-05-27 16:08:53 -0700287 * @param saveImages
The Android Open Source Project0c908882009-03-03 19:32:16 -0800288 */
John Reckaed9c542011-05-27 16:08:53 -0700289 void saveState(Bundle outState, boolean saveImages) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800290 final int numTabs = getTabCount();
Michael Kolbc831b632011-05-11 09:30:34 -0700291 long[] ids = new long[numTabs];
292 int i = 0;
293 for (Tab tab : mTabs) {
294 ids[i++] = tab.getId();
295 if (tab.saveState()) {
John Reckaed9c542011-05-27 16:08:53 -0700296 outState.putBundle(Long.toString(tab.getId()),
297 tab.getSavedState(saveImages));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800298 }
299 }
Michael Kolbc831b632011-05-11 09:30:34 -0700300 outState.putLongArray(POSITIONS, ids);
Michael Kolbdbf39812011-06-10 14:06:40 -0700301 Tab current = getCurrentTab();
302 long cid = -1;
303 if (current != null) {
304 cid = current.getId();
305 }
Michael Kolbc831b632011-05-11 09:30:34 -0700306 outState.putLong(CURRENT, cid);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800307 }
308
309 /**
Patrick Scott7d50a932011-02-04 09:27:26 -0500310 * Check if the state can be restored. If the state can be restored, the
Michael Kolbc831b632011-05-11 09:30:34 -0700311 * current tab id is returned. This can be passed to restoreState below
Patrick Scott7d50a932011-02-04 09:27:26 -0500312 * in order to restore the correct tab. Otherwise, -1 is returned and the
313 * state cannot be restored.
314 */
Michael Kolbc831b632011-05-11 09:30:34 -0700315 long canRestoreState(Bundle inState, boolean restoreIncognitoTabs) {
316 final long[] ids = (inState == null) ? null : inState.getLongArray(POSITIONS);
317 if (ids == null) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500318 return -1;
319 }
Michael Kolbc831b632011-05-11 09:30:34 -0700320 final long oldcurrent = inState.getLong(CURRENT);
321 long current = -1;
Patrick Scott7d50a932011-02-04 09:27:26 -0500322 if (restoreIncognitoTabs ||
Michael Kolbc831b632011-05-11 09:30:34 -0700323 !inState.getBundle(Long.toString(oldcurrent)).getBoolean(Tab.INCOGNITO)) {
324 current = oldcurrent;
Patrick Scott7d50a932011-02-04 09:27:26 -0500325 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700326 // pick first non incognito tab
327 for (long id : ids) {
328 if (!inState.getBundle(Long.toString(id)).getBoolean(Tab.INCOGNITO)) {
329 current = id;
Patrick Scott7d50a932011-02-04 09:27:26 -0500330 break;
331 }
332 }
333 }
Michael Kolbc831b632011-05-11 09:30:34 -0700334 return current;
Patrick Scott7d50a932011-02-04 09:27:26 -0500335 }
336
337 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800338 * Restore the state of all the tabs.
Michael Kolbc831b632011-05-11 09:30:34 -0700339 * @param currentId The tab id to restore.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800340 * @param inState The saved state of all the tabs.
Michael Kolb1bf23132010-11-19 12:55:12 -0800341 * @param restoreIncognitoTabs Restoring private browsing tabs
342 * @param restoreAll All webviews get restored, not just the current tab
343 * (this does not override handling of incognito tabs)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800344 * @return True if there were previous tabs that were restored. False if
345 * there was no saved state or restoring the state failed.
346 */
Michael Kolbc831b632011-05-11 09:30:34 -0700347 void restoreState(Bundle inState, long currentId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500348 boolean restoreIncognitoTabs, boolean restoreAll) {
Michael Kolbc831b632011-05-11 09:30:34 -0700349 if (currentId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500350 return;
351 }
Michael Kolbc831b632011-05-11 09:30:34 -0700352 long[] ids = inState.getLongArray(POSITIONS);
353 long maxId = -Long.MAX_VALUE;
354 HashMap<Long, Tab> tabMap = new HashMap<Long, Tab>();
355 for (long id : ids) {
356 if (id > maxId) {
357 maxId = id;
358 }
359 final String idkey = Long.toString(id);
360 Bundle state = inState.getBundle(idkey);
John Reckd8c74522011-06-14 08:45:00 -0700361 if (state == null || state.isEmpty()) {
362 // Skip tab
363 continue;
364 } else if (!restoreIncognitoTabs
Michael Kolbc831b632011-05-11 09:30:34 -0700365 && state.getBoolean(Tab.INCOGNITO)) {
366 // ignore tab
367 } else if (id == currentId || restoreAll) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500368 Tab t = createNewTab();
Michael Kolbc831b632011-05-11 09:30:34 -0700369 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500370 // Me must set the current tab before restoring the state
371 // so that all the client classes are set.
Michael Kolbc831b632011-05-11 09:30:34 -0700372 if (id == currentId) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500373 setCurrentTab(t);
374 }
375 if (!t.restoreState(state)) {
376 Log.w(LOGTAG, "Fail in restoreState, load home page.");
377 t.getWebView().loadUrl(BrowserSettings.getInstance()
378 .getHomePage());
379 }
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700380 } else {
Patrick Scott7d50a932011-02-04 09:27:26 -0500381 // Create a new tab and don't restore the state yet, add it
382 // to the tab list
Michael Kolb7bcafde2011-05-09 13:55:59 -0700383 Tab t = new Tab(mController, null);
Michael Kolbc831b632011-05-11 09:30:34 -0700384 t.setId(id);
385 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500386 if (state != null) {
387 t.setSavedState(state);
388 // Need to maintain the app id and original url so we
389 // can possibly reuse this tab.
390 t.setAppId(state.getString(Tab.APPID));
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700391 }
Patrick Scott7d50a932011-02-04 09:27:26 -0500392 mTabs.add(t);
393 // added the tab to the front as they are not current
394 mTabQueue.add(0, t);
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700395 }
Michael Kolbc831b632011-05-11 09:30:34 -0700396 // make sure that there is no id overlap between the restored
397 // and new tabs
398 sNextId = maxId + 1;
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700399
Michael Kolbc831b632011-05-11 09:30:34 -0700400 }
John Reckd8c74522011-06-14 08:45:00 -0700401 if (mCurrentTab == -1) {
402 if (getTabCount() > 0) {
403 setCurrentTab(getTab(0));
404 } else {
405 Tab t = createNewTab();
406 setCurrentTab(t);
407 t.getWebView().loadUrl(BrowserSettings.getInstance()
408 .getHomePage());
409 }
410 }
Michael Kolbc831b632011-05-11 09:30:34 -0700411 // restore parent/child relationships
412 for (long id : ids) {
413 final Tab tab = tabMap.get(id);
414 final Bundle b = inState.getBundle(Long.toString(id));
415 if ((b != null) && (tab != null)) {
416 final long parentId = b.getLong(Tab.PARENTTAB, -1);
417 if (parentId != -1) {
418 final Tab parent = tabMap.get(parentId);
Patrick Scott7d50a932011-02-04 09:27:26 -0500419 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -0700420 parent.addChildTab(tab);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800421 }
422 }
423 }
424 }
John Reckd8c74522011-06-14 08:45:00 -0700425 loadSnapshotTabs();
426
427 }
428
429 void loadSnapshotTabs() {
430 ContentResolver cr = mController.getActivity().getContentResolver();
431 Cursor c = cr.query(Snapshots.CONTENT_URI, new String[] { "_id" },
432 null, null, null);
433 try {
434 while (c.moveToNext()) {
435 createSnapshotTab(c.getLong(0));
436 }
437 } finally {
438 c.close();
439 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800440 }
441
442 /**
Grace Klobaf56f68d2010-04-11 22:53:42 -0700443 * Free the memory in this order, 1) free the background tabs; 2) free the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800444 * WebView cache;
445 */
446 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700447 if (getTabCount() == 0) return;
448
Grace Klobaf56f68d2010-04-11 22:53:42 -0700449 // free the least frequently used background tabs
450 Vector<Tab> tabs = getHalfLeastUsedTabs(getCurrentTab());
451 if (tabs.size() > 0) {
452 Log.w(LOGTAG, "Free " + tabs.size() + " tabs in the browser");
453 for (Tab t : tabs) {
454 // store the WebView's state.
455 t.saveState();
456 // destroy the tab
457 t.destroy();
458 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800459 return;
460 }
461
Derek Sollenberger4433d032009-06-10 15:37:21 -0400462 // free the WebView's unused memory (this includes the cache)
463 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800464 WebView view = getCurrentWebView();
465 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400466 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800467 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800468 }
469
Grace Klobaf56f68d2010-04-11 22:53:42 -0700470 private Vector<Tab> getHalfLeastUsedTabs(Tab current) {
471 Vector<Tab> tabsToGo = new Vector<Tab>();
472
Patrick Scott2a67de42009-08-31 09:48:37 -0400473 // Don't do anything if we only have 1 tab or if the current tab is
474 // null.
475 if (getTabCount() == 1 || current == null) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700476 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800477 }
478
Grace Klobaf56f68d2010-04-11 22:53:42 -0700479 if (mTabQueue.size() == 0) {
480 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800481 }
482
Grace Klobaf56f68d2010-04-11 22:53:42 -0700483 // Rip through the queue starting at the beginning and tear down half of
484 // available tabs which are not the current tab or the parent of the
485 // current tab.
486 int openTabCount = 0;
487 for (Tab t : mTabQueue) {
488 if (t != null && t.getWebView() != null) {
489 openTabCount++;
Michael Kolbc831b632011-05-11 09:30:34 -0700490 if (t != current && t != current.getParent()) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700491 tabsToGo.add(t);
492 }
493 }
494 }
495
496 openTabCount /= 2;
497 if (tabsToGo.size() > openTabCount) {
498 tabsToGo.setSize(openTabCount);
499 }
500
501 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800502 }
503
The Android Open Source Project0c908882009-03-03 19:32:16 -0800504 /**
505 * Show the tab that contains the given WebView.
506 * @param view The WebView used to find the tab.
507 */
508 Tab getTabFromView(WebView view) {
Michael Kolbc831b632011-05-11 09:30:34 -0700509 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700510 if (t.getSubWebView() == view || t.getWebView() == view) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800511 return t;
512 }
513 }
514 return null;
515 }
516
517 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700518 * Return the tab with the matching application id.
519 * @param id The application identifier.
520 */
Michael Kolbc831b632011-05-11 09:30:34 -0700521 Tab getTabFromAppId(String id) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700522 if (id == null) {
523 return null;
524 }
Michael Kolbc831b632011-05-11 09:30:34 -0700525 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700526 if (id.equals(t.getAppId())) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700527 return t;
528 }
529 }
530 return null;
531 }
532
Grace Kloba22ac16e2009-10-07 18:00:23 -0700533 /**
534 * Stop loading in all opened WebView including subWindows.
535 */
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700536 void stopAllLoading() {
Michael Kolbc831b632011-05-11 09:30:34 -0700537 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700538 final WebView webview = t.getWebView();
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700539 if (webview != null) {
540 webview.stopLoading();
541 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700542 final WebView subview = t.getSubWebView();
543 if (subview != null) {
544 webview.stopLoading();
545 }
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700546 }
547 }
548
Patrick Scottcd115892009-07-16 09:42:58 -0400549 // This method checks if a non-app tab (one created within the browser)
550 // matches the given url.
551 private boolean tabMatchesUrl(Tab t, String url) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700552 if (t.getAppId() != null) {
Patrick Scottcd115892009-07-16 09:42:58 -0400553 return false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700554 }
555 WebView webview = t.getWebView();
556 if (webview == null) {
Patrick Scottcd115892009-07-16 09:42:58 -0400557 return false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700558 } else if (url.equals(webview.getUrl())
559 || url.equals(webview.getOriginalUrl())) {
Patrick Scottcd115892009-07-16 09:42:58 -0400560 return true;
561 }
562 return false;
563 }
564
565 /**
566 * Return the tab that has no app id associated with it and the url of the
567 * tab matches the given url.
568 * @param url The url to search for.
569 */
570 Tab findUnusedTabWithUrl(String url) {
571 if (url == null) {
572 return null;
573 }
574 // Check the current tab first.
575 Tab t = getCurrentTab();
576 if (t != null && tabMatchesUrl(t, url)) {
577 return t;
578 }
579 // Now check all the rest.
Michael Kolbc831b632011-05-11 09:30:34 -0700580 for (Tab tab : mTabs) {
581 if (tabMatchesUrl(tab, url)) {
Patrick Scottcd115892009-07-16 09:42:58 -0400582 return t;
583 }
584 }
585 return null;
586 }
587
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700588 /**
John Reck30c714c2010-12-16 17:30:34 -0800589 * Recreate the main WebView of the given tab.
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700590 */
John Reck30c714c2010-12-16 17:30:34 -0800591 void recreateWebView(Tab t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700592 final WebView w = t.getWebView();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700593 if (w != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700594 t.destroy();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700595 }
596 // Create a new WebView. If this tab is the current tab, we need to put
597 // back all the clients so force it to be the current tab.
Steve Block2bc69912009-07-30 14:45:13 +0100598 t.setWebView(createNewWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700599 if (getCurrentTab() == t) {
600 setCurrentTab(t, true);
601 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700602 // Clear the saved state and picker data
603 t.setSavedState(null);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700604 }
605
606 /**
607 * Creates a new WebView and registers it with the global settings.
608 */
609 private WebView createNewWebView() {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700610 return createNewWebView(false);
611 }
612
613 /**
614 * Creates a new WebView and registers it with the global settings.
615 * @param privateBrowsing When true, enables private browsing in the new
616 * WebView.
617 */
618 private WebView createNewWebView(boolean privateBrowsing) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700619 return mController.getWebViewFactory().createWebView(privateBrowsing);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700620 }
621
622 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800623 * Put the current tab in the background and set newTab as the current tab.
624 * @param newTab The new tab. If newTab is null, the current tab is not
625 * set.
626 */
627 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700628 return setCurrentTab(newTab, false);
629 }
630
631 /**
632 * If force is true, this method skips the check for newTab == current.
633 */
634 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800635 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700636 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800637 return true;
638 }
639 if (current != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700640 current.putInBackground();
641 mCurrentTab = -1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800642 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800643 if (newTab == null) {
644 return false;
645 }
646
647 // Move the newTab to the end of the queue
648 int index = mTabQueue.indexOf(newTab);
649 if (index != -1) {
650 mTabQueue.remove(index);
651 }
652 mTabQueue.add(newTab);
653
The Android Open Source Project0c908882009-03-03 19:32:16 -0800654 // Display the new current tab
655 mCurrentTab = mTabs.indexOf(newTab);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700656 WebView mainView = newTab.getWebView();
John Reckd8c74522011-06-14 08:45:00 -0700657 boolean needRestore = !newTab.isSnapshot() && (mainView == null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800658 if (needRestore) {
659 // Same work as in createNewTab() except don't do new Tab()
Steve Block2bc69912009-07-30 14:45:13 +0100660 mainView = createNewWebView();
661 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800662 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700663 newTab.putInForeground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800664 if (needRestore) {
665 // Have to finish setCurrentTab work before calling restoreState
Grace Kloba22ac16e2009-10-07 18:00:23 -0700666 if (!newTab.restoreState(newTab.getSavedState())) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800667 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
668 }
669 }
670 return true;
671 }
Michael Kolbfe251992010-07-08 15:41:55 -0700672
The Android Open Source Project0c908882009-03-03 19:32:16 -0800673}