1 /*
2  * Copyright (C) 2014 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.settings.utils;
18 
19 import android.app.Activity;
20 import android.app.VoiceInteractor.AbortVoiceRequest;
21 import android.app.VoiceInteractor.CompleteVoiceRequest;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.util.Log;
25 
26 /**
27  * Activity for modifying a setting using the Voice Interaction API. This activity
28  * will only allow modifying the setting if the intent was sent using
29  * {@link android.service.voice.VoiceInteractionSession#startVoiceActivity startVoiceActivity}
30  * by the current Voice Interaction Service.
31  */
32 abstract public class VoiceSettingsActivity extends Activity {
33 
34     private static final String TAG = "VoiceSettingsActivity";
35 
36     @Override
onCreate(Bundle savedInstanceState)37     public void onCreate(Bundle savedInstanceState) {
38         super.onCreate(savedInstanceState);
39 
40         if (isVoiceInteractionRoot()) {
41             // Only permit if this is a voice interaction.
42             if (onVoiceSettingInteraction(getIntent())) {
43                 // If it's complete, finish.
44                 finish();
45             }
46         } else {
47             Log.v(TAG, "Cannot modify settings without voice interaction");
48             finish();
49         }
50     }
51 
52     /**
53      * Modify the setting as a voice interaction. Should return true if the
54      * voice interaction is complete or false if more interaction is required.
55      */
onVoiceSettingInteraction(Intent intent)56     abstract protected boolean onVoiceSettingInteraction(Intent intent);
57 
58     /**
59      * Send a notification that the interaction was successful. If {@param prompt} is
60      * not null, then it will be read to the user.
61      */
notifySuccess(CharSequence prompt)62     protected void notifySuccess(CharSequence prompt) {
63         if (getVoiceInteractor() != null) {
64             getVoiceInteractor().submitRequest(new CompleteVoiceRequest(prompt, null) {
65                 @Override
66                 public void onCompleteResult(Bundle options) {
67                     finish();
68                 }
69             });
70         }
71     }
72 
73     /**
74      * Indicates when the setting could not be changed.
75      */
notifyFailure(CharSequence prompt)76     protected void notifyFailure(CharSequence prompt) {
77         if (getVoiceInteractor() != null) {
78             getVoiceInteractor().submitRequest(new AbortVoiceRequest(prompt, null));
79         }
80     }
81 }
82