The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 19 | import android.os.Bundle; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 20 | import android.util.Log; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 21 | import android.webkit.WebView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 22 | |
| 23 | import java.io.File; |
| 24 | import java.util.ArrayList; |
Elliott Slaughter | 3d6df16 | 2010-08-25 13:17:44 -0700 | [diff] [blame] | 25 | import java.util.HashMap; |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 26 | import java.util.List; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 27 | import java.util.Vector; |
| 28 | |
| 29 | class TabControl { |
| 30 | // Log Tag |
| 31 | private static final String LOGTAG = "TabControl"; |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 32 | |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 33 | // next Tab ID, starting at 1 |
| 34 | private static long sNextId = 1; |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 35 | |
| 36 | private static final String POSITIONS = "positions"; |
| 37 | private static final String CURRENT = "current"; |
| 38 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 39 | // Maximum number of tabs. |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 40 | private int mMaxTabs; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 41 | // Private array of WebViews that are used as tabs. |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 42 | private ArrayList<Tab> mTabs; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 43 | // Queue of most recently viewed tabs. |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 44 | private ArrayList<Tab> mTabQueue; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 45 | // Current position in mTabs. |
| 46 | private int mCurrentTab = -1; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 47 | // the main browser controller |
| 48 | private final Controller mController; |
| 49 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 50 | private final File mThumbnailDir; |
| 51 | |
| 52 | /** |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 53 | * Construct a new TabControl object |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 54 | */ |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 55 | TabControl(Controller controller) { |
| 56 | mController = controller; |
| 57 | mThumbnailDir = mController.getActivity() |
| 58 | .getDir("thumbnails", 0); |
| 59 | mMaxTabs = mController.getMaxTabs(); |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 60 | mTabs = new ArrayList<Tab>(mMaxTabs); |
| 61 | mTabQueue = new ArrayList<Tab>(mMaxTabs); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 64 | static long getNextId() { |
| 65 | return sNextId++; |
| 66 | } |
| 67 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 68 | File getThumbnailDir() { |
| 69 | return mThumbnailDir; |
| 70 | } |
| 71 | |
Michael Kolb | 6877575 | 2010-08-19 12:42:01 -0700 | [diff] [blame] | 72 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 73 | * 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 Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 82 | return t.getWebView(); |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 86 | * 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 Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 95 | return t.getTopWindow(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 96 | } |
| 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 Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 107 | return t.getSubWebView(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 111 | * return the list of tabs |
| 112 | */ |
| 113 | List<Tab> getTabs() { |
| 114 | return mTabs; |
| 115 | } |
| 116 | |
| 117 | /** |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 118 | * 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 120 | * exist. |
| 121 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 122 | Tab getTab(int position) { |
| 123 | if (position >= 0 && position < mTabs.size()) { |
| 124 | return mTabs.get(position); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 125 | } |
| 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 Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 138 | * Return the current tab position. |
| 139 | * @return The current tab position |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 140 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 141 | int getCurrentPosition() { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 142 | return mCurrentTab; |
| 143 | } |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 144 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 145 | /** |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 146 | * Given a Tab, find it's position |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 147 | * @param Tab to find |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 148 | * @return position of Tab or -1 if not found |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 149 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 150 | int getTabPosition(Tab tab) { |
Patrick Scott | ae641ac | 2009-04-20 13:51:49 -0400 | [diff] [blame] | 151 | if (tab == null) { |
| 152 | return -1; |
| 153 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 154 | return mTabs.indexOf(tab); |
| 155 | } |
| 156 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 157 | boolean canCreateNewTab() { |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 158 | return mMaxTabs != mTabs.size(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 159 | } |
| 160 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 161 | /** |
Elliott Slaughter | e440a88 | 2010-08-20 13:54:45 -0700 | [diff] [blame] | 162 | * 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 Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 167 | if (tab.getWebView() != null |
| 168 | && tab.getWebView().isPrivateBrowsingEnabled()) { |
Elliott Slaughter | e440a88 | 2010-08-20 13:54:45 -0700 | [diff] [blame] | 169 | return true; |
| 170 | } |
| 171 | } |
| 172 | return false; |
| 173 | } |
| 174 | |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 175 | void addPreloadedTab(Tab tab) { |
| 176 | tab.setId(getNextId()); |
| 177 | mTabs.add(tab); |
| 178 | tab.setController(mController); |
| 179 | mController.onSetWebView(tab, tab.getWebView()); |
| 180 | tab.putInBackground(); |
| 181 | } |
| 182 | |
Elliott Slaughter | e440a88 | 2010-08-20 13:54:45 -0700 | [diff] [blame] | 183 | /** |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 184 | * Create a new tab. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 185 | * @return The newly createTab or null if we have reached the maximum |
| 186 | * number of open tabs. |
| 187 | */ |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 188 | Tab createNewTab(boolean privateBrowsing) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 189 | int size = mTabs.size(); |
| 190 | // Return false if we have maxed out on tabs |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 191 | if (mMaxTabs == size) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 192 | return null; |
| 193 | } |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 194 | final WebView w = createNewWebView(privateBrowsing); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 195 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 196 | // Create a new tab and add it to the tab list |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 197 | Tab t = new Tab(mController, w); |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 198 | t.setId(getNextId()); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 199 | mTabs.add(t); |
The Android Open Source Project | 4e5f587 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 200 | // Initially put the tab in the background. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 201 | t.putInBackground(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 202 | return t; |
| 203 | } |
| 204 | |
| 205 | /** |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 206 | * Create a new tab with default values for closeOnExit(false), |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 207 | * appId(null), url(null), and privateBrowsing(false). |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 208 | */ |
| 209 | Tab createNewTab() { |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 210 | return createNewTab(false); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 211 | } |
| 212 | |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 213 | SnapshotTab createSnapshotTab(long snapshotId) { |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 214 | SnapshotTab t = new SnapshotTab(mController, snapshotId); |
| 215 | t.setId(getNextId()); |
| 216 | mTabs.add(t); |
| 217 | return t; |
| 218 | } |
| 219 | |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 220 | /** |
Leon Scroggins | fde9746 | 2010-01-11 13:06:21 -0500 | [diff] [blame] | 221 | * Remove the parent child relationships from all tabs. |
| 222 | */ |
| 223 | void removeParentChildRelationShips() { |
| 224 | for (Tab tab : mTabs) { |
| 225 | tab.removeFromTree(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 230 | * Remove the tab from the list. If the tab is the current tab shown, the |
| 231 | * last created tab will be shown. |
| 232 | * @param t The tab to be removed. |
| 233 | */ |
| 234 | boolean removeTab(Tab t) { |
| 235 | if (t == null) { |
| 236 | return false; |
| 237 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 238 | |
Patrick Scott | d944d4d | 2010-01-27 16:39:11 -0500 | [diff] [blame] | 239 | // Grab the current tab before modifying the list. |
| 240 | Tab current = getCurrentTab(); |
| 241 | |
| 242 | // Remove t from our list of tabs. |
| 243 | mTabs.remove(t); |
| 244 | |
| 245 | // Put the tab in the background only if it is the current one. |
| 246 | if (current == t) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 247 | t.putInBackground(); |
| 248 | mCurrentTab = -1; |
Patrick Scott | d944d4d | 2010-01-27 16:39:11 -0500 | [diff] [blame] | 249 | } else { |
| 250 | // If a tab that is earlier in the list gets removed, the current |
| 251 | // index no longer points to the correct tab. |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 252 | mCurrentTab = getTabPosition(current); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 255 | // destroy the tab |
| 256 | t.destroy(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 257 | // clear it's references to parent and children |
| 258 | t.removeFromTree(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 259 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 260 | // Remove it from the queue of viewed tabs. |
| 261 | mTabQueue.remove(t); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 262 | return true; |
| 263 | } |
| 264 | |
| 265 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 266 | * Destroy all the tabs and subwindows |
| 267 | */ |
| 268 | void destroy() { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 269 | for (Tab t : mTabs) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 270 | t.destroy(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 271 | } |
| 272 | mTabs.clear(); |
| 273 | mTabQueue.clear(); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Returns the number of tabs created. |
| 278 | * @return The number of tabs created. |
| 279 | */ |
| 280 | int getTabCount() { |
| 281 | return mTabs.size(); |
| 282 | } |
| 283 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 284 | /** |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 285 | * save the tab state: |
| 286 | * current position |
| 287 | * position sorted array of tab ids |
| 288 | * for each tab id, save the tab state |
| 289 | * @param outState |
John Reck | aed9c54 | 2011-05-27 16:08:53 -0700 | [diff] [blame] | 290 | * @param saveImages |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 291 | */ |
John Reck | aed9c54 | 2011-05-27 16:08:53 -0700 | [diff] [blame] | 292 | void saveState(Bundle outState, boolean saveImages) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 293 | final int numTabs = getTabCount(); |
John Reck | 24f1826 | 2011-06-17 14:47:20 -0700 | [diff] [blame] | 294 | if (numTabs == 0) { |
| 295 | return; |
| 296 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 297 | long[] ids = new long[numTabs]; |
| 298 | int i = 0; |
| 299 | for (Tab tab : mTabs) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 300 | if (tab.saveState()) { |
Michael Kolb | 3ae7f74 | 2011-07-13 15:18:25 -0700 | [diff] [blame] | 301 | ids[i++] = tab.getId(); |
John Reck | aed9c54 | 2011-05-27 16:08:53 -0700 | [diff] [blame] | 302 | outState.putBundle(Long.toString(tab.getId()), |
| 303 | tab.getSavedState(saveImages)); |
Michael Kolb | 3ae7f74 | 2011-07-13 15:18:25 -0700 | [diff] [blame] | 304 | } else { |
| 305 | ids[i++] = -1; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 306 | } |
| 307 | } |
John Reck | 24f1826 | 2011-06-17 14:47:20 -0700 | [diff] [blame] | 308 | if (!outState.isEmpty()) { |
| 309 | outState.putLongArray(POSITIONS, ids); |
| 310 | Tab current = getCurrentTab(); |
| 311 | long cid = -1; |
| 312 | if (current != null) { |
| 313 | cid = current.getId(); |
| 314 | } |
| 315 | outState.putLong(CURRENT, cid); |
Michael Kolb | dbf3981 | 2011-06-10 14:06:40 -0700 | [diff] [blame] | 316 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /** |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 320 | * Check if the state can be restored. If the state can be restored, the |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 321 | * current tab id is returned. This can be passed to restoreState below |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 322 | * in order to restore the correct tab. Otherwise, -1 is returned and the |
| 323 | * state cannot be restored. |
| 324 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 325 | long canRestoreState(Bundle inState, boolean restoreIncognitoTabs) { |
| 326 | final long[] ids = (inState == null) ? null : inState.getLongArray(POSITIONS); |
| 327 | if (ids == null) { |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 328 | return -1; |
| 329 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 330 | final long oldcurrent = inState.getLong(CURRENT); |
| 331 | long current = -1; |
Michael Kolb | 3ae7f74 | 2011-07-13 15:18:25 -0700 | [diff] [blame] | 332 | if (restoreIncognitoTabs || (hasState(oldcurrent, inState) && !isIncognito(oldcurrent, inState))) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 333 | current = oldcurrent; |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 334 | } else { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 335 | // pick first non incognito tab |
| 336 | for (long id : ids) { |
Michael Kolb | 3ae7f74 | 2011-07-13 15:18:25 -0700 | [diff] [blame] | 337 | if (hasState(id, inState) && !isIncognito(id, inState)) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 338 | current = id; |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | } |
| 342 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 343 | return current; |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 344 | } |
| 345 | |
Michael Kolb | 3ae7f74 | 2011-07-13 15:18:25 -0700 | [diff] [blame] | 346 | private boolean hasState(long id, Bundle state) { |
| 347 | if (id == -1) return false; |
| 348 | Bundle tab = state.getBundle(Long.toString(id)); |
| 349 | return ((tab != null) && !tab.isEmpty()); |
| 350 | } |
| 351 | |
| 352 | private boolean isIncognito(long id, Bundle state) { |
| 353 | Bundle tabstate = state.getBundle(Long.toString(id)); |
| 354 | if ((tabstate != null) && !tabstate.isEmpty()) { |
| 355 | return tabstate.getBoolean(Tab.INCOGNITO); |
| 356 | } |
| 357 | return false; |
| 358 | } |
| 359 | |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 360 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 361 | * Restore the state of all the tabs. |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 362 | * @param currentId The tab id to restore. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 363 | * @param inState The saved state of all the tabs. |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 364 | * @param restoreIncognitoTabs Restoring private browsing tabs |
| 365 | * @param restoreAll All webviews get restored, not just the current tab |
| 366 | * (this does not override handling of incognito tabs) |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 367 | * @return True if there were previous tabs that were restored. False if |
| 368 | * there was no saved state or restoring the state failed. |
| 369 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 370 | void restoreState(Bundle inState, long currentId, |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 371 | boolean restoreIncognitoTabs, boolean restoreAll) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 372 | if (currentId == -1) { |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 373 | return; |
| 374 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 375 | long[] ids = inState.getLongArray(POSITIONS); |
| 376 | long maxId = -Long.MAX_VALUE; |
| 377 | HashMap<Long, Tab> tabMap = new HashMap<Long, Tab>(); |
| 378 | for (long id : ids) { |
| 379 | if (id > maxId) { |
| 380 | maxId = id; |
| 381 | } |
| 382 | final String idkey = Long.toString(id); |
| 383 | Bundle state = inState.getBundle(idkey); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 384 | if (state == null || state.isEmpty()) { |
| 385 | // Skip tab |
| 386 | continue; |
| 387 | } else if (!restoreIncognitoTabs |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 388 | && state.getBoolean(Tab.INCOGNITO)) { |
| 389 | // ignore tab |
| 390 | } else if (id == currentId || restoreAll) { |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 391 | Tab t = createNewTab(); |
Narayan Kamath | 79e5d8d | 2011-07-20 14:12:38 +0100 | [diff] [blame] | 392 | if (t == null) { |
| 393 | // We could "break" at this point, but we want |
| 394 | // sNextId to be set correctly. |
| 395 | continue; |
| 396 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 397 | tabMap.put(id, t); |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 398 | // Me must set the current tab before restoring the state |
| 399 | // so that all the client classes are set. |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 400 | if (id == currentId) { |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 401 | setCurrentTab(t); |
| 402 | } |
| 403 | if (!t.restoreState(state)) { |
| 404 | Log.w(LOGTAG, "Fail in restoreState, load home page."); |
| 405 | t.getWebView().loadUrl(BrowserSettings.getInstance() |
| 406 | .getHomePage()); |
| 407 | } |
Elliott Slaughter | 3d6df16 | 2010-08-25 13:17:44 -0700 | [diff] [blame] | 408 | } else { |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 409 | // Create a new tab and don't restore the state yet, add it |
| 410 | // to the tab list |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 411 | Tab t = new Tab(mController, null); |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 412 | t.setId(id); |
| 413 | tabMap.put(id, t); |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 414 | if (state != null) { |
| 415 | t.setSavedState(state); |
| 416 | // Need to maintain the app id and original url so we |
| 417 | // can possibly reuse this tab. |
| 418 | t.setAppId(state.getString(Tab.APPID)); |
Elliott Slaughter | 3d6df16 | 2010-08-25 13:17:44 -0700 | [diff] [blame] | 419 | } |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 420 | mTabs.add(t); |
| 421 | // added the tab to the front as they are not current |
| 422 | mTabQueue.add(0, t); |
Elliott Slaughter | 3d6df16 | 2010-08-25 13:17:44 -0700 | [diff] [blame] | 423 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 424 | } |
Narayan Kamath | 79e5d8d | 2011-07-20 14:12:38 +0100 | [diff] [blame] | 425 | |
| 426 | // make sure that there is no id overlap between the restored |
| 427 | // and new tabs |
| 428 | sNextId = maxId + 1; |
| 429 | |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 430 | if (mCurrentTab == -1) { |
| 431 | if (getTabCount() > 0) { |
| 432 | setCurrentTab(getTab(0)); |
| 433 | } else { |
| 434 | Tab t = createNewTab(); |
| 435 | setCurrentTab(t); |
| 436 | t.getWebView().loadUrl(BrowserSettings.getInstance() |
| 437 | .getHomePage()); |
| 438 | } |
| 439 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 440 | // restore parent/child relationships |
| 441 | for (long id : ids) { |
| 442 | final Tab tab = tabMap.get(id); |
| 443 | final Bundle b = inState.getBundle(Long.toString(id)); |
| 444 | if ((b != null) && (tab != null)) { |
| 445 | final long parentId = b.getLong(Tab.PARENTTAB, -1); |
| 446 | if (parentId != -1) { |
| 447 | final Tab parent = tabMap.get(parentId); |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 448 | if (parent != null) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 449 | parent.addChildTab(tab); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | } |
| 453 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /** |
Grace Kloba | f56f68d | 2010-04-11 22:53:42 -0700 | [diff] [blame] | 457 | * Free the memory in this order, 1) free the background tabs; 2) free the |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 458 | * WebView cache; |
| 459 | */ |
| 460 | void freeMemory() { |
Grace Kloba | 92c18a5 | 2009-07-31 23:48:32 -0700 | [diff] [blame] | 461 | if (getTabCount() == 0) return; |
| 462 | |
Grace Kloba | f56f68d | 2010-04-11 22:53:42 -0700 | [diff] [blame] | 463 | // free the least frequently used background tabs |
| 464 | Vector<Tab> tabs = getHalfLeastUsedTabs(getCurrentTab()); |
| 465 | if (tabs.size() > 0) { |
| 466 | Log.w(LOGTAG, "Free " + tabs.size() + " tabs in the browser"); |
| 467 | for (Tab t : tabs) { |
| 468 | // store the WebView's state. |
| 469 | t.saveState(); |
| 470 | // destroy the tab |
| 471 | t.destroy(); |
| 472 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 473 | return; |
| 474 | } |
| 475 | |
Derek Sollenberger | 4433d03 | 2009-06-10 15:37:21 -0400 | [diff] [blame] | 476 | // free the WebView's unused memory (this includes the cache) |
| 477 | Log.w(LOGTAG, "Free WebView's unused memory and cache"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 478 | WebView view = getCurrentWebView(); |
| 479 | if (view != null) { |
Derek Sollenberger | 4433d03 | 2009-06-10 15:37:21 -0400 | [diff] [blame] | 480 | view.freeMemory(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 481 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 482 | } |
| 483 | |
Grace Kloba | f56f68d | 2010-04-11 22:53:42 -0700 | [diff] [blame] | 484 | private Vector<Tab> getHalfLeastUsedTabs(Tab current) { |
| 485 | Vector<Tab> tabsToGo = new Vector<Tab>(); |
| 486 | |
Patrick Scott | 2a67de4 | 2009-08-31 09:48:37 -0400 | [diff] [blame] | 487 | // Don't do anything if we only have 1 tab or if the current tab is |
| 488 | // null. |
| 489 | if (getTabCount() == 1 || current == null) { |
Grace Kloba | f56f68d | 2010-04-11 22:53:42 -0700 | [diff] [blame] | 490 | return tabsToGo; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Grace Kloba | f56f68d | 2010-04-11 22:53:42 -0700 | [diff] [blame] | 493 | if (mTabQueue.size() == 0) { |
| 494 | return tabsToGo; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 495 | } |
| 496 | |
Grace Kloba | f56f68d | 2010-04-11 22:53:42 -0700 | [diff] [blame] | 497 | // Rip through the queue starting at the beginning and tear down half of |
| 498 | // available tabs which are not the current tab or the parent of the |
| 499 | // current tab. |
| 500 | int openTabCount = 0; |
| 501 | for (Tab t : mTabQueue) { |
| 502 | if (t != null && t.getWebView() != null) { |
| 503 | openTabCount++; |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 504 | if (t != current && t != current.getParent()) { |
Grace Kloba | f56f68d | 2010-04-11 22:53:42 -0700 | [diff] [blame] | 505 | tabsToGo.add(t); |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | openTabCount /= 2; |
| 511 | if (tabsToGo.size() > openTabCount) { |
| 512 | tabsToGo.setSize(openTabCount); |
| 513 | } |
| 514 | |
| 515 | return tabsToGo; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 516 | } |
| 517 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 518 | /** |
| 519 | * Show the tab that contains the given WebView. |
| 520 | * @param view The WebView used to find the tab. |
| 521 | */ |
| 522 | Tab getTabFromView(WebView view) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 523 | for (Tab t : mTabs) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 524 | if (t.getSubWebView() == view || t.getWebView() == view) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 525 | return t; |
| 526 | } |
| 527 | } |
| 528 | return null; |
| 529 | } |
| 530 | |
| 531 | /** |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 532 | * Return the tab with the matching application id. |
| 533 | * @param id The application identifier. |
| 534 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 535 | Tab getTabFromAppId(String id) { |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 536 | if (id == null) { |
| 537 | return null; |
| 538 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 539 | for (Tab t : mTabs) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 540 | if (id.equals(t.getAppId())) { |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 541 | return t; |
| 542 | } |
| 543 | } |
| 544 | return null; |
| 545 | } |
| 546 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 547 | /** |
| 548 | * Stop loading in all opened WebView including subWindows. |
| 549 | */ |
Grace Kloba | 5d0e02e | 2009-10-05 15:15:36 -0700 | [diff] [blame] | 550 | void stopAllLoading() { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 551 | for (Tab t : mTabs) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 552 | final WebView webview = t.getWebView(); |
Grace Kloba | 5d0e02e | 2009-10-05 15:15:36 -0700 | [diff] [blame] | 553 | if (webview != null) { |
| 554 | webview.stopLoading(); |
| 555 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 556 | final WebView subview = t.getSubWebView(); |
| 557 | if (subview != null) { |
| 558 | webview.stopLoading(); |
| 559 | } |
Grace Kloba | 5d0e02e | 2009-10-05 15:15:36 -0700 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame] | 563 | // This method checks if a tab matches the given url. |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 564 | private boolean tabMatchesUrl(Tab t, String url) { |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame] | 565 | return url.equals(t.getUrl()) || url.equals(t.getOriginalUrl()); |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /** |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame] | 569 | * Return the tab that matches the given url. |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 570 | * @param url The url to search for. |
| 571 | */ |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame] | 572 | Tab findTabWithUrl(String url) { |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 573 | if (url == null) { |
| 574 | return null; |
| 575 | } |
| 576 | // Check the current tab first. |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame] | 577 | Tab currentTab = getCurrentTab(); |
| 578 | if (currentTab != null && tabMatchesUrl(currentTab, url)) { |
| 579 | return currentTab; |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 580 | } |
| 581 | // Now check all the rest. |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 582 | for (Tab tab : mTabs) { |
| 583 | if (tabMatchesUrl(tab, url)) { |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame] | 584 | return tab; |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | return null; |
| 588 | } |
| 589 | |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 590 | /** |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 591 | * Recreate the main WebView of the given tab. |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 592 | */ |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 593 | void recreateWebView(Tab t) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 594 | final WebView w = t.getWebView(); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 595 | if (w != null) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 596 | t.destroy(); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 597 | } |
| 598 | // Create a new WebView. If this tab is the current tab, we need to put |
| 599 | // back all the clients so force it to be the current tab. |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 600 | t.setWebView(createNewWebView()); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 601 | if (getCurrentTab() == t) { |
| 602 | setCurrentTab(t, true); |
| 603 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 604 | // Clear the saved state and picker data |
| 605 | t.setSavedState(null); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Creates a new WebView and registers it with the global settings. |
| 610 | */ |
| 611 | private WebView createNewWebView() { |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 612 | return createNewWebView(false); |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * Creates a new WebView and registers it with the global settings. |
| 617 | * @param privateBrowsing When true, enables private browsing in the new |
| 618 | * WebView. |
| 619 | */ |
| 620 | private WebView createNewWebView(boolean privateBrowsing) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 621 | return mController.getWebViewFactory().createWebView(privateBrowsing); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 625 | * Put the current tab in the background and set newTab as the current tab. |
| 626 | * @param newTab The new tab. If newTab is null, the current tab is not |
| 627 | * set. |
| 628 | */ |
| 629 | boolean setCurrentTab(Tab newTab) { |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 630 | return setCurrentTab(newTab, false); |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * If force is true, this method skips the check for newTab == current. |
| 635 | */ |
| 636 | private boolean setCurrentTab(Tab newTab, boolean force) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 637 | Tab current = getTab(mCurrentTab); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 638 | if (current == newTab && !force) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 639 | return true; |
| 640 | } |
| 641 | if (current != null) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 642 | current.putInBackground(); |
| 643 | mCurrentTab = -1; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 644 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 645 | if (newTab == null) { |
| 646 | return false; |
| 647 | } |
| 648 | |
| 649 | // Move the newTab to the end of the queue |
| 650 | int index = mTabQueue.indexOf(newTab); |
| 651 | if (index != -1) { |
| 652 | mTabQueue.remove(index); |
| 653 | } |
| 654 | mTabQueue.add(newTab); |
| 655 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 656 | // Display the new current tab |
| 657 | mCurrentTab = mTabs.indexOf(newTab); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 658 | WebView mainView = newTab.getWebView(); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 659 | boolean needRestore = !newTab.isSnapshot() && (mainView == null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 660 | if (needRestore) { |
| 661 | // Same work as in createNewTab() except don't do new Tab() |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 662 | mainView = createNewWebView(); |
| 663 | newTab.setWebView(mainView); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 664 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 665 | newTab.putInForeground(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 666 | if (needRestore) { |
| 667 | // Have to finish setCurrentTab work before calling restoreState |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 668 | if (!newTab.restoreState(newTab.getSavedState())) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 669 | mainView.loadUrl(BrowserSettings.getInstance().getHomePage()); |
| 670 | } |
| 671 | } |
| 672 | return true; |
| 673 | } |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 674 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 675 | } |