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