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; |
Matthew Ng | ca4592b | 2018-08-06 14:12:37 -0700 | [diff] [blame^] | 25 | import android.util.FloatProperty; |
Jason Monk | 5b3b485 | 2017-03-27 17:40:56 -0400 | [diff] [blame] | 26 | import android.view.Gravity; |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 27 | |
Matthew Ng | eb5ce83 | 2018-05-15 17:50:37 -0700 | [diff] [blame] | 28 | import com.android.systemui.R; |
| 29 | import com.android.systemui.statusbar.phone.ShadowKeyDrawable; |
| 30 | |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Drawable for {@link KeyButtonView}s which contains an asset for both normal mode and light |
| 33 | * navigation bar mode. |
| 34 | */ |
| 35 | public class KeyButtonDrawable extends LayerDrawable { |
| 36 | |
Matthew Ng | ca4592b | 2018-08-06 14:12:37 -0700 | [diff] [blame^] | 37 | public static final FloatProperty<KeyButtonDrawable> KEY_DRAWABLE_ROTATE = |
| 38 | new FloatProperty<KeyButtonDrawable>("KeyButtonRotation") { |
| 39 | @Override |
| 40 | public void setValue(KeyButtonDrawable drawable, float degree) { |
| 41 | drawable.setRotation(degree); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public Float get(KeyButtonDrawable drawable) { |
| 46 | return drawable.getRotation(); |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | public static final FloatProperty<KeyButtonDrawable> KEY_DRAWABLE_TRANSLATE_Y = |
| 51 | new FloatProperty<KeyButtonDrawable>("KeyButtonTranslateY") { |
| 52 | @Override |
| 53 | public void setValue(KeyButtonDrawable drawable, float y) { |
| 54 | drawable.setTranslationY(y); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public Float get(KeyButtonDrawable drawable) { |
| 59 | return drawable.getTranslationY(); |
| 60 | } |
| 61 | }; |
| 62 | |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 63 | private final boolean mHasDarkDrawable; |
| 64 | |
Matthew Ng | eb5ce83 | 2018-05-15 17:50:37 -0700 | [diff] [blame] | 65 | public static KeyButtonDrawable create(Context lightContext, Drawable lightDrawable, |
| 66 | @Nullable Drawable darkDrawable, boolean hasShadow) { |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 67 | if (darkDrawable != null) { |
Matthew Ng | eb5ce83 | 2018-05-15 17:50:37 -0700 | [diff] [blame] | 68 | ShadowKeyDrawable light = new ShadowKeyDrawable(lightDrawable.mutate()); |
| 69 | ShadowKeyDrawable dark = new ShadowKeyDrawable(darkDrawable.mutate()); |
| 70 | if (hasShadow) { |
| 71 | // Only apply the shadow on the light drawable |
| 72 | Resources res = lightContext.getResources(); |
| 73 | int offsetX = res.getDimensionPixelSize(R.dimen.nav_key_button_shadow_offset_x); |
| 74 | int offsetY = res.getDimensionPixelSize(R.dimen.nav_key_button_shadow_offset_y); |
| 75 | int radius = res.getDimensionPixelSize(R.dimen.nav_key_button_shadow_radius); |
| 76 | int color = lightContext.getColor(R.color.nav_key_button_shadow_color); |
| 77 | light.setShadowProperties(offsetX, offsetY, radius, color); |
| 78 | } |
| 79 | return new KeyButtonDrawable(new Drawable[] { light, dark }); |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 80 | } else { |
Matthew Ng | eb5ce83 | 2018-05-15 17:50:37 -0700 | [diff] [blame] | 81 | return new KeyButtonDrawable(new Drawable[] { |
| 82 | new ShadowKeyDrawable(lightDrawable.mutate()) }); |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Mike Digman | 7d09277 | 2018-01-11 12:10:32 -0800 | [diff] [blame] | 86 | protected KeyButtonDrawable(Drawable[] drawables) { |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 87 | super(drawables); |
Jason Monk | 5b3b485 | 2017-03-27 17:40:56 -0400 | [diff] [blame] | 88 | for (int i = 0; i < drawables.length; i++) { |
| 89 | setLayerGravity(i, Gravity.CENTER); |
| 90 | } |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 91 | mutate(); |
| 92 | mHasDarkDrawable = drawables.length > 1; |
| 93 | setDarkIntensity(0f); |
| 94 | } |
| 95 | |
| 96 | public void setDarkIntensity(float intensity) { |
| 97 | if (!mHasDarkDrawable) { |
| 98 | return; |
| 99 | } |
| 100 | getDrawable(0).setAlpha((int) ((1 - intensity) * 255f)); |
| 101 | getDrawable(1).setAlpha((int) (intensity * 255f)); |
| 102 | invalidateSelf(); |
| 103 | } |
Matthew Ng | eb5ce83 | 2018-05-15 17:50:37 -0700 | [diff] [blame] | 104 | |
| 105 | public void setRotation(float degrees) { |
| 106 | if (getDrawable(0) instanceof ShadowKeyDrawable) { |
| 107 | ((ShadowKeyDrawable) getDrawable(0)).setRotation(degrees); |
| 108 | } |
| 109 | if (mHasDarkDrawable && getDrawable(1) instanceof ShadowKeyDrawable) { |
| 110 | ((ShadowKeyDrawable) getDrawable(1)).setRotation(degrees); |
| 111 | } |
| 112 | } |
Matthew Ng | ca4592b | 2018-08-06 14:12:37 -0700 | [diff] [blame^] | 113 | |
| 114 | public void setTranslationY(float y) { |
| 115 | if (getDrawable(0) instanceof ShadowKeyDrawable) { |
| 116 | ((ShadowKeyDrawable) getDrawable(0)).setTranslationY(y); |
| 117 | } |
| 118 | if (mHasDarkDrawable && getDrawable(1) instanceof ShadowKeyDrawable) { |
| 119 | ((ShadowKeyDrawable) getDrawable(1)).setTranslationY(y); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | public float getRotation() { |
| 124 | if (getDrawable(0) instanceof ShadowKeyDrawable) { |
| 125 | return ((ShadowKeyDrawable) getDrawable(0)).getRotation(); |
| 126 | } |
| 127 | if (mHasDarkDrawable && getDrawable(1) instanceof ShadowKeyDrawable) { |
| 128 | return ((ShadowKeyDrawable) getDrawable(1)).getRotation(); |
| 129 | } |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | public float getTranslationY() { |
| 134 | if (getDrawable(0) instanceof ShadowKeyDrawable) { |
| 135 | return ((ShadowKeyDrawable) getDrawable(0)).getTranslationY(); |
| 136 | } |
| 137 | if (mHasDarkDrawable && getDrawable(1) instanceof ShadowKeyDrawable) { |
| 138 | return ((ShadowKeyDrawable) getDrawable(1)).getTranslationY(); |
| 139 | } |
| 140 | return 0; |
| 141 | } |
Jorim Jaggi | 40db029 | 2016-06-27 17:58:03 -0700 | [diff] [blame] | 142 | } |