1 /* 2 * Copyright (C) 2020 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 android.voicerecognition.cts; 18 19 import android.app.Activity; 20 import android.content.ComponentName; 21 import android.content.Intent; 22 import android.os.Bundle; 23 import android.os.Handler; 24 import android.speech.RecognitionListener; 25 import android.speech.RecognitionSupportCallback; 26 import android.speech.RecognizerIntent; 27 import android.speech.SpeechRecognizer; 28 29 import androidx.annotation.NonNull; 30 31 import java.util.ArrayList; 32 import java.util.List; 33 import java.util.concurrent.CountDownLatch; 34 import java.util.concurrent.Executors; 35 36 /** 37 * An activity that uses SpeechRecognition APIs. SpeechRecognition will bind the RecognitionService 38 * to provide the voice recognition functions. 39 */ 40 public class SpeechRecognitionActivity extends Activity { 41 private final String TAG = "SpeechRecognitionActivity"; 42 43 SpeechRecognizer mRecognizer; 44 45 private Handler mHandler; 46 private SpeechRecognizerListener mListener; 47 48 final List<CallbackMethod> mCallbackMethodsInvoked = new ArrayList<>(); 49 50 public boolean mStartListeningCalled; 51 public CountDownLatch mCountDownLatch; 52 53 @Override onCreate(Bundle savedInstanceState)54 protected void onCreate(Bundle savedInstanceState) { 55 super.onCreate(savedInstanceState); 56 setContentView(R.layout.main); 57 } 58 59 @Override onDestroy()60 protected void onDestroy() { 61 if (mRecognizer != null) { 62 mRecognizer.destroy(); 63 mRecognizer = null; 64 } 65 super.onDestroy(); 66 } 67 startListening()68 public void startListening() { 69 final Intent recognizerIntent = 70 new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 71 recognizerIntent.putExtra( 72 RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName()); 73 startListening(recognizerIntent); 74 } 75 startListening(Intent intent)76 public void startListening(Intent intent) { 77 mHandler.post(() -> mRecognizer.startListening(intent)); 78 } 79 stopListening()80 public void stopListening() { 81 mHandler.post(mRecognizer::stopListening); 82 } 83 cancel()84 public void cancel() { 85 mHandler.post(mRecognizer::cancel); 86 } 87 destroyRecognizer()88 public void destroyRecognizer() { 89 mHandler.post(mRecognizer::destroy); 90 } 91 checkRecognitionSupport(Intent intent, RecognitionSupportCallback rsc)92 public void checkRecognitionSupport(Intent intent, RecognitionSupportCallback rsc) { 93 mHandler.post(() -> mRecognizer.checkRecognitionSupport(intent, 94 Executors.newSingleThreadExecutor(), rsc)); 95 } 96 triggerModelDownload(Intent intent)97 public void triggerModelDownload(Intent intent) { 98 mHandler.post(() -> mRecognizer.triggerModelDownload(intent)); 99 } 100 init(boolean onDevice, String customRecognizerComponent)101 public void init(boolean onDevice, String customRecognizerComponent) { 102 mHandler = new Handler(getMainLooper()); 103 mHandler.post(() -> { 104 if (onDevice) { 105 mRecognizer = SpeechRecognizer.createOnDeviceTestingSpeechRecognizer(this); 106 } else if (customRecognizerComponent != null) { 107 mRecognizer = SpeechRecognizer.createSpeechRecognizer(this, 108 ComponentName.unflattenFromString(customRecognizerComponent)); 109 } else { 110 mRecognizer = SpeechRecognizer.createSpeechRecognizer(this); 111 } 112 113 mListener = new SpeechRecognizerListener(); 114 mRecognizer.setRecognitionListener(mListener); 115 mRecognizer.setRecognitionListener(mListener); 116 mStartListeningCalled = false; 117 mCountDownLatch = new CountDownLatch(1); 118 }); 119 } 120 121 private class SpeechRecognizerListener implements RecognitionListener { 122 123 @Override onReadyForSpeech(Bundle params)124 public void onReadyForSpeech(Bundle params) { 125 mCallbackMethodsInvoked.add(CallbackMethod.CALLBACK_METHOD_READY_FOR_SPEECH); 126 } 127 128 @Override onBeginningOfSpeech()129 public void onBeginningOfSpeech() { 130 mCallbackMethodsInvoked.add(CallbackMethod.CALLBACK_METHOD_BEGINNING_OF_SPEECH); 131 } 132 133 @Override onRmsChanged(float rmsdB)134 public void onRmsChanged(float rmsdB) { 135 mCallbackMethodsInvoked.add(CallbackMethod.CALLBACK_METHOD_RMS_CHANGED); 136 } 137 138 @Override onBufferReceived(byte[] buffer)139 public void onBufferReceived(byte[] buffer) { 140 mCallbackMethodsInvoked.add(CallbackMethod.CALLBACK_METHOD_BUFFER_RECEIVED); 141 } 142 143 @Override onEndOfSpeech()144 public void onEndOfSpeech() { 145 mCallbackMethodsInvoked.add(CallbackMethod.CALLBACK_METHOD_END_OF_SPEECH); 146 } 147 148 @Override onError(int error)149 public void onError(int error) { 150 mCallbackMethodsInvoked.add(CallbackMethod.CALLBACK_METHOD_ERROR); 151 } 152 153 @Override onResults(Bundle results)154 public void onResults(Bundle results) { 155 mCallbackMethodsInvoked.add(CallbackMethod.CALLBACK_METHOD_RESULTS); 156 mStartListeningCalled = true; 157 mCountDownLatch.countDown(); 158 } 159 160 @Override onPartialResults(Bundle partialResults)161 public void onPartialResults(Bundle partialResults) { 162 mCallbackMethodsInvoked.add(CallbackMethod.CALLBACK_METHOD_PARTIAL_RESULTS); 163 } 164 165 @Override onSegmentResults(@onNull Bundle segmentResults)166 public void onSegmentResults(@NonNull Bundle segmentResults) { 167 mCallbackMethodsInvoked.add(CallbackMethod.CALLBACK_METHOD_SEGMENTS_RESULTS); 168 } 169 170 @Override onEndOfSegmentedSession()171 public void onEndOfSegmentedSession() { 172 mCallbackMethodsInvoked.add(CallbackMethod.CALLBACK_METHOD_END_SEGMENTED_SESSION); 173 } 174 175 @Override onEvent(int eventType, Bundle params)176 public void onEvent(int eventType, Bundle params) { 177 } 178 } 179 } 180