Mike Digman | 7d09277 | 2018-01-11 12:10:32 -0800 | [diff] [blame] | 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.drawable.Drawable; |
| 21 | |
| 22 | import 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 | */ |
| 28 | public class TintedKeyButtonDrawable extends KeyButtonDrawable { |
| 29 | |
| 30 | private final int mLightColor; |
| 31 | private final int mDarkColor; |
| 32 | |
Mike Digman | c821758 | 2018-03-06 13:08:33 -0800 | [diff] [blame] | 33 | public static final float DARK_INTENSITY_NOT_SET = -1f; |
| 34 | private float mDarkIntensity = DARK_INTENSITY_NOT_SET; |
| 35 | |
Mike Digman | 7d09277 | 2018-01-11 12:10:32 -0800 | [diff] [blame] | 36 | 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 Digman | c821758 | 2018-03-06 13:08:33 -0800 | [diff] [blame] | 45 | setDarkIntensity(0f); // Set initial coloration |
Mike Digman | 7d09277 | 2018-01-11 12:10:32 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public void setDarkIntensity(float intensity) { |
| 50 | // Duplicate intensity scaling from KeyButtonDrawable |
Mike Digman | c821758 | 2018-03-06 13:08:33 -0800 | [diff] [blame] | 51 | mDarkIntensity = intensity; |
Mike Digman | 7d09277 | 2018-01-11 12:10:32 -0800 | [diff] [blame] | 52 | 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 Digman | c821758 | 2018-03-06 13:08:33 -0800 | [diff] [blame] | 60 | // 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 Digman | 7d09277 | 2018-01-11 12:10:32 -0800 | [diff] [blame] | 71 | } |
| 72 | } |