@@ -41,12 +41,12 @@ public class DatabaseHelper extends SQLiteOpenHelper {
4141 static final boolean DBG = true ;
4242
4343 private static final String NAME = "sound_model.db" ;
44- private static final int VERSION = 3 ;
44+ private static final int VERSION = 4 ;
4545
4646 public static interface SoundModelContract {
4747 public static final String TABLE = "sound_model" ;
48- public static final String KEY_KEYPHRASE_ID = "keyphrase_id" ;
4948 public static final String KEY_MODEL_UUID = "model_uuid" ;
49+ public static final String KEY_KEYPHRASE_ID = "keyphrase_id" ;
5050 public static final String KEY_TYPE = "type" ;
5151 public static final String KEY_DATA = "data" ;
5252 public static final String KEY_RECOGNITION_MODES = "recognition_modes" ;
@@ -58,8 +58,8 @@ public static interface SoundModelContract {
5858 // Table Create Statement
5959 private static final String CREATE_TABLE_SOUND_MODEL = "CREATE TABLE "
6060 + SoundModelContract .TABLE + "("
61- + SoundModelContract .KEY_KEYPHRASE_ID + " INTEGER PRIMARY KEY,"
62- + SoundModelContract .KEY_MODEL_UUID + " TEXT ,"
61+ + SoundModelContract .KEY_MODEL_UUID + " TEXT PRIMARY KEY,"
62+ + SoundModelContract .KEY_KEYPHRASE_ID + " INTEGER ,"
6363 + SoundModelContract .KEY_TYPE + " INTEGER,"
6464 + SoundModelContract .KEY_DATA + " BLOB,"
6565 + SoundModelContract .KEY_RECOGNITION_MODES + " INTEGER,"
@@ -122,10 +122,16 @@ public boolean updateKeyphraseSoundModel(KeyphraseSoundModel soundModel) {
122122 /**
123123 * Deletes the sound model and associated keyphrases.
124124 */
125- public boolean deleteKeyphraseSoundModel (int keyphraseId ) {
125+ public boolean deleteKeyphraseSoundModel (UUID modelUuid ) {
126+ if (modelUuid == null ) {
127+ Slog .w (TAG , "Model UUID must be specified for deletion" );
128+ return false ;
129+ }
130+
126131 synchronized (this ) {
127132 SQLiteDatabase db = getWritableDatabase ();
128- String soundModelClause = SoundModelContract .KEY_KEYPHRASE_ID + "=" + keyphraseId ;
133+ String soundModelClause = SoundModelContract .KEY_MODEL_UUID + "="
134+ + modelUuid .toString ();
129135
130136 try {
131137 return db .delete (SoundModelContract .TABLE , soundModelClause , null ) != 0 ;
@@ -151,52 +157,56 @@ public KeyphraseSoundModel getKeyphraseSoundModel(int keyphraseId) {
151157
152158 try {
153159 if (c .moveToFirst ()) {
154- int type = c .getInt (c .getColumnIndex (SoundModelContract .KEY_TYPE ));
155- if (type != SoundTrigger .SoundModel .TYPE_KEYPHRASE ) {
156- Slog .w (TAG , "No KeyphraseSoundModel available for the given keyphrase" );
157- return null ;
158- }
159-
160- String modelUuid = c .getString (
161- c .getColumnIndex (SoundModelContract .KEY_MODEL_UUID ));
162- if (modelUuid == null ) {
163- Slog .w (TAG , "Ignoring sound model since it doesn't specify an ID" );
164- return null ;
165- }
166-
167- byte [] data = c .getBlob (c .getColumnIndex (SoundModelContract .KEY_DATA ));
168- int recognitionModes = c .getInt (
169- c .getColumnIndex (SoundModelContract .KEY_RECOGNITION_MODES ));
170- int [] users = getArrayForCommaSeparatedString (
171- c .getString (c .getColumnIndex (SoundModelContract .KEY_USERS )));
172- String locale = c .getString (c .getColumnIndex (SoundModelContract .KEY_LOCALE ));
173- String text = c .getString (
174- c .getColumnIndex (SoundModelContract .KEY_HINT_TEXT ));
175-
176- // Only add keyphrases meant for the current user.
177- if (users == null ) {
178- // No users present in the keyphrase.
179- Slog .w (TAG , "Ignoring keyphrase since it doesn't specify users" );
180- return null ;
181- }
182- boolean isAvailableForCurrentUser = false ;
183- int currentUser = mUserManager .getUserHandle ();
184- for (int user : users ) {
185- if (currentUser == user ) {
186- isAvailableForCurrentUser = true ;
187- break ;
160+ do {
161+ int type = c .getInt (c .getColumnIndex (SoundModelContract .KEY_TYPE ));
162+ if (type != SoundTrigger .SoundModel .TYPE_KEYPHRASE ) {
163+ Slog .w (TAG , "Ignoring sound model since it's type is incorrect" );
164+ continue ;
165+ }
166+
167+ String modelUuid = c .getString (
168+ c .getColumnIndex (SoundModelContract .KEY_MODEL_UUID ));
169+ if (modelUuid == null ) {
170+ Slog .w (TAG , "Ignoring sound model since it doesn't specify an ID" );
171+ continue ;
188172 }
189- }
190- if (!isAvailableForCurrentUser ) {
191- Slog .w (TAG , "Ignoring keyphrase since it's not for the current user" );
192- return null ;
193- }
194-
195- Keyphrase [] keyphrases = new Keyphrase [1 ];
196- keyphrases [0 ] = new Keyphrase (
197- keyphraseId , recognitionModes , locale , text , users );
198- return new KeyphraseSoundModel (UUID .fromString (modelUuid ),
199- null /* FIXME use vendor UUID */ , data , keyphrases );
173+
174+ byte [] data = c .getBlob (c .getColumnIndex (SoundModelContract .KEY_DATA ));
175+ int recognitionModes = c .getInt (
176+ c .getColumnIndex (SoundModelContract .KEY_RECOGNITION_MODES ));
177+ int [] users = getArrayForCommaSeparatedString (
178+ c .getString (c .getColumnIndex (SoundModelContract .KEY_USERS )));
179+ String locale = c .getString (
180+ c .getColumnIndex (SoundModelContract .KEY_LOCALE ));
181+ String text = c .getString (
182+ c .getColumnIndex (SoundModelContract .KEY_HINT_TEXT ));
183+
184+ // Only add keyphrases meant for the current user.
185+ if (users == null ) {
186+ // No users present in the keyphrase.
187+ Slog .w (TAG , "Ignoring keyphrase since it doesn't specify users" );
188+ continue ;
189+ }
190+
191+ boolean isAvailableForCurrentUser = false ;
192+ int currentUser = mUserManager .getUserHandle ();
193+ for (int user : users ) {
194+ if (currentUser == user ) {
195+ isAvailableForCurrentUser = true ;
196+ break ;
197+ }
198+ }
199+ if (!isAvailableForCurrentUser ) {
200+ Slog .w (TAG , "Ignoring keyphrase since it's not for the current user" );
201+ continue ;
202+ }
203+
204+ Keyphrase [] keyphrases = new Keyphrase [1 ];
205+ keyphrases [0 ] = new Keyphrase (
206+ keyphraseId , recognitionModes , locale , text , users );
207+ return new KeyphraseSoundModel (UUID .fromString (modelUuid ),
208+ null /* FIXME use vendor UUID */ , data , keyphrases );
209+ } while (c .moveToNext ());
200210 }
201211 Slog .w (TAG , "No SoundModel available for the given keyphrase" );
202212 } finally {
0 commit comments