1 /*
2  * Copyright (c) 2016, 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 package com.android.car.hvac.ui;
17 
18 import android.content.Context;
19 import android.util.AttributeSet;
20 import android.widget.ImageView;
21 import android.widget.LinearLayout;
22 
23 import androidx.annotation.IntDef;
24 
25 import com.android.car.hvac.R;
26 import java.util.ArrayList;
27 
28 /**
29  * A set of buttons that controls the fan direction of the vehicle. Turning on one state will
30  * turn off the other states.
31  */
32 public class FanDirectionButtons extends LinearLayout {
33     public static final int FAN_DIRECTION_FACE = 0;
34     public static final int FAN_DIRECTION_FACE_FLOOR = 1;
35     public static final int FAN_DIRECTION_FLOOR = 2;
36     public static final int FAN_DIRECTION_FLOOR_DEFROSTER = 3;
37     public static final int FAN_DIRECTION_COUNT = 4;
38 
39     @IntDef({FAN_DIRECTION_FACE, FAN_DIRECTION_FACE_FLOOR,
40             FAN_DIRECTION_FLOOR, FAN_DIRECTION_FLOOR_DEFROSTER})
41     public @interface FanDirection {}
42 
43     /**
44      * A resource id array for all fan direction buttons.
45      */
46     private static final int[] FAN_DIRECTION_BUTTON_IDS = { R.id.direction_1, R.id.direction_2,
47                                   R.id.direction_3, R.id.direction_4 };
48 
49     /**
50      * A listener that is notified when a fan direction button has been clicked.
51      */
52     public interface FanDirectionClickListener {
onFanDirectionClicked(@anDirection int direction)53         void onFanDirectionClicked(@FanDirection int direction);
54     }
55 
56     private final ArrayList<ImageView> mButtonArray = new ArrayList<ImageView>();
57 
58     private int mCurrentDirection = -1;
59     private FanDirectionClickListener mListener;
60 
FanDirectionButtons(Context context)61     public FanDirectionButtons(Context context) {
62         super(context);
63         init();
64     }
65 
FanDirectionButtons(Context context, AttributeSet attrs)66     public FanDirectionButtons(Context context, AttributeSet attrs) {
67         super(context, attrs);
68         init();
69     }
70 
FanDirectionButtons(Context context, AttributeSet attrs, int defStyleAttr)71     public FanDirectionButtons(Context context, AttributeSet attrs, int defStyleAttr) {
72         super(context, attrs, defStyleAttr);
73         init();
74     }
75 
setFanDirectionClickListener(FanDirectionClickListener listener)76     public void setFanDirectionClickListener(FanDirectionClickListener listener) {
77         mListener = listener;
78     }
79 
setFanDirection(@anDirection int direction)80     public void setFanDirection(@FanDirection int direction) {
81         if (direction != mCurrentDirection) {
82             updateFanButtonToOn(direction);
83         }
84     }
85 
init()86     private void init() {
87         inflate(getContext(), R.layout.fan_direction, this);
88     }
89 
90     @Override
onFinishInflate()91     protected void onFinishInflate() {
92         super.onFinishInflate();
93         setOrientation(HORIZONTAL);
94 
95         for (int i = 0; i < FAN_DIRECTION_COUNT; i++) {
96             ImageView button = (ImageView) findViewById(FAN_DIRECTION_BUTTON_IDS[i]);
97             button.setTag(i);
98             button.setOnClickListener(v -> {
99                 int direction = (int) v.getTag();
100                 if (direction != mCurrentDirection) {
101                     updateFanButtonToOn(direction);
102                     if (mListener != null) {
103                         mListener.onFanDirectionClicked(direction);
104                     }
105                 }
106             });
107 
108             mButtonArray.add(button);
109         }
110     }
111 
updateFanButtonToOn(int directionToOn)112     private void updateFanButtonToOn(int directionToOn) {
113         if (mCurrentDirection != -1) {
114             mButtonArray.get(mCurrentDirection).setSelected(false);
115         }
116         mButtonArray.get(directionToOn).setSelected(true);
117         mCurrentDirection = directionToOn;
118     }
119 }
120