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