Add method to retrieve the current appop mode

Bug: 111551990
Test: atest AdaptiveBatteryTest
Change-Id: Ib152ede1ec2a33c152dc1768776e11a8cc9d28c5
diff --git a/common/device-side/util/src/com/android/compatibility/common/util/AppOpsUtils.java b/common/device-side/util/src/com/android/compatibility/common/util/AppOpsUtils.java
index 20c5b9e..939d6da 100644
--- a/common/device-side/util/src/com/android/compatibility/common/util/AppOpsUtils.java
+++ b/common/device-side/util/src/com/android/compatibility/common/util/AppOpsUtils.java
@@ -74,6 +74,25 @@
     }
 
     /**
+     * Get the app op mode (e.g. MODE_ALLOWED, MODE_DEFAULT) for a single package and operation.
+     */
+    public static int getOpMode(String packageName, String opStr)
+            throws IOException {
+        String opState = getOpState(packageName, opStr);
+        if (opState.contains(" allow")) {
+            return MODE_ALLOWED;
+        } else if (opState.contains(" deny")) {
+            return MODE_ERRORED;
+        } else if (opState.contains(" ignore")) {
+            return MODE_IGNORED;
+        } else if (opState.contains(" default")) {
+            return MODE_DEFAULT;
+        } else {
+            throw new IllegalStateException("Unexpected app op mode returned " + opState);
+        }
+    }
+
+    /**
      * Returns whether an allowed operation has been logged by the AppOpsManager for a
      * package. Operations are noted when the app attempts to perform them and calls e.g.
      * {@link AppOpsManager#noteOperation}.
@@ -86,7 +105,7 @@
     }
 
     /**
-     * Returns whether an allowed operation has been logged by the AppOpsManager for a
+     * Returns whether a rejected operation has been logged by the AppOpsManager for a
      * package. Operations are noted when the app attempts to perform them and calls e.g.
      * {@link AppOpsManager#noteOperation}.
      *
@@ -110,4 +129,4 @@
     private static String runCommand(String command) throws IOException {
         return SystemUtil.runShellCommand(InstrumentationRegistry.getInstrumentation(), command);
     }
-}
\ No newline at end of file
+}