Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c2ce3fc7 authored by Atneya Nair's avatar Atneya Nair
Browse files

Unify STManager behavior with no ST module

- Ensure all STManager methods throw IllegalState if they are called
when there is no STModule
- Retain getModuleProperties return null instead, as this behavior is
  used to determine if the STModule exists
- Minor cleanup to throw NPE if null is passed to several methods.
- AssertionError if fake HAL doesn't exist, since this should never
happen.

Bug: 272147641
Fixes: 279102299
Test: SoundTriggerManagerTest
Change-Id: Icf4a6a172b5bf706267586eb952960bae649b74c
parent 55dcabec
Loading
Loading
Loading
Loading
+38 −30
Original line number Diff line number Diff line
@@ -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<UUID, SoundTriggerDetector> mReceiverInstanceMap;
    private final HashMap<UUID, SoundTriggerDetector> mReceiverInstanceMap = new HashMap<>();

    /**
     * @hide
@@ -121,7 +122,6 @@ public final class SoundTriggerManager {
        }
        mContext = context;
        mSoundTriggerService = soundTriggerService;
        mReceiverInstanceMap = new HashMap<UUID, SoundTriggerDetector>();
    }

    /**
@@ -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<UUID, SoundTriggerDetector>();
    }

    /**
@@ -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();
        }