1 /*
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 
17 package com.android.systemui.statusbar.notification;
18 
19 import android.animation.Animator;
20 import android.animation.AnimatorListenerAdapter;
21 import android.animation.ValueAnimator;
22 import android.graphics.ColorMatrix;
23 import android.graphics.ColorMatrixColorFilter;
24 import android.view.View;
25 import android.widget.ImageView;
26 
27 import com.android.app.animation.Interpolators;
28 import com.android.systemui.R;
29 import com.android.systemui.statusbar.notification.stack.StackStateAnimator;
30 
31 import java.util.function.Consumer;
32 
33 public class NotificationDozeHelper {
34     private static final int DOZE_ANIMATOR_TAG = R.id.doze_intensity_tag;
35     private final ColorMatrix mGrayscaleColorMatrix = new ColorMatrix();
36 
fadeGrayscale(final ImageView target, final boolean dark, long delay)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) {
41                 updateGrayscale(target, (float) animation.getAnimatedValue());
42             }
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 
updateGrayscale(ImageView target, boolean dark)53     public void updateGrayscale(ImageView target, boolean dark) {
54         updateGrayscale(target, dark ? 1 : 0);
55     }
56 
updateGrayscale(ImageView target, float darkAmount)57     public void updateGrayscale(ImageView target, float darkAmount) {
58         if (darkAmount > 0) {
59             updateGrayscaleMatrix(darkAmount);
60             target.setColorFilter(new ColorMatrixColorFilter(mGrayscaleColorMatrix));
61         } else {
62             target.setColorFilter(null);
63         }
64     }
65 
66     // TODO: this should be using StatusBarStateController#getDozeAmount
startIntensityAnimation(ValueAnimator.AnimatorUpdateListener updateListener, boolean dark, long delay, Animator.AnimatorListener listener)67     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);
73         animator.setDuration(StackStateAnimator.ANIMATION_DURATION_WAKEUP);
74         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 
setDozing(Consumer<Float> listener, boolean dozing, boolean animate, long delay, View view)82     public void setDozing(Consumer<Float> listener, boolean dozing,
83             boolean animate, long delay, View view) {
84         if (animate) {
85             startIntensityAnimation(a -> listener.accept((Float) a.getAnimatedValue()), dozing,
86                     delay,
87                     new AnimatorListenerAdapter() {
88 
89                         @Override
90                         public void onAnimationEnd(Animator animation) {
91                             view.setTag(DOZE_ANIMATOR_TAG, null);
92                         }
93 
94                         @Override
95                         public void onAnimationStart(Animator animation) {
96                             view.setTag(DOZE_ANIMATOR_TAG, animation);
97                         }
98                     } /* listener */);
99         } else {
100             Animator animator = (Animator) view.getTag(DOZE_ANIMATOR_TAG);
101             if (animator != null) {
102                 animator.cancel();
103             }
104             listener.accept(dozing ? 1f : 0f);
105         }
106     }
107 
updateGrayscaleMatrix(float intensity)108     public void updateGrayscaleMatrix(float intensity) {
109         mGrayscaleColorMatrix.setSaturation(1 - intensity);
110     }
111 
getGrayscaleColorMatrix()112     public ColorMatrix getGrayscaleColorMatrix() {
113         return mGrayscaleColorMatrix;
114     }
115 }
116