blob: 2d90d231773fb5b15f273c803b6c9b23d4138a80 [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
Michael Kolb8233fac2010-10-26 16:08:53 -070019import com.android.browser.IntentHandler.UrlData;
20
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.WebBackForwardList;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024import android.webkit.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025
26import java.io.File;
27import java.util.ArrayList;
Elliott Slaughter3d6df162010-08-25 13:17:44 -070028import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080029import java.util.List;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import java.util.Vector;
31
32class TabControl {
33 // Log Tag
34 private static final String LOGTAG = "TabControl";
35 // Maximum number of tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070036 private int mMaxTabs;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037 // Private array of WebViews that are used as tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070038 private ArrayList<Tab> mTabs;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039 // Queue of most recently viewed tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070040 private ArrayList<Tab> mTabQueue;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041 // Current position in mTabs.
42 private int mCurrentTab = -1;
Michael Kolb8233fac2010-10-26 16:08:53 -070043 // the main browser controller
44 private final Controller mController;
45
The Android Open Source Project0c908882009-03-03 19:32:16 -080046 private final File mThumbnailDir;
47
48 /**
Michael Kolb8233fac2010-10-26 16:08:53 -070049 * Construct a new TabControl object
The Android Open Source Project0c908882009-03-03 19:32:16 -080050 */
Michael Kolb8233fac2010-10-26 16:08:53 -070051 TabControl(Controller controller) {
52 mController = controller;
53 mThumbnailDir = mController.getActivity()
54 .getDir("thumbnails", 0);
55 mMaxTabs = mController.getMaxTabs();
Michael Kolb6e4653e2010-09-27 16:22:38 -070056 mTabs = new ArrayList<Tab>(mMaxTabs);
57 mTabQueue = new ArrayList<Tab>(mMaxTabs);
The Android Open Source Project0c908882009-03-03 19:32:16 -080058 }
59
60 File getThumbnailDir() {
61 return mThumbnailDir;
62 }
63
Michael Kolb68775752010-08-19 12:42:01 -070064 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080065 * Return the current tab's main WebView. This will always return the main
66 * WebView for a given tab and not a subwindow.
67 * @return The current tab's WebView.
68 */
69 WebView getCurrentWebView() {
70 Tab t = getTab(mCurrentTab);
71 if (t == null) {
72 return null;
73 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070074 return t.getWebView();
Ben Murdochbff2d602009-07-01 20:19:05 +010075 }
76
77 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080078 * Return the current tab's top-level WebView. This can return a subwindow
79 * if one exists.
80 * @return The top-level WebView of the current tab.
81 */
82 WebView getCurrentTopWebView() {
83 Tab t = getTab(mCurrentTab);
84 if (t == null) {
85 return null;
86 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070087 return t.getTopWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -080088 }
89
90 /**
91 * Return the current tab's subwindow if it exists.
92 * @return The subwindow of the current tab or null if it doesn't exist.
93 */
94 WebView getCurrentSubWindow() {
95 Tab t = getTab(mCurrentTab);
96 if (t == null) {
97 return null;
98 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070099 return t.getSubWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800100 }
101
102 /**
Michael Kolb1bf23132010-11-19 12:55:12 -0800103 * return the list of tabs
104 */
105 List<Tab> getTabs() {
106 return mTabs;
107 }
108
109 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110 * Return the tab at the specified index.
111 * @return The Tab for the specified index or null if the tab does not
112 * exist.
113 */
114 Tab getTab(int index) {
115 if (index >= 0 && index < mTabs.size()) {
116 return mTabs.get(index);
117 }
118 return null;
119 }
120
121 /**
122 * Return the current tab.
123 * @return The current tab.
124 */
125 Tab getCurrentTab() {
126 return getTab(mCurrentTab);
127 }
128
129 /**
130 * Return the current tab index.
131 * @return The current tab index
132 */
133 int getCurrentIndex() {
134 return mCurrentTab;
135 }
Michael Kolbfe251992010-07-08 15:41:55 -0700136
The Android Open Source Project0c908882009-03-03 19:32:16 -0800137 /**
138 * Given a Tab, find it's index
139 * @param Tab to find
140 * @return index of Tab or -1 if not found
141 */
142 int getTabIndex(Tab tab) {
Patrick Scottae641ac2009-04-20 13:51:49 -0400143 if (tab == null) {
144 return -1;
145 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 return mTabs.indexOf(tab);
147 }
148
Grace Kloba22ac16e2009-10-07 18:00:23 -0700149 boolean canCreateNewTab() {
Michael Kolb6e4653e2010-09-27 16:22:38 -0700150 return mMaxTabs != mTabs.size();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700151 }
152
The Android Open Source Project0c908882009-03-03 19:32:16 -0800153 /**
Elliott Slaughtere440a882010-08-20 13:54:45 -0700154 * Returns true if there are any incognito tabs open.
155 * @return True when any incognito tabs are open, false otherwise.
156 */
157 boolean hasAnyOpenIncognitoTabs() {
158 for (Tab tab : mTabs) {
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700159 if (tab.getWebView() != null && tab.getWebView().isPrivateBrowsingEnabled()) {
Elliott Slaughtere440a882010-08-20 13:54:45 -0700160 return true;
161 }
162 }
163 return false;
164 }
165
166 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700167 * Create a new tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800168 * @return The newly createTab or null if we have reached the maximum
169 * number of open tabs.
170 */
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700171 Tab createNewTab(boolean closeOnExit, String appId, String url,
172 boolean privateBrowsing) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800173 int size = mTabs.size();
174 // Return false if we have maxed out on tabs
Michael Kolb6e4653e2010-09-27 16:22:38 -0700175 if (mMaxTabs == size) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800176 return null;
177 }
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700178 final WebView w = createNewWebView(privateBrowsing);
Steve Block2bc69912009-07-30 14:45:13 +0100179
The Android Open Source Project0c908882009-03-03 19:32:16 -0800180 // Create a new tab and add it to the tab list
Michael Kolb8233fac2010-10-26 16:08:53 -0700181 Tab t = new Tab(mController, w, closeOnExit, appId, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800182 mTabs.add(t);
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700183 // Initially put the tab in the background.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700184 t.putInBackground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800185 return t;
186 }
187
188 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700189 * Create a new tab with default values for closeOnExit(false),
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700190 * appId(null), url(null), and privateBrowsing(false).
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700191 */
192 Tab createNewTab() {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700193 return createNewTab(false, null, null, false);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700194 }
195
196 /**
Leon Scrogginsfde97462010-01-11 13:06:21 -0500197 * Remove the parent child relationships from all tabs.
198 */
199 void removeParentChildRelationShips() {
200 for (Tab tab : mTabs) {
201 tab.removeFromTree();
202 }
203 }
204
205 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800206 * Remove the tab from the list. If the tab is the current tab shown, the
207 * last created tab will be shown.
208 * @param t The tab to be removed.
209 */
210 boolean removeTab(Tab t) {
211 if (t == null) {
212 return false;
213 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700214
Patrick Scottd944d4d2010-01-27 16:39:11 -0500215 // Grab the current tab before modifying the list.
216 Tab current = getCurrentTab();
217
218 // Remove t from our list of tabs.
219 mTabs.remove(t);
220
221 // Put the tab in the background only if it is the current one.
222 if (current == t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700223 t.putInBackground();
224 mCurrentTab = -1;
Patrick Scottd944d4d2010-01-27 16:39:11 -0500225 } else {
226 // If a tab that is earlier in the list gets removed, the current
227 // index no longer points to the correct tab.
228 mCurrentTab = getTabIndex(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800229 }
230
Grace Kloba22ac16e2009-10-07 18:00:23 -0700231 // destroy the tab
232 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800233 // clear it's references to parent and children
234 t.removeFromTree();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800235
236 // The tab indices have shifted, update all the saved state so we point
237 // to the correct index.
238 for (Tab tab : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700239 Vector<Tab> children = tab.getChildTabs();
240 if (children != null) {
241 for (Tab child : children) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800242 child.setParentTab(tab);
243 }
244 }
245 }
246
The Android Open Source Project0c908882009-03-03 19:32:16 -0800247 // Remove it from the queue of viewed tabs.
248 mTabQueue.remove(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800249 return true;
250 }
251
252 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800253 * Destroy all the tabs and subwindows
254 */
255 void destroy() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800256 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700257 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800258 }
259 mTabs.clear();
260 mTabQueue.clear();
261 }
262
263 /**
264 * Returns the number of tabs created.
265 * @return The number of tabs created.
266 */
267 int getTabCount() {
268 return mTabs.size();
269 }
270
The Android Open Source Project0c908882009-03-03 19:32:16 -0800271
272 /**
273 * Save the state of all the Tabs.
274 * @param outState The Bundle to save the state to.
275 */
276 void saveState(Bundle outState) {
277 final int numTabs = getTabCount();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700278 outState.putInt(Tab.NUMTABS, numTabs);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800279 final int index = getCurrentIndex();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700280 outState.putInt(Tab.CURRTAB, (index >= 0 && index < numTabs) ? index : 0);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800281 for (int i = 0; i < numTabs; i++) {
282 final Tab t = getTab(i);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700283 if (t.saveState()) {
284 outState.putBundle(Tab.WEBVIEW + i, t.getSavedState());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800285 }
286 }
287 }
288
289 /**
290 * Restore the state of all the tabs.
291 * @param inState The saved state of all the tabs.
Michael Kolb1bf23132010-11-19 12:55:12 -0800292 * @param restoreIncognitoTabs Restoring private browsing tabs
293 * @param restoreAll All webviews get restored, not just the current tab
294 * (this does not override handling of incognito tabs)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800295 * @return True if there were previous tabs that were restored. False if
296 * there was no saved state or restoring the state failed.
297 */
Michael Kolb1bf23132010-11-19 12:55:12 -0800298 boolean restoreState(Bundle inState, boolean restoreIncognitoTabs,
299 boolean restoreAll) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800300 final int numTabs = (inState == null)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700301 ? -1 : inState.getInt(Tab.NUMTABS, -1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800302 if (numTabs == -1) {
303 return false;
304 } else {
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700305 final int oldCurrentTab = inState.getInt(Tab.CURRTAB, -1);
306
307 // Determine whether the saved current tab can be restored, and
308 // if not, which tab will take its place.
309 int currentTab = -1;
Michael Kolb1bf23132010-11-19 12:55:12 -0800310 if (restoreIncognitoTabs
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700311 || !inState.getBundle(Tab.WEBVIEW + oldCurrentTab).getBoolean(Tab.INCOGNITO)) {
312 currentTab = oldCurrentTab;
313 } else {
314 for (int i = 0; i < numTabs; i++) {
315 if (!inState.getBundle(Tab.WEBVIEW + i).getBoolean(Tab.INCOGNITO)) {
316 currentTab = i;
317 break;
318 }
319 }
320 }
321 if (currentTab < 0) {
322 return false;
323 }
324
325 // Map saved tab indices to new indices, in case any incognito tabs
326 // need to not be restored.
327 HashMap<Integer, Integer> originalTabIndices = new HashMap<Integer, Integer>();
328 originalTabIndices.put(-1, -1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800329 for (int i = 0; i < numTabs; i++) {
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700330 Bundle state = inState.getBundle(Tab.WEBVIEW + i);
331
Michael Kolb1bf23132010-11-19 12:55:12 -0800332 if (!restoreIncognitoTabs && state != null && state.getBoolean(Tab.INCOGNITO)) {
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700333 originalTabIndices.put(i, -1);
Michael Kolb1bf23132010-11-19 12:55:12 -0800334 } else if (i == currentTab || restoreAll) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700335 Tab t = createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800336 // Me must set the current tab before restoring the state
337 // so that all the client classes are set.
Michael Kolb1bf23132010-11-19 12:55:12 -0800338 if (i == currentTab) {
339 setCurrentTab(t);
340 }
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700341 if (!t.restoreState(state)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800342 Log.w(LOGTAG, "Fail in restoreState, load home page.");
Grace Kloba22ac16e2009-10-07 18:00:23 -0700343 t.getWebView().loadUrl(BrowserSettings.getInstance()
The Android Open Source Project0c908882009-03-03 19:32:16 -0800344 .getHomePage());
345 }
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700346 originalTabIndices.put(i, getTabCount() - 1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800347 } else {
348 // Create a new tab and don't restore the state yet, add it
349 // to the tab list
Michael Kolb8233fac2010-10-26 16:08:53 -0700350 Tab t = new Tab(mController, null, false, null, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700351 if (state != null) {
352 t.setSavedState(state);
353 t.populatePickerDataFromSavedState();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700354 // Need to maintain the app id and original url so we
355 // can possibly reuse this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700356 t.setAppId(state.getString(Tab.APPID));
357 t.setOriginalUrl(state.getString(Tab.ORIGINALURL));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800358 }
359 mTabs.add(t);
Grace Klobaf56f68d2010-04-11 22:53:42 -0700360 // added the tab to the front as they are not current
361 mTabQueue.add(0, t);
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700362 originalTabIndices.put(i, getTabCount() - 1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800363 }
364 }
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700365
The Android Open Source Project0c908882009-03-03 19:32:16 -0800366 // Rebuild the tree of tabs. Do this after all tabs have been
367 // created/restored so that the parent tab exists.
368 for (int i = 0; i < numTabs; i++) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700369 final Bundle b = inState.getBundle(Tab.WEBVIEW + i);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800370 final Tab t = getTab(i);
371 if (b != null && t != null) {
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700372 final Integer parentIndex = originalTabIndices.get(b.getInt(Tab.PARENTTAB, -1));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800373 if (parentIndex != -1) {
374 final Tab parent = getTab(parentIndex);
375 if (parent != null) {
376 parent.addChildTab(t);
377 }
378 }
379 }
380 }
381 }
382 return true;
383 }
384
385 /**
Grace Klobaf56f68d2010-04-11 22:53:42 -0700386 * Free the memory in this order, 1) free the background tabs; 2) free the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800387 * WebView cache;
388 */
389 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700390 if (getTabCount() == 0) return;
391
Grace Klobaf56f68d2010-04-11 22:53:42 -0700392 // free the least frequently used background tabs
393 Vector<Tab> tabs = getHalfLeastUsedTabs(getCurrentTab());
394 if (tabs.size() > 0) {
395 Log.w(LOGTAG, "Free " + tabs.size() + " tabs in the browser");
396 for (Tab t : tabs) {
397 // store the WebView's state.
398 t.saveState();
399 // destroy the tab
400 t.destroy();
401 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800402 return;
403 }
404
Derek Sollenberger4433d032009-06-10 15:37:21 -0400405 // free the WebView's unused memory (this includes the cache)
406 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800407 WebView view = getCurrentWebView();
408 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400409 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800410 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800411 }
412
Grace Klobaf56f68d2010-04-11 22:53:42 -0700413 private Vector<Tab> getHalfLeastUsedTabs(Tab current) {
414 Vector<Tab> tabsToGo = new Vector<Tab>();
415
Patrick Scott2a67de42009-08-31 09:48:37 -0400416 // Don't do anything if we only have 1 tab or if the current tab is
417 // null.
418 if (getTabCount() == 1 || current == null) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700419 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800420 }
421
Grace Klobaf56f68d2010-04-11 22:53:42 -0700422 if (mTabQueue.size() == 0) {
423 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800424 }
425
Grace Klobaf56f68d2010-04-11 22:53:42 -0700426 // Rip through the queue starting at the beginning and tear down half of
427 // available tabs which are not the current tab or the parent of the
428 // current tab.
429 int openTabCount = 0;
430 for (Tab t : mTabQueue) {
431 if (t != null && t.getWebView() != null) {
432 openTabCount++;
433 if (t != current && t != current.getParentTab()) {
434 tabsToGo.add(t);
435 }
436 }
437 }
438
439 openTabCount /= 2;
440 if (tabsToGo.size() > openTabCount) {
441 tabsToGo.setSize(openTabCount);
442 }
443
444 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800445 }
446
The Android Open Source Project0c908882009-03-03 19:32:16 -0800447 /**
448 * Show the tab that contains the given WebView.
449 * @param view The WebView used to find the tab.
450 */
451 Tab getTabFromView(WebView view) {
452 final int size = getTabCount();
453 for (int i = 0; i < size; i++) {
454 final Tab t = getTab(i);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700455 if (t.getSubWebView() == view || t.getWebView() == view) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800456 return t;
457 }
458 }
459 return null;
460 }
461
462 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700463 * Return the tab with the matching application id.
464 * @param id The application identifier.
465 */
466 Tab getTabFromId(String id) {
467 if (id == null) {
468 return null;
469 }
470 final int size = getTabCount();
471 for (int i = 0; i < size; i++) {
472 final Tab t = getTab(i);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700473 if (id.equals(t.getAppId())) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700474 return t;
475 }
476 }
477 return null;
478 }
479
Grace Kloba22ac16e2009-10-07 18:00:23 -0700480 /**
481 * Stop loading in all opened WebView including subWindows.
482 */
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700483 void stopAllLoading() {
484 final int size = getTabCount();
485 for (int i = 0; i < size; i++) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700486 final Tab t = getTab(i);
487 final WebView webview = t.getWebView();
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700488 if (webview != null) {
489 webview.stopLoading();
490 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700491 final WebView subview = t.getSubWebView();
492 if (subview != null) {
493 webview.stopLoading();
494 }
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700495 }
496 }
497
Patrick Scottcd115892009-07-16 09:42:58 -0400498 // This method checks if a non-app tab (one created within the browser)
499 // matches the given url.
500 private boolean tabMatchesUrl(Tab t, String url) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700501 if (t.getAppId() != null) {
Patrick Scottcd115892009-07-16 09:42:58 -0400502 return false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700503 }
504 WebView webview = t.getWebView();
505 if (webview == null) {
Patrick Scottcd115892009-07-16 09:42:58 -0400506 return false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700507 } else if (url.equals(webview.getUrl())
508 || url.equals(webview.getOriginalUrl())) {
Patrick Scottcd115892009-07-16 09:42:58 -0400509 return true;
510 }
511 return false;
512 }
513
514 /**
515 * Return the tab that has no app id associated with it and the url of the
516 * tab matches the given url.
517 * @param url The url to search for.
518 */
519 Tab findUnusedTabWithUrl(String url) {
520 if (url == null) {
521 return null;
522 }
523 // Check the current tab first.
524 Tab t = getCurrentTab();
525 if (t != null && tabMatchesUrl(t, url)) {
526 return t;
527 }
528 // Now check all the rest.
529 final int size = getTabCount();
530 for (int i = 0; i < size; i++) {
531 t = getTab(i);
532 if (tabMatchesUrl(t, url)) {
533 return t;
534 }
535 }
536 return null;
537 }
538
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700539 /**
540 * Recreate the main WebView of the given tab. Returns true if the WebView
Leon Scroggins6eac63e2010-03-15 18:19:14 -0400541 * requires a load, whether it was due to the fact that it was deleted, or
542 * it is because it was a voice search.
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700543 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700544 boolean recreateWebView(Tab t, UrlData urlData) {
Leon Scroggins6eac63e2010-03-15 18:19:14 -0400545 final String url = urlData.mUrl;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700546 final WebView w = t.getWebView();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700547 if (w != null) {
Leon Scroggins47208682010-04-07 17:59:48 -0400548 if (url != null && url.equals(t.getOriginalUrl())
549 // Treat a voice intent as though it is a different URL,
550 // since it most likely is.
551 && urlData.mVoiceIntent == null) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700552 // The original url matches the current url. Just go back to the
553 // first history item so we can load it faster than if we
554 // rebuilt the WebView.
555 final WebBackForwardList list = w.copyBackForwardList();
556 if (list != null) {
557 w.goBackOrForward(-list.getCurrentIndex());
558 w.clearHistory(); // maintains the current page.
559 return false;
560 }
561 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700562 t.destroy();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700563 }
564 // Create a new WebView. If this tab is the current tab, we need to put
565 // back all the clients so force it to be the current tab.
Steve Block2bc69912009-07-30 14:45:13 +0100566 t.setWebView(createNewWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700567 if (getCurrentTab() == t) {
568 setCurrentTab(t, true);
569 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700570 // Clear the saved state and picker data
571 t.setSavedState(null);
572 t.clearPickerData();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700573 // Save the new url in order to avoid deleting the WebView.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700574 t.setOriginalUrl(url);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700575 return true;
576 }
577
578 /**
579 * Creates a new WebView and registers it with the global settings.
580 */
581 private WebView createNewWebView() {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700582 return createNewWebView(false);
583 }
584
585 /**
586 * Creates a new WebView and registers it with the global settings.
587 * @param privateBrowsing When true, enables private browsing in the new
588 * WebView.
589 */
590 private WebView createNewWebView(boolean privateBrowsing) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700591 return mController.getWebViewFactory().createWebView(privateBrowsing);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700592 }
593
594 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800595 * Put the current tab in the background and set newTab as the current tab.
596 * @param newTab The new tab. If newTab is null, the current tab is not
597 * set.
598 */
599 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700600 return setCurrentTab(newTab, false);
601 }
602
Grace Kloba22ac16e2009-10-07 18:00:23 -0700603 void pauseCurrentTab() {
Mike Reed7bfa63b2009-05-28 11:08:32 -0400604 Tab t = getCurrentTab();
605 if (t != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700606 t.pause();
Mike Reed7bfa63b2009-05-28 11:08:32 -0400607 }
608 }
609
Grace Kloba22ac16e2009-10-07 18:00:23 -0700610 void resumeCurrentTab() {
Mike Reed7bfa63b2009-05-28 11:08:32 -0400611 Tab t = getCurrentTab();
612 if (t != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700613 t.resume();
Mike Reed7bfa63b2009-05-28 11:08:32 -0400614 }
615 }
616
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700617 /**
618 * If force is true, this method skips the check for newTab == current.
619 */
620 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800621 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700622 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800623 return true;
624 }
625 if (current != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700626 current.putInBackground();
627 mCurrentTab = -1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800628 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800629 if (newTab == null) {
630 return false;
631 }
632
633 // Move the newTab to the end of the queue
634 int index = mTabQueue.indexOf(newTab);
635 if (index != -1) {
636 mTabQueue.remove(index);
637 }
638 mTabQueue.add(newTab);
639
The Android Open Source Project0c908882009-03-03 19:32:16 -0800640 // Display the new current tab
641 mCurrentTab = mTabs.indexOf(newTab);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700642 WebView mainView = newTab.getWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800643 boolean needRestore = (mainView == null);
644 if (needRestore) {
645 // Same work as in createNewTab() except don't do new Tab()
Steve Block2bc69912009-07-30 14:45:13 +0100646 mainView = createNewWebView();
647 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800648 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700649 newTab.putInForeground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800650 if (needRestore) {
651 // Have to finish setCurrentTab work before calling restoreState
Grace Kloba22ac16e2009-10-07 18:00:23 -0700652 if (!newTab.restoreState(newTab.getSavedState())) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800653 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
654 }
655 }
656 return true;
657 }
Michael Kolbfe251992010-07-08 15:41:55 -0700658
The Android Open Source Project0c908882009-03-03 19:32:16 -0800659}