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.controllers;
17 
18 import android.car.hardware.hvac.CarHvacManager;
19 import android.util.Log;
20 import android.util.SparseIntArray;
21 
22 import com.android.car.hvac.HvacController;
23 import com.android.car.hvac.ui.FanDirectionButtons;
24 
25 /**
26  * A controller to handle changes in the fan direction. Also maps fan directions specified
27  * in the {@link FanDirectionButtons} to the {@link CarHvacManager}{@code #FAN_DIRECTION_*}
28  * constants in the vehicle hardware.
29  */
30 public class FanDirectionButtonsController {
31     private static final String TAG = "FanDirectionButtonsController";
32     private final FanDirectionButtons mFanDirectionButtons;
33     private final HvacController mHvacController;
34     private final SparseIntArray mFanDirectionMap =
35             new SparseIntArray(FanDirectionButtons.FAN_DIRECTION_COUNT);
36 
FanDirectionButtonsController(FanDirectionButtons speedBar, HvacController controller)37     public FanDirectionButtonsController(FanDirectionButtons speedBar, HvacController controller) {
38         mFanDirectionButtons = speedBar;
39         mHvacController = controller;
40         initialize();
41     }
42 
initialize()43     private void initialize() {
44         // Note Car specific values are being used here, as not all cars have the floor
45         // and defroster fan direction.
46         mFanDirectionMap.put(FanDirectionButtons.FAN_DIRECTION_FACE,
47                 CarHvacManager.FAN_DIRECTION_FACE);
48         mFanDirectionMap.put(FanDirectionButtons.FAN_DIRECTION_FACE_FLOOR,
49                 (CarHvacManager.FAN_DIRECTION_FACE | CarHvacManager.FAN_DIRECTION_FLOOR));
50         mFanDirectionMap.put(FanDirectionButtons.FAN_DIRECTION_FLOOR,
51                 CarHvacManager.FAN_DIRECTION_FLOOR);
52         mFanDirectionMap.put(FanDirectionButtons.FAN_DIRECTION_FLOOR_DEFROSTER,
53                 (CarHvacManager.FAN_DIRECTION_DEFROST | CarHvacManager.FAN_DIRECTION_FLOOR));
54         mFanDirectionButtons.setFanDirectionClickListener(mListener);
55         mHvacController.registerCallback(mCallback);
56     }
57 
58     private final FanDirectionButtons.FanDirectionClickListener mListener
59             = new FanDirectionButtons.FanDirectionClickListener() {
60         @Override
61         public void onFanDirectionClicked(@FanDirectionButtons.FanDirection int direction) {
62             mHvacController.setFanDirection(mFanDirectionMap.get(direction));
63         }
64     };
65 
66     private HvacController.Callback mCallback = new HvacController.Callback() {
67         @Override
68         public void onFanDirectionChange(int direction) {
69             int index = mFanDirectionMap.indexOfValue(direction);
70             if (index == -1) {
71                 Log.w(TAG, "Unexpected fan direction: " + direction);
72                 return;
73             }
74             mFanDirectionButtons.setFanDirection(mFanDirectionMap.keyAt(index));
75         }
76     };
77 }
78