Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 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.content.Context; |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 20 | import android.content.Intent; |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 21 | import android.os.Bundle; |
| 22 | import android.service.voice.VoiceInteractionSession; |
| 23 | import android.util.Log; |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 24 | import android.view.View; |
| 25 | import android.widget.Button; |
| 26 | import android.widget.TextView; |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 27 | |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 28 | public class MainInteractionSession extends VoiceInteractionSession |
| 29 | implements View.OnClickListener { |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 30 | static final String TAG = "MainInteractionSession"; |
| 31 | |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 32 | Intent mStartIntent; |
| 33 | View mContentView; |
| 34 | TextView mText; |
| 35 | Button mStartButton; |
Dianne Hackborn | a2c076d | 2014-05-30 16:42:57 -0700 | [diff] [blame] | 36 | Button mConfirmButton; |
Barnaby James | d3fdb8b | 2014-07-07 10:51:06 -0700 | [diff] [blame] | 37 | Button mCompleteButton; |
Dianne Hackborn | a2c076d | 2014-05-30 16:42:57 -0700 | [diff] [blame] | 38 | Button mAbortButton; |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 39 | |
Dianne Hackborn | a2c076d | 2014-05-30 16:42:57 -0700 | [diff] [blame] | 40 | static final int STATE_IDLE = 0; |
| 41 | static final int STATE_LAUNCHING = 1; |
| 42 | static final int STATE_CONFIRM = 2; |
| 43 | static final int STATE_COMMAND = 3; |
| 44 | static final int STATE_ABORT_VOICE = 4; |
Barnaby James | d3fdb8b | 2014-07-07 10:51:06 -0700 | [diff] [blame] | 45 | static final int STATE_COMPLETE_VOICE = 5; |
Dianne Hackborn | a2c076d | 2014-05-30 16:42:57 -0700 | [diff] [blame] | 46 | |
| 47 | int mState = STATE_IDLE; |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 48 | Request mPendingRequest; |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 49 | |
| 50 | MainInteractionSession(Context context) { |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 51 | super(context); |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void onCreate(Bundle args) { |
| 56 | super.onCreate(args); |
| 57 | showWindow(); |
| 58 | mStartIntent = args.getParcelable("intent"); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public View onCreateContentView() { |
| 63 | mContentView = getLayoutInflater().inflate(R.layout.voice_interaction_session, null); |
| 64 | mText = (TextView)mContentView.findViewById(R.id.text); |
| 65 | mStartButton = (Button)mContentView.findViewById(R.id.start); |
| 66 | mStartButton.setOnClickListener(this); |
Dianne Hackborn | a2c076d | 2014-05-30 16:42:57 -0700 | [diff] [blame] | 67 | mConfirmButton = (Button)mContentView.findViewById(R.id.confirm); |
| 68 | mConfirmButton.setOnClickListener(this); |
Barnaby James | d3fdb8b | 2014-07-07 10:51:06 -0700 | [diff] [blame] | 69 | mCompleteButton = (Button)mContentView.findViewById(R.id.complete); |
| 70 | mCompleteButton.setOnClickListener(this); |
Dianne Hackborn | a2c076d | 2014-05-30 16:42:57 -0700 | [diff] [blame] | 71 | mAbortButton = (Button)mContentView.findViewById(R.id.abort); |
| 72 | mAbortButton.setOnClickListener(this); |
| 73 | updateState(); |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 74 | return mContentView; |
| 75 | } |
| 76 | |
Dianne Hackborn | a2c076d | 2014-05-30 16:42:57 -0700 | [diff] [blame] | 77 | void updateState() { |
| 78 | mStartButton.setEnabled(mState == STATE_IDLE); |
| 79 | mConfirmButton.setEnabled(mState == STATE_CONFIRM || mState == STATE_COMMAND); |
| 80 | mAbortButton.setEnabled(mState == STATE_ABORT_VOICE); |
Barnaby James | d3fdb8b | 2014-07-07 10:51:06 -0700 | [diff] [blame] | 81 | mCompleteButton.setEnabled(mState == STATE_COMPLETE_VOICE); |
Dianne Hackborn | a2c076d | 2014-05-30 16:42:57 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 84 | public void onClick(View v) { |
Dianne Hackborn | a2c076d | 2014-05-30 16:42:57 -0700 | [diff] [blame] | 85 | if (v == mStartButton) { |
| 86 | mState = STATE_LAUNCHING; |
| 87 | updateState(); |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 88 | startVoiceActivity(mStartIntent); |
Dianne Hackborn | a2c076d | 2014-05-30 16:42:57 -0700 | [diff] [blame] | 89 | } else if (v == mConfirmButton) { |
| 90 | if (mState == STATE_CONFIRM) { |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 91 | mPendingRequest.sendConfirmResult(true, null); |
| 92 | } else { |
| 93 | mPendingRequest.sendCommandResult(true, null); |
| 94 | } |
| 95 | mPendingRequest = null; |
Dianne Hackborn | a2c076d | 2014-05-30 16:42:57 -0700 | [diff] [blame] | 96 | mState = STATE_IDLE; |
| 97 | updateState(); |
| 98 | } else if (v == mAbortButton) { |
| 99 | mPendingRequest.sendAbortVoiceResult(null); |
| 100 | mPendingRequest = null; |
| 101 | mState = STATE_IDLE; |
| 102 | updateState(); |
Barnaby James | d3fdb8b | 2014-07-07 10:51:06 -0700 | [diff] [blame] | 103 | } else if (v== mCompleteButton) { |
| 104 | mPendingRequest.sendCompleteVoiceResult(null); |
| 105 | mPendingRequest = null; |
| 106 | mState = STATE_IDLE; |
| 107 | updateState(); |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 108 | } |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public boolean[] onGetSupportedCommands(Caller caller, String[] commands) { |
| 113 | return new boolean[commands.length]; |
| 114 | } |
| 115 | |
| 116 | @Override |
Dianne Hackborn | a2c076d | 2014-05-30 16:42:57 -0700 | [diff] [blame] | 117 | public void onConfirm(Caller caller, Request request, CharSequence prompt, Bundle extras) { |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 118 | Log.i(TAG, "onConfirm: prompt=" + prompt + " extras=" + extras); |
| 119 | mText.setText(prompt); |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 120 | mStartButton.setText("Confirm"); |
| 121 | mPendingRequest = request; |
Dianne Hackborn | a2c076d | 2014-05-30 16:42:57 -0700 | [diff] [blame] | 122 | mState = STATE_CONFIRM; |
| 123 | updateState(); |
| 124 | } |
| 125 | |
| 126 | @Override |
Barnaby James | d3fdb8b | 2014-07-07 10:51:06 -0700 | [diff] [blame] | 127 | public void onCompleteVoice(Caller caller, Request request, CharSequence message, Bundle extras) { |
| 128 | Log.i(TAG, "onCompleteVoice: message=" + message + " extras=" + extras); |
| 129 | mText.setText(message); |
| 130 | mPendingRequest = request; |
| 131 | mState = STATE_COMPLETE_VOICE; |
| 132 | updateState(); |
| 133 | } |
| 134 | |
| 135 | @Override |
Dianne Hackborn | a2c076d | 2014-05-30 16:42:57 -0700 | [diff] [blame] | 136 | public void onAbortVoice(Caller caller, Request request, CharSequence message, Bundle extras) { |
| 137 | Log.i(TAG, "onAbortVoice: message=" + message + " extras=" + extras); |
| 138 | mText.setText(message); |
| 139 | mPendingRequest = request; |
| 140 | mState = STATE_ABORT_VOICE; |
| 141 | updateState(); |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void onCommand(Caller caller, Request request, String command, Bundle extras) { |
| 146 | Log.i(TAG, "onCommand: command=" + command + " extras=" + extras); |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 147 | mText.setText("Command: " + command); |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 148 | mStartButton.setText("Finish Command"); |
| 149 | mPendingRequest = request; |
Dianne Hackborn | a2c076d | 2014-05-30 16:42:57 -0700 | [diff] [blame] | 150 | mState = STATE_COMMAND; |
| 151 | updateState(); |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void onCancel(Request request) { |
| 156 | Log.i(TAG, "onCancel"); |
| 157 | request.sendCancelResult(); |
| 158 | } |
| 159 | } |