1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.cts.verifier.audio;
18 
19 import android.os.Handler;
20 import android.os.Message;
21 import android.util.Log;
22 
23 public class AudioTestRunner implements Runnable {
24 
25     public static final int TEST_STARTED = 900;
26     public static final int TEST_MESSAGE = 903;
27     public static final int TEST_ENDED_OK = 904;
28     public static final int TEST_ENDED_ERROR = 905;
29 
30     private final String mTag;
31     public final int mTestId;
32     private final Handler mHandler;
AudioTestRunner(String tag, int testId, Handler handler)33     AudioTestRunner(String tag, int testId, Handler handler) {
34         mTag = tag;
35         mTestId = testId;
36         mHandler = handler;
37         Log.v(mTag,"New AudioTestRunner test: " + mTestId);
38     }
run()39     public void run() {
40         sendMessage(mTestId, TEST_STARTED,"");
41     };
42 
sleep(int durationMs)43     public void sleep(int durationMs) {
44         try {
45             Thread.sleep(durationMs);
46         } catch (InterruptedException e) {
47             e.printStackTrace();
48             //restore interrupted status
49             Thread.currentThread().interrupt();
50         }
51     }
52 
sendMessage(int msgType, String str)53     public void sendMessage(int msgType, String str) {
54         sendMessage(mTestId, msgType, str);
55     }
56 
sendMessage(int testId, int msgType, String str)57     public void sendMessage(int testId, int msgType, String str) {
58         Message msg = Message.obtain();
59         msg.what = msgType;
60         msg.obj = str;
61         msg.arg1 = testId;
62         mHandler.sendMessage(msg);
63     }
64 
65     public static class AudioTestRunnerMessageHandler extends Handler {
handleMessage(Message msg)66         public void handleMessage(Message msg) {
67             super.handleMessage(msg);
68             int testId = msg.arg1; //testId
69             String str = (String) msg.obj;
70             switch (msg.what) {
71                 case TEST_STARTED:
72                     testStarted(testId, str);
73                     break;
74                 case TEST_MESSAGE:
75                     testMessage(testId, str);
76                     break;
77                 case TEST_ENDED_OK:
78                     testEndedOk(testId, str);
79                     break;
80                 case TEST_ENDED_ERROR:
81                     testEndedError(testId, str);
82                     break;
83                 default:
84                     Log.e("TestHandler", String.format("Unknown message: %d", msg.what));
85             }
86         }
87 
testStarted(int testId, String str)88         public void testStarted(int testId, String str) {
89             //replace with your code
90         }
91 
testMessage(int testId, String str)92         public void testMessage(int testId, String str) {
93             //replace with your code
94         }
95 
testEndedOk(int testId, String str)96         public void testEndedOk(int testId, String str) {
97             //replace with your code
98         }
99 
testEndedError(int testId, String str)100         public void testEndedError(int testId, String str) {
101             //replace with your code
102         }
103     }
104 }