VoiceInteractionService.java revision 22968950b814e66a6aa119ea92ae648884cbe0d9
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.annotations.VisibleForTesting; 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 private DspInfo mDspInfo; 69 private KeyphraseEnrollmentInfo mKeyphraseEnrollmentInfo; 70 71 public void startSession(Bundle args) { 72 try { 73 mSystemService.startSession(mInterface, args); 74 } catch (RemoteException e) { 75 } 76 } 77 78 @Override 79 public void onCreate() { 80 super.onCreate(); 81 mSystemService = IVoiceInteractionManagerService.Stub.asInterface( 82 ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE)); 83 mKeyphraseEnrollmentInfo = new KeyphraseEnrollmentInfo(getPackageManager()); 84 // TODO(sansid): Read mDspInfo from the SoundTriggerModel API. 85 } 86 87 @Override 88 public IBinder onBind(Intent intent) { 89 if (SERVICE_INTERFACE.equals(intent.getAction())) { 90 return mInterface.asBinder(); 91 } 92 return null; 93 } 94 95 /** 96 * Indicates if always-on hotword detection is available for the given keyphrase and locale 97 * on this system. 98 * Availability implies that the hardware on this system is capable of listening for 99 * the given keyphrase or not. 100 * @param keyphrase The keyphrase whose availability is being checked. 101 * @param locale The locale for which the availability is being checked. 102 * @return Indicates if always-on hotword detection is available for the given keyphrase. 103 * TODO(sansid): Unhide this. 104 * @hide 105 */ 106 public final boolean isAlwaysOnHotwordAvailable(String keyphrase, String locale) { 107 // The available keyphrases is a combination of DSP availability and 108 // the keyphrases that have an enrollment application for them. 109 return mDspInfo != null 110 && mKeyphraseEnrollmentInfo.isKeyphraseEnrollmentSupported(keyphrase, locale); 111 } 112 113 /** 114 * @return Details of keyphrases available for enrollment. 115 * @hide 116 */ 117 @VisibleForTesting 118 protected final KeyphraseEnrollmentInfo getKeyphraseEnrollmentInfo() { 119 return mKeyphraseEnrollmentInfo; 120 } 121} 122