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