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.server.display.utils;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.hardware.Sensor;
22 import android.hardware.SensorManager;
23 import android.os.Temperature;
24 import android.text.TextUtils;
25 
26 import com.android.server.display.config.SensorData;
27 
28 import java.util.List;
29 
30 /**
31  * Provides utility methods for dealing with sensors.
32  */
33 public class SensorUtils {
34     public static final int NO_FALLBACK = 0;
35 
36     /**
37      * Finds the specified sensor for SensorData from DisplayDeviceConfig.
38      */
39     @Nullable
findSensor(@ullable SensorManager sensorManager, @Nullable SensorData sensorData, int fallbackType)40     public static Sensor findSensor(@Nullable SensorManager sensorManager,
41             @Nullable SensorData sensorData, int fallbackType) {
42         if (sensorData == null) {
43             return null;
44         } else {
45             return findSensor(sensorManager, sensorData.type, sensorData.name, fallbackType);
46         }
47     }
48     /**
49      * Finds the specified sensor by type and name using SensorManager.
50      */
51     @Nullable
findSensor(@ullable SensorManager sensorManager, @Nullable String sensorType, @Nullable String sensorName, int fallbackType)52     public static Sensor findSensor(@Nullable SensorManager sensorManager,
53             @Nullable String sensorType, @Nullable String sensorName, int fallbackType) {
54         if (sensorManager == null) {
55             return null;
56         }
57         final boolean isNameSpecified = !TextUtils.isEmpty(sensorName);
58         final boolean isTypeSpecified = !TextUtils.isEmpty(sensorType);
59         if (isNameSpecified || isTypeSpecified) {
60             final List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
61             for (Sensor sensor : sensors) {
62                 if ((!isNameSpecified || sensorName.equals(sensor.getName()))
63                         && (!isTypeSpecified || sensorType.equals(sensor.getStringType()))) {
64                     return sensor;
65                 }
66             }
67         }
68         if (fallbackType != NO_FALLBACK) {
69             return sensorManager.getDefaultSensor(fallbackType);
70         }
71 
72         return null;
73     }
74 
75     /**
76      * Convert string temperature type to its corresponding integer value.
77      */
getSensorTemperatureType(@onNull SensorData tempSensor)78     public static int getSensorTemperatureType(@NonNull SensorData tempSensor) {
79         if (tempSensor.type.equalsIgnoreCase(SensorData.TEMPERATURE_TYPE_DISPLAY)) {
80             return Temperature.TYPE_DISPLAY;
81         } else if (tempSensor.type.equalsIgnoreCase(SensorData.TEMPERATURE_TYPE_SKIN)) {
82             return Temperature.TYPE_SKIN;
83         }
84         throw new IllegalArgumentException(
85             "tempSensor doesn't support type: " + tempSensor.type);
86     }
87 
88 }
89