blob: 1a85c479a42c68cd53c4fc70a801279814d82d44 [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;
Jason Monk5b3b4852017-03-27 17:40:56 -040025import android.view.Gravity;
Jorim Jaggi40db0292016-06-27 17:58:03 -070026
Matthew Ngeb5ce832018-05-15 17:50:37 -070027import com.android.systemui.R;
28import com.android.systemui.statusbar.phone.ShadowKeyDrawable;
29
Jorim Jaggi40db0292016-06-27 17:58:03 -070030/**
31 * Drawable for {@link KeyButtonView}s which contains an asset for both normal mode and light
32 * navigation bar mode.
33 */
34public class KeyButtonDrawable extends LayerDrawable {
35
36 private final boolean mHasDarkDrawable;
37
Matthew Ngeb5ce832018-05-15 17:50:37 -070038 public static KeyButtonDrawable create(Context lightContext, Drawable lightDrawable,
39 @Nullable Drawable darkDrawable, boolean hasShadow) {
Jorim Jaggi40db0292016-06-27 17:58:03 -070040 if (darkDrawable != null) {
Matthew Ngeb5ce832018-05-15 17:50:37 -070041 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 Jaggi40db0292016-06-27 17:58:03 -070053 } else {
Matthew Ngeb5ce832018-05-15 17:50:37 -070054 return new KeyButtonDrawable(new Drawable[] {
55 new ShadowKeyDrawable(lightDrawable.mutate()) });
Jorim Jaggi40db0292016-06-27 17:58:03 -070056 }
57 }
58
Mike Digman7d092772018-01-11 12:10:32 -080059 protected KeyButtonDrawable(Drawable[] drawables) {
Jorim Jaggi40db0292016-06-27 17:58:03 -070060 super(drawables);
Jason Monk5b3b4852017-03-27 17:40:56 -040061 for (int i = 0; i < drawables.length; i++) {
62 setLayerGravity(i, Gravity.CENTER);
63 }
Jorim Jaggi40db0292016-06-27 17:58:03 -070064 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 Ngeb5ce832018-05-15 17:50:37 -070077
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 Jaggi40db0292016-06-27 17:58:03 -070086}