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