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.systemui.Interpolators;
28 import com.android.systemui.R;
29 import com.android.systemui.statusbar.phone.NotificationPanelView;
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 
startIntensityAnimation(ValueAnimator.AnimatorUpdateListener updateListener, boolean dark, long delay, Animator.AnimatorListener listener)66     public void startIntensityAnimation(ValueAnimator.AnimatorUpdateListener updateListener,
67             boolean dark, long delay, Animator.AnimatorListener listener) {
68         float startIntensity = dark ? 0f : 1f;
69         float endIntensity = dark ? 1f : 0f;
70         ValueAnimator animator = ValueAnimator.ofFloat(startIntensity, endIntensity);
71         animator.addUpdateListener(updateListener);
72         animator.setDuration(NotificationPanelView.DOZE_ANIMATION_DURATION);
73         animator.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN);
74         animator.setStartDelay(delay);
75         if (listener != null) {
76             animator.addListener(listener);
77         }
78         animator.start();
79     }
80 
setIntensityDark(Consumer<Float> listener, boolean dark, boolean animate, long delay, View view)81     public void setIntensityDark(Consumer<Float> listener, boolean dark,
82             boolean animate, long delay, View view) {
83         if (animate) {
84             startIntensityAnimation(a -> listener.accept((Float) a.getAnimatedValue()), dark, delay,
85                     new AnimatorListenerAdapter() {
86 
87                         @Override
88                         public void onAnimationEnd(Animator animation) {
89                             view.setTag(DOZE_ANIMATOR_TAG, null);
90                         }
91 
92                         @Override
93                         public void onAnimationStart(Animator animation) {
94                             view.setTag(DOZE_ANIMATOR_TAG, animation);
95                         }
96                     } /* listener */);
97         } else {
98             Animator animator = (Animator) view.getTag(DOZE_ANIMATOR_TAG);
99             if (animator != null) {
100                 animator.cancel();
101             }
102             listener.accept(dark ? 1f : 0f);
103         }
104     }
105 
updateGrayscaleMatrix(float intensity)106     public void updateGrayscaleMatrix(float intensity) {
107         mGrayscaleColorMatrix.setSaturation(1 - intensity);
108     }
109 
getGrayscaleColorMatrix()110     public ColorMatrix getGrayscaleColorMatrix() {
111         return mGrayscaleColorMatrix;
112     }
113 }
114