VoiceInteractionService.java revision e912ac012e7146e9b0e8589bd9d88790e55372e3
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.os.Bundle;
24import android.os.IBinder;
25import android.os.RemoteException;
26import android.os.ServiceManager;
27
28import com.android.internal.app.IVoiceInteractionManagerService;
29
30/**
31 * Top-level service of the current global voice interactor, which is providing
32 * support for hotwording, the back-end of a {@link android.app.VoiceInteractor}, etc.
33 * The current VoiceInteractionService that has been selected by the user is kept
34 * always running by the system, to allow it to do things like listen for hotwords
35 * in the background to instigate voice interactions.
36 *
37 * <p>Because this service is always running, it should be kept as lightweight as
38 * possible.  Heavy-weight operations (including showing UI) should be implemented
39 * in the associated {@link android.service.voice.VoiceInteractionSessionService} when
40 * an actual voice interaction is taking place, and that service should run in a
41 * separate process from this one.
42 */
43public class VoiceInteractionService extends Service {
44    /**
45     * The {@link Intent} that must be declared as handled by the service.
46     * To be supported, the service must also require the
47     * {@link android.Manifest.permission#BIND_VOICE_INTERACTION} permission so
48     * that other applications can not abuse it.
49     */
50    @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
51    public static final String SERVICE_INTERFACE =
52            "android.service.voice.VoiceInteractionService";
53
54    /**
55     * Name under which a VoiceInteractionService component publishes information about itself.
56     * This meta-data should reference an XML resource containing a
57     * <code>&lt;{@link
58     * android.R.styleable#VoiceInteractionService voice-interaction-service}&gt;</code> tag.
59     */
60    public static final String SERVICE_META_DATA = "android.voice_interaction";
61
62    IVoiceInteractionService mInterface = new IVoiceInteractionService.Stub() {
63    };
64
65    IVoiceInteractionManagerService mSystemService;
66
67    private DspInfo mDspInfo;
68    private KeyphraseEnrollmentInfo mKeyphraseEnrollmentInfo;
69
70    public void startSession(Bundle args) {
71        try {
72            mSystemService.startSession(mInterface, args);
73        } catch (RemoteException e) {
74        }
75    }
76
77    @Override
78    public void onCreate() {
79        super.onCreate();
80        mSystemService = IVoiceInteractionManagerService.Stub.asInterface(
81                ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE));
82        mKeyphraseEnrollmentInfo = new KeyphraseEnrollmentInfo(getPackageManager());
83        // TODO(sansid): Read mDspInfo from the SoundTriggerModel API.
84    }
85
86    @Override
87    public IBinder onBind(Intent intent) {
88        if (SERVICE_INTERFACE.equals(intent.getAction())) {
89            return mInterface.asBinder();
90        }
91        return null;
92    }
93
94    /**
95     * Indicates if always-on hotword detection is available for the given keyphrase and locale
96     * on this system.
97     * Availability implies that the hardware on this system is capable of listening for
98     * the given keyphrase or not.
99     * @param keyphrase The keyphrase whose availability is being checked.
100     * @param locale The locale for which the availability is being checked.
101     * @return Indicates if always-on hotword detection is available for the given keyphrase.
102     * TODO(sansid): Unhide this.
103     * @hide
104     */
105    public final boolean isAlwaysOnHotwordAvailable(String keyphrase, String locale) {
106        // The available keyphrases is a combination of DSP availability and
107        // the keyphrases that have an enrollment application for them.
108        return mDspInfo != null
109                && mKeyphraseEnrollmentInfo.isKeyphraseEnrollmentSupported(keyphrase, locale);
110    }
111}
112