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 15 package com.android.systemui.statusbar.phone; 16 17 import android.annotation.DrawableRes; 18 import android.annotation.Nullable; 19 import android.graphics.drawable.Drawable; 20 import android.view.View; 21 22 import java.util.ArrayList; 23 24 /** 25 * Dispatches common view calls to multiple views. This is used to handle 26 * multiples of the same nav bar icon appearing. 27 */ 28 public class ButtonDispatcher { 29 30 private final ArrayList<View> mViews = new ArrayList<>(); 31 32 private final int mId; 33 34 private View.OnClickListener mClickListener; 35 private View.OnTouchListener mTouchListener; 36 private View.OnLongClickListener mLongClickListener; 37 private Boolean mLongClickable; 38 private Integer mAlpha; 39 private Integer mVisibility = -1; 40 private int mImageResource = -1; 41 private Drawable mImageDrawable; 42 private View mCurrentView; 43 44 public ButtonDispatcher(int id) { 45 mId = id; 46 } 47 48 void clear() { 49 mViews.clear(); 50 } 51 52 void addView(View view, boolean landscape) { 53 addView(view); 54 if (view instanceof ButtonInterface) { 55 ((ButtonInterface) view).setLandscape(landscape); 56 } 57 } 58 59 void addView(View view) { 60 mViews.add(view); 61 view.setOnClickListener(mClickListener); 62 view.setOnTouchListener(mTouchListener); 63 view.setOnLongClickListener(mLongClickListener); 64 if (mLongClickable != null) { 65 view.setLongClickable(mLongClickable); 66 } 67 if (mAlpha != null) { 68 view.setAlpha(mAlpha); 69 } 70 if (mVisibility != null) { 71 view.setVisibility(mVisibility); 72 } 73 if (mImageResource > 0) { 74 ((ButtonInterface) view).setImageResource(mImageResource); 75 } else if (mImageDrawable != null) { 76 ((ButtonInterface) view).setImageDrawable(mImageDrawable); 77 } 78 } 79 80 public int getId() { 81 return mId; 82 } 83 84 public int getVisibility() { 85 return mVisibility != null ? mVisibility : View.VISIBLE; 86 } 87 88 public float getAlpha() { 89 return mAlpha != null ? mAlpha : 1; 90 } 91 92 public void setImageDrawable(Drawable drawable) { 93 mImageDrawable = drawable; 94 mImageResource = -1; 95 final int N = mViews.size(); 96 for (int i = 0; i < N; i++) { 97 ((ButtonInterface) mViews.get(i)).setImageDrawable(mImageDrawable); 98 } 99 } 100 101 public void setImageResource(int resource) { 102 mImageResource = resource; 103 mImageDrawable = null; 104 final int N = mViews.size(); 105 for (int i = 0; i < N; i++) { 106 ((ButtonInterface) mViews.get(i)).setImageResource(mImageResource); 107 } 108 } 109 110 public void setVisibility(int visibility) { 111 if (mVisibility == visibility) return; 112 mVisibility = visibility; 113 final int N = mViews.size(); 114 for (int i = 0; i < N; i++) { 115 mViews.get(i).setVisibility(mVisibility); 116 } 117 } 118 119 public void abortCurrentGesture() { 120 // This seems to be an instantaneous thing, so not going to persist it. 121 final int N = mViews.size(); 122 for (int i = 0; i < N; i++) { 123 ((ButtonInterface) mViews.get(i)).abortCurrentGesture(); 124 } 125 } 126 127 public void setAlpha(int alpha) { 128 mAlpha = alpha; 129 final int N = mViews.size(); 130 for (int i = 0; i < N; i++) { 131 mViews.get(i).setAlpha(alpha); 132 } 133 } 134 135 public void setOnClickListener(View.OnClickListener clickListener) { 136 mClickListener = clickListener; 137 final int N = mViews.size(); 138 for (int i = 0; i < N; i++) { 139 mViews.get(i).setOnClickListener(mClickListener); 140 } 141 } 142 143 public void setOnTouchListener(View.OnTouchListener touchListener) { 144 mTouchListener = touchListener; 145 final int N = mViews.size(); 146 for (int i = 0; i < N; i++) { 147 mViews.get(i).setOnTouchListener(mTouchListener); 148 } 149 } 150 151 public void setLongClickable(boolean isLongClickable) { 152 mLongClickable = isLongClickable; 153 final int N = mViews.size(); 154 for (int i = 0; i < N; i++) { 155 mViews.get(i).setLongClickable(mLongClickable); 156 } 157 } 158 159 public void setOnLongClickListener(View.OnLongClickListener longClickListener) { 160 mLongClickListener = longClickListener; 161 final int N = mViews.size(); 162 for (int i = 0; i < N; i++) { 163 mViews.get(i).setOnLongClickListener(mLongClickListener); 164 } 165 } 166 167 public ArrayList<View> getViews() { 168 return mViews; 169 } 170 171 public View getCurrentView() { 172 return mCurrentView; 173 } 174 175 public void setCurrentView(View currentView) { 176 mCurrentView = currentView.findViewById(mId); 177 } 178 179 public void setCarMode(boolean carMode) { 180 final int N = mViews.size(); 181 for (int i = 0; i < N; i++) { 182 final View view = mViews.get(i); 183 if (view instanceof ButtonInterface) { 184 ((ButtonInterface) view).setCarMode(carMode); 185 } 186 } 187 } 188 189 /** 190 * Interface for button actions. 191 */ 192 public interface ButtonInterface { 193 void setImageResource(@DrawableRes int resId); 194 195 void setImageDrawable(@Nullable Drawable drawable); 196 197 void abortCurrentGesture(); 198 199 void setLandscape(boolean landscape); 200 201 void setCarMode(boolean carMode); 202 } 203 } 204