From 2883ba69751de69811a38f086ecbe4c2032ca87d Mon Sep 17 00:00:00 2001 From: Sandeep Siddhartha Date: Wed, 16 Jul 2014 21:02:08 -0700 Subject: Plumb the database code to read/update sound models Change-Id: I135131b042481462f905bdb69ea8544e2561d177 --- .../server/voiceinteraction/DatabaseHelper.java | 23 ++++++++++++---------- .../VoiceInteractionManagerService.java | 12 +++++++---- 2 files changed, 21 insertions(+), 14 deletions(-) (limited to 'services/voiceinteraction') diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/DatabaseHelper.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/DatabaseHelper.java index 548e7d3..4a8623d 100644 --- a/services/voiceinteraction/java/com/android/server/voiceinteraction/DatabaseHelper.java +++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/DatabaseHelper.java @@ -20,7 +20,6 @@ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.hardware.soundtrigger.SoundTrigger; import android.hardware.soundtrigger.Keyphrase; @@ -75,7 +74,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { + SoundModelContract.KEY_TYPE + " INTEGER," + SoundModelContract.KEY_DATA + " BLOB" + ")"; - public DatabaseHelper(Context context, CursorFactory factory) { + public DatabaseHelper(Context context) { super(context, NAME, null, VERSION); } @@ -94,10 +93,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { onCreate(db); } - /** - * TODO: Change to addOrUpdate to handle changes here. - */ - public void addKeyphraseSoundModel(KeyphraseSoundModel soundModel) { + public boolean addOrUpdateKeyphraseSoundModel(KeyphraseSoundModel soundModel) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); // Generate a random ID for the model. @@ -105,19 +101,23 @@ public class DatabaseHelper extends SQLiteOpenHelper { values.put(SoundModelContract.KEY_DATA, soundModel.data); values.put(SoundModelContract.KEY_TYPE, SoundTrigger.SoundModel.TYPE_KEYPHRASE); - if (db.insert(SoundModelContract.TABLE, null, values) != -1) { + boolean status = true; + if (db.insertWithOnConflict( + SoundModelContract.TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE) != -1) { for (Keyphrase keyphrase : soundModel.keyphrases) { - addKeyphrase(soundModel.uuid, keyphrase); + status &= addKeyphrase(soundModel.uuid, keyphrase); } + return status; } else { Slog.w(TAG, "Failed to persist sound model to database"); + return false; } } /** * TODO(sansid): Change to addOrUpdate to handle changes here. */ - private void addKeyphrase(UUID modelId, Keyphrase keyphrase) { + private boolean addKeyphrase(UUID modelId, Keyphrase keyphrase) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KeyphraseContract.KEY_ID, keyphrase.id); @@ -125,8 +125,11 @@ public class DatabaseHelper extends SQLiteOpenHelper { values.put(KeyphraseContract.KEY_SOUND_MODEL_ID, keyphrase.id); 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.insert(KeyphraseContract.TABLE, null, values) != -1) { + return true; + } else { Slog.w(TAG, "Failed to persist keyphrase to database"); + return false; } } diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java index 85042f7..7204695 100644 --- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java +++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java @@ -62,11 +62,13 @@ public class VoiceInteractionManagerService extends SystemService { final Context mContext; final ContentResolver mResolver; + final DatabaseHelper mDbHelper; public VoiceInteractionManagerService(Context context) { super(context); mContext = context; mResolver = context.getContentResolver(); + mDbHelper = new DatabaseHelper(context); } @Override @@ -264,8 +266,7 @@ public class VoiceInteractionManagerService extends SystemService { final long caller = Binder.clearCallingIdentity(); try { - // TODO: Add the implementation here. - return null; + return mDbHelper.getKephraseSoundModels(); } finally { Binder.restoreCallingIdentity(caller); } @@ -282,8 +283,11 @@ public class VoiceInteractionManagerService extends SystemService { } final long caller = Binder.clearCallingIdentity(); try { - // TODO: Add the implementation here. - return VoiceInteractionManagerService.STATUS_ERROR; + if (mDbHelper.addOrUpdateKeyphraseSoundModel(model)) { + return STATUS_OK; + } else { + return STATUS_ERROR; + } } finally { Binder.restoreCallingIdentity(caller); } -- cgit v1.1