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.example7; 18 19 import android.content.Context; 20 import android.os.Handler; 21 import android.widget.Toast; 22 import androidx.test.InstrumentationRegistry; 23 import com.google.android.mobly.snippet.Snippet; 24 import com.google.android.mobly.snippet.event.EventCache; 25 import com.google.android.mobly.snippet.rpc.Rpc; 26 import com.google.android.mobly.snippet.rpc.RpcDefault; 27 import com.google.android.mobly.snippet.rpc.RpcOptional; 28 29 /** Demonstrates how to mark an RPC has default value or optional. */ 30 public class ExampleDefaultAndOptionalRpcSnippet implements Snippet { 31 32 private final Context mContext; 33 private final EventCache mEventCache = EventCache.getInstance(); 34 35 /** 36 * Since the APIs here deal with UI, most of them have to be called in a thread that has called 37 * looper. 38 */ 39 private final Handler mHandler; 40 ExampleDefaultAndOptionalRpcSnippet()41 public ExampleDefaultAndOptionalRpcSnippet() { 42 mContext = InstrumentationRegistry.getContext(); 43 mHandler = new Handler(mContext.getMainLooper()); 44 } 45 46 @Rpc(description = "Make a toast on screen.") makeToast( String message, @RpcDefault("true") Boolean bool, @RpcOptional Integer number)47 public String makeToast( 48 String message, @RpcDefault("true") Boolean bool, @RpcOptional Integer number) 49 throws InterruptedException { 50 if (number == null) { 51 showToast(String.format("%s, bool:%b", message, bool)); 52 } else { 53 showToast(String.format("%s, bool:%b, number:%d", message, bool, number)); 54 } 55 return "OK"; 56 } 57 58 @Override shutdown()59 public void shutdown() {} 60 showToast(final String message)61 private void showToast(final String message) { 62 mHandler.post( 63 new Runnable() { 64 @Override 65 public void run() { 66 Toast.makeText(mContext, message, Toast.LENGTH_LONG).show(); 67 } 68 }); 69 } 70 } 71