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 android.graphics.drawable;
18 
19 import com.android.internal.R;
20 import com.android.layoutlib.bridge.android.BridgeContext;
21 import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
22 
23 import android.content.res.Resources;
24 import android.content.res.Resources_Delegate;
25 import android.graphics.Canvas;
26 
27 public class AdaptiveIconDrawable_Delegate {
28     public static String sPath;
29 
30     /**
31      * Delegate that replaces a call to Resources.getString in
32      * the constructor of AdaptiveIconDrawable.
33      * This allows to pass a non-default value for the mask for adaptive icons.
34      */
35     @SuppressWarnings("unused")
getResourceString(Resources res, int resId)36     public static String getResourceString(Resources res, int resId) {
37         if (resId == R.string.config_icon_mask) {
38             return sPath;
39         }
40         return res.getString(resId);
41     }
42 
43     @LayoutlibDelegate
draw(AdaptiveIconDrawable thisDrawable, Canvas canvas)44     public static void draw(AdaptiveIconDrawable thisDrawable, Canvas canvas) {
45         Resources res = Resources.getSystem();
46         BridgeContext context = Resources_Delegate.getContext(res);
47         if (context.useThemedIcon() && thisDrawable.getMonochrome() != null) {
48             AdaptiveIconDrawable themedIcon =
49                     createThemedVersionFromMonochrome(thisDrawable.getMonochrome(), res);
50             themedIcon.onBoundsChange(thisDrawable.getBounds());
51             themedIcon.draw_Original(canvas);
52         } else {
53             thisDrawable.draw_Original(canvas);
54         }
55     }
56 
57     /**
58      * This builds the themed version of {@link AdaptiveIconDrawable}, copying what the
59      * framework does in {@link com.android.launcher3.Utilities#getFullDrawable}
60      */
createThemedVersionFromMonochrome(Drawable mono, Resources resources)61     private static AdaptiveIconDrawable createThemedVersionFromMonochrome(Drawable mono,
62             Resources resources) {
63         mono = mono.mutate();
64         int[] colors = getColors(resources);
65         mono.setTint(colors[1]);
66         return new AdaptiveIconDrawable(new ColorDrawable(colors[0]), mono);
67     }
68 
getColors(Resources resources)69     private static int[] getColors(Resources resources) {
70         int[] colors = new int[2];
71         if (resources.getConfiguration().isNightModeActive()) {
72             colors[0] = resources.getColor(android.R.color.system_neutral1_800, null);
73             colors[1] = resources.getColor(android.R.color.system_accent1_100, null);
74         } else {
75             colors[0] = resources.getColor(android.R.color.system_accent1_100, null);
76             colors[1] = resources.getColor(android.R.color.system_neutral2_700, null);
77         }
78         return colors;
79     }
80 }
81