Search in sources :

Example 1 with Keyphrase

use of android.hardware.soundtrigger.SoundTrigger.Keyphrase in project android_frameworks_base by ResurrectionRemix.

the class SoundTriggerTest method testKeyphraseSoundModelParcelUnparcel_zeroData.

@SmallTest
public void testKeyphraseSoundModelParcelUnparcel_zeroData() 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(), new byte[0], 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);
    assertEquals(ksm.type, unparceled.type);
    assertTrue(Arrays.equals(ksm.keyphrases, unparceled.keyphrases));
    assertTrue(Arrays.equals(ksm.data, unparceled.data));
}
Also used : KeyphraseSoundModel(android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel) Parcel(android.os.Parcel) Keyphrase(android.hardware.soundtrigger.SoundTrigger.Keyphrase) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 2 with Keyphrase

use of android.hardware.soundtrigger.SoundTrigger.Keyphrase in project android_frameworks_base by ResurrectionRemix.

the class SoundTriggerTest method testKeyphraseParcelUnparcel_pos.

@SmallTest
public void testKeyphraseParcelUnparcel_pos() throws Exception {
    Keyphrase keyphrase = new Keyphrase(1, 0, "en-US", "hello", new int[] { 1, 2, 3, 4, 5 });
    // Write to a parcel
    Parcel parcel = Parcel.obtain();
    keyphrase.writeToParcel(parcel, 0);
    // Read from it
    parcel.setDataPosition(0);
    Keyphrase unparceled = Keyphrase.CREATOR.createFromParcel(parcel);
    // Verify that they are the same
    assertEquals(keyphrase.id, unparceled.id);
    assertTrue(Arrays.equals(keyphrase.users, unparceled.users));
    assertEquals(keyphrase.locale, unparceled.locale);
    assertEquals(keyphrase.text, unparceled.text);
}
Also used : Parcel(android.os.Parcel) Keyphrase(android.hardware.soundtrigger.SoundTrigger.Keyphrase) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 3 with Keyphrase

use of android.hardware.soundtrigger.SoundTrigger.Keyphrase in project android_frameworks_base by ResurrectionRemix.

the class SoundTriggerTest method testKeyphraseSoundModelParcelUnparcel_largeData.

@LargeTest
public void testKeyphraseSoundModelParcelUnparcel_largeData() 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 });
    byte[] data = new byte[200 * 1024];
    mRandom.nextBytes(data);
    KeyphraseSoundModel ksm = new KeyphraseSoundModel(UUID.randomUUID(), UUID.randomUUID(), data, 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);
    assertEquals(ksm.type, unparceled.type);
    assertTrue(Arrays.equals(ksm.data, unparceled.data));
    assertTrue(Arrays.equals(ksm.keyphrases, unparceled.keyphrases));
}
Also used : KeyphraseSoundModel(android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel) Parcel(android.os.Parcel) Keyphrase(android.hardware.soundtrigger.SoundTrigger.Keyphrase) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 4 with Keyphrase

use of android.hardware.soundtrigger.SoundTrigger.Keyphrase in project android_frameworks_base by ResurrectionRemix.

the class SoundTriggerTest method testKeyphraseParcelUnparcel_noUsers.

@SmallTest
public void testKeyphraseParcelUnparcel_noUsers() throws Exception {
    Keyphrase keyphrase = new Keyphrase(1, 0, "en-US", "hello", null);
    // Write to a parcel
    Parcel parcel = Parcel.obtain();
    keyphrase.writeToParcel(parcel, 0);
    // Read from it
    parcel.setDataPosition(0);
    Keyphrase unparceled = Keyphrase.CREATOR.createFromParcel(parcel);
    // Verify that they are the same
    assertEquals(keyphrase.id, unparceled.id);
    assertNull(unparceled.users);
    assertEquals(keyphrase.locale, unparceled.locale);
    assertEquals(keyphrase.text, unparceled.text);
}
Also used : Parcel(android.os.Parcel) Keyphrase(android.hardware.soundtrigger.SoundTrigger.Keyphrase) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 5 with Keyphrase

use of android.hardware.soundtrigger.SoundTrigger.Keyphrase in project android_frameworks_base by ResurrectionRemix.

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)

Aggregations

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