1 /* 2 * Copyright (C) 2017 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.google.android.mobly.snippet.event; 18 19 import android.os.Bundle; 20 import com.google.android.mobly.snippet.rpc.JsonBuilder; 21 import org.json.JSONException; 22 import org.json.JSONObject; 23 24 /** Class used to store information from a callback event. */ 25 public class SnippetEvent { 26 27 // The ID used to associate an event to a callback object on the client side. 28 private final String mCallbackId; 29 // The name of this event, e.g. startXxxServiceOnSuccess. 30 private final String mName; 31 // The content of this event. We use Android's Bundle because it adheres to Android convention 32 // and adding data to it does not throw checked exceptions, which makes the world a better 33 // place. 34 private final Bundle mData = new Bundle(); 35 36 private final long mCreationTime; 37 38 /** 39 * Constructs an {@link SnippetEvent} object. 40 * 41 * <p>The object is used to store information from a callback method associated with a call to 42 * an {@link com.google.android.mobly.snippet.rpc.AsyncRpc} method. 43 * 44 * @param callbackId The callbackId passed to the {@link 45 * com.google.android.mobly.snippet.rpc.AsyncRpc} method. 46 * @param name The name of the event. 47 */ SnippetEvent(String callbackId, String name)48 public SnippetEvent(String callbackId, String name) { 49 if (callbackId == null) { 50 throw new IllegalArgumentException("SnippetEvent's callback ID shall not be null."); 51 } 52 if (name == null) { 53 throw new IllegalArgumentException("SnippetEvent's name shall not be null."); 54 } 55 mCallbackId = callbackId; 56 mName = name; 57 mCreationTime = System.currentTimeMillis(); 58 } 59 getCallbackId()60 public String getCallbackId() { 61 return mCallbackId; 62 } 63 getName()64 public String getName() { 65 return mName; 66 } 67 68 /** 69 * Get the internal bundle of this event. 70 * 71 * <p>This is the only way to add data to the event, because we can't inherit Bundle type and we 72 * don't want to dup all the getter and setters of {@link Bundle}. 73 * 74 * @return The Bundle that holds user data for this {@link SnippetEvent}. 75 */ getData()76 public Bundle getData() { 77 return mData; 78 } 79 getCreationTime()80 public long getCreationTime() { 81 return mCreationTime; 82 } 83 toJson()84 public JSONObject toJson() throws JSONException { 85 JSONObject result = new JSONObject(); 86 result.put("callbackId", getCallbackId()); 87 result.put("name", getName()); 88 result.put("time", getCreationTime()); 89 result.put("data", JsonBuilder.build(mData)); 90 return result; 91 } 92 } 93