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.example5; 18 19 import android.content.Context; 20 import android.os.Handler; 21 import androidx.test.InstrumentationRegistry; 22 import android.widget.Toast; 23 import com.google.android.mobly.snippet.Snippet; 24 import com.google.android.mobly.snippet.event.EventCache; 25 import com.google.android.mobly.snippet.event.SnippetEvent; 26 import com.google.android.mobly.snippet.rpc.AsyncRpc; 27 import com.google.android.mobly.snippet.rpc.Rpc; 28 import com.google.android.mobly.snippet.util.Log; 29 30 /** 31 * Demonstrates how to schedule an RPC. 32 */ 33 public class ExampleScheduleRpcSnippet implements Snippet { 34 35 /** 36 * This is a sample asynchronous task. 37 * 38 * In real world use cases, it can be a {@link android.content.BroadcastReceiver}, a Listener, 39 * or any other kind asynchronous callback class. 40 */ 41 public class AsyncTask implements Runnable { 42 43 private final String mCallbackId; 44 private final String mMessage; 45 AsyncTask(String callbackId, String message)46 public AsyncTask(String callbackId, String message) { 47 this.mCallbackId = callbackId; 48 this.mMessage = message; 49 } 50 51 /** 52 * Sleeps for 10s then make toast and post a {@link SnippetEvent} with some data. 53 * 54 * <p>If the sleep is interrupted, a {@link SnippetEvent} signaling failure will be posted 55 * instead. 56 */ 57 @Override run()58 public void run() { 59 Log.d("Sleeping for 10s before posting an event."); 60 SnippetEvent event = new SnippetEvent(mCallbackId, mMessage); 61 try { 62 Thread.sleep(10000); 63 showToast(mMessage); 64 } catch (InterruptedException e) { 65 event.getData().putBoolean("successful", false); 66 event.getData().putString("reason", "Sleep was interrupted."); 67 mEventCache.postEvent(event); 68 } 69 event.getData().putBoolean("successful", true); 70 event.getData().putString("eventName", mMessage); 71 mEventCache.postEvent(event); 72 } 73 } 74 75 private final Context mContext; 76 private final EventCache mEventCache = EventCache.getInstance(); 77 78 /** 79 * Since the APIs here deal with UI, most of them have to be called in a thread that has called 80 * looper. 81 */ 82 private final Handler mHandler; 83 ExampleScheduleRpcSnippet()84 public ExampleScheduleRpcSnippet() { 85 mContext = InstrumentationRegistry.getContext(); 86 mHandler = new Handler(mContext.getMainLooper()); 87 } 88 89 @Rpc(description = "Make a toast on screen.") makeToast(String message)90 public String makeToast(String message) throws InterruptedException { 91 showToast(message); 92 return "OK"; 93 } 94 95 @AsyncRpc(description = "Make a toast on screen after some time.") asyncMakeToast(String callbackId, String message)96 public void asyncMakeToast(String callbackId, String message) 97 throws Throwable { 98 Runnable asyncTask = new AsyncTask(callbackId, "asyncMakeToast"); 99 Thread thread = new Thread(asyncTask); 100 thread.start(); 101 } 102 103 @Override shutdown()104 public void shutdown() {} 105 showToast(final String message)106 private void showToast(final String message) { 107 mHandler.post( 108 new Runnable() { 109 @Override 110 public void run() { 111 Toast.makeText(mContext, message, Toast.LENGTH_LONG).show(); 112 } 113 }); 114 } 115 } 116 117