}
modelData.setHandle(handle[0]);
modelData.setLoaded();
+ Slog.d(TAG, "Generic sound model loaded with handle:" + handle[0]);
}
modelData.setCallback(callback);
modelData.setRecognitionConfig(recognitionConfig);
synchronized (mLock) {
if (DBG) {
- Slog.d(TAG, "startRecognition for keyphraseId=" + keyphraseId
+ Slog.d(TAG, "startKeyphraseRecognition for keyphraseId=" + keyphraseId
+ " soundModel=" + soundModel + ", listener=" + listener.asBinder()
+ ", recognitionConfig=" + recognitionConfig);
Slog.d(TAG, "moduleProperties=" + mModuleProperties);
}
if (mModuleProperties == null) {
- Slog.w(TAG, "Attempting startRecognition without the capability");
+ Slog.w(TAG, "Attempting startKeyphraseRecognition without the capability");
return STATUS_ERROR;
}
if (mModule == null) {
mModule = SoundTrigger.attachModule(mModuleProperties.id, this, null);
if (mModule == null) {
- Slog.w(TAG, "startRecognition cannot attach to sound trigger module");
+ Slog.w(TAG, "startKeyphraseRecognition cannot attach to sound trigger module");
return STATUS_ERROR;
}
}
}
if (currentCallback == null || !modelData.isModelStarted()) {
- // startRecognition hasn't been called or it failed.
- Slog.w(TAG, "Attempting stopRecognition without a successful startRecognition");
+ // startGenericRecognition hasn't been called or it failed.
+ Slog.w(TAG, "Attempting stopGenericRecognition without a successful" +
+ " startGenericRecognition");
return STATUS_ERROR;
}
if (currentCallback.asBinder() != listener.asBinder()) {
// We don't allow a different listener to stop the recognition than the one
// that started it.
- Slog.w(TAG, "Attempting stopRecognition for another recognition");
+ Slog.w(TAG, "Attempting stopGenericRecognition for another recognition");
return STATUS_ERROR;
}
- int status = stopGenericRecognitionLocked(modelData, false /* don't notify for synchronous calls */);
+ int status = stopGenericRecognitionLocked(modelData,
+ false /* don't notify for synchronous calls */);
if (status != SoundTrigger.STATUS_OK) {
+ Slog.w(TAG, "stopGenericRecognition failed: " + status);
return status;
}
// We leave the sound model loaded but not started, this helps us when we start
// back.
// Also clear the internal state once the recognition has been stopped.
- modelData.clearState();
+ modelData.setLoaded();
modelData.clearCallback();
if (!computeRecognitionRunning()) {
internalClearGlobalStateLocked();
return mModuleProperties;
}
+ int unloadKeyphraseSoundModel(int keyphraseId) {
+ if (mModule == null || mCurrentKeyphraseModelHandle == INVALID_VALUE) {
+ return STATUS_ERROR;
+ }
+ if (mKeyphraseId != keyphraseId) {
+ Slog.w(TAG, "Given sound model is not the one loaded.");
+ return STATUS_ERROR;
+ }
+
+ synchronized (mLock) {
+ // Stop recognition if it's the current one.
+ mRequested = false;
+ int status = updateRecognitionLocked(false /* don't notify */);
+ if (status != SoundTrigger.STATUS_OK) {
+ Slog.w(TAG, "Stop recognition failed for keyphrase ID:" + status);
+ }
+
+ status = mModule.unloadSoundModel(mCurrentKeyphraseModelHandle);
+ if (status != SoundTrigger.STATUS_OK) {
+ Slog.w(TAG, "unloadKeyphraseSoundModel call failed with " + status);
+ }
+ internalClearKeyphraseSoundModelLocked();
+ return status;
+ }
+ }
+
+ int unloadGenericSoundModel(UUID modelId) {
+ if (modelId == null || mModule == null) {
+ return STATUS_ERROR;
+ }
+ ModelData modelData = mGenericModelDataMap.get(modelId);
+ if (modelData == null) {
+ Slog.w(TAG, "Unload error: Attempting unload invalid generic model with id:" + modelId);
+ return STATUS_ERROR;
+ }
+ synchronized (mLock) {
+ if (!modelData.isModelLoaded()) {
+ // Nothing to do here.
+ Slog.i(TAG, "Unload: Given generic model is not loaded:" + modelId);
+ return STATUS_OK;
+ }
+ if (modelData.isModelStarted()) {
+ int status = stopGenericRecognitionLocked(modelData,
+ false /* don't notify for synchronous calls */);
+ if (status != SoundTrigger.STATUS_OK) {
+ Slog.w(TAG, "stopGenericRecognition failed: " + status);
+ }
+ }
+
+ int status = mModule.unloadSoundModel(modelData.getHandle());
+ if (status != SoundTrigger.STATUS_OK) {
+ Slog.w(TAG, "unloadGenericSoundModel() call failed with " + status);
+ Slog.w(TAG, "unloadGenericSoundModel() force-marking model as unloaded.");
+ }
+ mGenericModelDataMap.remove(modelId);
+ if (DBG) dumpGenericModelState();
+ return status;
+ }
+ }
+
//---- SoundTrigger.StatusListener methods
@Override
public void onRecognition(RecognitionEvent event) {
}
}
} else {
+ Slog.i(TAG, "startRecognition successful.");
modelData.setStarted();
// Notify of resume if needed.
if (notify) {
}
}
}
+ if (DBG) dumpGenericModelState();
return status;
}
}
}
}
+ if (DBG) dumpGenericModelState();
return status;
}
+ private void dumpGenericModelState() {
+ for (UUID modelId : mGenericModelDataMap.keySet()) {
+ ModelData modelData = mGenericModelDataMap.get(modelId);
+ Slog.i(TAG, "Model :" + modelData.toString());
+ }
+ }
+
// Computes whether we have any recognition running at all (voice or generic). Sets
// the mRecognitionRunning variable with the result.
private boolean computeRecognitionRunning() {
synchronized RecognitionConfig getRecognitionConfig() {
return mRecognitionConfig;
}
+
+ String stateToString() {
+ switch(mModelState) {
+ case MODEL_NOTLOADED: return "NOT_LOADED";
+ case MODEL_LOADED: return "LOADED";
+ case MODEL_STARTED: return "STARTED";
+ }
+ return "Unknown state";
+ }
+
+ public String toString() {
+ return "Handle: " + mModelHandle + "ModelState: " + stateToString();
+ }
}
}
import android.text.Editable;
import android.text.method.ScrollingMovementMethod;
import android.os.Bundle;
+import android.os.Handler;
+import android.os.PowerManager;
import android.os.UserManager;
import android.util.Log;
import android.view.View;
private TextView mDebugView = null;
private int mSelectedModelId = 1;
private ScrollView mScrollView = null;
+ private PowerManager.WakeLock mScreenWakelock;
+ private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
mDebugView.setMovementMethod(new ScrollingMovementMethod());
mSoundTriggerUtil = new SoundTriggerUtil(this);
mRandom = new Random();
+ mHandler = new Handler();
}
private void postMessage(String msg) {
});
}
- private UUID getSelectedUuid() {
+ private synchronized UUID getSelectedUuid() {
if (mSelectedModelId == 2) return mModelUuid2;
if (mSelectedModelId == 3) return mModelUuid3;
return mModelUuid1; // Default.
}
- private void setDetector(SoundTriggerDetector detector) {
- if (mSelectedModelId == 2) mDetector2 = detector;
- if (mSelectedModelId == 3) mDetector3 = detector;
+ private synchronized void setDetector(SoundTriggerDetector detector) {
+ if (mSelectedModelId == 2) {
+ mDetector2 = detector;
+ return;
+ }
+ if (mSelectedModelId == 3) {
+ mDetector3 = detector;
+ return;
+ }
mDetector1 = detector;
}
- private SoundTriggerDetector getDetector() {
+ private synchronized SoundTriggerDetector getDetector() {
if (mSelectedModelId == 2) return mDetector2;
if (mSelectedModelId == 3) return mDetector3;
return mDetector1;
}
+ private void screenWakeup() {
+ PowerManager pm = ((PowerManager)getSystemService(POWER_SERVICE));
+ if (mScreenWakelock == null) {
+ mScreenWakelock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "TAG");
+ }
+ mScreenWakelock.acquire();
+ }
+
+ private void screenRelease() {
+ PowerManager pm = ((PowerManager)getSystemService(POWER_SERVICE));
+ mScreenWakelock.release();
+ }
+
/**
* Called when the user clicks the enroll button.
* Performs a fresh enrollment.
Toast.makeText(this, "Sound model not found!!!", Toast.LENGTH_SHORT).show();
return;
}
- boolean status = mSoundTriggerUtil.deleteSoundModel(mModelUuid1);
+ boolean status = mSoundTriggerUtil.deleteSoundModel(modelUuid);
if (status) {
Toast.makeText(this, "Successfully deleted model UUID=" + soundModel.uuid,
Toast.LENGTH_SHORT)
}
}
- public void onRadioButtonClicked(View view) {
+ public synchronized void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.model_one:
- if (checked) mSelectedModelId = 1;
- postMessage("Selected model one.");
+ if (checked) {
+ mSelectedModelId = 1;
+ postMessage("Selected model one.");
+ }
break;
case R.id.model_two:
- if (checked) mSelectedModelId = 2;
- postMessage("Selected model two.");
+ if (checked) {
+ mSelectedModelId = 2;
+ postMessage("Selected model two.");
+ }
break;
case R.id.model_three:
- if (checked) mSelectedModelId = 3;
- postMessage("Selected model three.");
+ if (checked) {
+ mSelectedModelId = 3;
+ postMessage("Selected model three.");
+ }
break;
}
}
public void onDetected(SoundTriggerDetector.EventPayload event) {
postMessage("onDetected(): " + eventPayloadToString(event));
+ screenWakeup();
+ mHandler.postDelayed(new Runnable() {
+ @Override
+ public void run() {
+ screenRelease();
+ }
+ }, 1000L);
}
public void onError() {