| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.hardware.soundtrigger; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.compat.annotation.UnsupportedAppUsage; |
| 22 |
import android.media.permission.ClearCallingIdentityContext; |
| 23 |
import android.media.permission.Identity; |
| 24 |
import android.media.permission.SafeCloseable; |
| 25 |
import android.media.soundtrigger.PhraseRecognitionEvent; |
| 26 |
import android.media.soundtrigger.PhraseSoundModel; |
| 27 |
import android.media.soundtrigger.RecognitionEvent; |
| 28 |
import android.media.soundtrigger.SoundModel; |
| 29 |
import android.media.soundtrigger_middleware.ISoundTriggerCallback; |
| 30 |
import android.media.soundtrigger_middleware.ISoundTriggerMiddlewareService; |
| 31 |
import android.media.soundtrigger_middleware.ISoundTriggerModule; |
| 32 |
import android.os.Build; |
| 33 |
import android.os.Handler; |
| 34 |
import android.os.IBinder; |
| 35 |
import android.os.Looper; |
| 36 |
import android.os.Message; |
| 37 |
import android.os.RemoteException; |
| 38 |
import android.util.Log; |
| 39 |
|
| 40 |
import java.io.IOException; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class SoundTriggerModule { |
| 49 |
private static final String TAG = "SoundTriggerModule"; |
| 50 |
|
| 51 |
private static final int EVENT_RECOGNITION = 1; |
| 52 |
private static final int EVENT_SERVICE_DIED = 2; |
| 53 |
private static final int EVENT_RESOURCES_AVAILABLE = 3; |
| 54 |
private static final int EVENT_MODEL_UNLOADED = 4; |
| 55 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 56 |
private int mId; |
| 57 |
private EventHandlerDelegate mEventHandlerDelegate; |
| 58 |
private ISoundTriggerModule mService; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
SoundTriggerModule(@NonNull ISoundTriggerMiddlewareService service, |
| 65 |
int moduleId, @NonNull SoundTrigger.StatusListener listener, @NonNull Looper looper, |
| 66 |
@NonNull Identity originatorIdentity) |
| 67 |
throws RemoteException { |
| 68 |
mId = moduleId; |
| 69 |
mEventHandlerDelegate = new EventHandlerDelegate(listener, looper); |
| 70 |
|
| 71 |
try (SafeCloseable ignored = ClearCallingIdentityContext.create()) { |
| 72 |
mService = service.attachAsOriginator(moduleId, originatorIdentity, |
| 73 |
mEventHandlerDelegate); |
| 74 |
} |
| 75 |
mService.asBinder().linkToDeath(mEventHandlerDelegate, 0); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
SoundTriggerModule(@NonNull ISoundTriggerMiddlewareService service, |
| 83 |
int moduleId, @NonNull SoundTrigger.StatusListener listener, @NonNull Looper looper, |
| 84 |
@NonNull Identity middlemanIdentity, @NonNull Identity originatorIdentity) |
| 85 |
throws RemoteException { |
| 86 |
mId = moduleId; |
| 87 |
mEventHandlerDelegate = new EventHandlerDelegate(listener, looper); |
| 88 |
|
| 89 |
try (SafeCloseable ignored = ClearCallingIdentityContext.create()) { |
| 90 |
mService = service.attachAsMiddleman(moduleId, middlemanIdentity, originatorIdentity, |
| 91 |
mEventHandlerDelegate); |
| 92 |
} |
| 93 |
mService.asBinder().linkToDeath(mEventHandlerDelegate, 0); |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
protected void finalize() { |
| 98 |
detach(); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
@UnsupportedAppUsage |
| 107 |
public synchronized void detach() { |
| 108 |
try { |
| 109 |
if (mService != null) { |
| 110 |
mService.asBinder().unlinkToDeath(mEventHandlerDelegate, 0); |
| 111 |
mService.detach(); |
| 112 |
mService = null; |
| 113 |
} |
| 114 |
} catch (Exception e) { |
| 115 |
SoundTrigger.handleException(e); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
@UnsupportedAppUsage |
| 136 |
public synchronized int loadSoundModel(@NonNull SoundTrigger.SoundModel model, |
| 137 |
@NonNull int[] soundModelHandle) { |
| 138 |
try { |
| 139 |
if (model instanceof SoundTrigger.GenericSoundModel) { |
| 140 |
SoundModel aidlModel = ConversionUtil.api2aidlGenericSoundModel( |
| 141 |
(SoundTrigger.GenericSoundModel) model); |
| 142 |
try { |
| 143 |
soundModelHandle[0] = mService.loadModel(aidlModel); |
| 144 |
} finally { |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
if (aidlModel.data != null) { |
| 149 |
try { |
| 150 |
aidlModel.data.close(); |
| 151 |
} catch (IOException e) { |
| 152 |
Log.e(TAG, "Failed to close file", e); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
return SoundTrigger.STATUS_OK; |
| 157 |
} |
| 158 |
if (model instanceof SoundTrigger.KeyphraseSoundModel) { |
| 159 |
PhraseSoundModel aidlModel = ConversionUtil.api2aidlPhraseSoundModel( |
| 160 |
(SoundTrigger.KeyphraseSoundModel) model); |
| 161 |
try { |
| 162 |
soundModelHandle[0] = mService.loadPhraseModel(aidlModel); |
| 163 |
} finally { |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
if (aidlModel.common.data != null) { |
| 168 |
try { |
| 169 |
aidlModel.common.data.close(); |
| 170 |
} catch (IOException e) { |
| 171 |
Log.e(TAG, "Failed to close file", e); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
return SoundTrigger.STATUS_OK; |
| 176 |
} |
| 177 |
return SoundTrigger.STATUS_BAD_VALUE; |
| 178 |
} catch (Exception e) { |
| 179 |
return SoundTrigger.handleException(e); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
@UnsupportedAppUsage |
| 196 |
public synchronized int unloadSoundModel(int soundModelHandle) { |
| 197 |
try { |
| 198 |
mService.unloadModel(soundModelHandle); |
| 199 |
return SoundTrigger.STATUS_OK; |
| 200 |
} catch (Exception e) { |
| 201 |
return SoundTrigger.handleException(e); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
@UnsupportedAppUsage |
| 224 |
public synchronized int startRecognition(int soundModelHandle, |
| 225 |
SoundTrigger.RecognitionConfig config) { |
| 226 |
try { |
| 227 |
mService.startRecognition(soundModelHandle, |
| 228 |
ConversionUtil.api2aidlRecognitionConfig(config)); |
| 229 |
return SoundTrigger.STATUS_OK; |
| 230 |
} catch (Exception e) { |
| 231 |
return SoundTrigger.handleException(e); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
@UnsupportedAppUsage |
| 249 |
public synchronized int stopRecognition(int soundModelHandle) { |
| 250 |
try { |
| 251 |
mService.stopRecognition(soundModelHandle); |
| 252 |
return SoundTrigger.STATUS_OK; |
| 253 |
} catch (Exception e) { |
| 254 |
return SoundTrigger.handleException(e); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public synchronized int getModelState(int soundModelHandle) { |
| 275 |
try { |
| 276 |
mService.forceRecognitionEvent(soundModelHandle); |
| 277 |
return SoundTrigger.STATUS_OK; |
| 278 |
} catch (Exception e) { |
| 279 |
return SoundTrigger.handleException(e); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
public synchronized int setParameter(int soundModelHandle, @ModelParams int modelParam, |
| 299 |
int value) { |
| 300 |
try { |
| 301 |
mService.setModelParameter(soundModelHandle, |
| 302 |
ConversionUtil.api2aidlModelParameter(modelParam), value); |
| 303 |
return SoundTrigger.STATUS_OK; |
| 304 |
} catch (Exception e) { |
| 305 |
return SoundTrigger.handleException(e); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public synchronized int getParameter(int soundModelHandle, @ModelParams int modelParam) { |
| 322 |
try { |
| 323 |
return mService.getModelParameter(soundModelHandle, |
| 324 |
ConversionUtil.api2aidlModelParameter(modelParam)); |
| 325 |
} catch (RemoteException e) { |
| 326 |
throw e.rethrowFromSystemServer(); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
@Nullable |
| 339 |
public synchronized SoundTrigger.ModelParamRange queryParameter(int soundModelHandle, |
| 340 |
@ModelParams int modelParam) { |
| 341 |
try { |
| 342 |
return ConversionUtil.aidl2apiModelParameterRange(mService.queryModelParameterSupport( |
| 343 |
soundModelHandle, |
| 344 |
ConversionUtil.api2aidlModelParameter(modelParam))); |
| 345 |
} catch (RemoteException e) { |
| 346 |
throw e.rethrowFromSystemServer(); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
private class EventHandlerDelegate extends ISoundTriggerCallback.Stub implements |
| 351 |
IBinder.DeathRecipient { |
| 352 |
private final Handler mHandler; |
| 353 |
|
| 354 |
EventHandlerDelegate(@NonNull final SoundTrigger.StatusListener listener, |
| 355 |
@NonNull Looper looper) { |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
mHandler = new Handler(looper) { |
| 360 |
@Override |
| 361 |
public void handleMessage(Message msg) { |
| 362 |
switch (msg.what) { |
| 363 |
case EVENT_RECOGNITION: |
| 364 |
listener.onRecognition( |
| 365 |
(SoundTrigger.RecognitionEvent) msg.obj); |
| 366 |
break; |
| 367 |
case EVENT_RESOURCES_AVAILABLE: |
| 368 |
listener.onResourcesAvailable(); |
| 369 |
break; |
| 370 |
case EVENT_MODEL_UNLOADED: |
| 371 |
listener.onModelUnloaded((Integer) msg.obj); |
| 372 |
break; |
| 373 |
case EVENT_SERVICE_DIED: |
| 374 |
listener.onServiceDied(); |
| 375 |
break; |
| 376 |
default: |
| 377 |
Log.e(TAG, "Unknown message: " + msg.toString()); |
| 378 |
break; |
| 379 |
} |
| 380 |
} |
| 381 |
}; |
| 382 |
} |
| 383 |
|
| 384 |
@Override |
| 385 |
public synchronized void onRecognition(int handle, RecognitionEvent event, |
| 386 |
int captureSession) |
| 387 |
throws RemoteException { |
| 388 |
Message m = mHandler.obtainMessage(EVENT_RECOGNITION, |
| 389 |
ConversionUtil.aidl2apiRecognitionEvent(handle, captureSession, event)); |
| 390 |
mHandler.sendMessage(m); |
| 391 |
} |
| 392 |
|
| 393 |
@Override |
| 394 |
public synchronized void onPhraseRecognition(int handle, PhraseRecognitionEvent event, |
| 395 |
int captureSession) |
| 396 |
throws RemoteException { |
| 397 |
Message m = mHandler.obtainMessage(EVENT_RECOGNITION, |
| 398 |
ConversionUtil.aidl2apiPhraseRecognitionEvent(handle, captureSession, event)); |
| 399 |
mHandler.sendMessage(m); |
| 400 |
} |
| 401 |
|
| 402 |
@Override |
| 403 |
public void onModelUnloaded(int modelHandle) throws RemoteException { |
| 404 |
Message m = mHandler.obtainMessage(EVENT_MODEL_UNLOADED, modelHandle); |
| 405 |
mHandler.sendMessage(m); |
| 406 |
} |
| 407 |
|
| 408 |
@Override |
| 409 |
public synchronized void onResourcesAvailable() throws RemoteException { |
| 410 |
Message m = mHandler.obtainMessage(EVENT_RESOURCES_AVAILABLE); |
| 411 |
mHandler.sendMessage(m); |
| 412 |
} |
| 413 |
|
| 414 |
@Override |
| 415 |
public synchronized void onModuleDied() { |
| 416 |
Message m = mHandler.obtainMessage(EVENT_SERVICE_DIED); |
| 417 |
mHandler.sendMessage(m); |
| 418 |
} |
| 419 |
|
| 420 |
@Override |
| 421 |
public synchronized void binderDied() { |
| 422 |
Message m = mHandler.obtainMessage(EVENT_SERVICE_DIED); |
| 423 |
mHandler.sendMessage(m); |
| 424 |
} |
| 425 |
} |
| 426 |
} |