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.test.voiceinteraction;
18 
19 import android.app.Activity;
20 import android.app.VoiceInteractor;
21 import android.content.ComponentName;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.provider.Settings;
25 import android.service.voice.VoiceInteractionService;
26 import android.util.Log;
27 import android.view.View;
28 import android.widget.Button;
29 import android.widget.TextView;
30 
31 import java.util.Arrays;
32 
33 public class TestInteractionActivity extends Activity implements View.OnClickListener {
34     static final String TAG = "TestInteractionActivity";
35 
36     static final String REQUEST_ABORT = "abort";
37     static final String REQUEST_COMPLETE = "complete";
38     static final String REQUEST_COMMAND = "command";
39     static final String REQUEST_PICK = "pick";
40     static final String REQUEST_CONFIRM = "confirm";
41 
42     VoiceInteractor mInteractor;
43     VoiceInteractor.Request mCurrentRequest = null;
44     TextView mLog;
45     Button mAirplaneButton;
46     Button mAbortButton;
47     Button mCompleteButton;
48     Button mCommandButton;
49     Button mPickButton;
50     Button mJumpOutButton;
51     Button mCancelButton;
52 
53     @Override
onCreate(Bundle savedInstanceState)54     public void onCreate(Bundle savedInstanceState) {
55         super.onCreate(savedInstanceState);
56 
57         if (!isVoiceInteraction()) {
58             Log.w(TAG, "Not running as a voice interaction!");
59             finish();
60             return;
61         }
62 
63         if (!VoiceInteractionService.isActiveService(this,
64                 new ComponentName(this, MainInteractionService.class))) {
65             Log.w(TAG, "Not current voice interactor!");
66             finish();
67             return;
68         }
69 
70         setContentView(R.layout.test_interaction);
71         mLog = (TextView)findViewById(R.id.log);
72         mAirplaneButton = (Button)findViewById(R.id.airplane);
73         mAirplaneButton.setOnClickListener(this);
74         mAbortButton = (Button)findViewById(R.id.abort);
75         mAbortButton.setOnClickListener(this);
76         mCompleteButton = (Button)findViewById(R.id.complete);
77         mCompleteButton.setOnClickListener(this);
78         mCommandButton = (Button)findViewById(R.id.command);
79         mCommandButton.setOnClickListener(this);
80         mPickButton = (Button)findViewById(R.id.pick);
81         mPickButton.setOnClickListener(this);
82         mJumpOutButton = (Button)findViewById(R.id.jump);
83         mJumpOutButton.setOnClickListener(this);
84         mCancelButton = (Button)findViewById(R.id.cancel);
85         mCancelButton.setOnClickListener(this);
86 
87         mInteractor = getVoiceInteractor();
88 
89         VoiceInteractor.Request[] active = mInteractor.getActiveRequests();
90         for (int i=0; i<active.length; i++) {
91             Log.i(TAG, "Active #" + i + " / " + active[i].getName() + ": " + active[i]);
92         }
93 
94         mCurrentRequest = mInteractor.getActiveRequest(REQUEST_CONFIRM);
95         if (mCurrentRequest == null) {
96             mCurrentRequest = new VoiceInteractor.ConfirmationRequest(
97                     new VoiceInteractor.Prompt("This is a confirmation"), null) {
98                 @Override
99                 public void onCancel() {
100                     Log.i(TAG, "Canceled!");
101                     getActivity().finish();
102                 }
103 
104                 @Override
105                 public void onConfirmationResult(boolean confirmed, Bundle result) {
106                     Log.i(TAG, "Confirmation result: confirmed=" + confirmed + " result=" + result);
107                     getActivity().finish();
108                 }
109             };
110             mInteractor.submitRequest(mCurrentRequest, REQUEST_CONFIRM);
111             String[] cmds = new String[] {
112                     "com.android.test.voiceinteraction.COMMAND",
113                     "com.example.foo.bar"
114             };
115             boolean sup[] = mInteractor.supportsCommands(cmds);
116             for (int i=0; i<cmds.length; i++) {
117                 mLog.append(cmds[i] + ": " + (sup[i] ? "SUPPORTED" : "NOT SUPPORTED") + "\n");
118             }
119         } else {
120             Log.i(TAG, "Restarting with active confirmation: " + mCurrentRequest);
121         }
122     }
123 
124     @Override
onResume()125     public void onResume() {
126         super.onResume();
127     }
128 
129     @Override
onClick(View v)130     public void onClick(View v) {
131         if (v == mAirplaneButton) {
132             Intent intent = new Intent(Settings.ACTION_VOICE_CONTROL_AIRPLANE_MODE);
133             intent.addCategory(Intent.CATEGORY_VOICE);
134             intent.putExtra(Settings.EXTRA_AIRPLANE_MODE_ENABLED, true);
135             startActivity(intent);
136         } else if (v == mAbortButton) {
137             VoiceInteractor.AbortVoiceRequest req = new TestAbortVoice();
138             mInteractor.submitRequest(req, REQUEST_ABORT);
139         } else if (v == mCompleteButton) {
140             VoiceInteractor.CompleteVoiceRequest req = new TestCompleteVoice();
141             mInteractor.submitRequest(req, REQUEST_COMPLETE);
142         } else if (v == mCommandButton) {
143             VoiceInteractor.CommandRequest req = new TestCommand("Some arg");
144             mInteractor.submitRequest(req, REQUEST_COMMAND);
145         } else if (v == mPickButton) {
146             VoiceInteractor.PickOptionRequest.Option[] options =
147                     new VoiceInteractor.PickOptionRequest.Option[5];
148             options[0] = new VoiceInteractor.PickOptionRequest.Option("One");
149             options[1] = new VoiceInteractor.PickOptionRequest.Option("Two");
150             options[2] = new VoiceInteractor.PickOptionRequest.Option("Three");
151             options[3] = new VoiceInteractor.PickOptionRequest.Option("Four");
152             options[4] = new VoiceInteractor.PickOptionRequest.Option("Five");
153             VoiceInteractor.PickOptionRequest req = new TestPickOption(options);
154             mInteractor.submitRequest(req, REQUEST_PICK);
155         } else if (v == mJumpOutButton) {
156             Log.i(TAG, "Jump out");
157             Intent intent = new Intent(Intent.ACTION_MAIN);
158             intent.addCategory(Intent.CATEGORY_LAUNCHER);
159             intent.setComponent(new ComponentName(this, VoiceInteractionMain.class));
160             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
161             startActivity(intent);
162         } else if (v == mCancelButton && mCurrentRequest != null) {
163             Log.i(TAG, "Cancel request");
164             mCurrentRequest.cancel();
165         }
166     }
167 
168     @Override
onDestroy()169     public void onDestroy() {
170         super.onDestroy();
171     }
172 
173     static class TestAbortVoice extends VoiceInteractor.AbortVoiceRequest {
TestAbortVoice()174         public TestAbortVoice() {
175             super(new VoiceInteractor.Prompt("Dammit, we suck :("), null);
176         }
onCancel()177         @Override public void onCancel() {
178             Log.i(TAG, "Canceled!");
179             ((TestInteractionActivity)getActivity()).mLog.append("Canceled abort\n");
180         }
onAbortResult(Bundle result)181         @Override public void onAbortResult(Bundle result) {
182             Log.i(TAG, "Abort result: result=" + result);
183             ((TestInteractionActivity)getActivity()).mLog.append("Abort: result=" + result + "\n");
184             getActivity().finish();
185         }
186     }
187 
188     static class TestCompleteVoice extends VoiceInteractor.CompleteVoiceRequest {
TestCompleteVoice()189         public TestCompleteVoice() {
190             super(new VoiceInteractor.Prompt("Woohoo, completed!"), null);
191         }
onCancel()192         @Override public void onCancel() {
193             Log.i(TAG, "Canceled!");
194             ((TestInteractionActivity)getActivity()).mLog.append("Canceled complete\n");
195         }
onCompleteResult(Bundle result)196         @Override public void onCompleteResult(Bundle result) {
197             Log.i(TAG, "Complete result: result=" + result);
198             ((TestInteractionActivity)getActivity()).mLog.append("Complete: result="
199                     + result + "\n");
200             getActivity().finish();
201         }
202     }
203 
204     static class TestCommand extends VoiceInteractor.CommandRequest {
TestCommand(String arg)205         public TestCommand(String arg) {
206             super("com.android.test.voiceinteraction.COMMAND", makeBundle(arg));
207         }
onCancel()208         @Override public void onCancel() {
209             Log.i(TAG, "Canceled!");
210             ((TestInteractionActivity)getActivity()).mLog.append("Canceled command\n");
211         }
212         @Override
onCommandResult(boolean finished, Bundle result)213         public void onCommandResult(boolean finished, Bundle result) {
214             Log.i(TAG, "Command result: finished=" + finished + " result=" + result);
215             StringBuilder sb = new StringBuilder();
216             if (finished) {
217                 sb.append("Command final result: ");
218             } else {
219                 sb.append("Command intermediate result: ");
220             }
221             if (result != null) {
222                 result.getString("key");
223             }
224             sb.append(result);
225             sb.append("\n");
226             ((TestInteractionActivity)getActivity()).mLog.append(sb.toString());
227         }
makeBundle(String arg)228         static Bundle makeBundle(String arg) {
229             Bundle b = new Bundle();
230             b.putString("key", arg);
231             return b;
232         }
233     }
234 
235     static class TestPickOption extends VoiceInteractor.PickOptionRequest {
TestPickOption(Option[] options)236         public TestPickOption(Option[] options) {
237             super(new VoiceInteractor.Prompt("Need to pick something"), options, null);
238         }
onCancel()239         @Override public void onCancel() {
240             Log.i(TAG, "Canceled!");
241             ((TestInteractionActivity)getActivity()).mLog.append("Canceled pick\n");
242         }
243         @Override
onPickOptionResult(boolean finished, Option[] selections, Bundle result)244         public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
245             Log.i(TAG, "Pick result: finished=" + finished
246                     + " selections=" + Arrays.toString(selections)
247                     + " result=" + result);
248             StringBuilder sb = new StringBuilder();
249             if (finished) {
250                 sb.append("Pick final result: ");
251             } else {
252                 sb.append("Pick intermediate result: ");
253             }
254             for (int i=0; i<selections.length; i++) {
255                 if (i >= 1) {
256                     sb.append(", ");
257                 }
258                 sb.append(selections[i].getLabel());
259             }
260             sb.append("\n");
261             ((TestInteractionActivity)getActivity()).mLog.append(sb.toString());
262         }
263     }
264 }
265