Matthew Ng | 25593cc | 2018-09-12 16:05:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.phone; |
| 18 | |
| 19 | import android.annotation.DrawableRes; |
| 20 | import android.annotation.IdRes; |
| 21 | import android.content.Context; |
| 22 | import android.view.View; |
| 23 | import com.android.systemui.statusbar.policy.KeyButtonDrawable; |
| 24 | import 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 | */ |
| 30 | public 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 Ng | 90a56df | 2018-09-25 16:34:43 -0700 | [diff] [blame] | 70 | return KeyButtonDrawable.create(getContext().getApplicationContext(), mIconResId, |
| 71 | false /* shadow */); |
Matthew Ng | 25593cc | 2018-09-12 16:05:41 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Matthew Ng | 90a56df | 2018-09-25 16:34:43 -0700 | [diff] [blame] | 74 | /** |
| 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 Ng | 25593cc | 2018-09-12 16:05:41 -0700 | [diff] [blame] | 79 | protected Context getContext() { |
| 80 | return getCurrentView().getContext(); |
| 81 | } |
| 82 | } |