Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d1672245 authored by Sandeep Siddhartha's avatar Sandeep Siddhartha Committed by Android (Google) Code Review
Browse files

Merge "Test hotword flow" into lmp-dev

parents 61635036 7444c906
Loading
Loading
Loading
Loading
+11 −7
Original line number Diff line number Diff line
@@ -105,17 +105,18 @@ public class DatabaseHelper extends SQLiteOpenHelper {
        if (db.insertWithOnConflict(
                SoundModelContract.TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE) != -1) {
            for (Keyphrase keyphrase : soundModel.keyphrases) {
                status &= addOrUpdateKeyphrase(soundModel.uuid, keyphrase);
                status &= addOrUpdateKeyphrase(db, soundModel.uuid, keyphrase);
            }
            db.close();
            return status;
        } else {
            Slog.w(TAG, "Failed to persist sound model to database");
            db.close();
            return false;
        }
    }

    private boolean addOrUpdateKeyphrase(UUID modelId, Keyphrase keyphrase) {
        SQLiteDatabase db = getWritableDatabase();
    private boolean addOrUpdateKeyphrase(SQLiteDatabase db, UUID modelId, Keyphrase keyphrase) {
        ContentValues values = new ContentValues();
        values.put(KeyphraseContract.KEY_ID, keyphrase.id);
        values.put(KeyphraseContract.KEY_RECOGNITION_MODES, keyphrase.recognitionModeFlags);
@@ -148,6 +149,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
            Slog.w(TAG, "No keyphrases deleted from the database");
            status = false;
        }
        db.close();
        return status;
    }

@@ -157,7 +159,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
    public List<KeyphraseSoundModel> getKephraseSoundModels() {
        List<KeyphraseSoundModel> models = new ArrayList<>();
        String selectQuery = "SELECT  * FROM " + SoundModelContract.TABLE;
        SQLiteDatabase db = this.getReadableDatabase();
        SQLiteDatabase db = getReadableDatabase();
        Cursor c = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
@@ -172,17 +174,18 @@ public class DatabaseHelper extends SQLiteOpenHelper {
                byte[] data = c.getBlob(c.getColumnIndex(SoundModelContract.KEY_DATA));
                // Get all the keyphrases for this this sound model.
                models.add(new KeyphraseSoundModel(
                        UUID.fromString(id), data, getKeyphrasesForSoundModel(id)));
                        UUID.fromString(id), data, getKeyphrasesForSoundModel(db, id)));
            } while (c.moveToNext());
        }
        c.close();
        db.close();
        return models;
    }

    private Keyphrase[] getKeyphrasesForSoundModel(String modelId) {
    private Keyphrase[] getKeyphrasesForSoundModel(SQLiteDatabase db, String modelId) {
        List<Keyphrase> keyphrases = new ArrayList<>();
        String selectQuery = "SELECT  * FROM " + KeyphraseContract.TABLE
                + " WHERE " + KeyphraseContract.KEY_SOUND_MODEL_ID + " = '" + modelId + "'";
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
@@ -199,6 +202,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
        }
        Keyphrase[] keyphraseArr = new Keyphrase[keyphrases.size()];
        keyphrases.toArray(keyphraseArr);
        c.close();
        return keyphraseArr;
    }
}
+46 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ package com.android.test.voiceinteraction;

import android.content.Intent;
import android.os.Bundle;
import android.service.voice.AlwaysOnHotwordDetector;
import android.service.voice.AlwaysOnHotwordDetector.Callback;
import android.service.voice.VoiceInteractionService;
import android.util.Log;

@@ -26,6 +28,25 @@ import java.util.Arrays;
public class MainInteractionService extends VoiceInteractionService {
    static final String TAG = "MainInteractionService";

    private final Callback mHotwordCallback = new Callback() {
        @Override
        public void onDetected(byte[] data) {
            Log.i(TAG, "onDetected");
        }

        @Override
        public void onDetectionStarted() {
            Log.i(TAG, "onDetectionStarted");
        }

        @Override
        public void onDetectionStopped() {
            Log.i(TAG, "onDetectionStopped");
        }
    };

    private AlwaysOnHotwordDetector mHotwordDetector;

    @Override
    public void onReady() {
        super.onReady();
@@ -33,6 +54,31 @@ public class MainInteractionService extends VoiceInteractionService {
        Log.i(TAG, "Keyphrase enrollment error? " + getKeyphraseEnrollmentInfo().getParseError());
        Log.i(TAG, "Keyphrase enrollment meta-data: "
                + Arrays.toString(getKeyphraseEnrollmentInfo().listKeyphraseMetadata()));

        mHotwordDetector = getAlwaysOnHotwordDetector("Hello There", "en-US", mHotwordCallback);
        int availability = mHotwordDetector.getAvailability();
        Log.i(TAG, "Hotword availability = " + availability);

        switch (availability) {
            case AlwaysOnHotwordDetector.KEYPHRASE_HARDWARE_UNAVAILABLE:
                Log.i(TAG, "KEYPHRASE_HARDWARE_UNAVAILABLE");
                break;
            case AlwaysOnHotwordDetector.KEYPHRASE_UNSUPPORTED:
                Log.i(TAG, "KEYPHRASE_UNSUPPORTED");
                break;
            case AlwaysOnHotwordDetector.KEYPHRASE_UNENROLLED:
                Log.i(TAG, "KEYPHRASE_UNENROLLED");
                Intent enroll = mHotwordDetector.getManageIntent(
                        AlwaysOnHotwordDetector.MANAGE_ACTION_ENROLL);
                Log.i(TAG, "Need to enroll with " + enroll);
                break;
            case AlwaysOnHotwordDetector.KEYPHRASE_ENROLLED:
                Log.i(TAG, "KEYPHRASE_ENROLLED");
                int status = mHotwordDetector.startRecognition(
                        AlwaysOnHotwordDetector.RECOGNITION_FLAG_NONE);
                Log.i(TAG, "startRecognition status = " + status);
                break;
        }
    }

    @Override