StackStateAnimator.java revision 59b5a356b828fe60ea2874b0680a1bf7c84809a1
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.systemui.statusbar.stack;
18
19import android.animation.ValueAnimator;
20import android.view.View;
21import android.view.animation.AnimationUtils;
22import android.view.animation.Interpolator;
23import com.android.systemui.statusbar.ExpandableView;
24
25import java.util.ArrayList;
26
27/**
28 * An stack state animator which handles animations to new StackScrollStates
29 */
30public class StackStateAnimator {
31
32    private static final int ANIMATION_DURATION = 360;
33
34    private final Interpolator mFastOutSlowInInterpolator;
35    public NotificationStackScrollLayout mHostLayout;
36    private boolean mAnimationIsRunning;
37    private ArrayList<NotificationStackScrollLayout.AnimationEvent> mHandledEvents =
38            new ArrayList<>();
39
40    public StackStateAnimator(NotificationStackScrollLayout hostLayout) {
41        mHostLayout = hostLayout;
42        mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(hostLayout.getContext(),
43                        android.R.interpolator.fast_out_slow_in);
44    }
45
46    public boolean isRunning() {
47        return mAnimationIsRunning;
48    }
49
50    public void startAnimationForEvents(
51            ArrayList<NotificationStackScrollLayout.AnimationEvent> mAnimationEvents,
52            StackScrollState finalState) {
53        int numEvents = mAnimationEvents.size();
54        if (numEvents == 0) {
55            // No events, so we don't perform any animation
56            return;
57        }
58        long lastEventStartTime = mAnimationEvents.get(numEvents - 1).eventStartTime;
59        long eventEnd = lastEventStartTime + ANIMATION_DURATION;
60        long currentTime = AnimationUtils.currentAnimationTimeMillis();
61        long newDuration = eventEnd - currentTime;
62        if (newDuration <= 0) {
63            // last event is long before this, so we don't do anything
64            return;
65        }
66        initializeAddedViewStates(mAnimationEvents, finalState);
67        int childCount = mHostLayout.getChildCount();
68        boolean isFirstAnimatingView = true;
69        for (int i = 0; i < childCount; i++) {
70            final ExpandableView child = (ExpandableView) mHostLayout.getChildAt(i);
71            StackScrollState.ViewState viewState = finalState.getViewStateForView(child);
72            if (viewState == null) {
73                continue;
74            }
75            int childVisibility = child.getVisibility();
76            boolean wasVisible = childVisibility == View.VISIBLE;
77            final float alpha = viewState.alpha;
78            if (!wasVisible && alpha != 0 && !viewState.gone) {
79                child.setVisibility(View.VISIBLE);
80            }
81
82            startPropertyAnimation(newDuration, isFirstAnimatingView, child, viewState, alpha);
83
84            // TODO: animate clipBounds
85            child.setClipBounds(null);
86            int currentHeigth = child.getActualHeight();
87            if (viewState.height != currentHeigth) {
88                startHeightAnimation(newDuration, child, viewState, currentHeigth);
89            }
90            isFirstAnimatingView = false;
91        }
92        mAnimationIsRunning = true;
93    }
94
95    private void startPropertyAnimation(long newDuration, final boolean hasFinishAction,
96            final ExpandableView child, StackScrollState.ViewState viewState, final float alpha) {
97        child.animate().setInterpolator(mFastOutSlowInInterpolator)
98                .translationY(viewState.yTranslation)
99                .translationZ(viewState.zTranslation)
100                .setDuration(newDuration)
101                .withEndAction(new Runnable() {
102                    @Override
103                    public void run() {
104                        mAnimationIsRunning = false;
105                        if (hasFinishAction) {
106                            mHandledEvents.clear();
107                            mHostLayout.onChildAnimationFinished();
108                        }
109                        if (alpha == 0) {
110                            child.setVisibility(View.INVISIBLE);
111                        }
112                    }
113                });
114        if (alpha != child.getAlpha()) {
115            child.animate().withLayer().alpha(alpha);
116        }
117    }
118
119    private void startHeightAnimation(long newDuration, final ExpandableView child,
120            StackScrollState.ViewState viewState, int currentHeigth) {
121        ValueAnimator heightAnimator = ValueAnimator.ofInt(currentHeigth, viewState.height);
122        heightAnimator.setInterpolator(mFastOutSlowInInterpolator);
123        heightAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
124            @Override
125            public void onAnimationUpdate(ValueAnimator animation) {
126                child.setActualHeight((int) animation.getAnimatedValue());
127            }
128        });
129        heightAnimator.setDuration(newDuration);
130        heightAnimator.start();
131    }
132
133    /**
134     * Initialize the viewStates for the added children
135     *
136     * @param animationEvents the animation events who contain the added children
137     * @param finalState the final state to animate to
138     */
139    private void initializeAddedViewStates(
140            ArrayList<NotificationStackScrollLayout.AnimationEvent> animationEvents,
141            StackScrollState finalState) {
142        for (NotificationStackScrollLayout.AnimationEvent event: animationEvents) {
143            View changingView = event.changingView;
144            if (event.animationType == NotificationStackScrollLayout.AnimationEvent
145                    .ANIMATION_TYPE_ADD && !mHandledEvents.contains(event)) {
146
147                // This item is added, initialize it's properties.
148                StackScrollState.ViewState viewState = finalState.getViewStateForView(changingView);
149                if (viewState == null) {
150                    // The position for this child was never generated, let's continue.
151                    continue;
152                }
153                changingView.setAlpha(0);
154                changingView.setTranslationY(viewState.yTranslation);
155                changingView.setTranslationZ(viewState.zTranslation);
156                mHandledEvents.add(event);
157            }
158        }
159    }
160}
161