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.stack; 18 19 import android.animation.Animator; 20 import android.animation.AnimatorListenerAdapter; 21 import android.util.ArrayMap; 22 import android.util.Property; 23 import android.view.View; 24 import android.view.animation.Interpolator; 25 26 import java.util.function.Consumer; 27 28 /** 29 * Properties for a View animation 30 */ 31 public class AnimationProperties { 32 public long duration; 33 public long delay; 34 private ArrayMap<Property, Interpolator> mInterpolatorMap; 35 private Consumer<Property> mAnimationEndAction; 36 37 /** 38 * @return an animation filter for this animation. 39 */ getAnimationFilter()40 public AnimationFilter getAnimationFilter() { 41 return new AnimationFilter() { 42 @Override 43 public boolean shouldAnimateProperty(Property property) { 44 return true; 45 } 46 }; 47 } 48 49 /** 50 * @return a listener that will be added for a given property during its animation. 51 */ 52 public AnimatorListenerAdapter getAnimationFinishListener(Property property) { 53 if (mAnimationEndAction == null) { 54 return null; 55 } 56 Consumer<Property> endAction = mAnimationEndAction; 57 return new AnimatorListenerAdapter() { 58 private boolean mCancelled; 59 60 @Override 61 public void onAnimationCancel(Animator animation) { 62 mCancelled = true; 63 } 64 65 @Override 66 public void onAnimationEnd(Animator animation) { 67 if (!mCancelled) { 68 endAction.accept(property); 69 } 70 } 71 }; 72 } 73 74 public AnimationProperties setAnimationEndAction(Consumer<Property> listener) { 75 mAnimationEndAction = listener; 76 return this; 77 } 78 79 public boolean wasAdded(View view) { 80 return false; 81 } 82 83 /** 84 * Get a custom interpolator for a property instead of the normal one. 85 */ 86 public Interpolator getCustomInterpolator(View child, Property property) { 87 return mInterpolatorMap != null ? mInterpolatorMap.get(property) : null; 88 } 89 90 91 public void combineCustomInterpolators(AnimationProperties iconAnimationProperties) { 92 ArrayMap<Property, Interpolator> map = iconAnimationProperties.mInterpolatorMap; 93 if (map != null) { 94 if (mInterpolatorMap == null) { 95 mInterpolatorMap = new ArrayMap<>(); 96 } 97 mInterpolatorMap.putAll(map); 98 } 99 } 100 101 /** 102 * Set a custom interpolator to use for all views for a property. 103 */ 104 public AnimationProperties setCustomInterpolator(Property property, Interpolator interpolator) { 105 if (mInterpolatorMap == null) { 106 mInterpolatorMap = new ArrayMap<>(); 107 } 108 mInterpolatorMap.put(property, interpolator); 109 return this; 110 } 111 112 public AnimationProperties setDuration(long duration) { 113 this.duration = duration; 114 return this; 115 } 116 117 public AnimationProperties setDelay(long delay) { 118 this.delay = delay; 119 return this; 120 } 121 122 public AnimationProperties resetCustomInterpolators() { 123 mInterpolatorMap = null; 124 return this; 125 } 126 } 127