SoundTriggerManager.java revision a772e5fc062c8de48cb9c1d61755110f6b2e189b
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 17package android.media.soundtrigger; 18 19import android.annotation.NonNull; 20import android.annotation.Nullable; 21import android.annotation.SystemApi; 22import android.content.Context; 23import android.hardware.soundtrigger.SoundTrigger; 24import android.os.Handler; 25import android.os.ParcelUuid; 26import android.os.RemoteException; 27import android.util.Slog; 28 29import com.android.internal.app.ISoundTriggerService; 30 31import java.util.HashMap; 32import java.util.UUID; 33 34/** 35 * This class provides management of non-voice (general sound trigger) based sound recognition 36 * models. Usage of this class is restricted to system or signature applications only. This allows 37 * OEMs to write apps that can manage non-voice based sound trigger models. 38 * 39 * @hide 40 * TODO: Mark this as a SystemApi and get approval. 41 */ 42public final class SoundTriggerManager { 43 private static final boolean DBG = false; 44 private static final String TAG = "SoundTriggerManager"; 45 46 private final Context mContext; 47 private final ISoundTriggerService mSoundTriggerService; 48 49 // Stores a mapping from the sound model UUID to the SoundTriggerInstance created by 50 // the createSoundTriggerDetector() call. 51 private final HashMap<UUID, SoundTriggerDetector> mReceiverInstanceMap; 52 53 /** 54 * @hide 55 */ 56 public SoundTriggerManager(Context context, ISoundTriggerService soundTriggerService ) { 57 if (DBG) { 58 Slog.i(TAG, "SoundTriggerManager created."); 59 } 60 mSoundTriggerService = soundTriggerService; 61 mContext = context; 62 mReceiverInstanceMap = new HashMap<UUID, SoundTriggerDetector>(); 63 } 64 65 /** 66 * Updates the given sound trigger model. 67 */ 68 public void updateModel(Model model) { 69 try { 70 mSoundTriggerService.updateSoundModel(model.getGenericSoundModel()); 71 } catch (RemoteException e) { 72 } 73 } 74 75 /** 76 * Returns the sound trigger model represented by the given UUID. An instance of {@link Model} 77 * is returned. 78 */ 79 public Model getModel(UUID soundModelId) { 80 try { 81 return new Model(mSoundTriggerService.getSoundModel( 82 new ParcelUuid(soundModelId))); 83 } catch (RemoteException e) { 84 return null; 85 } 86 } 87 88 /** 89 * Deletes the sound model represented by the provided UUID. 90 */ 91 public void deleteModel(UUID soundModelId) { 92 try { 93 mSoundTriggerService.deleteSoundModel(new ParcelUuid(soundModelId)); 94 } catch (RemoteException e) { 95 } 96 } 97 98 /** 99 * Creates an instance of {@link SoundTriggerDetector} which can be used to start/stop 100 * recognition on the model and register for triggers from the model. Note that this call 101 * invalidates any previously returned instances for the same sound model Uuid. 102 * 103 * @param soundModelId UUID of the sound model to create the receiver object for. 104 * @param callback Instance of the {@link SoundTriggerDetector#Callback} object for the 105 * callbacks for the given sound model. 106 * @param handler The Handler to use for the callback operations. A null value will use the 107 * current thread's Looper. 108 * @return Instance of {@link SoundTriggerDetector} or null on error. 109 */ 110 @Nullable 111 public SoundTriggerDetector createSoundTriggerDetector(UUID soundModelId, 112 @NonNull SoundTriggerDetector.Callback callback, @Nullable Handler handler) { 113 if (soundModelId == null) { 114 return null; 115 } 116 117 SoundTriggerDetector oldInstance = mReceiverInstanceMap.get(soundModelId); 118 if (oldInstance != null) { 119 // Shutdown old instance. 120 } 121 SoundTriggerDetector newInstance = new SoundTriggerDetector(mSoundTriggerService, 122 soundModelId, callback, handler); 123 mReceiverInstanceMap.put(soundModelId, newInstance); 124 return newInstance; 125 } 126 127 /** 128 * Class captures the data and fields that represent a non-keyphrase sound model. Use the 129 * factory constructor {@link Model#create()} to create an instance. 130 */ 131 // We use encapsulation to expose the SoundTrigger.GenericSoundModel as a SystemApi. This 132 // prevents us from exposing SoundTrigger.GenericSoundModel as an Api. 133 public static class Model { 134 135 private SoundTrigger.GenericSoundModel mGenericSoundModel; 136 137 /** 138 * @hide 139 */ 140 Model(SoundTrigger.GenericSoundModel soundTriggerModel) { 141 mGenericSoundModel = soundTriggerModel; 142 } 143 144 /** 145 * Factory constructor to create a SoundModel instance for use with methods in this 146 * class. 147 */ 148 public static Model create(UUID modelUuid, UUID vendorUuid, byte[] data) { 149 return new Model(new SoundTrigger.GenericSoundModel(modelUuid, 150 vendorUuid, data)); 151 } 152 153 public UUID getModelUuid() { 154 return mGenericSoundModel.uuid; 155 } 156 157 public UUID getVendorUuid() { 158 return mGenericSoundModel.vendorUuid; 159 } 160 161 public byte[] getModelData() { 162 return mGenericSoundModel.data; 163 } 164 165 /** 166 * @hide 167 */ 168 SoundTrigger.GenericSoundModel getGenericSoundModel() { 169 return mGenericSoundModel; 170 } 171 } 172} 173