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