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