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