Debian Codegraph @

tests/SoundTriggerTestApp/src/com/android/test/soundtrigger/SoundTriggerUtil.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.test.soundtrigger;
18
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.hardware.soundtrigger.SoundTrigger.RecognitionEvent;
22 import android.hardware.soundtrigger.SoundTrigger.GenericSoundModel;
23 import android.media.soundtrigger.SoundTriggerDetector;
24 import android.media.soundtrigger.SoundTriggerManager;
25 import android.os.RemoteException;
26 import android.os.ServiceManager;
27 import android.os.ParcelUuid;
28 import android.util.Log;
29
30 import com.android.internal.app.ISoundTriggerService;
31
32 import java.lang.reflect.Method;
33 import java.lang.RuntimeException;
34 import java.util.UUID;
35
36 /**
37 * Utility class for the managing sound trigger sound models.
38 */
39 public class SoundTriggerUtil {
40 private static final String TAG = "SoundTriggerTestUtil";
41
42 private final SoundTriggerManager mSoundTriggerManager;
43 private final Context mContext;
44
45 public SoundTriggerUtil(Context context) {
46 mSoundTriggerManager = (SoundTriggerManager) context.getSystemService(
47 Context.SOUND_TRIGGER_SERVICE);
48 mContext = context;
49 }
50
51 /**
52 * Adds/Updates a sound model.
53 * The sound model must contain a valid UUID.
54 *
55 * @param soundModel The sound model to add/update.
56 * @return The true if the model was loaded successfully, false otherwise.
57 */
58 public boolean addOrUpdateSoundModel(SoundTriggerManager.Model soundModel) {
59 if (soundModel == null) {
60 throw new RuntimeException("Bad sound model");
61 }
62 mSoundTriggerManager.updateModel(soundModel);
63 // TODO: call loadSoundModel in the soundtrigger manager updateModel method
64 // instead of here. It is needed to keep soundtrigger manager internal
65 // state consistent.
66 return mSoundTriggerManager
67 .loadSoundModel(getGenericSoundModel(soundModel)) == 0;
68 }
69
70 private GenericSoundModel getGenericSoundModel(
71 SoundTriggerManager.Model soundModel) {
72 try {
73 Method method = SoundTriggerManager.Model.class
74 .getDeclaredMethod("getGenericSoundModel");
75 method.setAccessible(true);
76 return (GenericSoundModel) method.invoke(soundModel);
77 } catch (ReflectiveOperationException e) {
78 Log.e(TAG, "Failed to getGenericSoundModel: " + soundModel, e);
79 return null;
80 }
81 }
82
83 /**
84 * Gets the sound model for the given keyphrase, null if none exists.
85 * If a sound model for a given keyphrase exists, and it needs to be updated,
86 * it should be obtained using this method, updated and then passed in to
87 * {@link #addOrUpdateSoundModel(SoundTriggerManager.Model)} without changing the IDs.
88 *
89 * @param modelId The model ID to look-up the sound model for.
90 * @return The sound model if one was found, null otherwise.
91 */
92 @Nullable
93 public SoundTriggerManager.Model getSoundModel(UUID modelId) {
94 SoundTriggerManager.Model model = null;
95 model = mSoundTriggerManager.getModel(modelId);
96
97 if (model == null) {
98 Log.w(TAG, "No models present for the given keyphrase ID");
99 return null;
100 } else {
101 return model;
102 }
103 }
104
105 /**
106 * Deletes the sound model for the given keyphrase id.
107 *
108 * @param modelId The model ID to look-up the sound model for.
109 * @return {@code true} if the call succeeds, {@code false} otherwise.
110 */
111 public boolean deleteSoundModel(UUID modelId) {
112 mSoundTriggerManager.deleteModel(modelId);
113 return true;
114 }
115
116 /**
117 * Get the current model state
118 *
119 * @param modelId The model ID to look-up the sound model for.
120 * @return 0 if the call succeeds, or an error code if it fails.
121 */
122 public int getModelState(UUID modelId) {
123 return mSoundTriggerManager.getModelState(modelId);
124 }
125
126 public SoundTriggerDetector createSoundTriggerDetector(UUID modelId,
127 SoundTriggerDetector.Callback callback) {
128 return mSoundTriggerManager.createSoundTriggerDetector(modelId, callback, null);
129 }
130 }