Improve the dropbox proto dump logic

* Determine dropbox proto dump support through help menu. Previously we determine whether a device support dumping dropbox entries in proto format by attempting to call the method and catch exception. A better way is to directly ask the device by calling the --help command.
* Skip empty proto.
* Further increase the timeout value.
* Keeps falling back to studout parsing when proto parse error happened for truncated proto dumps or incompatible platform versions.

Test: atest + local
Change-Id: I47958b546a607bade23b5638c38aa25ab1ca1df7
diff --git a/harness/src/main/java/com/android/csuite/core/DeviceUtils.java b/harness/src/main/java/com/android/csuite/core/DeviceUtils.java
index 118ed1c..ba8cc50 100644
--- a/harness/src/main/java/com/android/csuite/core/DeviceUtils.java
+++ b/harness/src/main/java/com/android/csuite/core/DeviceUtils.java
@@ -634,6 +634,26 @@
      * @throws IOException when failed to dump or read the dropbox protos.
      */
     public List<DropboxEntry> getDropboxEntries(Set<String> tags) throws IOException {
+        CommandResult resHelp =
+                mRunUtilProvider
+                        .get()
+                        .runTimedCmd(
+                                1L * 60 * 1000,
+                                "sh",
+                                "-c",
+                                String.format(
+                                        "adb -s %s shell dumpsys dropbox --help",
+                                        mDevice.getSerialNumber()));
+        if (resHelp.getStatus() != CommandStatus.SUCCESS) {
+            throw new IOException("Dropbox dump help command failed: " + resHelp);
+        }
+        if (!resHelp.getStdout().contains("--proto")) {
+            // If dumping proto format is not supported such as in Android 10, the command will
+            // still succeed with exit code 0 and output strings instead of protobuf bytes,
+            // causing parse error. In this case we fallback to dumping dropbox --print option.
+            return getDropboxEntriesFromStdout(tags);
+        }
+
         List<DropboxEntry> entries = new ArrayList<>();
 
         for (String tag : tags) {
@@ -643,24 +663,30 @@
                     mRunUtilProvider
                             .get()
                             .runTimedCmd(
-                                    12L * 1000,
+                                    4L * 60 * 1000,
                                     "sh",
                                     "-c",
                                     String.format(
                                             "adb -s %s shell dumpsys dropbox --proto %s > %s",
                                             mDevice.getSerialNumber(), tag, dumpFile));
-
             if (res.getStatus() != CommandStatus.SUCCESS) {
                 throw new IOException("Dropbox dump command failed: " + res);
             }
 
+            if (Files.size(dumpFile) == 0) {
+                CLog.d("Skipping empty proto " + dumpFile);
+                continue;
+            }
+
+            CLog.d("Parsing proto for tag %s. Size: %s", tag, Files.size(dumpFile));
             DropBoxManagerServiceDumpProto proto;
             try {
                 proto = DropBoxManagerServiceDumpProto.parseFrom(Files.readAllBytes(dumpFile));
             } catch (InvalidProtocolBufferException e) {
-                // If dumping proto format is not supported such as in Android 10, the command will
-                // still succeed with exit code 0 and output strings instead of protobuf bytes,
-                // causing parse error. In this case we fallback to dumping dropbox --print option.
+                CLog.e(
+                        "Falling back to stdout dropbox dump due to unexpected proto parse error:"
+                                + " %s",
+                        e);
                 return getDropboxEntriesFromStdout(tags);
             }
             Files.delete(dumpFile);
@@ -746,7 +772,7 @@
                 mRunUtilProvider
                         .get()
                         .runTimedCmd(
-                                6000,
+                                4L * 60 * 1000,
                                 "sh",
                                 "-c",
                                 String.format(
@@ -775,7 +801,7 @@
                 mRunUtilProvider
                         .get()
                         .runTimedCmd(
-                                6000,
+                                4L * 60 * 1000,
                                 "sh",
                                 "-c",
                                 String.format(
diff --git a/harness/src/test/java/com/android/csuite/core/DeviceUtilsTest.java b/harness/src/test/java/com/android/csuite/core/DeviceUtilsTest.java
index edb2d39..f635c20 100644
--- a/harness/src/test/java/com/android/csuite/core/DeviceUtilsTest.java
+++ b/harness/src/test/java/com/android/csuite/core/DeviceUtilsTest.java
@@ -826,6 +826,12 @@
                         Mockito.anyLong(),
                         Mockito.eq("sh"),
                         Mockito.eq("-c"),
+                        Mockito.contains("dumpsys dropbox --help")))
+                .thenReturn(createSuccessfulCommandResultWithStdout("--proto"));
+        when(mRunUtil.runTimedCmd(
+                        Mockito.anyLong(),
+                        Mockito.eq("sh"),
+                        Mockito.eq("-c"),
                         Mockito.contains("dumpsys dropbox --proto")))
                 .thenReturn(createSuccessfulCommandResultWithStdout(""));
 
@@ -836,6 +842,12 @@
 
     @Test
     public void getDropboxEntries_entryExists_returnsEntry() throws Exception {
+        when(mRunUtil.runTimedCmd(
+                        Mockito.anyLong(),
+                        Mockito.eq("sh"),
+                        Mockito.eq("-c"),
+                        Mockito.contains("dumpsys dropbox --help")))
+                .thenReturn(createSuccessfulCommandResultWithStdout("--proto"));
         Path dumpFile = Files.createTempFile(mFileSystem.getPath("/"), "dropbox", ".proto");
         long time = 123;
         String data = "abc";