summaryrefslogtreecommitdiffstats
path: root/tests/VoiceInteraction/src/com/android/test/voiceinteraction/TestInteractionActivity.java
blob: c038414daf8c62e12eeb78bdcdc37a9945145be9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.test.voiceinteraction;

import android.app.Activity;
import android.app.VoiceInteractor;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.service.voice.VoiceInteractionService;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TestInteractionActivity extends Activity implements View.OnClickListener {
    static final String TAG = "TestInteractionActivity";

    static final String REQUEST_ABORT = "abort";
    static final String REQUEST_COMPLETE = "complete";
    static final String REQUEST_PICK = "pick";
    static final String REQUEST_CONFIRM = "confirm";

    VoiceInteractor mInteractor;
    VoiceInteractor.Request mCurrentRequest = null;
    TextView mLog;
    Button mAbortButton;
    Button mCompleteButton;
    Button mPickButton;
    Button mJumpOutButton;
    Button mCancelButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!isVoiceInteraction()) {
            Log.w(TAG, "Not running as a voice interaction!");
            finish();
            return;
        }

        if (!VoiceInteractionService.isActiveService(this,
                new ComponentName(this, MainInteractionService.class))) {
            Log.w(TAG, "Not current voice interactor!");
            finish();
            return;
        }

        setContentView(R.layout.test_interaction);
        mLog = (TextView)findViewById(R.id.log);
        mAbortButton = (Button)findViewById(R.id.abort);
        mAbortButton.setOnClickListener(this);
        mCompleteButton = (Button)findViewById(R.id.complete);
        mCompleteButton.setOnClickListener(this);
        mPickButton = (Button)findViewById(R.id.pick);
        mPickButton.setOnClickListener(this);
        mJumpOutButton = (Button)findViewById(R.id.jump);
        mJumpOutButton.setOnClickListener(this);
        mCancelButton = (Button)findViewById(R.id.cancel);
        mCancelButton.setOnClickListener(this);

        mInteractor = getVoiceInteractor();

        VoiceInteractor.Request[] active = mInteractor.getActiveRequests();
        for (int i=0; i<active.length; i++) {
            Log.i(TAG, "Active #" + i + " / " + active[i].getName() + ": " + active[i]);
        }

        mCurrentRequest = mInteractor.getActiveRequest(REQUEST_CONFIRM);
        if (mCurrentRequest == null) {
            mCurrentRequest = new VoiceInteractor.ConfirmationRequest(
                    new VoiceInteractor.Prompt("This is a confirmation"), null) {
                @Override
                public void onCancel() {
                    Log.i(TAG, "Canceled!");
                    getActivity().finish();
                }

                @Override
                public void onConfirmationResult(boolean confirmed, Bundle result) {
                    Log.i(TAG, "Confirmation result: confirmed=" + confirmed + " result=" + result);
                    getActivity().finish();
                }
            };
            mInteractor.submitRequest(mCurrentRequest, REQUEST_CONFIRM);
        } else {
            Log.i(TAG, "Restarting with active confirmation: " + mCurrentRequest);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        if (v == mAbortButton) {
            VoiceInteractor.AbortVoiceRequest req = new VoiceInteractor.AbortVoiceRequest(
                    new VoiceInteractor.Prompt("Dammit, we suck :("), null) {
                @Override
                public void onCancel() {
                    Log.i(TAG, "Canceled!");
                    mLog.append("Canceled abort\n");
                }

                @Override
                public void onAbortResult(Bundle result) {
                    Log.i(TAG, "Abort result: result=" + result);
                    mLog.append("Abort: result=" + result + "\n");
                    getActivity().finish();
                }
            };
            mInteractor.submitRequest(req, REQUEST_ABORT);
        } else if (v == mCompleteButton) {
            VoiceInteractor.CompleteVoiceRequest req = new VoiceInteractor.CompleteVoiceRequest(
                    new VoiceInteractor.Prompt("Woohoo, completed!"), null) {
                @Override
                public void onCancel() {
                    Log.i(TAG, "Canceled!");
                    mLog.append("Canceled complete\n");
                }

                @Override
                public void onCompleteResult(Bundle result) {
                    Log.i(TAG, "Complete result: result=" + result);
                    mLog.append("Complete: result=" + result + "\n");
                    getActivity().finish();
                }
            };
            mInteractor.submitRequest(req, REQUEST_COMPLETE);
        } else if (v == mPickButton) {
            VoiceInteractor.PickOptionRequest.Option[] options =
                    new VoiceInteractor.PickOptionRequest.Option[5];
            options[0] = new VoiceInteractor.PickOptionRequest.Option("One");
            options[1] = new VoiceInteractor.PickOptionRequest.Option("Two");
            options[2] = new VoiceInteractor.PickOptionRequest.Option("Three");
            options[3] = new VoiceInteractor.PickOptionRequest.Option("Four");
            options[4] = new VoiceInteractor.PickOptionRequest.Option("Five");
            VoiceInteractor.PickOptionRequest req = new VoiceInteractor.PickOptionRequest(
                    new VoiceInteractor.Prompt("Need to pick something"), options, null) {
                @Override
                public void onCancel() {
                    Log.i(TAG, "Canceled!");
                    mLog.append("Canceled pick\n");
                }

                @Override
                public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
                    Log.i(TAG, "Pick result: finished=" + finished + " selections=" + selections
                            + " result=" + result);
                    StringBuilder sb = new StringBuilder();
                    if (finished) {
                        sb.append("Pick final result: ");
                    } else {
                        sb.append("Pick intermediate result: ");
                    }
                    for (int i=0; i<selections.length; i++) {
                        if (i >= 1) {
                            sb.append(", ");
                        }
                        sb.append(selections[i].getLabel());
                    }
                    mLog.append(sb.toString());
                    if (finished) {
                        getActivity().finish();
                    }
                }
            };
            mInteractor.submitRequest(req, REQUEST_PICK);
        } else if (v == mJumpOutButton) {
            Log.i(TAG, "Jump out");
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setComponent(new ComponentName(this, VoiceInteractionMain.class));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        } else if (v == mCancelButton && mCurrentRequest != null) {
            Log.i(TAG, "Cancel request");
            mCurrentRequest.cancel();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}