TestInteractionActivity.java revision a3acdb33df7c7be7ff3d9f376ff833e4b0c1d897
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
17package com.android.test.voiceinteraction;
18
19import android.app.Activity;
20import android.app.VoiceInteractor;
21import android.content.ComponentName;
22import android.content.Intent;
23import android.os.Bundle;
24import android.service.voice.VoiceInteractionService;
25import android.util.Log;
26import android.view.View;
27import android.widget.Button;
28import android.widget.TextView;
29
30public class TestInteractionActivity extends Activity implements View.OnClickListener {
31    static final String TAG = "TestInteractionActivity";
32
33    static final String REQUEST_ABORT = "abort";
34    static final String REQUEST_COMPLETE = "complete";
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 mAbortButton;
42    Button mCompleteButton;
43    Button mPickButton;
44    Button mJumpOutButton;
45    Button mCancelButton;
46
47    @Override
48    public void onCreate(Bundle savedInstanceState) {
49        super.onCreate(savedInstanceState);
50
51        if (!isVoiceInteraction()) {
52            Log.w(TAG, "Not running as a voice interaction!");
53            finish();
54            return;
55        }
56
57        if (!VoiceInteractionService.isActiveService(this,
58                new ComponentName(this, MainInteractionService.class))) {
59            Log.w(TAG, "Not current voice interactor!");
60            finish();
61            return;
62        }
63
64        setContentView(R.layout.test_interaction);
65        mLog = (TextView)findViewById(R.id.log);
66        mAbortButton = (Button)findViewById(R.id.abort);
67        mAbortButton.setOnClickListener(this);
68        mCompleteButton = (Button)findViewById(R.id.complete);
69        mCompleteButton.setOnClickListener(this);
70        mPickButton = (Button)findViewById(R.id.pick);
71        mPickButton.setOnClickListener(this);
72        mJumpOutButton = (Button)findViewById(R.id.jump);
73        mJumpOutButton.setOnClickListener(this);
74        mCancelButton = (Button)findViewById(R.id.cancel);
75        mCancelButton.setOnClickListener(this);
76
77        mInteractor = getVoiceInteractor();
78
79        VoiceInteractor.Request[] active = mInteractor.getActiveRequests();
80        for (int i=0; i<active.length; i++) {
81            Log.i(TAG, "Active #" + i + " / " + active[i].getName() + ": " + active[i]);
82        }
83
84        mCurrentRequest = mInteractor.getActiveRequest(REQUEST_CONFIRM);
85        if (mCurrentRequest == null) {
86            mCurrentRequest = new VoiceInteractor.ConfirmationRequest(
87                    new VoiceInteractor.Prompt("This is a confirmation"), null) {
88                @Override
89                public void onCancel() {
90                    Log.i(TAG, "Canceled!");
91                    getActivity().finish();
92                }
93
94                @Override
95                public void onConfirmationResult(boolean confirmed, Bundle result) {
96                    Log.i(TAG, "Confirmation result: confirmed=" + confirmed + " result=" + result);
97                    getActivity().finish();
98                }
99            };
100            mInteractor.submitRequest(mCurrentRequest, REQUEST_CONFIRM);
101        } else {
102            Log.i(TAG, "Restarting with active confirmation: " + mCurrentRequest);
103        }
104    }
105
106    @Override
107    public void onResume() {
108        super.onResume();
109    }
110
111    @Override
112    public void onClick(View v) {
113        if (v == mAbortButton) {
114            VoiceInteractor.AbortVoiceRequest req = new VoiceInteractor.AbortVoiceRequest(
115                    new VoiceInteractor.Prompt("Dammit, we suck :("), null) {
116                @Override
117                public void onCancel() {
118                    Log.i(TAG, "Canceled!");
119                    mLog.append("Canceled abort\n");
120                }
121
122                @Override
123                public void onAbortResult(Bundle result) {
124                    Log.i(TAG, "Abort result: result=" + result);
125                    mLog.append("Abort: result=" + result + "\n");
126                    getActivity().finish();
127                }
128            };
129            mInteractor.submitRequest(req, REQUEST_ABORT);
130        } else if (v == mCompleteButton) {
131            VoiceInteractor.CompleteVoiceRequest req = new VoiceInteractor.CompleteVoiceRequest(
132                    new VoiceInteractor.Prompt("Woohoo, completed!"), null) {
133                @Override
134                public void onCancel() {
135                    Log.i(TAG, "Canceled!");
136                    mLog.append("Canceled complete\n");
137                }
138
139                @Override
140                public void onCompleteResult(Bundle result) {
141                    Log.i(TAG, "Complete result: result=" + result);
142                    mLog.append("Complete: result=" + result + "\n");
143                    getActivity().finish();
144                }
145            };
146            mInteractor.submitRequest(req, REQUEST_COMPLETE);
147        } else if (v == mPickButton) {
148            VoiceInteractor.PickOptionRequest.Option[] options =
149                    new VoiceInteractor.PickOptionRequest.Option[5];
150            options[0] = new VoiceInteractor.PickOptionRequest.Option("One");
151            options[1] = new VoiceInteractor.PickOptionRequest.Option("Two");
152            options[2] = new VoiceInteractor.PickOptionRequest.Option("Three");
153            options[3] = new VoiceInteractor.PickOptionRequest.Option("Four");
154            options[4] = new VoiceInteractor.PickOptionRequest.Option("Five");
155            VoiceInteractor.PickOptionRequest req = new VoiceInteractor.PickOptionRequest(
156                    new VoiceInteractor.Prompt("Need to pick something"), options, null) {
157                @Override
158                public void onCancel() {
159                    Log.i(TAG, "Canceled!");
160                    mLog.append("Canceled pick\n");
161                }
162
163                @Override
164                public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
165                    Log.i(TAG, "Pick result: finished=" + finished + " selections=" + selections
166                            + " result=" + result);
167                    StringBuilder sb = new StringBuilder();
168                    if (finished) {
169                        sb.append("Pick final result: ");
170                    } else {
171                        sb.append("Pick intermediate result: ");
172                    }
173                    for (int i=0; i<selections.length; i++) {
174                        if (i >= 1) {
175                            sb.append(", ");
176                        }
177                        sb.append(selections[i].getLabel());
178                    }
179                    mLog.append(sb.toString());
180                    if (finished) {
181                        getActivity().finish();
182                    }
183                }
184            };
185            mInteractor.submitRequest(req, REQUEST_PICK);
186        } else if (v == mJumpOutButton) {
187            Log.i(TAG, "Jump out");
188            Intent intent = new Intent(Intent.ACTION_MAIN);
189            intent.addCategory(Intent.CATEGORY_LAUNCHER);
190            intent.setComponent(new ComponentName(this, VoiceInteractionMain.class));
191            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
192            startActivity(intent);
193        } else if (v == mCancelButton && mCurrentRequest != null) {
194            Log.i(TAG, "Cancel request");
195            mCurrentRequest.cancel();
196        }
197    }
198
199    @Override
200    public void onDestroy() {
201        super.onDestroy();
202    }
203}
204