This source file includes following definitions.
- start
- getInfoBar
- getTarget
- getAnimationType
- onGlobalLayout
- continueAnimation
package org.chromium.chrome.browser.infobar;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateDecelerateInterpolator;
import org.chromium.base.ApiCompatibilityUtils;
import java.util.ArrayList;
public class AnimationHelper implements ViewTreeObserver.OnGlobalLayoutListener {
private static final long ANIMATION_DURATION_MS = 250;
public static final int ANIMATION_TYPE_SHOW = 0;
public static final int ANIMATION_TYPE_SWAP = 1;
public static final int ANIMATION_TYPE_HIDE = 2;
public static final int ANIMATION_TYPE_BOUNDARY = 3;
private final InfoBarContainer mContainer;
private final InfoBar mInfoBar;
private final ContentWrapperView mTargetWrapperView;
private final AnimatorSet mAnimatorSet;
private final int mAnimationType;
private final View mToShow;
private boolean mAnimationStarted;
public AnimationHelper(InfoBarContainer container, ContentWrapperView target, InfoBar infoBar,
View toShow, int animationType) {
mContainer = container;
mInfoBar = infoBar;
mTargetWrapperView = target;
mAnimatorSet = new AnimatorSet();
mAnimationType = animationType;
mToShow = toShow;
assert mContainer.indexOfChild(mTargetWrapperView) != -1;
}
public void start() {
mTargetWrapperView.prepareTransition(mToShow);
mContainer.prepareTransition(mToShow);
if (mToShow == null) {
continueAnimation();
} else {
mTargetWrapperView.getViewTreeObserver().addOnGlobalLayoutListener(this);
}
}
public InfoBar getInfoBar() {
return mInfoBar;
}
public ContentWrapperView getTarget() {
return mTargetWrapperView;
}
public int getAnimationType() {
return mAnimationType;
}
@Override
public void onGlobalLayout() {
ApiCompatibilityUtils.removeOnGlobalLayoutListener(mTargetWrapperView, this);
continueAnimation();
}
private void continueAnimation() {
if (mAnimationStarted) return;
mAnimationStarted = true;
boolean infoBarsOnTop = mContainer.areInfoBarsOnTop();
int indexOfWrapperView = mContainer.indexOfChild(mTargetWrapperView);
assert indexOfWrapperView != -1;
ArrayList<Animator> animators = new ArrayList<Animator>();
mTargetWrapperView.getAnimationsForTransition(animators);
int heightDifference = mTargetWrapperView.getTransitionHeightDifference();
int cumulativeTopStart = 0;
int cumulativeTopEnd = 0;
int cumulativeEndHeight = 0;
if (!infoBarsOnTop) {
if (heightDifference >= 0) {
cumulativeTopStart = heightDifference;
} else {
cumulativeTopEnd = -heightDifference;
}
}
for (int i = 0; i < mContainer.getChildCount(); ++i) {
View view = mContainer.getChildAt(i);
int startHeight = view.getHeight();
int endHeight = startHeight + (i == indexOfWrapperView ? heightDifference : 0);
int topStart = cumulativeTopStart;
int topEnd = cumulativeTopEnd;
int bottomStart = topStart + startHeight;
int bottomEnd = topEnd + endHeight;
if (topStart == topEnd && bottomStart == bottomEnd) {
view.setTop(topEnd);
view.setBottom(bottomEnd);
view.setY(topEnd);
view.setTranslationY(0);
} else {
int translation = heightDifference;
if (infoBarsOnTop) translation *= -1;
boolean translateDownward = false;
if (topStart < topEnd) {
translateDownward = infoBarsOnTop;
} else if (topStart > topEnd) {
translateDownward = !infoBarsOnTop;
} else {
translateDownward = bottomEnd > bottomStart;
}
PropertyValuesHolder viewTranslation;
if (translateDownward) {
view.setTop(topEnd);
view.setBottom(bottomEnd);
view.setTranslationY(translation);
view.setY(topEnd + translation);
viewTranslation =
PropertyValuesHolder.ofFloat("translationY", translation, 0.0f);
} else {
viewTranslation =
PropertyValuesHolder.ofFloat("translationY", 0.0f, -translation);
}
animators.add(ObjectAnimator.ofPropertyValuesHolder(view, viewTranslation));
}
cumulativeTopStart += startHeight;
cumulativeTopEnd += endHeight;
cumulativeEndHeight += endHeight;
}
final int oldContainerTop = mContainer.getTop();
final int oldContainerBottom = mContainer.getBottom();
final int newContainerTop;
final int newContainerBottom;
if (infoBarsOnTop) {
newContainerTop = oldContainerTop;
newContainerBottom = newContainerTop + cumulativeEndHeight;
} else {
newContainerBottom = oldContainerBottom;
newContainerTop = newContainerBottom - cumulativeEndHeight;
}
final int biggestContainerTop = Math.min(oldContainerTop, newContainerTop);
final int biggestContainerBottom = Math.max(oldContainerBottom, newContainerBottom);
mContainer.setTop(biggestContainerTop);
mContainer.setBottom(biggestContainerBottom);
mAnimatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
mTargetWrapperView.startTransition();
mContainer.startTransition();
}
@Override
public void onAnimationEnd(Animator animation) {
mTargetWrapperView.finishTransition();
mContainer.finishTransition();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && mToShow != null &&
(mAnimationType == ANIMATION_TYPE_SHOW ||
mAnimationType == ANIMATION_TYPE_SWAP)) {
mToShow.announceForAccessibility(
mInfoBar.getMessageText(mContainer.getContext()));
}
}
});
mAnimatorSet.playTogether(animators);
mAnimatorSet.setDuration(ANIMATION_DURATION_MS);
mAnimatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
mAnimatorSet.start();
}
}