blob: a8515f94517ee760c2bf798a0aa24e2dc3cedc1d [file] [log] [blame]
Brad Stenning38b46f82018-03-27 13:57:29 -07001/*
2 * Copyright (C) 2018 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
17package com.android.systemui.statusbar.car.hvac;
18
Brad Stenning1cd11252018-10-12 07:25:28 -070019import static android.car.VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;
20import static android.car.VehiclePropertyIds.HVAC_TEMPERATURE_DISPLAY_UNITS;
21
Brad Stenning38b46f82018-03-27 13:57:29 -070022import android.car.Car;
Keun young Park9e395bb2019-10-11 20:00:22 -070023import android.car.Car.CarServiceLifecycleListener;
Brad Stenning1cd11252018-10-12 07:25:28 -070024import android.car.VehicleUnit;
Brad Stenning38b46f82018-03-27 13:57:29 -070025import android.car.hardware.CarPropertyValue;
26import android.car.hardware.hvac.CarHvacManager;
27import android.car.hardware.hvac.CarHvacManager.CarHvacEventCallback;
Brad Stenning38b46f82018-03-27 13:57:29 -070028import android.content.Context;
Brad Stenning38b46f82018-03-27 13:57:29 -070029import android.os.Handler;
Brad Stenning38b46f82018-03-27 13:57:29 -070030import android.util.Log;
31
Brad Stenning2f3444f2018-04-18 10:28:24 -070032import java.util.ArrayList;
Brad Stenning38b46f82018-03-27 13:57:29 -070033import java.util.HashMap;
34import java.util.Iterator;
Brad Stenning2f3444f2018-04-18 10:28:24 -070035import java.util.List;
Brad Stenning38b46f82018-03-27 13:57:29 -070036import java.util.Map;
37import java.util.Objects;
38
39/**
40 * Manages the connection to the Car service and delegates value changes to the registered
41 * {@link TemperatureView}s
42 */
43public class HvacController {
44
45 public static final String TAG = "HvacController";
Brad Stenning8d1a51c2018-11-20 17:34:16 -080046 public static final int BIND_TO_HVAC_RETRY_DELAY = 5000;
Brad Stenning38b46f82018-03-27 13:57:29 -070047
48 private Context mContext;
49 private Handler mHandler;
50 private Car mCar;
51 private CarHvacManager mHvacManager;
Brad Stenning2f3444f2018-04-18 10:28:24 -070052 private HashMap<HvacKey, List<TemperatureView>> mTempComponents = new HashMap<>();
Keun young Park9e395bb2019-10-11 20:00:22 -070053
Brad Stenning38b46f82018-03-27 13:57:29 -070054 /**
55 * Callback for getting changes from {@link CarHvacManager} and setting the UI elements to
56 * match.
57 */
58 private final CarHvacEventCallback mHardwareCallback = new CarHvacEventCallback() {
59 @Override
60 public void onChangeEvent(final CarPropertyValue val) {
61 try {
62 int areaId = val.getAreaId();
63 int propertyId = val.getPropertyId();
Brad Stenning2f3444f2018-04-18 10:28:24 -070064 List<TemperatureView> temperatureViews = mTempComponents.get(
Brad Stenning38b46f82018-03-27 13:57:29 -070065 new HvacKey(propertyId, areaId));
Brad Stenning2f3444f2018-04-18 10:28:24 -070066 if (temperatureViews != null && !temperatureViews.isEmpty()) {
Brad Stenning38b46f82018-03-27 13:57:29 -070067 float value = (float) val.getValue();
Brad Stenning2f3444f2018-04-18 10:28:24 -070068 for (TemperatureView tempView : temperatureViews) {
69 tempView.setTemp(value);
70 }
Brad Stenning38b46f82018-03-27 13:57:29 -070071 } // else the data is not of interest
72 } catch (Exception e) {
73 // catch all so we don't take down the sysui if a new data type is
74 // introduced.
75 Log.e(TAG, "Failed handling hvac change event", e);
76 }
77 }
78
79 @Override
80 public void onErrorEvent(final int propertyId, final int zone) {
Brad Stenning8d1a51c2018-11-20 17:34:16 -080081 Log.d(TAG, "HVAC error event, propertyId: " + propertyId
82 + " zone: " + zone);
Brad Stenning38b46f82018-03-27 13:57:29 -070083 }
84 };
Brad Stenning8d1a51c2018-11-20 17:34:16 -080085
Keun young Park9e395bb2019-10-11 20:00:22 -070086 private final CarServiceLifecycleListener mCarServiceLifecycleListener = (car, ready) -> {
87 if (!ready) {
88 return;
89 }
90 try {
91 mHvacManager = (CarHvacManager) car.getCarManager(Car.HVAC_SERVICE);
92 mHvacManager.registerCallback(mHardwareCallback);
93 initComponents();
94 } catch (Exception e) {
95 Log.e(TAG, "Failed to correctly connect to HVAC", e);
Brad Stenning8d1a51c2018-11-20 17:34:16 -080096 }
97 };
98
99 public HvacController(Context context) {
100 mContext = context;
101 }
102
103 /**
104 * Create connection to the Car service. Note: call backs from the Car service
105 * ({@link CarHvacManager}) will happen on the same thread this method was called from.
106 */
107 public void connectToCarService() {
108 mHandler = new Handler();
Keun young Park9e395bb2019-10-11 20:00:22 -0700109 mCar = Car.createCar(mContext, /* handler= */ mHandler, Car.CAR_WAIT_TIMEOUT_DO_NOT_WAIT,
110 mCarServiceLifecycleListener);
Brad Stenning8d1a51c2018-11-20 17:34:16 -0800111 }
112
113 /**
114 * Add component to list and initialize it if the connection is up.
115 */
116 public void addHvacTextView(TemperatureView temperatureView) {
117
118 HvacKey hvacKey = new HvacKey(temperatureView.getPropertyId(), temperatureView.getAreaId());
119 if (!mTempComponents.containsKey(hvacKey)) {
120 mTempComponents.put(hvacKey, new ArrayList<>());
121 }
122 mTempComponents.get(hvacKey).add(temperatureView);
123 initComponent(temperatureView);
124 }
125
126 private void initComponents() {
127 Iterator<Map.Entry<HvacKey, List<TemperatureView>>> iterator =
128 mTempComponents.entrySet().iterator();
129 while (iterator.hasNext()) {
130 Map.Entry<HvacKey, List<TemperatureView>> next = iterator.next();
131 List<TemperatureView> temperatureViews = next.getValue();
132 for (TemperatureView view : temperatureViews) {
133 initComponent(view);
134 }
135 }
136 }
137
138 private void initComponent(TemperatureView view) {
139 int id = view.getPropertyId();
140 int zone = view.getAreaId();
Brad Stenning1cd11252018-10-12 07:25:28 -0700141
Brad Stenning8d1a51c2018-11-20 17:34:16 -0800142 try {
Brad Stenning1cd11252018-10-12 07:25:28 -0700143 if (mHvacManager != null
144 && mHvacManager.isPropertyAvailable(HVAC_TEMPERATURE_DISPLAY_UNITS,
145 VEHICLE_AREA_TYPE_GLOBAL)) {
146 if (mHvacManager.getIntProperty(HVAC_TEMPERATURE_DISPLAY_UNITS,
147 VEHICLE_AREA_TYPE_GLOBAL) == VehicleUnit.FAHRENHEIT) {
148 view.setDisplayInFahrenheit(true);
149 }
150
151 }
Brad Stenning8d1a51c2018-11-20 17:34:16 -0800152 if (mHvacManager == null || !mHvacManager.isPropertyAvailable(id, zone)) {
153 view.setTemp(Float.NaN);
154 return;
155 }
156 view.setTemp(mHvacManager.getFloatProperty(id, zone));
157 } catch (Exception e) {
158 view.setTemp(Float.NaN);
159 Log.e(TAG, "Failed to get value from hvac service", e);
160 }
161 }
Brad Stenning38b46f82018-03-27 13:57:29 -0700162
163 /**
Brad Stenning224b5b32018-03-28 21:26:57 -0700164 * Removes all registered components. This is useful if you need to rebuild the UI since
165 * components self register.
166 */
167 public void removeAllComponents() {
168 mTempComponents.clear();
169 }
170
171 /**
Brad Stenning38b46f82018-03-27 13:57:29 -0700172 * Key for storing {@link TemperatureView}s in a hash map
173 */
174 private static class HvacKey {
175
176 int mPropertyId;
177 int mAreaId;
178
Brad Stenning8d1a51c2018-11-20 17:34:16 -0800179 private HvacKey(int propertyId, int areaId) {
Brad Stenning38b46f82018-03-27 13:57:29 -0700180 mPropertyId = propertyId;
181 mAreaId = areaId;
182 }
183
184 @Override
185 public boolean equals(Object o) {
186 if (this == o) return true;
187 if (o == null || getClass() != o.getClass()) return false;
188 HvacKey hvacKey = (HvacKey) o;
Brad Stenning8d1a51c2018-11-20 17:34:16 -0800189 return mPropertyId == hvacKey.mPropertyId
190 && mAreaId == hvacKey.mAreaId;
Brad Stenning38b46f82018-03-27 13:57:29 -0700191 }
192
193 @Override
194 public int hashCode() {
195 return Objects.hash(mPropertyId, mAreaId);
196 }
197 }
198}