Home | History | Annotate | Download | only in policy
      1 /*
      2  * Copyright (C) 2018 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.policy;
     18 
     19 import android.annotation.ColorInt;
     20 import android.graphics.Color;
     21 import android.graphics.drawable.Drawable;
     22 
     23 import com.android.internal.graphics.ColorUtils;
     24 import com.android.systemui.statusbar.phone.ShadowKeyDrawable;
     25 
     26 /**
     27  * Drawable for {@link KeyButtonView}s which contains a single asset and colors for light and dark
     28  * navigation bar mode.
     29  */
     30 public class TintedKeyButtonDrawable extends KeyButtonDrawable {
     31 
     32     private final int mLightColor;
     33     private final int mDarkColor;
     34 
     35     public static final float DARK_INTENSITY_NOT_SET = -1f;
     36     private float mDarkIntensity = DARK_INTENSITY_NOT_SET;
     37 
     38     public static TintedKeyButtonDrawable create(Drawable drawable, @ColorInt int lightColor,
     39             @ColorInt int darkColor) {
     40         return new TintedKeyButtonDrawable(new Drawable[] { drawable },
     41                 lightColor, darkColor);
     42     }
     43 
     44     private TintedKeyButtonDrawable(Drawable[] drawables, int lightColor, int darkColor){
     45         super(drawables);
     46         mLightColor = lightColor;
     47         mDarkColor = darkColor;
     48         setDarkIntensity(0f); // Set initial coloration
     49     }
     50 
     51     @Override
     52     public void setDarkIntensity(float intensity) {
     53         // Duplicate intensity scaling from KeyButtonDrawable
     54         mDarkIntensity = intensity;
     55 
     56         // Dark and light colors may have an alpha component
     57         final int intermediateColor = ColorUtils.compositeColors(
     58                 blendAlpha(mDarkColor, intensity),
     59                 blendAlpha(mLightColor, (1f - intensity)));
     60 
     61         getDrawable(0).setTint(intermediateColor);
     62         invalidateSelf();
     63     }
     64 
     65     private int blendAlpha(int color, float alpha) {
     66         final float newAlpha = alpha < 0f ? 0f : (alpha > 1f ? 1f : alpha);
     67         final float colorAlpha = Color.alpha(color) / 255f;
     68         final int alphaInt = (int) (255 * newAlpha * colorAlpha); // Blend by multiplying
     69         // Ensure alpha is clamped [0-255] or ColorUtils will crash
     70         return ColorUtils.setAlphaComponent(color, alphaInt);
     71     }
     72 
     73     public boolean isDarkIntensitySet() {
     74         return mDarkIntensity != DARK_INTENSITY_NOT_SET;
     75     }
     76 
     77     public float getDarkIntensity() {
     78         return mDarkIntensity;
     79     }
     80 }
     81