1 /*
2  * Copyright (C) 2020 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.internal.widget;
18 
19 import android.annotation.ColorInt;
20 import android.app.Notification;
21 import android.content.Context;
22 import android.content.res.Configuration;
23 import android.graphics.PorterDuff;
24 import android.graphics.drawable.Drawable;
25 
26 import com.android.internal.util.ContrastColorUtil;
27 
28 /** Helpers for colored icons */
29 final class ColoredIconHelper {
30 
31     @ColorInt
32     static final int COLOR_INVALID = Notification.COLOR_INVALID;
33 
ColoredIconHelper()34     private ColoredIconHelper() {
35     }
36 
37     /**
38      * Apply a gray tint or the original color to a drawable, accounting for the night mode in
39      * selecting the gray.
40      */
applyGrayTint(Context ctx, Drawable drawable, boolean apply, int originalColor)41     static void applyGrayTint(Context ctx, Drawable drawable, boolean apply, int originalColor) {
42         if (originalColor == COLOR_INVALID) {
43             return;
44         }
45         if (apply) {
46             // lets gray it out
47             Configuration config = ctx.getResources().getConfiguration();
48             boolean inNightMode = (config.uiMode & Configuration.UI_MODE_NIGHT_MASK)
49                     == Configuration.UI_MODE_NIGHT_YES;
50             int grey = ContrastColorUtil.resolveColor(ctx, Notification.COLOR_DEFAULT, inNightMode);
51             drawable.mutate().setColorFilter(grey, PorterDuff.Mode.SRC_ATOP);
52         } else {
53             // lets reset it
54             drawable.mutate().setColorFilter(originalColor, PorterDuff.Mode.SRC_ATOP);
55         }
56     }
57 }
58