Search in sources :

Example 6 with KeyphraseSoundModel

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

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 7 with KeyphraseSoundModel

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

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 8 with KeyphraseSoundModel

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

Example 9 with KeyphraseSoundModel

use of android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel 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 10 with KeyphraseSoundModel

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

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