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;
19
20import com.android.systemui.plugins.statusbar.phone.NavBarButtonProvider.ButtonInterface;
21import com.android.systemui.statusbar.policy.KeyButtonDrawable;
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 Float mDarkIntensity;
41    private Integer mVisibility = -1;
42    private KeyButtonDrawable mImageDrawable;
43    private View mCurrentView;
44    private boolean mVertical;
45
46    public ButtonDispatcher(int id) {
47        mId = id;
48    }
49
50    void clear() {
51        mViews.clear();
52    }
53
54    void addView(View view) {
55        mViews.add(view);
56        view.setOnClickListener(mClickListener);
57        view.setOnTouchListener(mTouchListener);
58        view.setOnLongClickListener(mLongClickListener);
59        if (mLongClickable != null) {
60            view.setLongClickable(mLongClickable);
61        }
62        if (mAlpha != null) {
63            view.setAlpha(mAlpha);
64        }
65        if (mDarkIntensity != null) {
66            ((ButtonInterface) view).setDarkIntensity(mDarkIntensity);
67        }
68        if (mVisibility != null) {
69            view.setVisibility(mVisibility);
70        }
71        if (mImageDrawable != null) {
72            ((ButtonInterface) view).setImageDrawable(mImageDrawable);
73        }
74
75        if (view instanceof  ButtonInterface) {
76            ((ButtonInterface) view).setVertical(mVertical);
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(KeyButtonDrawable drawable) {
93        mImageDrawable = drawable;
94        final int N = mViews.size();
95        for (int i = 0; i < N; i++) {
96            ((ButtonInterface) mViews.get(i)).setImageDrawable(mImageDrawable);
97        }
98    }
99
100    public void setVisibility(int visibility) {
101        if (mVisibility == visibility) return;
102        mVisibility = visibility;
103        final int N = mViews.size();
104        for (int i = 0; i < N; i++) {
105            mViews.get(i).setVisibility(mVisibility);
106        }
107    }
108
109    public void abortCurrentGesture() {
110        // This seems to be an instantaneous thing, so not going to persist it.
111        final int N = mViews.size();
112        for (int i = 0; i < N; i++) {
113            ((ButtonInterface) mViews.get(i)).abortCurrentGesture();
114        }
115    }
116
117    public void setAlpha(int alpha) {
118        mAlpha = alpha;
119        final int N = mViews.size();
120        for (int i = 0; i < N; i++) {
121            mViews.get(i).setAlpha(alpha);
122        }
123    }
124
125    public void setDarkIntensity(float darkIntensity) {
126        mDarkIntensity = darkIntensity;
127        final int N = mViews.size();
128        for (int i = 0; i < N; i++) {
129            ((ButtonInterface) mViews.get(i)).setDarkIntensity(darkIntensity);
130        }
131    }
132
133    public void setOnClickListener(View.OnClickListener clickListener) {
134        mClickListener = clickListener;
135        final int N = mViews.size();
136        for (int i = 0; i < N; i++) {
137            mViews.get(i).setOnClickListener(mClickListener);
138        }
139    }
140
141    public void setOnTouchListener(View.OnTouchListener touchListener) {
142        mTouchListener = touchListener;
143        final int N = mViews.size();
144        for (int i = 0; i < N; i++) {
145            mViews.get(i).setOnTouchListener(mTouchListener);
146        }
147    }
148
149    public void setLongClickable(boolean isLongClickable) {
150        mLongClickable = isLongClickable;
151        final int N = mViews.size();
152        for (int i = 0; i < N; i++) {
153            mViews.get(i).setLongClickable(mLongClickable);
154        }
155    }
156
157    public void setOnLongClickListener(View.OnLongClickListener longClickListener) {
158        mLongClickListener = longClickListener;
159        final int N = mViews.size();
160        for (int i = 0; i < N; i++) {
161            mViews.get(i).setOnLongClickListener(mLongClickListener);
162        }
163    }
164
165    public ArrayList<View> getViews() {
166        return mViews;
167    }
168
169    public View getCurrentView() {
170        return mCurrentView;
171    }
172
173    public void setCurrentView(View currentView) {
174        mCurrentView = currentView.findViewById(mId);
175    }
176
177    public void setCarMode(boolean carMode) {
178        final int N = mViews.size();
179        for (int i = 0; i < N; i++) {
180            final View view = mViews.get(i);
181            if (view instanceof ButtonInterface) {
182                ((ButtonInterface) view).setCarMode(carMode);
183            }
184        }
185    }
186
187    public void setVertical(boolean vertical) {
188        mVertical = vertical;
189        final int N = mViews.size();
190        for (int i = 0; i < N; i++) {
191            final View view = mViews.get(i);
192            if (view instanceof ButtonInterface) {
193                ((ButtonInterface) view).setVertical(vertical);
194            }
195        }
196    }
197}
198