Pass SurfaceTrace through WM command in binary format.

Maybe we could add a pretty printer later, this is mostly,
intended for consumption by the test framework though, so for
now we defer parsing until that part.

Test: cts-tradefed run singleCommand cts -o --module CtsWindowManagerHostTestCases --test android.server.cts.SurfaceViewMovementTests#testSurfaceMovesWithParent
Change-Id: I75add6a9644cd4bbbb9d06aab97b9bf9f016655b
diff --git a/cmds/wm/src/com/android/commands/wm/Wm.java b/cmds/wm/src/com/android/commands/wm/Wm.java
index b46cd67..84fb626 100644
--- a/cmds/wm/src/com/android/commands/wm/Wm.java
+++ b/cmds/wm/src/com/android/commands/wm/Wm.java
@@ -77,7 +77,7 @@
                 "wm dismiss-keyguard: dismiss the keyguard, prompting the user for auth if " +
                 "necessary.\n" +
                 "\n" +
-                "wm surface-trace: log surface commands to stdout.\n"
+                "wm surface-trace: log surface commands to stdout in a binary format.\n"
                 );
     }
 
@@ -112,46 +112,15 @@
         }
     }
 
-    private void parseTrace(String next, DataInputStream is) throws Exception {
-        switch (next) {
-        case "Alpha":
-            System.out.println(is.readFloat());
-            break;
-        case "Layer":
-            System.out.println(is.readInt());
-            break;
-        case "Position":
-            System.out.println(is.readFloat() + ", " + is.readFloat());
-            break;
-        case "Size":
-            System.out.println(is.readInt() + ", " + is.readInt());
-            break;
-        case "LayerStack":
-            System.out.println(is.readInt());
-            break;
-        case "Matrix":
-            System.out.println(is.readFloat() + "," + is.readFloat() + "," + is.readFloat() + "," +
-                    is.readFloat());
-            break;
-        case "Hide":
-        case "Show":
-        case "GeometryAppliesWithResize":
-            break;
-        }
-    }
-
     private void runSurfaceTrace() throws Exception {
-        ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe();
-
-        mWm.enableSurfaceTrace(fds[1]);
-        DataInputStream is = new DataInputStream(new FileInputStream(fds[0].getFileDescriptor()));
+        ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(FileDescriptor.out);
+        mWm.enableSurfaceTrace(pfd);
 
         try {
-            while (true) {
-                String cmd = is.readUTF();
-                String window = is.readUTF();
-                System.out.print(cmd + "(" + window + "): ");
-                parseTrace(cmd, is);
+            // No one is going to wake us up, we are just waiting on SIGINT. Otherwise
+            // the WM can happily continue writing to our stdout.
+            synchronized (this) {
+                this.wait();
             }
         } finally {
             mWm.disableSurfaceTrace();