Home | History | Annotate | Download | only in voiceinteraction
      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;
     20 import android.content.Intent;
     21 import android.os.Bundle;
     22 import android.service.voice.VoiceInteractionSession;
     23 import android.util.Log;
     24 import android.view.View;
     25 import android.widget.Button;
     26 import android.widget.TextView;
     27 
     28 public class MainInteractionSession extends VoiceInteractionSession
     29         implements View.OnClickListener {
     30     static final String TAG = "MainInteractionSession";
     31 
     32     Intent mStartIntent;
     33     View mContentView;
     34     TextView mText;
     35     Button mStartButton;
     36     Button mConfirmButton;
     37     Button mCompleteButton;
     38     Button mAbortButton;
     39 
     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;
     45     static final int STATE_COMPLETE_VOICE = 5;
     46 
     47     int mState = STATE_IDLE;
     48     Request mPendingRequest;
     49 
     50     MainInteractionSession(Context context) {
     51         super(context);
     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);
     67         mConfirmButton = (Button)mContentView.findViewById(R.id.confirm);
     68         mConfirmButton.setOnClickListener(this);
     69         mCompleteButton = (Button)mContentView.findViewById(R.id.complete);
     70         mCompleteButton.setOnClickListener(this);
     71         mAbortButton = (Button)mContentView.findViewById(R.id.abort);
     72         mAbortButton.setOnClickListener(this);
     73         updateState();
     74         return mContentView;
     75     }
     76 
     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);
     81         mCompleteButton.setEnabled(mState == STATE_COMPLETE_VOICE);
     82     }
     83 
     84     public void onClick(View v) {
     85         if (v == mStartButton) {
     86             mState = STATE_LAUNCHING;
     87             updateState();
     88             startVoiceActivity(mStartIntent);
     89         } else if (v == mConfirmButton) {
     90             if (mState == STATE_CONFIRM) {
     91                 mPendingRequest.sendConfirmResult(true, null);
     92             } else {
     93                 mPendingRequest.sendCommandResult(true, null);
     94             }
     95             mPendingRequest = null;
     96             mState = STATE_IDLE;
     97             updateState();
     98         } else if (v == mAbortButton) {
     99             mPendingRequest.sendAbortVoiceResult(null);
    100             mPendingRequest = null;
    101             mState = STATE_IDLE;
    102             updateState();
    103         } else if (v== mCompleteButton) {
    104             mPendingRequest.sendCompleteVoiceResult(null);
    105             mPendingRequest = null;
    106             mState = STATE_IDLE;
    107             updateState();
    108         }
    109     }
    110 
    111     @Override
    112     public boolean[] onGetSupportedCommands(Caller caller, String[] commands) {
    113         return new boolean[commands.length];
    114     }
    115 
    116     @Override
    117     public void onConfirm(Caller caller, Request request, CharSequence prompt, Bundle extras) {
    118         Log.i(TAG, "onConfirm: prompt=" + prompt + " extras=" + extras);
    119         mText.setText(prompt);
    120         mStartButton.setText("Confirm");
    121         mPendingRequest = request;
    122         mState = STATE_CONFIRM;
    123         updateState();
    124     }
    125 
    126     @Override
    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
    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();
    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);
    147         mText.setText("Command: " + command);
    148         mStartButton.setText("Finish Command");
    149         mPendingRequest = request;
    150         mState = STATE_COMMAND;
    151         updateState();
    152     }
    153 
    154     @Override
    155     public void onCancel(Request request) {
    156         Log.i(TAG, "onCancel");
    157         request.sendCancelResult();
    158     }
    159 }
    160