1 /*
2  * Copyright (C) 2020 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 internal class VoiceController {
31     /**
32      * If the `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      */
notifyVoiceSuccessnull38     fun notifyVoiceSuccess(activity: Activity, message: String) {
39         if (!Utils.isMOrLater) {
40             return
41         }
42 
43         val voiceInteractor: VoiceInteractor? = activity.getVoiceInteractor()
44         voiceInteractor?.let {
45             val prompt = Prompt(message)
46             it.submitRequest(CompleteVoiceRequest(prompt, null))
47         }
48     }
49 
50     /**
51      * If the `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      */
notifyVoiceFailurenull57     fun notifyVoiceFailure(activity: Activity, message: String) {
58         if (!Utils.isMOrLater) {
59             return
60         }
61 
62         val voiceInteractor: VoiceInteractor? = activity.getVoiceInteractor()
63         voiceInteractor?.let {
64             val prompt = Prompt(message)
65             it.submitRequest(AbortVoiceRequest(prompt, null))
66         }
67     }
68 }