Skip to content

Commit 256e1a6

Browse files
author
Sandeep Siddhartha
committed
Handle same keyphrase for multiple users
Also notify the VIS of a change after finishing the change Bug: 16816191 Change-Id: I6c96595819cd07182c3825c522d9bae559bb6814
1 parent a13104f commit 256e1a6

File tree

2 files changed

+69
-56
lines changed

2 files changed

+69
-56
lines changed

services/voiceinteraction/java/com/android/server/voiceinteraction/DatabaseHelper.java

Lines changed: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {

services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,19 +301,22 @@ public int deleteKeyphraseSoundModel(int keyphraseId) {
301301
}
302302

303303
final long caller = Binder.clearCallingIdentity();
304+
boolean deleted = false;
304305
try {
305-
if (mDbHelper.deleteKeyphraseSoundModel(keyphraseId)) {
306+
KeyphraseSoundModel soundModel = mDbHelper.getKeyphraseSoundModel(keyphraseId);
307+
if (soundModel != null) {
308+
deleted = mDbHelper.deleteKeyphraseSoundModel(soundModel.uuid);
309+
}
310+
return deleted ? SoundTriggerHelper.STATUS_OK : SoundTriggerHelper.STATUS_ERROR;
311+
} finally {
312+
if (deleted) {
306313
synchronized (this) {
307314
// Notify the voice interaction service of a change in sound models.
308315
if (mImpl != null && mImpl.mService != null) {
309316
mImpl.notifySoundModelsChangedLocked();
310317
}
311318
}
312-
return SoundTriggerHelper.STATUS_OK;
313-
} else {
314-
return SoundTriggerHelper.STATUS_ERROR;
315319
}
316-
} finally {
317320
Binder.restoreCallingIdentity(caller);
318321
}
319322
}

0 commit comments

Comments
 (0)