1 /* 2 * Copyright (C) 2024 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 android.hardware.devicestate; 18 19 import static android.hardware.devicestate.DeviceStateManager.INVALID_DEVICE_STATE_IDENTIFIER; 20 21 import android.annotation.NonNull; 22 23 import java.util.Iterator; 24 import java.util.List; 25 import java.util.Set; 26 27 /** 28 * Utilities for {@link DeviceStateManager}. 29 * @hide 30 */ 31 public class DeviceStateUtil { DeviceStateUtil()32 private DeviceStateUtil() { } 33 34 /** 35 * Returns the state identifier of the {@link DeviceState} that matches the 36 * {@code currentState}s physical properties. This will return the identifier of the 37 * {@link DeviceState} that matches the devices physical configuration. 38 * 39 * Returns {@link INVALID_DEVICE_STATE_IDENTIFIER} if there is no {@link DeviceState} in the 40 * provided list of {@code supportedStates} that matches. 41 * @hide 42 */ calculateBaseStateIdentifier(@onNull DeviceState currentState, @NonNull List<DeviceState> supportedStates)43 public static int calculateBaseStateIdentifier(@NonNull DeviceState currentState, 44 @NonNull List<DeviceState> supportedStates) { 45 DeviceState.Configuration stateConfiguration = currentState.getConfiguration(); 46 for (int i = 0; i < supportedStates.size(); i++) { 47 DeviceState stateToCompare = supportedStates.get(i); 48 if (stateToCompare.getConfiguration().getPhysicalProperties().isEmpty()) { 49 continue; 50 } 51 if (isDeviceStateMatchingPhysicalProperties(stateConfiguration.getPhysicalProperties(), 52 supportedStates.get(i))) { 53 return supportedStates.get(i).getIdentifier(); 54 } 55 } 56 return INVALID_DEVICE_STATE_IDENTIFIER; 57 } 58 59 /** 60 * Returns if the physical properties provided, matches the same physical properties on the 61 * provided {@link DeviceState}. 62 */ isDeviceStateMatchingPhysicalProperties( Set<@DeviceState.PhysicalDeviceStateProperties Integer> physicalProperties, DeviceState state)63 private static boolean isDeviceStateMatchingPhysicalProperties( 64 Set<@DeviceState.PhysicalDeviceStateProperties Integer> physicalProperties, 65 DeviceState state) { 66 Iterator<@DeviceState.PhysicalDeviceStateProperties Integer> iterator = 67 physicalProperties.iterator(); 68 while (iterator.hasNext()) { 69 if (!state.hasProperty(iterator.next())) { 70 return false; 71 } 72 } 73 return true; 74 } 75 76 } 77