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.example3; 18 19 import com.google.android.mobly.snippet.Snippet; 20 import com.google.android.mobly.snippet.event.EventCache; 21 import com.google.android.mobly.snippet.event.SnippetEvent; 22 import com.google.android.mobly.snippet.rpc.AsyncRpc; 23 import com.google.android.mobly.snippet.util.Log; 24 25 public class ExampleAsyncSnippet implements Snippet { 26 27 private final EventCache mEventCache = EventCache.getInstance(); 28 29 /** 30 * This is a sample asynchronous task. 31 * 32 * In real world use cases, it can be a {@link android.content.BroadcastReceiver}, a Listener, 33 * or any other kind asynchronous callback class. 34 */ 35 public class AsyncTask implements Runnable { 36 37 private final String mCallbackId; 38 private final int mSecretNumber; 39 AsyncTask(String callbackId, int secreteNumber)40 public AsyncTask(String callbackId, int secreteNumber) { 41 this.mCallbackId = callbackId; 42 this.mSecretNumber = secreteNumber; 43 } 44 45 /** 46 * Sleeps for 10s then post a {@link SnippetEvent} with some data. 47 * 48 * If the sleep is interrupted, a {@link SnippetEvent} signaling failure will be posted instead. 49 */ run()50 public void run() { 51 Log.d("Sleeping for 10s before posting an event."); 52 SnippetEvent event = new SnippetEvent(mCallbackId, "AsyncTaskResult"); 53 try { 54 Thread.sleep(10000); 55 } catch (InterruptedException e) { 56 event.getData().putBoolean("successful", false); 57 event.getData().putString("reason", "Sleep was interrupted."); 58 mEventCache.postEvent(event); 59 } 60 event.getData().putBoolean("successful", true); 61 event.getData().putString("exampleData", "Here's a simple event."); 62 event.getData().putInt("secretNumber", mSecretNumber); 63 mEventCache.postEvent(event); 64 } 65 } 66 67 /** 68 * An Rpc method demonstrating the async event mechanism. 69 * 70 * This call returns immediately, but starts a task in a separate thread which will post an 71 * event 10s after the task was started. 72 * 73 * Expect to see an event on the client side that looks like: 74 * 75 * { 76 * 'callbackId': '2-1', 77 * 'name': 'AsyncTaskResult', 78 * 'time': 20460228696, 79 * 'data': { 80 * 'exampleData': "Here's a simple event.", 81 * 'successful': True, 82 * 'secretNumber': 12 83 * } 84 * } 85 * 86 * @param callbackId The ID that should be used to tag {@link SnippetEvent} objects triggered by 87 * this method. 88 * @throws InterruptedException 89 */ 90 @AsyncRpc(description = "This triggers an async event and returns.") tryEvent(String callbackId, int secretNumber)91 public void tryEvent(String callbackId, int secretNumber) throws InterruptedException { 92 Runnable asyncTask = new AsyncTask(callbackId, secretNumber); 93 Thread thread = new Thread(asyncTask); 94 thread.start(); 95 } 96 @Override shutdown()97 public void shutdown() {} 98 } 99