blob: 92f26d65f25eb4920098ed02a8d0cf3a6c3b2093 [file] [log] [blame]
Selim Cinek4ffd6362015-12-29 15:12:23 +01001/*
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
17package com.android.systemui.statusbar.notification;
18
19import android.graphics.drawable.Icon;
20import android.util.Pools;
21import android.view.View;
22import android.widget.ImageView;
23
Selim Cinek8a71ad02016-09-07 20:00:25 -070024import com.android.systemui.Interpolators;
Selim Cinek4ffd6362015-12-29 15:12:23 +010025import com.android.systemui.R;
Selim Cinek8a71ad02016-09-07 20:00:25 -070026import com.android.systemui.statusbar.CrossFadeHelper;
27import com.android.systemui.statusbar.TransformableView;
28import com.android.systemui.statusbar.stack.StackStateAnimator;
Selim Cinek4ffd6362015-12-29 15:12:23 +010029
30/**
31 * A transform state of a image view.
32*/
33public class ImageTransformState extends TransformState {
Selim Cinek8a71ad02016-09-07 20:00:25 -070034 public static final long ANIMATION_DURATION_LENGTH = 210;
Selim Cinek4ffd6362015-12-29 15:12:23 +010035
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 Cinek5d6ef8d2017-05-18 22:16:00 -070051 if (super.sameAs(otherState)) {
52 return true;
53 }
Selim Cinek4ffd6362015-12-29 15:12:23 +010054 if (otherState instanceof ImageTransformState) {
Selim Cinek646d2052016-01-03 14:42:02 +080055 return mIcon != null && mIcon.sameAs(((ImageTransformState) otherState).getIcon());
Selim Cinek4ffd6362015-12-29 15:12:23 +010056 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -070057 return false;
Selim Cinek4ffd6362015-12-29 15:12:23 +010058 }
59
Selim Cinek8a71ad02016-09-07 20:00:25 -070060 @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 Cinek4ffd6362015-12-29 15:12:23 +0100106 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 Cinek317effc2017-06-29 17:21:31 +0200119 protected boolean transformScale(TransformState otherState) {
Selim Cinek3bdbf282016-01-14 17:04:47 -0800120 return true;
121 }
122
123 @Override
Selim Cinek4ffd6362015-12-29 15:12:23 +0100124 public void recycle() {
Selim Cinek0ffbda62016-01-01 20:29:12 +0100125 super.recycle();
Selim Cinek4ffd6362015-12-29 15:12:23 +0100126 sInstancePool.release(this);
127 }
128
129 @Override
130 protected void reset() {
131 super.reset();
132 mIcon = null;
133 }
134}