Filter returned entries and add "binder_" package prerix

Reporting all the Binder services that exist in the device resulted in
excessive growth of the data to handle.

Reduce the data size by introducing a Set that keeps the list if
services being monitored. Every service in the Set is reported
regardless of its availability. For this reason, the new `is_available`
field is also introduced and `interface_descriptor` is reported as an
empty string if the service does not exist.

Test: atest CtsEdiHostTestCases:android.edi.cts.ServiceDeviceInfo#testCollectDeviceInfo
Bug: 397599218
Bug: 383274773
Change-Id: I25d44bc5fa3171bd1266f280fe0f973c82f39ea3
Signed-off-by: Alessio Balsini <balsini@google.com>
diff --git a/hostsidetests/edi/src/android/edi/cts/ServiceDeviceInfo.java b/hostsidetests/edi/src/android/edi/cts/ServiceDeviceInfo.java
index c629db8..1ea372d 100644
--- a/hostsidetests/edi/src/android/edi/cts/ServiceDeviceInfo.java
+++ b/hostsidetests/edi/src/android/edi/cts/ServiceDeviceInfo.java
@@ -21,11 +21,53 @@
 import com.android.tradefed.log.LogUtil.CLog;
 import com.android.tradefed.util.CommandResult;
 
-public class ServiceDeviceInfo extends DeviceInfo {
+import java.util.Set;
 
-    private static final String PACKAGE = "services";
+public class ServiceDeviceInfo extends DeviceInfo {
+    // Add new binder services to be monitored in the following Set
+    private static final Set<String> MONITORED_SERVICES = Set.of("tradeinmode");
+
+    private static final String PACKAGE = "binder_services";
     private static final String NAME = "name";
     private static final String INTERFACE_DESCRIPTOR = "interface_descriptor";
+    private static final String AVAILABILITY = "is_available";
+
+    private static class ServiceEntry {
+        public String name;
+        public String interfaceDescriptor;
+    }
+
+    protected ServiceEntry parseServicesLine(HostInfoStore store, String line) throws Exception {
+        // The first line begins returns the number of services found and can
+        // be skipped. It's the only line string with "Found".
+        if (line.startsWith("Found")) {
+            return null;
+        }
+
+        // Each line has the format:
+        // "<int:line number> <str:service name>: [<interface descriptor>]
+        String[] entries = line.split("\\s+");
+        if (entries.length != 3) {
+            return null;
+        }
+
+        // Remove the last character of the string as it corresponds to a colon.
+        if (entries[1].length() == 0) {
+            return null;
+        }
+
+        ServiceEntry se = new ServiceEntry();
+
+        se.name = entries[1].substring(0, entries[1].length() - 1);
+
+        // Remove the first and last characters of the string as they correspond
+        // to square brackets.
+        if (entries.length > 2 && entries[2].length() > 2) {
+            se.interfaceDescriptor = entries[2].substring(1, entries[2].length() - 1);
+        }
+
+        return se;
+    }
 
     @Override
     protected void collectDeviceInfo(HostInfoStore store) throws Exception {
@@ -51,38 +93,23 @@
 
         store.startArray(PACKAGE);
 
-        // Iterate over every line of the output, each line corresponding to a
-        // service found in the device.
-        for (String line : output.split("\\r?\\n")) {
-            if (line.startsWith("Found")) {
-                // The first line begins returns the number of services found and can
-                // be skipped. It's the only line string with "Found".
-                continue;
-            }
+        for (String name : MONITORED_SERVICES) {
+            ServiceEntry se = null;
+            boolean found = false;
 
-            // Each line has the format:
-            // "<int:line number> <str:service name>: [<interface descriptor>]
-            String[] entries = line.split("\\s+");
-            if (entries.length != 3) {
-                continue;
+            // Iterate over every line of the output, each line corresponding to a
+            // service found in the device.
+            for (String line : output.split("\\r?\\n")) {
+                se = parseServicesLine(store, line);
+                if (se != null && name.equals(se.name)) {
+                    found = true;
+                    break;
+                }
             }
-
-            // Remove the last character of the string as it corresponds to a colon.
-            if (entries[1].length() == 0) {
-                continue;
-            }
-            String name = entries[1].substring(0, entries[1].length() - 1);
-
-            // Remove the first and last characters of the string as they correspond
-            // to square brackets.
-            String interface_descriptor = "";
-            if (entries.length > 2 && entries[2].length() > 2) {
-                interface_descriptor = entries[2].substring(1, entries[2].length() - 1);
-            }
-
             store.startGroup();
             store.addResult(NAME, name);
-            store.addResult(INTERFACE_DESCRIPTOR, interface_descriptor);
+            store.addResult(INTERFACE_DESCRIPTOR, found ? se.interfaceDescriptor : "");
+            store.addResult(AVAILABILITY, found);
             store.endGroup();
         }