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 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 19 | import com.android.browser.IntentHandler.UrlData; |
| 20 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 21 | import android.os.Bundle; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 22 | import android.util.Log; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 23 | import android.webkit.WebBackForwardList; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 24 | import android.webkit.WebView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 25 | |
| 26 | import java.io.File; |
| 27 | import java.util.ArrayList; |
Elliott Slaughter | 3d6df16 | 2010-08-25 13:17:44 -0700 | [diff] [blame] | 28 | import java.util.HashMap; |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 29 | import java.util.List; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 30 | import java.util.Vector; |
| 31 | |
| 32 | class TabControl { |
| 33 | // Log Tag |
| 34 | private static final String LOGTAG = "TabControl"; |
| 35 | // Maximum number of tabs. |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 36 | private int mMaxTabs; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 37 | // Private array of WebViews that are used as tabs. |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 38 | private ArrayList<Tab> mTabs; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 39 | // Queue of most recently viewed tabs. |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 40 | private ArrayList<Tab> mTabQueue; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 41 | // Current position in mTabs. |
| 42 | private int mCurrentTab = -1; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 43 | // the main browser controller |
| 44 | private final Controller mController; |
| 45 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 46 | private final File mThumbnailDir; |
| 47 | |
| 48 | /** |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 49 | * Construct a new TabControl object |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 50 | */ |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 51 | TabControl(Controller controller) { |
| 52 | mController = controller; |
| 53 | mThumbnailDir = mController.getActivity() |
| 54 | .getDir("thumbnails", 0); |
| 55 | mMaxTabs = mController.getMaxTabs(); |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 56 | mTabs = new ArrayList<Tab>(mMaxTabs); |
| 57 | mTabQueue = new ArrayList<Tab>(mMaxTabs); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | File getThumbnailDir() { |
| 61 | return mThumbnailDir; |
| 62 | } |
| 63 | |
Michael Kolb | 6877575 | 2010-08-19 12:42:01 -0700 | [diff] [blame] | 64 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 65 | * 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 Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 74 | return t.getWebView(); |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 78 | * 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 Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 87 | return t.getTopWindow(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 88 | } |
| 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 Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 99 | return t.getSubWebView(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 103 | * return the list of tabs |
| 104 | */ |
| 105 | List<Tab> getTabs() { |
| 106 | return mTabs; |
| 107 | } |
| 108 | |
| 109 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 110 | * 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 Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 136 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 137 | /** |
| 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 Scott | ae641ac | 2009-04-20 13:51:49 -0400 | [diff] [blame] | 143 | if (tab == null) { |
| 144 | return -1; |
| 145 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 146 | return mTabs.indexOf(tab); |
| 147 | } |
| 148 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 149 | boolean canCreateNewTab() { |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 150 | return mMaxTabs != mTabs.size(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 151 | } |
| 152 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 153 | /** |
Elliott Slaughter | e440a88 | 2010-08-20 13:54:45 -0700 | [diff] [blame] | 154 | * 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 Slaughter | 3d6df16 | 2010-08-25 13:17:44 -0700 | [diff] [blame] | 159 | if (tab.getWebView() != null && tab.getWebView().isPrivateBrowsingEnabled()) { |
Elliott Slaughter | e440a88 | 2010-08-20 13:54:45 -0700 | [diff] [blame] | 160 | return true; |
| 161 | } |
| 162 | } |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | /** |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 167 | * Create a new tab. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 168 | * @return The newly createTab or null if we have reached the maximum |
| 169 | * number of open tabs. |
| 170 | */ |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 171 | Tab createNewTab(boolean closeOnExit, String appId, String url, |
| 172 | boolean privateBrowsing) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 173 | int size = mTabs.size(); |
| 174 | // Return false if we have maxed out on tabs |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 175 | if (mMaxTabs == size) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 176 | return null; |
| 177 | } |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 178 | final WebView w = createNewWebView(privateBrowsing); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 179 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 180 | // Create a new tab and add it to the tab list |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 181 | Tab t = new Tab(mController, w, closeOnExit, appId, url); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 182 | mTabs.add(t); |
The Android Open Source Project | 4e5f587 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 183 | // Initially put the tab in the background. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 184 | t.putInBackground(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 185 | return t; |
| 186 | } |
| 187 | |
| 188 | /** |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 189 | * Create a new tab with default values for closeOnExit(false), |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 190 | * appId(null), url(null), and privateBrowsing(false). |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 191 | */ |
| 192 | Tab createNewTab() { |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 193 | return createNewTab(false, null, null, false); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
Leon Scroggins | fde9746 | 2010-01-11 13:06:21 -0500 | [diff] [blame] | 197 | * 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 206 | * 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 Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 214 | |
Patrick Scott | d944d4d | 2010-01-27 16:39:11 -0500 | [diff] [blame] | 215 | // 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 Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 223 | t.putInBackground(); |
| 224 | mCurrentTab = -1; |
Patrick Scott | d944d4d | 2010-01-27 16:39:11 -0500 | [diff] [blame] | 225 | } 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 231 | // destroy the tab |
| 232 | t.destroy(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 233 | // clear it's references to parent and children |
| 234 | t.removeFromTree(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 235 | |
| 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 Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 239 | Vector<Tab> children = tab.getChildTabs(); |
| 240 | if (children != null) { |
| 241 | for (Tab child : children) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 242 | child.setParentTab(tab); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 247 | // Remove it from the queue of viewed tabs. |
| 248 | mTabQueue.remove(t); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 249 | return true; |
| 250 | } |
| 251 | |
| 252 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 253 | * Destroy all the tabs and subwindows |
| 254 | */ |
| 255 | void destroy() { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 256 | for (Tab t : mTabs) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 257 | t.destroy(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 258 | } |
| 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 271 | |
| 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 Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 278 | outState.putInt(Tab.NUMTABS, numTabs); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 279 | final int index = getCurrentIndex(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 280 | outState.putInt(Tab.CURRTAB, (index >= 0 && index < numTabs) ? index : 0); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 281 | for (int i = 0; i < numTabs; i++) { |
| 282 | final Tab t = getTab(i); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 283 | if (t.saveState()) { |
| 284 | outState.putBundle(Tab.WEBVIEW + i, t.getSavedState()); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /** |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame^] | 290 | * Check if the state can be restored. If the state can be restored, the |
| 291 | * current tab index is returned. This can be passed to restoreState below |
| 292 | * in order to restore the correct tab. Otherwise, -1 is returned and the |
| 293 | * state cannot be restored. |
| 294 | */ |
| 295 | int canRestoreState(Bundle inState, boolean restoreIncognitoTabs) { |
| 296 | final int numTabs = (inState == null) |
| 297 | ? - 1 : inState.getInt(Tab.NUMTABS, -1); |
| 298 | if (numTabs == -1) { |
| 299 | return -1; |
| 300 | } |
| 301 | final int oldCurrentTab = inState.getInt(Tab.CURRTAB, -1); |
| 302 | |
| 303 | // Determine whether the saved current tab can be restored, and if not, |
| 304 | // which tab will take its place. |
| 305 | int currentTab = -1; |
| 306 | if (restoreIncognitoTabs || |
| 307 | !inState.getBundle(Tab.WEBVIEW + oldCurrentTab) |
| 308 | .getBoolean(Tab.INCOGNITO)) { |
| 309 | currentTab = oldCurrentTab; |
| 310 | } else { |
| 311 | for (int i = 0; i < numTabs; i++) { |
| 312 | if (!inState.getBundle(Tab.WEBVIEW + i) |
| 313 | .getBoolean(Tab.INCOGNITO)) { |
| 314 | currentTab = i; |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | return currentTab; |
| 321 | } |
| 322 | |
| 323 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 324 | * Restore the state of all the tabs. |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame^] | 325 | * @param currentTab The tab index to restore. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 326 | * @param inState The saved state of all the tabs. |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 327 | * @param restoreIncognitoTabs Restoring private browsing tabs |
| 328 | * @param restoreAll All webviews get restored, not just the current tab |
| 329 | * (this does not override handling of incognito tabs) |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 330 | * @return True if there were previous tabs that were restored. False if |
| 331 | * there was no saved state or restoring the state failed. |
| 332 | */ |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame^] | 333 | void restoreState(Bundle inState, int currentTab, |
| 334 | boolean restoreIncognitoTabs, boolean restoreAll) { |
| 335 | if (currentTab == -1) { |
| 336 | return; |
| 337 | } |
Elliott Slaughter | 3d6df16 | 2010-08-25 13:17:44 -0700 | [diff] [blame] | 338 | |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame^] | 339 | // If currentTab is valid, numTabs must be present. |
| 340 | final int numTabs = inState.getInt(Tab.NUMTABS, -1); |
| 341 | |
| 342 | // Map saved tab indices to new indices, in case any incognito tabs |
| 343 | // need to not be restored. |
| 344 | HashMap<Integer, Integer> originalTabIndices = new HashMap<Integer, Integer>(); |
| 345 | originalTabIndices.put(-1, -1); |
| 346 | for (int i = 0; i < numTabs; i++) { |
| 347 | Bundle state = inState.getBundle(Tab.WEBVIEW + i); |
| 348 | |
| 349 | if (!restoreIncognitoTabs && state != null && state.getBoolean(Tab.INCOGNITO)) { |
| 350 | originalTabIndices.put(i, -1); |
| 351 | } else if (i == currentTab || restoreAll) { |
| 352 | Tab t = createNewTab(); |
| 353 | // Me must set the current tab before restoring the state |
| 354 | // so that all the client classes are set. |
| 355 | if (i == currentTab) { |
| 356 | setCurrentTab(t); |
| 357 | } |
| 358 | if (!t.restoreState(state)) { |
| 359 | Log.w(LOGTAG, "Fail in restoreState, load home page."); |
| 360 | t.getWebView().loadUrl(BrowserSettings.getInstance() |
| 361 | .getHomePage()); |
| 362 | } |
| 363 | originalTabIndices.put(i, getTabCount() - 1); |
Elliott Slaughter | 3d6df16 | 2010-08-25 13:17:44 -0700 | [diff] [blame] | 364 | } else { |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame^] | 365 | // Create a new tab and don't restore the state yet, add it |
| 366 | // to the tab list |
| 367 | Tab t = new Tab(mController, null, false, null, null); |
| 368 | if (state != null) { |
| 369 | t.setSavedState(state); |
| 370 | // Need to maintain the app id and original url so we |
| 371 | // can possibly reuse this tab. |
| 372 | t.setAppId(state.getString(Tab.APPID)); |
Elliott Slaughter | 3d6df16 | 2010-08-25 13:17:44 -0700 | [diff] [blame] | 373 | } |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame^] | 374 | mTabs.add(t); |
| 375 | // added the tab to the front as they are not current |
| 376 | mTabQueue.add(0, t); |
| 377 | originalTabIndices.put(i, getTabCount() - 1); |
Elliott Slaughter | 3d6df16 | 2010-08-25 13:17:44 -0700 | [diff] [blame] | 378 | } |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame^] | 379 | } |
Elliott Slaughter | 3d6df16 | 2010-08-25 13:17:44 -0700 | [diff] [blame] | 380 | |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame^] | 381 | // Rebuild the tree of tabs. Do this after all tabs have been |
| 382 | // created/restored so that the parent tab exists. |
| 383 | for (int i = 0; i < numTabs; i++) { |
| 384 | final Bundle b = inState.getBundle(Tab.WEBVIEW + i); |
| 385 | final Tab t = getTab(i); |
| 386 | if (b != null && t != null) { |
| 387 | final Integer parentIndex = originalTabIndices.get(b.getInt(Tab.PARENTTAB, -1)); |
| 388 | if (parentIndex != -1) { |
| 389 | final Tab parent = getTab(parentIndex); |
| 390 | if (parent != null) { |
| 391 | parent.addChildTab(t); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | } |
| 395 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /** |
Grace Kloba | f56f68d | 2010-04-11 22:53:42 -0700 | [diff] [blame] | 399 | * 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] | 400 | * WebView cache; |
| 401 | */ |
| 402 | void freeMemory() { |
Grace Kloba | 92c18a5 | 2009-07-31 23:48:32 -0700 | [diff] [blame] | 403 | if (getTabCount() == 0) return; |
| 404 | |
Grace Kloba | f56f68d | 2010-04-11 22:53:42 -0700 | [diff] [blame] | 405 | // free the least frequently used background tabs |
| 406 | Vector<Tab> tabs = getHalfLeastUsedTabs(getCurrentTab()); |
| 407 | if (tabs.size() > 0) { |
| 408 | Log.w(LOGTAG, "Free " + tabs.size() + " tabs in the browser"); |
| 409 | for (Tab t : tabs) { |
| 410 | // store the WebView's state. |
| 411 | t.saveState(); |
| 412 | // destroy the tab |
| 413 | t.destroy(); |
| 414 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 415 | return; |
| 416 | } |
| 417 | |
Derek Sollenberger | 4433d03 | 2009-06-10 15:37:21 -0400 | [diff] [blame] | 418 | // free the WebView's unused memory (this includes the cache) |
| 419 | 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] | 420 | WebView view = getCurrentWebView(); |
| 421 | if (view != null) { |
Derek Sollenberger | 4433d03 | 2009-06-10 15:37:21 -0400 | [diff] [blame] | 422 | view.freeMemory(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 423 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Grace Kloba | f56f68d | 2010-04-11 22:53:42 -0700 | [diff] [blame] | 426 | private Vector<Tab> getHalfLeastUsedTabs(Tab current) { |
| 427 | Vector<Tab> tabsToGo = new Vector<Tab>(); |
| 428 | |
Patrick Scott | 2a67de4 | 2009-08-31 09:48:37 -0400 | [diff] [blame] | 429 | // Don't do anything if we only have 1 tab or if the current tab is |
| 430 | // null. |
| 431 | if (getTabCount() == 1 || current == null) { |
Grace Kloba | f56f68d | 2010-04-11 22:53:42 -0700 | [diff] [blame] | 432 | return tabsToGo; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Grace Kloba | f56f68d | 2010-04-11 22:53:42 -0700 | [diff] [blame] | 435 | if (mTabQueue.size() == 0) { |
| 436 | return tabsToGo; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Grace Kloba | f56f68d | 2010-04-11 22:53:42 -0700 | [diff] [blame] | 439 | // Rip through the queue starting at the beginning and tear down half of |
| 440 | // available tabs which are not the current tab or the parent of the |
| 441 | // current tab. |
| 442 | int openTabCount = 0; |
| 443 | for (Tab t : mTabQueue) { |
| 444 | if (t != null && t.getWebView() != null) { |
| 445 | openTabCount++; |
| 446 | if (t != current && t != current.getParentTab()) { |
| 447 | tabsToGo.add(t); |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | openTabCount /= 2; |
| 453 | if (tabsToGo.size() > openTabCount) { |
| 454 | tabsToGo.setSize(openTabCount); |
| 455 | } |
| 456 | |
| 457 | return tabsToGo; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 458 | } |
| 459 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 460 | /** |
| 461 | * Show the tab that contains the given WebView. |
| 462 | * @param view The WebView used to find the tab. |
| 463 | */ |
| 464 | Tab getTabFromView(WebView view) { |
| 465 | final int size = getTabCount(); |
| 466 | for (int i = 0; i < size; i++) { |
| 467 | final Tab t = getTab(i); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 468 | if (t.getSubWebView() == view || t.getWebView() == view) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 469 | return t; |
| 470 | } |
| 471 | } |
| 472 | return null; |
| 473 | } |
| 474 | |
| 475 | /** |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 476 | * Return the tab with the matching application id. |
| 477 | * @param id The application identifier. |
| 478 | */ |
| 479 | Tab getTabFromId(String id) { |
| 480 | if (id == null) { |
| 481 | return null; |
| 482 | } |
| 483 | final int size = getTabCount(); |
| 484 | for (int i = 0; i < size; i++) { |
| 485 | final Tab t = getTab(i); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 486 | if (id.equals(t.getAppId())) { |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 487 | return t; |
| 488 | } |
| 489 | } |
| 490 | return null; |
| 491 | } |
| 492 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 493 | /** |
| 494 | * Stop loading in all opened WebView including subWindows. |
| 495 | */ |
Grace Kloba | 5d0e02e | 2009-10-05 15:15:36 -0700 | [diff] [blame] | 496 | void stopAllLoading() { |
| 497 | final int size = getTabCount(); |
| 498 | for (int i = 0; i < size; i++) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 499 | final Tab t = getTab(i); |
| 500 | final WebView webview = t.getWebView(); |
Grace Kloba | 5d0e02e | 2009-10-05 15:15:36 -0700 | [diff] [blame] | 501 | if (webview != null) { |
| 502 | webview.stopLoading(); |
| 503 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 504 | final WebView subview = t.getSubWebView(); |
| 505 | if (subview != null) { |
| 506 | webview.stopLoading(); |
| 507 | } |
Grace Kloba | 5d0e02e | 2009-10-05 15:15:36 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 511 | // This method checks if a non-app tab (one created within the browser) |
| 512 | // matches the given url. |
| 513 | private boolean tabMatchesUrl(Tab t, String url) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 514 | if (t.getAppId() != null) { |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 515 | return false; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 516 | } |
| 517 | WebView webview = t.getWebView(); |
| 518 | if (webview == null) { |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 519 | return false; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 520 | } else if (url.equals(webview.getUrl()) |
| 521 | || url.equals(webview.getOriginalUrl())) { |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 522 | return true; |
| 523 | } |
| 524 | return false; |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Return the tab that has no app id associated with it and the url of the |
| 529 | * tab matches the given url. |
| 530 | * @param url The url to search for. |
| 531 | */ |
| 532 | Tab findUnusedTabWithUrl(String url) { |
| 533 | if (url == null) { |
| 534 | return null; |
| 535 | } |
| 536 | // Check the current tab first. |
| 537 | Tab t = getCurrentTab(); |
| 538 | if (t != null && tabMatchesUrl(t, url)) { |
| 539 | return t; |
| 540 | } |
| 541 | // Now check all the rest. |
| 542 | final int size = getTabCount(); |
| 543 | for (int i = 0; i < size; i++) { |
| 544 | t = getTab(i); |
| 545 | if (tabMatchesUrl(t, url)) { |
| 546 | return t; |
| 547 | } |
| 548 | } |
| 549 | return null; |
| 550 | } |
| 551 | |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 552 | /** |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 553 | * Recreate the main WebView of the given tab. |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 554 | */ |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 555 | void recreateWebView(Tab t) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 556 | final WebView w = t.getWebView(); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 557 | if (w != null) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 558 | t.destroy(); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 559 | } |
| 560 | // Create a new WebView. If this tab is the current tab, we need to put |
| 561 | // back all the clients so force it to be the current tab. |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 562 | t.setWebView(createNewWebView()); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 563 | if (getCurrentTab() == t) { |
| 564 | setCurrentTab(t, true); |
| 565 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 566 | // Clear the saved state and picker data |
| 567 | t.setSavedState(null); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Creates a new WebView and registers it with the global settings. |
| 572 | */ |
| 573 | private WebView createNewWebView() { |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 574 | return createNewWebView(false); |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Creates a new WebView and registers it with the global settings. |
| 579 | * @param privateBrowsing When true, enables private browsing in the new |
| 580 | * WebView. |
| 581 | */ |
| 582 | private WebView createNewWebView(boolean privateBrowsing) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 583 | return mController.getWebViewFactory().createWebView(privateBrowsing); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 587 | * Put the current tab in the background and set newTab as the current tab. |
| 588 | * @param newTab The new tab. If newTab is null, the current tab is not |
| 589 | * set. |
| 590 | */ |
| 591 | boolean setCurrentTab(Tab newTab) { |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 592 | return setCurrentTab(newTab, false); |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * If force is true, this method skips the check for newTab == current. |
| 597 | */ |
| 598 | private boolean setCurrentTab(Tab newTab, boolean force) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 599 | Tab current = getTab(mCurrentTab); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 600 | if (current == newTab && !force) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 601 | return true; |
| 602 | } |
| 603 | if (current != null) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 604 | current.putInBackground(); |
| 605 | mCurrentTab = -1; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 606 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 607 | if (newTab == null) { |
| 608 | return false; |
| 609 | } |
| 610 | |
| 611 | // Move the newTab to the end of the queue |
| 612 | int index = mTabQueue.indexOf(newTab); |
| 613 | if (index != -1) { |
| 614 | mTabQueue.remove(index); |
| 615 | } |
| 616 | mTabQueue.add(newTab); |
| 617 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 618 | // Display the new current tab |
| 619 | mCurrentTab = mTabs.indexOf(newTab); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 620 | WebView mainView = newTab.getWebView(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 621 | boolean needRestore = (mainView == null); |
| 622 | if (needRestore) { |
| 623 | // Same work as in createNewTab() except don't do new Tab() |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 624 | mainView = createNewWebView(); |
| 625 | newTab.setWebView(mainView); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 626 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 627 | newTab.putInForeground(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 628 | if (needRestore) { |
| 629 | // Have to finish setCurrentTab work before calling restoreState |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 630 | if (!newTab.restoreState(newTab.getSavedState())) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 631 | mainView.loadUrl(BrowserSettings.getInstance().getHomePage()); |
| 632 | } |
| 633 | } |
| 634 | return true; |
| 635 | } |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 636 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 637 | } |