Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 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; |
Arunesh Mishra | c722ec41 | 2016-01-27 13:29:12 -0800 | [diff] [blame] | 21 | import android.hardware.soundtrigger.SoundTrigger.GenericSoundModel; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 22 | import android.media.soundtrigger.SoundTriggerDetector; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 23 | import android.media.soundtrigger.SoundTriggerManager; |
| 24 | import android.os.RemoteException; |
| 25 | import android.os.ServiceManager; |
| 26 | import android.os.ParcelUuid; |
| 27 | import android.util.Log; |
| 28 | |
| 29 | import com.android.internal.app.ISoundTriggerService; |
| 30 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 31 | import java.lang.RuntimeException; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 32 | import java.util.UUID; |
| 33 | |
| 34 | /** |
| 35 | * Utility class for the managing sound trigger sound models. |
| 36 | */ |
| 37 | public class SoundTriggerUtil { |
Chris Thornton | dfa7c3b | 2016-06-30 22:05:51 -0700 | [diff] [blame] | 38 | private static final String TAG = "SoundTriggerTestUtil"; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 39 | |
| 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 Mishra | c722ec41 | 2016-01-27 13:29:12 -0800 | [diff] [blame] | 58 | public boolean addOrUpdateSoundModel(GenericSoundModel soundModel) { |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 59 | try { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 60 | if (soundModel == null) { |
| 61 | throw new RuntimeException("Bad sound model"); |
| 62 | } |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 63 | mSoundTriggerService.updateSoundModel(soundModel); |
| 64 | } catch (RemoteException e) { |
| 65 | Log.e(TAG, "RemoteException in updateSoundModel", e); |
| 66 | } |
| 67 | return true; |
| 68 | } |
| 69 | |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 70 | /** |
| 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 Mishra | c722ec41 | 2016-01-27 13:29:12 -0800 | [diff] [blame] | 74 | * {@link #addOrUpdateSoundModel(GenericSoundModel)} without changing the IDs. |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 75 | * |
| 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 Mishra | c722ec41 | 2016-01-27 13:29:12 -0800 | [diff] [blame] | 80 | public GenericSoundModel getSoundModel(UUID modelId) { |
| 81 | GenericSoundModel model = null; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 82 | 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 Thornton | dfa7c3b | 2016-06-30 22:05:51 -0700 | [diff] [blame] | 89 | Log.w(TAG, "No models present for the given keyphrase ID"); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 90 | 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 Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 102 | public boolean deleteSoundModel(UUID modelId) { |
| 103 | try { |
| 104 | mSoundTriggerService.deleteSoundModel(new ParcelUuid(modelId)); |
| 105 | } catch (RemoteException e) { |
Chris Thornton | dfa7c3b | 2016-06-30 22:05:51 -0700 | [diff] [blame] | 106 | Log.e(TAG, "RemoteException in deleteSoundModel"); |
| 107 | return false; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 108 | } |
| 109 | return true; |
| 110 | } |
| 111 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 112 | public SoundTriggerDetector createSoundTriggerDetector(UUID modelId, |
| 113 | SoundTriggerDetector.Callback callback) { |
| 114 | return mSoundTriggerManager.createSoundTriggerDetector(modelId, callback, null); |
| 115 | } |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 116 | } |