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