VoiceInteractionService.java revision e5706787599f8397e19e66afa70753ba350cc91b
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; 28 29import com.android.internal.app.IVoiceInteractionManagerService; 30 31/** 32 * Top-level service of the current global voice interactor, which is providing 33 * support for hotwording, the back-end of a {@link android.app.VoiceInteractor}, etc. 34 * The current VoiceInteractionService that has been selected by the user is kept 35 * always running by the system, to allow it to do things like listen for hotwords 36 * in the background to instigate voice interactions. 37 * 38 * <p>Because this service is always running, it should be kept as lightweight as 39 * possible. Heavy-weight operations (including showing UI) should be implemented 40 * in the associated {@link android.service.voice.VoiceInteractionSessionService} when 41 * an actual voice interaction is taking place, and that service should run in a 42 * separate process from this one. 43 */ 44public class VoiceInteractionService extends Service { 45 /** 46 * The {@link Intent} that must be declared as handled by the service. 47 * To be supported, the service must also require the 48 * {@link android.Manifest.permission#BIND_VOICE_INTERACTION} permission so 49 * that other applications can not abuse it. 50 */ 51 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) 52 public static final String SERVICE_INTERFACE = 53 "android.service.voice.VoiceInteractionService"; 54 55 /** 56 * Name under which a VoiceInteractionService component publishes information about itself. 57 * This meta-data should reference an XML resource containing a 58 * <code><{@link 59 * android.R.styleable#VoiceInteractionService voice-interaction-service}></code> tag. 60 */ 61 public static final String SERVICE_META_DATA = "android.voice_interaction"; 62 63 IVoiceInteractionService mInterface = new IVoiceInteractionService.Stub() { 64 }; 65 66 IVoiceInteractionManagerService mSystemService; 67 68 public void startSession(Bundle args) { 69 try { 70 mSystemService.startSession(mInterface, args); 71 } catch (RemoteException e) { 72 } 73 } 74 75 @Override 76 public void onCreate() { 77 super.onCreate(); 78 mSystemService = IVoiceInteractionManagerService.Stub.asInterface( 79 ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE)); 80 } 81 82 @Override 83 public IBinder onBind(Intent intent) { 84 if (SERVICE_INTERFACE.equals(intent.getAction())) { 85 return mInterface.asBinder(); 86 } 87 return null; 88 } 89} 90