Merge "Make Trusted Hotword session permissions follow previous behavior" into sc-dev

This commit is contained in:
Ahaan Ugale
2021-08-06 19:21:30 +00:00
committed by Android (Google) Code Review
3 changed files with 26 additions and 9 deletions

View File

@@ -783,6 +783,9 @@ public class AlwaysOnHotwordDetector extends AbstractHotwordDetector {
* This may happen if another detector has been instantiated or the * This may happen if another detector has been instantiated or the
* {@link VoiceInteractionService} hosting this detector has been shut down. * {@link VoiceInteractionService} hosting this detector has been shut down.
*/ */
// TODO: Remove this RequiresPermission since it isn't actually enforced. Also fix the javadoc
// about permissions enforcement (when it throws vs when it just returns false) for other
// methods in this class.
@RequiresPermission(allOf = {RECORD_AUDIO, CAPTURE_AUDIO_HOTWORD}) @RequiresPermission(allOf = {RECORD_AUDIO, CAPTURE_AUDIO_HOTWORD})
@Override @Override
public boolean stopRecognition() { public boolean stopRecognition() {

View File

@@ -82,6 +82,9 @@ class SoftwareHotwordDetector extends AbstractHotwordDetector {
try { try {
mManagerService.startListeningFromMic( mManagerService.startListeningFromMic(
mAudioFormat, new BinderCallback(mHandler, mCallback)); mAudioFormat, new BinderCallback(mHandler, mCallback));
} catch (SecurityException e) {
Slog.e(TAG, "startRecognition failed: " + e);
return false;
} catch (RemoteException e) { } catch (RemoteException e) {
e.rethrowFromSystemServer(); e.rethrowFromSystemServer();
} }

View File

@@ -60,7 +60,7 @@ final class SoundTriggerSessionPermissionsDecorator implements
@Override @Override
public SoundTrigger.ModuleProperties getDspModuleProperties() throws RemoteException { public SoundTrigger.ModuleProperties getDspModuleProperties() throws RemoteException {
// No permission needed. // No permission needed here (the app must have the Assistant Role to retrieve the session).
return mDelegate.getDspModuleProperties(); return mDelegate.getDspModuleProperties();
} }
@@ -71,7 +71,9 @@ final class SoundTriggerSessionPermissionsDecorator implements
if (DEBUG) { if (DEBUG) {
Slog.d(TAG, "startRecognition"); Slog.d(TAG, "startRecognition");
} }
enforcePermissions(); if (!isHoldingPermissions()) {
return SoundTrigger.STATUS_PERMISSION_DENIED;
}
return mDelegate.startRecognition(i, s, iHotwordRecognitionStatusCallback, return mDelegate.startRecognition(i, s, iHotwordRecognitionStatusCallback,
recognitionConfig, b); recognitionConfig, b);
} }
@@ -80,25 +82,28 @@ final class SoundTriggerSessionPermissionsDecorator implements
public int stopRecognition(int i, public int stopRecognition(int i,
IHotwordRecognitionStatusCallback iHotwordRecognitionStatusCallback) IHotwordRecognitionStatusCallback iHotwordRecognitionStatusCallback)
throws RemoteException { throws RemoteException {
enforcePermissions(); // Stopping a model does not require special permissions. Having a handle to the session is
// sufficient.
return mDelegate.stopRecognition(i, iHotwordRecognitionStatusCallback); return mDelegate.stopRecognition(i, iHotwordRecognitionStatusCallback);
} }
@Override @Override
public int setParameter(int i, int i1, int i2) throws RemoteException { public int setParameter(int i, int i1, int i2) throws RemoteException {
enforcePermissions(); if (!isHoldingPermissions()) {
return SoundTrigger.STATUS_PERMISSION_DENIED;
}
return mDelegate.setParameter(i, i1, i2); return mDelegate.setParameter(i, i1, i2);
} }
@Override @Override
public int getParameter(int i, int i1) throws RemoteException { public int getParameter(int i, int i1) throws RemoteException {
enforcePermissions(); // No permission needed here (the app must have the Assistant Role to retrieve the session).
return mDelegate.getParameter(i, i1); return mDelegate.getParameter(i, i1);
} }
@Override @Override
public SoundTrigger.ModelParamRange queryParameter(int i, int i1) throws RemoteException { public SoundTrigger.ModelParamRange queryParameter(int i, int i1) throws RemoteException {
enforcePermissions(); // No permission needed here (the app must have the Assistant Role to retrieve the session).
return mDelegate.queryParameter(i, i1); return mDelegate.queryParameter(i, i1);
} }
@@ -109,9 +114,15 @@ final class SoundTriggerSessionPermissionsDecorator implements
} }
// TODO: Share this code with SoundTriggerMiddlewarePermission. // TODO: Share this code with SoundTriggerMiddlewarePermission.
private void enforcePermissions() { private boolean isHoldingPermissions() {
try {
enforcePermissionForPreflight(mContext, mOriginatorIdentity, RECORD_AUDIO); enforcePermissionForPreflight(mContext, mOriginatorIdentity, RECORD_AUDIO);
enforcePermissionForPreflight(mContext, mOriginatorIdentity, CAPTURE_AUDIO_HOTWORD); enforcePermissionForPreflight(mContext, mOriginatorIdentity, CAPTURE_AUDIO_HOTWORD);
return true;
} catch (SecurityException e) {
Slog.e(TAG, e.toString());
return false;
}
} }
/** /**