StackStateAnimator.java revision cfc91cf2648cdaa2268e70ebfeed421ed86a6221
1572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek/*
2572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek * Copyright (C) 2014 The Android Open Source Project
3572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek *
4572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek * Licensed under the Apache License, Version 2.0 (the "License");
5572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek * you may not use this file except in compliance with the License.
6572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek * You may obtain a copy of the License at
7572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek *
8572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek *      http://www.apache.org/licenses/LICENSE-2.0
9572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek *
10572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek * Unless required by applicable law or agreed to in writing, software
11572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek * distributed under the License is distributed on an "AS IS" BASIS,
12572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek * See the License for the specific language governing permissions and
14572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek * limitations under the License
15572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek */
16572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek
17572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinekpackage com.android.systemui.statusbar.stack;
18572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek
19eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinekimport android.animation.Animator;
20eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinekimport android.animation.AnimatorListenerAdapter;
21eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinekimport android.animation.ObjectAnimator;
22d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggiimport android.animation.PropertyValuesHolder;
23572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinekimport android.animation.ValueAnimator;
24572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinekimport android.view.View;
25572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinekimport android.view.animation.AnimationUtils;
26572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinekimport android.view.animation.Interpolator;
27eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek
28eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinekimport com.android.systemui.R;
29572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinekimport com.android.systemui.statusbar.ExpandableView;
308b73006a36b3000a9847534dcb01a2e7066e9d93Jorim Jaggiimport com.android.systemui.statusbar.SpeedBumpView;
31572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek
32572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinekimport java.util.ArrayList;
33eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinekimport java.util.HashSet;
34eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinekimport java.util.Set;
35eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinekimport java.util.Stack;
36572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek
37572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek/**
38572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek * An stack state animator which handles animations to new StackScrollStates
39572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek */
40572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinekpublic class StackStateAnimator {
41572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek
425aa045cc6bca84f5c11f1a99999546ba5e5949a5Jorim Jaggi    public static final int ANIMATION_DURATION_STANDARD = 360;
4360d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi    public static final int ANIMATION_DURATION_GO_TO_FULL_SHADE = 448;
448efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek    public static final int ANIMATION_DURATION_APPEAR_DISAPPEAR = 464;
455aa045cc6bca84f5c11f1a99999546ba5e5949a5Jorim Jaggi    public static final int ANIMATION_DURATION_DIMMED_ACTIVATED = 220;
468efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek    public static final int ANIMATION_DELAY_PER_ELEMENT_INTERRUPTING = 80;
478efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek    public static final int ANIMATION_DELAY_PER_ELEMENT_MANUAL = 32;
4860d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi    public static final int ANIMATION_DELAY_PER_ELEMENT_GO_TO_FULL_SHADE = 48;
498efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek    private static final int DELAY_EFFECT_MAX_INDEX_DIFFERENCE = 2;
505aa045cc6bca84f5c11f1a99999546ba5e5949a5Jorim Jaggi
51eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private static final int TAG_ANIMATOR_TRANSLATION_Y = R.id.translation_y_animator_tag;
52eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private static final int TAG_ANIMATOR_TRANSLATION_Z = R.id.translation_z_animator_tag;
53d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi    private static final int TAG_ANIMATOR_SCALE = R.id.scale_animator_tag;
54eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private static final int TAG_ANIMATOR_ALPHA = R.id.alpha_animator_tag;
55eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private static final int TAG_ANIMATOR_HEIGHT = R.id.height_animator_tag;
56eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private static final int TAG_ANIMATOR_TOP_INSET = R.id.top_inset_animator_tag;
57eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private static final int TAG_END_TRANSLATION_Y = R.id.translation_y_animator_end_value_tag;
58eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private static final int TAG_END_TRANSLATION_Z = R.id.translation_z_animator_end_value_tag;
59d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi    private static final int TAG_END_SCALE = R.id.scale_animator_end_value_tag;
60eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private static final int TAG_END_ALPHA = R.id.alpha_animator_end_value_tag;
61eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private static final int TAG_END_HEIGHT = R.id.height_animator_end_value_tag;
62eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private static final int TAG_END_TOP_INSET = R.id.top_inset_animator_end_value_tag;
638df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek    private static final int TAG_START_TRANSLATION_Y = R.id.translation_y_animator_start_value_tag;
648df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek    private static final int TAG_START_TRANSLATION_Z = R.id.translation_z_animator_start_value_tag;
658df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek    private static final int TAG_START_SCALE = R.id.scale_animator_start_value_tag;
668df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek    private static final int TAG_START_ALPHA = R.id.alpha_animator_start_value_tag;
678df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek    private static final int TAG_START_HEIGHT = R.id.height_animator_start_value_tag;
688df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek    private static final int TAG_START_TOP_INSET = R.id.top_inset_animator_start_value_tag;
69572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek
70572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek    private final Interpolator mFastOutSlowInInterpolator;
7160d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi    private final int mGoToFullShadeAppearingTranslation;
72572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek    public NotificationStackScrollLayout mHostLayout;
73eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private ArrayList<NotificationStackScrollLayout.AnimationEvent> mNewEvents =
74eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            new ArrayList<>();
758efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek    private ArrayList<View> mNewAddChildren = new ArrayList<>();
7660d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi    private Set<Animator> mAnimatorSet = new HashSet<>();
7760d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi    private Stack<AnimatorListenerAdapter> mAnimationListenerPool = new Stack<>();
78d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi    private AnimationFilter mAnimationFilter = new AnimationFilter();
795aa045cc6bca84f5c11f1a99999546ba5e5949a5Jorim Jaggi    private long mCurrentLength;
80dbc3dce886cb7abba23a9f18e60d0f5af5c93226Jorim Jaggi    private long mCurrentAdditionalDelay;
81572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek
8260d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi    /** The current index for the last child which was not added in this event set. */
8360d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi    private int mCurrentLastNotAddedIndex;
8460d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi
858d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek    private ValueAnimator mTopOverScrollAnimator;
868d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek    private ValueAnimator mBottomOverScrollAnimator;
878d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek
88572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek    public StackStateAnimator(NotificationStackScrollLayout hostLayout) {
89572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek        mHostLayout = hostLayout;
90572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek        mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(hostLayout.getContext(),
91eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek                android.R.interpolator.fast_out_slow_in);
9260d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        mGoToFullShadeAppearingTranslation =
9360d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi                hostLayout.getContext().getResources().getDimensionPixelSize(
9460d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi                        R.dimen.go_to_full_shade_appearing_translation);
95572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek    }
96572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek
97572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek    public boolean isRunning() {
98eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        return !mAnimatorSet.isEmpty();
99572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek    }
100572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek
101572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek    public void startAnimationForEvents(
1020dd6881ea481c855976214807c17595b34a2920aJorim Jaggi            ArrayList<NotificationStackScrollLayout.AnimationEvent> mAnimationEvents,
103dbc3dce886cb7abba23a9f18e60d0f5af5c93226Jorim Jaggi            StackScrollState finalState, long additionalDelay) {
104eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek
105eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        processAnimationEvents(mAnimationEvents, finalState);
106eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek
107572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek        int childCount = mHostLayout.getChildCount();
108d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        mAnimationFilter.applyCombination(mNewEvents);
109dbc3dce886cb7abba23a9f18e60d0f5af5c93226Jorim Jaggi        mCurrentAdditionalDelay = additionalDelay;
1105aa045cc6bca84f5c11f1a99999546ba5e5949a5Jorim Jaggi        mCurrentLength = NotificationStackScrollLayout.AnimationEvent.combineLength(mNewEvents);
11160d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        mCurrentLastNotAddedIndex = findLastNotAddedIndex(finalState);
112572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek        for (int i = 0; i < childCount; i++) {
113572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek            final ExpandableView child = (ExpandableView) mHostLayout.getChildAt(i);
1148efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek
115572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek            StackScrollState.ViewState viewState = finalState.getViewStateForView(child);
1168efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek            if (viewState == null || child.getVisibility() == View.GONE) {
117572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek                continue;
118572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek            }
119572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek
120572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek            child.setClipBounds(null);
1218b73006a36b3000a9847534dcb01a2e7066e9d93Jorim Jaggi            startAnimations(child, viewState, finalState, i);
122572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek        }
123eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        if (!isRunning()) {
124eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            // no child has preformed any animation, lets finish
125eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            onAnimationFinished();
126eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        }
1278efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        mNewEvents.clear();
1288efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        mNewAddChildren.clear();
129572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek    }
130572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek
13160d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi    private int findLastNotAddedIndex(StackScrollState finalState) {
13260d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        int childCount = mHostLayout.getChildCount();
13360d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        for (int i = childCount - 1; i >= 0; i--) {
13460d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            final ExpandableView child = (ExpandableView) mHostLayout.getChildAt(i);
13560d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi
13660d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            StackScrollState.ViewState viewState = finalState.getViewStateForView(child);
13760d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            if (viewState == null || child.getVisibility() == View.GONE) {
13860d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi                continue;
13960d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            }
14060d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            if (!mNewAddChildren.contains(child)) {
14160d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi                return viewState.notGoneIndex;
14260d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            }
14360d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        }
14460d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        return -1;
14560d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi    }
14660d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi
147eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    /**
148eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek     * Start an animation to the given viewState
149eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek     */
1508efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek    private void startAnimations(final ExpandableView child, StackScrollState.ViewState viewState,
1518b73006a36b3000a9847534dcb01a2e7066e9d93Jorim Jaggi            StackScrollState finalState, int i) {
152eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        int childVisibility = child.getVisibility();
153eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        boolean wasVisible = childVisibility == View.VISIBLE;
154eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        final float alpha = viewState.alpha;
155eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        if (!wasVisible && alpha != 0 && !viewState.gone) {
156eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            child.setVisibility(View.VISIBLE);
157eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        }
1588efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek
1598efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        boolean yTranslationChanging = child.getTranslationY() != viewState.yTranslation;
1608efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        boolean zTranslationChanging = child.getTranslationZ() != viewState.zTranslation;
1618efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        boolean scaleChanging = child.getScaleX() != viewState.scale;
1628efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        boolean alphaChanging = alpha != child.getAlpha();
1638efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        boolean heightChanging = viewState.height != child.getActualHeight();
164708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        boolean topInsetChanging = viewState.clipTopAmount != child.getClipTopAmount();
1658efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        boolean wasAdded = mNewAddChildren.contains(child);
1668efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        boolean hasDelays = mAnimationFilter.hasDelays;
1678efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        boolean isDelayRelevant = yTranslationChanging || zTranslationChanging || scaleChanging ||
168708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                alphaChanging || heightChanging || topInsetChanging;
169cfc91cf2648cdaa2268e70ebfeed421ed86a6221Selim Cinek        boolean noAnimation = wasAdded;
1708efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        long delay = 0;
17160d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        long duration = mCurrentLength;
1728efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        if (hasDelays && isDelayRelevant || wasAdded) {
173dbc3dce886cb7abba23a9f18e60d0f5af5c93226Jorim Jaggi            delay = mCurrentAdditionalDelay + calculateChildAnimationDelay(viewState, finalState);
1748efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        }
1758efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek
17660d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        if (wasAdded && mAnimationFilter.hasGoToFullShadeEvent) {
17760d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            child.setTranslationY(child.getTranslationY() + mGoToFullShadeAppearingTranslation);
17860d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            yTranslationChanging = true;
17960d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            float longerDurationFactor = viewState.notGoneIndex - mCurrentLastNotAddedIndex;
18060d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            longerDurationFactor = (float) Math.pow(longerDurationFactor, 0.7f);
18160d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            duration = ANIMATION_DURATION_APPEAR_DISAPPEAR + 50 +
18260d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi                    (long) (100 * longerDurationFactor);
18360d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        }
18460d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi
185eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        // start translationY animation
1868efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        if (yTranslationChanging) {
187cfc91cf2648cdaa2268e70ebfeed421ed86a6221Selim Cinek            if (noAnimation && !mAnimationFilter.hasGoToFullShadeEvent) {
188d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek                child.setTranslationY(viewState.yTranslation);
189d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek            } else {
190d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek                startYTranslationAnimation(child, viewState, duration, delay);
191d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek            }
192eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        }
1938efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek
194eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        // start translationZ animation
1958efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        if (zTranslationChanging) {
196d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek            if (noAnimation) {
197d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek                child.setTranslationZ(viewState.zTranslation);
198d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek            } else {
199d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek                startZTranslationAnimation(child, viewState, duration, delay);
200d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek            }
201d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        }
2028efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek
203d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        // start scale animation
2048efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        if (scaleChanging) {
205d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek            if (noAnimation) {
206d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek                child.setScaleX(viewState.scale);
207d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek                child.setScaleY(viewState.scale);
208d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek            } else {
209d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek                startScaleAnimation(child, viewState, duration);
210d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek            }
211eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        }
2128efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek
213eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        // start alpha animation
214eceda3d83814e20cabddc4f0755d475fa2f3d8ffDan Sandler        if (alphaChanging && child.getTranslationX() == 0) {
215d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek            if (noAnimation) {
216d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek                child.setAlpha(viewState.alpha);
217d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek            } else {
218d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek                startAlphaAnimation(child, viewState, duration, delay);
219d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek            }
220eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        }
2218efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek
222eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        // start height animation
223d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek        if (heightChanging && child.getActualHeight() != 0) {
224d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek            if (noAnimation) {
225d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek                child.setActualHeight(viewState.height, false);
226d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek            } else {
227d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek                startHeightAnimation(child, viewState, duration, delay);
228d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek            }
22959b5a356b828fe60ea2874b0680a1bf7c84809a1Jorim Jaggi        }
2308efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek
231708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        // start top inset animation
232708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        if (topInsetChanging) {
233d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek            if (noAnimation) {
234d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek                child.setClipTopAmount(viewState.clipTopAmount);
235d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek            } else {
236d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek                startInsetAnimation(child, viewState, duration, delay);
237d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek            }
238708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        }
239708a6c120da6750d281195ef15a240a5627efed4Selim Cinek
240d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        // start dimmed animation
241d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek        child.setDimmed(viewState.dimmed, mAnimationFilter.animateDimmed && !wasAdded
242d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek                && !noAnimation);
2438efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek
244bf370992508c55d1f2493923bdc1834a0710e4baJohn Spurlock        // start dark animation
245d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek        child.setDark(viewState.dark, mAnimationFilter.animateDark && !noAnimation);
246bf370992508c55d1f2493923bdc1834a0710e4baJohn Spurlock
2473d2b94bf8e32640e57573ebb17911b1db9440231Selim Cinek        // apply speed bump state
2483d2b94bf8e32640e57573ebb17911b1db9440231Selim Cinek        child.setBelowSpeedBump(viewState.belowSpeedBump);
2493d2b94bf8e32640e57573ebb17911b1db9440231Selim Cinek
250ae44128776410abd11bd06ae700db9cc4606a773Jorim Jaggi        // start hiding sensitive animation
251d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek        child.setHideSensitive(viewState.hideSensitive, mAnimationFilter.animateHideSensitive &&
252d2319fbe6a53ac4c38ca02e4d8e32da86ed0994bSelim Cinek                !wasAdded && !noAnimation, delay, duration);
253ae44128776410abd11bd06ae700db9cc4606a773Jorim Jaggi
2548efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        if (wasAdded) {
25560d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            child.performAddAnimation(delay, mCurrentLength);
2568efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        }
2578b73006a36b3000a9847534dcb01a2e7066e9d93Jorim Jaggi        if (child instanceof SpeedBumpView) {
2588b73006a36b3000a9847534dcb01a2e7066e9d93Jorim Jaggi            finalState.performSpeedBumpAnimation(i, (SpeedBumpView) child, viewState,
2598b73006a36b3000a9847534dcb01a2e7066e9d93Jorim Jaggi                    delay + duration);
2608b73006a36b3000a9847534dcb01a2e7066e9d93Jorim Jaggi        }
2618efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek    }
2628efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek
2638efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek    private long calculateChildAnimationDelay(StackScrollState.ViewState viewState,
2648efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek            StackScrollState finalState) {
26560d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        if (mAnimationFilter.hasGoToFullShadeEvent) {
26660d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            return calculateDelayGoToFullShade(viewState);
26760d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        }
2688efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        long minDelay = 0;
2698efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        for (NotificationStackScrollLayout.AnimationEvent event : mNewEvents) {
2708efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek            long delayPerElement = ANIMATION_DELAY_PER_ELEMENT_INTERRUPTING;
2718efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek            switch (event.animationType) {
2728efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                case NotificationStackScrollLayout.AnimationEvent.ANIMATION_TYPE_ADD: {
2738efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    int ownIndex = viewState.notGoneIndex;
2748efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    int changingIndex = finalState
2758efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                            .getViewStateForView(event.changingView).notGoneIndex;
2768efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    int difference = Math.abs(ownIndex - changingIndex);
2778efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    difference = Math.max(0, Math.min(DELAY_EFFECT_MAX_INDEX_DIFFERENCE,
2788efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                            difference - 1));
2798efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    long delay = (DELAY_EFFECT_MAX_INDEX_DIFFERENCE - difference) * delayPerElement;
2808efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    minDelay = Math.max(delay, minDelay);
2818efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    break;
2828efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                }
2838efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                case NotificationStackScrollLayout.AnimationEvent.ANIMATION_TYPE_REMOVE_SWIPED_OUT:
2848efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    delayPerElement = ANIMATION_DELAY_PER_ELEMENT_MANUAL;
2858efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                case NotificationStackScrollLayout.AnimationEvent.ANIMATION_TYPE_REMOVE: {
2868efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    int ownIndex = viewState.notGoneIndex;
2878efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    boolean noNextView = event.viewAfterChangingView == null;
2888efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    View viewAfterChangingView = noNextView
2898efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                            ? mHostLayout.getLastChildNotGone()
2908efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                            : event.viewAfterChangingView;
2918efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek
2928efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    int nextIndex = finalState
2938efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                            .getViewStateForView(viewAfterChangingView).notGoneIndex;
2948efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    if (ownIndex >= nextIndex) {
2958efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                        // we only have the view afterwards
2968efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                        ownIndex++;
2978efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    }
2988efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    int difference = Math.abs(ownIndex - nextIndex);
2998efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    difference = Math.max(0, Math.min(DELAY_EFFECT_MAX_INDEX_DIFFERENCE,
3008efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                            difference - 1));
3018efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    long delay = difference * delayPerElement;
3028efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    minDelay = Math.max(delay, minDelay);
3038efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    break;
3048efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                }
3058efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                default:
3068efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    break;
3078efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek            }
3088efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        }
3098efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        return minDelay;
310572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek    }
311572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek
31260d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi    private long calculateDelayGoToFullShade(StackScrollState.ViewState viewState) {
31360d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        float index = viewState.notGoneIndex;
31460d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        index = (float) Math.pow(index, 0.7f);
31560d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        return (long) (index * ANIMATION_DELAY_PER_ELEMENT_GO_TO_FULL_SHADE);
31660d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi    }
31760d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi
318eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private void startHeightAnimation(final ExpandableView child,
31960d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            StackScrollState.ViewState viewState, long duration, long delay) {
3208df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        Integer previousStartValue = getChildTag(child, TAG_START_HEIGHT);
321d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        Integer previousEndValue = getChildTag(child, TAG_END_HEIGHT);
3228df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        int newEndValue = viewState.height;
3238df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        if (previousEndValue != null && previousEndValue == newEndValue) {
324eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            return;
325eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        }
326eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        ValueAnimator previousAnimator = getChildTag(child, TAG_ANIMATOR_HEIGHT);
3278df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        if (!mAnimationFilter.animateHeight) {
3288df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            // just a local update was performed
3298df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            if (previousAnimator != null) {
3308df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                // we need to increase all animation keyframes of the previous animator by the
3318df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                // relative change to the end value
3328df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                PropertyValuesHolder[] values = previousAnimator.getValues();
3338df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                int relativeDiff = newEndValue - previousEndValue;
3348df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                int newStartValue = previousStartValue + relativeDiff;
3358df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                values[0].setIntValues(newStartValue, newEndValue);
3368df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setTag(TAG_START_HEIGHT, newStartValue);
3378df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setTag(TAG_END_HEIGHT, newEndValue);
3388df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                previousAnimator.setCurrentPlayTime(previousAnimator.getCurrentPlayTime());
3398df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                return;
3408df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            } else {
3418df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                // no new animation needed, let's just apply the value
3428df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setActualHeight(newEndValue, false);
3438df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                return;
344eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            }
345eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        }
346eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek
3478df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        ValueAnimator animator = ValueAnimator.ofInt(child.getActualHeight(), newEndValue);
348eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
349572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek            @Override
350572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek            public void onAnimationUpdate(ValueAnimator animation) {
351d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi                child.setActualHeight((int) animation.getAnimatedValue(),
352d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi                        false /* notifyListeners */);
353572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek            }
354572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek        });
355eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        animator.setInterpolator(mFastOutSlowInInterpolator);
35660d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        long newDuration = cancelAnimatorAndGetNewDuration(duration, previousAnimator);
357eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        animator.setDuration(newDuration);
3588efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        if (delay > 0 && (previousAnimator == null || !previousAnimator.isRunning())) {
3598efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek            animator.setStartDelay(delay);
3608efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        }
361eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        animator.addListener(getGlobalAnimationFinishedListener());
362eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        // remove the tag when the animation is finished
363eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        animator.addListener(new AnimatorListenerAdapter() {
364eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            @Override
365eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            public void onAnimationEnd(Animator animation) {
366eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek                child.setTag(TAG_ANIMATOR_HEIGHT, null);
3678df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setTag(TAG_START_HEIGHT, null);
368eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek                child.setTag(TAG_END_HEIGHT, null);
369eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            }
370eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        });
3718efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        startAnimator(animator);
372eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        child.setTag(TAG_ANIMATOR_HEIGHT, animator);
3738df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        child.setTag(TAG_START_HEIGHT, child.getActualHeight());
3748df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        child.setTag(TAG_END_HEIGHT, newEndValue);
375eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    }
376eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek
377708a6c120da6750d281195ef15a240a5627efed4Selim Cinek    private void startInsetAnimation(final ExpandableView child,
37860d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            StackScrollState.ViewState viewState, long duration, long delay) {
379708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        Integer previousStartValue = getChildTag(child, TAG_START_TOP_INSET);
380708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        Integer previousEndValue = getChildTag(child, TAG_END_TOP_INSET);
381708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        int newEndValue = viewState.clipTopAmount;
382708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        if (previousEndValue != null && previousEndValue == newEndValue) {
383708a6c120da6750d281195ef15a240a5627efed4Selim Cinek            return;
384708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        }
385708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        ValueAnimator previousAnimator = getChildTag(child, TAG_ANIMATOR_TOP_INSET);
386708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        if (!mAnimationFilter.animateTopInset) {
387708a6c120da6750d281195ef15a240a5627efed4Selim Cinek            // just a local update was performed
388708a6c120da6750d281195ef15a240a5627efed4Selim Cinek            if (previousAnimator != null) {
389708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                // we need to increase all animation keyframes of the previous animator by the
390708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                // relative change to the end value
391708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                PropertyValuesHolder[] values = previousAnimator.getValues();
392708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                int relativeDiff = newEndValue - previousEndValue;
393708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                int newStartValue = previousStartValue + relativeDiff;
394708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                values[0].setIntValues(newStartValue, newEndValue);
395708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                child.setTag(TAG_START_TOP_INSET, newStartValue);
396708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                child.setTag(TAG_END_TOP_INSET, newEndValue);
397708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                previousAnimator.setCurrentPlayTime(previousAnimator.getCurrentPlayTime());
398708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                return;
399708a6c120da6750d281195ef15a240a5627efed4Selim Cinek            } else {
400708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                // no new animation needed, let's just apply the value
401708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                child.setClipTopAmount(newEndValue);
402708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                return;
403708a6c120da6750d281195ef15a240a5627efed4Selim Cinek            }
404708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        }
405708a6c120da6750d281195ef15a240a5627efed4Selim Cinek
406708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        ValueAnimator animator = ValueAnimator.ofInt(child.getClipTopAmount(), newEndValue);
407708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
408708a6c120da6750d281195ef15a240a5627efed4Selim Cinek            @Override
409708a6c120da6750d281195ef15a240a5627efed4Selim Cinek            public void onAnimationUpdate(ValueAnimator animation) {
410708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                child.setClipTopAmount((int) animation.getAnimatedValue());
411708a6c120da6750d281195ef15a240a5627efed4Selim Cinek            }
412708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        });
413708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        animator.setInterpolator(mFastOutSlowInInterpolator);
41460d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        long newDuration = cancelAnimatorAndGetNewDuration(duration, previousAnimator);
415708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        animator.setDuration(newDuration);
416708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        if (delay > 0 && (previousAnimator == null || !previousAnimator.isRunning())) {
417708a6c120da6750d281195ef15a240a5627efed4Selim Cinek            animator.setStartDelay(delay);
418708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        }
419708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        animator.addListener(getGlobalAnimationFinishedListener());
420708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        // remove the tag when the animation is finished
421708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        animator.addListener(new AnimatorListenerAdapter() {
422708a6c120da6750d281195ef15a240a5627efed4Selim Cinek            @Override
423708a6c120da6750d281195ef15a240a5627efed4Selim Cinek            public void onAnimationEnd(Animator animation) {
424708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                child.setTag(TAG_ANIMATOR_TOP_INSET, null);
425708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                child.setTag(TAG_START_TOP_INSET, null);
426708a6c120da6750d281195ef15a240a5627efed4Selim Cinek                child.setTag(TAG_END_TOP_INSET, null);
427708a6c120da6750d281195ef15a240a5627efed4Selim Cinek            }
428708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        });
429708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        startAnimator(animator);
430708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        child.setTag(TAG_ANIMATOR_TOP_INSET, animator);
431708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        child.setTag(TAG_START_TOP_INSET, child.getClipTopAmount());
432708a6c120da6750d281195ef15a240a5627efed4Selim Cinek        child.setTag(TAG_END_TOP_INSET, newEndValue);
433708a6c120da6750d281195ef15a240a5627efed4Selim Cinek    }
434708a6c120da6750d281195ef15a240a5627efed4Selim Cinek
435eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private void startAlphaAnimation(final ExpandableView child,
43660d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            final StackScrollState.ViewState viewState, long duration, long delay) {
4378df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        Float previousStartValue = getChildTag(child,TAG_START_ALPHA);
438eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        Float previousEndValue = getChildTag(child,TAG_END_ALPHA);
4398df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        final float newEndValue = viewState.alpha;
4408df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        if (previousEndValue != null && previousEndValue == newEndValue) {
441eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            return;
442eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        }
443eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        ObjectAnimator previousAnimator = getChildTag(child, TAG_ANIMATOR_ALPHA);
4448df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        if (!mAnimationFilter.animateAlpha) {
4458df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            // just a local update was performed
4468df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            if (previousAnimator != null) {
4478df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                // we need to increase all animation keyframes of the previous animator by the
4488df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                // relative change to the end value
4498df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                PropertyValuesHolder[] values = previousAnimator.getValues();
4508df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                float relativeDiff = newEndValue - previousEndValue;
4518df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                float newStartValue = previousStartValue + relativeDiff;
4528df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                values[0].setFloatValues(newStartValue, newEndValue);
4538df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setTag(TAG_START_ALPHA, newStartValue);
4548df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setTag(TAG_END_ALPHA, newEndValue);
4558df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                previousAnimator.setCurrentPlayTime(previousAnimator.getCurrentPlayTime());
4568df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                return;
4578df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            } else {
4588df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                // no new animation needed, let's just apply the value
4598df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setAlpha(newEndValue);
4608df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                if (newEndValue == 0) {
4618df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                    child.setVisibility(View.INVISIBLE);
4628df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                }
463eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            }
464eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        }
465eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek
466eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        ObjectAnimator animator = ObjectAnimator.ofFloat(child, View.ALPHA,
4678df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.getAlpha(), newEndValue);
468eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        animator.setInterpolator(mFastOutSlowInInterpolator);
469eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        // Handle layer type
470eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        child.setLayerType(View.LAYER_TYPE_HARDWARE, null);
471eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        animator.addListener(new AnimatorListenerAdapter() {
472eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            public boolean mWasCancelled;
473eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek
474eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            @Override
475eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            public void onAnimationEnd(Animator animation) {
4761efb240c1a0aeca9492cf8891794712adfdb1fa7Selim Cinek                child.setLayerType(View.LAYER_TYPE_NONE, null);
4778df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                if (newEndValue == 0 && !mWasCancelled) {
478eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek                    child.setVisibility(View.INVISIBLE);
479eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek                }
480eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek                child.setTag(TAG_ANIMATOR_ALPHA, null);
4818df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setTag(TAG_START_ALPHA, null);
482eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek                child.setTag(TAG_END_ALPHA, null);
483eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            }
484eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek
485eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            @Override
486eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            public void onAnimationCancel(Animator animation) {
487eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek                mWasCancelled = true;
488eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            }
489eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek
490eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            @Override
491eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            public void onAnimationStart(Animator animation) {
492eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek                mWasCancelled = false;
493eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            }
494eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        });
49560d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        long newDuration = cancelAnimatorAndGetNewDuration(duration, previousAnimator);
4963af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek        animator.setDuration(newDuration);
4978efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        if (delay > 0 && (previousAnimator == null || !previousAnimator.isRunning())) {
4988efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek            animator.setStartDelay(delay);
4998efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        }
500eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        animator.addListener(getGlobalAnimationFinishedListener());
501eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        // remove the tag when the animation is finished
502eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        animator.addListener(new AnimatorListenerAdapter() {
503eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            @Override
504eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            public void onAnimationEnd(Animator animation) {
505eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek
506eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            }
507eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        });
5088efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        startAnimator(animator);
509eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        child.setTag(TAG_ANIMATOR_ALPHA, animator);
5108df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        child.setTag(TAG_START_ALPHA, child.getAlpha());
5118df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        child.setTag(TAG_END_ALPHA, newEndValue);
512572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek    }
513572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek
514eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private void startZTranslationAnimation(final ExpandableView child,
51560d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            final StackScrollState.ViewState viewState, long duration, long delay) {
5168df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        Float previousStartValue = getChildTag(child,TAG_START_TRANSLATION_Z);
517eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        Float previousEndValue = getChildTag(child,TAG_END_TRANSLATION_Z);
5188df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        float newEndValue = viewState.zTranslation;
5198df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        if (previousEndValue != null && previousEndValue == newEndValue) {
520eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            return;
521eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        }
522eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        ObjectAnimator previousAnimator = getChildTag(child, TAG_ANIMATOR_TRANSLATION_Z);
5238df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        if (!mAnimationFilter.animateZ) {
5248df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            // just a local update was performed
5258df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            if (previousAnimator != null) {
5268df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                // we need to increase all animation keyframes of the previous animator by the
5278df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                // relative change to the end value
5288df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                PropertyValuesHolder[] values = previousAnimator.getValues();
5298df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                float relativeDiff = newEndValue - previousEndValue;
5308df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                float newStartValue = previousStartValue + relativeDiff;
5318df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                values[0].setFloatValues(newStartValue, newEndValue);
5328df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setTag(TAG_START_TRANSLATION_Z, newStartValue);
5338df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setTag(TAG_END_TRANSLATION_Z, newEndValue);
5348df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                previousAnimator.setCurrentPlayTime(previousAnimator.getCurrentPlayTime());
5358df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                return;
5368df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            } else {
5378df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                // no new animation needed, let's just apply the value
5388df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setTranslationZ(newEndValue);
539eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            }
540eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        }
541eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek
542eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        ObjectAnimator animator = ObjectAnimator.ofFloat(child, View.TRANSLATION_Z,
5438df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.getTranslationZ(), newEndValue);
544eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        animator.setInterpolator(mFastOutSlowInInterpolator);
54560d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        long newDuration = cancelAnimatorAndGetNewDuration(duration, previousAnimator);
5463af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek        animator.setDuration(newDuration);
5478efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        if (delay > 0 && (previousAnimator == null || !previousAnimator.isRunning())) {
5488efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek            animator.setStartDelay(delay);
5498efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        }
550eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        animator.addListener(getGlobalAnimationFinishedListener());
551eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        // remove the tag when the animation is finished
552eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        animator.addListener(new AnimatorListenerAdapter() {
553eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            @Override
554eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            public void onAnimationEnd(Animator animation) {
555eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek                child.setTag(TAG_ANIMATOR_TRANSLATION_Z, null);
5568df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setTag(TAG_START_TRANSLATION_Z, null);
557eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek                child.setTag(TAG_END_TRANSLATION_Z, null);
558eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            }
559eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        });
5608efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        startAnimator(animator);
561eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        child.setTag(TAG_ANIMATOR_TRANSLATION_Z, animator);
5628df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        child.setTag(TAG_START_TRANSLATION_Z, child.getTranslationZ());
5638df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        child.setTag(TAG_END_TRANSLATION_Z, newEndValue);
564eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    }
565eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek
566eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private void startYTranslationAnimation(final ExpandableView child,
56760d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            StackScrollState.ViewState viewState, long duration, long delay) {
5688df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        Float previousStartValue = getChildTag(child,TAG_START_TRANSLATION_Y);
569eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        Float previousEndValue = getChildTag(child,TAG_END_TRANSLATION_Y);
5708df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        float newEndValue = viewState.yTranslation;
5718df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        if (previousEndValue != null && previousEndValue == newEndValue) {
572eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            return;
573eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        }
574eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        ObjectAnimator previousAnimator = getChildTag(child, TAG_ANIMATOR_TRANSLATION_Y);
5758df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        if (!mAnimationFilter.animateY) {
5768df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            // just a local update was performed
5778df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            if (previousAnimator != null) {
5788df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                // we need to increase all animation keyframes of the previous animator by the
5798df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                // relative change to the end value
5808df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                PropertyValuesHolder[] values = previousAnimator.getValues();
5818df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                float relativeDiff = newEndValue - previousEndValue;
5828df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                float newStartValue = previousStartValue + relativeDiff;
5838df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                values[0].setFloatValues(newStartValue, newEndValue);
5848df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setTag(TAG_START_TRANSLATION_Y, newStartValue);
5858df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setTag(TAG_END_TRANSLATION_Y, newEndValue);
5868df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                previousAnimator.setCurrentPlayTime(previousAnimator.getCurrentPlayTime());
5878df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                return;
5888df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            } else {
5898df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                // no new animation needed, let's just apply the value
5908df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setTranslationY(newEndValue);
5918df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                return;
592eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            }
593eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        }
594eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek
595eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        ObjectAnimator animator = ObjectAnimator.ofFloat(child, View.TRANSLATION_Y,
5968df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.getTranslationY(), newEndValue);
597eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        animator.setInterpolator(mFastOutSlowInInterpolator);
59860d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        long newDuration = cancelAnimatorAndGetNewDuration(duration, previousAnimator);
5993af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek        animator.setDuration(newDuration);
6008efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        if (delay > 0 && (previousAnimator == null || !previousAnimator.isRunning())) {
6018efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek            animator.setStartDelay(delay);
6028efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        }
603eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        animator.addListener(getGlobalAnimationFinishedListener());
604eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        // remove the tag when the animation is finished
605eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        animator.addListener(new AnimatorListenerAdapter() {
606eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            @Override
607eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            public void onAnimationEnd(Animator animation) {
608eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek                child.setTag(TAG_ANIMATOR_TRANSLATION_Y, null);
6098df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setTag(TAG_START_TRANSLATION_Y, null);
610eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek                child.setTag(TAG_END_TRANSLATION_Y, null);
611eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek            }
612eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        });
6138efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        startAnimator(animator);
614eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        child.setTag(TAG_ANIMATOR_TRANSLATION_Y, animator);
6158df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        child.setTag(TAG_START_TRANSLATION_Y, child.getTranslationY());
6168df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        child.setTag(TAG_END_TRANSLATION_Y, newEndValue);
617eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    }
618eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek
619d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi    private void startScaleAnimation(final ExpandableView child,
62060d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi            StackScrollState.ViewState viewState, long duration) {
6218df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        Float previousStartValue = getChildTag(child, TAG_START_SCALE);
622d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        Float previousEndValue = getChildTag(child, TAG_END_SCALE);
6238df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        float newEndValue = viewState.scale;
6248df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        if (previousEndValue != null && previousEndValue == newEndValue) {
625d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi            return;
626d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        }
627d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        ObjectAnimator previousAnimator = getChildTag(child, TAG_ANIMATOR_SCALE);
6288df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        if (!mAnimationFilter.animateScale) {
6298df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            // just a local update was performed
6308df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            if (previousAnimator != null) {
6318df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                // we need to increase all animation keyframes of the previous animator by the
6328df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                // relative change to the end value
6338df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                PropertyValuesHolder[] values = previousAnimator.getValues();
6348df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                float relativeDiff = newEndValue - previousEndValue;
6358df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                float newStartValue = previousStartValue + relativeDiff;
6368df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                values[0].setFloatValues(newStartValue, newEndValue);
6378df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                values[1].setFloatValues(newStartValue, newEndValue);
6388df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setTag(TAG_START_SCALE, newStartValue);
6398df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setTag(TAG_END_SCALE, newEndValue);
6408df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                previousAnimator.setCurrentPlayTime(previousAnimator.getCurrentPlayTime());
6418df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                return;
6428df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            } else {
6438df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                // no new animation needed, let's just apply the value
6448df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setScaleX(newEndValue);
6458df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setScaleY(newEndValue);
646d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi            }
647d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        }
648d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi
649d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        PropertyValuesHolder holderX =
6508df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                PropertyValuesHolder.ofFloat(View.SCALE_X, child.getScaleX(), newEndValue);
651d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        PropertyValuesHolder holderY =
6528df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                PropertyValuesHolder.ofFloat(View.SCALE_Y, child.getScaleY(), newEndValue);
653d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(child, holderX, holderY);
654d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        animator.setInterpolator(mFastOutSlowInInterpolator);
65560d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        long newDuration = cancelAnimatorAndGetNewDuration(duration, previousAnimator);
656d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        animator.setDuration(newDuration);
657d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        animator.addListener(getGlobalAnimationFinishedListener());
658d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        // remove the tag when the animation is finished
659d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        animator.addListener(new AnimatorListenerAdapter() {
660d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi            @Override
661d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi            public void onAnimationEnd(Animator animation) {
662d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi                child.setTag(TAG_ANIMATOR_SCALE, null);
6638df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                child.setTag(TAG_START_SCALE, null);
664d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi                child.setTag(TAG_END_SCALE, null);
665d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi            }
666d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        });
6678efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        startAnimator(animator);
668d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi        child.setTag(TAG_ANIMATOR_SCALE, animator);
6698df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        child.setTag(TAG_START_SCALE, child.getScaleX());
6708df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek        child.setTag(TAG_END_SCALE, newEndValue);
671d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi    }
672d552d9d8e964c102e6832610be46cf2c041e8829Jorim Jaggi
6738efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek    private void startAnimator(ValueAnimator animator) {
6748efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek        mAnimatorSet.add(animator);
6753af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek        animator.start();
6763af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek    }
6773af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek
6783af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek    /**
6793af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek     * @return an adapter which ensures that onAnimationFinished is called once no animation is
6803af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek     *         running anymore
6813af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek     */
6823af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek    private AnimatorListenerAdapter getGlobalAnimationFinishedListener() {
6833af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek        if (!mAnimationListenerPool.empty()) {
6843af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek            return mAnimationListenerPool.pop();
6853af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek        }
6863af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek
6873af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek        // We need to create a new one, no reusable ones found
6883af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek        return new AnimatorListenerAdapter() {
6893af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek            private boolean mWasCancelled;
6903af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek
6913af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek            @Override
6923af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek            public void onAnimationEnd(Animator animation) {
6933af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek                mAnimatorSet.remove(animation);
6943af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek                if (mAnimatorSet.isEmpty() && !mWasCancelled) {
6953af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek                    onAnimationFinished();
6963af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek                }
6973af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek                mAnimationListenerPool.push(this);
6983af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek            }
6993af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek
7003af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek            @Override
7013af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek            public void onAnimationCancel(Animator animation) {
7023af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek                mWasCancelled = true;
7033af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek            }
7043af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek
7053af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek            @Override
7063af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek            public void onAnimationStart(Animator animation) {
7073af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek                mWasCancelled = false;
7083af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek            }
7093af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek        };
7103af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek    }
7113af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek
712eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private <T> T getChildTag(View child, int tag) {
713eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        return (T) child.getTag(tag);
714eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    }
715eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek
716eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    /**
717eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek     * Cancel the previous animator and get the duration of the new animation.
71839610545f0c2714a3526bc935effe57b421542d1Selim Cinek     *
71960d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi     * @param duration the new duration
720eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek     * @param previousAnimator the animator which was running before
721eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek     * @return the new duration
722eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek     */
72360d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi    private long cancelAnimatorAndGetNewDuration(long duration, ValueAnimator previousAnimator) {
72460d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi        long newDuration = duration;
725eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        if (previousAnimator != null) {
7268df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            // We take either the desired length of the new animation or the remaining time of
7278df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            // the previous animator, whichever is longer.
7288df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek            newDuration = Math.max(previousAnimator.getDuration()
7298df56452cb696ebdee82df6fb255892eabf3febcSelim Cinek                    - previousAnimator.getCurrentPlayTime(), newDuration);
7303af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek            previousAnimator.cancel();
731eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        }
7323af00cf10660c7fdc0582dc12361c13673d0c9bbSelim Cinek        return newDuration;
733eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    }
734eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek
735eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private void onAnimationFinished() {
736eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek        mHostLayout.onChildAnimationFinished();
737eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    }
738eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek
739eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    /**
740eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek     * Process the animationEvents for a new animation
741eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek     *
742eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek     * @param animationEvents the animation events for the animation to perform
74339610545f0c2714a3526bc935effe57b421542d1Selim Cinek     * @param finalState the final state to animate to
74439610545f0c2714a3526bc935effe57b421542d1Selim Cinek     */
745eb973565f3efc6417ca35363e4d6c642947775d8Selim Cinek    private void processAnimationEvents(
7460dd6881ea481c855976214807c17595b34a2920aJorim Jaggi            ArrayList<NotificationStackScrollLayout.AnimationEvent> animationEvents,
747572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek            StackScrollState finalState) {
7488d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek        for (NotificationStackScrollLayout.AnimationEvent event : animationEvents) {
7498efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek            final ExpandableView changingView = (ExpandableView) event.changingView;
7508efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek            if (event.animationType ==
7518efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    NotificationStackScrollLayout.AnimationEvent.ANIMATION_TYPE_ADD) {
7528efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek
7538efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                // This item is added, initialize it's properties.
7548efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                StackScrollState.ViewState viewState = finalState
7558efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                        .getViewStateForView(changingView);
7568efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                if (viewState == null) {
7578efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    // The position for this child was never generated, let's continue.
7588efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    continue;
7598efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                }
7608efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                if (changingView.getVisibility() == View.GONE) {
7618efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    // The view was set to gone but the state never removed
7628efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    finalState.removeViewStateForView(changingView);
7638efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    continue;
76439610545f0c2714a3526bc935effe57b421542d1Selim Cinek                }
7658efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                changingView.setAlpha(viewState.alpha);
7668efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                changingView.setTranslationY(viewState.yTranslation);
7678efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                changingView.setTranslationZ(viewState.zTranslation);
7688efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                changingView.setActualHeight(viewState.height, false);
7698efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                mNewAddChildren.add(changingView);
7708efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek
7718efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek            } else if (event.animationType ==
7728efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    NotificationStackScrollLayout.AnimationEvent.ANIMATION_TYPE_REMOVE) {
7738efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                if (changingView.getVisibility() == View.GONE) {
77495ed59283bd25fb363d13c000a7408bcafb5e93eSelim Cinek                    mHostLayout.getOverlay().remove(changingView);
7758efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    continue;
7768efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                }
7778efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek
7788efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                // Find the amount to translate up. This is needed in order to understand the
7798efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                // direction of the remove animation (either downwards or upwards)
7808efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                StackScrollState.ViewState viewState = finalState
7818efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                        .getViewStateForView(event.viewAfterChangingView);
7828efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                int actualHeight = changingView.getActualHeight();
7838efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                // upwards by default
7848efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                float translationDirection = -1.0f;
7858efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                if (viewState != null) {
7868efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    // there was a view after this one, Approximate the distance the next child
7878efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    // travelled
7888efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    translationDirection = ((viewState.yTranslation
7898efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                            - (changingView.getTranslationY() + actualHeight / 2.0f)) * 2 /
7908efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                            actualHeight);
7918efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    translationDirection = Math.max(Math.min(translationDirection, 1.0f),-1.0f);
7928efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek
7938efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                }
79460d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi                changingView.performRemoveAnimation(ANIMATION_DURATION_APPEAR_DISAPPEAR,
79560d07c597c3f996deb3f2743466fe5279ca15e8dJorim Jaggi                        translationDirection, new Runnable() {
7968efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    @Override
7978efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    public void run() {
7988efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                        // remove the temporary overlay
7998efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                        mHostLayout.getOverlay().remove(changingView);
8008efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                    }
8018efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek                });
802572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek            }
8038efa6dde2b4f2cdbf046b87b7366404c3cc46219Selim Cinek            mNewEvents.add(event);
804572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek        }
805572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek    }
8068d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek
807475b21dfe517ec04f435f6b02f4a53083d040db4Jorim Jaggi    public void animateOverScrollToAmount(float targetAmount, final boolean onTop,
808475b21dfe517ec04f435f6b02f4a53083d040db4Jorim Jaggi            final boolean isRubberbanded) {
8098d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek        final float startOverScrollAmount = mHostLayout.getCurrentOverScrollAmount(onTop);
8109012958742c7a66b37ba5f2196f9086bb1980e6bJorim Jaggi        if (targetAmount == startOverScrollAmount) {
8119012958742c7a66b37ba5f2196f9086bb1980e6bJorim Jaggi            return;
8129012958742c7a66b37ba5f2196f9086bb1980e6bJorim Jaggi        }
8138d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek        cancelOverScrollAnimators(onTop);
8148d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek        ValueAnimator overScrollAnimator = ValueAnimator.ofFloat(startOverScrollAmount,
8158d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek                targetAmount);
8168d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek        overScrollAnimator.setDuration(ANIMATION_DURATION_STANDARD);
8178d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek        overScrollAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
8188d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek            @Override
8198d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek            public void onAnimationUpdate(ValueAnimator animation) {
8208d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek                float currentOverScroll = (float) animation.getAnimatedValue();
82147c85a3525dcd0bbd3168632830e8ab491d18462Jorim Jaggi                mHostLayout.setOverScrollAmount(
822475b21dfe517ec04f435f6b02f4a53083d040db4Jorim Jaggi                        currentOverScroll, onTop, false /* animate */, false /* cancelAnimators */,
823475b21dfe517ec04f435f6b02f4a53083d040db4Jorim Jaggi                        isRubberbanded);
8248d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek            }
8258d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek        });
8268d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek        overScrollAnimator.setInterpolator(mFastOutSlowInInterpolator);
82747c85a3525dcd0bbd3168632830e8ab491d18462Jorim Jaggi        overScrollAnimator.addListener(new AnimatorListenerAdapter() {
82847c85a3525dcd0bbd3168632830e8ab491d18462Jorim Jaggi            @Override
82947c85a3525dcd0bbd3168632830e8ab491d18462Jorim Jaggi            public void onAnimationEnd(Animator animation) {
83047c85a3525dcd0bbd3168632830e8ab491d18462Jorim Jaggi                if (onTop) {
83147c85a3525dcd0bbd3168632830e8ab491d18462Jorim Jaggi                    mTopOverScrollAnimator = null;
83247c85a3525dcd0bbd3168632830e8ab491d18462Jorim Jaggi                } else {
83347c85a3525dcd0bbd3168632830e8ab491d18462Jorim Jaggi                    mBottomOverScrollAnimator = null;
83447c85a3525dcd0bbd3168632830e8ab491d18462Jorim Jaggi                }
83547c85a3525dcd0bbd3168632830e8ab491d18462Jorim Jaggi            }
83647c85a3525dcd0bbd3168632830e8ab491d18462Jorim Jaggi        });
8378d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek        overScrollAnimator.start();
8388d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek        if (onTop) {
8398d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek            mTopOverScrollAnimator = overScrollAnimator;
8408d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek        } else {
8418d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek            mBottomOverScrollAnimator = overScrollAnimator;
8428d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek        }
8438d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek    }
8448d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek
8458d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek    public void cancelOverScrollAnimators(boolean onTop) {
8468d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek        ValueAnimator currentAnimator = onTop ? mTopOverScrollAnimator : mBottomOverScrollAnimator;
8478d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek        if (currentAnimator != null) {
8488d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek            currentAnimator.cancel();
8498d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek        }
8508d9ff9c2c66bc1d3b92eb6992d58599ff80ed6dcSelim Cinek    }
851572bbd42a473980c2d59af80d378f6270ba6860aSelim Cinek}
852