FileDocCategorySizeDatePackage
VoiceInteractionSession.javaAPI DocAndroid 5.1 API31607Thu Mar 12 22:22:10 GMT 2015android.service.voice

VoiceInteractionSession

public abstract class VoiceInteractionSession extends Object implements KeyEvent.Callback
An active interaction session, started by a {@link VoiceInteractionService}.

Fields Summary
static final String
TAG
static final boolean
DEBUG
final android.content.Context
mContext
final com.android.internal.os.HandlerCaller
mHandlerCaller
final KeyEvent.DispatcherState
mDispatcherState
com.android.internal.app.IVoiceInteractionManagerService
mSystemService
android.os.IBinder
mToken
int
mTheme
android.view.LayoutInflater
mInflater
android.content.res.TypedArray
mThemeAttrs
android.view.View
mRootView
android.widget.FrameLayout
mContentFrame
android.inputmethodservice.SoftInputWindow
mWindow
boolean
mInitialized
boolean
mWindowAdded
boolean
mWindowVisible
boolean
mWindowWasVisible
boolean
mInShowWindow
final android.util.ArrayMap
mActiveRequests
final Insets
mTmpInsets
final int[]
mTmpLocation
final WeakReference
mWeakRef
final com.android.internal.app.IVoiceInteractor
mInteractor
final IVoiceInteractionSession
mSession
static final int
MSG_START_CONFIRMATION
static final int
MSG_START_COMPLETE_VOICE
static final int
MSG_START_ABORT_VOICE
static final int
MSG_START_COMMAND
static final int
MSG_SUPPORTS_COMMANDS
static final int
MSG_CANCEL
static final int
MSG_TASK_STARTED
static final int
MSG_TASK_FINISHED
static final int
MSG_CLOSE_SYSTEM_DIALOGS
static final int
MSG_DESTROY
final MyCallbacks
mCallbacks
final ViewTreeObserver.OnComputeInternalInsetsListener
mInsetsComputer
Constructors Summary
public VoiceInteractionSession(android.content.Context context)


       
        this(context, new Handler());
    
public VoiceInteractionSession(android.content.Context context, android.os.Handler handler)

        mContext = context;
        mHandlerCaller = new HandlerCaller(context, handler.getLooper(),
                mCallbacks, true);
    
Methods Summary
voiddoCreate(com.android.internal.app.IVoiceInteractionManagerService service, android.os.IBinder token, android.os.Bundle args)

        mSystemService = service;
        mToken = token;
        onCreate(args);
    
voiddoDestroy()

        onDestroy();
        if (mInitialized) {
            mRootView.getViewTreeObserver().removeOnComputeInternalInsetsListener(
                    mInsetsComputer);
            if (mWindowAdded) {
                mWindow.dismiss();
                mWindowAdded = false;
            }
            mInitialized = false;
        }
    
public voidfinish()
Finish the session.

        if (mToken == null) {
            throw new IllegalStateException("Can't call before onCreate()");
        }
        hideWindow();
        try {
            mSystemService.finish(mToken);
        } catch (RemoteException e) {
        }
    
public android.view.LayoutInflatergetLayoutInflater()

hide
Convenience for inflating views.

        return mInflater;
    
public android.app.DialoggetWindow()

hide
Retrieve the window being used to show the session's UI.

        return mWindow;
    
public voidhideWindow()

hide

        if (mWindowVisible) {
            mWindow.hide();
            mWindowVisible = false;
        }
    
voidinitViews()

        mInitialized = true;

        mThemeAttrs = mContext.obtainStyledAttributes(android.R.styleable.VoiceInteractionSession);
        mRootView = mInflater.inflate(
                com.android.internal.R.layout.voice_interaction_session, null);
        mRootView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
        mWindow.setContentView(mRootView);
        mRootView.getViewTreeObserver().addOnComputeInternalInsetsListener(mInsetsComputer);

        mContentFrame = (FrameLayout)mRootView.findViewById(android.R.id.content);
    
android.service.voice.VoiceInteractionSession$RequestnewRequest(com.android.internal.app.IVoiceInteractorCallback callback)

        synchronized (this) {
            Request req = new Request(callback, this);
            mActiveRequests.put(req.mInterface.asBinder(), req);
            return req;
        }
    
public voidonAbortVoice(android.service.voice.VoiceInteractionSession$Caller caller, android.service.voice.VoiceInteractionSession$Request request, java.lang.CharSequence message, android.os.Bundle extras)

hide
SystemApi
Request to abort the voice interaction session because the voice activity can not complete its interaction using voice. Corresponds to {@link android.app.VoiceInteractor.AbortVoiceRequest VoiceInteractor.AbortVoiceRequest}. The default implementation just sends an empty confirmation back to allow the activity to exit.
param
caller Who is making the request.
param
request The active request.
param
message The message informing the user of the problem, as per {@link android.app.VoiceInteractor.AbortVoiceRequest VoiceInteractor.AbortVoiceRequest}.
param
extras Any additional information, as per {@link android.app.VoiceInteractor.AbortVoiceRequest VoiceInteractor.AbortVoiceRequest}.

        request.sendAbortVoiceResult(null);
    
public voidonBackPressed()

hide

        finish();
    
public abstract voidonCancel(android.service.voice.VoiceInteractionSession$Request request)

hide
SystemApi
Called when the {@link android.app.VoiceInteractor} has asked to cancel a {@link Request} that was previously delivered to {@link #onConfirm} or {@link #onCommand}.
param
request The request that is being canceled.

public voidonCloseSystemDialogs()
Sessions automatically watch for requests that all system UI be closed (such as when the user presses HOME), which will appear here. The default implementation always calls {@link #finish}.

        finish();
    
public abstract voidonCommand(android.service.voice.VoiceInteractionSession$Caller caller, android.service.voice.VoiceInteractionSession$Request request, java.lang.String command, android.os.Bundle extras)

hide
SystemApi
Process an arbitrary extended command from the caller, corresponding to a {@link android.app.VoiceInteractor.CommandRequest VoiceInteractor.CommandRequest}.
param
caller Who is making the request.
param
request The active request.
param
command The command that is being executed, as per {@link android.app.VoiceInteractor.CommandRequest VoiceInteractor.CommandRequest}.
param
extras Any additional information, as per {@link android.app.VoiceInteractor.CommandRequest VoiceInteractor.CommandRequest}.

public voidonCompleteVoice(android.service.voice.VoiceInteractionSession$Caller caller, android.service.voice.VoiceInteractionSession$Request request, java.lang.CharSequence message, android.os.Bundle extras)

hide
SystemApi
Request to complete the voice interaction session because the voice activity successfully completed its interaction using voice. Corresponds to {@link android.app.VoiceInteractor.CompleteVoiceRequest VoiceInteractor.CompleteVoiceRequest}. The default implementation just sends an empty confirmation back to allow the activity to exit.
param
caller Who is making the request.
param
request The active request.
param
message The message informing the user of the problem, as per {@link android.app.VoiceInteractor.CompleteVoiceRequest VoiceInteractor.CompleteVoiceRequest}.
param
extras Any additional information, as per {@link android.app.VoiceInteractor.CompleteVoiceRequest VoiceInteractor.CompleteVoiceRequest}.

        request.sendCompleteVoiceResult(null);
    
public voidonComputeInsets(android.service.voice.VoiceInteractionSession$Insets outInsets)

hide
Compute the interesting insets into your UI. The default implementation uses the entire window frame as the insets. The default touchable insets are {@link Insets#TOUCHABLE_INSETS_FRAME}.
param
outInsets Fill in with the current UI insets.

        int[] loc = mTmpLocation;
        View decor = getWindow().getWindow().getDecorView();
        decor.getLocationInWindow(loc);
        outInsets.contentInsets.top = 0;
        outInsets.contentInsets.left = 0;
        outInsets.contentInsets.right = 0;
        outInsets.contentInsets.bottom = 0;
        outInsets.touchableInsets = Insets.TOUCHABLE_INSETS_FRAME;
        outInsets.touchableRegion.setEmpty();
    
public abstract voidonConfirm(android.service.voice.VoiceInteractionSession$Caller caller, android.service.voice.VoiceInteractionSession$Request request, java.lang.CharSequence prompt, android.os.Bundle extras)

hide
SystemApi
Request to confirm with the user before proceeding with an unrecoverable operation, corresponding to a {@link android.app.VoiceInteractor.ConfirmationRequest VoiceInteractor.ConfirmationRequest}.
param
caller Who is making the request.
param
request The active request.
param
prompt The prompt informing the user of what will happen, as per {@link android.app.VoiceInteractor.ConfirmationRequest VoiceInteractor.ConfirmationRequest}.
param
extras Any additional information, as per {@link android.app.VoiceInteractor.ConfirmationRequest VoiceInteractor.ConfirmationRequest}.

public voidonCreate(android.os.Bundle args)
Initiatize a new session.

param
args The arguments that were supplied to {@link VoiceInteractionService#startSession VoiceInteractionService.startSession}.

        mTheme = mTheme != 0 ? mTheme
                : com.android.internal.R.style.Theme_DeviceDefault_VoiceInteractionSession;
        mInflater = (LayoutInflater)mContext.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        mWindow = new SoftInputWindow(mContext, "VoiceInteractionSession", mTheme,
                mCallbacks, this, mDispatcherState,
                WindowManager.LayoutParams.TYPE_VOICE_INTERACTION, Gravity.TOP, true);
        mWindow.getWindow().addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        initViews();
        mWindow.getWindow().setLayout(MATCH_PARENT, WRAP_CONTENT);
        mWindow.setToken(mToken);
    
public android.view.ViewonCreateContentView()

hide
Hook in which to create the session's UI.

        return null;
    
public voidonDestroy()
Last callback to the session as it is being finished.

    
public boolean[]onGetSupportedCommands(android.service.voice.VoiceInteractionSession$Caller caller, java.lang.String[] commands)

hide
SystemApi
Request to query for what extended commands the session supports.
param
caller Who is making the request.
param
commands An array of commands that are being queried.
return
Return an array of booleans indicating which of each entry in the command array is supported. A true entry in the array indicates the command is supported; false indicates it is not. The default implementation returns an array of all false entries.

        return new boolean[commands.length];
    
public booleanonKeyDown(int keyCode, android.view.KeyEvent event)

hide

        return false;
    
public booleanonKeyLongPress(int keyCode, android.view.KeyEvent event)

hide

        return false;
    
public booleanonKeyMultiple(int keyCode, int count, android.view.KeyEvent event)

hide

        return false;
    
public booleanonKeyUp(int keyCode, android.view.KeyEvent event)

hide

        return false;
    
public voidonTaskFinished(android.content.Intent intent, int taskId)

hide
SystemApi
Called when the last activity of a task initiated by {@link #startVoiceActivity(android.content.Intent)} has finished. The default implementation calls {@link #finish()} on the assumption that this represents the completion of a voice action. You can override the implementation if you would like a different behavior.
param
intent The original {@link Intent} supplied to {@link #startVoiceActivity(android.content.Intent)}.
param
taskId Unique ID of the finished task.

        finish();
    
public voidonTaskStarted(android.content.Intent intent, int taskId)

hide
SystemApi
Called when a task initiated by {@link #startVoiceActivity(android.content.Intent)} has actually started.
param
intent The original {@link Intent} supplied to {@link #startVoiceActivity(android.content.Intent)}.
param
taskId Unique ID of the now running task.

    
android.service.voice.VoiceInteractionSession$RequestremoveRequest(android.os.IBinder reqInterface)

        synchronized (this) {
            Request req = mActiveRequests.get(reqInterface);
            if (req != null) {
                mActiveRequests.remove(req);
            }
            return req;
        }
    
public voidsetContentView(android.view.View view)

        mContentFrame.removeAllViews();
        mContentFrame.addView(view, new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));

    
public voidsetTheme(int theme)

hide
You can call this to customize the theme used by your IME's window. This must be set before {@link #onCreate}, so you will typically call it in your constructor with the resource ID of your custom theme.

        if (mWindow != null) {
            throw new IllegalStateException("Must be called before onCreate()");
        }
        mTheme = theme;
    
public voidshowWindow()

hide

        if (DEBUG) Log.v(TAG, "Showing window: mWindowAdded=" + mWindowAdded
                + " mWindowVisible=" + mWindowVisible);

        if (mInShowWindow) {
            Log.w(TAG, "Re-entrance in to showWindow");
            return;
        }

        try {
            mInShowWindow = true;
            if (!mWindowVisible) {
                mWindowVisible = true;
                if (!mWindowAdded) {
                    mWindowAdded = true;
                    View v = onCreateContentView();
                    if (v != null) {
                        setContentView(v);
                    }
                }
                mWindow.show();
            }
        } finally {
            mWindowWasVisible = true;
            mInShowWindow = false;
        }
    
public voidstartVoiceActivity(android.content.Intent intent)

hide
Ask that a new activity be started for voice interaction. This will create a new dedicated task in the activity manager for this voice interaction session; this means that {@link Intent#FLAG_ACTIVITY_NEW_TASK Intent.FLAG_ACTIVITY_NEW_TASK} will be set for you to make it a new task.

The newly started activity will be displayed to the user in a special way, as a layer under the voice interaction UI.

As the voice activity runs, it can retrieve a {@link android.app.VoiceInteractor} through which it can perform voice interactions through your session. These requests for voice interactions will appear as callbacks on {@link #onGetSupportedCommands}, {@link #onConfirm}, {@link #onCommand}, and {@link #onCancel}.

You will receive a call to {@link #onTaskStarted} when the task starts up and {@link #onTaskFinished} when the last activity has finished.

param
intent The Intent to start this voice interaction. The given Intent will always have {@link Intent#CATEGORY_VOICE Intent.CATEGORY_VOICE} added to it, since this is part of a voice interaction.

        if (mToken == null) {
            throw new IllegalStateException("Can't call before onCreate()");
        }
        try {
            intent.migrateExtraStreamToClipData();
            intent.prepareToLeaveProcess();
            int res = mSystemService.startVoiceActivity(mToken, intent,
                    intent.resolveType(mContext.getContentResolver()));
            Instrumentation.checkStartActivityResult(res, intent);
        } catch (RemoteException e) {
        }