Debian Codegraph @

core/java/android/service/voice/VoiceInteractionService.java

raw symbols SCIP
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 android.service.voice;
18
19 import android.Manifest;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.RequiresPermission;
23 import android.annotation.SdkConstant;
24 import android.annotation.SuppressLint;
25 import android.annotation.SystemApi;
26 import android.app.Service;
27 import android.compat.annotation.UnsupportedAppUsage;
28 import android.content.ComponentName;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.hardware.soundtrigger.KeyphraseEnrollmentInfo;
32 import android.media.voice.KeyphraseModelManager;
33 import android.os.Bundle;
34 import android.os.Handler;
35 import android.os.IBinder;
36 import android.os.PersistableBundle;
37 import android.os.RemoteException;
38 import android.os.ServiceManager;
39 import android.os.SharedMemory;
40 import android.provider.Settings;
41 import android.util.ArraySet;
42 import android.util.Log;
43
44 import com.android.internal.annotations.VisibleForTesting;
45 import com.android.internal.app.IVoiceActionCheckCallback;
46 import com.android.internal.app.IVoiceInteractionManagerService;
47 import com.android.internal.util.function.pooled.PooledLambda;
48
49 import java.io.FileDescriptor;
50 import java.io.PrintWriter;
51 import java.util.ArrayList;
52 import java.util.Collections;
53 import java.util.List;
54 import java.util.Locale;
55 import java.util.Objects;
56 import java.util.Set;
57
58 /**
59 * Top-level service of the current global voice interactor, which is providing
60 * support for hotwording, the back-end of a {@link android.app.VoiceInteractor}, etc.
61 * The current VoiceInteractionService that has been selected by the user is kept
62 * always running by the system, to allow it to do things like listen for hotwords
63 * in the background to instigate voice interactions.
64 *
65 * <p>Because this service is always running, it should be kept as lightweight as
66 * possible. Heavy-weight operations (including showing UI) should be implemented
67 * in the associated {@link android.service.voice.VoiceInteractionSessionService} when
68 * an actual voice interaction is taking place, and that service should run in a
69 * separate process from this one.
70 */
71 public class VoiceInteractionService extends Service {
72 static final String TAG = VoiceInteractionService.class.getSimpleName();
73
74 /**
75 * The {@link Intent} that must be declared as handled by the service.
76 * To be supported, the service must also require the
77 * {@link android.Manifest.permission#BIND_VOICE_INTERACTION} permission so
78 * that other applications can not abuse it.
79 */
80 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
81 public static final String SERVICE_INTERFACE =
82 "android.service.voice.VoiceInteractionService";
83
84 /**
85 * Name under which a VoiceInteractionService component publishes information about itself.
86 * This meta-data should reference an XML resource containing a
87 * <code>&lt;{@link
88 * android.R.styleable#VoiceInteractionService voice-interaction-service}&gt;</code> tag.
89 */
90 public static final String SERVICE_META_DATA = "android.voice_interaction";
91
92 IVoiceInteractionService mInterface = new IVoiceInteractionService.Stub() {
93 @Override
94 public void ready() {
95 Handler.getMain().executeOrSendMessage(PooledLambda.obtainMessage(
96 VoiceInteractionService::onReady, VoiceInteractionService.this));
97 }
98
99 @Override
100 public void shutdown() {
101 Handler.getMain().executeOrSendMessage(PooledLambda.obtainMessage(
102 VoiceInteractionService::onShutdownInternal, VoiceInteractionService.this));
103 }
104
105 @Override
106 public void soundModelsChanged() {
107 Handler.getMain().executeOrSendMessage(PooledLambda.obtainMessage(
108 VoiceInteractionService::onSoundModelsChangedInternal,
109 VoiceInteractionService.this));
110 }
111
112 @Override
113 public void launchVoiceAssistFromKeyguard() {
114 Handler.getMain().executeOrSendMessage(PooledLambda.obtainMessage(
115 VoiceInteractionService::onLaunchVoiceAssistFromKeyguard,
116 VoiceInteractionService.this));
117 }
118
119 @Override
120 public void getActiveServiceSupportedActions(List<String> voiceActions,
121 IVoiceActionCheckCallback callback) {
122 Handler.getMain().executeOrSendMessage(
123 PooledLambda.obtainMessage(VoiceInteractionService::onHandleVoiceActionCheck,
124 VoiceInteractionService.this,
125 voiceActions,
126 callback));
127 }
128 };
129
130 IVoiceInteractionManagerService mSystemService;
131
132 private final Object mLock = new Object();
133
134 private KeyphraseEnrollmentInfo mKeyphraseEnrollmentInfo;
135
136 private AlwaysOnHotwordDetector mHotwordDetector;
137 private SoftwareHotwordDetector mSoftwareHotwordDetector;
138
139 /**
140 * Called when a user has activated an affordance to launch voice assist from the Keyguard.
141 *
142 * <p>This method will only be called if the VoiceInteractionService has set
143 * {@link android.R.attr#supportsLaunchVoiceAssistFromKeyguard} and the Keyguard is showing.</p>
144 *
145 * <p>A valid implementation must start a new activity that should use {@link
146 * android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED} to display
147 * on top of the lock screen.</p>
148 */
149 public void onLaunchVoiceAssistFromKeyguard() {
150 }
151
152 /**
153 * Check whether the given service component is the currently active
154 * VoiceInteractionService.
155 */
156 public static boolean isActiveService(Context context, ComponentName service) {
157 String cur = Settings.Secure.getString(context.getContentResolver(),
158 Settings.Secure.VOICE_INTERACTION_SERVICE);
159 if (cur == null || cur.isEmpty()) {
160 return false;
161 }
162 ComponentName curComp = ComponentName.unflattenFromString(cur);
163 if (curComp == null) {
164 return false;
165 }
166 return curComp.equals(service);
167 }
168
169 /**
170 * Set contextual options you would always like to have disabled when a session
171 * is shown. The flags may be any combination of
172 * {@link VoiceInteractionSession#SHOW_WITH_ASSIST VoiceInteractionSession.SHOW_WITH_ASSIST} and
173 * {@link VoiceInteractionSession#SHOW_WITH_SCREENSHOT
174 * VoiceInteractionSession.SHOW_WITH_SCREENSHOT}.
175 */
176 public void setDisabledShowContext(int flags) {
177 try {
178 mSystemService.setDisabledShowContext(flags);
179 } catch (RemoteException e) {
180 }
181 }
182
183 /**
184 * Return the value set by {@link #setDisabledShowContext}.
185 */
186 public int getDisabledShowContext() {
187 try {
188 return mSystemService.getDisabledShowContext();
189 } catch (RemoteException e) {
190 return 0;
191 }
192 }
193
194 /**
195 * Request that the associated {@link android.service.voice.VoiceInteractionSession} be
196 * shown to the user, starting it if necessary.
197 * @param args Arbitrary arguments that will be propagated to the session.
198 * @param flags Indicates additional optional behavior that should be performed. May
199 * be any combination of
200 * {@link VoiceInteractionSession#SHOW_WITH_ASSIST VoiceInteractionSession.SHOW_WITH_ASSIST} and
201 * {@link VoiceInteractionSession#SHOW_WITH_SCREENSHOT
202 * VoiceInteractionSession.SHOW_WITH_SCREENSHOT}
203 * to request that the system generate and deliver assist data on the current foreground
204 * app as part of showing the session UI.
205 */
206 public void showSession(Bundle args, int flags) {
207 if (mSystemService == null) {
208 throw new IllegalStateException("Not available until onReady() is called");
209 }
210 try {
211 mSystemService.showSession(args, flags);
212 } catch (RemoteException e) {
213 }
214 }
215
216 /**
217 * Request to query for what extended voice actions this service supports. This method will
218 * be called when the system checks the supported actions of this
219 * {@link VoiceInteractionService}. Supported actions may be delivered to
220 * {@link VoiceInteractionSession} later to request a session to perform an action.
221 *
222 * <p>Voice actions are defined in support libraries and could vary based on platform context.
223 * For example, car related voice actions will be defined in car support libraries.
224 *
225 * @param voiceActions A set of checked voice actions.
226 * @return Returns a subset of checked voice actions. Additional voice actions in the
227 * returned set will be ignored. Returns empty set if no actions are supported.
228 */
229 @NonNull
230 public Set<String> onGetSupportedVoiceActions(@NonNull Set<String> voiceActions) {
231 return Collections.emptySet();
232 }
233
234 @Override
235 public IBinder onBind(Intent intent) {
236 if (SERVICE_INTERFACE.equals(intent.getAction())) {
237 return mInterface.asBinder();
238 }
239 return null;
240 }
241
242 /**
243 * Called during service initialization to tell you when the system is ready
244 * to receive interaction from it. You should generally do initialization here
245 * rather than in {@link #onCreate}. Methods such as {@link #showSession} will
246 * not be operational until this point.
247 */
248 public void onReady() {
249 mSystemService = IVoiceInteractionManagerService.Stub.asInterface(
250 ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE));
251 Objects.requireNonNull(mSystemService);
252 try {
253 mSystemService.asBinder().linkToDeath(mDeathRecipient, 0);
254 } catch (RemoteException e) {
255 Log.wtf(TAG, "unable to link to death with system service");
256 }
257 mKeyphraseEnrollmentInfo = new KeyphraseEnrollmentInfo(getPackageManager());
258 }
259
260 private IBinder.DeathRecipient mDeathRecipient = () -> {
261 Log.e(TAG, "system service binder died shutting down");
262 Handler.getMain().executeOrSendMessage(PooledLambda.obtainMessage(
263 VoiceInteractionService::onShutdownInternal, VoiceInteractionService.this));
264 };
265
266
267 private void onShutdownInternal() {
268 onShutdown();
269 // Stop any active recognitions when shutting down.
270 // This ensures that if implementations forget to stop any active recognition,
271 // It's still guaranteed to have been stopped.
272 // This helps with cases where the voice interaction implementation is changed
273 // by the user.
274 safelyShutdownAllHotwordDetectors();
275 }
276
277 /**
278 * Called during service de-initialization to tell you when the system is shutting the
279 * service down.
280 * At this point this service may no longer be the active {@link VoiceInteractionService}.
281 */
282 public void onShutdown() {
283 }
284
285 private void onSoundModelsChangedInternal() {
286 synchronized (this) {
287 if (mHotwordDetector != null) {
288 // TODO: Stop recognition if a sound model that was being recognized gets deleted.
289 mHotwordDetector.onSoundModelsChanged();
290 }
291 }
292 }
293
294 private void onHandleVoiceActionCheck(List<String> voiceActions,
295 IVoiceActionCheckCallback callback) {
296 if (callback != null) {
297 try {
298 Set<String> voiceActionsSet = new ArraySet<>(voiceActions);
299 Set<String> resultSet = onGetSupportedVoiceActions(voiceActionsSet);
300 callback.onComplete(new ArrayList<>(resultSet));
301 } catch (RemoteException e) {
302 }
303 }
304 }
305
306 /**
307 * Creates an {@link AlwaysOnHotwordDetector} for the given keyphrase and locale.
308 * This instance must be retained and used by the client.
309 * Calling this a second time invalidates the previously created hotword detector
310 * which can no longer be used to manage recognition.
311 *
312 * @param keyphrase The keyphrase that's being used, for example "Hello Android".
313 * @param locale The locale for which the enrollment needs to be performed.
314 * @param callback The callback to notify of detection events.
315 * @return An always-on hotword detector for the given keyphrase and locale.
316 *
317 * @hide
318 */
319 @SystemApi
320 @NonNull
321 public final AlwaysOnHotwordDetector createAlwaysOnHotwordDetector(
322 @SuppressLint("MissingNullability") String keyphrase, // TODO: nullability properly
323 @SuppressLint({"MissingNullability", "UseIcu"}) Locale locale,
324 @SuppressLint("MissingNullability") AlwaysOnHotwordDetector.Callback callback) {
325 return createAlwaysOnHotwordDetectorInternal(keyphrase, locale,
326 /* supportHotwordDetectionService= */ false, /* options= */ null,
327 /* sharedMemory= */ null, callback);
328 }
329
330 /**
331 * Create an {@link AlwaysOnHotwordDetector} and trigger a {@link HotwordDetectionService}
332 * service, then it will also pass the read-only data to hotword detection service.
333 *
334 * Like {@see #createAlwaysOnHotwordDetector(String, Locale, AlwaysOnHotwordDetector.Callback)
335 * }. Before calling this function, you should set a valid hotword detection service with
336 * android:hotwordDetectionService in an android.voice_interaction metadata file and set
337 * android:isolatedProcess="true" in the AndroidManifest.xml of hotword detection service.
338 * Otherwise it will throw IllegalStateException. After calling this function, the system will
339 * also trigger a hotword detection service and pass the read-only data back to it.
340 *
341 * <p>Note: The system will trigger hotword detection service after calling this function when
342 * all conditions meet the requirements.
343 *
344 * @param keyphrase The keyphrase that's being used, for example "Hello Android".
345 * @param locale The locale for which the enrollment needs to be performed.
346 * @param options Application configuration data provided by the
347 * {@link VoiceInteractionService}. PersistableBundle does not allow any remotable objects or
348 * other contents that can be used to communicate with other processes.
349 * @param sharedMemory The unrestricted data blob provided by the
350 * {@link VoiceInteractionService}. Use this to provide the hotword models data or other
351 * such data to the trusted process.
352 * @param callback The callback to notify of detection events.
353 * @return An always-on hotword detector for the given keyphrase and locale.
354 *
355 * @hide
356 */
357 @SystemApi
358 @RequiresPermission(Manifest.permission.MANAGE_HOTWORD_DETECTION)
359 @NonNull
360 public final AlwaysOnHotwordDetector createAlwaysOnHotwordDetector(
361 @SuppressLint("MissingNullability") String keyphrase, // TODO: nullability properly
362 @SuppressLint({"MissingNullability", "UseIcu"}) Locale locale,
363 @Nullable PersistableBundle options,
364 @Nullable SharedMemory sharedMemory,
365 @SuppressLint("MissingNullability") AlwaysOnHotwordDetector.Callback callback) {
366 return createAlwaysOnHotwordDetectorInternal(keyphrase, locale,
367 /* supportHotwordDetectionService= */ true, options,
368 sharedMemory, callback);
369 }
370
371 private AlwaysOnHotwordDetector createAlwaysOnHotwordDetectorInternal(
372 @SuppressLint("MissingNullability") String keyphrase, // TODO: nullability properly
373 @SuppressLint({"MissingNullability", "UseIcu"}) Locale locale,
374 boolean supportHotwordDetectionService,
375 @Nullable PersistableBundle options,
376 @Nullable SharedMemory sharedMemory,
377 @SuppressLint("MissingNullability") AlwaysOnHotwordDetector.Callback callback) {
378 if (mSystemService == null) {
379 throw new IllegalStateException("Not available until onReady() is called");
380 }
381 synchronized (mLock) {
382 // Allow only one concurrent recognition via the APIs.
383 safelyShutdownAllHotwordDetectors();
384 mHotwordDetector = new AlwaysOnHotwordDetector(keyphrase, locale, callback,
385 mKeyphraseEnrollmentInfo, mSystemService,
386 getApplicationContext().getApplicationInfo().targetSdkVersion,
387 supportHotwordDetectionService, options, sharedMemory);
388 mHotwordDetector.registerOnDestroyListener((detector) -> onDspHotwordDetectorDestroyed(
389 (AlwaysOnHotwordDetector) detector));
390 }
391 return mHotwordDetector;
392 }
393
394 /**
395 * Creates a {@link HotwordDetector} and initializes the application's
396 * {@link HotwordDetectionService} using {@code options} and {code sharedMemory}.
397 *
398 * <p>To be able to call this, you need to set android:hotwordDetectionService in the
399 * android.voice_interaction metadata file to a valid hotword detection service, and set
400 * android:isolatedProcess="true" in the hotword detection service's declaration. Otherwise,
401 * this throws an {@link IllegalStateException}.
402 *
403 * <p>This instance must be retained and used by the client.
404 * Calling this a second time invalidates the previously created hotword detector
405 * which can no longer be used to manage recognition.
406 *
407 * <p>Using this has a noticeable impact on battery, since the microphone is kept open
408 * for the lifetime of the recognition {@link HotwordDetector#startRecognition() session}. On
409 * devices where hardware filtering is available (such as through a DSP), it's highly
410 * recommended to use {@link #createAlwaysOnHotwordDetector} instead.
411 *
412 * @param options Application configuration data to be provided to the
413 * {@link HotwordDetectionService}. PersistableBundle does not allow any remotable objects or
414 * other contents that can be used to communicate with other processes.
415 * @param sharedMemory The unrestricted data blob to be provided to the
416 * {@link HotwordDetectionService}. Use this to provide hotword models or other such data to the
417 * sandboxed process.
418 * @param callback The callback to notify of detection events.
419 * @return A hotword detector for the given audio format.
420 *
421 * @see #createAlwaysOnHotwordDetector(String, Locale, PersistableBundle, SharedMemory,
422 * AlwaysOnHotwordDetector.Callback)
423 *
424 * @hide
425 */
426 @SystemApi
427 @RequiresPermission(Manifest.permission.MANAGE_HOTWORD_DETECTION)
428 @NonNull
429 public final HotwordDetector createHotwordDetector(
430 @Nullable PersistableBundle options,
431 @Nullable SharedMemory sharedMemory,
432 @NonNull HotwordDetector.Callback callback) {
433 if (mSystemService == null) {
434 throw new IllegalStateException("Not available until onReady() is called");
435 }
436 synchronized (mLock) {
437 // Allow only one concurrent recognition via the APIs.
438 safelyShutdownAllHotwordDetectors();
439 mSoftwareHotwordDetector =
440 new SoftwareHotwordDetector(
441 mSystemService, null, options, sharedMemory, callback);
442 mSoftwareHotwordDetector.registerOnDestroyListener(
443 (detector) -> onMicrophoneHotwordDetectorDestroyed(
444 (SoftwareHotwordDetector) detector));
445 }
446 return mSoftwareHotwordDetector;
447 }
448
449 /**
450 * Creates an {@link KeyphraseModelManager} to use for enrolling voice models outside of the
451 * pre-bundled system voice models.
452 * @hide
453 */
454 @SystemApi
455 @RequiresPermission(Manifest.permission.MANAGE_VOICE_KEYPHRASES)
456 @NonNull
457 public final KeyphraseModelManager createKeyphraseModelManager() {
458 if (mSystemService == null) {
459 throw new IllegalStateException("Not available until onReady() is called");
460 }
461 synchronized (mLock) {
462 return new KeyphraseModelManager(mSystemService);
463 }
464 }
465
466 /**
467 * @return Details of keyphrases available for enrollment.
468 * @hide
469 */
470 @VisibleForTesting
471 protected final KeyphraseEnrollmentInfo getKeyphraseEnrollmentInfo() {
472 return mKeyphraseEnrollmentInfo;
473 }
474
475 /**
476 * Checks if a given keyphrase and locale are supported to create an
477 * {@link AlwaysOnHotwordDetector}.
478 *
479 * @return true if the keyphrase and locale combination is supported, false otherwise.
480 * @hide
481 */
482 @UnsupportedAppUsage
483 public final boolean isKeyphraseAndLocaleSupportedForHotword(String keyphrase, Locale locale) {
484 if (mKeyphraseEnrollmentInfo == null) {
485 return false;
486 }
487 return mKeyphraseEnrollmentInfo.getKeyphraseMetadata(keyphrase, locale) != null;
488 }
489
490 private void safelyShutdownAllHotwordDetectors() {
491 synchronized (mLock) {
492 if (mHotwordDetector != null) {
493 try {
494 mHotwordDetector.destroy();
495 } catch (Exception ex) {
496 Log.i(TAG, "exception destroying AlwaysOnHotwordDetector", ex);
497 }
498 }
499
500 if (mSoftwareHotwordDetector != null) {
501 try {
502 mSoftwareHotwordDetector.destroy();
503 } catch (Exception ex) {
504 Log.i(TAG, "exception destroying SoftwareHotwordDetector", ex);
505 }
506 }
507 }
508 }
509
510 private void onDspHotwordDetectorDestroyed(@NonNull AlwaysOnHotwordDetector detector) {
511 synchronized (mLock) {
512 mHotwordDetector = null;
513 }
514 }
515
516 private void onMicrophoneHotwordDetectorDestroyed(@NonNull SoftwareHotwordDetector detector) {
517 synchronized (mLock) {
518 mSoftwareHotwordDetector = null;
519 }
520 }
521
522 /**
523 * Provide hints to be reflected in the system UI.
524 *
525 * @param hints Arguments used to show UI.
526 */
527 public final void setUiHints(@NonNull Bundle hints) {
528 if (hints == null) {
529 throw new IllegalArgumentException("Hints must be non-null");
530 }
531
532 try {
533 mSystemService.setUiHints(hints);
534 } catch (RemoteException e) {
535 throw e.rethrowFromSystemServer();
536 }
537 }
538
539 @Override
540 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
541 pw.println("VOICE INTERACTION");
542 synchronized (mLock) {
543 pw.println(" AlwaysOnHotwordDetector");
544 if (mHotwordDetector == null) {
545 pw.println(" NULL");
546 } else {
547 mHotwordDetector.dump(" ", pw);
548 }
549
550 pw.println(" MicrophoneHotwordDetector");
551 if (mSoftwareHotwordDetector == null) {
552 pw.println(" NULL");
553 } else {
554 mSoftwareHotwordDetector.dump(" ", pw);
555 }
556 }
557 }
558 }