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