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

Commit 0e2f629e authored by Ytai Ben-Tsvi's avatar Ytai Ben-Tsvi
Browse files

Use standard API in SoundTriggerTestApp

Avoid going directly to the service and use via the manager class
instead.

Change-Id: I1fe96c5e5f3bcce06308413e47bc64ab60acce8c
Bug: 163865561
parent 28ba5b97
Loading
Loading
Loading
Loading
+11 −11
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.hardware.soundtrigger.SoundTrigger.GenericSoundModel;
import android.media.AudioAttributes;
import android.media.AudioFormat;
import android.media.AudioManager;
@@ -31,6 +30,7 @@ import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaPlayer;
import android.media.soundtrigger.SoundTriggerDetector;
import android.media.soundtrigger.SoundTriggerManager;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
@@ -232,8 +232,8 @@ public class SoundTriggerTestService extends Service {
        public AudioTrack captureAudioTrack;
    }

    private GenericSoundModel createNewSoundModel(ModelInfo modelInfo) {
        return new GenericSoundModel(modelInfo.modelUuid, modelInfo.vendorUuid,
    private SoundTriggerManager.Model createNewSoundModel(ModelInfo modelInfo) {
        return SoundTriggerManager.Model.create(modelInfo.modelUuid, modelInfo.vendorUuid,
                modelInfo.modelData);
    }

@@ -246,16 +246,16 @@ public class SoundTriggerTestService extends Service {

        postMessage("Loading model: " + modelInfo.name);

        GenericSoundModel soundModel = createNewSoundModel(modelInfo);
        SoundTriggerManager.Model soundModel = createNewSoundModel(modelInfo);

        boolean status = mSoundTriggerUtil.addOrUpdateSoundModel(soundModel);
        if (status) {
            postToast("Successfully loaded " + modelInfo.name + ", UUID="
                    + soundModel.getUuid());
                    + soundModel.getModelUuid());
            setModelState(modelInfo, "Loaded");
        } else {
            postErrorToast("Failed to load " + modelInfo.name + ", UUID="
                    + soundModel.getUuid() + "!");
                    + soundModel.getModelUuid() + "!");
            setModelState(modelInfo, "Failed to load");
        }
    }
@@ -269,7 +269,7 @@ public class SoundTriggerTestService extends Service {

        postMessage("Unloading model: " + modelInfo.name);

        GenericSoundModel soundModel = mSoundTriggerUtil.getSoundModel(modelUuid);
        SoundTriggerManager.Model soundModel = mSoundTriggerUtil.getSoundModel(modelUuid);
        if (soundModel == null) {
            postErrorToast("Sound model not found for " + modelInfo.name + "!");
            return;
@@ -278,11 +278,11 @@ public class SoundTriggerTestService extends Service {
        boolean status = mSoundTriggerUtil.deleteSoundModel(modelUuid);
        if (status) {
            postToast("Successfully unloaded " + modelInfo.name + ", UUID="
                    + soundModel.getUuid());
                    + soundModel.getModelUuid());
            setModelState(modelInfo, "Unloaded");
        } else {
            postErrorToast("Failed to unload " +
                    modelInfo.name + ", UUID=" + soundModel.getUuid() + "!");
                    modelInfo.name + ", UUID=" + soundModel.getModelUuid() + "!");
            setModelState(modelInfo, "Failed to unload");
        }
    }
@@ -294,12 +294,12 @@ public class SoundTriggerTestService extends Service {
            return;
        }
        postMessage("Reloading model: " + modelInfo.name);
        GenericSoundModel soundModel = mSoundTriggerUtil.getSoundModel(modelUuid);
        SoundTriggerManager.Model soundModel = mSoundTriggerUtil.getSoundModel(modelUuid);
        if (soundModel == null) {
            postErrorToast("Sound model not found for " + modelInfo.name + "!");
            return;
        }
        GenericSoundModel updated = createNewSoundModel(modelInfo);
        SoundTriggerManager.Model updated = createNewSoundModel(modelInfo);
        boolean status = mSoundTriggerUtil.addOrUpdateSoundModel(updated);
        if (status) {
            postToast("Successfully reloaded " + modelInfo.name + ", UUID="
+9 −26
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package com.android.test.soundtrigger;

import android.annotation.Nullable;
import android.content.Context;
import android.hardware.soundtrigger.SoundTrigger.GenericSoundModel;
import android.media.soundtrigger.SoundTriggerDetector;
import android.media.soundtrigger.SoundTriggerManager;
import android.os.RemoteException;
@@ -37,13 +36,10 @@ import java.util.UUID;
public class SoundTriggerUtil {
    private static final String TAG = "SoundTriggerTestUtil";

    private final ISoundTriggerService mSoundTriggerService;
    private final SoundTriggerManager mSoundTriggerManager;
    private final Context mContext;

    public SoundTriggerUtil(Context context) {
        mSoundTriggerService = ISoundTriggerService.Stub.asInterface(
                ServiceManager.getService(Context.SOUND_TRIGGER_SERVICE));
        mSoundTriggerManager = (SoundTriggerManager) context.getSystemService(
                Context.SOUND_TRIGGER_SERVICE);
        mContext = context;
@@ -55,15 +51,11 @@ public class SoundTriggerUtil {
     *
     * @param soundModel The sound model to add/update.
     */
    public boolean addOrUpdateSoundModel(GenericSoundModel soundModel) {
        try {
    public boolean addOrUpdateSoundModel(SoundTriggerManager.Model soundModel) {
        if (soundModel == null) {
            throw new RuntimeException("Bad sound model");
        }
            mSoundTriggerService.updateSoundModel(soundModel);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in updateSoundModel", e);
        }
        mSoundTriggerManager.updateModel(soundModel);
        return true;
    }

@@ -71,19 +63,15 @@ public class SoundTriggerUtil {
     * Gets the sound model for the given keyphrase, null if none exists.
     * If a sound model for a given keyphrase exists, and it needs to be updated,
     * it should be obtained using this method, updated and then passed in to
     * {@link #addOrUpdateSoundModel(GenericSoundModel)} without changing the IDs.
     * {@link #addOrUpdateSoundModel(SoundTriggerManager.Model)} without changing the IDs.
     *
     * @param modelId The model ID to look-up the sound model for.
     * @return The sound model if one was found, null otherwise.
     */
    @Nullable
    public GenericSoundModel getSoundModel(UUID modelId) {
        GenericSoundModel model = null;
        try {
            model = mSoundTriggerService.getSoundModel(new ParcelUuid(modelId));
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in updateKeyphraseSoundModel");
        }
    public SoundTriggerManager.Model getSoundModel(UUID modelId) {
        SoundTriggerManager.Model model = null;
        model = mSoundTriggerManager.getModel(modelId);

        if (model == null) {
            Log.w(TAG, "No models present for the given keyphrase ID");
@@ -100,12 +88,7 @@ public class SoundTriggerUtil {
     * @return {@code true} if the call succeeds, {@code false} otherwise.
     */
    public boolean deleteSoundModel(UUID modelId) {
        try {
            mSoundTriggerService.deleteSoundModel(new ParcelUuid(modelId));
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in deleteSoundModel");
            return false;
        }
        mSoundTriggerManager.deleteModel(modelId);
        return true;
    }