VoiceInteractionService.java revision c03c9167c2d9a1e22fb2b176b00a0524177fb037
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.Instrumentation;
21import android.app.Service;
22import android.content.Context;
23import android.content.Intent;
24import android.os.Bundle;
25import android.os.IBinder;
26import android.os.RemoteException;
27import android.os.ServiceManager;
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    public void startSession(Bundle args) {
68        try {
69            mSystemService.startSession(mInterface, args);
70        } catch (RemoteException e) {
71        }
72    }
73
74    @Override
75    public void onCreate() {
76        super.onCreate();
77        mSystemService = IVoiceInteractionManagerService.Stub.asInterface(
78                ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE));
79    }
80
81    @Override
82    public IBinder onBind(Intent intent) {
83        if (SERVICE_INTERFACE.equals(intent.getAction())) {
84            return mInterface.asBinder();
85        }
86        return null;
87    }
88}
89