blob: ef042bab76258bc1f418495d422c3d218db136fc [file] [log] [blame]
Adrian Roos7bcf6d32017-04-04 16:44:25 -07001/*
2 * Copyright (C) 2017 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.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ValueAnimator;
22import android.graphics.ColorMatrix;
23import android.graphics.ColorMatrixColorFilter;
Selim Cinek653c1392018-05-15 13:02:42 -070024import android.view.View;
Adrian Roos7bcf6d32017-04-04 16:44:25 -070025import android.widget.ImageView;
26
27import com.android.systemui.Interpolators;
Selim Cinek653c1392018-05-15 13:02:42 -070028import com.android.systemui.R;
Lucas Dupin1a8b33d2018-11-12 18:18:15 -080029import com.android.systemui.statusbar.notification.stack.StackStateAnimator;
Adrian Roos7bcf6d32017-04-04 16:44:25 -070030
Adrian Roos456e0052017-04-04 16:44:29 -070031import java.util.function.Consumer;
32
Adrian Roos7bcf6d32017-04-04 16:44:25 -070033public class NotificationDozeHelper {
Selim Cinek653c1392018-05-15 13:02:42 -070034 private static final int DOZE_ANIMATOR_TAG = R.id.doze_intensity_tag;
Adrian Roos7bcf6d32017-04-04 16:44:25 -070035 private final ColorMatrix mGrayscaleColorMatrix = new ColorMatrix();
36
37 public void fadeGrayscale(final ImageView target, final boolean dark, long delay) {
38 startIntensityAnimation(new ValueAnimator.AnimatorUpdateListener() {
39 @Override
40 public void onAnimationUpdate(ValueAnimator animation) {
Adrian Roosf3eacd32017-04-14 16:35:58 -070041 updateGrayscale(target, (float) animation.getAnimatedValue());
Adrian Roos7bcf6d32017-04-04 16:44:25 -070042 }
43 }, dark, delay, new AnimatorListenerAdapter() {
44 @Override
45 public void onAnimationEnd(Animator animation) {
46 if (!dark) {
47 target.setColorFilter(null);
48 }
49 }
50 });
51 }
52
53 public void updateGrayscale(ImageView target, boolean dark) {
Adrian Roosf3eacd32017-04-14 16:35:58 -070054 updateGrayscale(target, dark ? 1 : 0);
55 }
56
57 public void updateGrayscale(ImageView target, float darkAmount) {
58 if (darkAmount > 0) {
59 updateGrayscaleMatrix(darkAmount);
Adrian Roos7bcf6d32017-04-04 16:44:25 -070060 target.setColorFilter(new ColorMatrixColorFilter(mGrayscaleColorMatrix));
61 } else {
62 target.setColorFilter(null);
63 }
64 }
65
Lucas Dupin1a8b33d2018-11-12 18:18:15 -080066 // TODO: this should be using StatusBarStateController#getDozeAmount
Adrian Roos7bcf6d32017-04-04 16:44:25 -070067 public void startIntensityAnimation(ValueAnimator.AnimatorUpdateListener updateListener,
68 boolean dark, long delay, Animator.AnimatorListener listener) {
69 float startIntensity = dark ? 0f : 1f;
70 float endIntensity = dark ? 1f : 0f;
71 ValueAnimator animator = ValueAnimator.ofFloat(startIntensity, endIntensity);
72 animator.addUpdateListener(updateListener);
Lucas Dupin1a8b33d2018-11-12 18:18:15 -080073 animator.setDuration(StackStateAnimator.ANIMATION_DURATION_WAKEUP);
Adrian Roos7bcf6d32017-04-04 16:44:25 -070074 animator.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN);
75 animator.setStartDelay(delay);
76 if (listener != null) {
77 animator.addListener(listener);
78 }
79 animator.start();
80 }
81
Adrian Roos456e0052017-04-04 16:44:29 -070082 public void setIntensityDark(Consumer<Float> listener, boolean dark,
Selim Cinek653c1392018-05-15 13:02:42 -070083 boolean animate, long delay, View view) {
Adrian Roos456e0052017-04-04 16:44:29 -070084 if (animate) {
85 startIntensityAnimation(a -> listener.accept((Float) a.getAnimatedValue()), dark, delay,
Selim Cinek653c1392018-05-15 13:02:42 -070086 new AnimatorListenerAdapter() {
87
88 @Override
89 public void onAnimationEnd(Animator animation) {
90 view.setTag(DOZE_ANIMATOR_TAG, null);
91 }
92
93 @Override
94 public void onAnimationStart(Animator animation) {
95 view.setTag(DOZE_ANIMATOR_TAG, animation);
96 }
97 } /* listener */);
Adrian Roos456e0052017-04-04 16:44:29 -070098 } else {
Selim Cinek653c1392018-05-15 13:02:42 -070099 Animator animator = (Animator) view.getTag(DOZE_ANIMATOR_TAG);
100 if (animator != null) {
101 animator.cancel();
102 }
Adrian Roos456e0052017-04-04 16:44:29 -0700103 listener.accept(dark ? 1f : 0f);
104 }
105 }
106
Adrian Roos7bcf6d32017-04-04 16:44:25 -0700107 public void updateGrayscaleMatrix(float intensity) {
108 mGrayscaleColorMatrix.setSaturation(1 - intensity);
109 }
110
111 public ColorMatrix getGrayscaleColorMatrix() {
112 return mGrayscaleColorMatrix;
113 }
114}