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.os.Bundle;
22import android.util.Log;
23import android.view.Gravity;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.Button;
27
28public class TestInteractionActivity extends Activity implements View.OnClickListener {
29    static final String TAG = "TestInteractionActivity";
30
31    VoiceInteractor mInteractor;
32    Button mAbortButton;
33    Button mCompleteButton;
34
35    @Override
36    public void onCreate(Bundle savedInstanceState) {
37        super.onCreate(savedInstanceState);
38
39        if (!isVoiceInteraction()) {
40            Log.w(TAG, "Not running as a voice interaction!");
41            finish();
42            return;
43        }
44
45        setContentView(R.layout.test_interaction);
46        mAbortButton = (Button)findViewById(R.id.abort);
47        mAbortButton.setOnClickListener(this);
48        mCompleteButton = (Button)findViewById(R.id.complete);
49        mCompleteButton.setOnClickListener(this);
50
51        // Framework should take care of these.
52        getWindow().setGravity(Gravity.TOP);
53        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
54                ViewGroup.LayoutParams.WRAP_CONTENT);
55
56        mInteractor = getVoiceInteractor();
57        VoiceInteractor.ConfirmationRequest req = new VoiceInteractor.ConfirmationRequest(
58                "This is a confirmation", null) {
59            @Override
60            public void onCancel() {
61                Log.i(TAG, "Canceled!");
62                getActivity().finish();
63            }
64
65            @Override
66            public void onConfirmationResult(boolean confirmed, Bundle result) {
67                Log.i(TAG, "Confirmation result: confirmed=" + confirmed + " result=" + result);
68                getActivity().finish();
69            }
70        };
71        mInteractor.submitRequest(req);
72    }
73
74    @Override
75    public void onResume() {
76        super.onResume();
77    }
78
79    @Override
80    public void onClick(View v) {
81        if (v == mAbortButton) {
82            VoiceInteractor.AbortVoiceRequest req = new VoiceInteractor.AbortVoiceRequest(
83                    "Dammit, we suck :(", null) {
84                @Override
85                public void onCancel() {
86                    Log.i(TAG, "Canceled!");
87                }
88
89                @Override
90                public void onAbortResult(Bundle result) {
91                    Log.i(TAG, "Abort result: result=" + result);
92                    getActivity().finish();
93                }
94            };
95            mInteractor.submitRequest(req);
96        } else if (v == mCompleteButton) {
97            VoiceInteractor.CompleteVoiceRequest req = new VoiceInteractor.CompleteVoiceRequest(
98                    "Woohoo, completed!", null) {
99                @Override
100                public void onCancel() {
101                    Log.i(TAG, "Canceled!");
102                }
103
104                @Override
105                public void onCompleteResult(Bundle result) {
106                    Log.i(TAG, "Complete result: result=" + result);
107                    getActivity().finish();
108                }
109            };
110            mInteractor.submitRequest(req);
111        }
112    }
113
114    @Override
115    public void onDestroy() {
116        super.onDestroy();
117    }
118}
119