blob: c7ab27ba715d1b6ef0c82725931003a1eeea7b27 [file] [log] [blame]
Matthew Ng25593cc2018-09-12 16:05:41 -07001/*
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
17package com.android.systemui.statusbar.phone;
18
19import android.annotation.DrawableRes;
20import android.annotation.IdRes;
21import android.content.Context;
22import android.view.View;
23import com.android.systemui.statusbar.policy.KeyButtonDrawable;
24import com.android.systemui.statusbar.policy.KeyButtonView;
25
26/**
27 * Simple contextual button that is added to the {@link ContextualButtonGroup}. Extend if need extra
28 * functionality.
29 */
30public class ContextualButton extends ButtonDispatcher {
31
32 protected final @DrawableRes int mIconResId;
33
34 /**
35 * Create a contextual button that will use a {@link KeyButtonView} and
36 * {@link KeyButtonDrawable} get and show the button from xml to its icon drawable.
37 * @param buttonResId the button view from xml layout
38 * @param iconResId icon resource to be used
39 */
40 public ContextualButton(@IdRes int buttonResId, @DrawableRes int iconResId) {
41 super(buttonResId);
42 mIconResId = iconResId;
43 }
44
45 /**
46 * Reload the drawable from resource id, should reapply the previous dark intensity.
47 */
48 public void updateIcon() {
49 final KeyButtonDrawable currentDrawable = getImageDrawable();
50 KeyButtonDrawable drawable = getNewDrawable();
51 if (currentDrawable != null) {
52 drawable.setDarkIntensity(currentDrawable.getDarkIntensity());
53 }
54 setImageDrawable(drawable);
55 }
56
57 @Override
58 public void setVisibility(int visibility) {
59 super.setVisibility(visibility);
60
61 // Stop any active animations if hidden
62 final KeyButtonDrawable currentDrawable = getImageDrawable();
63 if (visibility != View.VISIBLE && currentDrawable != null && currentDrawable.canAnimate()) {
64 currentDrawable.clearAnimationCallbacks();
65 currentDrawable.resetAnimation();
66 }
67 }
68
69 protected KeyButtonDrawable getNewDrawable() {
Matthew Ng90a56df2018-09-25 16:34:43 -070070 return KeyButtonDrawable.create(getContext().getApplicationContext(), mIconResId,
71 false /* shadow */);
Matthew Ng25593cc2018-09-12 16:05:41 -070072 }
73
Matthew Ng90a56df2018-09-25 16:34:43 -070074 /**
75 * This context is from the view that could be stale after rotation or config change. To get
76 * correct resources use getApplicationContext() as well.
77 * @return current view context
78 */
Matthew Ng25593cc2018-09-12 16:05:41 -070079 protected Context getContext() {
80 return getCurrentView().getContext();
81 }
82}