FileDocCategorySizeDatePackage
NativeDaemonEvent.javaAPI DocAndroid 5.1 API8380Thu Mar 12 22:22:42 GMT 2015com.android.server

NativeDaemonEvent

public class NativeDaemonEvent extends Object
Parsed event from native side of {@link NativeDaemonConnector}.

Fields Summary
private final int
mCmdNumber
private final int
mCode
private final String
mMessage
private final String
mRawEvent
private final String
mLogMessage
private String[]
mParsed
public static final String
SENSITIVE_MARKER
Constructors Summary
private NativeDaemonEvent(int cmdNumber, int code, String message, String rawEvent, String logMessage)

        mCmdNumber = cmdNumber;
        mCode = code;
        mMessage = message;
        mRawEvent = rawEvent;
        mLogMessage = logMessage;
        mParsed = null;
    
Methods Summary
public voidcheckCode(int code)
Verify this event matches the given code.

throws
IllegalStateException if {@link #getCode()} doesn't match.

        if (mCode != code) {
            throw new IllegalStateException("Expected " + code + " but was: " + this);
        }
    
public static java.lang.String[]filterMessageList(com.android.server.NativeDaemonEvent[] events, int matchCode)
Filter the given {@link NativeDaemonEvent} list, returning {@link #getMessage()} for any events matching the requested code.

        final ArrayList<String> result = Lists.newArrayList();
        for (NativeDaemonEvent event : events) {
            if (event.getCode() == matchCode) {
                result.add(event.getMessage());
            }
        }
        return result.toArray(new String[result.size()]);
    
public intgetCmdNumber()


       
        return mCmdNumber;
    
public intgetCode()

        return mCode;
    
public java.lang.StringgetField(int n)
Find the Nth field of the event. This ignores and code or cmdNum, the first return value is given for N=0. Also understands "\"quoted\" multiword responses" and tries them as a single field

        if (mParsed == null) {
            mParsed = unescapeArgs(mRawEvent);
        }
        n += 2; // skip code and command#
        if (n > mParsed.length) return null;
            return mParsed[n];
        
public java.lang.StringgetMessage()

        return mMessage;
    
public java.lang.StringgetRawEvent()

        return mRawEvent;
    
public booleanisClassClientError()
Test if event represents a command syntax or argument error.

        return mCode >= 500 && mCode < 600;
    
public booleanisClassContinue()
Test if event represents a partial response which is continued in additional subsequent events.

        return mCode >= 100 && mCode < 200;
    
public booleanisClassOk()
Test if event represents a command success.

        return mCode >= 200 && mCode < 300;
    
public booleanisClassServerError()
Test if event represents a remote native daemon error.

        return mCode >= 400 && mCode < 500;
    
public booleanisClassUnsolicited()
Test if event represents an unsolicited event from native daemon.

        return isClassUnsolicited(mCode);
    
private static booleanisClassUnsolicited(int code)

        return code >= 600 && code < 700;
    
public static com.android.server.NativeDaemonEventparseRawEvent(java.lang.String rawEvent)
Parse the given raw event into {@link NativeDaemonEvent} instance.

throws
IllegalArgumentException when line doesn't match format expected from native side.

        final String[] parsed = rawEvent.split(" ");
        if (parsed.length < 2) {
            throw new IllegalArgumentException("Insufficient arguments");
        }

        int skiplength = 0;

        final int code;
        try {
            code = Integer.parseInt(parsed[0]);
            skiplength = parsed[0].length() + 1;
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("problem parsing code", e);
        }

        int cmdNumber = -1;
        if (isClassUnsolicited(code) == false) {
            if (parsed.length < 3) {
                throw new IllegalArgumentException("Insufficient arguemnts");
            }
            try {
                cmdNumber = Integer.parseInt(parsed[1]);
                skiplength += parsed[1].length() + 1;
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("problem parsing cmdNumber", e);
            }
        }

        String logMessage = rawEvent;
        if (parsed.length > 2 && parsed[2].equals(SENSITIVE_MARKER)) {
            skiplength += parsed[2].length() + 1;
            logMessage = parsed[0] + " " + parsed[1] + " {}";
        }

        final String message = rawEvent.substring(skiplength);

        return new NativeDaemonEvent(cmdNumber, code, message, rawEvent, logMessage);
    
public java.lang.StringtoString()

        return mLogMessage;
    
public static java.lang.String[]unescapeArgs(java.lang.String rawEvent)

        final boolean DEBUG_ROUTINE = false;
        final String LOGTAG = "unescapeArgs";
        final ArrayList<String> parsed = new ArrayList<String>();
        final int length = rawEvent.length();
        int current = 0;
        int wordEnd = -1;
        boolean quoted = false;

        if (DEBUG_ROUTINE) Slog.e(LOGTAG, "parsing '" + rawEvent + "'");
        if (rawEvent.charAt(current) == '\"") {
            quoted = true;
            current++;
        }
        while (current < length) {
            // find the end of the word
            char terminator = quoted ? '\"" : ' ";
            wordEnd = current;
            while (wordEnd < length && rawEvent.charAt(wordEnd) != terminator) {
                if (rawEvent.charAt(wordEnd) == '\\") {
                    // skip the escaped char
                    ++wordEnd;
                }
                ++wordEnd;
            }
            if (wordEnd > length) wordEnd = length;
            String word = rawEvent.substring(current, wordEnd);
            current += word.length();
            if (!quoted) {
                word = word.trim();
            } else {
                current++;  // skip the trailing quote
            }
            // unescape stuff within the word
            word = word.replace("\\\\", "\\");
            word = word.replace("\\\"", "\"");

            if (DEBUG_ROUTINE) Slog.e(LOGTAG, "found '" + word + "'");
            parsed.add(word);

            // find the beginning of the next word - either of these options
            int nextSpace = rawEvent.indexOf(' ", current);
            int nextQuote = rawEvent.indexOf(" \"", current);
            if (DEBUG_ROUTINE) {
                Slog.e(LOGTAG, "nextSpace=" + nextSpace + ", nextQuote=" + nextQuote);
            }
            if (nextQuote > -1 && nextQuote <= nextSpace) {
                quoted = true;
                current = nextQuote + 2;
            } else {
                quoted = false;
                if (nextSpace > -1) {
                    current = nextSpace + 1;
                }
            } // else we just start the next word after the current and read til the end
            if (DEBUG_ROUTINE) {
                Slog.e(LOGTAG, "next loop - current=" + current +
                        ", length=" + length + ", quoted=" + quoted);
            }
        }
        return parsed.toArray(new String[parsed.size()]);