From 4a32297c1e0b22b0d18a7eac55a9fdf65b20b632 Mon Sep 17 00:00:00 2001 From: San Mehat Date: Wed, 20 Jan 2010 15:14:31 -0800 Subject: [PATCH] NativeDaemonConnector: Add a convenience method for obtaining lists Signed-off-by: San Mehat --- .../android/server/NativeDaemonConnector.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/services/java/com/android/server/NativeDaemonConnector.java b/services/java/com/android/server/NativeDaemonConnector.java index da3e562fb5..98e00dc11f 100644 --- a/services/java/com/android/server/NativeDaemonConnector.java +++ b/services/java/com/android/server/NativeDaemonConnector.java @@ -201,6 +201,9 @@ final class NativeDaemonConnector implements Runnable { } } + /** + * Issue a command to the native daemon and return the responses + */ public synchronized ArrayList doCommand(String cmd) throws IllegalStateException { sendCommand(cmd); @@ -236,4 +239,38 @@ final class NativeDaemonConnector implements Runnable { } return response; } + + /* + * Issues a list command and returns the cooked list + */ + public String[] doListCommand(String cmd, int expectedResponseCode) + throws IllegalStateException { + + ArrayList rsp = doCommand(cmd); + String[] rdata = new String[rsp.size()-1]; + int idx = 0; + + for (String line : rsp) { + try { + String[] tok = line.split(" "); + int code = Integer.parseInt(tok[0]); + if (code == expectedResponseCode) { + if (tok.length !=2) { + throw new IllegalStateException( + String.format("Malformatted list entry '%s'", line)); + } + rdata[idx++] = tok[1]; + } else if (code == NativeDaemonConnector.ResponseCode.CommandOkay) { + return rdata; + } else { + throw new IllegalStateException( + String.format("Expected list response %d, but got %d", + expectedResponseCode, code)); + } + } catch (NumberFormatException nfe) { + throw new IllegalStateException(String.format("Error reading code '%s'", line)); + } + } + throw new IllegalStateException("Got an empty response"); + } }