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