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.voiceinteraction;
18 
19 import android.content.Intent;
20 import android.service.voice.AlwaysOnHotwordDetector;
21 import android.service.voice.AlwaysOnHotwordDetector.Callback;
22 import android.service.voice.AlwaysOnHotwordDetector.EventPayload;
23 import android.service.voice.VoiceInteractionService;
24 import android.util.Log;
25 
26 import java.util.Arrays;
27 import java.util.Locale;
28 
29 public class MainInteractionService extends VoiceInteractionService {
30     static final String TAG = "MainInteractionService";
31 
32     private final Callback mHotwordCallback = new Callback() {
33         @Override
34         public void onAvailabilityChanged(int status) {
35             Log.i(TAG, "onAvailabilityChanged(" + status + ")");
36             hotwordAvailabilityChangeHelper(status);
37         }
38 
39         @Override
40         public void onDetected(EventPayload eventPayload) {
41             Log.i(TAG, "onDetected");
42         }
43 
44         @Override
45         public void onError() {
46             Log.i(TAG, "onError");
47         }
48 
49         @Override
50         public void onRecognitionPaused() {
51             Log.i(TAG, "onRecognitionPaused");
52         }
53 
54         @Override
55         public void onRecognitionResumed() {
56             Log.i(TAG, "onRecognitionResumed");
57         }
58     };
59 
60     private AlwaysOnHotwordDetector mHotwordDetector;
61 
62     @Override
onReady()63     public void onReady() {
64         super.onReady();
65         Log.i(TAG, "Creating " + this);
66         Log.i(TAG, "Keyphrase enrollment error? " + getKeyphraseEnrollmentInfo().getParseError());
67         Log.i(TAG, "Keyphrase enrollment meta-data: "
68                 + Arrays.toString(getKeyphraseEnrollmentInfo().listKeyphraseMetadata().toArray(
69                 new android.hardware.soundtrigger.KeyphraseMetadata[0])));
70 
71         mHotwordDetector = createAlwaysOnHotwordDetector(
72                 "Hello There", Locale.forLanguageTag("en-US"), mHotwordCallback);
73     }
74 
hotwordAvailabilityChangeHelper(int availability)75     private void hotwordAvailabilityChangeHelper(int availability) {
76         Log.i(TAG, "Hotword availability = " + availability);
77         switch (availability) {
78             case AlwaysOnHotwordDetector.STATE_HARDWARE_UNAVAILABLE:
79                 Log.i(TAG, "STATE_HARDWARE_UNAVAILABLE");
80                 break;
81             case AlwaysOnHotwordDetector.STATE_KEYPHRASE_UNSUPPORTED:
82                 Log.i(TAG, "STATE_KEYPHRASE_UNSUPPORTED");
83                 break;
84             case AlwaysOnHotwordDetector.STATE_KEYPHRASE_UNENROLLED:
85                 Log.i(TAG, "STATE_KEYPHRASE_UNENROLLED");
86                 Intent enroll = mHotwordDetector.createEnrollIntent();
87                 Log.i(TAG, "Need to enroll with " + enroll);
88                 break;
89             case AlwaysOnHotwordDetector.STATE_KEYPHRASE_ENROLLED:
90                 Log.i(TAG, "STATE_KEYPHRASE_ENROLLED - starting recognition");
91                 if (mHotwordDetector.startRecognition(
92                         AlwaysOnHotwordDetector.RECOGNITION_FLAG_NONE)) {
93                     Log.i(TAG, "startRecognition succeeded");
94                 } else {
95                     Log.i(TAG, "startRecognition failed");
96                 }
97                 break;
98         }
99     }
100 }
101