Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.Nullable; |
Matthew Ng | eb5ce83 | 2018-05-15 17:50:37 -0700 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.res.Resources; |
| 22 | import android.graphics.Color; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
| 24 | import android.graphics.drawable.LayerDrawable; |
Jason Monk | 5b3b485 | 2017-03-27 17:40:56 -0400 | [diff] [blame] | 25 | import android.view.Gravity; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 26 | |
Matthew Ng | eb5ce83 | 2018-05-15 17:50:37 -0700 | [diff] [blame] | 27 | import com.android.systemui.R; |
| 28 | import com.android.systemui.statusbar.phone.ShadowKeyDrawable; |
| 29 | |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Drawable for {@link KeyButtonView}s which contains an asset for both normal mode and light |
| 32 | * navigation bar mode. |
| 33 | */ |
| 34 | public class KeyButtonDrawable extends LayerDrawable { |
| 35 | |
| 36 | private final boolean mHasDarkDrawable; |
| 37 | |
Matthew Ng | eb5ce83 | 2018-05-15 17:50:37 -0700 | [diff] [blame] | 38 | public static KeyButtonDrawable create(Context lightContext, Drawable lightDrawable, |
| 39 | @Nullable Drawable darkDrawable, boolean hasShadow) { |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 40 | if (darkDrawable != null) { |
Matthew Ng | eb5ce83 | 2018-05-15 17:50:37 -0700 | [diff] [blame] | 41 | ShadowKeyDrawable light = new ShadowKeyDrawable(lightDrawable.mutate()); |
| 42 | ShadowKeyDrawable dark = new ShadowKeyDrawable(darkDrawable.mutate()); |
| 43 | if (hasShadow) { |
| 44 | // Only apply the shadow on the light drawable |
| 45 | Resources res = lightContext.getResources(); |
| 46 | int offsetX = res.getDimensionPixelSize(R.dimen.nav_key_button_shadow_offset_x); |
| 47 | int offsetY = res.getDimensionPixelSize(R.dimen.nav_key_button_shadow_offset_y); |
| 48 | int radius = res.getDimensionPixelSize(R.dimen.nav_key_button_shadow_radius); |
| 49 | int color = lightContext.getColor(R.color.nav_key_button_shadow_color); |
| 50 | light.setShadowProperties(offsetX, offsetY, radius, color); |
| 51 | } |
| 52 | return new KeyButtonDrawable(new Drawable[] { light, dark }); |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 53 | } else { |
Matthew Ng | eb5ce83 | 2018-05-15 17:50:37 -0700 | [diff] [blame] | 54 | return new KeyButtonDrawable(new Drawable[] { |
| 55 | new ShadowKeyDrawable(lightDrawable.mutate()) }); |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Mike Digman | 7d09277 | 2018-01-11 12:10:32 -0800 | [diff] [blame] | 59 | protected KeyButtonDrawable(Drawable[] drawables) { |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 60 | super(drawables); |
Jason Monk | 5b3b485 | 2017-03-27 17:40:56 -0400 | [diff] [blame] | 61 | for (int i = 0; i < drawables.length; i++) { |
| 62 | setLayerGravity(i, Gravity.CENTER); |
| 63 | } |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 64 | mutate(); |
| 65 | mHasDarkDrawable = drawables.length > 1; |
| 66 | setDarkIntensity(0f); |
| 67 | } |
| 68 | |
| 69 | public void setDarkIntensity(float intensity) { |
| 70 | if (!mHasDarkDrawable) { |
| 71 | return; |
| 72 | } |
| 73 | getDrawable(0).setAlpha((int) ((1 - intensity) * 255f)); |
| 74 | getDrawable(1).setAlpha((int) (intensity * 255f)); |
| 75 | invalidateSelf(); |
| 76 | } |
Matthew Ng | eb5ce83 | 2018-05-15 17:50:37 -0700 | [diff] [blame] | 77 | |
| 78 | public void setRotation(float degrees) { |
| 79 | if (getDrawable(0) instanceof ShadowKeyDrawable) { |
| 80 | ((ShadowKeyDrawable) getDrawable(0)).setRotation(degrees); |
| 81 | } |
| 82 | if (mHasDarkDrawable && getDrawable(1) instanceof ShadowKeyDrawable) { |
| 83 | ((ShadowKeyDrawable) getDrawable(1)).setRotation(degrees); |
| 84 | } |
| 85 | } |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 86 | } |