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 
17 package com.android.internal.app;
18 
19 import android.content.ComponentName;
20 import android.content.Intent;
21 import android.hardware.soundtrigger.KeyphraseMetadata;
22 import android.hardware.soundtrigger.SoundTrigger;
23 import android.media.AudioFormat;
24 import android.media.permission.Identity;
25 import android.os.Bundle;
26 import android.os.ParcelFileDescriptor;
27 import android.os.PersistableBundle;
28 import android.os.RemoteCallback;
29 import android.os.SharedMemory;
30 import android.service.voice.IVisualQueryDetectionVoiceInteractionCallback;
31 import android.service.voice.IMicrophoneHotwordDetectionVoiceInteractionCallback;
32 import android.service.voice.IVoiceInteractionService;
33 import android.service.voice.IVoiceInteractionSession;
34 import android.service.voice.VisibleActivityInfo;
35 
36 import com.android.internal.app.IHotwordRecognitionStatusCallback;
37 import com.android.internal.app.IVoiceActionCheckCallback;
38 import com.android.internal.app.IVoiceInteractionSessionListener;
39 import com.android.internal.app.IVoiceInteractionSessionShowCallback;
40 import com.android.internal.app.IVoiceInteractionSoundTriggerSession;
41 import com.android.internal.app.IVoiceInteractor;
42 import com.android.internal.app.IVisualQueryDetectionAttentionListener;
43 import com.android.internal.app.IVisualQueryRecognitionStatusListener;
44 
45 interface IVoiceInteractionManagerService {
showSession(in Bundle sessionArgs, int flags, String attributionTag)46     void showSession(in Bundle sessionArgs, int flags, String attributionTag);
deliverNewSession(IBinder token, IVoiceInteractionSession session, IVoiceInteractor interactor)47     boolean deliverNewSession(IBinder token, IVoiceInteractionSession session,
48             IVoiceInteractor interactor);
showSessionFromSession(IBinder token, in Bundle sessionArgs, int flags, String attributionTag)49     boolean showSessionFromSession(IBinder token, in Bundle sessionArgs, int flags,
50             String attributionTag);
hideSessionFromSession(IBinder token)51     boolean hideSessionFromSession(IBinder token);
startVoiceActivity(IBinder token, in Intent intent, String resolvedType, String attributionTag)52     int startVoiceActivity(IBinder token, in Intent intent, String resolvedType,
53             String attributionTag);
startAssistantActivity(IBinder token, in Intent intent, String resolvedType, String attributionTag, in Bundle bundle)54     int startAssistantActivity(IBinder token, in Intent intent, String resolvedType,
55             String attributionTag, in Bundle bundle);
setKeepAwake(IBinder token, boolean keepAwake)56     void setKeepAwake(IBinder token, boolean keepAwake);
closeSystemDialogs(IBinder token)57     void closeSystemDialogs(IBinder token);
finish(IBinder token)58     void finish(IBinder token);
setDisabledShowContext(int flags)59     void setDisabledShowContext(int flags);
getDisabledShowContext()60     int getDisabledShowContext();
getUserDisabledShowContext()61     int getUserDisabledShowContext();
62 
63     /**
64      * Gets the registered Sound model for keyphrase detection for the current user.
65      * May be null if no matching sound model exists.
66      * Caller must either be the active voice interaction service via
67      * {@link Settings.Secure.VOICE_INTERACTION_SERVICE}, or the caller must be a voice model
68      * enrollment application detected by
69      * {@link android.hardware.soundtrigger.KeyphraseEnrollmentInfo}.
70      *
71      * @param keyphraseId The unique identifier for the keyphrase.
72      * @param bcp47Locale The BCP47 language tag  for the keyphrase's locale.
73      * @RequiresPermission Manifest.permission.MANAGE_VOICE_KEYPHRASES
74      */
75     @UnsupportedAppUsage(maxTargetSdk = 30, trackingBug = 170729553)
getKeyphraseSoundModel(int keyphraseId, in String bcp47Locale)76     SoundTrigger.KeyphraseSoundModel getKeyphraseSoundModel(int keyphraseId, in String bcp47Locale);
77     /**
78      * Add/Update the given keyphrase sound model for the current user.
79      * Caller must either be the active voice interaction service via
80      * {@link Settings.Secure.VOICE_INTERACTION_SERVICE}, or the caller must be a voice model
81      * enrollment application detected by
82      * {@link android.hardware.soundtrigger.KeyphraseEnrollmentInfo}.
83      *
84      * @param model The keyphrase sound model to store peristantly.
85      * @RequiresPermission Manifest.permission.MANAGE_VOICE_KEYPHRASES
86      */
updateKeyphraseSoundModel(in SoundTrigger.KeyphraseSoundModel model)87     int updateKeyphraseSoundModel(in SoundTrigger.KeyphraseSoundModel model);
88     /**
89      * Deletes the given keyphrase sound model for the current user.
90      * Caller must either be the active voice interaction service via
91      * {@link Settings.Secure.VOICE_INTERACTION_SERVICE}, or the caller must be a voice model
92      * enrollment application detected by
93      * {@link android.hardware.soundtrigger.KeyphraseEnrollmentInfo}.
94      *
95      * @param keyphraseId The unique identifier for the keyphrase.
96      * @param bcp47Locale The BCP47 language tag  for the keyphrase's locale.
97      * @RequiresPermission Manifest.permission.MANAGE_VOICE_KEYPHRASES
98      */
deleteKeyphraseSoundModel(int keyphraseId, in String bcp47Locale)99     int deleteKeyphraseSoundModel(int keyphraseId, in String bcp47Locale);
100 
101     /**
102      * Override the persistent enrolled model database with an in-memory
103      * fake for testing purposes.
104      *
105      * @param enabled - {@code true} to enable the test database. {@code false} to enable
106      * the real, persistent database.
107      * @param token - IBinder used to register a death listener to clean-up the override
108      * if tests do not clean up gracefully.
109      */
110     @EnforcePermission("MANAGE_VOICE_KEYPHRASES")
111     @JavaPassthrough(annotation= "@android.annotation.RequiresPermission(" +
112             "android.Manifest.permission.MANAGE_VOICE_KEYPHRASES)")
setModelDatabaseForTestEnabled(boolean enabled, IBinder token)113     void setModelDatabaseForTestEnabled(boolean enabled, IBinder token);
114 
115     /**
116      * Indicates if there's a keyphrase sound model available for the given keyphrase ID and the
117      * user ID of the caller.
118      * Caller must be the active voice interaction service via
119      * {@link Settings.Secure.VOICE_INTERACTION_SERVICE}.
120      *
121      * @param keyphraseId The unique identifier for the keyphrase.
122      * @param bcp47Locale The BCP47 language tag  for the keyphrase's locale.
123      */
isEnrolledForKeyphrase(int keyphraseId, String bcp47Locale)124     boolean isEnrolledForKeyphrase(int keyphraseId, String bcp47Locale);
125 
126     /**
127      * Generates KeyphraseMetadata for an enrolled sound model based on keyphrase string, locale,
128      * and the user ID of the caller.
129      * Caller must be the active voice interaction service via
130      * {@link Settings.Secure.VOICE_INTERACTION_SERVICE}.
131      *
132      * @param keyphrase Keyphrase text associated with the enrolled model
133      * @param bcp47Locale The BCP47 language tag for the keyphrase's locale.
134      * @return The metadata for the enrolled voice model bassed on the passed in parameters. Null if
135      *         no matching voice model exists.
136      */
getEnrolledKeyphraseMetadata(String keyphrase, String bcp47Locale)137     KeyphraseMetadata getEnrolledKeyphraseMetadata(String keyphrase, String bcp47Locale);
138     /**
139      * @return the component name for the currently active voice interaction service
140      */
getActiveServiceComponentName()141     ComponentName getActiveServiceComponentName();
142 
143     /**
144      * Shows the session for the currently active service. Used to start a new session from system
145      * affordances.
146      *
147      * @param args the bundle to pass as arguments to the voice interaction session
148      * @param sourceFlags flags indicating the source of this show
149      * @param attributionTag the attribution tag of the calling context or {@code null} for default
150      *                       attribution
151      * @param showCallback optional callback to be notified when the session was shown
152      * @param activityToken optional token of activity that needs to be on top
153      * @RequiresPermission Manifest.permission.ACCESS_VOICE_INTERACTION_SERVICE
154      */
155     @EnforcePermission("ACCESS_VOICE_INTERACTION_SERVICE")
showSessionForActiveService(in Bundle args, int sourceFlags, String attributionTag, IVoiceInteractionSessionShowCallback showCallback, IBinder activityToken)156     boolean showSessionForActiveService(in Bundle args, int sourceFlags, String attributionTag,
157             IVoiceInteractionSessionShowCallback showCallback, IBinder activityToken);
158 
159     /**
160      * Hides the session from the active service, if it is showing.
161      * @RequiresPermission Manifest.permission.ACCESS_VOICE_INTERACTION_SERVICE
162      */
163     @EnforcePermission("ACCESS_VOICE_INTERACTION_SERVICE")
hideCurrentSession()164     void hideCurrentSession();
165 
166     /**
167      * Notifies the active service that a launch was requested from the Keyguard. This will only
168      * be called if {@link #activeServiceSupportsLaunchFromKeyguard()} returns true.
169      * @RequiresPermission Manifest.permission.ACCESS_VOICE_INTERACTION_SERVICE
170      */
171     @EnforcePermission("ACCESS_VOICE_INTERACTION_SERVICE")
launchVoiceAssistFromKeyguard()172     void launchVoiceAssistFromKeyguard();
173 
174     /**
175      * Indicates whether there is a voice session running (but not necessarily showing).
176      * @RequiresPermission Manifest.permission.ACCESS_VOICE_INTERACTION_SERVICE
177      */
178     @EnforcePermission("ACCESS_VOICE_INTERACTION_SERVICE")
isSessionRunning()179     boolean isSessionRunning();
180 
181     /**
182      * Indicates whether the currently active voice interaction service is capable of handling the
183      * assist gesture.
184      * @RequiresPermission Manifest.permission.ACCESS_VOICE_INTERACTION_SERVICE
185      */
186     @EnforcePermission("ACCESS_VOICE_INTERACTION_SERVICE")
activeServiceSupportsAssist()187     boolean activeServiceSupportsAssist();
188 
189     /**
190      * Indicates whether the currently active voice interaction service is capable of being launched
191      * from the lockscreen.
192      * @RequiresPermission Manifest.permission.ACCESS_VOICE_INTERACTION_SERVICE
193      */
194     @EnforcePermission("ACCESS_VOICE_INTERACTION_SERVICE")
activeServiceSupportsLaunchFromKeyguard()195     boolean activeServiceSupportsLaunchFromKeyguard();
196 
197     /**
198      * Called when the lockscreen got shown.
199      * @RequiresPermission Manifest.permission.ACCESS_VOICE_INTERACTION_SERVICE
200      */
201     @EnforcePermission("ACCESS_VOICE_INTERACTION_SERVICE")
onLockscreenShown()202     void onLockscreenShown();
203 
204     /**
205      * Register a voice interaction listener.
206      * @RequiresPermission Manifest.permission.ACCESS_VOICE_INTERACTION_SERVICE
207      */
208     @EnforcePermission("ACCESS_VOICE_INTERACTION_SERVICE")
registerVoiceInteractionSessionListener(IVoiceInteractionSessionListener listener)209     void registerVoiceInteractionSessionListener(IVoiceInteractionSessionListener listener);
210 
211     /**
212      * Checks the availability of a set of voice actions for the current active voice service.
213      * Returns all supported voice actions.
214      * @RequiresPermission Manifest.permission.ACCESS_VOICE_INTERACTION_SERVICE
215      */
216     @EnforcePermission("ACCESS_VOICE_INTERACTION_SERVICE")
getActiveServiceSupportedActions(in List<String> voiceActions, in IVoiceActionCheckCallback callback)217     void getActiveServiceSupportedActions(in List<String> voiceActions,
218      in IVoiceActionCheckCallback callback);
219 
220     /**
221      * Provide hints for showing UI.
222      * Caller must be the active voice interaction service via
223      * {@link Settings.Secure.VOICE_INTERACTION_SERVICE}.
224      */
setUiHints(in Bundle hints)225     void setUiHints(in Bundle hints);
226 
227     /**
228      * Requests a list of supported actions from a specific activity.
229      */
requestDirectActions(in IBinder token, int taskId, IBinder assistToken, in RemoteCallback cancellationCallback, in RemoteCallback callback)230     void requestDirectActions(in IBinder token, int taskId, IBinder assistToken,
231              in RemoteCallback cancellationCallback, in RemoteCallback callback);
232 
233     /**
234      * Requests performing an action from a specific activity.
235      */
performDirectAction(in IBinder token, String actionId, in Bundle arguments, int taskId, IBinder assistToken, in RemoteCallback cancellationCallback, in RemoteCallback resultCallback)236     void performDirectAction(in IBinder token, String actionId, in Bundle arguments, int taskId,
237             IBinder assistToken, in RemoteCallback cancellationCallback,
238             in RemoteCallback resultCallback);
239 
240     /**
241      * Temporarily disables voice interaction (for example, on Automotive when the display is off).
242      *
243      * It will shutdown the service, and only re-enable it after it's called again (or after a
244      * system restart).
245      *
246      * NOTE: it's only effective when the service itself is available / enabled in the device, so
247      * calling setDisable(false) would be a no-op when it isn't.
248      */
249     @EnforcePermission("ACCESS_VOICE_INTERACTION_SERVICE")
setDisabled(boolean disabled)250     void setDisabled(boolean disabled);
251 
252     /**
253      * Creates a session, allowing controlling running sound models on detection hardware.
254      * Caller must provide an identity, used for permission tracking purposes.
255      * The uid/pid elements of the identity will be ignored by the server and replaced with the ones
256      * provided by binder.
257      *
258      * The client argument is any binder owned by the client, used for tracking is death and
259      * cleaning up in this event.
260      */
createSoundTriggerSessionAsOriginator( in Identity originatorIdentity, IBinder client, in SoundTrigger.ModuleProperties moduleProperties)261     IVoiceInteractionSoundTriggerSession createSoundTriggerSessionAsOriginator(
262             in Identity originatorIdentity,
263             IBinder client,
264             in SoundTrigger.ModuleProperties moduleProperties);
265 
266     /**
267      * Lists properties of SoundTrigger modules that can be attached to by
268      * @{link createSoundTriggerSessionAsOriginator}.
269      */
listModuleProperties(in Identity originatorIdentity)270     List<SoundTrigger.ModuleProperties> listModuleProperties(in Identity originatorIdentity);
271 
272     /**
273      * Set configuration and pass read-only data to hotword detection service.
274      *
275      * @param options Application configuration data to provide to the
276      * {@link HotwordDetectionService}. PersistableBundle does not allow any remotable objects or
277      * other contents that can be used to communicate with other processes.
278      * @param sharedMemory The unrestricted data blob to provide to the
279      * {@link HotwordDetectionService}. Use this to provide the hotword models data or other
280      * such data to the trusted process.
281      * @param token Use this to identify which detector calls this method.
282      */
283     @EnforcePermission("MANAGE_HOTWORD_DETECTION")
updateState( in PersistableBundle options, in SharedMemory sharedMemory, in IBinder token)284     void updateState(
285             in PersistableBundle options,
286             in SharedMemory sharedMemory,
287             in IBinder token);
288 
289     /**
290      * Set configuration and pass read-only data to hotword detection service when creating
291      * the detector.
292      *
293      * Caller must provide an identity, used for permission tracking purposes.
294      * The uid/pid elements of the identity will be ignored by the server and replaced with the ones
295      * provided by binder.
296      *
297      * @param options Application configuration data to provide to the
298      * {@link HotwordDetectionService}. PersistableBundle does not allow any remotable objects or
299      * other contents that can be used to communicate with other processes.
300      * @param sharedMemory The unrestricted data blob to provide to the
301      * {@link HotwordDetectionService}. Use this to provide the hotword models data or other
302      * such data to the trusted process.
303      * @param token Use this to identify which detector calls this method.
304      * @param callback Use this to report {@link HotwordDetectionService} status.
305      * @param detectorType Indicate which detector is used.
306      */
307     @EnforcePermission("MANAGE_HOTWORD_DETECTION")
initAndVerifyDetector( in Identity originatorIdentity, in PersistableBundle options, in SharedMemory sharedMemory, in IBinder token, in IHotwordRecognitionStatusCallback callback, int detectorType)308     void initAndVerifyDetector(
309             in Identity originatorIdentity,
310             in PersistableBundle options,
311             in SharedMemory sharedMemory,
312             in IBinder token,
313             in IHotwordRecognitionStatusCallback callback,
314             int detectorType);
315 
316     /**
317      * Destroy the detector callback.
318      *
319      * @param token Indicate which callback will be destroyed.
320      */
destroyDetector(in IBinder token)321     void destroyDetector(in IBinder token);
322 
323     /**
324      * Requests to shutdown hotword detection service.
325      */
shutdownHotwordDetectionService()326     void shutdownHotwordDetectionService();
327 
328     @EnforcePermission("ACCESS_VOICE_INTERACTION_SERVICE")
subscribeVisualQueryRecognitionStatus(in IVisualQueryRecognitionStatusListener listener)329     void subscribeVisualQueryRecognitionStatus(in IVisualQueryRecognitionStatusListener listener);
330 
331     @EnforcePermission("ACCESS_VOICE_INTERACTION_SERVICE")
enableVisualQueryDetection(in IVisualQueryDetectionAttentionListener Listener)332     void enableVisualQueryDetection(in IVisualQueryDetectionAttentionListener Listener);
333 
334     @EnforcePermission("ACCESS_VOICE_INTERACTION_SERVICE")
disableVisualQueryDetection()335     void disableVisualQueryDetection();
336 
startPerceiving(in IVisualQueryDetectionVoiceInteractionCallback callback)337     void startPerceiving(in IVisualQueryDetectionVoiceInteractionCallback callback);
338 
stopPerceiving()339     void stopPerceiving();
340 
startListeningFromMic( in AudioFormat audioFormat, in IMicrophoneHotwordDetectionVoiceInteractionCallback callback)341     void startListeningFromMic(
342         in AudioFormat audioFormat,
343         in IMicrophoneHotwordDetectionVoiceInteractionCallback callback);
344 
stopListeningFromMic()345     void stopListeningFromMic();
346 
startListeningFromExternalSource( in ParcelFileDescriptor audioStream, in AudioFormat audioFormat, in PersistableBundle options, in IBinder token, in IMicrophoneHotwordDetectionVoiceInteractionCallback callback)347     void startListeningFromExternalSource(
348         in ParcelFileDescriptor audioStream,
349         in AudioFormat audioFormat,
350         in PersistableBundle options,
351         in IBinder token,
352         in IMicrophoneHotwordDetectionVoiceInteractionCallback callback);
353 
354     /**
355      * Test API to simulate to trigger hardware recognition event for test.
356      */
triggerHardwareRecognitionEventForTest( in SoundTrigger.KeyphraseRecognitionEvent event, in IHotwordRecognitionStatusCallback callback)357     void triggerHardwareRecognitionEventForTest(
358             in SoundTrigger.KeyphraseRecognitionEvent event,
359             in IHotwordRecognitionStatusCallback callback);
360 
361     /**
362      * Starts to listen the status of visible activity.
363      */
startListeningVisibleActivityChanged(in IBinder token)364     void startListeningVisibleActivityChanged(in IBinder token);
365 
366     /**
367      * Stops to listen the status of visible activity.
368      */
stopListeningVisibleActivityChanged(in IBinder token)369     void stopListeningVisibleActivityChanged(in IBinder token);
370 
371     /**
372      * Notifies when the session window is shown or hidden.
373      */
setSessionWindowVisible(in IBinder token, boolean visible)374     void setSessionWindowVisible(in IBinder token, boolean visible);
375 
376     /**
377      * Notifies when the Activity lifecycle event changed.
378      *
379      * @param activityToken The token of activity.
380      * @param type The type of lifecycle event of the activity lifecycle.
381      */
notifyActivityEventChanged( in IBinder activityToken, int type)382     oneway void notifyActivityEventChanged(
383             in IBinder activityToken,
384             int type);
385 }
386