Home | History | Annotate | Download | only in launcher2
      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 
     17 package com.android.launcher2;
     18 
     19 import android.animation.Animator;
     20 import android.animation.AnimatorListenerAdapter;
     21 import android.animation.ValueAnimator;
     22 import android.util.Log;
     23 import android.view.View;
     24 import android.view.ViewPropertyAnimator;
     25 import 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  */
     32 public 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         boolean isFinalFrame = Float.compare(1f, animation.getAnimatedFraction()) == 0;
     95 
     96         if (!mHandlingOnAnimationUpdate &&
     97             sVisible &&
     98             // If the current play time exceeds the duration, or the animated fraction is 1,
     99             // the animation will get finished, even if we call setCurrentPlayTime -- therefore
    100             // don't adjust the animation in that case
    101             animation.getCurrentPlayTime() < animation.getDuration() && !isFinalFrame) {
    102             mHandlingOnAnimationUpdate = true;
    103             long frameNum = sGlobalFrameCounter - mStartFrame;
    104             // If we haven't drawn our first frame, reset the time to t = 0
    105             // (give up after MAX_DELAY ms of waiting though - might happen, for example, if we
    106             // are no longer in the foreground and no frames are being rendered ever)
    107             if (frameNum == 0 && currentTime < mStartTime + MAX_DELAY) {
    108                 // The first frame on animations doesn't always trigger an invalidate...
    109                 // force an invalidate here to make sure the animation continues to advance
    110                 mTarget.getRootView().invalidate();
    111                 animation.setCurrentPlayTime(0);
    112 
    113             // For the second frame, if the first frame took more than 16ms,
    114             // adjust the start time and pretend it took only 16ms anyway. This
    115             // prevents a large jump in the animation due to an expensive first frame
    116             } else if (frameNum == 1 && currentTime < mStartTime + MAX_DELAY &&
    117                        !mAdjustedSecondFrameTime &&
    118                        currentTime > mStartTime + IDEAL_FRAME_DURATION) {
    119                 animation.setCurrentPlayTime(IDEAL_FRAME_DURATION);
    120                 mAdjustedSecondFrameTime = true;
    121             } else {
    122                 if (frameNum > 1) {
    123                     mTarget.post(new Runnable() {
    124                             public void run() {
    125                                 animation.removeUpdateListener(FirstFrameAnimatorHelper.this);
    126                             }
    127                         });
    128                 }
    129                 if (DEBUG) print(animation);
    130             }
    131             mHandlingOnAnimationUpdate = false;
    132         } else {
    133             if (DEBUG) print(animation);
    134         }
    135     }
    136 
    137     public void print(ValueAnimator animation) {
    138         float flatFraction = animation.getCurrentPlayTime() / (float) animation.getDuration();
    139         Log.d("FirstFrameAnimatorHelper", sGlobalFrameCounter +
    140               "(" + (sGlobalFrameCounter - mStartFrame) + ") " + mTarget + " dirty? " +
    141               mTarget.isDirty() + " " + flatFraction + " " + this + " " + animation);
    142     }
    143 }
    144