diff options
author | Sandeep Siddhartha <sansid@google.com> | 2014-07-16 22:53:19 -0700 |
---|---|---|
committer | Sandeep Siddhartha <sansid@google.com> | 2014-07-17 11:01:25 -0700 |
commit | f8cf71d753b86895f987f682c80491aa2967e0c2 (patch) | |
tree | 349336f0d94ba39e461521b2376ab5dfb5585a2d /services | |
parent | b3325cbb267da0e663742fec172d7ed7ffe2aa1f (diff) | |
download | frameworks_base-f8cf71d753b86895f987f682c80491aa2967e0c2.zip frameworks_base-f8cf71d753b86895f987f682c80491aa2967e0c2.tar.gz frameworks_base-f8cf71d753b86895f987f682c80491aa2967e0c2.tar.bz2 |
Support model deletion
the updateKeyphrase call is also used to clear out a sound model currently,
this happens when the sound model passed in has no keyphrases.
We can revisit if we need another deleteSoundModel method
- Also fix an issue with the way we were writing keyphrase to the DB
and when updating keyphrase, we actually addOrUpdate
Change-Id: Ib7250c2fdafef6bc40387912a79366c334d73292
Diffstat (limited to 'services')
2 files changed, 43 insertions, 12 deletions
diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/DatabaseHelper.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/DatabaseHelper.java index 4a8623d..bed85fc 100644 --- a/services/voiceinteraction/java/com/android/server/voiceinteraction/DatabaseHelper.java +++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/DatabaseHelper.java @@ -94,7 +94,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { } public boolean addOrUpdateKeyphraseSoundModel(KeyphraseSoundModel soundModel) { - SQLiteDatabase db = this.getWritableDatabase(); + SQLiteDatabase db = getWritableDatabase(); ContentValues values = new ContentValues(); // Generate a random ID for the model. values.put(SoundModelContract.KEY_ID, soundModel.uuid.toString()); @@ -105,7 +105,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { if (db.insertWithOnConflict( SoundModelContract.TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE) != -1) { for (Keyphrase keyphrase : soundModel.keyphrases) { - status &= addKeyphrase(soundModel.uuid, keyphrase); + status &= addOrUpdateKeyphrase(soundModel.uuid, keyphrase); } return status; } else { @@ -114,18 +114,16 @@ public class DatabaseHelper extends SQLiteOpenHelper { } } - /** - * TODO(sansid): Change to addOrUpdate to handle changes here. - */ - private boolean addKeyphrase(UUID modelId, Keyphrase keyphrase) { - SQLiteDatabase db = this.getWritableDatabase(); + private boolean addOrUpdateKeyphrase(UUID modelId, Keyphrase keyphrase) { + SQLiteDatabase db = getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KeyphraseContract.KEY_ID, keyphrase.id); values.put(KeyphraseContract.KEY_RECOGNITION_MODES, keyphrase.recognitionModeFlags); - values.put(KeyphraseContract.KEY_SOUND_MODEL_ID, keyphrase.id); + values.put(KeyphraseContract.KEY_SOUND_MODEL_ID, modelId.toString()); values.put(KeyphraseContract.KEY_HINT_TEXT, keyphrase.hintText); values.put(KeyphraseContract.KEY_LOCALE, keyphrase.locale); - if (db.insert(KeyphraseContract.TABLE, null, values) != -1) { + if (db.insertWithOnConflict( + KeyphraseContract.TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE) != -1) { return true; } else { Slog.w(TAG, "Failed to persist keyphrase to database"); @@ -134,6 +132,26 @@ public class DatabaseHelper extends SQLiteOpenHelper { } /** + * Deletes the sound model and associated keyphrases. + */ + public boolean deleteKeyphraseSoundModel(UUID uuid) { + SQLiteDatabase db = getWritableDatabase(); + String modelId = uuid.toString(); + String soundModelClause = SoundModelContract.KEY_ID + "=" + modelId; + boolean status = true; + if (db.delete(SoundModelContract.TABLE, soundModelClause, null) == 0) { + Slog.w(TAG, "No sound models deleted from the database"); + status = false; + } + String keyphraseClause = KeyphraseContract.KEY_SOUND_MODEL_ID + "=" + modelId; + if (db.delete(KeyphraseContract.TABLE, keyphraseClause, null) == 0) { + Slog.w(TAG, "No keyphrases deleted from the database"); + status = false; + } + return status; + } + + /** * Lists all the keyphrase sound models currently registered with the system. */ public List<KeyphraseSoundModel> getKephraseSoundModels() { diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java index 7204695..63702ba 100644 --- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java +++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java @@ -281,12 +281,25 @@ public class VoiceInteractionManagerService extends SystemService { throw new SecurityException("Caller does not hold the permission " + Manifest.permission.MANAGE_VOICE_KEYPHRASES); } + if (model == null) { + throw new IllegalArgumentException("Model must not be null"); + } + final long caller = Binder.clearCallingIdentity(); try { - if (mDbHelper.addOrUpdateKeyphraseSoundModel(model)) { - return STATUS_OK; + // If the keyphrases are not present in the model, delete the model. + if (model.keyphrases == null) { + if (mDbHelper.deleteKeyphraseSoundModel(model.uuid)) { + return STATUS_OK; + } else { + return STATUS_ERROR; + } } else { - return STATUS_ERROR; + if (mDbHelper.addOrUpdateKeyphraseSoundModel(model)) { + return STATUS_OK; + } else { + return STATUS_ERROR; + } } } finally { Binder.restoreCallingIdentity(caller); |