diff options
author | Ang Li <angli@google.com> | 2017-02-23 17:12:22 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-23 17:12:22 -0800 |
commit | 03f59812e047af19f365288940160bf3dbea5b71 (patch) | |
tree | d9ebc96071d3a6e55ee8f55acda66c9502983c71 /third_party | |
parent | 2d15a98d8d6b79793ca887680ed53bf8b6ce7047 (diff) | |
download | mobly-snippet-lib-03f59812e047af19f365288940160bf3dbea5b71.tar.gz |
Async rpc updates. (#43)
* Fix a bug where help() fails if AsyncRpc exists.
* Use Bundle instead of JSONObject to hold data in SnippetEvent to
simplify exception handling for adding data.
* Add a dedicated example for async rpc.
Diffstat (limited to 'third_party')
2 files changed, 22 insertions, 16 deletions
diff --git a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/event/SnippetEvent.java b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/event/SnippetEvent.java index 3db2906..a90d9eb 100644 --- a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/event/SnippetEvent.java +++ b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/event/SnippetEvent.java @@ -16,6 +16,8 @@ package com.google.android.mobly.snippet.event; +import android.os.Bundle; +import com.google.android.mobly.snippet.rpc.JsonBuilder; import org.json.JSONException; import org.json.JSONObject; @@ -26,8 +28,10 @@ public class SnippetEvent { private final String mCallbackId; // The name of this event, e.g. startXxxServiceOnSuccess. private final String mName; - // The content of this event. - private final JSONObject mData = new JSONObject(); + // The content of this event. We use Android's Bundle because it adheres to Android convention + // and adding data to it does not throw checked exceptions, which makes the world a better + // place. + private final Bundle mData = new Bundle(); private final long mCreationTime; @@ -62,20 +66,14 @@ public class SnippetEvent { } /** - * Add serializable data to the Event. + * Get the internal bundle of this event. * - * <p>This is usually for information passed by the original callback API. The data has to be - * JSON serializable so it can be transferred to the client side. + * <p>This is the only way to add data to the event, because we can't inherit Bundle type and we + * don't want to dup all the getter and setters of {@link Bundle}. * - * @param name Name of the data set. - * @param data Content of the data. - * @throws JSONException + * @return The Bundle that holds user data for this {@link SnippetEvent}. */ - public void addData(String name, Object data) throws JSONException { - mData.put(name, data); - } - - private JSONObject getData() { + public Bundle getData() { return mData; } @@ -88,7 +86,7 @@ public class SnippetEvent { result.put("callbackId", getCallbackId()); result.put("name", getName()); result.put("time", getCreationTime()); - result.put("data", getData()); + result.put("data", JsonBuilder.build(mData)); return result; } } diff --git a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/MethodDescriptor.java b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/MethodDescriptor.java index 6c268a0..4c04bb6 100644 --- a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/MethodDescriptor.java +++ b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/MethodDescriptor.java @@ -197,6 +197,15 @@ public final class MethodDescriptor { public boolean isAsync() { return mMethod.isAnnotationPresent(AsyncRpc.class); } + + private String getAnnotationDescription() { + if (isAsync()) { + AsyncRpc annotation = mMethod.getAnnotation(AsyncRpc.class); + return annotation.description(); + } + Rpc annotation = mMethod.getAnnotation(Rpc.class); + return annotation.description(); + } /** * Returns a human-readable help text for this RPC, based on annotations in the source code. * @@ -211,14 +220,13 @@ public final class MethodDescriptor { } paramBuilder.append(parameterTypes[i].getSimpleName()); } - Rpc rpcAnnotation = mMethod.getAnnotation(Rpc.class); String help = String.format( "%s(%s) returns %s // %s", mMethod.getName(), paramBuilder, mMethod.getReturnType().getSimpleName(), - rpcAnnotation.description()); + getAnnotationDescription()); return help; } } |