Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 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 | |
Selim Cinek | 8a71ad0 | 2016-09-07 20:00:25 -0700 | [diff] [blame] | 24 | import com.android.systemui.Interpolators; |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 25 | import com.android.systemui.R; |
Selim Cinek | 8a71ad0 | 2016-09-07 20:00:25 -0700 | [diff] [blame] | 26 | import com.android.systemui.statusbar.CrossFadeHelper; |
| 27 | import com.android.systemui.statusbar.TransformableView; |
| 28 | import com.android.systemui.statusbar.stack.StackStateAnimator; |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * A transform state of a image view. |
| 32 | */ |
| 33 | public class ImageTransformState extends TransformState { |
Selim Cinek | 8a71ad0 | 2016-09-07 20:00:25 -0700 | [diff] [blame] | 34 | public static final long ANIMATION_DURATION_LENGTH = 210; |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 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) { |
Selim Cinek | 5d6ef8d | 2017-05-18 22:16:00 -0700 | [diff] [blame] | 51 | if (super.sameAs(otherState)) { |
| 52 | return true; |
| 53 | } |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 54 | if (otherState instanceof ImageTransformState) { |
Selim Cinek | 646d205 | 2016-01-03 14:42:02 +0800 | [diff] [blame] | 55 | return mIcon != null && mIcon.sameAs(((ImageTransformState) otherState).getIcon()); |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 56 | } |
Selim Cinek | 5d6ef8d | 2017-05-18 22:16:00 -0700 | [diff] [blame] | 57 | return false; |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Selim Cinek | 8a71ad0 | 2016-09-07 20:00:25 -0700 | [diff] [blame] | 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 | |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 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 |
Selim Cinek | 317effc | 2017-06-29 17:21:31 +0200 | [diff] [blame] | 119 | protected boolean transformScale(TransformState otherState) { |
Selim Cinek | 3bdbf28 | 2016-01-14 17:04:47 -0800 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | |
| 123 | @Override |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 124 | public void recycle() { |
Selim Cinek | 0ffbda6 | 2016-01-01 20:29:12 +0100 | [diff] [blame] | 125 | super.recycle(); |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 126 | sInstancePool.release(this); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | protected void reset() { |
| 131 | super.reset(); |
| 132 | mIcon = null; |
| 133 | } |
| 134 | } |