• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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 
setIntensityDark(Consumer<Float> listener, boolean dark, boolean animate, long delay, View view)82     public void setIntensityDark(Consumer<Float> listener, boolean dark,
83             boolean animate, long delay, View view) {
84         if (animate) {
85             startIntensityAnimation(a -> listener.accept((Float) a.getAnimatedValue()), dark, delay,
86                     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 */);
98         } else {
99             Animator animator = (Animator) view.getTag(DOZE_ANIMATOR_TAG);
100             if (animator != null) {
101                 animator.cancel();
102             }
103             listener.accept(dark ? 1f : 0f);
104         }
105     }
106 
updateGrayscaleMatrix(float intensity)107     public void updateGrayscaleMatrix(float intensity) {
108         mGrayscaleColorMatrix.setSaturation(1 - intensity);
109     }
110 
getGrayscaleColorMatrix()111     public ColorMatrix getGrayscaleColorMatrix() {
112         return mGrayscaleColorMatrix;
113     }
114 }
115