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; |
Matthew Ng | 761562d | 2018-09-17 11:13:21 -0700 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Matthew Ng | 25593cc | 2018-09-12 16:05:41 -0700 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.view.View; |
Matthew Ng | 761562d | 2018-09-17 11:13:21 -0700 | [diff] [blame] | 24 | |
Matthew Ng | 25593cc | 2018-09-12 16:05:41 -0700 | [diff] [blame] | 25 | import com.android.systemui.statusbar.policy.KeyButtonDrawable; |
| 26 | import com.android.systemui.statusbar.policy.KeyButtonView; |
| 27 | |
| 28 | /** |
| 29 | * Simple contextual button that is added to the {@link ContextualButtonGroup}. Extend if need extra |
| 30 | * functionality. |
| 31 | */ |
| 32 | public class ContextualButton extends ButtonDispatcher { |
| 33 | |
Matthew Ng | 761562d | 2018-09-17 11:13:21 -0700 | [diff] [blame] | 34 | private ContextButtonListener mListener; |
| 35 | private ContextualButtonGroup mGroup; |
| 36 | |
Matthew Ng | 25593cc | 2018-09-12 16:05:41 -0700 | [diff] [blame] | 37 | protected final @DrawableRes int mIconResId; |
| 38 | |
| 39 | /** |
| 40 | * Create a contextual button that will use a {@link KeyButtonView} and |
| 41 | * {@link KeyButtonDrawable} get and show the button from xml to its icon drawable. |
| 42 | * @param buttonResId the button view from xml layout |
| 43 | * @param iconResId icon resource to be used |
| 44 | */ |
| 45 | public ContextualButton(@IdRes int buttonResId, @DrawableRes int iconResId) { |
| 46 | super(buttonResId); |
| 47 | mIconResId = iconResId; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Reload the drawable from resource id, should reapply the previous dark intensity. |
| 52 | */ |
| 53 | public void updateIcon() { |
| 54 | final KeyButtonDrawable currentDrawable = getImageDrawable(); |
| 55 | KeyButtonDrawable drawable = getNewDrawable(); |
| 56 | if (currentDrawable != null) { |
| 57 | drawable.setDarkIntensity(currentDrawable.getDarkIntensity()); |
| 58 | } |
| 59 | setImageDrawable(drawable); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void setVisibility(int visibility) { |
| 64 | super.setVisibility(visibility); |
| 65 | |
| 66 | // Stop any active animations if hidden |
| 67 | final KeyButtonDrawable currentDrawable = getImageDrawable(); |
| 68 | if (visibility != View.VISIBLE && currentDrawable != null && currentDrawable.canAnimate()) { |
| 69 | currentDrawable.clearAnimationCallbacks(); |
| 70 | currentDrawable.resetAnimation(); |
| 71 | } |
Matthew Ng | 761562d | 2018-09-17 11:13:21 -0700 | [diff] [blame] | 72 | |
| 73 | if (mListener != null) { |
| 74 | mListener.onVisibilityChanged(this, visibility == View.VISIBLE); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public void setListener(ContextButtonListener listener) { |
| 79 | mListener = listener; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Show this button based on its priority compared to other buttons in the group. If not |
| 84 | * attached to a group it will set its own visibility to be visible. |
| 85 | * @return if visible |
| 86 | */ |
| 87 | public boolean show() { |
| 88 | if (mGroup == null) { |
| 89 | setVisibility(View.VISIBLE); |
| 90 | return true; |
| 91 | } |
| 92 | return mGroup.setButtonVisiblity(getId(), true /* visible */) == View.VISIBLE; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Hide this button. |
| 97 | * @return if visible |
| 98 | */ |
| 99 | public boolean hide() { |
| 100 | if (mGroup == null) { |
| 101 | setVisibility(View.INVISIBLE); |
| 102 | return false; |
| 103 | } |
| 104 | return mGroup.setButtonVisiblity(getId(), false /* visible */) != View.VISIBLE; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Called when this button was added to the group. Keep a reference to the group to show based |
| 109 | * on priority compared to other buttons. |
| 110 | * @param group the holder of all the buttons |
| 111 | */ |
| 112 | void attachToGroup(@NonNull ContextualButtonGroup group) { |
| 113 | mGroup = group; |
Matthew Ng | 25593cc | 2018-09-12 16:05:41 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | protected KeyButtonDrawable getNewDrawable() { |
Matthew Ng | 90a56df | 2018-09-25 16:34:43 -0700 | [diff] [blame] | 117 | return KeyButtonDrawable.create(getContext().getApplicationContext(), mIconResId, |
| 118 | false /* shadow */); |
Matthew Ng | 25593cc | 2018-09-12 16:05:41 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Matthew Ng | 90a56df | 2018-09-25 16:34:43 -0700 | [diff] [blame] | 121 | /** |
| 122 | * This context is from the view that could be stale after rotation or config change. To get |
| 123 | * correct resources use getApplicationContext() as well. |
| 124 | * @return current view context |
| 125 | */ |
Matthew Ng | 25593cc | 2018-09-12 16:05:41 -0700 | [diff] [blame] | 126 | protected Context getContext() { |
| 127 | return getCurrentView().getContext(); |
| 128 | } |
Matthew Ng | 761562d | 2018-09-17 11:13:21 -0700 | [diff] [blame] | 129 | |
| 130 | public interface ContextButtonListener { |
| 131 | void onVisibilityChanged(ContextualButton button, boolean visible); |
| 132 | } |
Matthew Ng | 25593cc | 2018-09-12 16:05:41 -0700 | [diff] [blame] | 133 | } |