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