The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 19 | import android.os.Bundle; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 20 | import android.util.Log; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 21 | import android.webkit.WebView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 22 | |
| 23 | import java.io.File; |
| 24 | import java.util.ArrayList; |
Elliott Slaughter | 3d6df16 | 2010-08-25 13:17:44 -0700 | [diff] [blame] | 25 | import java.util.HashMap; |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 26 | import java.util.List; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 27 | import java.util.Vector; |
| 28 | |
| 29 | class TabControl { |
| 30 | // Log Tag |
| 31 | private static final String LOGTAG = "TabControl"; |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 32 | |
| 33 | // next Tab ID |
| 34 | private static long sNextId = 0; |
| 35 | |
| 36 | private static final String POSITIONS = "positions"; |
| 37 | private static final String CURRENT = "current"; |
| 38 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 39 | // Maximum number of tabs. |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 40 | private int mMaxTabs; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 41 | // Private array of WebViews that are used as tabs. |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 42 | private ArrayList<Tab> mTabs; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 43 | // Queue of most recently viewed tabs. |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 44 | private ArrayList<Tab> mTabQueue; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 45 | // Current position in mTabs. |
| 46 | private int mCurrentTab = -1; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 47 | // the main browser controller |
| 48 | private final Controller mController; |
| 49 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 50 | private final File mThumbnailDir; |
| 51 | |
| 52 | /** |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 53 | * Construct a new TabControl object |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 54 | */ |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 55 | TabControl(Controller controller) { |
| 56 | mController = controller; |
| 57 | mThumbnailDir = mController.getActivity() |
| 58 | .getDir("thumbnails", 0); |
| 59 | mMaxTabs = mController.getMaxTabs(); |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 60 | mTabs = new ArrayList<Tab>(mMaxTabs); |
| 61 | mTabQueue = new ArrayList<Tab>(mMaxTabs); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 64 | static long getNextId() { |
| 65 | return sNextId++; |
| 66 | } |
| 67 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 68 | File getThumbnailDir() { |
| 69 | return mThumbnailDir; |
| 70 | } |
| 71 | |
Michael Kolb | 6877575 | 2010-08-19 12:42:01 -0700 | [diff] [blame] | 72 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 73 | * Return the current tab's main WebView. This will always return the main |
| 74 | * WebView for a given tab and not a subwindow. |
| 75 | * @return The current tab's WebView. |
| 76 | */ |
| 77 | WebView getCurrentWebView() { |
| 78 | Tab t = getTab(mCurrentTab); |
| 79 | if (t == null) { |
| 80 | return null; |
| 81 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 82 | return t.getWebView(); |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 86 | * Return the current tab's top-level WebView. This can return a subwindow |
| 87 | * if one exists. |
| 88 | * @return The top-level WebView of the current tab. |
| 89 | */ |
| 90 | WebView getCurrentTopWebView() { |
| 91 | Tab t = getTab(mCurrentTab); |
| 92 | if (t == null) { |
| 93 | return null; |
| 94 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 95 | return t.getTopWindow(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Return the current tab's subwindow if it exists. |
| 100 | * @return The subwindow of the current tab or null if it doesn't exist. |
| 101 | */ |
| 102 | WebView getCurrentSubWindow() { |
| 103 | Tab t = getTab(mCurrentTab); |
| 104 | if (t == null) { |
| 105 | return null; |
| 106 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 107 | return t.getSubWebView(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 111 | * return the list of tabs |
| 112 | */ |
| 113 | List<Tab> getTabs() { |
| 114 | return mTabs; |
| 115 | } |
| 116 | |
| 117 | /** |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 118 | * Return the tab at the specified position. |
| 119 | * @return The Tab for the specified position or null if the tab does not |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 120 | * exist. |
| 121 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 122 | Tab getTab(int position) { |
| 123 | if (position >= 0 && position < mTabs.size()) { |
| 124 | return mTabs.get(position); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 125 | } |
| 126 | return null; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Return the current tab. |
| 131 | * @return The current tab. |
| 132 | */ |
| 133 | Tab getCurrentTab() { |
| 134 | return getTab(mCurrentTab); |
| 135 | } |
| 136 | |
| 137 | /** |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 138 | * Return the current tab position. |
| 139 | * @return The current tab position |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 140 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 141 | int getCurrentPosition() { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 142 | return mCurrentTab; |
| 143 | } |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 144 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 145 | /** |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 146 | * Given a Tab, find it's position |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 147 | * @param Tab to find |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 148 | * @return position of Tab or -1 if not found |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 149 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 150 | int getTabPosition(Tab tab) { |
Patrick Scott | ae641ac | 2009-04-20 13:51:49 -0400 | [diff] [blame] | 151 | if (tab == null) { |
| 152 | return -1; |
| 153 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 154 | return mTabs.indexOf(tab); |
| 155 | } |
| 156 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 157 | boolean canCreateNewTab() { |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 158 | return mMaxTabs != mTabs.size(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 159 | } |
| 160 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 161 | /** |
Elliott Slaughter | e440a88 | 2010-08-20 13:54:45 -0700 | [diff] [blame] | 162 | * Returns true if there are any incognito tabs open. |
| 163 | * @return True when any incognito tabs are open, false otherwise. |
| 164 | */ |
| 165 | boolean hasAnyOpenIncognitoTabs() { |
| 166 | for (Tab tab : mTabs) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 167 | if (tab.getWebView() != null |
| 168 | && tab.getWebView().isPrivateBrowsingEnabled()) { |
Elliott Slaughter | e440a88 | 2010-08-20 13:54:45 -0700 | [diff] [blame] | 169 | return true; |
| 170 | } |
| 171 | } |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | /** |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 176 | * Create a new tab. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 177 | * @return The newly createTab or null if we have reached the maximum |
| 178 | * number of open tabs. |
| 179 | */ |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 180 | Tab createNewTab(boolean privateBrowsing) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 181 | int size = mTabs.size(); |
| 182 | // Return false if we have maxed out on tabs |
Michael Kolb | 6e4653e | 2010-09-27 16:22:38 -0700 | [diff] [blame] | 183 | if (mMaxTabs == size) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 184 | return null; |
| 185 | } |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 186 | final WebView w = createNewWebView(privateBrowsing); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 187 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 188 | // Create a new tab and add it to the tab list |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 189 | Tab t = new Tab(mController, w); |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 190 | t.setId(getNextId()); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 191 | mTabs.add(t); |
The Android Open Source Project | 4e5f587 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 192 | // Initially put the tab in the background. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 193 | t.putInBackground(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 194 | return t; |
| 195 | } |
| 196 | |
| 197 | /** |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 198 | * Create a new tab with default values for closeOnExit(false), |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 199 | * appId(null), url(null), and privateBrowsing(false). |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 200 | */ |
| 201 | Tab createNewTab() { |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 202 | return createNewTab(false); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /** |
Leon Scroggins | fde9746 | 2010-01-11 13:06:21 -0500 | [diff] [blame] | 206 | * Remove the parent child relationships from all tabs. |
| 207 | */ |
| 208 | void removeParentChildRelationShips() { |
| 209 | for (Tab tab : mTabs) { |
| 210 | tab.removeFromTree(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 215 | * Remove the tab from the list. If the tab is the current tab shown, the |
| 216 | * last created tab will be shown. |
| 217 | * @param t The tab to be removed. |
| 218 | */ |
| 219 | boolean removeTab(Tab t) { |
| 220 | if (t == null) { |
| 221 | return false; |
| 222 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 223 | |
Patrick Scott | d944d4d | 2010-01-27 16:39:11 -0500 | [diff] [blame] | 224 | // Grab the current tab before modifying the list. |
| 225 | Tab current = getCurrentTab(); |
| 226 | |
| 227 | // Remove t from our list of tabs. |
| 228 | mTabs.remove(t); |
| 229 | |
| 230 | // Put the tab in the background only if it is the current one. |
| 231 | if (current == t) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 232 | t.putInBackground(); |
| 233 | mCurrentTab = -1; |
Patrick Scott | d944d4d | 2010-01-27 16:39:11 -0500 | [diff] [blame] | 234 | } else { |
| 235 | // If a tab that is earlier in the list gets removed, the current |
| 236 | // index no longer points to the correct tab. |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 237 | mCurrentTab = getTabPosition(current); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 240 | // destroy the tab |
| 241 | t.destroy(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 242 | // clear it's references to parent and children |
| 243 | t.removeFromTree(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 244 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 245 | // Remove it from the queue of viewed tabs. |
| 246 | mTabQueue.remove(t); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 247 | return true; |
| 248 | } |
| 249 | |
| 250 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 251 | * Destroy all the tabs and subwindows |
| 252 | */ |
| 253 | void destroy() { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 254 | for (Tab t : mTabs) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 255 | t.destroy(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 256 | } |
| 257 | mTabs.clear(); |
| 258 | mTabQueue.clear(); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Returns the number of tabs created. |
| 263 | * @return The number of tabs created. |
| 264 | */ |
| 265 | int getTabCount() { |
| 266 | return mTabs.size(); |
| 267 | } |
| 268 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 269 | /** |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 270 | * save the tab state: |
| 271 | * current position |
| 272 | * position sorted array of tab ids |
| 273 | * for each tab id, save the tab state |
| 274 | * @param outState |
John Reck | aed9c54 | 2011-05-27 16:08:53 -0700 | [diff] [blame] | 275 | * @param saveImages |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 276 | */ |
John Reck | aed9c54 | 2011-05-27 16:08:53 -0700 | [diff] [blame] | 277 | void saveState(Bundle outState, boolean saveImages) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 278 | final int numTabs = getTabCount(); |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 279 | long[] ids = new long[numTabs]; |
| 280 | int i = 0; |
| 281 | for (Tab tab : mTabs) { |
| 282 | ids[i++] = tab.getId(); |
| 283 | if (tab.saveState()) { |
John Reck | aed9c54 | 2011-05-27 16:08:53 -0700 | [diff] [blame] | 284 | outState.putBundle(Long.toString(tab.getId()), |
| 285 | tab.getSavedState(saveImages)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 288 | outState.putLongArray(POSITIONS, ids); |
| 289 | final long cid = getCurrentTab().getId(); |
| 290 | outState.putLong(CURRENT, cid); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 294 | * Check if the state can be restored. If the state can be restored, the |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 295 | * current tab id is returned. This can be passed to restoreState below |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 296 | * in order to restore the correct tab. Otherwise, -1 is returned and the |
| 297 | * state cannot be restored. |
| 298 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 299 | long canRestoreState(Bundle inState, boolean restoreIncognitoTabs) { |
| 300 | final long[] ids = (inState == null) ? null : inState.getLongArray(POSITIONS); |
| 301 | if (ids == null) { |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 302 | return -1; |
| 303 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 304 | final long oldcurrent = inState.getLong(CURRENT); |
| 305 | long current = -1; |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 306 | if (restoreIncognitoTabs || |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 307 | !inState.getBundle(Long.toString(oldcurrent)).getBoolean(Tab.INCOGNITO)) { |
| 308 | current = oldcurrent; |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 309 | } else { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 310 | // pick first non incognito tab |
| 311 | for (long id : ids) { |
| 312 | if (!inState.getBundle(Long.toString(id)).getBoolean(Tab.INCOGNITO)) { |
| 313 | current = id; |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 314 | break; |
| 315 | } |
| 316 | } |
| 317 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 318 | return current; |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 322 | * Restore the state of all the tabs. |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 323 | * @param currentId The tab id to restore. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 324 | * @param inState The saved state of all the tabs. |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 325 | * @param restoreIncognitoTabs Restoring private browsing tabs |
| 326 | * @param restoreAll All webviews get restored, not just the current tab |
| 327 | * (this does not override handling of incognito tabs) |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 328 | * @return True if there were previous tabs that were restored. False if |
| 329 | * there was no saved state or restoring the state failed. |
| 330 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 331 | void restoreState(Bundle inState, long currentId, |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 332 | boolean restoreIncognitoTabs, boolean restoreAll) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 333 | if (currentId == -1) { |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 334 | return; |
| 335 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 336 | long[] ids = inState.getLongArray(POSITIONS); |
| 337 | long maxId = -Long.MAX_VALUE; |
| 338 | HashMap<Long, Tab> tabMap = new HashMap<Long, Tab>(); |
| 339 | for (long id : ids) { |
| 340 | if (id > maxId) { |
| 341 | maxId = id; |
| 342 | } |
| 343 | final String idkey = Long.toString(id); |
| 344 | Bundle state = inState.getBundle(idkey); |
| 345 | if (!restoreIncognitoTabs && state != null |
| 346 | && state.getBoolean(Tab.INCOGNITO)) { |
| 347 | // ignore tab |
| 348 | } else if (id == currentId || restoreAll) { |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 349 | Tab t = createNewTab(); |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 350 | tabMap.put(id, t); |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 351 | // Me must set the current tab before restoring the state |
| 352 | // so that all the client classes are set. |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 353 | if (id == currentId) { |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 354 | setCurrentTab(t); |
| 355 | } |
| 356 | if (!t.restoreState(state)) { |
| 357 | Log.w(LOGTAG, "Fail in restoreState, load home page."); |
| 358 | t.getWebView().loadUrl(BrowserSettings.getInstance() |
| 359 | .getHomePage()); |
| 360 | } |
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 |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 364 | Tab t = new Tab(mController, null); |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 365 | t.setId(id); |
| 366 | tabMap.put(id, t); |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 367 | if (state != null) { |
| 368 | t.setSavedState(state); |
| 369 | // Need to maintain the app id and original url so we |
| 370 | // can possibly reuse this tab. |
| 371 | t.setAppId(state.getString(Tab.APPID)); |
Elliott Slaughter | 3d6df16 | 2010-08-25 13:17:44 -0700 | [diff] [blame] | 372 | } |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 373 | mTabs.add(t); |
| 374 | // added the tab to the front as they are not current |
| 375 | mTabQueue.add(0, t); |
Elliott Slaughter | 3d6df16 | 2010-08-25 13:17:44 -0700 | [diff] [blame] | 376 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 377 | // make sure that there is no id overlap between the restored |
| 378 | // and new tabs |
| 379 | sNextId = maxId + 1; |
Elliott Slaughter | 3d6df16 | 2010-08-25 13:17:44 -0700 | [diff] [blame] | 380 | |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 381 | } |
| 382 | // restore parent/child relationships |
| 383 | for (long id : ids) { |
| 384 | final Tab tab = tabMap.get(id); |
| 385 | final Bundle b = inState.getBundle(Long.toString(id)); |
| 386 | if ((b != null) && (tab != null)) { |
| 387 | final long parentId = b.getLong(Tab.PARENTTAB, -1); |
| 388 | if (parentId != -1) { |
| 389 | final Tab parent = tabMap.get(parentId); |
Patrick Scott | 7d50a93 | 2011-02-04 09:27:26 -0500 | [diff] [blame] | 390 | if (parent != null) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 391 | parent.addChildTab(tab); |
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++; |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 446 | if (t != current && t != current.getParent()) { |
Grace Kloba | f56f68d | 2010-04-11 22:53:42 -0700 | [diff] [blame] | 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) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 465 | for (Tab t : mTabs) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 466 | if (t.getSubWebView() == view || t.getWebView() == view) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 467 | return t; |
| 468 | } |
| 469 | } |
| 470 | return null; |
| 471 | } |
| 472 | |
| 473 | /** |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 474 | * Return the tab with the matching application id. |
| 475 | * @param id The application identifier. |
| 476 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 477 | Tab getTabFromAppId(String id) { |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 478 | if (id == null) { |
| 479 | return null; |
| 480 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 481 | for (Tab t : mTabs) { |
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() { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 493 | for (Tab t : mTabs) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 494 | final WebView webview = t.getWebView(); |
Grace Kloba | 5d0e02e | 2009-10-05 15:15:36 -0700 | [diff] [blame] | 495 | if (webview != null) { |
| 496 | webview.stopLoading(); |
| 497 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 498 | final WebView subview = t.getSubWebView(); |
| 499 | if (subview != null) { |
| 500 | webview.stopLoading(); |
| 501 | } |
Grace Kloba | 5d0e02e | 2009-10-05 15:15:36 -0700 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 505 | // This method checks if a non-app tab (one created within the browser) |
| 506 | // matches the given url. |
| 507 | private boolean tabMatchesUrl(Tab t, String url) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 508 | if (t.getAppId() != null) { |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 509 | return false; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 510 | } |
| 511 | WebView webview = t.getWebView(); |
| 512 | if (webview == null) { |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 513 | return false; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 514 | } else if (url.equals(webview.getUrl()) |
| 515 | || url.equals(webview.getOriginalUrl())) { |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 516 | return true; |
| 517 | } |
| 518 | return false; |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Return the tab that has no app id associated with it and the url of the |
| 523 | * tab matches the given url. |
| 524 | * @param url The url to search for. |
| 525 | */ |
| 526 | Tab findUnusedTabWithUrl(String url) { |
| 527 | if (url == null) { |
| 528 | return null; |
| 529 | } |
| 530 | // Check the current tab first. |
| 531 | Tab t = getCurrentTab(); |
| 532 | if (t != null && tabMatchesUrl(t, url)) { |
| 533 | return t; |
| 534 | } |
| 535 | // Now check all the rest. |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 536 | for (Tab tab : mTabs) { |
| 537 | if (tabMatchesUrl(tab, url)) { |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 538 | return t; |
| 539 | } |
| 540 | } |
| 541 | return null; |
| 542 | } |
| 543 | |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 544 | /** |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 545 | * Recreate the main WebView of the given tab. |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 546 | */ |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 547 | void recreateWebView(Tab t) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 548 | final WebView w = t.getWebView(); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 549 | if (w != null) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 550 | t.destroy(); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 551 | } |
| 552 | // Create a new WebView. If this tab is the current tab, we need to put |
| 553 | // back all the clients so force it to be the current tab. |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 554 | t.setWebView(createNewWebView()); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 555 | if (getCurrentTab() == t) { |
| 556 | setCurrentTab(t, true); |
| 557 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 558 | // Clear the saved state and picker data |
| 559 | t.setSavedState(null); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /** |
| 563 | * Creates a new WebView and registers it with the global settings. |
| 564 | */ |
| 565 | private WebView createNewWebView() { |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 566 | return createNewWebView(false); |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Creates a new WebView and registers it with the global settings. |
| 571 | * @param privateBrowsing When true, enables private browsing in the new |
| 572 | * WebView. |
| 573 | */ |
| 574 | private WebView createNewWebView(boolean privateBrowsing) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 575 | return mController.getWebViewFactory().createWebView(privateBrowsing); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 579 | * Put the current tab in the background and set newTab as the current tab. |
| 580 | * @param newTab The new tab. If newTab is null, the current tab is not |
| 581 | * set. |
| 582 | */ |
| 583 | boolean setCurrentTab(Tab newTab) { |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 584 | return setCurrentTab(newTab, false); |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * If force is true, this method skips the check for newTab == current. |
| 589 | */ |
| 590 | private boolean setCurrentTab(Tab newTab, boolean force) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 591 | Tab current = getTab(mCurrentTab); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 592 | if (current == newTab && !force) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 593 | return true; |
| 594 | } |
| 595 | if (current != null) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 596 | current.putInBackground(); |
| 597 | mCurrentTab = -1; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 598 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 599 | if (newTab == null) { |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | // Move the newTab to the end of the queue |
| 604 | int index = mTabQueue.indexOf(newTab); |
| 605 | if (index != -1) { |
| 606 | mTabQueue.remove(index); |
| 607 | } |
| 608 | mTabQueue.add(newTab); |
| 609 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 610 | // Display the new current tab |
| 611 | mCurrentTab = mTabs.indexOf(newTab); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 612 | WebView mainView = newTab.getWebView(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 613 | boolean needRestore = (mainView == null); |
| 614 | if (needRestore) { |
| 615 | // Same work as in createNewTab() except don't do new Tab() |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 616 | mainView = createNewWebView(); |
| 617 | newTab.setWebView(mainView); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 618 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 619 | newTab.putInForeground(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 620 | if (needRestore) { |
| 621 | // Have to finish setCurrentTab work before calling restoreState |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 622 | if (!newTab.restoreState(newTab.getSavedState())) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 623 | mainView.loadUrl(BrowserSettings.getInstance().getHomePage()); |
| 624 | } |
| 625 | } |
| 626 | return true; |
| 627 | } |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 628 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 629 | } |