MainInteractionService.java revision db92e35211d875eab75787c593eea2988c8a560f
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 17package com.android.test.voiceinteraction; 18 19import android.content.Intent; 20import android.os.Bundle; 21import android.service.voice.AlwaysOnHotwordDetector; 22import android.service.voice.AlwaysOnHotwordDetector.Callback; 23import android.service.voice.AlwaysOnHotwordDetector.EventPayload; 24import android.service.voice.VoiceInteractionService; 25import android.util.Log; 26 27import java.util.Arrays; 28 29public 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 50 private AlwaysOnHotwordDetector mHotwordDetector; 51 52 @Override 53 public void onReady() { 54 super.onReady(); 55 Log.i(TAG, "Creating " + this); 56 Log.i(TAG, "Keyphrase enrollment error? " + getKeyphraseEnrollmentInfo().getParseError()); 57 Log.i(TAG, "Keyphrase enrollment meta-data: " 58 + Arrays.toString(getKeyphraseEnrollmentInfo().listKeyphraseMetadata())); 59 60 mHotwordDetector = createAlwaysOnHotwordDetector("Hello There", "en-US", mHotwordCallback); 61 } 62 63 @Override 64 public int onStartCommand(Intent intent, int flags, int startId) { 65 Bundle args = new Bundle(); 66 args.putParcelable("intent", new Intent(this, TestInteractionActivity.class)); 67 startSession(args); 68 stopSelf(startId); 69 return START_NOT_STICKY; 70 } 71 72 private void hotwordAvailabilityChangeHelper(int availability) { 73 Log.i(TAG, "Hotword availability = " + availability); 74 switch (availability) { 75 case AlwaysOnHotwordDetector.STATE_HARDWARE_UNAVAILABLE: 76 Log.i(TAG, "STATE_HARDWARE_UNAVAILABLE"); 77 break; 78 case AlwaysOnHotwordDetector.STATE_KEYPHRASE_UNSUPPORTED: 79 Log.i(TAG, "STATE_KEYPHRASE_UNSUPPORTED"); 80 break; 81 case AlwaysOnHotwordDetector.STATE_KEYPHRASE_UNENROLLED: 82 Log.i(TAG, "STATE_KEYPHRASE_UNENROLLED"); 83 Intent enroll = mHotwordDetector.getManageIntent( 84 AlwaysOnHotwordDetector.MANAGE_ACTION_ENROLL); 85 Log.i(TAG, "Need to enroll with " + enroll); 86 break; 87 case AlwaysOnHotwordDetector.STATE_KEYPHRASE_ENROLLED: 88 Log.i(TAG, "STATE_KEYPHRASE_ENROLLED - starting recognition"); 89 if (mHotwordDetector.startRecognition( 90 AlwaysOnHotwordDetector.RECOGNITION_FLAG_NONE)) { 91 Log.i(TAG, "startRecognition succeeded"); 92 } else { 93 Log.i(TAG, "startRecognition failed"); 94 } 95 break; 96 } 97 } 98} 99