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