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.annotation.Nullable;
20 import android.content.Context;
21 import android.graphics.Color;
22 import android.graphics.PorterDuff.Mode;
23 import android.graphics.PorterDuffColorFilter;
24 import android.graphics.drawable.Drawable;
25 import android.widget.ImageView;
26 
27 import com.android.systemui.R;
28 
29 public class NotificationIconDozeHelper extends NotificationDozeHelper {
30 
31     private final int mImageDarkAlpha;
32     private final int mImageDarkColor = 0xffffffff;
33 
34     @Nullable
35     private PorterDuffColorFilter mImageColorFilter = null;
36 
37     private int mColor = Color.BLACK;
38 
NotificationIconDozeHelper(Context ctx)39     public NotificationIconDozeHelper(Context ctx) {
40         mImageDarkAlpha = ctx.getResources().getInteger(R.integer.doze_small_icon_alpha);
41     }
42 
setColor(int color)43     public void setColor(int color) {
44         mColor = color;
45     }
46 
setImageDark(ImageView target, boolean dark, boolean fade, long delay, boolean useGrayscale)47     public void setImageDark(ImageView target, boolean dark, boolean fade, long delay,
48             boolean useGrayscale) {
49         if (fade) {
50             if (!useGrayscale) {
51                 fadeImageColorFilter(target, dark, delay);
52                 fadeImageAlpha(target, dark, delay);
53             } else {
54                 fadeGrayscale(target, dark, delay);
55             }
56         } else {
57             if (!useGrayscale) {
58                 updateImageColorFilter(target, dark);
59                 updateImageAlpha(target, dark);
60             } else {
61                 updateGrayscale(target, dark);
62             }
63         }
64     }
65 
fadeImageColorFilter(final ImageView target, boolean dark, long delay)66     private void fadeImageColorFilter(final ImageView target, boolean dark, long delay) {
67         startIntensityAnimation(animation -> {
68             updateImageColorFilter(target, (Float) animation.getAnimatedValue());
69         }, dark, delay, null /* listener */);
70     }
71 
fadeImageAlpha(final ImageView target, boolean dark, long delay)72     private void fadeImageAlpha(final ImageView target, boolean dark, long delay) {
73         startIntensityAnimation(animation -> {
74             float t = (float) animation.getAnimatedValue();
75             target.setImageAlpha((int) (255 * (1f - t) + mImageDarkAlpha * t));
76         }, dark, delay, null /* listener */);
77     }
78 
updateImageColorFilter(ImageView target, boolean dark)79     private void updateImageColorFilter(ImageView target, boolean dark) {
80         updateImageColorFilter(target, dark ? 1f : 0f);
81     }
82 
updateImageColorFilter(ImageView target, float intensity)83     private void updateImageColorFilter(ImageView target, float intensity) {
84         int color = NotificationUtils.interpolateColors(mColor, mImageDarkColor, intensity);
85         if (mImageColorFilter == null || mImageColorFilter.getColor() != color) {
86             mImageColorFilter = new PorterDuffColorFilter(color, Mode.SRC_ATOP);
87         }
88         Drawable imageDrawable = target.getDrawable();
89 
90         // Also, the notification might have been modified during the animation, so background
91         // might be null here.
92         if (imageDrawable != null) {
93             Drawable d = imageDrawable.mutate();
94             // DrawableContainer ignores the color filter if it's already set, so clear it first to
95             // get it set and invalidated properly.
96             d.setColorFilter(null);
97             d.setColorFilter(mImageColorFilter);
98         }
99     }
100 
updateImageAlpha(ImageView target, boolean dark)101     private void updateImageAlpha(ImageView target, boolean dark) {
102         target.setImageAlpha(dark ? mImageDarkAlpha : 255);
103     }
104 
105 }
106