Always attempt grant/revoke for Automotive builds.

The UI representation of the granted state in Automotive does not use
checked state. As a result, the test logic always assumed permissions
were granted causing expected state failures later on.

Bug: 140733948

Test: run cts-dev -m CtsAppSecurityHostTestCases -t android.appsecurity.cts.PermissionsHostTest

Change-Id: I61c94434185ef21f78875c48a81a344f1fc212a7
diff --git a/hostsidetests/appsecurity/test-apps/UsePermissionApp23/src/com/android/cts/usepermission/BasePermissionsTest.java b/hostsidetests/appsecurity/test-apps/UsePermissionApp23/src/com/android/cts/usepermission/BasePermissionsTest.java
index b2f9f3f..18a692f 100755
--- a/hostsidetests/appsecurity/test-apps/UsePermissionApp23/src/com/android/cts/usepermission/BasePermissionsTest.java
+++ b/hostsidetests/appsecurity/test-apps/UsePermissionApp23/src/com/android/cts/usepermission/BasePermissionsTest.java
@@ -389,6 +389,14 @@
 
         waitForIdle();
 
+        // Dismiss the DeprecatedTargetSdkVersionDialog if shown.
+        String okLabel = mContext.getResources().getString(android.R.string.ok);
+        UiObject2 button = getUiDevice().findObject(By.text(okLabel));
+        if (button != null) {
+            button.click();
+            waitForIdle();
+        }
+
         // Open the permissions UI
         String label = mContext.getResources().getString(R.string.Permissions);
         AccessibilityNodeInfo permLabelView = getNodeTimed(() -> findByText(label), true);
@@ -423,7 +431,8 @@
 
             final boolean wasGranted = !getUiDevice().wait(Until.findObject(By.text(denyLabel)),
                     GLOBAL_TIMEOUT_MILLIS).isChecked();
-            if (granted != wasGranted) {
+            // Automotive does not use checked state to represent granted state.
+            if (granted != wasGranted || isAutomotive()) {
                 // Toggle the permission
 
                 if (granted) {
@@ -434,7 +443,7 @@
                 }
                 waitForIdle();
 
-                if (wasGranted && legacyApp) {
+                if (!granted && legacyApp) {
                     scrollToBottomIfWatch();
                     Context context = getInstrumentation().getContext();
                     String packageName = context.getPackageManager()
@@ -644,4 +653,9 @@
         return getInstrumentation().getContext().getPackageManager()
                 .hasSystemFeature(PackageManager.FEATURE_LEANBACK);
     }
+
+    private static boolean isAutomotive() {
+        return getInstrumentation().getContext().getPackageManager()
+                .hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE);
+    }
  }