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.util.Log;
19 import com.android.car.hvac.HvacController;
20 import com.android.car.hvac.ui.FanSpeedBar;
21 
22 /**
23  * Controller for the fan speed bar to adjust fan speed.
24  */
25 public class FanSpeedBarController {
26     private final static String TAG = "FanSpeedBarCtrl";
27 
28     private final FanSpeedBar mFanSpeedBar;
29     private final HvacController mHvacController;
30     private int mCurrentFanSpeed;
31 
32     // Note the following are car specific values.
33     private static final int MAX_FAN_SPEED = 6;
34     private static final int MIN_FAN_SPEED = 1;
35 
FanSpeedBarController(FanSpeedBar speedBar, HvacController controller)36     public FanSpeedBarController(FanSpeedBar speedBar, HvacController controller) {
37         mFanSpeedBar = speedBar;
38         mHvacController = controller;
39         initialize();
40     }
41 
initialize()42     private void initialize() {
43         mFanSpeedBar.setFanspeedButtonClickListener(mClickListener);
44         mHvacController.registerCallback(mCallback);
45         // During initialization, we do not need to animate the changes.
46         handleFanSpeedUpdate(mHvacController.getFanSpeed(), false /* animateUpdate */);
47     }
48 
handleFanSpeedUpdate(int speed, boolean animateUpdate)49     private void handleFanSpeedUpdate(int speed, boolean animateUpdate) {
50         if (Log.isLoggable(TAG, Log.DEBUG)) {
51             Log.d(TAG, "Fan speed bar being set to value: " + speed);
52         }
53 
54         mCurrentFanSpeed = speed;
55         if (mCurrentFanSpeed == MIN_FAN_SPEED) {
56             mFanSpeedBar.setOff();
57         } else if (mCurrentFanSpeed >= MAX_FAN_SPEED) {
58             mFanSpeedBar.setMax();
59         } else if (mCurrentFanSpeed < MAX_FAN_SPEED && mCurrentFanSpeed > MIN_FAN_SPEED) {
60             // Note car specific values being used:
61             // The lowest fanspeed is represented by the off button, the first segment
62             // actually represents the second fan speed setting.
63             if (animateUpdate) {
64                 mFanSpeedBar.animateToSpeedSegment(mCurrentFanSpeed - 1);
65             } else {
66                 mFanSpeedBar.setSpeedSegment(mCurrentFanSpeed - 1);
67             }
68         }
69     }
70 
71     private FanSpeedBar.FanSpeedButtonClickListener mClickListener
72             = new FanSpeedBar.FanSpeedButtonClickListener() {
73         @Override
74         public void onMaxButtonClicked() {
75             mHvacController.setFanSpeed(MAX_FAN_SPEED);
76         }
77 
78         @Override
79         public void onOffButtonClicked() {
80             mHvacController.setFanSpeed(MIN_FAN_SPEED);
81         }
82 
83         @Override
84         public void onFanSpeedSegmentClicked(int position) {
85             // Note car specific values being used:
86             // The lowest fanspeed is represented by the off button, the first segment
87             // actually represents the second fan speed setting.
88             mHvacController.setFanSpeed(position + 1);
89         }
90     };
91 
92     private HvacController.Callback mCallback = new HvacController.Callback() {
93         @Override
94         public void onFanSpeedChange(int speed) {
95             handleFanSpeedUpdate(speed, true /* animateUpdate */);
96         }
97     };
98 }
99