FileDocCategorySizeDatePackage
VoiceInteractor.javaAPI DocAndroid 5.1 API18476Thu Mar 12 22:22:10 GMT 2015android.app

VoiceInteractor

public class VoiceInteractor extends Object
hide
Interface for an {@link Activity} to interact with the user through voice. Use {@link android.app.Activity#getVoiceInteractor() Activity.getVoiceInteractor} to retrieve the interface, if the activity is currently involved in a voice interaction.

The voice interactor revolves around submitting voice interaction requests to the back-end voice interaction service that is working with the user. These requests are submitted with {@link #submitRequest}, providing a new instance of a {@link Request} subclass describing the type of operation to perform -- currently the possible requests are {@link ConfirmationRequest} and {@link CommandRequest}.

Once a request is submitted, the voice system will process it and eventually deliver the result to the request object. The application can cancel a pending request at any time.

The VoiceInteractor is integrated with Activity's state saving mechanism, so that if an activity is being restarted with retained state, it will retain the current VoiceInteractor and any outstanding requests. Because of this, you should always use {@link Request#getActivity() Request.getActivity} to get back to the activity of a request, rather than holding on to the activity instance yourself, either explicitly or implicitly through a non-static inner class.

Fields Summary
static final String
TAG
static final boolean
DEBUG
final com.android.internal.app.IVoiceInteractor
mInteractor
android.content.Context
mContext
Activity
mActivity
final com.android.internal.os.HandlerCaller
mHandlerCaller
final HandlerCaller.Callback
mHandlerCallerCallback
final IVoiceInteractorCallback.Stub
mCallback
final android.util.ArrayMap
mActiveRequests
static final int
MSG_CONFIRMATION_RESULT
static final int
MSG_COMPLETE_VOICE_RESULT
static final int
MSG_ABORT_VOICE_RESULT
static final int
MSG_COMMAND_RESULT
static final int
MSG_CANCEL_RESULT
Constructors Summary
VoiceInteractor(com.android.internal.app.IVoiceInteractor interactor, android.content.Context context, Activity activity, android.os.Looper looper)

        mInteractor = interactor;
        mContext = context;
        mActivity = activity;
        mHandlerCaller = new HandlerCaller(context, looper, mHandlerCallerCallback, true);
    
Methods Summary
voidattachActivity(Activity activity)

        if (mActivity == activity) {
            return;
        }
        mContext = activity;
        mActivity = activity;
        ArrayList<Request> reqs = makeRequestList();
        if (reqs != null) {
            for (int i=0; i<reqs.size(); i++) {
                Request req = reqs.get(i);
                req.mContext = activity;
                req.mActivity = activity;
                req.onAttached(activity);
            }
        }
    
voiddetachActivity()

        ArrayList<Request> reqs = makeRequestList();
        if (reqs != null) {
            for (int i=0; i<reqs.size(); i++) {
                Request req = reqs.get(i);
                req.onDetached();
                req.mActivity = null;
                req.mContext = null;
            }
        }
        mContext = null;
        mActivity = null;
    
private java.util.ArrayListmakeRequestList()

        final int N = mActiveRequests.size();
        if (N < 1) {
            return null;
        }
        ArrayList<Request> list = new ArrayList<Request>(N);
        for (int i=0; i<N; i++) {
            list.add(mActiveRequests.valueAt(i));
        }
        return list;
    
android.app.VoiceInteractor$RequestpullRequest(com.android.internal.app.IVoiceInteractorRequest request, boolean complete)

        synchronized (mActiveRequests) {
            Request req = mActiveRequests.get(request.asBinder());
            if (req != null && complete) {
                mActiveRequests.remove(request.asBinder());
            }
            return req;
        }
    
public booleansubmitRequest(android.app.VoiceInteractor$Request request)

        try {
            IVoiceInteractorRequest ireq = request.submit(mInteractor,
                    mContext.getOpPackageName(), mCallback);
            request.mRequestInterface = ireq;
            request.mContext = mContext;
            request.mActivity = mActivity;
            synchronized (mActiveRequests) {
                mActiveRequests.put(ireq.asBinder(), request);
            }
            return true;
        } catch (RemoteException e) {
            Log.w(TAG, "Remove voice interactor service died", e);
            return false;
        }
    
public boolean[]supportsCommands(java.lang.String[] commands)
Queries the supported commands available from the VoiceinteractionService. The command is a string that describes the generic operation to be performed. An example might be "com.google.voice.commands.REQUEST_NUMBER_BAGS" to request the number of bags as part of airline check-in. (This is not an actual working example.)

param
commands

        try {
            boolean[] res = mInteractor.supportsCommands(mContext.getOpPackageName(), commands);
            if (DEBUG) Log.d(TAG, "supportsCommands: cmds=" + commands + " res=" + res);
            return res;
        } catch (RemoteException e) {
            throw new RuntimeException("Voice interactor has died", e);
        }