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; 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.RuntimeException; 33 import java.util.UUID; 34 35 /** 36 * Utility class for the managing sound trigger sound models. 37 */ 38 public class SoundTriggerUtil { 39 private static final String TAG = "TestSoundTriggerUtil:Hotsound"; 40 41 private final ISoundTriggerService mSoundTriggerService; 42 private final SoundTriggerManager mSoundTriggerManager; 43 private final Context mContext; 44 45 public SoundTriggerUtil(Context context) { 46 mSoundTriggerService = ISoundTriggerService.Stub.asInterface( 47 ServiceManager.getService(Context.SOUND_TRIGGER_SERVICE)); 48 mSoundTriggerManager = (SoundTriggerManager) context.getSystemService( 49 Context.SOUND_TRIGGER_SERVICE); 50 mContext = context; 51 } 52 53 /** 54 * Adds/Updates a sound model. 55 * The sound model must contain a valid UUID. 56 * 57 * @param soundModel The sound model to add/update. 58 */ 59 public boolean addOrUpdateSoundModel(GenericSoundModel soundModel) { 60 try { 61 if (soundModel == null) { 62 throw new RuntimeException("Bad sound model"); 63 } 64 mSoundTriggerService.updateSoundModel(soundModel); 65 } catch (RemoteException e) { 66 Log.e(TAG, "RemoteException in updateSoundModel", e); 67 } 68 return true; 69 } 70 71 public void addOrUpdateSoundModel(SoundTriggerManager.Model soundModel) { 72 mSoundTriggerManager.updateModel(soundModel); 73 } 74 75 /** 76 * Gets the sound model for the given keyphrase, null if none exists. 77 * If a sound model for a given keyphrase exists, and it needs to be updated, 78 * it should be obtained using this method, updated and then passed in to 79 * {@link #addOrUpdateSoundModel(GenericSoundModel)} without changing the IDs. 80 * 81 * @param modelId The model ID to look-up the sound model for. 82 * @return The sound model if one was found, null otherwise. 83 */ 84 @Nullable 85 public GenericSoundModel getSoundModel(UUID modelId) { 86 GenericSoundModel model = null; 87 try { 88 model = mSoundTriggerService.getSoundModel(new ParcelUuid(modelId)); 89 } catch (RemoteException e) { 90 Log.e(TAG, "RemoteException in updateKeyphraseSoundModel"); 91 } 92 93 if (model == null) { 94 Log.w(TAG, "No models present for the gien keyphrase ID"); 95 return null; 96 } else { 97 return model; 98 } 99 } 100 101 /** 102 * Deletes the sound model for the given keyphrase id. 103 * 104 * @param modelId The model ID to look-up the sound model for. 105 * @return {@code true} if the call succeeds, {@code false} otherwise. 106 */ 107 @Nullable 108 public boolean deleteSoundModel(UUID modelId) { 109 try { 110 mSoundTriggerService.deleteSoundModel(new ParcelUuid(modelId)); 111 } catch (RemoteException e) { 112 Log.e(TAG, "RemoteException in updateSoundModel"); 113 } 114 return true; 115 } 116 117 public void deleteSoundModelUsingManager(UUID modelId) { 118 mSoundTriggerManager.deleteModel(modelId); 119 } 120 121 public SoundTriggerDetector createSoundTriggerDetector(UUID modelId, 122 SoundTriggerDetector.Callback callback) { 123 return mSoundTriggerManager.createSoundTriggerDetector(modelId, callback, null); 124 } 125 126 } 127