Revert "Update CTS tests to use the updated DeviceStateManager A..."

Revert submission 26228978-device-state-callbacks

Reason for revert: <Potential culprit for b/325379002  - verifying through ABTD before revert submission. This is part of the standard investigation process, and does not mean your CL will be reverted.>

Reverted changes: /q/submissionid:26228978-device-state-callbacks

Change-Id: Iebe6915702e42e9b54708896b0248801984d47e0
diff --git a/tests/devicestate/src/android/hardware/devicestate/cts/DeviceStateManagerTests.java b/tests/devicestate/src/android/hardware/devicestate/cts/DeviceStateManagerTests.java
index 6848422..2b3415a 100644
--- a/tests/devicestate/src/android/hardware/devicestate/cts/DeviceStateManagerTests.java
+++ b/tests/devicestate/src/android/hardware/devicestate/cts/DeviceStateManagerTests.java
@@ -16,9 +16,8 @@
 
 package android.hardware.devicestate.cts;
 
-import static android.hardware.devicestate.DeviceStateManager.MAXIMUM_DEVICE_STATE_IDENTIFIER;
-import static android.hardware.devicestate.DeviceStateManager.MINIMUM_DEVICE_STATE_IDENTIFIER;
-import static android.server.wm.DeviceStateUtils.assertValidDeviceState;
+import static android.hardware.devicestate.DeviceStateManager.MAXIMUM_DEVICE_STATE;
+import static android.hardware.devicestate.DeviceStateManager.MINIMUM_DEVICE_STATE;
 import static android.server.wm.DeviceStateUtils.assertValidState;
 import static android.server.wm.DeviceStateUtils.runWithControlDeviceStatePermission;
 import static android.view.Display.DEFAULT_DISPLAY;
@@ -33,12 +32,15 @@
 import static org.mockito.Mockito.timeout;
 import static org.mockito.Mockito.verify;
 
-import android.hardware.devicestate.DeviceState;
+import android.content.Context;
+import android.content.res.Resources;
 import android.hardware.devicestate.DeviceStateManager;
 import android.hardware.devicestate.DeviceStateRequest;
-import android.util.ArraySet;
+import android.server.wm.jetpack.extensions.util.ExtensionsUtil;
+import android.server.wm.jetpack.extensions.util.Version;
 
 import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.platform.app.InstrumentationRegistry;
 
 import com.android.compatibility.common.util.ApiTest;
 import com.android.compatibility.common.util.PollingCheck;
@@ -48,7 +50,6 @@
 import org.mockito.ArgumentCaptor;
 
 import java.util.HashSet;
-import java.util.List;
 import java.util.Set;
 import java.util.concurrent.Executor;
 
@@ -60,16 +61,15 @@
 
     private static final int INVALID_DEVICE_STATE = -1;
 
+    /** Vendor extension version. Some API behaviors are only available in newer version. */
+    private static final Version WM_EXTENSION_VERSION = ExtensionsUtil.getExtensionVersion();
+
     /**
      * Tests that {@link DeviceStateManager#getSupportedStates()} returns at least one state and
      * that none of the returned states are in the range
-     * [{@link #MINIMUM_DEVICE_STATE_IDENTIFIER}, {@link #MAXIMUM_DEVICE_STATE_IDENTIFIER}].
+     * [{@link #MINIMUM_DEVICE_STATE}, {@link #MAXIMUM_DEVICE_STATE}].
      */
-    @ApiTest(apis = {
-            "android.hardware.devicestate.DeviceStateManager#getSupportedStates",
-            "android.hardware.devicestate.DeviceStateManager#getSupportedDeviceStates",
-            "android.hardware.devicestate.DeviceState#getIdentifier",
-            "android.hardware.devicestate.DeviceState#getName"})
+    @ApiTest(apis = {"android.hardware.devicestate.DeviceStateManager#getSupportedStates"})
     @Test
     public void testValidSupportedStates() throws Exception {
         final int[] supportedStates = getDeviceStateManager().getSupportedStates();
@@ -79,13 +79,6 @@
             final int state = supportedStates[i];
             assertValidState(state);
         }
-
-        final List<DeviceState> supportedDeviceStates =
-                getDeviceStateManager().getSupportedDeviceStates();
-
-        for (DeviceState state: supportedDeviceStates) {
-            assertValidDeviceState(state);
-        }
     }
 
     /**
@@ -94,27 +87,24 @@
      * triggered with a value equal to the requested state.
      */
     @ApiTest(apis = {
-            "android.hardware.devicestate.DeviceStateManager#getSupportedDeviceStates",
-            "android.hardware.devicestate.DeviceStateManager#requestState",
-            "android.hardware.devicestate.DeviceState#getIdentifier"})
+            "android.hardware.devicestate.DeviceStateManager#getSupportedStates",
+            "android.hardware.devicestate.DeviceStateManager#requestState"})
     @Test
     public void testRequestAllSupportedStates() throws Throwable {
-        final ArgumentCaptor<DeviceState> intAgumentCaptor = ArgumentCaptor.forClass(
-                DeviceState.class);
+        final ArgumentCaptor<Integer> intAgumentCaptor = ArgumentCaptor.forClass(Integer.class);
         final DeviceStateManager.DeviceStateCallback callback
                 = mock(DeviceStateManager.DeviceStateCallback.class);
         final DeviceStateManager manager = getDeviceStateManager();
         manager.registerCallback(Runnable::run, callback);
 
-        final List<DeviceState> supportedStates = manager.getSupportedDeviceStates();
-        for (int i = 0; i < supportedStates.size(); i++) {
-            final int stateToRequest = supportedStates.get(i).getIdentifier();
-            final DeviceStateRequest request =
-                    DeviceStateRequest.newBuilder(stateToRequest).build();
+        final int[] supportedStates = manager.getSupportedStates();
+        for (int i = 0; i < supportedStates.length; i++) {
+            final DeviceStateRequest request
+                    = DeviceStateRequest.newBuilder(supportedStates[i]).build();
 
             runWithRequestActive(request, false, () -> {
-                verify(callback, atLeastOnce()).onDeviceStateChanged(intAgumentCaptor.capture());
-                assertEquals(intAgumentCaptor.getValue().getIdentifier(), request.getState());
+                verify(callback, atLeastOnce()).onStateChanged(intAgumentCaptor.capture());
+                assertEquals(intAgumentCaptor.getValue().intValue(), request.getState());
             });
         }
     }
@@ -136,28 +126,28 @@
     /**
      * Tests that calling {@link DeviceStateManager#requestState(DeviceStateRequest, Executor,
      * DeviceStateRequest.Callback)} throws an {@link java.lang.IllegalArgumentException} if
-     * supplied with a state above {@link MAXIMUM_DEVICE_STATE_IDENTIFIER}.
+     * supplied with a state above {@link MAXIMUM_DEVICE_STATE}.
      */
     @ApiTest(apis = {"android.hardware.devicestate.DeviceStateManager#requestState"})
     @Test(expected = IllegalArgumentException.class)
     public void testRequestStateTooLarge() throws Throwable {
         final DeviceStateManager manager = getDeviceStateManager();
-        final DeviceStateRequest request =
-                DeviceStateRequest.newBuilder(MAXIMUM_DEVICE_STATE_IDENTIFIER + 1).build();
+        final DeviceStateRequest request
+                = DeviceStateRequest.newBuilder(MAXIMUM_DEVICE_STATE + 1).build();
         runWithControlDeviceStatePermission(() -> manager.requestState(request, null, null));
     }
 
     /**
      * Tests that calling {@link DeviceStateManager#requestState(DeviceStateRequest, Executor,
      * DeviceStateRequest.Callback)} throws an {@link java.lang.IllegalArgumentException} if
-     * supplied with a state below {@link MINIMUM_DEVICE_STATE_IDENTIFIER}.
+     * supplied with a state below {@link MINIMUM_DEVICE_STATE}.
      */
     @ApiTest(apis = {"android.hardware.devicestate.DeviceStateManager#requestState"})
     @Test(expected = IllegalArgumentException.class)
     public void testRequestStateTooSmall() throws Throwable {
         final DeviceStateManager manager = getDeviceStateManager();
-        final DeviceStateRequest request =
-                DeviceStateRequest.newBuilder(MINIMUM_DEVICE_STATE_IDENTIFIER - 1).build();
+        final DeviceStateRequest request
+                = DeviceStateRequest.newBuilder(MINIMUM_DEVICE_STATE - 1).build();
         runWithControlDeviceStatePermission(() -> manager.requestState(request, null, null));
     }
 
@@ -166,20 +156,19 @@
      * DeviceStateRequest.Callback)} is not successful and results in a failure to change the
      * state of the device due to the state requested not being available for apps to request.
      */
-    @ApiTest(apis = {
-            "android.hardware.devicestate.DeviceStateManager#requestState",
-            "android.hardware.devicestate.DeviceState#hasProperty"})
+    @ApiTest(apis = {"android.hardware.devicestate.DeviceStateManager#requestState"})
     @Test
     public void testRequestStateFailsAsTopApp_ifStateNotDefinedAsAvailableForAppsToRequest()
             throws IllegalArgumentException {
         final DeviceStateManager manager = getDeviceStateManager();
-        final List<DeviceState> supportedStates = manager.getSupportedDeviceStates();
+        final int[] supportedStates = manager.getSupportedStates();
         // We want to verify that the app can change device state
         // So we only attempt if there are more than 1 possible state.
-        assumeTrue(supportedStates.size() > 1);
-        Set<Integer> statesAvailableToRequest = getAvailableStatesToRequest(supportedStates);
+        assumeTrue(supportedStates.length > 1);
+        Set<Integer> statesAvailableToRequest = getAvailableStatesToRequest(
+                InstrumentationRegistry.getInstrumentation().getTargetContext(), supportedStates);
         // checks that not every state is available for an app to request
-        assumeTrue(statesAvailableToRequest.size() < supportedStates.size());
+        assumeTrue(statesAvailableToRequest.size() < supportedStates.length);
 
         Set<Integer> availableDeviceStates = generateDeviceStateSet(supportedStates);
 
@@ -213,23 +202,21 @@
      * DeviceStateRequest.Callback)} is successful and results in a registered callback being
      * triggered with a value equal to the requested state.
      */
-    @ApiTest(apis = {
-            "android.hardware.devicestate.DeviceStateManager#requestState",
-            "android.hardware.devicestate.DeviceStateManager#cancelStateRequest",
-            "android.hardware.devicestate.DeviceState#hasProperty"})
+    @ApiTest(apis = {"android.hardware.devicestate.DeviceStateManager#requestState"})
     @Test
     public void testRequestStateSucceedsAsTopApp_ifStateDefinedAsAvailableForAppsToRequest()
             throws Throwable {
         final DeviceStateManager manager = getDeviceStateManager();
-        final List<DeviceState> supportedStates = manager.getSupportedDeviceStates();
+        final int[] supportedStates = manager.getSupportedStates();
 
         // We want to verify that the app can change device state
         // So we only attempt if there are more than 1 possible state.
-        assumeTrue(supportedStates.size() > 1);
-        final Set<Integer> statesAvailableToRequest = getAvailableStatesToRequest(supportedStates);
-        assumeFalse(statesAvailableToRequest.isEmpty());
+        assumeTrue(supportedStates.length > 1);
+        Set<Integer> statesAvailableToRequest = getAvailableStatesToRequest(
+                InstrumentationRegistry.getInstrumentation().getTargetContext(), supportedStates);
+        assumeTrue(statesAvailableToRequest.size() > 0);
 
-        final Set<Integer> availableDeviceStates = generateDeviceStateSet(supportedStates);
+        Set<Integer> availableDeviceStates = generateDeviceStateSet(supportedStates);
 
         final StateTrackingCallback callback = new StateTrackingCallback();
         manager.registerCallback(Runnable::run, callback);
@@ -242,9 +229,9 @@
                 DEFAULT_DISPLAY
         );
 
-        final DeviceStateTestActivity activity = activitySession.getActivity();
+        DeviceStateTestActivity activity = activitySession.getActivity();
 
-        final Set<Integer> possibleStates = possibleStates(true /* shouldSucceed */,
+        Set<Integer> possibleStates = possibleStates(true /* shouldSucceed */,
                 availableDeviceStates,
                 statesAvailableToRequest);
         int nextState = calculateDifferentState(callback.mCurrentState, possibleStates);
@@ -268,20 +255,19 @@
      * Tests that calling {@link DeviceStateManager#requestState} is unsuccessful and results in a
      * failure to update the state of the device as expected since the activity is backgrounded.
      */
-    @ApiTest(apis = {
-            "android.hardware.devicestate.DeviceStateManager#requestState",
-            "android.hardware.devidestate.DeviceState#hasProperty" })
+    @ApiTest(apis = {"android.hardware.devicestate.DeviceStateManager#requestState"})
     @Test
     public void testRequestStateFailsAsBackgroundApp() throws IllegalArgumentException {
         final DeviceStateManager manager = getDeviceStateManager();
-        final List<DeviceState> supportedStates = manager.getSupportedDeviceStates();
+        final int[] supportedStates = manager.getSupportedStates();
         // We want to verify that the app can change device state
         // So we only attempt if there are more than 1 possible state.
-        assumeTrue(supportedStates.size() > 1);
-        final Set<Integer> statesAvailableToRequest = getAvailableStatesToRequest(supportedStates);
-        assumeFalse(statesAvailableToRequest.isEmpty());
+        assumeTrue(supportedStates.length > 1);
+        Set<Integer> statesAvailableToRequest = getAvailableStatesToRequest(
+                InstrumentationRegistry.getInstrumentation().getTargetContext(), supportedStates);
+        assumeTrue(statesAvailableToRequest.size() > 0);
 
-        final Set<Integer> availableDeviceStates = generateDeviceStateSet(supportedStates);
+        Set<Integer> availableDeviceStates = generateDeviceStateSet(supportedStates);
 
         final StateTrackingCallback callback = new StateTrackingCallback();
         manager.registerCallback(Runnable::run, callback);
@@ -294,12 +280,12 @@
                 DEFAULT_DISPLAY
         );
 
-        final DeviceStateTestActivity activity = activitySession.getActivity();
+        DeviceStateTestActivity activity = activitySession.getActivity();
         assertFalse(activity.requestStateFailed);
 
         launchHomeActivity(); // places our test activity in the background
 
-        final Set<Integer> possibleStates = possibleStates(true /* shouldSucceed */,
+        Set<Integer> possibleStates = possibleStates(true /* shouldSucceed */,
                 availableDeviceStates,
                 statesAvailableToRequest);
         int nextState = calculateDifferentState(callback.mCurrentState, possibleStates);
@@ -319,14 +305,15 @@
     @Test
     public void testCancelStateRequestFromNewActivity() throws Throwable {
         final DeviceStateManager manager = getDeviceStateManager();
-        final List<DeviceState> supportedStates = manager.getSupportedDeviceStates();
+        final int[] supportedStates = manager.getSupportedStates();
         // We want to verify that the app can change device state
         // So we only attempt if there are more than 1 possible state.
-        assumeTrue(supportedStates.size() > 1);
-        final Set<Integer> statesAvailableToRequest = getAvailableStatesToRequest(supportedStates);
+        assumeTrue(supportedStates.length > 1);
+        Set<Integer> statesAvailableToRequest = getAvailableStatesToRequest(
+                InstrumentationRegistry.getInstrumentation().getTargetContext(), supportedStates);
         assumeFalse(statesAvailableToRequest.isEmpty());
 
-        final Set<Integer> availableDeviceStates = generateDeviceStateSet(supportedStates);
+        Set<Integer> availableDeviceStates = generateDeviceStateSet(supportedStates);
 
         final StateTrackingCallback callback = new StateTrackingCallback();
         manager.registerCallback(Runnable::run, callback);
@@ -343,7 +330,7 @@
 
         int originalState = callback.mCurrentState;
 
-        final Set<Integer> possibleStates = possibleStates(true /* shouldSucceed */,
+        Set<Integer> possibleStates = possibleStates(true /* shouldSucceed */,
                 availableDeviceStates,
                 statesAvailableToRequest);
         int nextState = calculateDifferentState(callback.mCurrentState, possibleStates);
@@ -382,21 +369,41 @@
 
 
     /**
-     * Returns a set of device states that are available to be requested by an application.
+     * Reads in the states that are available to be requested by apps from the configuration file
+     * and returns a set of all valid states that are read in.
      *
+     * @param context         The context used to get the configuration values from
+     *                        {@link Resources}
      * @param supportedStates The device states that are supported on that device.
      * @return {@link Set} of valid device states that are read in.
      */
-    private static Set<Integer> getAvailableStatesToRequest(List<DeviceState> supportedStates) {
-        final Set<Integer> availableStatesToRequest = new HashSet<>();
-        for (DeviceState state : supportedStates) {
-            if (state.hasProperty(DeviceState.PROPERTY_POLICY_AVAILABLE_FOR_APP_REQUEST)) {
-                availableStatesToRequest.add(state.getIdentifier());
+    private static Set<Integer> getAvailableStatesToRequest(Context context,
+            int[] supportedStates) {
+        Set<Integer> availableStatesToRequest = new HashSet<>();
+        String[] availableStateIdentifiers = context.getResources().getStringArray(
+                Resources.getSystem().getIdentifier("config_deviceStatesAvailableForAppRequests",
+                        "array",
+                        "android"));
+        for (String identifier : availableStateIdentifiers) {
+            int stateIdentifier = context.getResources()
+                    .getIdentifier(identifier, "integer", "android");
+            int state = context.getResources().getInteger(stateIdentifier);
+            if (isValidState(state, supportedStates)) {
+                availableStatesToRequest.add(context.getResources().getInteger(stateIdentifier));
             }
         }
         return availableStatesToRequest;
     }
 
+    private static boolean isValidState(int state, int[] supportedStates) {
+        for (int i = 0; i < supportedStates.length; i++) {
+            if (state == supportedStates[i]) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     /**
      * Generates a set of possible device states based on a {@link Set} of valid device states,
      * {@code supportedDeviceStates}, and the set of device states available to be requested
@@ -422,7 +429,7 @@
             throw new IllegalArgumentException("Available states include invalid device states");
         }
 
-        final Set<Integer> availableStates = new HashSet<>(supportedDeviceStates);
+        Set<Integer> availableStates = new HashSet<>(supportedDeviceStates);
 
         if (shouldSucceed) {
             availableStates.retainAll(availableStatesToRequest);
@@ -464,10 +471,10 @@
      *
      * @param states Device states that are supported on the device
      */
-    private static Set<Integer> generateDeviceStateSet(List<DeviceState> states) {
-        Set<Integer> supportedStates = new ArraySet<>();
-        for (DeviceState state: states) {
-            supportedStates.add(state.getIdentifier());
+    private static Set<Integer> generateDeviceStateSet(int[] states) {
+        Set<Integer> supportedStates = new HashSet<>();
+        for (int i = 0; i < states.length; i++) {
+            supportedStates.add(states[i]);
         }
         return supportedStates;
     }
diff --git a/tests/framework/base/windowmanager/util/src/android/server/wm/DeviceStateUtils.java b/tests/framework/base/windowmanager/util/src/android/server/wm/DeviceStateUtils.java
index 53e421f..55d0926 100644
--- a/tests/framework/base/windowmanager/util/src/android/server/wm/DeviceStateUtils.java
+++ b/tests/framework/base/windowmanager/util/src/android/server/wm/DeviceStateUtils.java
@@ -16,14 +16,11 @@
 
 package android.server.wm;
 
-import static android.hardware.devicestate.DeviceStateManager.MAXIMUM_DEVICE_STATE_IDENTIFIER;
-import static android.hardware.devicestate.DeviceStateManager.MINIMUM_DEVICE_STATE_IDENTIFIER;
+import static android.hardware.devicestate.DeviceStateManager.MAXIMUM_DEVICE_STATE;
+import static android.hardware.devicestate.DeviceStateManager.MINIMUM_DEVICE_STATE;
 
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
-import android.hardware.devicestate.DeviceState;
-
 import androidx.annotation.NonNull;
 
 import com.android.compatibility.common.util.SystemUtil;
@@ -58,22 +55,8 @@
      * [{@link MINIMUM_DEVICE_STATE}, {@link MAXIMUM_DEVICE_STATE}].
      */
     public static void assertValidState(int state) {
-        assertTrue("Device state identifier is smaller than the minimum state identifier value",
-                state >= MINIMUM_DEVICE_STATE_IDENTIFIER);
-        assertTrue("Device state identifier is larger than the maximum state identifier value",
-                state <= MAXIMUM_DEVICE_STATE_IDENTIFIER);
-    }
-
-    /**
-     * asserts that the provided {@code state} has an identifier in the range
-     * [{@link MINIMUM_DEVICE_STATE}, {@link MAXIMUM_DEVICE_STATE}] and a non-null {@code name}.
-     */
-    public static void assertValidDeviceState(DeviceState state) {
-        assertTrue("Device state identifier is smaller than the minimum state identifier value",
-                state.getIdentifier() >= MINIMUM_DEVICE_STATE_IDENTIFIER);
-        assertTrue("Device state identifier is larger than the maximum state identifier value",
-                state.getIdentifier() <= MAXIMUM_DEVICE_STATE_IDENTIFIER);
-        assertNotNull("Device state name was null", state.getName());
+        assertTrue(state >= MINIMUM_DEVICE_STATE);
+        assertTrue(state <= MAXIMUM_DEVICE_STATE);
     }
 
     private DeviceStateUtils() {}