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