1 /* 2 * Copyright (C) 2021 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.referenceui; 18 19 import android.annotation.ColorInt; 20 import android.content.Context; 21 import android.content.res.Resources; 22 import android.content.res.TypedArray; 23 import android.graphics.drawable.GradientDrawable; 24 import android.util.AttributeSet; 25 import android.util.Log; 26 import android.view.View; 27 28 import androidx.annotation.Nullable; 29 import androidx.annotation.VisibleForTesting; 30 31 import com.android.systemui.R; 32 import com.android.systemui.car.hvac.TemperatureControlView; 33 34 /** 35 * {@link TemperatureControlView} with an added feature of the temperature control bar background 36 * adjusting based on the temperature set. 37 */ 38 public class BackgroundAdjustingTemperatureControlView extends TemperatureControlView { 39 40 private final static String TAG = "BgAdjTemperatureCtlView"; 41 42 private View mTemperatureBarView; 43 private int[] mUpperLimits; 44 private @ColorInt int[] mTempColors; 45 private int mOffColor; 46 BackgroundAdjustingTemperatureControlView(Context context, @Nullable AttributeSet attrs)47 public BackgroundAdjustingTemperatureControlView(Context context, 48 @Nullable AttributeSet attrs) { 49 super(context, attrs); 50 } 51 52 @Override onFinishInflate()53 public void onFinishInflate() { 54 mTemperatureBarView = findViewById(R.id.hvac_temperature_bar); 55 56 Resources res = getResources(); 57 mUpperLimits = res.getIntArray(R.array.hvac_temperature_control_levels); 58 59 TypedArray colorRes = res.obtainTypedArray(R.array.hvac_temperature_level_backgrounds); 60 mTempColors = new int[colorRes.length()]; 61 for (int i = 0; i < colorRes.length(); i++) { 62 mTempColors[i] = colorRes.getColor(i, 63 res.getColor(R.color.hvac_temperature_default_bg_color, 64 getContext().getTheme())); 65 } 66 colorRes.recycle(); 67 mOffColor = res.getColor(R.color.hvac_temperature_off_text_bg_color, /* theme= */ null); 68 // call super.onFinishInflate() last since it may trigger other methods like 69 // updateTemperatureViewUiThread() which can't execute prior to these fixtures being set 70 super.onFinishInflate(); 71 } 72 73 @Override updateTemperatureViewUiThread()74 protected void updateTemperatureViewUiThread() { 75 mTempTextView.setText(getTempInDisplay()); 76 ((GradientDrawable) mTemperatureBarView.getBackground()).setColor( 77 isTemperatureAvailableForChange() 78 ? getTemperatureColor(getCurrentTempC()) 79 : mOffColor); 80 boolean canChangeTemperature = isTemperatureAvailableForChange(); 81 mIncreaseButton.setVisibility(canChangeTemperature ? View.VISIBLE : View.INVISIBLE); 82 mDecreaseButton.setVisibility(canChangeTemperature ? View.VISIBLE : View.INVISIBLE); 83 } 84 85 @VisibleForTesting getUpperLimits()86 int[] getUpperLimits() { 87 return mUpperLimits; 88 } 89 90 @VisibleForTesting 91 @ColorInt getTempColors()92 int[] getTempColors() { 93 return mTempColors; 94 } 95 96 @VisibleForTesting 97 @ColorInt getTemperatureColor(float temperatureC)98 int getTemperatureColor(float temperatureC) { 99 for (int i = 0; i < mUpperLimits.length; i++) { 100 float upperLimit = mUpperLimits[i] / 10f; 101 int tempColor = mTempColors[i]; 102 if (temperatureC <= upperLimit) { 103 return tempColor; 104 } 105 } 106 Log.w(TAG, "getTemperatureColor: Temperature set is not within the range defined. " 107 + "Returning the last color defined instead."); 108 return mTempColors[mTempColors.length - 1]; 109 } 110 } 111