Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.app.ActionBar; |
| 20 | import android.app.Activity; |
| 21 | import android.content.Context; |
| 22 | import android.content.res.Configuration; |
| 23 | import android.content.res.Resources; |
| 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.BitmapFactory; |
| 26 | import android.graphics.PixelFormat; |
| 27 | import android.graphics.drawable.Drawable; |
| 28 | import android.os.Bundle; |
| 29 | import android.text.TextUtils; |
| 30 | import android.util.Log; |
| 31 | import android.view.ActionMode; |
| 32 | import android.view.Gravity; |
| 33 | import android.view.LayoutInflater; |
| 34 | import android.view.Menu; |
| 35 | import android.view.MenuItem; |
| 36 | import android.view.View; |
| 37 | import android.view.ViewGroup; |
| 38 | import android.view.WindowManager; |
| 39 | import android.webkit.WebChromeClient; |
| 40 | import android.webkit.WebHistoryItem; |
| 41 | import android.webkit.WebView; |
| 42 | import android.widget.FrameLayout; |
| 43 | import android.widget.LinearLayout; |
| 44 | import android.widget.Toast; |
| 45 | |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame^] | 46 | import java.util.List; |
| 47 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 48 | /** |
| 49 | * UI interface definitions |
| 50 | */ |
| 51 | public class BaseUi implements UI, WebViewFactory { |
| 52 | |
| 53 | private static final String LOGTAG = "BaseUi"; |
| 54 | |
| 55 | private static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = |
| 56 | new FrameLayout.LayoutParams( |
| 57 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 58 | ViewGroup.LayoutParams.MATCH_PARENT); |
| 59 | |
| 60 | private static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = |
| 61 | new FrameLayout.LayoutParams( |
| 62 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 63 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 64 | Gravity.CENTER); |
| 65 | |
| 66 | Activity mActivity; |
| 67 | UiController mUiController; |
| 68 | TabControl mTabControl; |
| 69 | |
| 70 | private Drawable mSecLockIcon; |
| 71 | private Drawable mMixLockIcon; |
| 72 | |
| 73 | private boolean mXLargeScreenSize; |
| 74 | private FrameLayout mBrowserFrameLayout; |
| 75 | private FrameLayout mContentView; |
| 76 | private FrameLayout mCustomViewContainer; |
| 77 | private TitleBarBase mTitleBar; |
| 78 | private TitleBarBase mFakeTitleBar; |
| 79 | private TabBar mTabBar; |
| 80 | |
| 81 | private View mCustomView; |
| 82 | private WebChromeClient.CustomViewCallback mCustomViewCallback; |
| 83 | |
| 84 | private CombinedBookmarkHistoryView mComboView; |
| 85 | |
| 86 | private LinearLayout mErrorConsoleContainer = null; |
| 87 | |
| 88 | private Toast mStopToast; |
| 89 | private ActiveTabsPage mActiveTabsPage; |
| 90 | |
| 91 | // the default <video> poster |
| 92 | private Bitmap mDefaultVideoPoster; |
| 93 | // the video progress view |
| 94 | private View mVideoProgressView; |
| 95 | |
| 96 | boolean mExtendedMenuOpen; |
| 97 | boolean mOptionsMenuOpen; |
| 98 | |
| 99 | private boolean mActivityPaused; |
| 100 | |
| 101 | public BaseUi(Activity browser, UiController controller) { |
| 102 | mActivity = browser; |
| 103 | mUiController = controller; |
| 104 | mTabControl = controller.getTabControl(); |
| 105 | Resources res = mActivity.getResources(); |
| 106 | mSecLockIcon = res.getDrawable(R.drawable.ic_secure); |
| 107 | mMixLockIcon = res.getDrawable(R.drawable.ic_partial_secure); |
| 108 | |
| 109 | |
| 110 | mXLargeScreenSize = (res.getConfiguration().screenLayout |
| 111 | & Configuration.SCREENLAYOUT_SIZE_MASK) |
| 112 | == Configuration.SCREENLAYOUT_SIZE_XLARGE; |
| 113 | |
| 114 | FrameLayout frameLayout = (FrameLayout) mActivity.getWindow() |
| 115 | .getDecorView().findViewById(android.R.id.content); |
| 116 | mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(mActivity) |
| 117 | .inflate(R.layout.custom_screen, null); |
| 118 | mContentView = (FrameLayout) mBrowserFrameLayout.findViewById( |
| 119 | R.id.main_content); |
| 120 | mErrorConsoleContainer = (LinearLayout) mBrowserFrameLayout |
| 121 | .findViewById(R.id.error_console); |
| 122 | mCustomViewContainer = (FrameLayout) mBrowserFrameLayout |
| 123 | .findViewById(R.id.fullscreen_custom_content); |
| 124 | frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS); |
| 125 | |
| 126 | if (mXLargeScreenSize) { |
| 127 | mTitleBar = new TitleBarXLarge(mActivity, mUiController); |
| 128 | mTitleBar.setProgress(100); |
| 129 | mFakeTitleBar = new TitleBarXLarge(mActivity, mUiController); |
| 130 | ActionBar actionBar = mActivity.getActionBar(); |
| 131 | mTabBar = new TabBar(mActivity, mUiController, this); |
| 132 | actionBar.setCustomNavigationMode(mTabBar); |
| 133 | } else { |
| 134 | mTitleBar = new TitleBar(mActivity, mUiController); |
| 135 | // mTitleBar will be always be shown in the fully loaded mode on |
| 136 | // phone |
| 137 | mTitleBar.setProgress(100); |
| 138 | mFakeTitleBar = new TitleBar(mActivity, mUiController); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // webview factory |
| 143 | |
| 144 | @Override |
| 145 | public WebView createWebView(boolean privateBrowsing) { |
| 146 | // Create a new WebView |
| 147 | ScrollWebView w = new ScrollWebView(mActivity, null, |
| 148 | android.R.attr.webViewStyle, privateBrowsing); |
| 149 | w.setScrollbarFadingEnabled(true); |
| 150 | w.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY); |
| 151 | w.setMapTrackballToArrowKeys(false); // use trackball directly |
| 152 | // Enable the built-in zoom |
| 153 | w.getSettings().setBuiltInZoomControls(true); |
| 154 | if (mXLargeScreenSize) { |
| 155 | w.setScrollListener(mTabBar); |
| 156 | w.getSettings().setDisplayZoomControls(false); |
| 157 | } |
| 158 | |
| 159 | // Add this WebView to the settings observer list and update the |
| 160 | // settings |
| 161 | final BrowserSettings s = BrowserSettings.getInstance(); |
| 162 | s.addObserver(w.getSettings()).update(s, null); |
| 163 | return w; |
| 164 | } |
| 165 | |
| 166 | void stopWebViewScrolling() { |
| 167 | ScrollWebView web = (ScrollWebView) mUiController.getCurrentWebView(); |
| 168 | if (web != null) { |
| 169 | web.stopScroll(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | private void cancelStopToast() { |
| 174 | if (mStopToast != null) { |
| 175 | mStopToast.cancel(); |
| 176 | mStopToast = null; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // lifecycle |
| 181 | |
| 182 | public void onPause() { |
| 183 | // FIXME: This removes the active tabs page and resets the menu to |
| 184 | // MAIN_MENU. A better solution might be to do this work in onNewIntent |
| 185 | // but then we would need to save it in onSaveInstanceState and restore |
| 186 | // it in onCreate/onRestoreInstanceState |
| 187 | if (mActiveTabsPage != null) { |
| 188 | mUiController.removeActiveTabsPage(true); |
| 189 | } |
| 190 | cancelStopToast(); |
| 191 | mActivityPaused = true; |
| 192 | } |
| 193 | |
| 194 | public void onResume() { |
| 195 | mActivityPaused = false; |
| 196 | } |
| 197 | |
| 198 | public void onDestroy() { |
| 199 | hideFakeTitleBar(); |
| 200 | } |
| 201 | |
| 202 | public void onConfigurationChanged(Configuration config) { |
| 203 | } |
| 204 | |
| 205 | // key handling |
| 206 | |
| 207 | @Override |
| 208 | public boolean onBackKey() { |
| 209 | if (mActiveTabsPage != null) { |
| 210 | // if tab page is showing, hide it |
| 211 | mUiController.removeActiveTabsPage(true); |
| 212 | return true; |
| 213 | } |
| 214 | if (mComboView != null) { |
| 215 | if (!mComboView.onBackPressed()) { |
| 216 | mUiController.removeComboView(); |
| 217 | } |
| 218 | return true; |
| 219 | } |
| 220 | if (mCustomView != null) { |
| 221 | mUiController.hideCustomView(); |
| 222 | return true; |
| 223 | } |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | // WebView callbacks |
| 228 | |
| 229 | @Override |
| 230 | public void onPageStarted(Tab tab, String url, Bitmap favicon) { |
| 231 | if (mXLargeScreenSize) { |
| 232 | mTabBar.onPageStarted(tab, url, favicon); |
| 233 | } |
| 234 | if (tab.inForeground()) { |
| 235 | resetLockIcon(tab, url); |
| 236 | setUrlTitle(tab, url, null); |
| 237 | setFavicon(tab, favicon); |
| 238 | } |
| 239 | |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public void onPageFinished(Tab tab, String url) { |
| 244 | if (mXLargeScreenSize) { |
| 245 | mTabBar.onPageFinished(tab); |
| 246 | } |
| 247 | if (tab.inForeground()) { |
| 248 | // Reset the title and icon in case we stopped a provisional load. |
| 249 | resetTitleAndIcon(tab); |
| 250 | // Update the lock icon image only once we are done loading |
| 251 | updateLockIconToLatest(tab); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public void onPageStopped(Tab tab) { |
| 257 | cancelStopToast(); |
| 258 | if (tab.inForeground()) { |
| 259 | mStopToast = Toast |
| 260 | .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT); |
| 261 | mStopToast.show(); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public void onProgressChanged(Tab tab, int progress) { |
| 267 | if (mXLargeScreenSize) { |
| 268 | mTabBar.onProgress(tab, progress); |
| 269 | } |
| 270 | if (tab.inForeground()) { |
| 271 | mFakeTitleBar.setProgress(progress); |
| 272 | if (progress == 100) { |
| 273 | if (!mOptionsMenuOpen || !mExtendedMenuOpen) { |
| 274 | hideFakeTitleBar(); |
| 275 | } |
| 276 | } else { |
| 277 | if (!mOptionsMenuOpen || mExtendedMenuOpen) { |
| 278 | showFakeTitleBar(); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | @Override |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame^] | 285 | public boolean needsRestoreAllTabs() { |
| 286 | return mXLargeScreenSize; |
| 287 | } |
| 288 | |
| 289 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 290 | public void addTab(Tab tab) { |
| 291 | if (mXLargeScreenSize) { |
| 292 | mTabBar.onNewTab(tab); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public void setActiveTab(Tab tab) { |
| 298 | Tab current = mTabControl.getCurrentTab(); |
| 299 | if ((tab != current) && (current != null)) { |
| 300 | removeTabFromContentView(current); |
| 301 | } |
| 302 | attachTabToContentView(tab); |
| 303 | setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole()); |
| 304 | |
| 305 | WebView view = tab.getWebView(); |
| 306 | view.setEmbeddedTitleBar(mTitleBar); |
| 307 | if (tab.isInVoiceSearchMode()) { |
| 308 | showVoiceTitleBar(tab.getVoiceDisplayTitle()); |
| 309 | } else { |
| 310 | revertVoiceTitleBar(tab); |
| 311 | } |
| 312 | |
| 313 | if (mXLargeScreenSize) { |
| 314 | // Request focus on the top window. |
| 315 | mTabBar.onSetActiveTab(tab); |
| 316 | } |
| 317 | resetTitleIconAndProgress(tab); |
| 318 | updateLockIconToLatest(tab); |
| 319 | tab.getTopWindow().requestFocus(); |
| 320 | } |
| 321 | |
| 322 | @Override |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame^] | 323 | public void updateTabs(List<Tab> tabs) { |
| 324 | if (mXLargeScreenSize) { |
| 325 | mTabBar.updateTabs(tabs); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 330 | public void removeTab(Tab tab) { |
| 331 | if (mTabControl.getCurrentTab() == tab) { |
| 332 | removeTabFromContentView(tab); |
| 333 | } |
| 334 | if (mXLargeScreenSize) { |
| 335 | mTabBar.onRemoveTab(tab); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | @Override |
| 340 | public void detachTab(Tab tab) { |
| 341 | removeTabFromContentView(tab); |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public void attachTab(Tab tab) { |
| 346 | attachTabToContentView(tab); |
| 347 | } |
| 348 | |
| 349 | private void attachTabToContentView(Tab tab) { |
| 350 | if (tab.getWebView() == null) { |
| 351 | return; |
| 352 | } |
| 353 | View container = tab.getViewContainer(); |
| 354 | WebView mainView = tab.getWebView(); |
| 355 | // Attach the WebView to the container and then attach the |
| 356 | // container to the content view. |
| 357 | FrameLayout wrapper = |
| 358 | (FrameLayout) container.findViewById(R.id.webview_wrapper); |
| 359 | ViewGroup parent = (ViewGroup) mainView.getParent(); |
| 360 | if (parent != wrapper) { |
| 361 | if (parent != null) { |
| 362 | Log.w(LOGTAG, "mMainView already has a parent in" |
| 363 | + " attachTabToContentView!"); |
| 364 | parent.removeView(mainView); |
| 365 | } |
| 366 | wrapper.addView(mainView); |
| 367 | } else { |
| 368 | Log.w(LOGTAG, "mMainView is already attached to wrapper in" |
| 369 | + " attachTabToContentView!"); |
| 370 | } |
| 371 | parent = (ViewGroup) container.getParent(); |
| 372 | if (parent != mContentView) { |
| 373 | if (parent != null) { |
| 374 | Log.w(LOGTAG, "mContainer already has a parent in" |
| 375 | + " attachTabToContentView!"); |
| 376 | parent.removeView(container); |
| 377 | } |
| 378 | mContentView.addView(container, COVER_SCREEN_PARAMS); |
| 379 | } else { |
| 380 | Log.w(LOGTAG, "mContainer is already attached to content in" |
| 381 | + " attachTabToContentView!"); |
| 382 | } |
| 383 | mUiController.attachSubWindow(tab); |
| 384 | } |
| 385 | |
| 386 | private void removeTabFromContentView(Tab tab) { |
| 387 | // Remove the container that contains the main WebView. |
| 388 | WebView mainView = tab.getWebView(); |
| 389 | View container = tab.getViewContainer(); |
| 390 | if (mainView == null) { |
| 391 | return; |
| 392 | } |
| 393 | // Remove the container from the content and then remove the |
| 394 | // WebView from the container. This will trigger a focus change |
| 395 | // needed by WebView. |
| 396 | FrameLayout wrapper = |
| 397 | (FrameLayout) container.findViewById(R.id.webview_wrapper); |
| 398 | wrapper.removeView(mainView); |
| 399 | mContentView.removeView(container); |
| 400 | mUiController.endActionMode(); |
| 401 | mUiController.removeSubWindow(tab); |
| 402 | ErrorConsoleView errorConsole = tab.getErrorConsole(false); |
| 403 | if (errorConsole != null) { |
| 404 | mErrorConsoleContainer.removeView(errorConsole); |
| 405 | } |
| 406 | mainView.setEmbeddedTitleBar(null); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Remove the sub window from the content view. |
| 411 | */ |
| 412 | @Override |
| 413 | public void removeSubWindow(View subviewContainer) { |
| 414 | mContentView.removeView(subviewContainer); |
| 415 | mUiController.endActionMode(); |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Attach the sub window to the content view. |
| 420 | */ |
| 421 | @Override |
| 422 | public void attachSubWindow(View container) { |
| 423 | mContentView.addView(container, COVER_SCREEN_PARAMS); |
| 424 | } |
| 425 | |
| 426 | void showFakeTitleBar() { |
| 427 | if (!isFakeTitleBarShowing() && mActiveTabsPage == null && |
| 428 | !mActivityPaused) { |
| 429 | WebView mainView = mUiController.getCurrentWebView(); |
| 430 | // if there is no current WebView, don't show the faked title bar; |
| 431 | if (mainView == null) { |
| 432 | return; |
| 433 | } |
| 434 | // Do not need to check for null, since the current tab will have |
| 435 | // at least a main WebView, or we would have returned above. |
| 436 | if (mUiController.isInCustomActionMode()) { |
| 437 | // Do not show the fake title bar, while a custom ActionMode |
| 438 | // (i.e. find or select) is showing. |
| 439 | return; |
| 440 | } |
| 441 | if (mXLargeScreenSize) { |
| 442 | mContentView.addView(mFakeTitleBar); |
| 443 | mTabBar.onShowTitleBar(); |
| 444 | } else { |
| 445 | WindowManager manager = (WindowManager) |
| 446 | mActivity.getSystemService(Context.WINDOW_SERVICE); |
| 447 | |
| 448 | // Add the title bar to the window manager so it can receive |
| 449 | // touches |
| 450 | // while the menu is up |
| 451 | WindowManager.LayoutParams params = |
| 452 | new WindowManager.LayoutParams( |
| 453 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 454 | ViewGroup.LayoutParams.WRAP_CONTENT, |
| 455 | WindowManager.LayoutParams.TYPE_APPLICATION, |
| 456 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, |
| 457 | PixelFormat.TRANSLUCENT); |
| 458 | params.gravity = Gravity.TOP; |
| 459 | boolean atTop = mainView.getScrollY() == 0; |
| 460 | params.windowAnimations = atTop ? 0 : R.style.TitleBar; |
| 461 | manager.addView(mFakeTitleBar, params); |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | void hideFakeTitleBar() { |
| 467 | if (!isFakeTitleBarShowing()) return; |
| 468 | if (mXLargeScreenSize) { |
| 469 | mContentView.removeView(mFakeTitleBar); |
| 470 | mTabBar.onHideTitleBar(); |
| 471 | } else { |
| 472 | WindowManager.LayoutParams params = |
| 473 | (WindowManager.LayoutParams) mFakeTitleBar.getLayoutParams(); |
| 474 | WebView mainView = mUiController.getCurrentWebView(); |
| 475 | // Although we decided whether or not to animate based on the |
| 476 | // current |
| 477 | // scroll position, the scroll position may have changed since the |
| 478 | // fake title bar was displayed. Make sure it has the appropriate |
| 479 | // animation/lack thereof before removing. |
| 480 | params.windowAnimations = |
| 481 | mainView != null && mainView.getScrollY() == 0 ? |
| 482 | 0 : R.style.TitleBar; |
| 483 | WindowManager manager = (WindowManager) mActivity |
| 484 | .getSystemService(Context.WINDOW_SERVICE); |
| 485 | manager.updateViewLayout(mFakeTitleBar, params); |
| 486 | manager.removeView(mFakeTitleBar); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | boolean isFakeTitleBarShowing() { |
| 491 | return (mFakeTitleBar.getParent() != null); |
| 492 | } |
| 493 | |
| 494 | @Override |
| 495 | public void showComboView(boolean startWithHistory, Bundle extras) { |
| 496 | mComboView = new CombinedBookmarkHistoryView(mActivity, |
| 497 | mUiController, |
| 498 | startWithHistory ? |
| 499 | CombinedBookmarkHistoryView.FRAGMENT_ID_HISTORY |
| 500 | : CombinedBookmarkHistoryView.FRAGMENT_ID_BOOKMARKS, |
| 501 | extras); |
| 502 | mTitleBar.setVisibility(View.GONE); |
| 503 | hideFakeTitleBar(); |
| 504 | mContentView.addView(mComboView, COVER_SCREEN_PARAMS); |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * dismiss the ComboPage |
| 509 | */ |
| 510 | @Override |
| 511 | public void hideComboView() { |
| 512 | if (mComboView != null) { |
| 513 | mContentView.removeView(mComboView); |
| 514 | mTitleBar.setVisibility(View.VISIBLE); |
| 515 | mComboView = null; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | @Override |
| 520 | public void showCustomView(View view, |
| 521 | WebChromeClient.CustomViewCallback callback) { |
| 522 | // if a view already exists then immediately terminate the new one |
| 523 | if (mCustomView != null) { |
| 524 | callback.onCustomViewHidden(); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | // Add the custom view to its container. |
| 529 | mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER); |
| 530 | mCustomView = view; |
| 531 | mCustomViewCallback = callback; |
| 532 | // Hide the content view. |
| 533 | mContentView.setVisibility(View.GONE); |
| 534 | // Finally show the custom view container. |
| 535 | setStatusBarVisibility(false); |
| 536 | mCustomViewContainer.setVisibility(View.VISIBLE); |
| 537 | mCustomViewContainer.bringToFront(); |
| 538 | } |
| 539 | |
| 540 | @Override |
| 541 | public void onHideCustomView() { |
| 542 | if (mCustomView == null) |
| 543 | return; |
| 544 | |
| 545 | // Hide the custom view. |
| 546 | mCustomView.setVisibility(View.GONE); |
| 547 | // Remove the custom view from its container. |
| 548 | mCustomViewContainer.removeView(mCustomView); |
| 549 | mCustomView = null; |
| 550 | mCustomViewContainer.setVisibility(View.GONE); |
| 551 | mCustomViewCallback.onCustomViewHidden(); |
| 552 | // Show the content view. |
| 553 | setStatusBarVisibility(true); |
| 554 | mContentView.setVisibility(View.VISIBLE); |
| 555 | } |
| 556 | |
| 557 | @Override |
| 558 | public boolean isCustomViewShowing() { |
| 559 | return mCustomView != null; |
| 560 | } |
| 561 | |
| 562 | @Override |
| 563 | public void showVoiceTitleBar(String title) { |
| 564 | mTitleBar.setInVoiceMode(true); |
| 565 | mTitleBar.setDisplayTitle(title); |
| 566 | mFakeTitleBar.setInVoiceMode(true); |
| 567 | mFakeTitleBar.setDisplayTitle(title); |
| 568 | } |
| 569 | |
| 570 | @Override |
| 571 | public void revertVoiceTitleBar(Tab tab) { |
| 572 | mTitleBar.setInVoiceMode(false); |
| 573 | String url = tab.getCurrentUrl(); |
| 574 | mTitleBar.setDisplayTitle(url); |
| 575 | mFakeTitleBar.setInVoiceMode(false); |
| 576 | mFakeTitleBar.setDisplayTitle(url); |
| 577 | } |
| 578 | |
| 579 | // ------------------------------------------------------------------------- |
| 580 | |
| 581 | @Override |
| 582 | public void resetTitleAndRevertLockIcon(Tab tab) { |
| 583 | tab.revertLockIcon(); |
| 584 | updateLockIconToLatest(tab); |
| 585 | resetTitleIconAndProgress(tab); |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * Resets the lock icon. This method is called when we start a new load and |
| 590 | * know the url to be loaded. |
| 591 | */ |
| 592 | private void resetLockIcon(Tab tab, String url) { |
| 593 | // Save the lock-icon state (we revert to it if the load gets cancelled) |
| 594 | tab.resetLockIcon(url); |
| 595 | updateLockIconImage(Tab.LOCK_ICON_UNSECURE); |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * Update the lock icon to correspond to our latest state. |
| 600 | */ |
| 601 | private void updateLockIconToLatest(Tab t) { |
| 602 | if (t != null) { |
| 603 | updateLockIconImage(t.getLockIconType()); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Reset the title, favicon, and progress. |
| 609 | */ |
| 610 | private void resetTitleIconAndProgress(Tab tab) { |
| 611 | WebView current = tab.getWebView(); |
| 612 | if (current == null) { |
| 613 | return; |
| 614 | } |
| 615 | resetTitleAndIcon(current); |
| 616 | int progress = current.getProgress(); |
| 617 | current.getWebChromeClient().onProgressChanged(current, progress); |
| 618 | } |
| 619 | |
| 620 | @Override |
| 621 | public void resetTitleAndIcon(Tab tab) { |
| 622 | WebView current = tab.getWebView(); |
| 623 | if (current != null) { |
| 624 | resetTitleAndIcon(current); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | // Reset the title and the icon based on the given item. |
| 629 | private void resetTitleAndIcon(WebView view) { |
| 630 | WebHistoryItem item = view.copyBackForwardList().getCurrentItem(); |
| 631 | Tab tab = mTabControl.getTabFromView(view); |
| 632 | if (item != null) { |
| 633 | setUrlTitle(tab, item.getUrl(), item.getTitle()); |
| 634 | setFavicon(tab, item.getFavicon()); |
| 635 | } else { |
| 636 | setUrlTitle(tab, null, null); |
| 637 | setFavicon(tab, null); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * Updates the lock-icon image in the title-bar. |
| 643 | */ |
| 644 | private void updateLockIconImage(int lockIconType) { |
| 645 | Drawable d = null; |
| 646 | if (lockIconType == Tab.LOCK_ICON_SECURE) { |
| 647 | d = mSecLockIcon; |
| 648 | } else if (lockIconType == Tab.LOCK_ICON_MIXED) { |
| 649 | d = mMixLockIcon; |
| 650 | } |
| 651 | mTitleBar.setLock(d); |
| 652 | mFakeTitleBar.setLock(d); |
| 653 | } |
| 654 | |
| 655 | // active tabs page |
| 656 | |
| 657 | public void showActiveTabsPage() { |
| 658 | mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController); |
| 659 | mTitleBar.setVisibility(View.GONE); |
| 660 | hideFakeTitleBar(); |
| 661 | mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS); |
| 662 | mActiveTabsPage.requestFocus(); |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * Remove the active tabs page. |
| 667 | * @param needToAttach If true, the active tabs page did not attach a tab |
| 668 | * to the content view, so we need to do that here. |
| 669 | */ |
| 670 | public void removeActiveTabsPage() { |
| 671 | mContentView.removeView(mActiveTabsPage); |
| 672 | mTitleBar.setVisibility(View.VISIBLE); |
| 673 | mActiveTabsPage = null; |
| 674 | } |
| 675 | |
| 676 | // action mode callbacks |
| 677 | |
| 678 | @Override |
| 679 | public void onActionModeStarted(ActionMode mode) { |
| 680 | // hide the fake title bar when CAB is shown |
| 681 | hideFakeTitleBar(); |
| 682 | } |
| 683 | |
| 684 | @Override |
| 685 | public void onActionModeFinished(boolean inLoad) { |
| 686 | if (inLoad) { |
| 687 | // the titlebar was removed when the CAB was shown |
| 688 | // if the page is loading, show it again |
| 689 | showFakeTitleBar(); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | // menu handling callbacks |
| 694 | |
| 695 | @Override |
| 696 | public void onOptionsMenuOpened() { |
| 697 | mOptionsMenuOpen = true; |
| 698 | // options menu opened, show fake title bar |
| 699 | showFakeTitleBar(); |
| 700 | } |
| 701 | |
| 702 | @Override |
| 703 | public void onExtendedMenuOpened() { |
| 704 | // Switching the menu to expanded view, so hide the |
| 705 | // title bar. |
| 706 | mExtendedMenuOpen = true; |
| 707 | hideFakeTitleBar(); |
| 708 | } |
| 709 | |
| 710 | @Override |
| 711 | public void onOptionsMenuClosed(boolean inLoad) { |
| 712 | mOptionsMenuOpen = false; |
| 713 | if (!inLoad) { |
| 714 | hideFakeTitleBar(); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | @Override |
| 719 | public void onExtendedMenuClosed(boolean inLoad) { |
| 720 | mExtendedMenuOpen = false; |
| 721 | if (inLoad) { |
| 722 | showFakeTitleBar(); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | @Override |
| 727 | public void onContextMenuCreated(Menu menu) { |
| 728 | hideFakeTitleBar(); |
| 729 | } |
| 730 | |
| 731 | @Override |
| 732 | public void onContextMenuClosed(Menu menu, boolean inLoad) { |
| 733 | if (inLoad) { |
| 734 | showFakeTitleBar(); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | @Override |
| 739 | public void onScroll(boolean titleVisible) { |
| 740 | if (mTabBar != null) { |
| 741 | mTabBar.onScroll(titleVisible); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | // error console |
| 746 | |
| 747 | @Override |
| 748 | public void setShouldShowErrorConsole(Tab tab, boolean flag) { |
| 749 | ErrorConsoleView errorConsole = tab.getErrorConsole(true); |
| 750 | if (flag) { |
| 751 | // Setting the show state of the console will cause it's the layout |
| 752 | // to be inflated. |
| 753 | if (errorConsole.numberOfErrors() > 0) { |
| 754 | errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED); |
| 755 | } else { |
| 756 | errorConsole.showConsole(ErrorConsoleView.SHOW_NONE); |
| 757 | } |
| 758 | if (errorConsole.getParent() != null) { |
| 759 | mErrorConsoleContainer.removeView(errorConsole); |
| 760 | } |
| 761 | // Now we can add it to the main view. |
| 762 | mErrorConsoleContainer.addView(errorConsole, |
| 763 | new LinearLayout.LayoutParams( |
| 764 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 765 | ViewGroup.LayoutParams.WRAP_CONTENT)); |
| 766 | } else { |
| 767 | mErrorConsoleContainer.removeView(errorConsole); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | private void setStatusBarVisibility(boolean visible) { |
| 772 | int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN; |
| 773 | mActivity.getWindow().setFlags(flag, |
| 774 | WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 775 | } |
| 776 | |
| 777 | @Override |
| 778 | public void setUrlTitle(Tab tab, String url, String title) { |
| 779 | if (TextUtils.isEmpty(title)) { |
| 780 | if (TextUtils.isEmpty(url)) { |
| 781 | title = mActivity.getResources() |
| 782 | .getString(R.string.title_bar_loading); |
| 783 | } else { |
| 784 | title = url; |
| 785 | } |
| 786 | } |
| 787 | if (tab.isInVoiceSearchMode()) return; |
| 788 | if (tab.inForeground()) { |
| 789 | mTitleBar.setDisplayTitle(url); |
| 790 | mFakeTitleBar.setDisplayTitle(url); |
| 791 | } |
| 792 | if (mXLargeScreenSize) { |
| 793 | mTabBar.onUrlAndTitle(tab, url, title); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | // Set the favicon in the title bar. |
| 798 | @Override |
| 799 | public void setFavicon(Tab tab, Bitmap icon) { |
| 800 | mTitleBar.setFavicon(icon); |
| 801 | mFakeTitleBar.setFavicon(icon); |
| 802 | if (mXLargeScreenSize) { |
| 803 | mTabBar.onFavicon(tab, icon); |
| 804 | } |
| 805 | } |
| 806 | @Override |
| 807 | public boolean showsWeb() { |
| 808 | return mCustomView == null && mActiveTabsPage == null |
| 809 | && mComboView == null; |
| 810 | } |
| 811 | |
| 812 | @Override |
| 813 | public void onPrepareOptionsMenu(Menu menu) { |
| 814 | if (!mXLargeScreenSize) { |
| 815 | final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id); |
| 816 | newtab.setEnabled(mUiController.getTabControl().canCreateNewTab()); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | // ------------------------------------------------------------------------- |
| 821 | // Helper function for WebChromeClient |
| 822 | // ------------------------------------------------------------------------- |
| 823 | |
| 824 | @Override |
| 825 | public Bitmap getDefaultVideoPoster() { |
| 826 | if (mDefaultVideoPoster == null) { |
| 827 | mDefaultVideoPoster = BitmapFactory.decodeResource( |
| 828 | mActivity.getResources(), R.drawable.default_video_poster); |
| 829 | } |
| 830 | return mDefaultVideoPoster; |
| 831 | } |
| 832 | |
| 833 | @Override |
| 834 | public View getVideoLoadingProgressView() { |
| 835 | if (mVideoProgressView == null) { |
| 836 | LayoutInflater inflater = LayoutInflater.from(mActivity); |
| 837 | mVideoProgressView = inflater.inflate( |
| 838 | R.layout.video_loading_progress, null); |
| 839 | } |
| 840 | return mVideoProgressView; |
| 841 | } |
| 842 | |
| 843 | } |