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

Commit 25ec8c6d authored by Ytai Ben-Tsvi's avatar Ytai Ben-Tsvi
Browse files

Extract some event utilities

Those will be used in several places.

Test: Compile
Change-Id: I9d08cdf68bc6093d0a03d9149a607c8992813232
parent 8ea0eb70
Loading
Loading
Loading
Loading
+71 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.soundtrigger_middleware;

import android.media.soundtrigger.PhraseRecognitionEvent;
import android.media.soundtrigger.PhraseRecognitionExtra;
import android.media.soundtrigger.RecognitionEvent;
import android.media.soundtrigger.RecognitionStatus;
import android.media.soundtrigger.SoundModelType;

/**
 * Utilities for working with sound trigger related AIDL generated types.
 */
public class AidlUtil {
    /**
     * Initialize a new recognition event.
     * @return The new event.
     */
    static RecognitionEvent newEmptyRecognitionEvent() {
        RecognitionEvent result = new RecognitionEvent();
        result.data = new byte[0];
        return result;
    }

    /**
     * Initialize a new phrase recognition event.
     * @return The new event.
     */
    static PhraseRecognitionEvent newEmptyPhraseRecognitionEvent() {
        PhraseRecognitionEvent result = new PhraseRecognitionEvent();
        result.common = newEmptyRecognitionEvent();
        result.phraseExtras = new PhraseRecognitionExtra[0];
        return result;
    }

    /**
     * Creates a new generic abort event.
     * @return The new event.
     */
    static RecognitionEvent newAbortEvent() {
        RecognitionEvent event = newEmptyRecognitionEvent();
        event.type = SoundModelType.GENERIC;
        event.status = RecognitionStatus.ABORTED;
        return event;
    }

    /**
     * Creates a new generic phrase event.
     * @return The new event.
     */
    static PhraseRecognitionEvent newAbortPhraseEvent() {
        PhraseRecognitionEvent event = newEmptyPhraseRecognitionEvent();
        event.common.type = SoundModelType.KEYPHRASE;
        event.common.status = RecognitionStatus.ABORTED;
        return event;
    }
}
+8 −30
Original line number Diff line number Diff line
@@ -20,12 +20,10 @@ import android.annotation.NonNull;
import android.media.permission.SafeCloseable;
import android.media.soundtrigger.ModelParameterRange;
import android.media.soundtrigger.PhraseRecognitionEvent;
import android.media.soundtrigger.PhraseRecognitionExtra;
import android.media.soundtrigger.PhraseSoundModel;
import android.media.soundtrigger.Properties;
import android.media.soundtrigger.RecognitionConfig;
import android.media.soundtrigger.RecognitionEvent;
import android.media.soundtrigger.RecognitionStatus;
import android.media.soundtrigger.SoundModel;
import android.media.soundtrigger.SoundModelType;
import android.media.soundtrigger.Status;
@@ -392,20 +390,13 @@ public class SoundTriggerHalConcurrentCaptureHandler implements ISoundTriggerHal
    /** Notify the client that recognition has been aborted. */
    private static void notifyAbort(int modelHandle, LoadedModel model) {
        switch (model.type) {
            case SoundModelType.GENERIC: {
                RecognitionEvent event = newEmptyRecognitionEvent();
                event.status = RecognitionStatus.ABORTED;
                event.type = SoundModelType.GENERIC;
                model.callback.recognitionCallback(modelHandle, event);
            }
            case SoundModelType.GENERIC:
                model.callback.recognitionCallback(modelHandle, AidlUtil.newAbortEvent());
                break;

            case SoundModelType.KEYPHRASE: {
                PhraseRecognitionEvent event = newEmptyPhraseRecognitionEvent();
                event.common.status = RecognitionStatus.ABORTED;
                event.common.type = SoundModelType.KEYPHRASE;
                model.callback.phraseRecognitionCallback(modelHandle, event);
            }
            case SoundModelType.KEYPHRASE:
                model.callback.phraseRecognitionCallback(modelHandle,
                        AidlUtil.newAbortPhraseEvent());
                break;
        }
    }
@@ -416,19 +407,6 @@ public class SoundTriggerHalConcurrentCaptureHandler implements ISoundTriggerHal
        mNotifier.unregisterListener(this);
    }

    private static PhraseRecognitionEvent newEmptyPhraseRecognitionEvent() {
        PhraseRecognitionEvent result = new PhraseRecognitionEvent();
        result.common = newEmptyRecognitionEvent();
        result.phraseExtras = new PhraseRecognitionExtra[0];
        return result;
    }

    private static RecognitionEvent newEmptyRecognitionEvent() {
        RecognitionEvent result = new RecognitionEvent();
        result.data = new byte[0];
        return result;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////
    // All methods below do trivial delegation - no interesting logic.
    @Override