Home | History | Annotate | Download | only in notification
      1 /*
      2  * Copyright (C) 2016 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.systemui.statusbar.notification;
     18 
     19 import android.graphics.drawable.Icon;
     20 import android.util.Pools;
     21 import android.view.View;
     22 import android.widget.ImageView;
     23 
     24 import com.android.systemui.Interpolators;
     25 import com.android.systemui.R;
     26 import com.android.systemui.statusbar.CrossFadeHelper;
     27 import com.android.systemui.statusbar.TransformableView;
     28 import com.android.systemui.statusbar.stack.StackStateAnimator;
     29 
     30 /**
     31  * A transform state of a image view.
     32 */
     33 public class ImageTransformState extends TransformState {
     34     public static final long ANIMATION_DURATION_LENGTH = 210;
     35 
     36     public static final int ICON_TAG = R.id.image_icon_tag;
     37     private static Pools.SimplePool<ImageTransformState> sInstancePool
     38             = new Pools.SimplePool<>(40);
     39     private Icon mIcon;
     40 
     41     @Override
     42     public void initFrom(View view) {
     43         super.initFrom(view);
     44         if (view instanceof ImageView) {
     45             mIcon = (Icon) view.getTag(ICON_TAG);
     46         }
     47     }
     48 
     49     @Override
     50     protected boolean sameAs(TransformState otherState) {
     51         if (super.sameAs(otherState)) {
     52             return true;
     53         }
     54         if (otherState instanceof ImageTransformState) {
     55             return mIcon != null && mIcon.sameAs(((ImageTransformState) otherState).getIcon());
     56         }
     57         return false;
     58     }
     59 
     60     @Override
     61     public void appear(float transformationAmount, TransformableView otherView) {
     62         if (otherView instanceof HybridNotificationView) {
     63             if (transformationAmount == 0.0f) {
     64                 mTransformedView.setPivotY(0);
     65                 mTransformedView.setPivotX(mTransformedView.getWidth() / 2);
     66                 prepareFadeIn();
     67             }
     68             transformationAmount = mapToDuration(transformationAmount);
     69             CrossFadeHelper.fadeIn(mTransformedView, transformationAmount, false /* remap */);
     70             transformationAmount = Interpolators.LINEAR_OUT_SLOW_IN.getInterpolation(
     71                     transformationAmount);
     72             mTransformedView.setScaleX(transformationAmount);
     73             mTransformedView.setScaleY(transformationAmount);
     74         } else {
     75             super.appear(transformationAmount, otherView);
     76         }
     77     }
     78 
     79     @Override
     80     public void disappear(float transformationAmount, TransformableView otherView) {
     81         if (otherView instanceof HybridNotificationView) {
     82             if (transformationAmount == 0.0f) {
     83                 mTransformedView.setPivotY(0);
     84                 mTransformedView.setPivotX(mTransformedView.getWidth() / 2);
     85             }
     86             transformationAmount = mapToDuration(1.0f - transformationAmount);
     87             CrossFadeHelper.fadeOut(mTransformedView, 1.0f - transformationAmount,
     88                     false /* remap */);
     89             transformationAmount = Interpolators.LINEAR_OUT_SLOW_IN.getInterpolation(
     90                     transformationAmount);
     91             mTransformedView.setScaleX(transformationAmount);
     92             mTransformedView.setScaleY(transformationAmount);
     93         } else {
     94             super.disappear(transformationAmount, otherView);
     95         }
     96     }
     97 
     98     private static float mapToDuration(float scaleAmount) {
     99         // Assuming a linear interpolator, we can easily map it to our new duration
    100         scaleAmount = (scaleAmount * StackStateAnimator.ANIMATION_DURATION_STANDARD
    101                 - (StackStateAnimator.ANIMATION_DURATION_STANDARD - ANIMATION_DURATION_LENGTH))
    102                         / ANIMATION_DURATION_LENGTH;
    103         return Math.max(Math.min(scaleAmount, 1.0f), 0.0f);
    104     }
    105 
    106     public Icon getIcon() {
    107         return mIcon;
    108     }
    109 
    110     public static ImageTransformState obtain() {
    111         ImageTransformState instance = sInstancePool.acquire();
    112         if (instance != null) {
    113             return instance;
    114         }
    115         return new ImageTransformState();
    116     }
    117 
    118     @Override
    119     protected boolean transformScale(TransformState otherState) {
    120         return true;
    121     }
    122 
    123     @Override
    124     public void recycle() {
    125         super.recycle();
    126         sInstancePool.release(this);
    127     }
    128 
    129     @Override
    130     protected void reset() {
    131         super.reset();
    132         mIcon = null;
    133     }
    134 }
    135