• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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.speech;
18 
19 import android.content.ComponentName;
20 import android.content.Intent;
21 import android.content.ServiceConnection;
22 import android.os.Bundle;
23 import android.os.IBinder;
24 import android.os.RemoteException;
25 import android.speech.RecognitionResult;
26 import android.util.Log;
27 
28 import java.util.List;
29 
30 /**
31  * Utils for Google's network-based speech recognizer, which lets you perform
32  * speech-to-text translation through RecognitionService. IRecognitionService
33  * and IRecognitionListener are the core interfaces; you begin recognition
34  * through IRecognitionService and subscribe to callbacks about when the user
35  * stopped speaking, results come in, errors, etc. through IRecognitionListener.
36  * RecognitionServiceUtil includes default IRecognitionListener and
37  * ServiceConnection implementations to reduce the amount of boilerplate.
38  *
39  * The Service provides no user interface. See RecognitionActivity if you
40  * want the standard voice search UI.
41  *
42  * Below is a small skeleton of how to use the recognizer:
43  *
44  * ServiceConnection conn = new RecognitionServiceUtil.Connection();
45  * mContext.bindService(RecognitionServiceUtil.sDefaultIntent,
46  *     conn, Context.BIND_AUTO_CREATE);
47  * IRecognitionListener listener = new RecognitionServiceWrapper.NullListener() {
48  *         public void onResults(List<String> results) {
49  *             // Do something with recognition transcripts
50  *         }
51  *     }
52  *
53  * // Must wait for conn.mService to be populated, then call below
54  * conn.mService.startListening(null, listener);
55  *
56  * {@hide}
57  */
58 public class RecognitionServiceUtil {
59     public static final Intent sDefaultIntent = new Intent(
60             RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
61 
62     // Recognize request parameters
63     public static final String USE_LOCATION = "useLocation";
64     public static final String CONTACT_AUTH_TOKEN = "contactAuthToken";
65 
66     // Bundles
67     public static final String NOISE_LEVEL = "NoiseLevel";
68     public static final String SIGNAL_NOISE_RATIO = "SignalNoiseRatio";
69 
RecognitionServiceUtil()70     private RecognitionServiceUtil() {}
71 
72     /**
73      * IRecognitionListener which does nothing in response to recognition
74      * callbacks. You can subclass from this and override only the methods
75      * whose events you want to respond to.
76      */
77     public static class NullListener extends IRecognitionListener.Stub {
onReadyForSpeech(Bundle bundle)78         public void onReadyForSpeech(Bundle bundle) {}
onBeginningOfSpeech()79         public void onBeginningOfSpeech() {}
onRmsChanged(float rmsdB)80         public void onRmsChanged(float rmsdB) {}
onBufferReceived(byte[] buf)81         public void onBufferReceived(byte[] buf) {}
onEndOfSpeech()82         public void onEndOfSpeech() {}
onError(int error)83         public void onError(int error) {}
onResults(List<RecognitionResult> results, long key)84         public void onResults(List<RecognitionResult> results, long key) {}
85     }
86 
87     /**
88      * Basic ServiceConnection which just records mService variable.
89      */
90     public static class Connection implements ServiceConnection {
91         public IRecognitionService mService;
92 
onServiceConnected(ComponentName name, IBinder service)93         public synchronized void onServiceConnected(ComponentName name, IBinder service) {
94             mService = IRecognitionService.Stub.asInterface(service);
95         }
96 
onServiceDisconnected(ComponentName name)97         public void onServiceDisconnected(ComponentName name) {
98             mService = null;
99         }
100     }
101 }
102