blob: 6b89b62bb1e3c4c85986d3d6a1c4507be4a6f924 [file] [log] [blame]
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001/*
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
17package com.android.test.soundtrigger;
18
19import android.annotation.Nullable;
20import android.content.Context;
Arunesh Mishrac722ec412016-01-27 13:29:12 -080021import android.hardware.soundtrigger.SoundTrigger.GenericSoundModel;
Arunesh Mishra3fff7f52016-02-09 12:15:19 -080022import android.media.soundtrigger.SoundTriggerDetector;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080023import android.media.soundtrigger.SoundTriggerManager;
24import android.os.RemoteException;
25import android.os.ServiceManager;
26import android.os.ParcelUuid;
27import android.util.Log;
28
29import com.android.internal.app.ISoundTriggerService;
30
Arunesh Mishra3fff7f52016-02-09 12:15:19 -080031import java.lang.RuntimeException;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080032import java.util.UUID;
33
34/**
35 * Utility class for the managing sound trigger sound models.
36 */
37public class SoundTriggerUtil {
Chris Thorntondfa7c3b2016-06-30 22:05:51 -070038 private static final String TAG = "SoundTriggerTestUtil";
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080039
40 private final ISoundTriggerService mSoundTriggerService;
41 private final SoundTriggerManager mSoundTriggerManager;
42 private final Context mContext;
43
44 public SoundTriggerUtil(Context context) {
45 mSoundTriggerService = ISoundTriggerService.Stub.asInterface(
46 ServiceManager.getService(Context.SOUND_TRIGGER_SERVICE));
47 mSoundTriggerManager = (SoundTriggerManager) context.getSystemService(
48 Context.SOUND_TRIGGER_SERVICE);
49 mContext = context;
50 }
51
52 /**
53 * Adds/Updates a sound model.
54 * The sound model must contain a valid UUID.
55 *
56 * @param soundModel The sound model to add/update.
57 */
Arunesh Mishrac722ec412016-01-27 13:29:12 -080058 public boolean addOrUpdateSoundModel(GenericSoundModel soundModel) {
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080059 try {
Arunesh Mishra3fff7f52016-02-09 12:15:19 -080060 if (soundModel == null) {
61 throw new RuntimeException("Bad sound model");
62 }
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080063 mSoundTriggerService.updateSoundModel(soundModel);
64 } catch (RemoteException e) {
65 Log.e(TAG, "RemoteException in updateSoundModel", e);
66 }
67 return true;
68 }
69
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080070 /**
71 * Gets the sound model for the given keyphrase, null if none exists.
72 * If a sound model for a given keyphrase exists, and it needs to be updated,
73 * it should be obtained using this method, updated and then passed in to
Arunesh Mishrac722ec412016-01-27 13:29:12 -080074 * {@link #addOrUpdateSoundModel(GenericSoundModel)} without changing the IDs.
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080075 *
76 * @param modelId The model ID to look-up the sound model for.
77 * @return The sound model if one was found, null otherwise.
78 */
79 @Nullable
Arunesh Mishrac722ec412016-01-27 13:29:12 -080080 public GenericSoundModel getSoundModel(UUID modelId) {
81 GenericSoundModel model = null;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080082 try {
83 model = mSoundTriggerService.getSoundModel(new ParcelUuid(modelId));
84 } catch (RemoteException e) {
85 Log.e(TAG, "RemoteException in updateKeyphraseSoundModel");
86 }
87
88 if (model == null) {
Chris Thorntondfa7c3b2016-06-30 22:05:51 -070089 Log.w(TAG, "No models present for the given keyphrase ID");
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080090 return null;
91 } else {
92 return model;
93 }
94 }
95
96 /**
97 * Deletes the sound model for the given keyphrase id.
98 *
99 * @param modelId The model ID to look-up the sound model for.
100 * @return {@code true} if the call succeeds, {@code false} otherwise.
101 */
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800102 public boolean deleteSoundModel(UUID modelId) {
103 try {
104 mSoundTriggerService.deleteSoundModel(new ParcelUuid(modelId));
105 } catch (RemoteException e) {
Chris Thorntondfa7c3b2016-06-30 22:05:51 -0700106 Log.e(TAG, "RemoteException in deleteSoundModel");
107 return false;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800108 }
109 return true;
110 }
111
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800112 public SoundTriggerDetector createSoundTriggerDetector(UUID modelId,
113 SoundTriggerDetector.Callback callback) {
114 return mSoundTriggerManager.createSoundTriggerDetector(modelId, callback, null);
115 }
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800116}