blob: 56f6726fae5be9f0c7a32de9ef38e7cd19c8d8d8 [file] [log] [blame]
Mike Digman7d092772018-01-11 12:10:32 -08001/*
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
17package com.android.systemui.statusbar.policy;
18
19import android.annotation.ColorInt;
20import android.graphics.drawable.Drawable;
21
22import com.android.internal.graphics.ColorUtils;
23
24/**
25 * Drawable for {@link KeyButtonView}s which contains a single asset and colors for light and dark
26 * navigation bar mode.
27 */
28public class TintedKeyButtonDrawable extends KeyButtonDrawable {
29
30 private final int mLightColor;
31 private final int mDarkColor;
32
Mike Digmanc8217582018-03-06 13:08:33 -080033 public static final float DARK_INTENSITY_NOT_SET = -1f;
34 private float mDarkIntensity = DARK_INTENSITY_NOT_SET;
35
Mike Digman7d092772018-01-11 12:10:32 -080036 public static TintedKeyButtonDrawable create(Drawable drawable, @ColorInt int lightColor,
37 @ColorInt int darkColor) {
38 return new TintedKeyButtonDrawable(new Drawable[] { drawable }, lightColor, darkColor);
39 }
40
41 private TintedKeyButtonDrawable(Drawable[] drawables, int lightColor, int darkColor){
42 super(drawables);
43 mLightColor = lightColor;
44 mDarkColor = darkColor;
Mike Digmanc8217582018-03-06 13:08:33 -080045 setDarkIntensity(0f); // Set initial coloration
Mike Digman7d092772018-01-11 12:10:32 -080046 }
47
48 @Override
49 public void setDarkIntensity(float intensity) {
50 // Duplicate intensity scaling from KeyButtonDrawable
Mike Digmanc8217582018-03-06 13:08:33 -080051 mDarkIntensity = intensity;
Mike Digman7d092772018-01-11 12:10:32 -080052 int intermediateColor = ColorUtils.compositeColors(
53 setAlphaFloat(mDarkColor, intensity),
54 setAlphaFloat(mLightColor,1f - intensity));
55 getDrawable(0).setTint(intermediateColor);
56 invalidateSelf();
57 }
58
59 private int setAlphaFloat(int color, float alpha) {
Mike Digmanc8217582018-03-06 13:08:33 -080060 // Ensure alpha is clamped [0-255] or ColorUtils will crash
61 final int alphaInt = alpha > 1f ? 255 : (alpha < 0f ? 0 : ((int) alpha*255));
62 return ColorUtils.setAlphaComponent(color, alphaInt);
63 }
64
65 public boolean isDarkIntensitySet() {
66 return mDarkIntensity == DARK_INTENSITY_NOT_SET;
67 }
68
69 public float getDarkIntensity() {
70 return mDarkIntensity;
Mike Digman7d092772018-01-11 12:10:32 -080071 }
72}