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