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