1 /* 2 * Copyright (C) 2020 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 17 package com.android.systemui.car.hvac; 18 19 import static com.android.systemui.car.hvac.HvacController.convertToCelsius; 20 import static com.android.systemui.car.hvac.HvacController.convertToFahrenheit; 21 22 import android.content.Context; 23 import android.content.res.TypedArray; 24 import android.util.AttributeSet; 25 import android.view.LayoutInflater; 26 import android.widget.LinearLayout; 27 import android.widget.TextView; 28 29 import com.android.systemui.R; 30 31 /** 32 * Displays temperature with a button to decrease and a button to increase on either side. 33 * Properties configured in the XML: 34 * hvacAreaId - Example: VehicleSeat.SEAT_ROW_1_LEFT (1) 35 */ 36 public class AdjustableTemperatureView extends LinearLayout implements TemperatureView { 37 38 private final int mAreaId; 39 private TextView mTempTextView; 40 private float mMinTempC; 41 private float mMaxTempC; 42 private String mTempFormat; 43 private String mNullTempText; 44 private String mMinTempText; 45 private String mMaxTempText; 46 private boolean mDisplayInFahrenheit = false; 47 48 private HvacController mHvacController; 49 private float mCurrentTempC; 50 AdjustableTemperatureView(Context context, AttributeSet attrs)51 public AdjustableTemperatureView(Context context, AttributeSet attrs) { 52 super(context, attrs); 53 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TemperatureView); 54 mAreaId = typedArray.getInt(R.styleable.TemperatureView_hvacAreaId, -1); 55 } 56 57 @Override onFinishInflate()58 public void onFinishInflate() { 59 super.onFinishInflate(); 60 LayoutInflater.from(getContext()).inflate(R.layout.adjustable_temperature_view, 61 /* root= */ this); 62 mTempFormat = getResources().getString(R.string.hvac_temperature_format); 63 mMinTempC = getResources().getFloat(R.dimen.hvac_min_value_celsius); 64 mMaxTempC = getResources().getFloat(R.dimen.hvac_max_value_celsius); 65 mNullTempText = getResources().getString(R.string.hvac_null_temp_text); 66 mMinTempText = getResources().getString(R.string.hvac_min_text); 67 mMaxTempText = getResources().getString(R.string.hvac_max_text); 68 initializeButtons(); 69 } 70 71 @Override setHvacController(HvacController controller)72 public void setHvacController(HvacController controller) { 73 mHvacController = controller; 74 } 75 76 @Override setTemp(float tempC)77 public void setTemp(float tempC) { 78 if (mTempTextView == null) { 79 mTempTextView = findViewById(R.id.hvac_temperature_text); 80 } 81 if (Float.isNaN(tempC)) { 82 mTempTextView.setText(mNullTempText); 83 return; 84 } 85 if (tempC <= mMinTempC) { 86 mTempTextView.setText(mMinTempText); 87 mCurrentTempC = mMinTempC; 88 return; 89 } 90 if (tempC >= mMaxTempC) { 91 mTempTextView.setText(mMaxTempText); 92 mCurrentTempC = mMaxTempC; 93 return; 94 } 95 mCurrentTempC = tempC; 96 updateUi(); 97 } 98 99 @Override setDisplayInFahrenheit(boolean displayFahrenheit)100 public void setDisplayInFahrenheit(boolean displayFahrenheit) { 101 mDisplayInFahrenheit = displayFahrenheit; 102 setTemp(mCurrentTempC); 103 } 104 105 @Override getAreaId()106 public int getAreaId() { 107 return mAreaId; 108 } 109 110 @Override onLocaleListChanged()111 public void onLocaleListChanged() { 112 updateUi(); 113 } 114 updateUi()115 private void updateUi() { 116 mTempTextView.setText(String.format(mTempFormat, 117 mDisplayInFahrenheit ? convertToFahrenheit(mCurrentTempC) : mCurrentTempC)); 118 } 119 initializeButtons()120 private void initializeButtons() { 121 findViewById(R.id.hvac_decrease_button).setOnClickListener(v -> { 122 float newTemp = mDisplayInFahrenheit ? convertToCelsius( 123 convertToFahrenheit(mCurrentTempC) - 1) : (mCurrentTempC - 1); 124 setTemperature(newTemp, mAreaId); 125 }); 126 127 findViewById(R.id.hvac_increase_button).setOnClickListener(v -> { 128 float newTemp = mDisplayInFahrenheit ? convertToCelsius( 129 convertToFahrenheit(mCurrentTempC) + 1) : (mCurrentTempC + 1); 130 setTemperature(newTemp, mAreaId); 131 }); 132 } 133 setTemperature(float tempC, int zone)134 private void setTemperature(float tempC, int zone) { 135 if (tempC < mMaxTempC && tempC > mMinTempC && mHvacController != null) { 136 mHvacController.setTemperature(tempC, zone); 137 } 138 } 139 } 140