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