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