diff --git a/media/java/android/media/soundtrigger/SoundTriggerManager.java b/media/java/android/media/soundtrigger/SoundTriggerManager.java index b6d70afa85b78..7835287118772 100644 --- a/media/java/android/media/soundtrigger/SoundTriggerManager.java +++ b/media/java/android/media/soundtrigger/SoundTriggerManager.java @@ -54,7 +54,6 @@ import android.util.Slog; import com.android.internal.app.ISoundTriggerService; import com.android.internal.app.ISoundTriggerSession; -import com.android.internal.util.Preconditions; import java.util.HashMap; import java.util.List; @@ -67,6 +66,8 @@ import java.util.concurrent.Executor; * models. Usage of this class is restricted to system or signature applications only. This allows * OEMs to write apps that can manage non-voice based sound trigger models. * + * If no ST module is available, {@link getModuleProperties()} will return {@code null}, and all + * other methods will throw {@link IllegalStateException}. * @hide */ @SystemApi @@ -82,7 +83,7 @@ public final class SoundTriggerManager { // Stores a mapping from the sound model UUID to the SoundTriggerInstance created by // the createSoundTriggerDetector() call. - private final HashMap mReceiverInstanceMap; + private final HashMap mReceiverInstanceMap = new HashMap<>(); /** * @hide @@ -121,7 +122,6 @@ public final class SoundTriggerManager { } mContext = context; mSoundTriggerService = soundTriggerService; - mReceiverInstanceMap = new HashMap(); } /** @@ -160,7 +160,7 @@ public final class SoundTriggerManager { .findFirst() .orElse(null); if (moduleProps == null) { - throw new IllegalStateException("Fake ST HAL should always be available"); + throw new AssertionError("Fake ST HAL should always be available"); } return moduleProps; } @@ -183,7 +183,6 @@ public final class SoundTriggerManager { } mContext = Objects.requireNonNull(context); mSoundTriggerService = Objects.requireNonNull(soundTriggerService); - mReceiverInstanceMap = new HashMap(); } /** @@ -241,7 +240,8 @@ public final class SoundTriggerManager { } try { GenericSoundModel model = - mSoundTriggerSession.getSoundModel(new ParcelUuid(soundModelId)); + mSoundTriggerSession.getSoundModel( + new ParcelUuid(Objects.requireNonNull(soundModelId))); if (model == null) { return null; } @@ -265,7 +265,8 @@ public final class SoundTriggerManager { } try { - mSoundTriggerSession.deleteSoundModel(new ParcelUuid(soundModelId)); + mSoundTriggerSession.deleteSoundModel( + new ParcelUuid(Objects.requireNonNull(soundModelId))); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -291,8 +292,8 @@ public final class SoundTriggerManager { @RequiresPermission(android.Manifest.permission.MANAGE_SOUND_TRIGGER) public SoundTriggerDetector createSoundTriggerDetector(UUID soundModelId, @NonNull SoundTriggerDetector.Callback callback, @Nullable Handler handler) { - if (soundModelId == null || mSoundTriggerSession == null) { - return null; + if (mSoundTriggerSession == null) { + throw new IllegalStateException("No underlying SoundTriggerModule available"); } SoundTriggerDetector oldInstance = mReceiverInstanceMap.get(soundModelId); @@ -301,7 +302,8 @@ public final class SoundTriggerManager { } try { SoundTriggerDetector newInstance = new SoundTriggerDetector(mSoundTriggerSession, - mSoundTriggerSession.getSoundModel(new ParcelUuid(soundModelId)), + mSoundTriggerSession.getSoundModel( + new ParcelUuid(Objects.requireNonNull(soundModelId))), callback, handler); mReceiverInstanceMap.put(soundModelId, newInstance); return newInstance; @@ -469,8 +471,8 @@ public final class SoundTriggerManager { @UnsupportedAppUsage @TestApi public int loadSoundModel(@NonNull SoundModel soundModel) { - if (soundModel == null || mSoundTriggerSession == null) { - return STATUS_ERROR; + if (mSoundTriggerSession == null) { + throw new IllegalStateException("No underlying SoundTriggerModule available"); } try { @@ -513,11 +515,11 @@ public final class SoundTriggerManager { @UnsupportedAppUsage public int startRecognition(@NonNull UUID soundModelId, @Nullable Bundle params, @NonNull ComponentName detectionService, @NonNull RecognitionConfig config) { - Preconditions.checkNotNull(soundModelId); - Preconditions.checkNotNull(detectionService); - Preconditions.checkNotNull(config); + Objects.requireNonNull(soundModelId); + Objects.requireNonNull(detectionService); + Objects.requireNonNull(config); if (mSoundTriggerSession == null) { - return STATUS_ERROR; + throw new IllegalStateException("No underlying SoundTriggerModule available"); } try { return mSoundTriggerSession.startRecognitionForService(new ParcelUuid(soundModelId), @@ -534,11 +536,12 @@ public final class SoundTriggerManager { @RequiresPermission(android.Manifest.permission.MANAGE_SOUND_TRIGGER) @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) public int stopRecognition(UUID soundModelId) { - if (soundModelId == null || mSoundTriggerSession == null) { - return STATUS_ERROR; + if (mSoundTriggerSession == null) { + throw new IllegalStateException("No underlying SoundTriggerModule available"); } try { - return mSoundTriggerSession.stopRecognitionForService(new ParcelUuid(soundModelId)); + return mSoundTriggerSession.stopRecognitionForService( + new ParcelUuid(Objects.requireNonNull(soundModelId))); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -551,12 +554,12 @@ public final class SoundTriggerManager { @RequiresPermission(android.Manifest.permission.MANAGE_SOUND_TRIGGER) @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) public int unloadSoundModel(UUID soundModelId) { - if (soundModelId == null || mSoundTriggerSession == null) { - return STATUS_ERROR; + if (mSoundTriggerSession == null) { + throw new IllegalStateException("No underlying SoundTriggerModule available"); } try { return mSoundTriggerSession.unloadSoundModel( - new ParcelUuid(soundModelId)); + new ParcelUuid(Objects.requireNonNull(soundModelId))); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -604,7 +607,10 @@ public final class SoundTriggerManager { @RequiresPermission(android.Manifest.permission.MANAGE_SOUND_TRIGGER) @UnsupportedAppUsage public int getModelState(UUID soundModelId) { - if (soundModelId == null || mSoundTriggerSession == null) { + if (mSoundTriggerSession == null) { + throw new IllegalStateException("No underlying SoundTriggerModule available"); + } + if (soundModelId == null) { return STATUS_ERROR; } try { @@ -652,11 +658,12 @@ public final class SoundTriggerManager { public int setParameter(@Nullable UUID soundModelId, @ModelParams int modelParam, int value) { if (mSoundTriggerSession == null) { - return SoundTrigger.STATUS_INVALID_OPERATION; + throw new IllegalStateException("No underlying SoundTriggerModule available"); } try { - return mSoundTriggerSession.setParameter(new ParcelUuid(soundModelId), modelParam, + return mSoundTriggerSession.setParameter( + new ParcelUuid(Objects.requireNonNull(soundModelId)), modelParam, value); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); @@ -679,11 +686,11 @@ public final class SoundTriggerManager { public int getParameter(@NonNull UUID soundModelId, @ModelParams int modelParam) { if (mSoundTriggerSession == null) { - throw new IllegalArgumentException("Sound model is not loaded: " - + soundModelId.toString()); + throw new IllegalStateException("No underlying SoundTriggerModule available"); } try { - return mSoundTriggerSession.getParameter(new ParcelUuid(soundModelId), modelParam); + return mSoundTriggerSession.getParameter( + new ParcelUuid(Objects.requireNonNull(soundModelId)), modelParam); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -703,10 +710,11 @@ public final class SoundTriggerManager { public ModelParamRange queryParameter(@Nullable UUID soundModelId, @ModelParams int modelParam) { if (mSoundTriggerSession == null) { - return null; + throw new IllegalStateException("No underlying SoundTriggerModule available"); } try { - return mSoundTriggerSession.queryParameter(new ParcelUuid(soundModelId), modelParam); + return mSoundTriggerSession.queryParameter( + new ParcelUuid(Objects.requireNonNull(soundModelId)), modelParam); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); }