OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.widget; | 5 package org.chromium.chrome.browser.widget; |
6 | 6 |
7 import android.animation.Animator; | 7 import android.animation.Animator; |
8 import android.animation.AnimatorListenerAdapter; | 8 import android.animation.AnimatorListenerAdapter; |
9 import android.animation.ValueAnimator; | 9 import android.animation.ValueAnimator; |
10 import android.content.Context; | 10 import android.content.Context; |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
329 * @return Whether or not the toolbar Android View is hidden due to being sc rolled off-screen. | 329 * @return Whether or not the toolbar Android View is hidden due to being sc rolled off-screen. |
330 */ | 330 */ |
331 private boolean isToolbarAndroidViewHidden() { | 331 private boolean isToolbarAndroidViewHidden() { |
332 return mFullscreenManager == null || mFullscreenManager.getBottomControl Offset() > 0; | 332 return mFullscreenManager == null || mFullscreenManager.getBottomControl Offset() > 0; |
333 } | 333 } |
334 | 334 |
335 /** | 335 /** |
336 * Adds layout change listeners to the views that the bottom sheet depends o n. Namely the | 336 * Adds layout change listeners to the views that the bottom sheet depends o n. Namely the |
337 * heights of the root view and control container are important as they are used in many of the | 337 * heights of the root view and control container are important as they are used in many of the |
338 * calculations in this class. | 338 * calculations in this class. |
339 * @param activity An activity for loading native pages. | |
340 * @param root The container of the bottom sheet. | 339 * @param root The container of the bottom sheet. |
341 * @param controlContainer The container for the toolbar. | 340 * @param controlContainer The container for the toolbar. |
342 */ | 341 */ |
343 public void init(View root, View controlContainer) { | 342 public void init(View root, View controlContainer) { |
344 mControlContainer = controlContainer; | 343 mControlContainer = controlContainer; |
345 mToolbarHeight = mControlContainer.getHeight(); | 344 mToolbarHeight = mControlContainer.getHeight(); |
346 | 345 |
347 mBottomSheetContentContainer = (FrameLayout) findViewById(R.id.bottom_sh eet_content); | 346 mBottomSheetContentContainer = (FrameLayout) findViewById(R.id.bottom_sh eet_content); |
348 | 347 |
349 mCurrentState = SHEET_STATE_PEEK; | 348 mCurrentState = SHEET_STATE_PEEK; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
386 mPlaceholder = new View(getContext()); | 385 mPlaceholder = new View(getContext()); |
387 LayoutParams placeHolderParams = | 386 LayoutParams placeHolderParams = |
388 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_P ARENT); | 387 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_P ARENT); |
389 mPlaceholder.setBackgroundColor( | 388 mPlaceholder.setBackgroundColor( |
390 ApiCompatibilityUtils.getColor(getResources(), android.R.color.w hite)); | 389 ApiCompatibilityUtils.getColor(getResources(), android.R.color.w hite)); |
391 mBottomSheetContentContainer.addView(mPlaceholder, placeHolderParams); | 390 mBottomSheetContentContainer.addView(mPlaceholder, placeHolderParams); |
392 } | 391 } |
393 | 392 |
394 @Override | 393 @Override |
395 public int loadUrl(LoadUrlParams params) { | 394 public int loadUrl(LoadUrlParams params) { |
395 for (BottomSheetObserver o : mObservers) o.onLoadUrl(params.getUrl()); | |
396 | |
396 // Native page URLs in this context do not need to communicate with the tab. | 397 // Native page URLs in this context do not need to communicate with the tab. |
397 if (NativePageFactory.isNativePageUrl(params.getUrl(), isIncognito())) { | 398 if (NativePageFactory.isNativePageUrl(params.getUrl(), isIncognito())) { |
398 return TabLoadStatus.PAGE_LOAD_FAILED; | 399 return TabLoadStatus.PAGE_LOAD_FAILED; |
399 } | 400 } |
400 | 401 |
401 // In all non-native cases, minimize the sheet. | 402 // In all non-native cases, minimize the sheet. |
402 setSheetState(SHEET_STATE_PEEK, true); | 403 setSheetState(SHEET_STATE_PEEK, true); |
403 | 404 |
404 assert mTabModelSelector != null; | 405 assert mTabModelSelector != null; |
405 | 406 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
440 if (mControlContainer == null) return false; | 441 if (mControlContainer == null) return false; |
441 | 442 |
442 mControlContainer.getLocationInWindow(mLocationArray); | 443 mControlContainer.getLocationInWindow(mLocationArray); |
443 | 444 |
444 return e.getRawY() < mLocationArray[1] + mToolbarHeight; | 445 return e.getRawY() < mLocationArray[1] + mToolbarHeight; |
445 } | 446 } |
446 | 447 |
447 /** | 448 /** |
448 * A notification that the sheet is exiting the peek state into one that sho ws content. | 449 * A notification that the sheet is exiting the peek state into one that sho ws content. |
449 */ | 450 */ |
450 private void onExitPeekState() { | 451 private void onSheetOpened() { |
451 if (mSuggestionsContent == null) { | 452 if (mSuggestionsContent == null) { |
452 mSuggestionsContent = new SuggestionsBottomSheetContent( | 453 mSuggestionsContent = new SuggestionsBottomSheetContent( |
453 mTabModelSelector.getCurrentTab().getActivity(), this, mTabM odelSelector); | 454 mTabModelSelector.getCurrentTab().getActivity(), this, mTabM odelSelector); |
454 } | 455 } |
455 | 456 |
456 showContent(mSuggestionsContent); | 457 showContent(mSuggestionsContent); |
458 | |
459 for (BottomSheetObserver o : mObservers) o.onSheetOpened(); | |
457 } | 460 } |
458 | 461 |
459 /** | 462 /** |
463 * A notification that the sheet has returned to the peeking state. | |
464 */ | |
465 private void onSheetClosed() { | |
466 for (BottomSheetObserver o : mObservers) o.onSheetClosed(); | |
467 } | |
468 | |
469 /** | |
460 * Show content in the bottom sheet's content area. | 470 * Show content in the bottom sheet's content area. |
461 * @param content The {@link BottomSheetContent} to show. | 471 * @param content The {@link BottomSheetContent} to show. |
462 */ | 472 */ |
463 private void showContent(BottomSheetContent content) { | 473 private void showContent(BottomSheetContent content) { |
464 // If the desired content is already showing, do nothing. | 474 // If the desired content is already showing, do nothing. |
465 if (mSheetContent == content) return; | 475 if (mSheetContent == content) return; |
466 | 476 |
467 if (mSheetContent != null) { | 477 if (mSheetContent != null) { |
468 mBottomSheetContentContainer.removeView(mSheetContent.getScrollingCo ntentView()); | 478 mBottomSheetContentContainer.removeView(mSheetContent.getScrollingCo ntentView()); |
469 mSheetContent = null; | 479 mSheetContent = null; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
591 return mContainerHeight - getTranslationY(); | 601 return mContainerHeight - getTranslationY(); |
592 } | 602 } |
593 | 603 |
594 /** | 604 /** |
595 * Sets the sheet's offset relative to the bottom of the screen. | 605 * Sets the sheet's offset relative to the bottom of the screen. |
596 * @param offset The offset that the sheet should be. | 606 * @param offset The offset that the sheet should be. |
597 */ | 607 */ |
598 private void setSheetOffsetFromBottom(float offset) { | 608 private void setSheetOffsetFromBottom(float offset) { |
599 if (MathUtils.areFloatsEqual(getSheetOffsetFromBottom(), getMinOffset()) | 609 if (MathUtils.areFloatsEqual(getSheetOffsetFromBottom(), getMinOffset()) |
600 && offset > getMinOffset()) { | 610 && offset > getMinOffset()) { |
601 onExitPeekState(); | 611 onSheetOpened(); |
612 } | |
613 | |
614 if (MathUtils.areFloatsEqual(offset, getMinOffset()) | |
dgn
2017/02/20 11:37:14
nit: else if to make it more easily spottable that
mdjones
2017/02/21 17:19:10
Done. One option to make this more clear is settin
| |
615 && getSheetOffsetFromBottom() > getMinOffset()) { | |
616 onSheetClosed(); | |
602 } | 617 } |
603 | 618 |
604 setTranslationY(mContainerHeight - offset); | 619 setTranslationY(mContainerHeight - offset); |
605 sendUpdatePeekToHalfEvent(); | 620 sendUpdatePeekToHalfEvent(); |
606 } | 621 } |
607 | 622 |
608 /** | 623 /** |
624 * This is the same as {@link #setSheetOffsetFromBottom(float)} but exclusiv ely for testing. | |
625 * @param offset The offset to set the sheet to. | |
626 */ | |
627 @VisibleForTesting | |
628 public void setSheetOffsetFromBottomForTesting(float offset) { | |
629 setSheetOffsetFromBottom(offset); | |
630 } | |
631 | |
632 /** | |
609 * @return The ratio of the height of the screen that the peeking state is. | 633 * @return The ratio of the height of the screen that the peeking state is. |
610 */ | 634 */ |
611 private float getPeekRatio() { | 635 @VisibleForTesting |
636 public float getPeekRatio() { | |
612 return mStateRatios[0]; | 637 return mStateRatios[0]; |
613 } | 638 } |
614 | 639 |
615 /** | 640 /** |
616 * @return The ratio of the height of the screen that the half expanded stat e is. | 641 * @return The ratio of the height of the screen that the half expanded stat e is. |
617 */ | 642 */ |
618 private float getHalfRatio() { | 643 @VisibleForTesting |
644 public float getHalfRatio() { | |
619 return mStateRatios[1]; | 645 return mStateRatios[1]; |
620 } | 646 } |
621 | 647 |
622 /** | 648 /** |
623 * @return The ratio of the height of the screen that the fully expanded sta te is. | 649 * @return The ratio of the height of the screen that the fully expanded sta te is. |
624 */ | 650 */ |
625 private float getFullRatio() { | 651 @VisibleForTesting |
652 public float getFullRatio() { | |
626 return mStateRatios[2]; | 653 return mStateRatios[2]; |
627 } | 654 } |
628 | 655 |
629 /** | 656 /** |
657 * @return The height of the container that the bottom sheet exists in. | |
658 */ | |
659 @VisibleForTesting | |
660 public float getSheetContainerHeight() { | |
661 return mContainerHeight; | |
662 } | |
663 | |
664 /** | |
630 * Sends a notification if the sheet is transitioning from the peeking to ha lf expanded state. | 665 * Sends a notification if the sheet is transitioning from the peeking to ha lf expanded state. |
631 * This method only sends events when the sheet is between the peeking and h alf states. | 666 * This method only sends events when the sheet is between the peeking and h alf states. |
632 */ | 667 */ |
633 private void sendUpdatePeekToHalfEvent() { | 668 private void sendUpdatePeekToHalfEvent() { |
634 float screenRatio = | 669 float screenRatio = |
635 mContainerHeight > 0 ? getSheetOffsetFromBottom() / mContainerHe ight : 0; | 670 mContainerHeight > 0 ? getSheetOffsetFromBottom() / mContainerHe ight : 0; |
636 | 671 |
637 // This ratio is relative to the peek and half positions of the sheet ra ther than the height | 672 // This ratio is relative to the peek and half positions of the sheet ra ther than the height |
638 // of the screen. | 673 // of the screen. |
639 float peekHalfRatio = MathUtils.clamp( | 674 float peekHalfRatio = MathUtils.clamp( |
640 (screenRatio - getPeekRatio()) / (getHalfRatio() - getPeekRatio( )), 0, 1); | 675 (screenRatio - getPeekRatio()) / (getHalfRatio() - getPeekRatio( )), 0, 1); |
641 | 676 |
642 // If the ratio is close enough to zero, just set it to zero. | 677 // If the ratio is close enough to zero, just set it to zero. |
643 if (MathUtils.areFloatsEqual(peekHalfRatio, 0f)) peekHalfRatio = 0f; | 678 if (MathUtils.areFloatsEqual(peekHalfRatio, 0f)) peekHalfRatio = 0f; |
644 | 679 |
645 for (BottomSheetObserver o : mObservers) { | 680 if (mLastPeekToHalfRatioSent < 1f || peekHalfRatio < 1f) { |
646 if (mLastPeekToHalfRatioSent < 1f || peekHalfRatio < 1f) { | 681 mLastPeekToHalfRatioSent = peekHalfRatio; |
647 mLastPeekToHalfRatioSent = peekHalfRatio; | 682 for (BottomSheetObserver o : mObservers) { |
648 o.onTransitionPeekToHalf(peekHalfRatio); | 683 o.onTransitionPeekToHalf(peekHalfRatio); |
649 } | 684 } |
650 } | 685 } |
651 } | 686 } |
652 | 687 |
653 /** | 688 /** |
654 * Moves the sheet to the provided state. | 689 * Moves the sheet to the provided state. |
655 * @param state The state to move the panel to. | 690 * @param state The state to move the panel to. |
656 * @param animate If true, the sheet will animate to the provided state, oth erwise it will | 691 * @param animate If true, the sheet will animate to the provided state, oth erwise it will |
657 * move there instantly. | 692 * move there instantly. |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
748 } | 783 } |
749 | 784 |
750 @Override | 785 @Override |
751 public void onFadingViewClick() { | 786 public void onFadingViewClick() { |
752 setSheetState(SHEET_STATE_PEEK, true); | 787 setSheetState(SHEET_STATE_PEEK, true); |
753 } | 788 } |
754 | 789 |
755 @Override | 790 @Override |
756 public void onFadingViewHidden() {} | 791 public void onFadingViewHidden() {} |
757 } | 792 } |
OLD | NEW |