VoiceInteractionService.java revision 8ecaf5f5cfd18e0436db1a27ccf46a063e9aacd7
1/**
2 * Copyright (C) 2014 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
17package android.service.voice;
18
19import android.annotation.SdkConstant;
20import android.app.Service;
21import android.content.Context;
22import android.content.Intent;
23import android.hardware.soundtrigger.KeyphraseEnrollmentInfo;
24import android.hardware.soundtrigger.SoundTriggerHelper;
25import android.os.Bundle;
26import android.os.IBinder;
27import android.os.RemoteException;
28import android.os.ServiceManager;
29
30import com.android.internal.annotations.VisibleForTesting;
31import com.android.internal.app.IVoiceInteractionManagerService;
32
33
34/**
35 * Top-level service of the current global voice interactor, which is providing
36 * support for hotwording, the back-end of a {@link android.app.VoiceInteractor}, etc.
37 * The current VoiceInteractionService that has been selected by the user is kept
38 * always running by the system, to allow it to do things like listen for hotwords
39 * in the background to instigate voice interactions.
40 *
41 * <p>Because this service is always running, it should be kept as lightweight as
42 * possible.  Heavy-weight operations (including showing UI) should be implemented
43 * in the associated {@link android.service.voice.VoiceInteractionSessionService} when
44 * an actual voice interaction is taking place, and that service should run in a
45 * separate process from this one.
46 */
47public class VoiceInteractionService extends Service {
48    /**
49     * The {@link Intent} that must be declared as handled by the service.
50     * To be supported, the service must also require the
51     * {@link android.Manifest.permission#BIND_VOICE_INTERACTION} permission so
52     * that other applications can not abuse it.
53     */
54    @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
55    public static final String SERVICE_INTERFACE =
56            "android.service.voice.VoiceInteractionService";
57
58    /**
59     * Name under which a VoiceInteractionService component publishes information about itself.
60     * This meta-data should reference an XML resource containing a
61     * <code>&lt;{@link
62     * android.R.styleable#VoiceInteractionService voice-interaction-service}&gt;</code> tag.
63     */
64    public static final String SERVICE_META_DATA = "android.voice_interaction";
65
66    IVoiceInteractionService mInterface = new IVoiceInteractionService.Stub() {
67    };
68
69    IVoiceInteractionManagerService mSystemService;
70
71    private KeyphraseEnrollmentInfo mKeyphraseEnrollmentInfo;
72    private SoundTriggerHelper mSoundTriggerHelper;
73
74    public void startSession(Bundle args) {
75        try {
76            mSystemService.startSession(mInterface, args);
77        } catch (RemoteException e) {
78        }
79    }
80
81    @Override
82    public void onCreate() {
83        super.onCreate();
84        mSystemService = IVoiceInteractionManagerService.Stub.asInterface(
85                ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE));
86        mKeyphraseEnrollmentInfo = new KeyphraseEnrollmentInfo(getPackageManager());
87        mSoundTriggerHelper = new SoundTriggerHelper();
88    }
89
90    @Override
91    public IBinder onBind(Intent intent) {
92        if (SERVICE_INTERFACE.equals(intent.getAction())) {
93            return mInterface.asBinder();
94        }
95        return null;
96    }
97
98    /**
99     * @param keyphrase The keyphrase that's being used, for example "Hello Android".
100     * @param locale The locale for which the enrollment needs to be performed.
101     *        This is a Java locale, for example "en_US".
102     * @param callback The callback to notify of detection events.
103     * @return An always-on hotword detector for the given keyphrase and locale.
104     */
105    public final AlwaysOnHotwordDetector getAlwaysOnHotwordDetector(
106            String keyphrase, String locale, AlwaysOnHotwordDetector.Callback callback) {
107        return new AlwaysOnHotwordDetector(keyphrase, locale, callback,
108                mKeyphraseEnrollmentInfo, mSoundTriggerHelper, mInterface, mSystemService);
109    }
110
111    /**
112     * @return Details of keyphrases available for enrollment.
113     * @hide
114     */
115    @VisibleForTesting
116    protected final KeyphraseEnrollmentInfo getKeyphraseEnrollmentInfo() {
117        return mKeyphraseEnrollmentInfo;
118    }
119}
120