1 /* 2 * Copyright (C) 2017 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.googlecode.android_scripting.facade; 18 19 import android.content.Intent; 20 import android.speech.RecognizerIntent; 21 22 import com.googlecode.android_scripting.jsonrpc.RpcReceiver; 23 import com.googlecode.android_scripting.rpc.Rpc; 24 import com.googlecode.android_scripting.rpc.RpcOptional; 25 import com.googlecode.android_scripting.rpc.RpcParameter; 26 27 import java.util.ArrayList; 28 29 /** 30 * A facade containing RPC implementations related to the speech-to-text functionality of Android. 31 * 32 * 33 */ 34 public class SpeechRecognitionFacade extends RpcReceiver { 35 private final AndroidFacade mAndroidFacade; 36 37 /** 38 * @param activityLauncher 39 * a helper object that launches activities in a blocking manner 40 */ SpeechRecognitionFacade(FacadeManager manager)41 public SpeechRecognitionFacade(FacadeManager manager) { 42 super(manager); 43 mAndroidFacade = manager.getReceiver(AndroidFacade.class); 44 } 45 46 @Rpc(description = "Recognizes user's speech and returns the most likely result.", returns = "An empty string in case the speech cannot be recongnized.") recognizeSpeech( @pcParametername = "prompt", description = "text prompt to show to the user when asking them to speak") @pcOptional final String prompt, @RpcParameter(name = "language", description = "language override to inform the recognizer that it should expect speech in a language different than the one set in the java.util.Locale.getDefault()") @RpcOptional final String language, @RpcParameter(name = "languageModel", description = "informs the recognizer which speech model to prefer (see android.speech.RecognizeIntent)") @RpcOptional final String languageModel)47 public String recognizeSpeech( 48 @RpcParameter(name = "prompt", description = "text prompt to show to the user when asking them to speak") @RpcOptional final String prompt, 49 @RpcParameter(name = "language", description = "language override to inform the recognizer that it should expect speech in a language different than the one set in the java.util.Locale.getDefault()") @RpcOptional final String language, 50 @RpcParameter(name = "languageModel", description = "informs the recognizer which speech model to prefer (see android.speech.RecognizeIntent)") @RpcOptional final String languageModel) { 51 final Intent recognitionIntent = 52 new Intent(android.speech.RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 53 54 // Setup intent parameters (if provided). 55 if (language != null) { 56 recognitionIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, ""); 57 } 58 if (languageModel != null) { 59 recognitionIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, ""); 60 } 61 if (prompt != null) { 62 recognitionIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, ""); 63 } 64 65 // Run the activity an retrieve the result. 66 final Intent data = mAndroidFacade.startActivityForResult(recognitionIntent); 67 68 if (data.hasExtra(android.speech.RecognizerIntent.EXTRA_RESULTS)) { 69 // The result consists of an array-list containing one entry for each 70 // possible result. The most likely result is the first entry. 71 ArrayList<String> results = 72 data.getStringArrayListExtra(android.speech.RecognizerIntent.EXTRA_RESULTS); 73 return results.get(0); 74 } 75 76 return ""; 77 } 78 79 @Override shutdown()80 public void shutdown() { 81 } 82 } 83