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