aboutsummaryrefslogtreecommitdiff
path: root/bridge/src/android/graphics/drawable/AdaptiveIconDrawable_Delegate.java
blob: 3c090e0e5a063f18dfbd899c0356049610732229 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.graphics.drawable;

import com.android.internal.R;
import com.android.layoutlib.bridge.android.BridgeContext;
import com.android.tools.layoutlib.annotations.LayoutlibDelegate;

import android.content.res.Resources;
import android.content.res.Resources_Delegate;
import android.graphics.Canvas;

public class AdaptiveIconDrawable_Delegate {
    public static String sPath;

    /**
     * Delegate that replaces a call to Resources.getString in
     * the constructor of AdaptiveIconDrawable.
     * This allows to pass a non-default value for the mask for adaptive icons.
     */
    @SuppressWarnings("unused")
    public static String getResourceString(Resources res, int resId) {
        if (resId == R.string.config_icon_mask) {
            return sPath;
        }
        return res.getString(resId);
    }

    @LayoutlibDelegate
    public static void draw(AdaptiveIconDrawable thisDrawable, Canvas canvas) {
        Resources res = Resources.getSystem();
        BridgeContext context = Resources_Delegate.getContext(res);
        if (context.useThemedIcon() && thisDrawable.getMonochrome() != null) {
            AdaptiveIconDrawable themedIcon =
                    createThemedVersionFromMonochrome(thisDrawable.getMonochrome(), res);
            themedIcon.onBoundsChange(thisDrawable.getBounds());
            themedIcon.draw_Original(canvas);
        } else {
            thisDrawable.draw_Original(canvas);
        }
    }

    /**
     * This builds the themed version of {@link AdaptiveIconDrawable}, copying what the
     * framework does in {@link com.android.launcher3.Utilities#getFullDrawable}
     */
    private static AdaptiveIconDrawable createThemedVersionFromMonochrome(Drawable mono,
            Resources resources) {
        mono = mono.mutate();
        int[] colors = getColors(resources);
        mono.setTint(colors[1]);
        return new AdaptiveIconDrawable(new ColorDrawable(colors[0]), mono);
    }

    private static int[] getColors(Resources resources) {
        int[] colors = new int[2];
        if (resources.getConfiguration().isNightModeActive()) {
            colors[0] = resources.getColor(android.R.color.system_neutral1_800, null);
            colors[1] = resources.getColor(android.R.color.system_accent1_100, null);
        } else {
            colors[0] = resources.getColor(android.R.color.system_accent1_100, null);
            colors[1] = resources.getColor(android.R.color.system_neutral2_700, null);
        }
        return colors;
    }
}