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.Nullable; 20 import android.hardware.Sensor; 21 import android.hardware.SensorManager; 22 import android.text.TextUtils; 23 24 import com.android.server.display.DisplayDeviceConfig; 25 26 import java.util.List; 27 28 /** 29 * Provides utility methods for dealing with sensors. 30 */ 31 public class SensorUtils { 32 public static final int NO_FALLBACK = 0; 33 34 /** 35 * Finds the specified sensor for SensorData from DisplayDeviceConfig. 36 */ 37 @Nullable findSensor(@ullable SensorManager sensorManager, @Nullable DisplayDeviceConfig.SensorData sensorData, int fallbackType)38 public static Sensor findSensor(@Nullable SensorManager sensorManager, 39 @Nullable DisplayDeviceConfig.SensorData sensorData, int fallbackType) { 40 if (sensorData == null) { 41 return null; 42 } else { 43 return findSensor(sensorManager, sensorData.type, sensorData.name, fallbackType); 44 } 45 } 46 /** 47 * Finds the specified sensor by type and name using SensorManager. 48 */ 49 @Nullable findSensor(@ullable SensorManager sensorManager, @Nullable String sensorType, @Nullable String sensorName, int fallbackType)50 public static Sensor findSensor(@Nullable SensorManager sensorManager, 51 @Nullable String sensorType, @Nullable String sensorName, int fallbackType) { 52 if (sensorManager == null) { 53 return null; 54 } 55 final boolean isNameSpecified = !TextUtils.isEmpty(sensorName); 56 final boolean isTypeSpecified = !TextUtils.isEmpty(sensorType); 57 if (isNameSpecified || isTypeSpecified) { 58 final List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL); 59 for (Sensor sensor : sensors) { 60 if ((!isNameSpecified || sensorName.equals(sensor.getName())) 61 && (!isTypeSpecified || sensorType.equals(sensor.getStringType()))) { 62 return sensor; 63 } 64 } 65 } 66 if (fallbackType != NO_FALLBACK) { 67 return sensorManager.getDefaultSensor(fallbackType); 68 } 69 70 return null; 71 } 72 73 } 74