VoiceInteractionService.java revision f7a13df899c30ddddbbf63274bc28174f6391f29
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/** 33 * Top-level service of the current global voice interactor, which is providing 34 * support for hotwording, the back-end of a {@link android.app.VoiceInteractor}, etc. 35 * The current VoiceInteractionService that has been selected by the user is kept 36 * always running by the system, to allow it to do things like listen for hotwords 37 * in the background to instigate voice interactions. 38 * 39 * <p>Because this service is always running, it should be kept as lightweight as 40 * possible. Heavy-weight operations (including showing UI) should be implemented 41 * in the associated {@link android.service.voice.VoiceInteractionSessionService} when 42 * an actual voice interaction is taking place, and that service should run in a 43 * separate process from this one. 44 */ 45public class VoiceInteractionService extends Service { 46 /** 47 * The {@link Intent} that must be declared as handled by the service. 48 * To be supported, the service must also require the 49 * {@link android.Manifest.permission#BIND_VOICE_INTERACTION} permission so 50 * that other applications can not abuse it. 51 */ 52 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) 53 public static final String SERVICE_INTERFACE = 54 "android.service.voice.VoiceInteractionService"; 55 56 // TODO(sansid): Unhide these. 57 /** @hide */ 58 public static final int KEYPHRASE_UNAVAILABLE = 0; 59 /** @hide */ 60 public static final int KEYPHRASE_UNENROLLED = 1; 61 /** @hide */ 62 public static final int KEYPHRASE_ENROLLED = 2; 63 /** @hide */ 64 public static final int KEYPHRASE_ACTIVE = 3; 65 66 /** 67 * Name under which a VoiceInteractionService component publishes information about itself. 68 * This meta-data should reference an XML resource containing a 69 * <code><{@link 70 * android.R.styleable#VoiceInteractionService voice-interaction-service}></code> tag. 71 */ 72 public static final String SERVICE_META_DATA = "android.voice_interaction"; 73 74 IVoiceInteractionService mInterface = new IVoiceInteractionService.Stub() { 75 }; 76 77 IVoiceInteractionManagerService mSystemService; 78 79 private SoundTriggerManager mSoundTriggerManager; 80 private KeyphraseEnrollmentInfo mKeyphraseEnrollmentInfo; 81 82 public void startSession(Bundle args) { 83 try { 84 mSystemService.startSession(mInterface, args); 85 } catch (RemoteException e) { 86 } 87 } 88 89 @Override 90 public void onCreate() { 91 super.onCreate(); 92 mSystemService = IVoiceInteractionManagerService.Stub.asInterface( 93 ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE)); 94 mKeyphraseEnrollmentInfo = new KeyphraseEnrollmentInfo(getPackageManager()); 95 mSoundTriggerManager = new SoundTriggerManager(); 96 } 97 98 @Override 99 public IBinder onBind(Intent intent) { 100 if (SERVICE_INTERFACE.equals(intent.getAction())) { 101 return mInterface.asBinder(); 102 } 103 return null; 104 } 105 106 /** 107 * Gets the state of always-on hotword detection for the given keyphrase and locale 108 * on this system. 109 * Availability implies that the hardware on this system is capable of listening for 110 * the given keyphrase or not. 111 * The return code is one of {@link #KEYPHRASE_UNAVAILABLE}, {@link #KEYPHRASE_UNENROLLED} 112 * {@link #KEYPHRASE_ENROLLED} or {@link #KEYPHRASE_ACTIVE}. 113 * 114 * @param keyphrase The keyphrase whose availability is being checked. 115 * @param locale The locale for which the availability is being checked. 116 * @return Indicates if always-on hotword detection is available for the given keyphrase. 117 * TODO(sansid): Unhide this. 118 * @hide 119 */ 120 public final int getAlwaysOnKeyphraseAvailability(String keyphrase, String locale) { 121 // The available keyphrases is a combination of DSP availability and 122 // the keyphrases that have an enrollment application for them. 123 if (!mSoundTriggerManager.isKeyphraseSupported(keyphrase, locale) 124 || !mKeyphraseEnrollmentInfo.isKeyphraseEnrollmentSupported(keyphrase, locale)) { 125 return KEYPHRASE_UNAVAILABLE; 126 } 127 if (!mSoundTriggerManager.isKeyphraseEnrolled(keyphrase, locale)) { 128 return KEYPHRASE_UNENROLLED; 129 } 130 if (!mSoundTriggerManager.isKeyphraseActive(keyphrase, locale)) { 131 return KEYPHRASE_ENROLLED; 132 } else { 133 return KEYPHRASE_ACTIVE; 134 } 135 } 136 137 /** 138 * @return Details of keyphrases available for enrollment. 139 * @hide 140 */ 141 @VisibleForTesting 142 protected final KeyphraseEnrollmentInfo getKeyphraseEnrollmentInfo() { 143 return mKeyphraseEnrollmentInfo; 144 } 145} 146