1 /*
2  * Copyright (C) 2016 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.deskclock.controller;
18 
19 import android.annotation.TargetApi;
20 import android.app.Activity;
21 import android.app.VoiceInteractor;
22 import android.app.VoiceInteractor.AbortVoiceRequest;
23 import android.app.VoiceInteractor.CompleteVoiceRequest;
24 import android.app.VoiceInteractor.Prompt;
25 import android.os.Build;
26 
27 import com.android.deskclock.Utils;
28 
29 @TargetApi(Build.VERSION_CODES.M)
30 class VoiceController {
31     /**
32      * If the {@code activity} is currently hosting a voice interaction session, indicate the voice
33      * command was processed successfully.
34      *
35      * @param activity an Activity that may be hosting a voice interaction session
36      * @param message to be spoken to the user to indicate success
37      */
notifyVoiceSuccess(Activity activity, String message)38     void notifyVoiceSuccess(Activity activity, String message) {
39         if (!Utils.isMOrLater()) {
40             return;
41         }
42 
43         final VoiceInteractor voiceInteractor = activity.getVoiceInteractor();
44         if (voiceInteractor != null) {
45             final Prompt prompt = new Prompt(message);
46             voiceInteractor.submitRequest(new CompleteVoiceRequest(prompt, null));
47         }
48     }
49 
50     /**
51      * If the {@code activity} is currently hosting a voice interaction session, indicate the voice
52      * command failed and must be aborted.
53      *
54      * @param activity an Activity that may be hosting a voice interaction session
55      * @param message to be spoken to the user to indicate failure
56      */
notifyVoiceFailure(Activity activity, String message)57     void notifyVoiceFailure(Activity activity, String message) {
58         if (!Utils.isMOrLater()) {
59             return;
60         }
61 
62         final VoiceInteractor voiceInteractor = activity.getVoiceInteractor();
63         if (voiceInteractor != null) {
64             final Prompt prompt = new Prompt(message);
65             voiceInteractor.submitRequest(new AbortVoiceRequest(prompt, null));
66         }
67     }
68 }