StackStateAnimator.java revision 572bbd42a473980c2d59af80d378f6270ba6860a
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.Animator;
20import android.animation.ValueAnimator;
21import android.view.View;
22import android.view.animation.AnimationUtils;
23import android.view.animation.Interpolator;
24import com.android.systemui.statusbar.ExpandableView;
25
26import java.util.ArrayList;
27
28/**
29 * An stack state animator which handles animations to new StackScrollStates
30 */
31public class StackStateAnimator {
32
33    private static final int ANIMATION_DURATION = 360;
34
35    private final Interpolator mFastOutSlowInInterpolator;
36    public NotificationStackScrollLayout mHostLayout;
37    private boolean mAnimationIsRunning;
38    private ArrayList<NotificationStackScrollLayout.ChildHierarchyChangeEvent> mHandledEvents =
39            new ArrayList<>();
40
41    public StackStateAnimator(NotificationStackScrollLayout hostLayout) {
42        mHostLayout = hostLayout;
43        mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(hostLayout.getContext(),
44                        android.R.interpolator.fast_out_slow_in);
45    }
46
47    public boolean isRunning() {
48        return mAnimationIsRunning;
49    }
50
51    public void startAnimationForEvents(
52            ArrayList<NotificationStackScrollLayout.ChildHierarchyChangeEvent> mAnimationEvents,
53            StackScrollState finalState) {
54        int numEvents = mAnimationEvents.size();
55        if (numEvents == 0) {
56            // No events, so we don't perform any animation
57            return;
58        }
59        long lastEventStartTime = mAnimationEvents.get(numEvents - 1).eventStartTime;
60        long eventEnd = lastEventStartTime + ANIMATION_DURATION;
61        long currentTime = AnimationUtils.currentAnimationTimeMillis();
62        long newDuration = eventEnd - currentTime;
63        if (newDuration <= 0) {
64            // last event is long before this, so we don't do anything
65            return;
66        }
67        initializeAddedViewStates(mAnimationEvents, finalState);
68        int childCount = mHostLayout.getChildCount();
69        for (int i = 0; i < childCount; i++) {
70            final boolean isFirstView = i == 0;
71            final ExpandableView child = (ExpandableView) mHostLayout.getChildAt(i);
72            StackScrollState.ViewState viewState = finalState.getViewStateForView(child);
73            if (viewState == null) {
74                continue;
75            }
76            int childVisibility = child.getVisibility();
77            boolean wasVisible = childVisibility == View.VISIBLE;
78            final float alpha = viewState.alpha;
79            if (!wasVisible && alpha != 0 && !viewState.gone) {
80                child.setVisibility(View.VISIBLE);
81            }
82
83            startPropertyAnimation(newDuration, isFirstView, child, viewState, alpha);
84
85            // TODO: animate clipBounds
86            child.setClipBounds(null);
87            int currentHeigth = child.getActualHeight();
88            if (viewState.height != currentHeigth) {
89                startHeightAnimation(newDuration, child, viewState, currentHeigth);
90            }
91        }
92        mAnimationIsRunning = true;
93    }
94
95    private void startPropertyAnimation(long newDuration, final boolean isFirstView,
96            final ExpandableView child, StackScrollState.ViewState viewState, final float alpha) {
97        child.animate().setInterpolator(mFastOutSlowInInterpolator)
98                .alpha(alpha)
99                .translationY(viewState.yTranslation)
100                .translationZ(viewState.zTranslation)
101                .setDuration(newDuration)
102                .withEndAction(new Runnable() {
103                    @Override
104                    public void run() {
105                        mAnimationIsRunning = false;
106                        if (isFirstView) {
107                            mHandledEvents.clear();
108                            mHostLayout.onChildAnimationFinished();
109                        }
110                        if (alpha == 0) {
111                            child.setVisibility(View.INVISIBLE);
112                        }
113                    }
114                });
115    }
116
117    private void startHeightAnimation(long newDuration, final ExpandableView child,
118            StackScrollState.ViewState viewState, int currentHeigth) {
119        ValueAnimator heightAnimator = ValueAnimator.ofInt(currentHeigth, viewState.height);
120        heightAnimator.setInterpolator(mFastOutSlowInInterpolator);
121        heightAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
122            @Override
123            public void onAnimationUpdate(ValueAnimator animation) {
124                child.setActualHeight((int) animation.getAnimatedValue());
125            }
126        });
127        heightAnimator.setDuration(newDuration);
128        heightAnimator.start();
129    }
130
131    private void initializeAddedViewStates(
132            ArrayList<NotificationStackScrollLayout.ChildHierarchyChangeEvent> mAnimationEvents,
133            StackScrollState finalState) {
134        for (NotificationStackScrollLayout.ChildHierarchyChangeEvent event: mAnimationEvents) {
135            View changingView = event.changingView;
136            if (event.animationType == NotificationStackScrollLayout.ChildHierarchyChangeEvent
137                    .ANIMATION_TYPE_ADD && !mHandledEvents.contains(event)) {
138
139                // This item is added, initialize it's properties.
140                StackScrollState.ViewState viewState = finalState.getViewStateForView(changingView);
141                changingView.setAlpha(0);
142                changingView.setTranslationY(viewState.yTranslation);
143                changingView.setTranslationZ(viewState.zTranslation);
144                mHandledEvents.add(event);
145            }
146        }
147    }
148}
149