OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.widget.findinpage; |
| 6 |
| 7 import android.animation.Animator; |
| 8 import android.animation.AnimatorListenerAdapter; |
| 9 import android.animation.ObjectAnimator; |
| 10 import android.content.Context; |
| 11 import android.content.res.Resources; |
| 12 import android.graphics.Rect; |
| 13 import android.util.AttributeSet; |
| 14 import android.view.View; |
| 15 import android.view.animation.DecelerateInterpolator; |
| 16 import android.widget.FrameLayout; |
| 17 |
| 18 import com.google.android.apps.chrome.R; |
| 19 |
| 20 /** |
| 21 * A tablet specific version of the {@link FindToolbar}. |
| 22 */ |
| 23 public class FindToolbarTablet extends FindToolbar { |
| 24 private static final int ENTER_EXIT_ANIMATION_DURATION_MS = 200; |
| 25 private static final int MAKE_ROOM_ANIMATION_DURATION_MS = 200; |
| 26 |
| 27 private static final float Y_INSET_DP = 8.f; |
| 28 |
| 29 private ObjectAnimator mCurrentAnimation; |
| 30 |
| 31 private ObjectAnimator mAnimationEnter; |
| 32 private ObjectAnimator mAnimationLeave; |
| 33 |
| 34 private final int mYInsetPx; |
| 35 |
| 36 /** |
| 37 * Creates an instance of a {@link FindToolbarTablet}. |
| 38 * @param context The Context to create the {@link FindToolbarTablet} under. |
| 39 * @param attrs The AttributeSet used to create the {@link FindToolbarTablet
}. |
| 40 */ |
| 41 public FindToolbarTablet(Context context, AttributeSet attrs) { |
| 42 super(context, attrs); |
| 43 |
| 44 mYInsetPx = (int) (context.getResources().getDisplayMetrics().density *
Y_INSET_DP); |
| 45 } |
| 46 |
| 47 @Override |
| 48 public void onFinishInflate() { |
| 49 super.onFinishInflate(); |
| 50 |
| 51 setVisibility(View.GONE); |
| 52 |
| 53 Resources resources = getContext().getResources(); |
| 54 int width = resources.getDimensionPixelSize(R.dimen.find_in_page_popup_w
idth); |
| 55 int endMargin = resources.getDimensionPixelOffset(R.dimen.find_in_page_p
opup_margin_end); |
| 56 int translateWidth = width + endMargin; |
| 57 |
| 58 mAnimationEnter = ObjectAnimator.ofFloat(this, "translationX", translate
Width, 0); |
| 59 mAnimationEnter.setDuration(ENTER_EXIT_ANIMATION_DURATION_MS); |
| 60 mAnimationEnter.setInterpolator(new DecelerateInterpolator()); |
| 61 mAnimationEnter.addListener(new AnimatorListenerAdapter() { |
| 62 @Override |
| 63 public void onAnimationStart(Animator animation) { |
| 64 setVisibility(View.VISIBLE); |
| 65 postInvalidateOnAnimation(); |
| 66 superActivate(); |
| 67 } |
| 68 |
| 69 @Override |
| 70 public void onAnimationEnd(Animator animation) { |
| 71 mCurrentAnimation = null; |
| 72 } |
| 73 }); |
| 74 |
| 75 mAnimationLeave = ObjectAnimator.ofFloat(this, "translationX", 0, transl
ateWidth); |
| 76 mAnimationLeave.setDuration(ENTER_EXIT_ANIMATION_DURATION_MS); |
| 77 mAnimationLeave.setInterpolator(new DecelerateInterpolator()); |
| 78 mAnimationLeave.addListener(new AnimatorListenerAdapter() { |
| 79 @Override |
| 80 public void onAnimationStart(Animator animation) { |
| 81 setVisibility(View.VISIBLE); |
| 82 postInvalidateOnAnimation(); |
| 83 } |
| 84 |
| 85 @Override |
| 86 public void onAnimationEnd(Animator animation) { |
| 87 setVisibility(View.GONE); |
| 88 mCurrentAnimation = null; |
| 89 } |
| 90 }); |
| 91 } |
| 92 |
| 93 @Override |
| 94 public void activate() { |
| 95 if (mCurrentAnimation == mAnimationEnter) return; |
| 96 |
| 97 if (isViewAvailable()) setShowState(true); |
| 98 } |
| 99 |
| 100 @Override |
| 101 public void deactivate() { |
| 102 super.deactivate(); |
| 103 |
| 104 if (mCurrentAnimation == mAnimationLeave) return; |
| 105 |
| 106 setShowState(false); |
| 107 } |
| 108 |
| 109 @Override |
| 110 public boolean isAnimating() { |
| 111 return mCurrentAnimation != null; |
| 112 } |
| 113 |
| 114 @Override |
| 115 public void findResultSelected(Rect rect) { |
| 116 super.findResultSelected(rect); |
| 117 |
| 118 boolean makeRoom = false; |
| 119 float density = getContext().getResources().getDisplayMetrics().density; |
| 120 |
| 121 if (rect != null && rect.intersects((int) (getLeft() / density), 0, |
| 122 (int) (getRight() / density), (int) (getHeight() / density))) { |
| 123 makeRoom = true; |
| 124 } |
| 125 |
| 126 setMakeRoomForResults(makeRoom); |
| 127 } |
| 128 |
| 129 @Override |
| 130 protected void clearResults() { |
| 131 super.clearResults(); |
| 132 setMakeRoomForResults(false); |
| 133 } |
| 134 |
| 135 private void setMakeRoomForResults(boolean makeRoom) { |
| 136 float translationY = makeRoom ? -(getHeight() - mYInsetPx) : 0.f; |
| 137 |
| 138 if (translationY != getTranslationY()) { |
| 139 mCurrentAnimation = ObjectAnimator.ofFloat(this, "translationY", get
TranslationY(), |
| 140 translationY); |
| 141 mCurrentAnimation.setDuration(MAKE_ROOM_ANIMATION_DURATION_MS); |
| 142 mAnimationLeave.setInterpolator(new DecelerateInterpolator()); |
| 143 mAnimationLeave.addListener(new AnimatorListenerAdapter() { |
| 144 @Override |
| 145 public void onAnimationStart(Animator animation) { |
| 146 postInvalidateOnAnimation(); |
| 147 } |
| 148 |
| 149 @Override |
| 150 public void onAnimationEnd(Animator animation) { |
| 151 mCurrentAnimation = null; |
| 152 } |
| 153 }); |
| 154 mTabModelSelector.getCurrentTab() |
| 155 .getWindowAndroid() |
| 156 .startAnimationOverContent(mCurrentAnimation); |
| 157 } |
| 158 } |
| 159 |
| 160 private void setShowState(boolean show) { |
| 161 ObjectAnimator nextAnimator = null; |
| 162 |
| 163 if (show && getVisibility() != View.VISIBLE && mCurrentAnimation != mAni
mationEnter) { |
| 164 View anchorView = getRootView().findViewById(R.id.toolbar); |
| 165 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutPa
rams(); |
| 166 lp.topMargin = anchorView.getBottom() - mYInsetPx; |
| 167 setLayoutParams(lp); |
| 168 nextAnimator = mAnimationEnter; |
| 169 } else if (!show && getVisibility() != View.GONE && mCurrentAnimation !=
mAnimationLeave) { |
| 170 nextAnimator = mAnimationLeave; |
| 171 onHideAnimationStart(); |
| 172 } |
| 173 |
| 174 if (nextAnimator != null) { |
| 175 mCurrentAnimation = nextAnimator; |
| 176 mTabModelSelector.getCurrentTab() |
| 177 .getWindowAndroid() |
| 178 .startAnimationOverContent(nextAnimator); |
| 179 postInvalidateOnAnimation(); |
| 180 } |
| 181 } |
| 182 |
| 183 /** |
| 184 * This is here so that Animation inner classes can access the parent activa
te methods. |
| 185 */ |
| 186 private void superActivate() { |
| 187 super.activate(); |
| 188 } |
| 189 } |
OLD | NEW |