Home | History | Annotate | Download | only in ui
      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.content.res.Resources;
     20 import android.graphics.drawable.Drawable;
     21 import android.util.AttributeSet;
     22 import android.util.Pair;
     23 import android.view.View;
     24 import android.widget.ImageView;
     25 import android.widget.LinearLayout;
     26 
     27 import androidx.annotation.IntDef;
     28 
     29 import com.android.car.hvac.R;
     30 
     31 import java.util.HashMap;
     32 import java.util.Map;
     33 
     34 /**
     35  * A set of buttons that controls the fan direction of the vehicle. Turning on one state will
     36  * turn off the other states.
     37  */
     38 public class FanDirectionButtons extends LinearLayout {
     39     public static final int FAN_DIRECTION_FACE = 0;
     40     public static final int FAN_DIRECTION_FACE_FLOOR = 1;
     41     public static final int FAN_DIRECTION_FLOOR = 2;
     42     public static final int FAN_DIRECTION_FLOOR_DEFROSTER = 3;
     43 
     44     @IntDef({FAN_DIRECTION_FACE, FAN_DIRECTION_FACE_FLOOR,
     45             FAN_DIRECTION_FLOOR, FAN_DIRECTION_FLOOR_DEFROSTER})
     46     public @interface FanDirection {}
     47 
     48     /**
     49      * A listener that is notified when a fan direction button has been clicked.
     50      */
     51     public interface FanDirectionClickListener {
     52         void onFanDirectionClicked(@FanDirection int direction);
     53     }
     54 
     55     private static final float UNSELECTED_BUTTON_ALPHA = 0.5f;
     56     private static final float SELECTED_BUTTON_ALPHA = 1.0f;
     57 
     58     private final Map<ImageView, Pair<Drawable, Drawable>> mFanMap = new HashMap<>();
     59     private final Map<ImageView, Integer> mControlMap = new HashMap<>();
     60 
     61     private FanDirectionClickListener mListener;
     62 
     63     public FanDirectionButtons(Context context) {
     64         super(context);
     65         init();
     66     }
     67 
     68     public FanDirectionButtons(Context context, AttributeSet attrs) {
     69         super(context, attrs);
     70         init();
     71     }
     72 
     73     public FanDirectionButtons(Context context, AttributeSet attrs, int defStyleAttr) {
     74         super(context, attrs, defStyleAttr);
     75         init();
     76     }
     77 
     78     public void setFanDirectionClickListener(FanDirectionClickListener listener) {
     79         mListener = listener;
     80     }
     81 
     82     private void init() {
     83         inflate(getContext(), R.layout.fan_direction, this);
     84     }
     85 
     86     @Override
     87     protected void onFinishInflate() {
     88         super.onFinishInflate();
     89         Resources res = getResources();
     90 
     91         setOrientation(HORIZONTAL);
     92 
     93         ImageView directionButton1 = (ImageView) findViewById(R.id.direction_1);
     94         ImageView directionButton2 = (ImageView) findViewById(R.id.direction_2);
     95         ImageView directionButton3 = (ImageView) findViewById(R.id.direction_3);
     96         ImageView directionButton4 = (ImageView) findViewById(R.id.direction_4);
     97 
     98         Drawable directionOn1 = res.getDrawable(R.drawable.ic_fan_direction_1_on);
     99         Drawable directionOn2 = res.getDrawable(R.drawable.ic_fan_direction_2_on);
    100         Drawable directionOn3 = res.getDrawable(R.drawable.ic_fan_direction_3_on);
    101         Drawable directionOn4 = res.getDrawable(R.drawable.ic_fan_direction_4_on);
    102 
    103         Drawable directionOff1 = res.getDrawable(R.drawable.ic_fan_direction_1_off);
    104         Drawable directionOff2 = res.getDrawable(R.drawable.ic_fan_direction_2_off);
    105         Drawable directionOff3 = res.getDrawable(R.drawable.ic_fan_direction_3_off);
    106         Drawable directionOff4 = res.getDrawable(R.drawable.ic_fan_direction_4_off);
    107 
    108         mFanMap.put(directionButton1, new Pair(directionOn1, directionOff1));
    109         mFanMap.put(directionButton2, new Pair(directionOn2, directionOff2));
    110         mFanMap.put(directionButton3, new Pair(directionOn3, directionOff3));
    111         mFanMap.put(directionButton4, new Pair(directionOn4, directionOff4));
    112 
    113         mControlMap.put(directionButton1, FAN_DIRECTION_FACE);
    114         mControlMap.put(directionButton2, FAN_DIRECTION_FACE_FLOOR);
    115         mControlMap.put(directionButton3, FAN_DIRECTION_FLOOR);
    116         mControlMap.put(directionButton4, FAN_DIRECTION_FLOOR_DEFROSTER);
    117 
    118         for (ImageView v : mFanMap.keySet()) {
    119             v.setOnClickListener(mFanDirectionClickListener);
    120         }
    121     }
    122 
    123     private final OnClickListener mFanDirectionClickListener = new OnClickListener() {
    124         @Override
    125         public void onClick(View v) {
    126             resetFanToOff();
    127             v.setAlpha(SELECTED_BUTTON_ALPHA);
    128             if (mFanMap.containsKey(v)) {
    129                 ((ImageView) v).setImageDrawable(mFanMap.get(v).first);
    130                 v.setAlpha(SELECTED_BUTTON_ALPHA);
    131                 @FanDirection int direction = mControlMap.get(v);
    132                 mListener.onFanDirectionClicked(direction);
    133             }
    134         }
    135     };
    136 
    137     private void resetFanToOff() {
    138         for (Map.Entry<ImageView, Pair<Drawable, Drawable>> entry : mFanMap.entrySet()) {
    139             ImageView button = entry.getKey();
    140             button.setImageDrawable(entry.getValue().second);
    141             button.setAlpha(UNSELECTED_BUTTON_ALPHA);
    142         }
    143     }
    144 }
    145