MainInteractionService.java revision 110f569b47bc21fb38ec25b6110ee302ce137e06
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.VoiceInteractionService;
24import android.util.Log;
25
26import java.util.Arrays;
27
28public class MainInteractionService extends VoiceInteractionService {
29    static final String TAG = "MainInteractionService";
30
31    private final Callback mHotwordCallback = new Callback() {
32        @Override
33        public void onDetected(byte[] data) {
34            Log.i(TAG, "onDetected");
35        }
36
37        @Override
38        public void onDetectionStopped() {
39            Log.i(TAG, "onDetectionStopped");
40        }
41    };
42
43    private AlwaysOnHotwordDetector mHotwordDetector;
44
45    @Override
46    public void onReady() {
47        super.onReady();
48        Log.i(TAG, "Creating " + this);
49        Log.i(TAG, "Keyphrase enrollment error? " + getKeyphraseEnrollmentInfo().getParseError());
50        Log.i(TAG, "Keyphrase enrollment meta-data: "
51                + Arrays.toString(getKeyphraseEnrollmentInfo().listKeyphraseMetadata()));
52
53        mHotwordDetector = getAlwaysOnHotwordDetector("Hello There", "en-US", mHotwordCallback);
54        int availability = mHotwordDetector.getAvailability();
55        Log.i(TAG, "Hotword availability = " + availability);
56
57        switch (availability) {
58            case AlwaysOnHotwordDetector.KEYPHRASE_HARDWARE_UNAVAILABLE:
59                Log.i(TAG, "KEYPHRASE_HARDWARE_UNAVAILABLE");
60                break;
61            case AlwaysOnHotwordDetector.KEYPHRASE_UNSUPPORTED:
62                Log.i(TAG, "KEYPHRASE_UNSUPPORTED");
63                break;
64            case AlwaysOnHotwordDetector.KEYPHRASE_UNENROLLED:
65                Log.i(TAG, "KEYPHRASE_UNENROLLED");
66                Intent enroll = mHotwordDetector.getManageIntent(
67                        AlwaysOnHotwordDetector.MANAGE_ACTION_ENROLL);
68                Log.i(TAG, "Need to enroll with " + enroll);
69                break;
70            case AlwaysOnHotwordDetector.KEYPHRASE_ENROLLED:
71                Log.i(TAG, "KEYPHRASE_ENROLLED");
72                int status = mHotwordDetector.startRecognition(
73                        AlwaysOnHotwordDetector.RECOGNITION_FLAG_NONE);
74                Log.i(TAG, "startRecognition status = " + status);
75                break;
76        }
77    }
78
79    @Override
80    public int onStartCommand(Intent intent, int flags, int startId) {
81        Bundle args = new Bundle();
82        args.putParcelable("intent", new Intent(this, TestInteractionActivity.class));
83        startSession(args);
84        stopSelf(startId);
85        return START_NOT_STICKY;
86    }
87}
88