summaryrefslogtreecommitdiff
path: root/tests/SoundTriggerTestApp/src/com/android/test/soundtrigger/SoundTriggerUtil.java
blob: cfe8c855ac815bda377f1607704fe2972073e2da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (C) 2014 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.test.soundtrigger;

import android.annotation.Nullable;
import android.content.Context;
import android.media.soundtrigger.SoundTriggerDetector;
import android.media.soundtrigger.SoundTriggerManager;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.ParcelUuid;
import android.util.Log;

import com.android.internal.app.ISoundTriggerService;

import java.lang.RuntimeException;
import java.util.UUID;

/**
 * Utility class for the managing sound trigger sound models.
 */
public class SoundTriggerUtil {
    private static final String TAG = "SoundTriggerTestUtil";

    private final SoundTriggerManager mSoundTriggerManager;
    private final Context mContext;

    public SoundTriggerUtil(Context context) {
        mSoundTriggerManager = (SoundTriggerManager) context.getSystemService(
                Context.SOUND_TRIGGER_SERVICE);
        mContext = context;
    }

    /**
     * Adds/Updates a sound model.
     * The sound model must contain a valid UUID.
     *
     * @param soundModel The sound model to add/update.
     */
    public boolean addOrUpdateSoundModel(SoundTriggerManager.Model soundModel) {
        if (soundModel == null) {
            throw new RuntimeException("Bad sound model");
        }
        mSoundTriggerManager.updateModel(soundModel);
        return true;
    }

    /**
     * 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(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 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");
            return null;
        } else {
            return model;
        }
    }

    /**
     * Deletes the sound model for the given keyphrase id.
     *
     * @param modelId The model ID to look-up the sound model for.
     * @return {@code true} if the call succeeds, {@code false} otherwise.
     */
    public boolean deleteSoundModel(UUID modelId) {
        mSoundTriggerManager.deleteModel(modelId);
        return true;
    }

    public SoundTriggerDetector createSoundTriggerDetector(UUID modelId,
            SoundTriggerDetector.Callback callback) {
        return mSoundTriggerManager.createSoundTriggerDetector(modelId, callback, null);
    }
}