1a20818257e6927560a676d99dbc563a0394f74d8Jason Monk/* 2a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * Copyright (C) 2016 The Android Open Source Project 3a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * 4a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * except in compliance with the License. You may obtain a copy of the License at 6a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * 7a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * http://www.apache.org/licenses/LICENSE-2.0 8a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * 9a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * Unless required by applicable law or agreed to in writing, software distributed under the 10a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * KIND, either express or implied. See the License for the specific language governing 12a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * permissions and limitations under the License. 13a20818257e6927560a676d99dbc563a0394f74d8Jason Monk */ 14a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 15a20818257e6927560a676d99dbc563a0394f74d8Jason Monkpackage com.android.systemui.statusbar.phone; 16a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 17a20818257e6927560a676d99dbc563a0394f74d8Jason Monkimport android.graphics.drawable.Drawable; 18a20818257e6927560a676d99dbc563a0394f74d8Jason Monkimport android.view.View; 19a20818257e6927560a676d99dbc563a0394f74d8Jason Monkimport android.widget.ImageView; 20a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 21c0d7058b14c24cd07912f5629c26b39b7b4673d5Winsonimport com.android.systemui.statusbar.policy.KeyButtonView; 22c0d7058b14c24cd07912f5629c26b39b7b4673d5Winson 23a20818257e6927560a676d99dbc563a0394f74d8Jason Monkimport java.util.ArrayList; 24a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 25a20818257e6927560a676d99dbc563a0394f74d8Jason Monk/** 26a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * Dispatches common view calls to multiple views. This is used to handle 27a20818257e6927560a676d99dbc563a0394f74d8Jason Monk * multiples of the same nav bar icon appearing. 28a20818257e6927560a676d99dbc563a0394f74d8Jason Monk */ 29a20818257e6927560a676d99dbc563a0394f74d8Jason Monkpublic class ButtonDispatcher { 30a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 31a20818257e6927560a676d99dbc563a0394f74d8Jason Monk private final ArrayList<View> mViews = new ArrayList<>(); 32a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 33a20818257e6927560a676d99dbc563a0394f74d8Jason Monk private final int mId; 34a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 35a20818257e6927560a676d99dbc563a0394f74d8Jason Monk private View.OnClickListener mClickListener; 36a20818257e6927560a676d99dbc563a0394f74d8Jason Monk private View.OnTouchListener mTouchListener; 37a20818257e6927560a676d99dbc563a0394f74d8Jason Monk private View.OnLongClickListener mLongClickListener; 38a20818257e6927560a676d99dbc563a0394f74d8Jason Monk private Boolean mLongClickable; 39a20818257e6927560a676d99dbc563a0394f74d8Jason Monk private Integer mAlpha; 40a20818257e6927560a676d99dbc563a0394f74d8Jason Monk private Integer mVisibility = -1; 41a20818257e6927560a676d99dbc563a0394f74d8Jason Monk private int mImageResource = -1; 42a20818257e6927560a676d99dbc563a0394f74d8Jason Monk private Drawable mImageDrawable; 43a20818257e6927560a676d99dbc563a0394f74d8Jason Monk private View mCurrentView; 44a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 45a20818257e6927560a676d99dbc563a0394f74d8Jason Monk public ButtonDispatcher(int id) { 46a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mId = id; 47a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 48a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 49a20818257e6927560a676d99dbc563a0394f74d8Jason Monk void clear() { 50a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mViews.clear(); 51a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 52a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 53a20818257e6927560a676d99dbc563a0394f74d8Jason Monk void addView(View view) { 54a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mViews.add(view); 55a20818257e6927560a676d99dbc563a0394f74d8Jason Monk view.setOnClickListener(mClickListener); 56a20818257e6927560a676d99dbc563a0394f74d8Jason Monk view.setOnTouchListener(mTouchListener); 57a20818257e6927560a676d99dbc563a0394f74d8Jason Monk view.setOnLongClickListener(mLongClickListener); 58a20818257e6927560a676d99dbc563a0394f74d8Jason Monk if (mLongClickable != null) { 59a20818257e6927560a676d99dbc563a0394f74d8Jason Monk view.setLongClickable(mLongClickable); 60a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 61a20818257e6927560a676d99dbc563a0394f74d8Jason Monk if (mAlpha != null) { 62a20818257e6927560a676d99dbc563a0394f74d8Jason Monk view.setAlpha(mAlpha); 63a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 64a20818257e6927560a676d99dbc563a0394f74d8Jason Monk if (mVisibility != null) { 65a20818257e6927560a676d99dbc563a0394f74d8Jason Monk view.setVisibility(mVisibility); 66a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 67a20818257e6927560a676d99dbc563a0394f74d8Jason Monk if (mImageResource > 0) { 68a20818257e6927560a676d99dbc563a0394f74d8Jason Monk ((ImageView) view).setImageResource(mImageResource); 69a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } else if (mImageDrawable != null) { 70a20818257e6927560a676d99dbc563a0394f74d8Jason Monk ((ImageView) view).setImageDrawable(mImageDrawable); 71a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 72a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 73a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 74a20818257e6927560a676d99dbc563a0394f74d8Jason Monk public int getId() { 75a20818257e6927560a676d99dbc563a0394f74d8Jason Monk return mId; 76a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 77a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 78a20818257e6927560a676d99dbc563a0394f74d8Jason Monk public int getVisibility() { 7925d3a437d5ed44d8f719a1eff9dff3eb7fa2b309Jason Monk return mVisibility != null ? mVisibility : View.VISIBLE; 80a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 81a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 82a20818257e6927560a676d99dbc563a0394f74d8Jason Monk public float getAlpha() { 8325d3a437d5ed44d8f719a1eff9dff3eb7fa2b309Jason Monk return mAlpha != null ? mAlpha : 1; 84a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 85a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 86a20818257e6927560a676d99dbc563a0394f74d8Jason Monk public void setImageDrawable(Drawable drawable) { 87a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mImageDrawable = drawable; 88a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mImageResource = -1; 89a20818257e6927560a676d99dbc563a0394f74d8Jason Monk final int N = mViews.size(); 90a20818257e6927560a676d99dbc563a0394f74d8Jason Monk for (int i = 0; i < N; i++) { 91a20818257e6927560a676d99dbc563a0394f74d8Jason Monk ((ImageView) mViews.get(i)).setImageDrawable(mImageDrawable); 92a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 93a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 94a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 95a20818257e6927560a676d99dbc563a0394f74d8Jason Monk public void setImageResource(int resource) { 96a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mImageResource = resource; 97a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mImageDrawable = null; 98a20818257e6927560a676d99dbc563a0394f74d8Jason Monk final int N = mViews.size(); 99a20818257e6927560a676d99dbc563a0394f74d8Jason Monk for (int i = 0; i < N; i++) { 100a20818257e6927560a676d99dbc563a0394f74d8Jason Monk ((ImageView) mViews.get(i)).setImageResource(mImageResource); 101a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 102a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 103a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 104a20818257e6927560a676d99dbc563a0394f74d8Jason Monk public void setVisibility(int visibility) { 105a20818257e6927560a676d99dbc563a0394f74d8Jason Monk if (mVisibility == visibility) return; 106a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mVisibility = visibility; 107a20818257e6927560a676d99dbc563a0394f74d8Jason Monk final int N = mViews.size(); 108a20818257e6927560a676d99dbc563a0394f74d8Jason Monk for (int i = 0; i < N; i++) { 109a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mViews.get(i).setVisibility(mVisibility); 110a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 111a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 112a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 113a20818257e6927560a676d99dbc563a0394f74d8Jason Monk public void abortCurrentGesture() { 114a20818257e6927560a676d99dbc563a0394f74d8Jason Monk // This seems to be an instantaneous thing, so not going to persist it. 115a20818257e6927560a676d99dbc563a0394f74d8Jason Monk final int N = mViews.size(); 116a20818257e6927560a676d99dbc563a0394f74d8Jason Monk for (int i = 0; i < N; i++) { 117a20818257e6927560a676d99dbc563a0394f74d8Jason Monk ((KeyButtonView) mViews.get(i)).abortCurrentGesture(); 118a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 119a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 120a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 121a20818257e6927560a676d99dbc563a0394f74d8Jason Monk public void setAlpha(int alpha) { 122a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mAlpha = alpha; 123a20818257e6927560a676d99dbc563a0394f74d8Jason Monk final int N = mViews.size(); 124a20818257e6927560a676d99dbc563a0394f74d8Jason Monk for (int i = 0; i < N; i++) { 125a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mViews.get(i).setAlpha(alpha); 126a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 127a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 128a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 129a20818257e6927560a676d99dbc563a0394f74d8Jason Monk public void setOnClickListener(View.OnClickListener clickListener) { 130a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mClickListener = clickListener; 131a20818257e6927560a676d99dbc563a0394f74d8Jason Monk final int N = mViews.size(); 132a20818257e6927560a676d99dbc563a0394f74d8Jason Monk for (int i = 0; i < N; i++) { 133a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mViews.get(i).setOnClickListener(mClickListener); 134a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 135a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 136a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 137a20818257e6927560a676d99dbc563a0394f74d8Jason Monk public void setOnTouchListener(View.OnTouchListener touchListener) { 138a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mTouchListener = touchListener; 139a20818257e6927560a676d99dbc563a0394f74d8Jason Monk final int N = mViews.size(); 140a20818257e6927560a676d99dbc563a0394f74d8Jason Monk for (int i = 0; i < N; i++) { 141a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mViews.get(i).setOnTouchListener(mTouchListener); 142a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 143a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 144a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 145a20818257e6927560a676d99dbc563a0394f74d8Jason Monk public void setLongClickable(boolean isLongClickable) { 146a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mLongClickable = isLongClickable; 147a20818257e6927560a676d99dbc563a0394f74d8Jason Monk final int N = mViews.size(); 148a20818257e6927560a676d99dbc563a0394f74d8Jason Monk for (int i = 0; i < N; i++) { 149a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mViews.get(i).setLongClickable(mLongClickable); 150a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 151a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 152a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 153a20818257e6927560a676d99dbc563a0394f74d8Jason Monk public void setOnLongClickListener(View.OnLongClickListener longClickListener) { 154a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mLongClickListener = longClickListener; 155a20818257e6927560a676d99dbc563a0394f74d8Jason Monk final int N = mViews.size(); 156a20818257e6927560a676d99dbc563a0394f74d8Jason Monk for (int i = 0; i < N; i++) { 157a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mViews.get(i).setOnLongClickListener(mLongClickListener); 158a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 159a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 160a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 161a20818257e6927560a676d99dbc563a0394f74d8Jason Monk public View getCurrentView() { 162a20818257e6927560a676d99dbc563a0394f74d8Jason Monk return mCurrentView; 163a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 164a20818257e6927560a676d99dbc563a0394f74d8Jason Monk 165a20818257e6927560a676d99dbc563a0394f74d8Jason Monk public void setCurrentView(View currentView) { 166a20818257e6927560a676d99dbc563a0394f74d8Jason Monk mCurrentView = currentView.findViewById(mId); 167a20818257e6927560a676d99dbc563a0394f74d8Jason Monk } 168a20818257e6927560a676d99dbc563a0394f74d8Jason Monk} 169