1bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux/*
2bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux * Copyright (C) 2016 The Android Open Source Project
3bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux *
4bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux * Licensed under the Apache License, Version 2.0 (the "License");
5bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux * you may not use this file except in compliance with the License.
6bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux * You may obtain a copy of the License at
7bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux *
8bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux *      http://www.apache.org/licenses/LICENSE-2.0
9bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux *
10bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux * Unless required by applicable law or agreed to in writing, software
11bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux * distributed under the License is distributed on an "AS IS" BASIS,
12bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux * See the License for the specific language governing permissions and
14bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux * limitations under the License.
15bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux */
16bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux
17bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieuxpackage com.android.deskclock.controller;
18bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux
1984dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieuximport android.annotation.TargetApi;
20bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieuximport android.app.Activity;
2184dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieuximport android.app.VoiceInteractor;
2284dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieuximport android.app.VoiceInteractor.AbortVoiceRequest;
2384dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieuximport android.app.VoiceInteractor.CompleteVoiceRequest;
2484dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieuximport android.app.VoiceInteractor.Prompt;
2584dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieuximport android.os.Build;
26bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux
2784dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieuximport com.android.deskclock.Utils;
2884dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux
2984dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux@TargetApi(Build.VERSION_CODES.M)
3084dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieuxclass VoiceController {
31bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux    /**
32bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux     * If the {@code activity} is currently hosting a voice interaction session, indicate the voice
33bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux     * command was processed successfully.
34bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux     *
35bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux     * @param activity an Activity that may be hosting a voice interaction session
36bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux     * @param message to be spoken to the user to indicate success
37bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux     */
3884dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux    void notifyVoiceSuccess(Activity activity, String message) {
3984dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux        if (!Utils.isMOrLater()) {
4084dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux            return;
4184dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux        }
4284dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux
4384dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux        final VoiceInteractor voiceInteractor = activity.getVoiceInteractor();
4484dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux        if (voiceInteractor != null) {
4584dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux            final Prompt prompt = new Prompt(message);
4684dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux            voiceInteractor.submitRequest(new CompleteVoiceRequest(prompt, null));
4784dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux        }
4884dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux    }
49bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux
50bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux    /**
51bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux     * If the {@code activity} is currently hosting a voice interaction session, indicate the voice
52bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux     * command failed and must be aborted.
53bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux     *
54bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux     * @param activity an Activity that may be hosting a voice interaction session
55bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux     * @param message to be spoken to the user to indicate failure
56bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux     */
5784dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux    void notifyVoiceFailure(Activity activity, String message) {
5884dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux        if (!Utils.isMOrLater()) {
5984dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux            return;
6084dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux        }
6184dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux
6284dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux        final VoiceInteractor voiceInteractor = activity.getVoiceInteractor();
6384dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux        if (voiceInteractor != null) {
6484dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux            final Prompt prompt = new Prompt(message);
6584dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux            voiceInteractor.submitRequest(new AbortVoiceRequest(prompt, null));
6684dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux        }
6784dc3cd5823e75ba4e422b1f72b03c5cca9a2fc7James Lemieux    }
68bd250dfa5bdcd618c5dfd7f48b662cf4b51c31a2James Lemieux}