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