summaryrefslogtreecommitdiffstats
path: root/core/java/android/speech/RecognitionServiceUtil.java
blob: a8c78684f6cdc99f2fa9ca5e8eb1d6ce6459225e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (C) 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package android.speech;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.speech.RecognitionResult;
import android.util.Log;

import java.util.List;

/**
 * Utils for Google's network-based speech recognizer, which lets you perform
 * speech-to-text translation through RecognitionService. IRecognitionService
 * and IRecognitionListener are the core interfaces; you begin recognition
 * through IRecognitionService and subscribe to callbacks about when the user
 * stopped speaking, results come in, errors, etc. through IRecognitionListener.
 * RecognitionServiceUtil includes default IRecognitionListener and
 * ServiceConnection implementations to reduce the amount of boilerplate.
 *
 * The Service provides no user interface. See RecognitionActivity if you
 * want the standard voice search UI.
 *
 * Below is a small skeleton of how to use the recognizer:
 *
 * ServiceConnection conn = new RecognitionServiceUtil.Connection();
 * mContext.bindService(RecognitionServiceUtil.sDefaultIntent,
 *     conn, Context.BIND_AUTO_CREATE);
 * IRecognitionListener listener = new RecognitionServiceWrapper.NullListener() {
 *         public void onResults(List<String> results) {
 *             // Do something with recognition transcripts
 *         }
 *     }
 *
 * // Must wait for conn.mService to be populated, then call below
 * conn.mService.startListening(null, listener);
 *
 * {@hide}
 */
public class RecognitionServiceUtil {
    public static final Intent sDefaultIntent = new Intent(
            RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

    // Recognize request parameters
    public static final String USE_LOCATION = "useLocation";
    public static final String CONTACT_AUTH_TOKEN = "contactAuthToken";
    
    // Bundles
    public static final String NOISE_LEVEL = "NoiseLevel";
    public static final String SIGNAL_NOISE_RATIO = "SignalNoiseRatio";

    private RecognitionServiceUtil() {}

    /**
     * IRecognitionListener which does nothing in response to recognition
     * callbacks. You can subclass from this and override only the methods
     * whose events you want to respond to.
     */
    public static class NullListener extends IRecognitionListener.Stub {
        public void onReadyForSpeech(Bundle bundle) {}
        public void onBeginningOfSpeech() {}
        public void onRmsChanged(float rmsdB) {}
        public void onBufferReceived(byte[] buf) {}
        public void onEndOfSpeech() {}
        public void onError(int error) {}
        public void onResults(List<RecognitionResult> results, long key) {}
    }

    /**
     * Basic ServiceConnection which just records mService variable.
     */
    public static class Connection implements ServiceConnection {
        public IRecognitionService mService;

        public synchronized void onServiceConnected(ComponentName name, IBinder service) {
            mService = IRecognitionService.Stub.asInterface(service);
        }

        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }
    }
}