| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 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 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 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 |
|
| 64 |
|
| 65 |
|
| 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 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 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 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public boolean deleteSoundModel(UUID modelId) { |
| 112 |
mSoundTriggerManager.deleteModel(modelId); |
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 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 |
} |