The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 19 | import android.os.Bundle; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 20 | import android.util.Log; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 21 | import android.view.View; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 22 | import android.webkit.WebBackForwardList; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 23 | import android.webkit.WebView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 24 | |
| 25 | import java.io.File; |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.Vector; |
| 28 | |
| 29 | class TabControl { |
| 30 | // Log Tag |
| 31 | private static final String LOGTAG = "TabControl"; |
| 32 | // Maximum number of tabs. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 33 | private static final int MAX_TABS = 8; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 34 | // Private array of WebViews that are used as tabs. |
| 35 | private ArrayList<Tab> mTabs = new ArrayList<Tab>(MAX_TABS); |
| 36 | // Queue of most recently viewed tabs. |
| 37 | private ArrayList<Tab> mTabQueue = new ArrayList<Tab>(MAX_TABS); |
| 38 | // Current position in mTabs. |
| 39 | private int mCurrentTab = -1; |
| 40 | // A private instance of BrowserActivity to interface with when adding and |
| 41 | // switching between tabs. |
| 42 | private final BrowserActivity mActivity; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 43 | // Directory to store thumbnails for each WebView. |
| 44 | private final File mThumbnailDir; |
| 45 | |
| 46 | /** |
| 47 | * Construct a new TabControl object that interfaces with the given |
| 48 | * BrowserActivity instance. |
| 49 | * @param activity A BrowserActivity instance that TabControl will interface |
| 50 | * with. |
| 51 | */ |
| 52 | TabControl(BrowserActivity activity) { |
| 53 | mActivity = activity; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 54 | mThumbnailDir = activity.getDir("thumbnails", 0); |
| 55 | } |
| 56 | |
| 57 | File getThumbnailDir() { |
| 58 | return mThumbnailDir; |
| 59 | } |
| 60 | |
| 61 | BrowserActivity getBrowserActivity() { |
| 62 | return mActivity; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Return the current tab's main WebView. This will always return the main |
| 67 | * WebView for a given tab and not a subwindow. |
| 68 | * @return The current tab's WebView. |
| 69 | */ |
| 70 | WebView getCurrentWebView() { |
| 71 | Tab t = getTab(mCurrentTab); |
| 72 | if (t == null) { |
| 73 | return null; |
| 74 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 75 | return t.getWebView(); |
Ben Murdoch | bff2d60 | 2009-07-01 20:19:05 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 79 | * Return the current tab's top-level WebView. This can return a subwindow |
| 80 | * if one exists. |
| 81 | * @return The top-level WebView of the current tab. |
| 82 | */ |
| 83 | WebView getCurrentTopWebView() { |
| 84 | Tab t = getTab(mCurrentTab); |
| 85 | if (t == null) { |
| 86 | return null; |
| 87 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 88 | return t.getTopWindow(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Return the current tab's subwindow if it exists. |
| 93 | * @return The subwindow of the current tab or null if it doesn't exist. |
| 94 | */ |
| 95 | WebView getCurrentSubWindow() { |
| 96 | Tab t = getTab(mCurrentTab); |
| 97 | if (t == null) { |
| 98 | return null; |
| 99 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 100 | return t.getSubWebView(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Return the tab at the specified index. |
| 105 | * @return The Tab for the specified index or null if the tab does not |
| 106 | * exist. |
| 107 | */ |
| 108 | Tab getTab(int index) { |
| 109 | if (index >= 0 && index < mTabs.size()) { |
| 110 | return mTabs.get(index); |
| 111 | } |
| 112 | return null; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Return the current tab. |
| 117 | * @return The current tab. |
| 118 | */ |
| 119 | Tab getCurrentTab() { |
| 120 | return getTab(mCurrentTab); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Return the current tab index. |
| 125 | * @return The current tab index |
| 126 | */ |
| 127 | int getCurrentIndex() { |
| 128 | return mCurrentTab; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Given a Tab, find it's index |
| 133 | * @param Tab to find |
| 134 | * @return index of Tab or -1 if not found |
| 135 | */ |
| 136 | int getTabIndex(Tab tab) { |
Patrick Scott | ae641ac | 2009-04-20 13:51:49 -0400 | [diff] [blame] | 137 | if (tab == null) { |
| 138 | return -1; |
| 139 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 140 | return mTabs.indexOf(tab); |
| 141 | } |
| 142 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 143 | boolean canCreateNewTab() { |
| 144 | return MAX_TABS != mTabs.size(); |
| 145 | } |
| 146 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 147 | /** |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 148 | * Create a new tab. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 149 | * @return The newly createTab or null if we have reached the maximum |
| 150 | * number of open tabs. |
| 151 | */ |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 152 | Tab createNewTab(boolean closeOnExit, String appId, String url) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 153 | int size = mTabs.size(); |
| 154 | // Return false if we have maxed out on tabs |
| 155 | if (MAX_TABS == size) { |
| 156 | return null; |
| 157 | } |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 158 | final WebView w = createNewWebView(); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 159 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 160 | // Create a new tab and add it to the tab list |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 161 | Tab t = new Tab(mActivity, w, closeOnExit, appId, url); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 162 | mTabs.add(t); |
The Android Open Source Project | 4e5f587 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 163 | // Initially put the tab in the background. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 164 | t.putInBackground(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 165 | return t; |
| 166 | } |
| 167 | |
| 168 | /** |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 169 | * Create a new tab with default values for closeOnExit(false), |
| 170 | * appId(null), and url(null). |
| 171 | */ |
| 172 | Tab createNewTab() { |
| 173 | return createNewTab(false, null, null); |
| 174 | } |
| 175 | |
| 176 | /** |
Leon Scroggins | fde9746 | 2010-01-11 13:06:21 -0500 | [diff] [blame^] | 177 | * Remove the parent child relationships from all tabs. |
| 178 | */ |
| 179 | void removeParentChildRelationShips() { |
| 180 | for (Tab tab : mTabs) { |
| 181 | tab.removeFromTree(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 186 | * Remove the tab from the list. If the tab is the current tab shown, the |
| 187 | * last created tab will be shown. |
| 188 | * @param t The tab to be removed. |
| 189 | */ |
| 190 | boolean removeTab(Tab t) { |
| 191 | if (t == null) { |
| 192 | return false; |
| 193 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 194 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 195 | // Only remove the tab if it is the current one. |
| 196 | if (getCurrentTab() == t) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 197 | t.putInBackground(); |
| 198 | mCurrentTab = -1; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 201 | // destroy the tab |
| 202 | t.destroy(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 203 | // clear it's references to parent and children |
| 204 | t.removeFromTree(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 205 | // Remove it from our list of tabs. |
| 206 | mTabs.remove(t); |
| 207 | |
| 208 | // The tab indices have shifted, update all the saved state so we point |
| 209 | // to the correct index. |
| 210 | for (Tab tab : mTabs) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 211 | Vector<Tab> children = tab.getChildTabs(); |
| 212 | if (children != null) { |
| 213 | for (Tab child : children) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 214 | child.setParentTab(tab); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 219 | // This tab may have been pushed in to the background and then closed. |
| 220 | // If the saved state contains a picture file, delete the file. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 221 | Bundle savedState = t.getSavedState(); |
| 222 | if (savedState != null) { |
| 223 | if (savedState.containsKey(Tab.CURRPICTURE)) { |
| 224 | new File(savedState.getString(Tab.CURRPICTURE)).delete(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | // Remove it from the queue of viewed tabs. |
| 229 | mTabQueue.remove(t); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 230 | return true; |
| 231 | } |
| 232 | |
| 233 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 234 | * Destroy all the tabs and subwindows |
| 235 | */ |
| 236 | void destroy() { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 237 | for (Tab t : mTabs) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 238 | t.destroy(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 239 | } |
| 240 | mTabs.clear(); |
| 241 | mTabQueue.clear(); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Returns the number of tabs created. |
| 246 | * @return The number of tabs created. |
| 247 | */ |
| 248 | int getTabCount() { |
| 249 | return mTabs.size(); |
| 250 | } |
| 251 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 252 | |
| 253 | /** |
| 254 | * Save the state of all the Tabs. |
| 255 | * @param outState The Bundle to save the state to. |
| 256 | */ |
| 257 | void saveState(Bundle outState) { |
| 258 | final int numTabs = getTabCount(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 259 | outState.putInt(Tab.NUMTABS, numTabs); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 260 | final int index = getCurrentIndex(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 261 | outState.putInt(Tab.CURRTAB, (index >= 0 && index < numTabs) ? index : 0); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 262 | for (int i = 0; i < numTabs; i++) { |
| 263 | final Tab t = getTab(i); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 264 | if (t.saveState()) { |
| 265 | outState.putBundle(Tab.WEBVIEW + i, t.getSavedState()); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Restore the state of all the tabs. |
| 272 | * @param inState The saved state of all the tabs. |
| 273 | * @return True if there were previous tabs that were restored. False if |
| 274 | * there was no saved state or restoring the state failed. |
| 275 | */ |
| 276 | boolean restoreState(Bundle inState) { |
| 277 | final int numTabs = (inState == null) |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 278 | ? -1 : inState.getInt(Tab.NUMTABS, -1); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 279 | if (numTabs == -1) { |
| 280 | return false; |
| 281 | } else { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 282 | final int currentTab = inState.getInt(Tab.CURRTAB, -1); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 283 | for (int i = 0; i < numTabs; i++) { |
| 284 | if (i == currentTab) { |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 285 | Tab t = createNewTab(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 286 | // Me must set the current tab before restoring the state |
| 287 | // so that all the client classes are set. |
| 288 | setCurrentTab(t); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 289 | if (!t.restoreState(inState.getBundle(Tab.WEBVIEW + i))) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 290 | Log.w(LOGTAG, "Fail in restoreState, load home page."); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 291 | t.getWebView().loadUrl(BrowserSettings.getInstance() |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 292 | .getHomePage()); |
| 293 | } |
| 294 | } else { |
| 295 | // Create a new tab and don't restore the state yet, add it |
| 296 | // to the tab list |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 297 | Tab t = new Tab(mActivity, null, false, null, null); |
| 298 | Bundle state = inState.getBundle(Tab.WEBVIEW + i); |
| 299 | if (state != null) { |
| 300 | t.setSavedState(state); |
| 301 | t.populatePickerDataFromSavedState(); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 302 | // Need to maintain the app id and original url so we |
| 303 | // can possibly reuse this tab. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 304 | t.setAppId(state.getString(Tab.APPID)); |
| 305 | t.setOriginalUrl(state.getString(Tab.ORIGINALURL)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 306 | } |
| 307 | mTabs.add(t); |
| 308 | mTabQueue.add(t); |
| 309 | } |
| 310 | } |
| 311 | // Rebuild the tree of tabs. Do this after all tabs have been |
| 312 | // created/restored so that the parent tab exists. |
| 313 | for (int i = 0; i < numTabs; i++) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 314 | final Bundle b = inState.getBundle(Tab.WEBVIEW + i); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 315 | final Tab t = getTab(i); |
| 316 | if (b != null && t != null) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 317 | final int parentIndex = b.getInt(Tab.PARENTTAB, -1); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 318 | if (parentIndex != -1) { |
| 319 | final Tab parent = getTab(parentIndex); |
| 320 | if (parent != null) { |
| 321 | parent.addChildTab(t); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Free the memory in this order, 1) free the background tab; 2) free the |
| 332 | * WebView cache; |
| 333 | */ |
| 334 | void freeMemory() { |
Grace Kloba | 92c18a5 | 2009-07-31 23:48:32 -0700 | [diff] [blame] | 335 | if (getTabCount() == 0) return; |
| 336 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 337 | // free the least frequently used background tab |
Patrick Scott | 2a67de4 | 2009-08-31 09:48:37 -0400 | [diff] [blame] | 338 | Tab t = getLeastUsedTab(getCurrentTab()); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 339 | if (t != null) { |
| 340 | Log.w(LOGTAG, "Free a tab in the browser"); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 341 | // store the WebView's state. |
| 342 | t.saveState(); |
| 343 | // destroy the tab |
| 344 | t.destroy(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 345 | return; |
| 346 | } |
| 347 | |
Derek Sollenberger | 4433d03 | 2009-06-10 15:37:21 -0400 | [diff] [blame] | 348 | // free the WebView's unused memory (this includes the cache) |
| 349 | 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] | 350 | WebView view = getCurrentWebView(); |
| 351 | if (view != null) { |
Derek Sollenberger | 4433d03 | 2009-06-10 15:37:21 -0400 | [diff] [blame] | 352 | view.freeMemory(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 353 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Patrick Scott | 2a67de4 | 2009-08-31 09:48:37 -0400 | [diff] [blame] | 356 | private Tab getLeastUsedTab(Tab current) { |
| 357 | // Don't do anything if we only have 1 tab or if the current tab is |
| 358 | // null. |
| 359 | if (getTabCount() == 1 || current == null) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 360 | return null; |
| 361 | } |
| 362 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 363 | // Rip through the queue starting at the beginning and tear down the |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 364 | // next available tab. |
| 365 | Tab t = null; |
| 366 | int i = 0; |
| 367 | final int queueSize = mTabQueue.size(); |
| 368 | if (queueSize == 0) { |
| 369 | return null; |
| 370 | } |
| 371 | do { |
| 372 | t = mTabQueue.get(i++); |
Grace Kloba | 92c18a5 | 2009-07-31 23:48:32 -0700 | [diff] [blame] | 373 | } while (i < queueSize |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 374 | && ((t != null && t.getWebView() == null) |
| 375 | || t == current.getParentTab())); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 376 | |
Patrick Scott | d068f80 | 2009-06-22 11:46:06 -0400 | [diff] [blame] | 377 | // Don't do anything if the last remaining tab is the current one or if |
| 378 | // the last tab has been freed already. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 379 | if (t == current || t.getWebView() == null) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 380 | return null; |
| 381 | } |
| 382 | |
| 383 | return t; |
| 384 | } |
| 385 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 386 | /** |
| 387 | * Show the tab that contains the given WebView. |
| 388 | * @param view The WebView used to find the tab. |
| 389 | */ |
| 390 | Tab getTabFromView(WebView view) { |
| 391 | final int size = getTabCount(); |
| 392 | for (int i = 0; i < size; i++) { |
| 393 | final Tab t = getTab(i); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 394 | if (t.getSubWebView() == view || t.getWebView() == view) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 395 | return t; |
| 396 | } |
| 397 | } |
| 398 | return null; |
| 399 | } |
| 400 | |
| 401 | /** |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 402 | * Return the tab with the matching application id. |
| 403 | * @param id The application identifier. |
| 404 | */ |
| 405 | Tab getTabFromId(String id) { |
| 406 | if (id == null) { |
| 407 | return null; |
| 408 | } |
| 409 | final int size = getTabCount(); |
| 410 | for (int i = 0; i < size; i++) { |
| 411 | final Tab t = getTab(i); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 412 | if (id.equals(t.getAppId())) { |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 413 | return t; |
| 414 | } |
| 415 | } |
| 416 | return null; |
| 417 | } |
| 418 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 419 | /** |
| 420 | * Stop loading in all opened WebView including subWindows. |
| 421 | */ |
Grace Kloba | 5d0e02e | 2009-10-05 15:15:36 -0700 | [diff] [blame] | 422 | void stopAllLoading() { |
| 423 | final int size = getTabCount(); |
| 424 | for (int i = 0; i < size; i++) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 425 | final Tab t = getTab(i); |
| 426 | final WebView webview = t.getWebView(); |
Grace Kloba | 5d0e02e | 2009-10-05 15:15:36 -0700 | [diff] [blame] | 427 | if (webview != null) { |
| 428 | webview.stopLoading(); |
| 429 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 430 | final WebView subview = t.getSubWebView(); |
| 431 | if (subview != null) { |
| 432 | webview.stopLoading(); |
| 433 | } |
Grace Kloba | 5d0e02e | 2009-10-05 15:15:36 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 437 | // This method checks if a non-app tab (one created within the browser) |
| 438 | // matches the given url. |
| 439 | private boolean tabMatchesUrl(Tab t, String url) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 440 | if (t.getAppId() != null) { |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 441 | return false; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 442 | } |
| 443 | WebView webview = t.getWebView(); |
| 444 | if (webview == null) { |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 445 | return false; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 446 | } else if (url.equals(webview.getUrl()) |
| 447 | || url.equals(webview.getOriginalUrl())) { |
Patrick Scott | cd11589 | 2009-07-16 09:42:58 -0400 | [diff] [blame] | 448 | return true; |
| 449 | } |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Return the tab that has no app id associated with it and the url of the |
| 455 | * tab matches the given url. |
| 456 | * @param url The url to search for. |
| 457 | */ |
| 458 | Tab findUnusedTabWithUrl(String url) { |
| 459 | if (url == null) { |
| 460 | return null; |
| 461 | } |
| 462 | // Check the current tab first. |
| 463 | Tab t = getCurrentTab(); |
| 464 | if (t != null && tabMatchesUrl(t, url)) { |
| 465 | return t; |
| 466 | } |
| 467 | // Now check all the rest. |
| 468 | final int size = getTabCount(); |
| 469 | for (int i = 0; i < size; i++) { |
| 470 | t = getTab(i); |
| 471 | if (tabMatchesUrl(t, url)) { |
| 472 | return t; |
| 473 | } |
| 474 | } |
| 475 | return null; |
| 476 | } |
| 477 | |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 478 | /** |
| 479 | * Recreate the main WebView of the given tab. Returns true if the WebView |
| 480 | * was deleted. |
| 481 | */ |
| 482 | boolean recreateWebView(Tab t, String url) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 483 | final WebView w = t.getWebView(); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 484 | if (w != null) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 485 | if (url != null && url.equals(t.getOriginalUrl())) { |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 486 | // The original url matches the current url. Just go back to the |
| 487 | // first history item so we can load it faster than if we |
| 488 | // rebuilt the WebView. |
| 489 | final WebBackForwardList list = w.copyBackForwardList(); |
| 490 | if (list != null) { |
| 491 | w.goBackOrForward(-list.getCurrentIndex()); |
| 492 | w.clearHistory(); // maintains the current page. |
| 493 | return false; |
| 494 | } |
| 495 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 496 | t.destroy(); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 497 | } |
| 498 | // Create a new WebView. If this tab is the current tab, we need to put |
| 499 | // back all the clients so force it to be the current tab. |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 500 | t.setWebView(createNewWebView()); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 501 | if (getCurrentTab() == t) { |
| 502 | setCurrentTab(t, true); |
| 503 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 504 | // Clear the saved state and picker data |
| 505 | t.setSavedState(null); |
| 506 | t.clearPickerData(); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 507 | // Save the new url in order to avoid deleting the WebView. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 508 | t.setOriginalUrl(url); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 509 | return true; |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Creates a new WebView and registers it with the global settings. |
| 514 | */ |
| 515 | private WebView createNewWebView() { |
| 516 | // Create a new WebView |
| 517 | WebView w = new WebView(mActivity); |
Grace Kloba | 67f0a56 | 2009-09-28 21:24:51 -0700 | [diff] [blame] | 518 | w.setScrollbarFadingEnabled(true); |
| 519 | w.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 520 | w.setMapTrackballToArrowKeys(false); // use trackball directly |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 521 | // Enable the built-in zoom |
| 522 | w.getSettings().setBuiltInZoomControls(true); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 523 | // Attach DownloadManager so that downloads can start in an active or |
| 524 | // a non-active window. This can happen when going to a site that does |
| 525 | // a redirect after a period of time. The user could have switched to |
| 526 | // another tab while waiting for the download to start. |
| 527 | w.setDownloadListener(mActivity); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 528 | // Add this WebView to the settings observer list and update the |
| 529 | // settings |
| 530 | final BrowserSettings s = BrowserSettings.getInstance(); |
| 531 | s.addObserver(w.getSettings()).update(s, null); |
Mike Reed | d5a80a5 | 2009-11-12 12:49:34 -0500 | [diff] [blame] | 532 | |
| 533 | // disable for now |
| 534 | //w.setDragTracker(new MeshTracker()); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 535 | return w; |
| 536 | } |
| 537 | |
| 538 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 539 | * Put the current tab in the background and set newTab as the current tab. |
| 540 | * @param newTab The new tab. If newTab is null, the current tab is not |
| 541 | * set. |
| 542 | */ |
| 543 | boolean setCurrentTab(Tab newTab) { |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 544 | return setCurrentTab(newTab, false); |
| 545 | } |
| 546 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 547 | void pauseCurrentTab() { |
Mike Reed | 7bfa63b | 2009-05-28 11:08:32 -0400 | [diff] [blame] | 548 | Tab t = getCurrentTab(); |
| 549 | if (t != null) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 550 | t.pause(); |
Mike Reed | 7bfa63b | 2009-05-28 11:08:32 -0400 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 554 | void resumeCurrentTab() { |
Mike Reed | 7bfa63b | 2009-05-28 11:08:32 -0400 | [diff] [blame] | 555 | Tab t = getCurrentTab(); |
| 556 | if (t != null) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 557 | t.resume(); |
Mike Reed | 7bfa63b | 2009-05-28 11:08:32 -0400 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 561 | /** |
| 562 | * If force is true, this method skips the check for newTab == current. |
| 563 | */ |
| 564 | private boolean setCurrentTab(Tab newTab, boolean force) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 565 | Tab current = getTab(mCurrentTab); |
The Android Open Source Project | f59ec87 | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 566 | if (current == newTab && !force) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 567 | return true; |
| 568 | } |
| 569 | if (current != null) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 570 | current.putInBackground(); |
| 571 | mCurrentTab = -1; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 572 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 573 | if (newTab == null) { |
| 574 | return false; |
| 575 | } |
| 576 | |
| 577 | // Move the newTab to the end of the queue |
| 578 | int index = mTabQueue.indexOf(newTab); |
| 579 | if (index != -1) { |
| 580 | mTabQueue.remove(index); |
| 581 | } |
| 582 | mTabQueue.add(newTab); |
| 583 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 584 | // Display the new current tab |
| 585 | mCurrentTab = mTabs.indexOf(newTab); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 586 | WebView mainView = newTab.getWebView(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 587 | boolean needRestore = (mainView == null); |
| 588 | if (needRestore) { |
| 589 | // Same work as in createNewTab() except don't do new Tab() |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 590 | mainView = createNewWebView(); |
| 591 | newTab.setWebView(mainView); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 592 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 593 | newTab.putInForeground(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 594 | if (needRestore) { |
| 595 | // Have to finish setCurrentTab work before calling restoreState |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 596 | if (!newTab.restoreState(newTab.getSavedState())) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 597 | mainView.loadUrl(BrowserSettings.getInstance().getHomePage()); |
| 598 | } |
| 599 | } |
| 600 | return true; |
| 601 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 602 | } |