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

Commit 4d75ccd3 authored by Michael Dooley's avatar Michael Dooley Committed by Android (Google) Code Review
Browse files

Merge "Converting sound trigger v2.2 getModelState to be asynchronous"

parents 175214c4 b2ab04ae
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -611,6 +611,13 @@ public class SoundTrigger {
     * @hide
     */
    public static final int RECOGNITION_STATUS_FAILURE = 2;
    /**
     * Recognition event was triggered by a getModelState request, not by the
     * DSP.
     *
     * @hide
     */
    public static final int RECOGNITION_STATUS_GET_STATE_RESPONSE = 3;

    /**
     *  A RecognitionEvent is provided by the
+13 −4
Original line number Diff line number Diff line
@@ -133,12 +133,21 @@ public class SoundTriggerModule {
    public native int stopRecognition(int soundModelHandle);

    /**
     * Get the current state of a {@link SoundTrigger.SoundModel}
     * Get the current state of a {@link SoundTrigger.SoundModel}.
     * The state will be returned asynchronously as a {@link SoundTrigger#RecognitionEvent}
     * in the callback registered in the {@link SoundTrigger.startRecognition} method.
     * @param soundModelHandle The sound model handle indicating which model's state to return
     * @return - {@link SoundTrigger#RecognitionEvent} in case of success
     *         - null in case of an error or if not supported
     * @return - {@link SoundTrigger#STATUS_OK} in case of success
     *         - {@link SoundTrigger#STATUS_ERROR} in case of unspecified error
     *         - {@link SoundTrigger#STATUS_PERMISSION_DENIED} if the caller does not have
     *         system permission
     *         - {@link SoundTrigger#STATUS_NO_INIT} if the native service cannot be reached
     *         - {@link SoundTrigger#STATUS_BAD_VALUE} if the sound model handle is invalid
     *         - {@link SoundTrigger#STATUS_DEAD_OBJECT} if the binder transaction to the native
     *         service fails
     *         - {@link SoundTrigger#STATUS_INVALID_OPERATION} if the call is out of sequence
     */
    public native SoundTrigger.RecognitionEvent getModelState(int soundModelHandle);
    public native int getModelState(int soundModelHandle);

    private class NativeEventHandlerDelegate {
        private final Handler mHandler;
+1 −1
Original line number Diff line number Diff line
@@ -53,5 +53,5 @@ interface ISoundTriggerService {
    /** For both ...Intent and ...Service based usage */
    boolean isRecognitionActive(in ParcelUuid parcelUuid);

    SoundTrigger.RecognitionEvent getModelState(in ParcelUuid parcelUuid);
    int getModelState(in ParcelUuid soundModelId);
}
+6 −49
Original line number Diff line number Diff line
@@ -788,61 +788,18 @@ android_hardware_SoundTrigger_stopRecognition(JNIEnv *env, jobject thiz,
    return status;
}

static jobject
static jint
android_hardware_SoundTrigger_getModelState(JNIEnv *env, jobject thiz,
                                            jint jHandle)
{
    jint status = SOUNDTRIGGER_STATUS_OK;
    ALOGV("getModelState");
    sp<SoundTrigger> module = getSoundTrigger(env, thiz);
    if (module == NULL) {
        return NULL;
    }
    sp<IMemory> memory;
    jint status = module->getModelState(jHandle, memory);
    if (status != 0 || memory == NULL) {
        ALOGW("getModelState, failed to get model state, status: %d", status);
        return NULL;
    }
    struct sound_trigger_recognition_event* event =
        (struct sound_trigger_recognition_event *)memory->pointer();
    if (event == NULL) {
        return NULL;
    }
    if (event->type != SOUND_MODEL_TYPE_GENERIC) {
        ALOGW("getModelState, unsupported model type: %d", event->type);
        return NULL;
    }

    jbyteArray jData = NULL;
    if (event->data_size) {
        jData = env->NewByteArray(event->data_size);
        jbyte *nData = env->GetByteArrayElements(jData, NULL);
        memcpy(nData, (char *)event + event->data_offset, event->data_size);
        env->ReleaseByteArrayElements(jData, nData, 0);
    }

    jobject jAudioFormat = NULL;
    if (event->trigger_in_data || event->capture_available) {
        jAudioFormat = env->NewObject(gAudioFormatClass,
                                      gAudioFormatCstor,
                                      audioFormatFromNative(event->audio_config.format),
                                      event->audio_config.sample_rate,
                                      inChannelMaskFromNative(event->audio_config.channel_mask));

    }
    jobject jEvent = NULL;
    jEvent = env->NewObject(gGenericRecognitionEventClass, gGenericRecognitionEventCstor,
                            event->status, event->model, event->capture_available,
                            event->capture_session, event->capture_delay_ms,
                            event->capture_preamble_ms, event->trigger_in_data,
                            jAudioFormat, jData);
    if (jAudioFormat != NULL) {
        env->DeleteLocalRef(jAudioFormat);
    }
    if (jData != NULL) {
        env->DeleteLocalRef(jData);
        return SOUNDTRIGGER_STATUS_ERROR;
    }
    return jEvent;
    status = module->getModelState(jHandle);
    return status;
}

static const JNINativeMethod gMethods[] = {
@@ -875,7 +832,7 @@ static const JNINativeMethod gModuleMethods[] = {
        "(I)I",
        (void *)android_hardware_SoundTrigger_stopRecognition},
    {"getModelState",
        "(I)Landroid/hardware/soundtrigger/SoundTrigger$RecognitionEvent;",
        "(I)I",
        (void *)android_hardware_SoundTrigger_getModelState},
};

+5 −6
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@ import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.annotation.UnsupportedAppUsage;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.hardware.soundtrigger.SoundTrigger;
@@ -367,15 +366,15 @@ public final class SoundTriggerManager {
    }

    /**
     * Synchronously get state of the indicated model.  The model state is returned as
     * a recognition event, or null if the model is not loaded, or if this method
     * is not supported.
     * Asynchronously get state of the indicated model.  The model state is returned as
     * a recognition event in the callback that was registered in the startRecognition
     * method.
     * @hide
     */
    @RequiresPermission(android.Manifest.permission.MANAGE_SOUND_TRIGGER)
    public SoundTrigger.RecognitionEvent getModelState(UUID soundModelId) {
    public int getModelState(UUID soundModelId) {
        if (soundModelId == null) {
            return null;
            return STATUS_ERROR;
        }
        try {
            return mSoundTriggerService.getModelState(new ParcelUuid(soundModelId));
Loading