1/*
2 * Copyright (C) 2015 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.deskclock;
18
19import android.app.Activity;
20import android.app.VoiceInteractor;
21
22/**
23 * Notifies Voice Interactor about whether the action
24 * was successful. Voice Interactor is called only if
25 * the build version is post-Lollipop.
26 */
27public final class Voice {
28
29    private static Delegate sDelegate = new VoiceInteractorDelegate();
30
31    private Voice() { }
32
33    public static void setDelegate(Delegate delegate) {
34        sDelegate = delegate;
35    }
36
37    public static void notifySuccess(Activity activity, String message) {
38        if (Utils.isMOrLater()) {
39            sDelegate.notifySuccess(activity.getVoiceInteractor(), message);
40        }
41    }
42
43    public static void notifyFailure(Activity activity, String message) {
44        if (Utils.isMOrLater()) {
45            sDelegate.notifyFailure(activity.getVoiceInteractor(), message);
46        }
47    }
48
49    public interface Delegate {
50        void notifySuccess(VoiceInteractor vi, String message);
51
52        void notifyFailure(VoiceInteractor vi, String message);
53    }
54
55    private static class VoiceInteractorDelegate implements Delegate {
56        @Override
57        public void notifySuccess(VoiceInteractor vi, String message) {
58            if (vi != null)  {
59                final VoiceInteractor.Prompt prompt = new VoiceInteractor.Prompt(message);
60                vi.submitRequest(new VoiceInteractor.CompleteVoiceRequest(prompt, null));
61            }
62        }
63
64        @Override
65        public void notifyFailure(VoiceInteractor vi, String message) {
66            if (vi != null)  {
67                final VoiceInteractor.Prompt prompt = new VoiceInteractor.Prompt(message);
68                vi.submitRequest(new VoiceInteractor.AbortVoiceRequest(prompt, null));
69            }
70        }
71    }
72}
73