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 | |
| 19 | import android.content.Context; |
| 20 | import android.net.http.SslError; |
| 21 | import android.os.Bundle; |
| 22 | import android.os.Message; |
| 23 | import android.util.Config; |
| 24 | import android.util.Log; |
| 25 | import android.view.Gravity; |
| 26 | import android.view.LayoutInflater; |
| 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
| 29 | import android.view.View.OnClickListener; |
| 30 | import android.webkit.HttpAuthHandler; |
| 31 | import android.webkit.JsPromptResult; |
| 32 | import android.webkit.JsResult; |
| 33 | import android.webkit.SslErrorHandler; |
| 34 | import android.webkit.WebBackForwardList; |
| 35 | import android.webkit.WebChromeClient; |
| 36 | import android.webkit.WebHistoryItem; |
| 37 | import android.webkit.WebView; |
| 38 | import android.webkit.WebViewClient; |
| 39 | import android.widget.FrameLayout; |
| 40 | import android.widget.ImageButton; |
| 41 | |
| 42 | import java.io.File; |
| 43 | import java.util.ArrayList; |
| 44 | import java.util.Vector; |
| 45 | |
| 46 | class TabControl { |
| 47 | // Log Tag |
| 48 | private static final String LOGTAG = "TabControl"; |
| 49 | // Maximum number of tabs. |
| 50 | static final int MAX_TABS = 8; |
| 51 | // Static instance of an empty callback. |
| 52 | private static final WebViewClient mEmptyClient = |
| 53 | new WebViewClient(); |
| 54 | // Instance of BackgroundChromeClient for background tabs. |
| 55 | private final BackgroundChromeClient mBackgroundChromeClient = |
| 56 | new BackgroundChromeClient(); |
| 57 | // Private array of WebViews that are used as tabs. |
| 58 | private ArrayList<Tab> mTabs = new ArrayList<Tab>(MAX_TABS); |
| 59 | // Queue of most recently viewed tabs. |
| 60 | private ArrayList<Tab> mTabQueue = new ArrayList<Tab>(MAX_TABS); |
| 61 | // Current position in mTabs. |
| 62 | private int mCurrentTab = -1; |
| 63 | // A private instance of BrowserActivity to interface with when adding and |
| 64 | // switching between tabs. |
| 65 | private final BrowserActivity mActivity; |
| 66 | // Inflation service for making subwindows. |
| 67 | private final LayoutInflater mInflateService; |
| 68 | // Subclass of WebViewClient used in subwindows to notify the main |
| 69 | // WebViewClient of certain WebView activities. |
| 70 | private class SubWindowClient extends WebViewClient { |
| 71 | // The main WebViewClient. |
| 72 | private final WebViewClient mClient; |
| 73 | |
| 74 | SubWindowClient(WebViewClient client) { |
| 75 | mClient = client; |
| 76 | } |
| 77 | @Override |
| 78 | public void doUpdateVisitedHistory(WebView view, String url, |
| 79 | boolean isReload) { |
| 80 | mClient.doUpdateVisitedHistory(view, url, isReload); |
| 81 | } |
| 82 | @Override |
| 83 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| 84 | return mClient.shouldOverrideUrlLoading(view, url); |
| 85 | } |
| 86 | @Override |
| 87 | public void onReceivedSslError(WebView view, SslErrorHandler handler, |
| 88 | SslError error) { |
| 89 | mClient.onReceivedSslError(view, handler, error); |
| 90 | } |
| 91 | @Override |
| 92 | public void onReceivedHttpAuthRequest(WebView view, |
| 93 | HttpAuthHandler handler, String host, String realm) { |
| 94 | mClient.onReceivedHttpAuthRequest(view, handler, host, realm); |
| 95 | } |
| 96 | @Override |
| 97 | public void onFormResubmission(WebView view, Message dontResend, |
| 98 | Message resend) { |
| 99 | mClient.onFormResubmission(view, dontResend, resend); |
| 100 | } |
| 101 | @Override |
| 102 | public void onReceivedError(WebView view, int errorCode, |
| 103 | String description, String failingUrl) { |
| 104 | mClient.onReceivedError(view, errorCode, description, failingUrl); |
| 105 | } |
| 106 | } |
| 107 | // Subclass of WebChromeClient to display javascript dialogs. |
| 108 | private class SubWindowChromeClient extends WebChromeClient { |
| 109 | // This subwindow's tab. |
| 110 | private final Tab mTab; |
| 111 | // The main WebChromeClient. |
| 112 | private final WebChromeClient mClient; |
| 113 | |
| 114 | SubWindowChromeClient(Tab t, WebChromeClient client) { |
| 115 | mTab = t; |
| 116 | mClient = client; |
| 117 | } |
| 118 | @Override |
| 119 | public void onProgressChanged(WebView view, int newProgress) { |
| 120 | mClient.onProgressChanged(view, newProgress); |
| 121 | } |
| 122 | @Override |
| 123 | public boolean onCreateWindow(WebView view, boolean dialog, |
| 124 | boolean userGesture, android.os.Message resultMsg) { |
| 125 | return mClient.onCreateWindow(view, dialog, userGesture, resultMsg); |
| 126 | } |
| 127 | @Override |
| 128 | public void onCloseWindow(WebView window) { |
| 129 | if (Config.DEBUG && window != mTab.mSubView) { |
| 130 | throw new AssertionError("Can't close the window"); |
| 131 | } |
| 132 | mActivity.dismissSubWindow(mTab); |
| 133 | } |
| 134 | } |
| 135 | // Background WebChromeClient for focusing tabs |
| 136 | private class BackgroundChromeClient extends WebChromeClient { |
| 137 | @Override |
| 138 | public void onRequestFocus(WebView view) { |
| 139 | Tab t = getTabFromView(view); |
| 140 | if (t != getCurrentTab()) { |
| 141 | mActivity.showTab(t); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Private class for maintaining Tabs with a main WebView and a subwindow. |
| 148 | */ |
| 149 | public class Tab { |
| 150 | // Main WebView |
| 151 | private WebView mMainView; |
| 152 | // Subwindow WebView |
| 153 | private WebView mSubView; |
| 154 | // Subwindow container |
| 155 | private View mSubViewContainer; |
| 156 | // Subwindow callback |
| 157 | private SubWindowClient mSubViewClient; |
| 158 | // Subwindow chrome callback |
| 159 | private SubWindowChromeClient mSubViewChromeClient; |
| 160 | // Saved bundle for when we are running low on memory. It contains the |
| 161 | // information needed to restore the WebView if the user goes back to |
| 162 | // the tab. |
| 163 | private Bundle mSavedState; |
| 164 | // Extra saved information for displaying the tab in the picker. |
| 165 | private String mUrl; |
| 166 | private String mTitle; |
| 167 | |
| 168 | // Parent Tab. This is the Tab that created this Tab, or null |
| 169 | // if the Tab was created by the UI |
| 170 | private Tab mParentTab; |
| 171 | // Tab that constructed by this Tab. This is used when this |
| 172 | // Tab is destroyed, it clears all mParentTab values in the |
| 173 | // children. |
| 174 | private Vector<Tab> mChildTabs; |
| 175 | |
| 176 | private Boolean mCloseOnExit; |
| 177 | |
| 178 | // Construct a new tab |
| 179 | private Tab(WebView w, boolean closeOnExit) { |
| 180 | mMainView = w; |
| 181 | mCloseOnExit = closeOnExit; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Return the top window of this tab; either the subwindow if it is not |
| 186 | * null or the main window. |
| 187 | * @return The top window of this tab. |
| 188 | */ |
| 189 | public WebView getTopWindow() { |
| 190 | if (mSubView != null) { |
| 191 | return mSubView; |
| 192 | } |
| 193 | return mMainView; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Return the main window of this tab. Note: if a tab is freed in the |
| 198 | * background, this can return null. It is only guaranteed to be |
| 199 | * non-null for the current tab. |
| 200 | * @return The main WebView of this tab. |
| 201 | */ |
| 202 | public WebView getWebView() { |
| 203 | return mMainView; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Return the subwindow of this tab or null if there is no subwindow. |
| 208 | * @return The subwindow of this tab or null. |
| 209 | */ |
| 210 | public WebView getSubWebView() { |
| 211 | return mSubView; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Return the subwindow container of this tab or null if there is no |
| 216 | * subwindow. |
| 217 | * @return The subwindow's container View. |
| 218 | */ |
| 219 | public View getSubWebViewContainer() { |
| 220 | return mSubViewContainer; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Get the url of this tab. Valid after calling populatePickerData, but |
| 225 | * before calling wipePickerData, or if the webview has been destroyed. |
| 226 | * |
| 227 | * @return The WebView's url or null. |
| 228 | */ |
| 229 | public String getUrl() { |
| 230 | return mUrl; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Get the title of this tab. Valid after calling populatePickerData, |
| 235 | * but before calling wipePickerData, or if the webview has been |
| 236 | * destroyed. If the url has no title, use the url instead. |
| 237 | * |
| 238 | * @return The WebView's title (or url) or null. |
| 239 | */ |
| 240 | public String getTitle() { |
| 241 | return mTitle; |
| 242 | } |
| 243 | |
| 244 | private void setParentTab(Tab parent) { |
| 245 | mParentTab = parent; |
| 246 | // This tab may have been freed due to low memory. If that is the |
| 247 | // case, the parent tab index is already saved. If we are changing |
| 248 | // that index (most likely due to removing the parent tab) we must |
| 249 | // update the parent tab index in the saved Bundle. |
| 250 | if (mSavedState != null) { |
| 251 | if (parent == null) { |
| 252 | mSavedState.remove(PARENTTAB); |
| 253 | } else { |
| 254 | mSavedState.putInt(PARENTTAB, getTabIndex(parent)); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * When a Tab is created through the content of another Tab, then |
| 261 | * we associate the Tabs. |
| 262 | * @param child the Tab that was created from this Tab |
| 263 | */ |
| 264 | public void addChildTab(Tab child) { |
| 265 | if (mChildTabs == null) { |
| 266 | mChildTabs = new Vector<Tab>(); |
| 267 | } |
| 268 | mChildTabs.add(child); |
| 269 | child.setParentTab(this); |
| 270 | } |
| 271 | |
| 272 | private void removeFromTree() { |
| 273 | // detach the children |
| 274 | if (mChildTabs != null) { |
| 275 | for(Tab t : mChildTabs) { |
| 276 | t.setParentTab(null); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // Find myself in my parent list |
| 281 | if (mParentTab != null) { |
| 282 | mParentTab.mChildTabs.remove(this); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * If this Tab was created through another Tab, then this method |
| 288 | * returns that Tab. |
| 289 | * @return the Tab parent or null |
| 290 | */ |
| 291 | public Tab getParentTab() { |
| 292 | return mParentTab; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Return whether this tab should be closed when it is backing out of |
| 297 | * the first page. |
| 298 | * @return TRUE if this tab should be closed when exit. |
| 299 | */ |
| 300 | public boolean closeOnExit() { |
| 301 | return mCloseOnExit; |
| 302 | } |
| 303 | }; |
| 304 | |
| 305 | // Directory to store thumbnails for each WebView. |
| 306 | private final File mThumbnailDir; |
| 307 | |
| 308 | /** |
| 309 | * Construct a new TabControl object that interfaces with the given |
| 310 | * BrowserActivity instance. |
| 311 | * @param activity A BrowserActivity instance that TabControl will interface |
| 312 | * with. |
| 313 | */ |
| 314 | TabControl(BrowserActivity activity) { |
| 315 | mActivity = activity; |
| 316 | mInflateService = |
| 317 | ((LayoutInflater) activity.getSystemService( |
| 318 | Context.LAYOUT_INFLATER_SERVICE)); |
| 319 | mThumbnailDir = activity.getDir("thumbnails", 0); |
| 320 | } |
| 321 | |
| 322 | File getThumbnailDir() { |
| 323 | return mThumbnailDir; |
| 324 | } |
| 325 | |
| 326 | BrowserActivity getBrowserActivity() { |
| 327 | return mActivity; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Return the current tab's main WebView. This will always return the main |
| 332 | * WebView for a given tab and not a subwindow. |
| 333 | * @return The current tab's WebView. |
| 334 | */ |
| 335 | WebView getCurrentWebView() { |
| 336 | Tab t = getTab(mCurrentTab); |
| 337 | if (t == null) { |
| 338 | return null; |
| 339 | } |
| 340 | return t.mMainView; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Return the current tab's top-level WebView. This can return a subwindow |
| 345 | * if one exists. |
| 346 | * @return The top-level WebView of the current tab. |
| 347 | */ |
| 348 | WebView getCurrentTopWebView() { |
| 349 | Tab t = getTab(mCurrentTab); |
| 350 | if (t == null) { |
| 351 | return null; |
| 352 | } |
| 353 | return t.mSubView != null ? t.mSubView : t.mMainView; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Return the current tab's subwindow if it exists. |
| 358 | * @return The subwindow of the current tab or null if it doesn't exist. |
| 359 | */ |
| 360 | WebView getCurrentSubWindow() { |
| 361 | Tab t = getTab(mCurrentTab); |
| 362 | if (t == null) { |
| 363 | return null; |
| 364 | } |
| 365 | return t.mSubView; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Return the tab at the specified index. |
| 370 | * @return The Tab for the specified index or null if the tab does not |
| 371 | * exist. |
| 372 | */ |
| 373 | Tab getTab(int index) { |
| 374 | if (index >= 0 && index < mTabs.size()) { |
| 375 | return mTabs.get(index); |
| 376 | } |
| 377 | return null; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Return the current tab. |
| 382 | * @return The current tab. |
| 383 | */ |
| 384 | Tab getCurrentTab() { |
| 385 | return getTab(mCurrentTab); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Return the current tab index. |
| 390 | * @return The current tab index |
| 391 | */ |
| 392 | int getCurrentIndex() { |
| 393 | return mCurrentTab; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Given a Tab, find it's index |
| 398 | * @param Tab to find |
| 399 | * @return index of Tab or -1 if not found |
| 400 | */ |
| 401 | int getTabIndex(Tab tab) { |
| 402 | return mTabs.indexOf(tab); |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Create a new tab and display the new tab immediately. |
| 407 | * @return The newly createTab or null if we have reached the maximum |
| 408 | * number of open tabs. |
| 409 | */ |
| 410 | Tab createNewTab(boolean closeOnExit) { |
| 411 | int size = mTabs.size(); |
| 412 | // Return false if we have maxed out on tabs |
| 413 | if (MAX_TABS == size) { |
| 414 | return null; |
| 415 | } |
| 416 | // Create a new WebView |
| 417 | WebView w = new WebView(mActivity); |
| 418 | w.setMapTrackballToArrowKeys(false); // use trackball directly |
| 419 | // Add this WebView to the settings observer list and update the |
| 420 | // settings |
| 421 | final BrowserSettings s = BrowserSettings.getInstance(); |
| 422 | s.addObserver(w.getSettings()).update(s, null); |
| 423 | // Create a new tab and add it to the tab list |
| 424 | Tab t = new Tab(w, closeOnExit); |
| 425 | mTabs.add(t); |
| 426 | return t; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Remove the tab from the list. If the tab is the current tab shown, the |
| 431 | * last created tab will be shown. |
| 432 | * @param t The tab to be removed. |
| 433 | */ |
| 434 | boolean removeTab(Tab t) { |
| 435 | if (t == null) { |
| 436 | return false; |
| 437 | } |
| 438 | // Only remove the tab if it is the current one. |
| 439 | if (getCurrentTab() == t) { |
| 440 | putTabInBackground(t); |
| 441 | } |
| 442 | |
| 443 | // Only destroy the WebView if it still exists. |
| 444 | if (t.mMainView != null) { |
| 445 | // Take down the sub window. |
| 446 | dismissSubWindow(t); |
| 447 | // Remove the WebView's settings from the BrowserSettings list of |
| 448 | // observers. |
| 449 | BrowserSettings.getInstance().deleteObserver( |
| 450 | t.mMainView.getSettings()); |
| 451 | // Destroy the main view and subview |
| 452 | t.mMainView.destroy(); |
| 453 | t.mMainView = null; |
| 454 | } |
| 455 | // clear it's references to parent and children |
| 456 | t.removeFromTree(); |
| 457 | |
| 458 | // Remove it from our list of tabs. |
| 459 | mTabs.remove(t); |
| 460 | |
| 461 | // The tab indices have shifted, update all the saved state so we point |
| 462 | // to the correct index. |
| 463 | for (Tab tab : mTabs) { |
| 464 | if (tab.mChildTabs != null) { |
| 465 | for (Tab child : tab.mChildTabs) { |
| 466 | child.setParentTab(tab); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | |
| 472 | // This tab may have been pushed in to the background and then closed. |
| 473 | // If the saved state contains a picture file, delete the file. |
| 474 | if (t.mSavedState != null) { |
| 475 | if (t.mSavedState.containsKey("picture")) { |
| 476 | new File(t.mSavedState.getString("picture")).delete(); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | // Remove it from the queue of viewed tabs. |
| 481 | mTabQueue.remove(t); |
| 482 | mCurrentTab = -1; |
| 483 | return true; |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Clear the back/forward list for all the current tabs. |
| 488 | */ |
| 489 | void clearHistory() { |
| 490 | int size = getTabCount(); |
| 491 | for (int i = 0; i < size; i++) { |
| 492 | Tab t = mTabs.get(i); |
| 493 | // TODO: if a tab is freed due to low memory, its history is not |
| 494 | // cleared here. |
| 495 | if (t.mMainView != null) { |
| 496 | t.mMainView.clearHistory(); |
| 497 | } |
| 498 | if (t.mSubView != null) { |
| 499 | t.mSubView.clearHistory(); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Destroy all the tabs and subwindows |
| 506 | */ |
| 507 | void destroy() { |
| 508 | BrowserSettings s = BrowserSettings.getInstance(); |
| 509 | for (Tab t : mTabs) { |
| 510 | if (t.mMainView != null) { |
| 511 | dismissSubWindow(t); |
| 512 | s.deleteObserver(t.mMainView.getSettings()); |
| 513 | t.mMainView.destroy(); |
| 514 | t.mMainView = null; |
| 515 | } |
| 516 | } |
| 517 | mTabs.clear(); |
| 518 | mTabQueue.clear(); |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Returns the number of tabs created. |
| 523 | * @return The number of tabs created. |
| 524 | */ |
| 525 | int getTabCount() { |
| 526 | return mTabs.size(); |
| 527 | } |
| 528 | |
| 529 | // Used for saving and restoring each Tab |
| 530 | private static final String WEBVIEW = "webview"; |
| 531 | private static final String NUMTABS = "numTabs"; |
| 532 | private static final String CURRTAB = "currentTab"; |
| 533 | private static final String CURRURL = "currentUrl"; |
| 534 | private static final String CURRTITLE = "currentTitle"; |
| 535 | private static final String CLOSEONEXIT = "closeonexit"; |
| 536 | private static final String PARENTTAB = "parentTab"; |
| 537 | |
| 538 | /** |
| 539 | * Save the state of all the Tabs. |
| 540 | * @param outState The Bundle to save the state to. |
| 541 | */ |
| 542 | void saveState(Bundle outState) { |
| 543 | final int numTabs = getTabCount(); |
| 544 | outState.putInt(NUMTABS, numTabs); |
| 545 | final int index = getCurrentIndex(); |
| 546 | outState.putInt(CURRTAB, (index >= 0 && index < numTabs) ? index : 0); |
| 547 | for (int i = 0; i < numTabs; i++) { |
| 548 | final Tab t = getTab(i); |
| 549 | if (saveState(t)) { |
| 550 | outState.putBundle(WEBVIEW + i, t.mSavedState); |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * Restore the state of all the tabs. |
| 557 | * @param inState The saved state of all the tabs. |
| 558 | * @return True if there were previous tabs that were restored. False if |
| 559 | * there was no saved state or restoring the state failed. |
| 560 | */ |
| 561 | boolean restoreState(Bundle inState) { |
| 562 | final int numTabs = (inState == null) |
| 563 | ? -1 : inState.getInt(NUMTABS, -1); |
| 564 | if (numTabs == -1) { |
| 565 | return false; |
| 566 | } else { |
| 567 | final int currentTab = inState.getInt(CURRTAB, -1); |
| 568 | for (int i = 0; i < numTabs; i++) { |
| 569 | if (i == currentTab) { |
| 570 | Tab t = createNewTab(false); |
| 571 | // Me must set the current tab before restoring the state |
| 572 | // so that all the client classes are set. |
| 573 | setCurrentTab(t); |
| 574 | if (!restoreState(inState.getBundle(WEBVIEW + i), t)) { |
| 575 | Log.w(LOGTAG, "Fail in restoreState, load home page."); |
| 576 | t.mMainView.loadUrl(BrowserSettings.getInstance() |
| 577 | .getHomePage()); |
| 578 | } |
| 579 | } else { |
| 580 | // Create a new tab and don't restore the state yet, add it |
| 581 | // to the tab list |
| 582 | Tab t = new Tab(null, false); |
| 583 | t.mSavedState = inState.getBundle(WEBVIEW + i); |
| 584 | if (t.mSavedState != null) { |
| 585 | t.mUrl = t.mSavedState.getString(CURRURL); |
| 586 | t.mTitle = t.mSavedState.getString(CURRTITLE); |
| 587 | } |
| 588 | mTabs.add(t); |
| 589 | mTabQueue.add(t); |
| 590 | } |
| 591 | } |
| 592 | // Rebuild the tree of tabs. Do this after all tabs have been |
| 593 | // created/restored so that the parent tab exists. |
| 594 | for (int i = 0; i < numTabs; i++) { |
| 595 | final Bundle b = inState.getBundle(WEBVIEW + i); |
| 596 | final Tab t = getTab(i); |
| 597 | if (b != null && t != null) { |
| 598 | final int parentIndex = b.getInt(PARENTTAB, -1); |
| 599 | if (parentIndex != -1) { |
| 600 | final Tab parent = getTab(parentIndex); |
| 601 | if (parent != null) { |
| 602 | parent.addChildTab(t); |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | return true; |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Free the memory in this order, 1) free the background tab; 2) free the |
| 613 | * WebView cache; |
| 614 | */ |
| 615 | void freeMemory() { |
| 616 | // free the least frequently used background tab |
| 617 | Tab t = getLeastUsedTab(); |
| 618 | if (t != null) { |
| 619 | Log.w(LOGTAG, "Free a tab in the browser"); |
| 620 | freeTab(t); |
| 621 | // force a gc |
| 622 | System.gc(); |
| 623 | return; |
| 624 | } |
| 625 | |
| 626 | // free the WebView cache |
| 627 | Log.w(LOGTAG, "Free WebView cache"); |
| 628 | WebView view = getCurrentWebView(); |
| 629 | if (view != null) { |
| 630 | view.clearCache(false); |
| 631 | } |
| 632 | // force a gc |
| 633 | System.gc(); |
| 634 | } |
| 635 | |
| 636 | private Tab getLeastUsedTab() { |
| 637 | // Don't do anything if we only have 1 tab. |
| 638 | if (getTabCount() == 1) { |
| 639 | return null; |
| 640 | } |
| 641 | |
| 642 | // Rip through the queue starting at the beginning and teardown the |
| 643 | // next available tab. |
| 644 | Tab t = null; |
| 645 | int i = 0; |
| 646 | final int queueSize = mTabQueue.size(); |
| 647 | if (queueSize == 0) { |
| 648 | return null; |
| 649 | } |
| 650 | do { |
| 651 | t = mTabQueue.get(i++); |
| 652 | } while (i < queueSize && t != null && t.mMainView == null); |
| 653 | |
| 654 | // Don't do anything if the last remaining tab is the current one. |
| 655 | if (t == getCurrentTab()) { |
| 656 | return null; |
| 657 | } |
| 658 | |
| 659 | return t; |
| 660 | } |
| 661 | |
| 662 | private void freeTab(Tab t) { |
| 663 | // Store the WebView's state. |
| 664 | saveState(t); |
| 665 | |
| 666 | // Tear down the tab. |
| 667 | dismissSubWindow(t); |
| 668 | // Remove the WebView's settings from the BrowserSettings list of |
| 669 | // observers. |
| 670 | BrowserSettings.getInstance().deleteObserver(t.mMainView.getSettings()); |
| 671 | t.mMainView.destroy(); |
| 672 | t.mMainView = null; |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Create a new subwindow unless a subwindow already exists. |
| 677 | * @return True if a new subwindow was created. False if one already exists. |
| 678 | */ |
| 679 | void createSubWindow() { |
| 680 | Tab t = getTab(mCurrentTab); |
| 681 | if (t != null && t.mSubView == null) { |
| 682 | final View v = mInflateService.inflate(R.layout.browser_subwindow, null); |
| 683 | final WebView w = (WebView) v.findViewById(R.id.webview); |
| 684 | w.setMapTrackballToArrowKeys(false); // use trackball directly |
| 685 | final SubWindowClient subClient = |
| 686 | new SubWindowClient(mActivity.getWebViewClient()); |
| 687 | final SubWindowChromeClient subChromeClient = |
| 688 | new SubWindowChromeClient(t, |
| 689 | mActivity.getWebChromeClient()); |
| 690 | w.setWebViewClient(subClient); |
| 691 | w.setWebChromeClient(subChromeClient); |
| 692 | w.setDownloadListener(mActivity); |
| 693 | w.setOnCreateContextMenuListener(mActivity); |
| 694 | final BrowserSettings s = BrowserSettings.getInstance(); |
| 695 | s.addObserver(w.getSettings()).update(s, null); |
| 696 | t.mSubView = w; |
| 697 | t.mSubViewClient = subClient; |
| 698 | t.mSubViewChromeClient = subChromeClient; |
| 699 | // FIXME: I really hate having to know the name of the view |
| 700 | // containing the webview. |
| 701 | t.mSubViewContainer = v.findViewById(R.id.subwindow_container); |
| 702 | final ImageButton cancel = |
| 703 | (ImageButton) v.findViewById(R.id.subwindow_close); |
| 704 | cancel.setOnClickListener(new OnClickListener() { |
| 705 | public void onClick(View v) { |
| 706 | subChromeClient.onCloseWindow(w); |
| 707 | } |
| 708 | }); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * Show the tab that contains the given WebView. |
| 714 | * @param view The WebView used to find the tab. |
| 715 | */ |
| 716 | Tab getTabFromView(WebView view) { |
| 717 | final int size = getTabCount(); |
| 718 | for (int i = 0; i < size; i++) { |
| 719 | final Tab t = getTab(i); |
| 720 | if (t.mSubView == view || t.mMainView == view) { |
| 721 | return t; |
| 722 | } |
| 723 | } |
| 724 | return null; |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * Put the current tab in the background and set newTab as the current tab. |
| 729 | * @param newTab The new tab. If newTab is null, the current tab is not |
| 730 | * set. |
| 731 | */ |
| 732 | boolean setCurrentTab(Tab newTab) { |
| 733 | Tab current = getTab(mCurrentTab); |
| 734 | if (current == newTab) { |
| 735 | return true; |
| 736 | } |
| 737 | if (current != null) { |
| 738 | // Remove the current WebView and the container of the subwindow |
| 739 | putTabInBackground(current); |
| 740 | } |
| 741 | |
| 742 | if (newTab == null) { |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | // Move the newTab to the end of the queue |
| 747 | int index = mTabQueue.indexOf(newTab); |
| 748 | if (index != -1) { |
| 749 | mTabQueue.remove(index); |
| 750 | } |
| 751 | mTabQueue.add(newTab); |
| 752 | |
| 753 | WebView mainView; |
| 754 | WebView subView; |
| 755 | |
| 756 | // Display the new current tab |
| 757 | mCurrentTab = mTabs.indexOf(newTab); |
| 758 | mainView = newTab.mMainView; |
| 759 | boolean needRestore = (mainView == null); |
| 760 | if (needRestore) { |
| 761 | // Same work as in createNewTab() except don't do new Tab() |
| 762 | newTab.mMainView = mainView = new WebView(mActivity); |
| 763 | mainView.setMapTrackballToArrowKeys(false); // use t-ball directly |
| 764 | |
| 765 | // Add this WebView to the settings observer list and update the |
| 766 | // settings |
| 767 | final BrowserSettings s = BrowserSettings.getInstance(); |
| 768 | s.addObserver(mainView.getSettings()).update(s, null); |
| 769 | } |
| 770 | mainView.setWebViewClient(mActivity.getWebViewClient()); |
| 771 | mainView.setWebChromeClient(mActivity.getWebChromeClient()); |
| 772 | mainView.setOnCreateContextMenuListener(mActivity); |
| 773 | mainView.setDownloadListener(mActivity); |
| 774 | // Add the subwindow if it exists |
| 775 | if (newTab.mSubViewContainer != null) { |
| 776 | subView = newTab.mSubView; |
| 777 | subView.setWebViewClient(newTab.mSubViewClient); |
| 778 | subView.setWebChromeClient(newTab.mSubViewChromeClient); |
| 779 | subView.setOnCreateContextMenuListener(mActivity); |
| 780 | subView.setDownloadListener(mActivity); |
| 781 | } |
| 782 | if (needRestore) { |
| 783 | // Have to finish setCurrentTab work before calling restoreState |
| 784 | if (!restoreState(newTab.mSavedState, newTab)) { |
| 785 | mainView.loadUrl(BrowserSettings.getInstance().getHomePage()); |
| 786 | } |
| 787 | } |
| 788 | return true; |
| 789 | } |
| 790 | |
| 791 | /* |
| 792 | * Put the tab in the background using all the empty/background clients. |
| 793 | */ |
| 794 | private void putTabInBackground(Tab t) { |
| 795 | WebView mainView = t.mMainView; |
| 796 | // Set an empty callback so that default actions are not triggered. |
| 797 | mainView.setWebViewClient(mEmptyClient); |
| 798 | mainView.setWebChromeClient(mBackgroundChromeClient); |
| 799 | mainView.setOnCreateContextMenuListener(null); |
| 800 | // Leave the DownloadManager attached so that downloads can start in |
| 801 | // a non-active window. This can happen when going to a site that does |
| 802 | // a redirect after a period of time. The user could have switched to |
| 803 | // another tab while waiting for the download to start. |
| 804 | mainView.setDownloadListener(mActivity); |
| 805 | WebView subView = t.mSubView; |
| 806 | if (subView != null) { |
| 807 | // Set an empty callback so that default actions are not triggered. |
| 808 | subView.setWebViewClient(mEmptyClient); |
| 809 | subView.setWebChromeClient(mBackgroundChromeClient); |
| 810 | subView.setOnCreateContextMenuListener(null); |
| 811 | subView.setDownloadListener(mActivity); |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | /* |
| 816 | * Dismiss the subwindow for the given tab. |
| 817 | */ |
| 818 | void dismissSubWindow(Tab t) { |
| 819 | if (t != null && t.mSubView != null) { |
| 820 | BrowserSettings.getInstance().deleteObserver( |
| 821 | t.mSubView.getSettings()); |
| 822 | t.mSubView.destroy(); |
| 823 | t.mSubView = null; |
| 824 | t.mSubViewContainer = null; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * Ensure that Tab t has a title, url, and favicon. |
| 830 | * @param t Tab to populate. |
| 831 | */ |
| 832 | /* package */ void populatePickerData(Tab t) { |
| 833 | if (t == null || t.mMainView == null) { |
| 834 | return; |
| 835 | } |
| 836 | // FIXME: The only place we cared about subwindow was for |
| 837 | // bookmarking (i.e. not when saving state). Was this deliberate? |
| 838 | final WebBackForwardList list = t.mMainView.copyBackForwardList(); |
| 839 | final WebHistoryItem item = |
| 840 | list != null ? list.getCurrentItem() : null; |
| 841 | populatePickerData(t, item); |
| 842 | } |
| 843 | |
| 844 | // Populate the picker data |
| 845 | private void populatePickerData(Tab t, WebHistoryItem item) { |
| 846 | if (item != null) { |
| 847 | t.mUrl = item.getUrl(); |
| 848 | t.mTitle = item.getTitle(); |
| 849 | if (t.mTitle == null) { |
| 850 | t.mTitle = t.mUrl; |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | /** |
| 856 | * Clean up the data for all tabs. |
| 857 | */ |
| 858 | /* package */ void wipeAllPickerData() { |
| 859 | int size = getTabCount(); |
| 860 | for (int i = 0; i < size; i++) { |
| 861 | final Tab t = getTab(i); |
| 862 | if (t != null && t.mSavedState == null) { |
| 863 | t.mUrl = null; |
| 864 | t.mTitle = null; |
| 865 | } |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | /* |
| 870 | * Save the state for an individual tab. |
| 871 | */ |
| 872 | private boolean saveState(Tab t) { |
| 873 | if (t != null) { |
| 874 | final WebView w = t.mMainView; |
| 875 | // If the WebView is null it means we ran low on memory and we |
| 876 | // already stored the saved state in mSavedState. |
| 877 | if (w == null) { |
| 878 | return true; |
| 879 | } |
| 880 | final Bundle b = new Bundle(); |
| 881 | final WebBackForwardList list = w.saveState(b); |
| 882 | if (list != null) { |
| 883 | final File f = new File(mThumbnailDir, w.hashCode() |
| 884 | + "_pic.save"); |
| 885 | if (w.savePicture(b, f)) { |
| 886 | b.putString("picture", f.getPath()); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | // Store some extra info for displaying the tab in the picker. |
| 891 | final WebHistoryItem item = |
| 892 | list != null ? list.getCurrentItem() : null; |
| 893 | populatePickerData(t, item); |
| 894 | if (t.mUrl != null) { |
| 895 | b.putString(CURRURL, t.mUrl); |
| 896 | } |
| 897 | if (t.mTitle != null) { |
| 898 | b.putString(CURRTITLE, t.mTitle); |
| 899 | } |
| 900 | b.putBoolean(CLOSEONEXIT, t.mCloseOnExit); |
| 901 | |
| 902 | // Remember the parent tab so the relationship can be restored. |
| 903 | if (t.mParentTab != null) { |
| 904 | b.putInt(PARENTTAB, getTabIndex(t.mParentTab)); |
| 905 | } |
| 906 | |
| 907 | // Remember the saved state. |
| 908 | t.mSavedState = b; |
| 909 | return true; |
| 910 | } |
| 911 | return false; |
| 912 | } |
| 913 | |
| 914 | /* |
| 915 | * Restore the state of the tab. |
| 916 | */ |
| 917 | private boolean restoreState(Bundle b, Tab t) { |
| 918 | if (b == null) { |
| 919 | return false; |
| 920 | } |
| 921 | final WebView w = t.mMainView; |
| 922 | final WebBackForwardList list = w.restoreState(b); |
| 923 | if (list == null) { |
| 924 | return false; |
| 925 | } |
| 926 | if (b.containsKey("picture")) { |
| 927 | final File f = new File(b.getString("picture")); |
| 928 | w.restorePicture(b, f); |
| 929 | f.delete(); |
| 930 | } |
| 931 | t.mSavedState = null; |
| 932 | t.mUrl = null; |
| 933 | t.mTitle = null; |
| 934 | t.mCloseOnExit = b.getBoolean(CLOSEONEXIT); |
| 935 | return true; |
| 936 | } |
| 937 | } |