blob: 8e31f3149bd006d8d622430a935c93883f9493e2 [file] [log] [blame]
Jorim Jaggi40db0292016-06-27 17:58:03 -07001/*
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
17package com.android.systemui.statusbar.policy;
18
19import android.annotation.Nullable;
Matthew Ngeb5ce832018-05-15 17:50:37 -070020import android.content.Context;
21import android.content.res.Resources;
22import android.graphics.Color;
Jorim Jaggi40db0292016-06-27 17:58:03 -070023import android.graphics.drawable.Drawable;
24import android.graphics.drawable.LayerDrawable;
Matthew Ngca4592b2018-08-06 14:12:37 -070025import android.util.FloatProperty;
Jason Monk5b3b4852017-03-27 17:40:56 -040026import android.view.Gravity;
Jorim Jaggi40db0292016-06-27 17:58:03 -070027
Matthew Ngeb5ce832018-05-15 17:50:37 -070028import com.android.systemui.R;
29import com.android.systemui.statusbar.phone.ShadowKeyDrawable;
30
Jorim Jaggi40db0292016-06-27 17:58:03 -070031/**
32 * Drawable for {@link KeyButtonView}s which contains an asset for both normal mode and light
33 * navigation bar mode.
34 */
35public class KeyButtonDrawable extends LayerDrawable {
36
Matthew Ngca4592b2018-08-06 14:12:37 -070037 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 Jaggi40db0292016-06-27 17:58:03 -070063 private final boolean mHasDarkDrawable;
64
Matthew Ngeb5ce832018-05-15 17:50:37 -070065 public static KeyButtonDrawable create(Context lightContext, Drawable lightDrawable,
66 @Nullable Drawable darkDrawable, boolean hasShadow) {
Jorim Jaggi40db0292016-06-27 17:58:03 -070067 if (darkDrawable != null) {
Matthew Ngeb5ce832018-05-15 17:50:37 -070068 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 Jaggi40db0292016-06-27 17:58:03 -070080 } else {
Matthew Ngeb5ce832018-05-15 17:50:37 -070081 return new KeyButtonDrawable(new Drawable[] {
82 new ShadowKeyDrawable(lightDrawable.mutate()) });
Jorim Jaggi40db0292016-06-27 17:58:03 -070083 }
84 }
85
Mike Digman7d092772018-01-11 12:10:32 -080086 protected KeyButtonDrawable(Drawable[] drawables) {
Jorim Jaggi40db0292016-06-27 17:58:03 -070087 super(drawables);
Jason Monk5b3b4852017-03-27 17:40:56 -040088 for (int i = 0; i < drawables.length; i++) {
89 setLayerGravity(i, Gravity.CENTER);
90 }
Jorim Jaggi40db0292016-06-27 17:58:03 -070091 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 Ngeb5ce832018-05-15 17:50:37 -0700104
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 Ngca4592b2018-08-06 14:12:37 -0700113
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 Jaggi40db0292016-06-27 17:58:03 -0700142}