NativeDaemonConnector: Improve error reporting and debug formatting

Updated:
  NativeDaemonConnector: Put debugging behind a conditional

Signed-off-by: San Mehat <san@google.com>
diff --git a/services/java/com/android/server/NativeDaemonConnector.java b/services/java/com/android/server/NativeDaemonConnector.java
index 9a066d3..cf083c0 100644
--- a/services/java/com/android/server/NativeDaemonConnector.java
+++ b/services/java/com/android/server/NativeDaemonConnector.java
@@ -21,7 +21,6 @@
 import android.os.Environment;
 import android.os.SystemClock;
 import android.os.SystemProperties;
-import android.util.Config;
 import android.util.Log;
 
 import java.io.IOException;
@@ -41,6 +40,7 @@
  * protocol.
  */
 final class NativeDaemonConnector implements Runnable {
+    private static final boolean LOCAL_LOGD = false;
 
     private BlockingQueue<String> mResponseQueue;
     private OutputStream          mOutputStream;
@@ -110,7 +110,7 @@
                 for (int i = 0; i < count; i++) {
                     if (buffer[i] == 0) {
                         String event = new String(buffer, start, i - start);
-//                        Log.d(TAG, "Got packet {" + event + "}");
+                        if (LOCAL_LOGD) Log.d(TAG, String.format("RCV <- {%s}", event));
 
                         String[] tokens = event.split(" ");
                         try {
@@ -177,7 +177,7 @@
      */
     private void sendCommand(String command, String argument) {
         synchronized (this) {
-             Log.d(TAG, "sendCommand {" + command + "} {" + argument + "}");
+            if (LOCAL_LOGD) Log.d(TAG, String.format("SND -> {%s} {%s}", command, argument));
             if (mOutputStream == null) {
                 Log.e(TAG, "No connection to daemon", new IllegalStateException());
             } else {
@@ -210,7 +210,7 @@
         while (!complete) {
             try {
                 String line = mResponseQueue.take();
-                Log.d(TAG, String.format("RSP -> {%s}", line));
+                if (LOCAL_LOGD) Log.d(TAG, String.format("RSP <- {%s}", line));
                 String[] tokens = line.split(" ");
                 try {
                     code = Integer.parseInt(tokens[0]);
@@ -250,13 +250,18 @@
         String[] rdata = new String[rsp.size()-1];
         int idx = 0;
 
-        for (String line : rsp) {
+        for (int i = 0; i < rsp.size(); i++) {
+            String line = rsp.get(i);
             try {
                 String[] tok = line.split(" ");
                 int code = Integer.parseInt(tok[0]);
                 if (code == expectedResponseCode) {
                     rdata[idx++] = line.substring(tok[0].length() + 1);
                 } else if (code == NativeDaemonConnector.ResponseCode.CommandOkay) {
+                    if (LOCAL_LOGD) Log.d(TAG, String.format("List terminated with {%s}", line));
+                    if (i != rsp.size()) {
+                        Log.w(TAG, String.format("Recv'd %d lines after list term", (rsp.size()-i)));
+                    }
                     return rdata;
                 } else {
                     throw new NativeDaemonConnectorException(