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.media.soundtrigger.SoundTriggerDetector; 22 import android.media.soundtrigger.SoundTriggerManager; 23 import android.os.RemoteException; 24 import android.os.ServiceManager; 25 import android.os.ParcelUuid; 26 import android.util.Log; 27 28 import com.android.internal.app.ISoundTriggerService; 29 30 import java.lang.RuntimeException; 31 import java.util.UUID; 32 33 /** 34 * Utility class for the managing sound trigger sound models. 35 */ 36 public class SoundTriggerUtil { 37 private static final String TAG = "SoundTriggerTestUtil"; 38 39 private final SoundTriggerManager mSoundTriggerManager; 40 private final Context mContext; 41 SoundTriggerUtil(Context context)42 public SoundTriggerUtil(Context context) { 43 mSoundTriggerManager = (SoundTriggerManager) context.getSystemService( 44 Context.SOUND_TRIGGER_SERVICE); 45 mContext = context; 46 } 47 48 /** 49 * Adds/Updates a sound model. 50 * The sound model must contain a valid UUID. 51 * 52 * @param soundModel The sound model to add/update. 53 */ addOrUpdateSoundModel(SoundTriggerManager.Model soundModel)54 public boolean addOrUpdateSoundModel(SoundTriggerManager.Model soundModel) { 55 if (soundModel == null) { 56 throw new RuntimeException("Bad sound model"); 57 } 58 mSoundTriggerManager.updateModel(soundModel); 59 return true; 60 } 61 62 /** 63 * Gets the sound model for the given keyphrase, null if none exists. 64 * If a sound model for a given keyphrase exists, and it needs to be updated, 65 * it should be obtained using this method, updated and then passed in to 66 * {@link #addOrUpdateSoundModel(SoundTriggerManager.Model)} without changing the IDs. 67 * 68 * @param modelId The model ID to look-up the sound model for. 69 * @return The sound model if one was found, null otherwise. 70 */ 71 @Nullable getSoundModel(UUID modelId)72 public SoundTriggerManager.Model getSoundModel(UUID modelId) { 73 SoundTriggerManager.Model model = null; 74 model = mSoundTriggerManager.getModel(modelId); 75 76 if (model == null) { 77 Log.w(TAG, "No models present for the given keyphrase ID"); 78 return null; 79 } else { 80 return model; 81 } 82 } 83 84 /** 85 * Deletes the sound model for the given keyphrase id. 86 * 87 * @param modelId The model ID to look-up the sound model for. 88 * @return {@code true} if the call succeeds, {@code false} otherwise. 89 */ deleteSoundModel(UUID modelId)90 public boolean deleteSoundModel(UUID modelId) { 91 mSoundTriggerManager.deleteModel(modelId); 92 return true; 93 } 94 createSoundTriggerDetector(UUID modelId, SoundTriggerDetector.Callback callback)95 public SoundTriggerDetector createSoundTriggerDetector(UUID modelId, 96 SoundTriggerDetector.Callback callback) { 97 return mSoundTriggerManager.createSoundTriggerDetector(modelId, callback, null); 98 } 99 } 100