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; 18 19 import android.content.Context; 20 import android.content.res.Resources.Theme; 21 import android.graphics.Canvas; 22 import android.graphics.ColorFilter; 23 import android.graphics.Rect; 24 import android.graphics.drawable.Drawable; 25 import android.graphics.drawable.LayerDrawable; 26 import android.view.ContextThemeWrapper; 27 import android.view.Gravity; 28 import com.android.settingslib.Utils; 29 import com.android.systemui.R; 30 31 /** 32 * NeutralGoodDrawable implements a drawable that will load 2 underlying drawable resources, one 33 * with each the DualToneDarkTheme and DualToneLightTheme, choosing which one based on what 34 * DarkIconDispatcher tells us about darkness 35 */ 36 public class NeutralGoodDrawable extends LayerDrawable { 37 create(Context context, int resId)38 public static NeutralGoodDrawable create(Context context, int resId) { 39 int dualToneLightTheme = Utils.getThemeAttr(context, R.attr.lightIconTheme); 40 int dualToneDarkTheme = Utils.getThemeAttr(context, R.attr.darkIconTheme); 41 ContextThemeWrapper light = new ContextThemeWrapper(context, dualToneLightTheme); 42 ContextThemeWrapper dark = new ContextThemeWrapper(context, dualToneDarkTheme); 43 44 return create(light, dark, resId); 45 } 46 47 /** 48 * For the on-the-go young entrepreneurial who wants to cache contexts 49 * @param light - a context using the R.attr.lightIconTheme 50 * @param dark - a context using the R.attr.darkIconTheme 51 * @param resId - the resId for our drawable 52 */ create(Context light, Context dark, int resId)53 public static NeutralGoodDrawable create(Context light, Context dark, int resId) { 54 return new NeutralGoodDrawable( 55 new Drawable[] { 56 light.getDrawable(resId).mutate(), 57 dark.getDrawable(resId).mutate() }); 58 } 59 NeutralGoodDrawable(Drawable []drawables)60 protected NeutralGoodDrawable(Drawable []drawables) { 61 super(drawables); 62 63 for (int i = 0; i < drawables.length; i++) { 64 setLayerGravity(i, Gravity.CENTER); 65 } 66 67 mutate(); 68 setDarkIntensity(0); 69 } 70 setDarkIntensity(float intensity)71 public void setDarkIntensity(float intensity) { 72 73 getDrawable(0).setAlpha((int) ((1 - intensity) * 255f)); 74 getDrawable(1).setAlpha((int) (intensity * 255f)); 75 76 invalidateSelf(); 77 } 78 } 79