1 /*
2  * Copyright (C) 2016 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.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  * @author Felix Arends (felix.arends@gmail.com)
33  *
34  */
35 public class SpeechRecognitionFacade extends RpcReceiver {
36   private final AndroidFacade mAndroidFacade;
37 
38   /**
39    * @param activityLauncher
40    *          a helper object that launches activities in a blocking manner
41    */
SpeechRecognitionFacade(FacadeManager manager)42   public SpeechRecognitionFacade(FacadeManager manager) {
43     super(manager);
44     mAndroidFacade = manager.getReceiver(AndroidFacade.class);
45   }
46 
47   @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)48   public String recognizeSpeech(
49       @RpcParameter(name = "prompt", description = "text prompt to show to the user when asking them to speak") @RpcOptional final String prompt,
50       @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,
51       @RpcParameter(name = "languageModel", description = "informs the recognizer which speech model to prefer (see android.speech.RecognizeIntent)") @RpcOptional final String languageModel) {
52     final Intent recognitionIntent =
53         new Intent(android.speech.RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
54 
55     // Setup intent parameters (if provided).
56     if (language != null) {
57       recognitionIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "");
58     }
59     if (languageModel != null) {
60       recognitionIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "");
61     }
62     if (prompt != null) {
63       recognitionIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "");
64     }
65 
66     // Run the activity an retrieve the result.
67     final Intent data = mAndroidFacade.startActivityForResult(recognitionIntent);
68 
69     if (data.hasExtra(android.speech.RecognizerIntent.EXTRA_RESULTS)) {
70       // The result consists of an array-list containing one entry for each
71       // possible result. The most likely result is the first entry.
72       ArrayList<String> results =
73           data.getStringArrayListExtra(android.speech.RecognizerIntent.EXTRA_RESULTS);
74       return results.get(0);
75     }
76 
77     return "";
78   }
79 
80   @Override
shutdown()81   public void shutdown() {
82   }
83 }
84