Search in sources :

Example 26 with KeyphraseSoundModel

use of android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel in project platform_frameworks_base by android.

the class DatabaseHelper method getKeyphraseSoundModel.

/**
     * Returns a matching {@link KeyphraseSoundModel} for the keyphrase ID.
     * Returns null if a match isn't found.
     *
     * TODO: We only support one keyphrase currently.
     */
public KeyphraseSoundModel getKeyphraseSoundModel(int keyphraseId, int userHandle, String bcp47Locale) {
    // Sanitize the locale to guard against SQL injection.
    bcp47Locale = Locale.forLanguageTag(bcp47Locale).toLanguageTag();
    synchronized (this) {
        // Find the corresponding sound model ID for the keyphrase.
        String selectQuery = "SELECT  * FROM " + SoundModelContract.TABLE + " WHERE " + SoundModelContract.KEY_KEYPHRASE_ID + "= '" + keyphraseId + "' AND " + SoundModelContract.KEY_LOCALE + "='" + bcp47Locale + "'";
        SQLiteDatabase db = getReadableDatabase();
        Cursor c = db.rawQuery(selectQuery, null);
        try {
            if (c.moveToFirst()) {
                do {
                    int type = c.getInt(c.getColumnIndex(SoundModelContract.KEY_TYPE));
                    if (type != SoundTrigger.SoundModel.TYPE_KEYPHRASE) {
                        if (DBG) {
                            Slog.w(TAG, "Ignoring SoundModel since it's type is incorrect");
                        }
                        continue;
                    }
                    String modelUuid = c.getString(c.getColumnIndex(SoundModelContract.KEY_MODEL_UUID));
                    if (modelUuid == null) {
                        Slog.w(TAG, "Ignoring SoundModel since it doesn't specify an ID");
                        continue;
                    }
                    String vendorUuidString = null;
                    int vendorUuidColumn = c.getColumnIndex(SoundModelContract.KEY_VENDOR_UUID);
                    if (vendorUuidColumn != -1) {
                        vendorUuidString = c.getString(vendorUuidColumn);
                    }
                    byte[] data = c.getBlob(c.getColumnIndex(SoundModelContract.KEY_DATA));
                    int recognitionModes = c.getInt(c.getColumnIndex(SoundModelContract.KEY_RECOGNITION_MODES));
                    int[] users = getArrayForCommaSeparatedString(c.getString(c.getColumnIndex(SoundModelContract.KEY_USERS)));
                    String modelLocale = c.getString(c.getColumnIndex(SoundModelContract.KEY_LOCALE));
                    String text = c.getString(c.getColumnIndex(SoundModelContract.KEY_HINT_TEXT));
                    // Only add keyphrases meant for the current user.
                    if (users == null) {
                        // No users present in the keyphrase.
                        Slog.w(TAG, "Ignoring SoundModel since it doesn't specify users");
                        continue;
                    }
                    boolean isAvailableForCurrentUser = false;
                    for (int user : users) {
                        if (userHandle == user) {
                            isAvailableForCurrentUser = true;
                            break;
                        }
                    }
                    if (!isAvailableForCurrentUser) {
                        if (DBG) {
                            Slog.w(TAG, "Ignoring SoundModel since user handles don't match");
                        }
                        continue;
                    } else {
                        if (DBG)
                            Slog.d(TAG, "Found a SoundModel for user: " + userHandle);
                    }
                    Keyphrase[] keyphrases = new Keyphrase[1];
                    keyphrases[0] = new Keyphrase(keyphraseId, recognitionModes, modelLocale, text, users);
                    UUID vendorUuid = null;
                    if (vendorUuidString != null) {
                        vendorUuid = UUID.fromString(vendorUuidString);
                    }
                    KeyphraseSoundModel model = new KeyphraseSoundModel(UUID.fromString(modelUuid), vendorUuid, data, keyphrases);
                    if (DBG) {
                        Slog.d(TAG, "Found SoundModel for the given keyphrase/locale/user: " + model);
                    }
                    return model;
                } while (c.moveToNext());
            }
            Slog.w(TAG, "No SoundModel available for the given keyphrase");
        } finally {
            c.close();
            db.close();
        }
        return null;
    }
}
Also used : KeyphraseSoundModel(android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Keyphrase(android.hardware.soundtrigger.SoundTrigger.Keyphrase) Cursor(android.database.Cursor) UUID(java.util.UUID)

Example 27 with KeyphraseSoundModel

use of android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel in project android_frameworks_base by DirtyUnicorns.

the class DatabaseHelper method getKeyphraseSoundModel.

/**
     * Returns a matching {@link KeyphraseSoundModel} for the keyphrase ID.
     * Returns null if a match isn't found.
     *
     * TODO: We only support one keyphrase currently.
     */
public KeyphraseSoundModel getKeyphraseSoundModel(int keyphraseId, int userHandle, String bcp47Locale) {
    // Sanitize the locale to guard against SQL injection.
    bcp47Locale = Locale.forLanguageTag(bcp47Locale).toLanguageTag();
    synchronized (this) {
        // Find the corresponding sound model ID for the keyphrase.
        String selectQuery = "SELECT  * FROM " + SoundModelContract.TABLE + " WHERE " + SoundModelContract.KEY_KEYPHRASE_ID + "= '" + keyphraseId + "' AND " + SoundModelContract.KEY_LOCALE + "='" + bcp47Locale + "'";
        SQLiteDatabase db = getReadableDatabase();
        Cursor c = db.rawQuery(selectQuery, null);
        try {
            if (c.moveToFirst()) {
                do {
                    int type = c.getInt(c.getColumnIndex(SoundModelContract.KEY_TYPE));
                    if (type != SoundTrigger.SoundModel.TYPE_KEYPHRASE) {
                        if (DBG) {
                            Slog.w(TAG, "Ignoring SoundModel since it's type is incorrect");
                        }
                        continue;
                    }
                    String modelUuid = c.getString(c.getColumnIndex(SoundModelContract.KEY_MODEL_UUID));
                    if (modelUuid == null) {
                        Slog.w(TAG, "Ignoring SoundModel since it doesn't specify an ID");
                        continue;
                    }
                    String vendorUuidString = null;
                    int vendorUuidColumn = c.getColumnIndex(SoundModelContract.KEY_VENDOR_UUID);
                    if (vendorUuidColumn != -1) {
                        vendorUuidString = c.getString(vendorUuidColumn);
                    }
                    byte[] data = c.getBlob(c.getColumnIndex(SoundModelContract.KEY_DATA));
                    int recognitionModes = c.getInt(c.getColumnIndex(SoundModelContract.KEY_RECOGNITION_MODES));
                    int[] users = getArrayForCommaSeparatedString(c.getString(c.getColumnIndex(SoundModelContract.KEY_USERS)));
                    String modelLocale = c.getString(c.getColumnIndex(SoundModelContract.KEY_LOCALE));
                    String text = c.getString(c.getColumnIndex(SoundModelContract.KEY_HINT_TEXT));
                    // Only add keyphrases meant for the current user.
                    if (users == null) {
                        // No users present in the keyphrase.
                        Slog.w(TAG, "Ignoring SoundModel since it doesn't specify users");
                        continue;
                    }
                    boolean isAvailableForCurrentUser = false;
                    for (int user : users) {
                        if (userHandle == user) {
                            isAvailableForCurrentUser = true;
                            break;
                        }
                    }
                    if (!isAvailableForCurrentUser) {
                        if (DBG) {
                            Slog.w(TAG, "Ignoring SoundModel since user handles don't match");
                        }
                        continue;
                    } else {
                        if (DBG)
                            Slog.d(TAG, "Found a SoundModel for user: " + userHandle);
                    }
                    Keyphrase[] keyphrases = new Keyphrase[1];
                    keyphrases[0] = new Keyphrase(keyphraseId, recognitionModes, modelLocale, text, users);
                    UUID vendorUuid = null;
                    if (vendorUuidString != null) {
                        vendorUuid = UUID.fromString(vendorUuidString);
                    }
                    KeyphraseSoundModel model = new KeyphraseSoundModel(UUID.fromString(modelUuid), vendorUuid, data, keyphrases);
                    if (DBG) {
                        Slog.d(TAG, "Found SoundModel for the given keyphrase/locale/user: " + model);
                    }
                    return model;
                } while (c.moveToNext());
            }
            Slog.w(TAG, "No SoundModel available for the given keyphrase");
        } finally {
            c.close();
            db.close();
        }
        return null;
    }
}
Also used : KeyphraseSoundModel(android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Keyphrase(android.hardware.soundtrigger.SoundTrigger.Keyphrase) Cursor(android.database.Cursor) UUID(java.util.UUID)

Example 28 with KeyphraseSoundModel

use of android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel in project android_frameworks_base by DirtyUnicorns.

the class TestEnrollmentActivity method onReEnrollButtonClicked.

/**
     * Called when the user clicks the re-enroll button.
     * Uses the previously enrolled sound model and makes changes to it before pushing it back.
     */
public void onReEnrollButtonClicked(View v) {
    KeyphraseSoundModel soundModel = mEnrollmentUtil.getSoundModel(KEYPHRASE_ID, BCP47_LOCALE);
    if (soundModel == null) {
        Toast.makeText(this, "Sound model not found!!!", Toast.LENGTH_SHORT).show();
        return;
    }
    // Generate a fake model to push.
    byte[] data = new byte[2048];
    mRandom.nextBytes(data);
    KeyphraseSoundModel updated = new KeyphraseSoundModel(soundModel.uuid, soundModel.vendorUuid, data, soundModel.keyphrases);
    boolean status = mEnrollmentUtil.addOrUpdateSoundModel(updated);
    if (status) {
        Toast.makeText(this, "Successfully re-enrolled, model UUID=" + updated.uuid, Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "Failed to re-enroll!!!", Toast.LENGTH_SHORT).show();
    }
}
Also used : KeyphraseSoundModel(android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel)

Example 29 with KeyphraseSoundModel

use of android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel in project android_frameworks_base by DirtyUnicorns.

the class TestEnrollmentActivity method onUnEnrollButtonClicked.

/**
     * Called when the user clicks the un-enroll button.
     * Clears the enrollment information for the user.
     */
public void onUnEnrollButtonClicked(View v) {
    KeyphraseSoundModel soundModel = mEnrollmentUtil.getSoundModel(KEYPHRASE_ID, BCP47_LOCALE);
    if (soundModel == null) {
        Toast.makeText(this, "Sound model not found!!!", Toast.LENGTH_SHORT).show();
        return;
    }
    boolean status = mEnrollmentUtil.deleteSoundModel(KEYPHRASE_ID, BCP47_LOCALE);
    if (status) {
        Toast.makeText(this, "Successfully un-enrolled, model UUID=" + soundModel.uuid, Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "Failed to un-enroll!!!", Toast.LENGTH_SHORT).show();
    }
}
Also used : KeyphraseSoundModel(android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel)

Example 30 with KeyphraseSoundModel

use of android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel in project android_frameworks_base by DirtyUnicorns.

the class SoundTriggerTest method testKeyphraseSoundModelParcelUnparcel_noData.

@SmallTest
public void testKeyphraseSoundModelParcelUnparcel_noData() throws Exception {
    Keyphrase[] keyphrases = new Keyphrase[2];
    keyphrases[0] = new Keyphrase(1, 0, "en-US", "hello", new int[] { 0 });
    keyphrases[1] = new Keyphrase(2, 0, "fr-FR", "there", new int[] { 1, 2 });
    KeyphraseSoundModel ksm = new KeyphraseSoundModel(UUID.randomUUID(), UUID.randomUUID(), null, keyphrases);
    // Write to a parcel
    Parcel parcel = Parcel.obtain();
    ksm.writeToParcel(parcel, 0);
    // Read from it
    parcel.setDataPosition(0);
    KeyphraseSoundModel unparceled = KeyphraseSoundModel.CREATOR.createFromParcel(parcel);
    // Verify that they are the same
    assertEquals(ksm.uuid, unparceled.uuid);
    assertNull(unparceled.data);
    assertEquals(ksm.type, unparceled.type);
    assertTrue(Arrays.equals(keyphrases, unparceled.keyphrases));
}
Also used : KeyphraseSoundModel(android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel) Parcel(android.os.Parcel) Keyphrase(android.hardware.soundtrigger.SoundTrigger.Keyphrase) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

KeyphraseSoundModel (android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel)40 Keyphrase (android.hardware.soundtrigger.SoundTrigger.Keyphrase)20 Parcel (android.os.Parcel)20 SmallTest (android.test.suitebuilder.annotation.SmallTest)16 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)8 UUID (java.util.UUID)8 Cursor (android.database.Cursor)4 LargeTest (android.test.suitebuilder.annotation.LargeTest)4