Search in sources :

Example 16 with Keyphrase

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

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 17 with Keyphrase

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

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 18 with Keyphrase

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

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 19 with Keyphrase

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

the class TestEnrollmentActivity method onEnrollButtonClicked.

/**
     * Called when the user clicks the enroll button.
     * Performs a fresh enrollment.
     */
public void onEnrollButtonClicked(View v) {
    Keyphrase kp = new Keyphrase(KEYPHRASE_ID, RECOGNITION_MODES, BCP47_LOCALE, TEXT, new int[] { UserManager.get(this).getUserHandle() });
    UUID modelUuid = UUID.randomUUID();
    // Generate a fake model to push.
    byte[] data = new byte[1024];
    mRandom.nextBytes(data);
    KeyphraseSoundModel soundModel = new KeyphraseSoundModel(modelUuid, null, data, new Keyphrase[] { kp });
    boolean status = mEnrollmentUtil.addOrUpdateSoundModel(soundModel);
    if (status) {
        Toast.makeText(this, "Successfully enrolled, model UUID=" + modelUuid, Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "Failed to enroll!!!" + modelUuid, Toast.LENGTH_SHORT).show();
    }
}
Also used : KeyphraseSoundModel(android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel) Keyphrase(android.hardware.soundtrigger.SoundTrigger.Keyphrase) UUID(java.util.UUID)

Example 20 with Keyphrase

use of android.hardware.soundtrigger.SoundTrigger.Keyphrase 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)

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