• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.app.Activity;
20 import android.hardware.soundtrigger.SoundTrigger;
21 import android.hardware.soundtrigger.SoundTrigger.Keyphrase;
22 import android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel;
23 import android.os.Bundle;
24 import android.os.UserManager;
25 import android.util.Log;
26 import android.view.View;
27 import android.widget.Toast;
28 
29 import java.util.Locale;
30 import java.util.Random;
31 import java.util.UUID;
32 
33 /**
34  * TODO: must be transitioned to a service.
35  */
36 public class TestEnrollmentActivity extends Activity {
37     private static final String TAG = "TestEnrollmentActivity";
38     private static final boolean DBG = false;
39 
40     /** Keyphrase related constants, must match those defined in enrollment_application.xml */
41     private static final int KEYPHRASE_ID = 101;
42     private static final int RECOGNITION_MODES = SoundTrigger.RECOGNITION_MODE_VOICE_TRIGGER;
43     private static final String BCP47_LOCALE = "fr-FR";
44     private static final String TEXT = "Hello There";
45 
46     private EnrollmentUtil mEnrollmentUtil;
47     private Random mRandom;
48 
49     @Override
onCreate(Bundle savedInstanceState)50     protected void onCreate(Bundle savedInstanceState) {
51         if (DBG) Log.d(TAG, "onCreate");
52         super.onCreate(savedInstanceState);
53         setContentView(R.layout.main);
54         mEnrollmentUtil = new EnrollmentUtil();
55         mRandom = new Random();
56     }
57 
58     /**
59      * Called when the user clicks the enroll button.
60      * Performs a fresh enrollment.
61      */
onEnrollButtonClicked(View v)62     public void onEnrollButtonClicked(View v) {
63         Keyphrase kp = new Keyphrase(KEYPHRASE_ID, RECOGNITION_MODES,
64                 Locale.forLanguageTag(BCP47_LOCALE), TEXT,
65                 new int[] { UserManager.get(this).getProcessUserId() /* current user */});
66         UUID modelUuid = UUID.randomUUID();
67         // Generate a fake model to push.
68         byte[] data = new byte[1024];
69         mRandom.nextBytes(data);
70         KeyphraseSoundModel soundModel = new KeyphraseSoundModel(modelUuid, null, data,
71                 new Keyphrase[] { kp });
72         boolean status = mEnrollmentUtil.addOrUpdateSoundModel(soundModel);
73         if (status) {
74             Toast.makeText(
75                     this, "Successfully enrolled, model UUID=" + modelUuid, Toast.LENGTH_SHORT)
76                     .show();
77         } else {
78             Toast.makeText(this, "Failed to enroll!!!" + modelUuid, Toast.LENGTH_SHORT).show();
79         }
80     }
81 
82     /**
83      * Called when the user clicks the un-enroll button.
84      * Clears the enrollment information for the user.
85      */
onUnEnrollButtonClicked(View v)86     public void onUnEnrollButtonClicked(View v) {
87         KeyphraseSoundModel soundModel = mEnrollmentUtil.getSoundModel(KEYPHRASE_ID, BCP47_LOCALE);
88         if (soundModel == null) {
89             Toast.makeText(this, "Sound model not found!!!", Toast.LENGTH_SHORT).show();
90             return;
91         }
92         boolean status = mEnrollmentUtil.deleteSoundModel(KEYPHRASE_ID, BCP47_LOCALE);
93         if (status) {
94             Toast.makeText(this, "Successfully un-enrolled, model UUID=" + soundModel.getUuid(),
95                     Toast.LENGTH_SHORT)
96                     .show();
97         } else {
98             Toast.makeText(this, "Failed to un-enroll!!!", Toast.LENGTH_SHORT).show();
99         }
100     }
101 
102     /**
103      * Called when the user clicks the re-enroll button.
104      * Uses the previously enrolled sound model and makes changes to it before pushing it back.
105      */
onReEnrollButtonClicked(View v)106     public void onReEnrollButtonClicked(View v) {
107         KeyphraseSoundModel soundModel = mEnrollmentUtil.getSoundModel(KEYPHRASE_ID, BCP47_LOCALE);
108         if (soundModel == null) {
109             Toast.makeText(this, "Sound model not found!!!", Toast.LENGTH_SHORT).show();
110             return;
111         }
112         // Generate a fake model to push.
113         byte[] data = new byte[2048];
114         mRandom.nextBytes(data);
115         KeyphraseSoundModel updated = new KeyphraseSoundModel(soundModel.getUuid(),
116                 soundModel.getVendorUuid(), data, soundModel.getKeyphrases());
117         boolean status = mEnrollmentUtil.addOrUpdateSoundModel(updated);
118         if (status) {
119             Toast.makeText(this, "Successfully re-enrolled, model UUID=" + updated.getUuid(),
120                     Toast.LENGTH_SHORT)
121                     .show();
122         } else {
123             Toast.makeText(this, "Failed to re-enroll!!!", Toast.LENGTH_SHORT).show();
124         }
125     }
126 }
127