1 /*
2  * Copyright (C) 2016 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.test.voiceinteraction;
18 
19 import android.app.Activity;
20 import android.app.VoiceInteractor;
21 import android.os.Bundle;
22 import android.util.Log;
23 import android.view.View;
24 import android.widget.Button;
25 import android.widget.TextView;
26 
27 public class StartVoiceInteractionActivity extends Activity implements View.OnClickListener {
28     static final String TAG = "LocalVoiceInteractionActivity";
29 
30     static final String REQUEST_ABORT = "abort";
31     static final String REQUEST_COMPLETE = "complete";
32     static final String REQUEST_COMMAND = "command";
33     static final String REQUEST_PICK = "pick";
34     static final String REQUEST_CONFIRM = "confirm";
35 
36     VoiceInteractor mInteractor;
37     VoiceInteractor.Request mCurrentRequest = null;
38     TextView mLog;
39     Button mCommandButton;
40     Button mPickButton;
41     Button mCancelButton;
42     Button mStartButton;
43     Button mStopButton;
44 
45     @Override
onCreate(Bundle savedInstanceState)46     public void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48 
49         setContentView(R.layout.local_interaction_app);
50 
51         mLog = (TextView)findViewById(R.id.log);
52         mCommandButton = (Button)findViewById(R.id.command);
53         mCommandButton.setOnClickListener(this);
54         mPickButton = (Button)findViewById(R.id.pick);
55         mPickButton.setOnClickListener(this);
56         mCancelButton = (Button)findViewById(R.id.cancel);
57         mCancelButton.setOnClickListener(this);
58         mStartButton = findViewById(R.id.start);
59         mStartButton.setOnClickListener(this);
60         mStopButton = findViewById(R.id.stop);
61         mStopButton.setOnClickListener(this);
62 
63         mLog.append("Local Voice Interaction Supported = " + isLocalVoiceInteractionSupported());
64     }
65 
66     @Override
onResume()67     public void onResume() {
68         super.onResume();
69     }
70 
71     @Override
onClick(View v)72     public void onClick(View v) {
73         if (v == mCommandButton) {
74             VoiceInteractor.CommandRequest req = new TestCommand("Some arg");
75             mInteractor.submitRequest(req, REQUEST_COMMAND);
76         } else if (v == mPickButton) {
77             VoiceInteractor.PickOptionRequest.Option[] options =
78                     new VoiceInteractor.PickOptionRequest.Option[5];
79             options[0] = new VoiceInteractor.PickOptionRequest.Option("One");
80             options[1] = new VoiceInteractor.PickOptionRequest.Option("Two");
81             options[2] = new VoiceInteractor.PickOptionRequest.Option("Three");
82             options[3] = new VoiceInteractor.PickOptionRequest.Option("Four");
83             options[4] = new VoiceInteractor.PickOptionRequest.Option("Five");
84             VoiceInteractor.PickOptionRequest req = new TestPickOption(options);
85             mInteractor.submitRequest(req, REQUEST_PICK);
86         } else if (v == mCancelButton && mCurrentRequest != null) {
87             Log.i(TAG, "Cancel request");
88             mCurrentRequest.cancel();
89         } else if (v == mStartButton) {
90             Bundle args = new Bundle();
91             args.putString("Foo", "Bar");
92             startLocalVoiceInteraction(args);
93         } else if (v == mStopButton) {
94             stopLocalVoiceInteraction();
95         }
96     }
97 
98     @Override
onLocalVoiceInteractionStarted()99     public void onLocalVoiceInteractionStarted() {
100         mInteractor = getVoiceInteractor();
101         mLog.append("\nLocalVoiceInteraction started!");
102         mStopButton.setEnabled(true);
103     }
104 
105     @Override
onLocalVoiceInteractionStopped()106     public void onLocalVoiceInteractionStopped() {
107         mInteractor = getVoiceInteractor();
108         mLog.append("\nLocalVoiceInteraction stopped!");
109         mStopButton.setEnabled(false);
110     }
111 
112     @Override
onDestroy()113     public void onDestroy() {
114         super.onDestroy();
115     }
116 
117     static class TestAbortVoice extends VoiceInteractor.AbortVoiceRequest {
TestAbortVoice()118         public TestAbortVoice() {
119             super(new VoiceInteractor.Prompt("Dammit, we suck :("), null);
120         }
onCancel()121         @Override public void onCancel() {
122             Log.i(TAG, "Canceled!");
123             ((StartVoiceInteractionActivity)getActivity()).mLog.append("Canceled abort\n");
124         }
onAbortResult(Bundle result)125         @Override public void onAbortResult(Bundle result) {
126             Log.i(TAG, "Abort result: result=" + result);
127             ((StartVoiceInteractionActivity)getActivity()).mLog.append(
128                     "Abort: result=" + result + "\n");
129             getActivity().finish();
130         }
131     }
132 
133     static class TestCompleteVoice extends VoiceInteractor.CompleteVoiceRequest {
TestCompleteVoice()134         public TestCompleteVoice() {
135             super(new VoiceInteractor.Prompt("Woohoo, completed!"), null);
136         }
onCancel()137         @Override public void onCancel() {
138             Log.i(TAG, "Canceled!");
139             ((StartVoiceInteractionActivity)getActivity()).mLog.append("Canceled complete\n");
140         }
onCompleteResult(Bundle result)141         @Override public void onCompleteResult(Bundle result) {
142             Log.i(TAG, "Complete result: result=" + result);
143             ((StartVoiceInteractionActivity)getActivity()).mLog.append("Complete: result="
144                     + result + "\n");
145             getActivity().finish();
146         }
147     }
148 
149     static class TestCommand extends VoiceInteractor.CommandRequest {
TestCommand(String arg)150         public TestCommand(String arg) {
151             super("com.android.test.voiceinteraction.COMMAND", makeBundle(arg));
152         }
onCancel()153         @Override public void onCancel() {
154             Log.i(TAG, "Canceled!");
155             ((StartVoiceInteractionActivity)getActivity()).mLog.append("Canceled command\n");
156         }
157         @Override
onCommandResult(boolean finished, Bundle result)158         public void onCommandResult(boolean finished, Bundle result) {
159             Log.i(TAG, "Command result: finished=" + finished + " result=" + result);
160             StringBuilder sb = new StringBuilder();
161             if (finished) {
162                 sb.append("Command final result: ");
163             } else {
164                 sb.append("Command intermediate result: ");
165             }
166             if (result != null) {
167                 result.getString("key");
168             }
169             sb.append(result);
170             sb.append("\n");
171             ((StartVoiceInteractionActivity)getActivity()).mLog.append(sb.toString());
172         }
makeBundle(String arg)173         static Bundle makeBundle(String arg) {
174             Bundle b = new Bundle();
175             b.putString("key", arg);
176             return b;
177         }
178     }
179 
180     static class TestPickOption extends VoiceInteractor.PickOptionRequest {
TestPickOption(Option[] options)181         public TestPickOption(Option[] options) {
182             super(new VoiceInteractor.Prompt("Need to pick something"), options, null);
183         }
onCancel()184         @Override public void onCancel() {
185             Log.i(TAG, "Canceled!");
186             ((StartVoiceInteractionActivity)getActivity()).mLog.append("Canceled pick\n");
187         }
188         @Override
onPickOptionResult(boolean finished, Option[] selections, Bundle result)189         public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
190             Log.i(TAG, "Pick result: finished=" + finished + " selections=" + selections
191                     + " result=" + result);
192             StringBuilder sb = new StringBuilder();
193             if (finished) {
194                 sb.append("Pick final result: ");
195             } else {
196                 sb.append("Pick intermediate result: ");
197             }
198             for (int i=0; i<selections.length; i++) {
199                 if (i >= 1) {
200                     sb.append(", ");
201                 }
202                 sb.append(selections[i].getLabel());
203             }
204             sb.append("\n");
205             ((StartVoiceInteractionActivity)getActivity()).mLog.append(sb.toString());
206         }
207     }
208 }
209