1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.test.voiceenrollment; 18 19 import java.util.Random; 20 import java.util.UUID; 21 22 import android.app.Activity; 23 import android.hardware.soundtrigger.SoundTrigger; 24 import android.hardware.soundtrigger.SoundTrigger.Keyphrase; 25 import android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel; 26 import android.os.Bundle; 27 import android.os.UserManager; 28 import android.util.Log; 29 import android.view.View; 30 import android.widget.Toast; 31 32 public class TestEnrollmentActivity extends Activity { 33 private static final String TAG = "TestEnrollmentActivity"; 34 private static final boolean DBG = true; 35 36 /** Keyphrase related constants, must match those defined in enrollment_application.xml */ 37 private static final int KEYPHRASE_ID = 101; 38 private static final int RECOGNITION_MODES = SoundTrigger.RECOGNITION_MODE_VOICE_TRIGGER; 39 private static final String BCP47_LOCALE = "fr-FR"; 40 private static final String TEXT = "Hello There"; 41 42 private EnrollmentUtil mEnrollmentUtil; 43 private Random mRandom; 44 45 @Override 46 protected void onCreate(Bundle savedInstanceState) { 47 if (DBG) Log.d(TAG, "onCreate"); 48 super.onCreate(savedInstanceState); 49 setContentView(R.layout.main); 50 mEnrollmentUtil = new EnrollmentUtil(); 51 mRandom = new Random(); 52 } 53 54 /** 55 * Called when the user clicks the enroll button. 56 * Performs a fresh enrollment. 57 */ 58 public void onEnrollButtonClicked(View v) { 59 Keyphrase kp = new Keyphrase(KEYPHRASE_ID, RECOGNITION_MODES, BCP47_LOCALE, TEXT, 60 new int[] { UserManager.get(this).getUserHandle() /* current user */}); 61 UUID modelUuid = UUID.randomUUID(); 62 // Generate a fake model to push. 63 byte[] data = new byte[1024]; 64 mRandom.nextBytes(data); 65 KeyphraseSoundModel soundModel = new KeyphraseSoundModel(modelUuid, null, data, 66 new Keyphrase[] { kp }); 67 boolean status = mEnrollmentUtil.addOrUpdateSoundModel(soundModel); 68 if (status) { 69 Toast.makeText( 70 this, "Successfully enrolled, model UUID=" + modelUuid, Toast.LENGTH_SHORT) 71 .show(); 72 } else { 73 Toast.makeText(this, "Failed to enroll!!!" + modelUuid, Toast.LENGTH_SHORT).show(); 74 } 75 } 76 77 /** 78 * Called when the user clicks the un-enroll button. 79 * Clears the enrollment information for the user. 80 */ 81 public void onUnEnrollButtonClicked(View v) { 82 KeyphraseSoundModel soundModel = mEnrollmentUtil.getSoundModel(KEYPHRASE_ID, BCP47_LOCALE); 83 if (soundModel == null) { 84 Toast.makeText(this, "Sound model not found!!!", Toast.LENGTH_SHORT).show(); 85 return; 86 } 87 boolean status = mEnrollmentUtil.deleteSoundModel(KEYPHRASE_ID, BCP47_LOCALE); 88 if (status) { 89 Toast.makeText(this, "Successfully un-enrolled, model UUID=" + soundModel.uuid, 90 Toast.LENGTH_SHORT) 91 .show(); 92 } else { 93 Toast.makeText(this, "Failed to un-enroll!!!", Toast.LENGTH_SHORT).show(); 94 } 95 } 96 97 /** 98 * Called when the user clicks the re-enroll button. 99 * Uses the previously enrolled sound model and makes changes to it before pushing it back. 100 */ 101 public void onReEnrollButtonClicked(View v) { 102 KeyphraseSoundModel soundModel = mEnrollmentUtil.getSoundModel(KEYPHRASE_ID, BCP47_LOCALE); 103 if (soundModel == null) { 104 Toast.makeText(this, "Sound model not found!!!", Toast.LENGTH_SHORT).show(); 105 return; 106 } 107 // Generate a fake model to push. 108 byte[] data = new byte[2048]; 109 mRandom.nextBytes(data); 110 KeyphraseSoundModel updated = new KeyphraseSoundModel(soundModel.uuid, 111 soundModel.vendorUuid, data, soundModel.keyphrases); 112 boolean status = mEnrollmentUtil.addOrUpdateSoundModel(updated); 113 if (status) { 114 Toast.makeText(this, "Successfully re-enrolled, model UUID=" + updated.uuid, 115 Toast.LENGTH_SHORT) 116 .show(); 117 } else { 118 Toast.makeText(this, "Failed to re-enroll!!!", Toast.LENGTH_SHORT).show(); 119 } 120 } 121 } 122