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.view.View;
     23 import android.widget.ImageView;
     24 
     25 import androidx.annotation.IntDef;
     26 
     27 import com.android.car.hvac.R;
     28 
     29 /**
     30  * A seat warmer button that has 3 heating levels.
     31  */
     32 public class SeatWarmerButton extends ImageView {
     33     public static final int HEAT_OFF = 0;
     34     public static final int HEAT_LEVEL_ONE = 1;
     35     public static final int HEAT_LEVEL_TWO = 2;
     36     public static final int HEAT_LEVEL_THREE = 3;
     37 
     38     public interface SeatWarmerButtonClickListener{
     39         void onSeatWarmerButtonClicked(@HeatingLevel int level);
     40     }
     41 
     42     @IntDef({HEAT_OFF, HEAT_LEVEL_ONE, HEAT_LEVEL_TWO, HEAT_LEVEL_THREE})
     43     public @interface HeatingLevel {}
     44 
     45     private @HeatingLevel int mCurrentHeatSetting;
     46 
     47     private final Drawable mStateDrawables[] = new Drawable[4];
     48 
     49     private SeatWarmerButtonClickListener mListener;
     50 
     51     public SeatWarmerButton(Context context) {
     52         super(context);
     53         init();
     54     }
     55 
     56     public SeatWarmerButton(Context context, AttributeSet attrs) {
     57         super(context, attrs);
     58         init();
     59     }
     60 
     61     public SeatWarmerButton(Context context, AttributeSet attrs, int defStyleAttr) {
     62         super(context, attrs, defStyleAttr);
     63         init();
     64     }
     65 
     66     private void init() {
     67         Resources res = getResources();
     68         mStateDrawables[0] = res.getDrawable(R.drawable.ic_seat_heat_off);
     69         mStateDrawables[1] = res.getDrawable(R.drawable.ic_seat_heat_level_1);
     70         mStateDrawables[2] = res.getDrawable(R.drawable.ic_seat_heat_level_2);
     71         mStateDrawables[3] = res.getDrawable(R.drawable.ic_seat_heat_level_3);
     72 
     73         setOnClickListener(new OnClickListener() {
     74             @Override
     75             public void onClick(View v) {
     76                 mCurrentHeatSetting = getNextHeatSetting(mCurrentHeatSetting);
     77                 Drawable d = getDrawableForState(mCurrentHeatSetting);
     78                 setImageDrawable(d);
     79                 if (mListener != null) {
     80                     mListener.onSeatWarmerButtonClicked(mCurrentHeatSetting);
     81                 }
     82             }
     83         });
     84     }
     85 
     86     public void setSeatWarmerClickListener(SeatWarmerButtonClickListener listener) {
     87         mListener = listener;
     88     }
     89 
     90     public void setHeatLevel(@HeatingLevel int level) {
     91         mCurrentHeatSetting = level;
     92         setImageDrawable(getDrawableForState(mCurrentHeatSetting));
     93     }
     94 
     95     private Drawable getDrawableForState(@HeatingLevel int level) {
     96         switch (level) {
     97             case HEAT_LEVEL_ONE:
     98                 return mStateDrawables[1];
     99             case HEAT_LEVEL_TWO:
    100                 return mStateDrawables[2];
    101             case HEAT_LEVEL_THREE:
    102                 return mStateDrawables[3];
    103             case HEAT_OFF:
    104             default:
    105                 return mStateDrawables[0];
    106         }
    107     }
    108 
    109     private @HeatingLevel int getNextHeatSetting(@HeatingLevel int level) {
    110         switch (level) {
    111             case HEAT_OFF:
    112                 return HEAT_LEVEL_ONE;
    113             case HEAT_LEVEL_ONE:
    114                 return HEAT_LEVEL_TWO;
    115             case HEAT_LEVEL_TWO:
    116                 return HEAT_LEVEL_THREE;
    117             case HEAT_LEVEL_THREE:
    118                 return HEAT_OFF;
    119             default:
    120                 return HEAT_OFF;
    121         }
    122     }
    123 }
    124