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