1/*
2 * Copyright (C) 2013 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.launcher3;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ValueAnimator;
22import android.util.Log;
23import android.view.View;
24import android.view.ViewPropertyAnimator;
25import android.view.ViewTreeObserver;
26
27/*
28 *  This is a helper class that listens to updates from the corresponding animation.
29 *  For the first two frames, it adjusts the current play time of the animation to
30 *  prevent jank at the beginning of the animation
31 */
32public class FirstFrameAnimatorHelper extends AnimatorListenerAdapter
33    implements ValueAnimator.AnimatorUpdateListener {
34    private static final boolean DEBUG = false;
35    private static final int MAX_DELAY = 1000;
36    private static final int IDEAL_FRAME_DURATION = 16;
37    private View mTarget;
38    private long mStartFrame;
39    private long mStartTime = -1;
40    private boolean mHandlingOnAnimationUpdate;
41    private boolean mAdjustedSecondFrameTime;
42
43    private static ViewTreeObserver.OnDrawListener sGlobalDrawListener;
44    private static long sGlobalFrameCounter;
45    private static boolean sVisible;
46
47    public FirstFrameAnimatorHelper(ValueAnimator animator, View target) {
48        mTarget = target;
49        animator.addUpdateListener(this);
50    }
51
52    public FirstFrameAnimatorHelper(ViewPropertyAnimator vpa, View target) {
53        mTarget = target;
54        vpa.setListener(this);
55    }
56
57    // only used for ViewPropertyAnimators
58    public void onAnimationStart(Animator animation) {
59        final ValueAnimator va = (ValueAnimator) animation;
60        va.addUpdateListener(FirstFrameAnimatorHelper.this);
61        onAnimationUpdate(va);
62    }
63
64    public static void setIsVisible(boolean visible) {
65        sVisible = visible;
66    }
67
68    public static void initializeDrawListener(View view) {
69        if (sGlobalDrawListener != null) {
70            view.getViewTreeObserver().removeOnDrawListener(sGlobalDrawListener);
71        }
72        sGlobalDrawListener = new ViewTreeObserver.OnDrawListener() {
73                private long mTime = System.currentTimeMillis();
74                public void onDraw() {
75                    sGlobalFrameCounter++;
76                    if (DEBUG) {
77                        long newTime = System.currentTimeMillis();
78                        Log.d("FirstFrameAnimatorHelper", "TICK " + (newTime - mTime));
79                        mTime = newTime;
80                    }
81                }
82            };
83        view.getViewTreeObserver().addOnDrawListener(sGlobalDrawListener);
84        sVisible = true;
85    }
86
87    public void onAnimationUpdate(final ValueAnimator animation) {
88        final long currentTime = System.currentTimeMillis();
89        if (mStartTime == -1) {
90            mStartFrame = sGlobalFrameCounter;
91            mStartTime = currentTime;
92        }
93
94        final long currentPlayTime = animation.getCurrentPlayTime();
95        if (!mHandlingOnAnimationUpdate &&
96            sVisible &&
97            // If the current play time exceeds the duration, the animation
98            // will get finished, even if we call setCurrentPlayTime -- therefore
99            // don't adjust the animation in that case
100            currentPlayTime < animation.getDuration()) {
101            mHandlingOnAnimationUpdate = true;
102            long frameNum = sGlobalFrameCounter - mStartFrame;
103            // If we haven't drawn our first frame, reset the time to t = 0
104            // (give up after MAX_DELAY ms of waiting though - might happen, for example, if we
105            // are no longer in the foreground and no frames are being rendered ever)
106            if (frameNum == 0 && currentTime < mStartTime + MAX_DELAY && currentPlayTime > 0) {
107                // The first frame on animations doesn't always trigger an invalidate...
108                // force an invalidate here to make sure the animation continues to advance
109                mTarget.getRootView().invalidate();
110                animation.setCurrentPlayTime(0);
111            // For the second frame, if the first frame took more than 16ms,
112            // adjust the start time and pretend it took only 16ms anyway. This
113            // prevents a large jump in the animation due to an expensive first frame
114            } else if (frameNum == 1 && currentTime < mStartTime + MAX_DELAY &&
115                       !mAdjustedSecondFrameTime &&
116                       currentTime > mStartTime + IDEAL_FRAME_DURATION &&
117                       currentPlayTime > IDEAL_FRAME_DURATION) {
118                animation.setCurrentPlayTime(IDEAL_FRAME_DURATION);
119                mAdjustedSecondFrameTime = true;
120            } else {
121                if (frameNum > 1) {
122                    mTarget.post(new Runnable() {
123                            public void run() {
124                                animation.removeUpdateListener(FirstFrameAnimatorHelper.this);
125                            }
126                        });
127                }
128                if (DEBUG) print(animation);
129            }
130            mHandlingOnAnimationUpdate = false;
131        } else {
132            if (DEBUG) print(animation);
133        }
134    }
135
136    public void print(ValueAnimator animation) {
137        float flatFraction = animation.getCurrentPlayTime() / (float) animation.getDuration();
138        Log.d("FirstFrameAnimatorHelper", sGlobalFrameCounter +
139              "(" + (sGlobalFrameCounter - mStartFrame) + ") " + mTarget + " dirty? " +
140              mTarget.isDirty() + " " + flatFraction + " " + this + " " + animation);
141    }
142}
143