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