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