Android APIs
public class

VoiceInteractionSession

extends Object
implements KeyEvent.Callback ComponentCallbacks2
java.lang.Object
   ↳ android.service.voice.VoiceInteractionSession

Class Overview

An active voice interaction session, providing a facility for the implementation to interact with the user in the voice interaction layer. The user interface is initially shown by default, and can be created be overriding onCreateContentView() in which the UI can be built.

A voice interaction session can be self-contained, ultimately calling finish() when done. It can also initiate voice interactions with applications by calling startVoiceActivity(Intent)

.

Summary

Nested Classes
class VoiceInteractionSession.AbortVoiceRequest A request to report that the current user interaction can not be completed with voice, as per VoiceInteractor.AbortVoiceRequest
class VoiceInteractionSession.CommandRequest A generic vendor-specific request, as per VoiceInteractor.CommandRequest
class VoiceInteractionSession.CompleteVoiceRequest A request to simply inform the user that the voice operation has completed, as per VoiceInteractor.CompleteVoiceRequest
class VoiceInteractionSession.ConfirmationRequest A request for confirmation from the user of an operation, as per VoiceInteractor.ConfirmationRequest
class VoiceInteractionSession.Insets Information about where interesting parts of the input method UI appear. 
class VoiceInteractionSession.PickOptionRequest A request for the user to pick from a set of option, as per VoiceInteractor.PickOptionRequest
class VoiceInteractionSession.Request Base class representing a request from a voice-driver app to perform a particular voice operation with the user. 
Constants
int SHOW_SOURCE_APPLICATION Flag for use with onShow(Bundle, int): indicates that the application itself has invoked the assistant.
int SHOW_SOURCE_ASSIST_GESTURE Flag for use with onShow(Bundle, int): indicates that the session has been started from the system assist gesture.
int SHOW_WITH_ASSIST Flag received in onShow(Bundle, int): originator requested that the session be started with assist data from the currently focused activity.
int SHOW_WITH_SCREENSHOT Flag received in onShow(Bundle, int): originator requested that the session be started with a screen shot of the currently focused activity.
[Expand]
Inherited Constants
From interface android.content.ComponentCallbacks2
Public Constructors
VoiceInteractionSession(Context context)
VoiceInteractionSession(Context context, Handler handler)
Public Methods
void closeSystemDialogs()
Request that all system dialogs (and status bar shade etc) be closed, allowing access to the session's UI.
void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)
Print the Service's state into the given stream.
void finish()
Finish the session.
Context getContext()
int getDisabledShowContext()
LayoutInflater getLayoutInflater()
Convenience for inflating views.
int getUserDisabledShowContext()
Return which show context flags have been disabled by the user through the system settings UI, so the session will never get this data.
Dialog getWindow()
Retrieve the window being used to show the session's UI.
void hide()
Hide the session's UI, if currently shown.
void onAssistStructureFailure(Throwable failure)
Called when there has been a failure transferring the AssistStructure to the assistant.
void onBackPressed()
Called when the user presses the back button while focus is in the session UI.
void onCancelRequest(VoiceInteractionSession.Request request)
void onCloseSystemDialogs()
Sessions automatically watch for requests that all system UI be closed (such as when the user presses HOME), which will appear here.
void onComputeInsets(VoiceInteractionSession.Insets outInsets)
Compute the interesting insets into your UI.
void onConfigurationChanged(Configuration newConfig)
Called by the system when the device configuration changes while your component is running.
void onCreate()
Initiatize a new session.
View onCreateContentView()
Hook in which to create the session's UI.
void onDestroy()
Last callback to the session as it is being finished.
boolean[] onGetSupportedCommands(String[] commands)
Request to query for what extended commands the session supports.
void onHandleAssist(Bundle data, AssistStructure structure, AssistContent content)
Called to receive data from the application that the user was currently viewing when an assist session is started.
void onHandleScreenshot(Bitmap screenshot)
Called to receive a screenshot of what the user was currently viewing when an assist session is started.
void onHide()
Called immediately after stopping to show the session UI.
boolean onKeyDown(int keyCode, KeyEvent event)
Called when a key down event has occurred.
boolean onKeyLongPress(int keyCode, KeyEvent event)
Called when a long press has occurred.
boolean onKeyMultiple(int keyCode, int count, KeyEvent event)
Called when multiple down/up pairs of the same key have occurred in a row.
boolean onKeyUp(int keyCode, KeyEvent event)
Called when a key up event has occurred.
void onLockscreenShown()
Called when the lockscreen was shown.
void onLowMemory()
This is called when the overall system is running low on memory, and actively running processes should trim their memory usage.
void onRequestAbortVoice(VoiceInteractionSession.AbortVoiceRequest request)
Request to abort the voice interaction session because the voice activity can not complete its interaction using voice.
void onRequestCommand(VoiceInteractionSession.CommandRequest request)
Process an arbitrary extended command from the caller, corresponding to a VoiceInteractor.CommandRequest.
void onRequestCompleteVoice(VoiceInteractionSession.CompleteVoiceRequest request)
Request to complete the voice interaction session because the voice activity successfully completed its interaction using voice.
void onRequestConfirmation(VoiceInteractionSession.ConfirmationRequest request)
Request to confirm with the user before proceeding with an unrecoverable operation, corresponding to a VoiceInteractor.ConfirmationRequest.
void onRequestPickOption(VoiceInteractionSession.PickOptionRequest request)
Request for the user to pick one of N options, corresponding to a VoiceInteractor.PickOptionRequest.
void onShow(Bundle args, int showFlags)
Called when the session UI is going to be shown.
void onTaskFinished(Intent intent, int taskId)
Called when the last activity of a task initiated by startVoiceActivity(android.content.Intent) has finished.
void onTaskStarted(Intent intent, int taskId)
Called when a task initiated by startVoiceActivity(android.content.Intent) has actually started.
void onTrimMemory(int level)
Called when the operating system has determined that it is a good time for a process to trim unneeded memory from its process.
void setContentView(View view)
void setDisabledShowContext(int flags)
void setKeepAwake(boolean keepAwake)
Set whether this session will keep the device awake while it is running a voice activity.
void setTheme(int theme)
You can call this to customize the theme used by your IME's window.
void show(Bundle args, int flags)
Show the UI for this session.
void startVoiceActivity(Intent intent)
Ask that a new activity be started for voice interaction.
[Expand]
Inherited Methods
From class java.lang.Object
From interface android.view.KeyEvent.Callback
From interface android.content.ComponentCallbacks2
From interface android.content.ComponentCallbacks

Constants

public static final int SHOW_SOURCE_APPLICATION

Added in API level 23

Flag for use with onShow(Bundle, int): indicates that the application itself has invoked the assistant.

Constant Value: 8 (0x00000008)

public static final int SHOW_SOURCE_ASSIST_GESTURE

Added in API level 23

Flag for use with onShow(Bundle, int): indicates that the session has been started from the system assist gesture.

Constant Value: 4 (0x00000004)

public static final int SHOW_WITH_ASSIST

Added in API level 23

Flag received in onShow(Bundle, int): originator requested that the session be started with assist data from the currently focused activity.

Constant Value: 1 (0x00000001)

public static final int SHOW_WITH_SCREENSHOT

Added in API level 23

Flag received in onShow(Bundle, int): originator requested that the session be started with a screen shot of the currently focused activity.

Constant Value: 2 (0x00000002)

Public Constructors

public VoiceInteractionSession (Context context)

Added in API level 21

public VoiceInteractionSession (Context context, Handler handler)

Added in API level 21

Public Methods

public void closeSystemDialogs ()

Added in API level 23

Request that all system dialogs (and status bar shade etc) be closed, allowing access to the session's UI. This will not cause the lock screen to be dismissed.

public void dump (String prefix, FileDescriptor fd, PrintWriter writer, String[] args)

Added in API level 23

Print the Service's state into the given stream. This gets invoked by VoiceInteractionSessionService when its Service dump(FileDescriptor, PrintWriter, String[]) method is called.

Parameters
prefix Text to print at the front of each line.
fd The raw file descriptor that the dump is being sent to.
writer The PrintWriter to which you should dump your state. This will be closed for you after you return.
args additional arguments to the dump request.

public void finish ()

Added in API level 21

Finish the session. This completely destroys the session -- the next time it is shown, an entirely new one will be created. You do not normally call this function; instead, use hide() and allow the system to destroy your session if it needs its RAM.

public Context getContext ()

Added in API level 23

public int getDisabledShowContext ()

Added in API level 23

public LayoutInflater getLayoutInflater ()

Added in API level 23

Convenience for inflating views.

public int getUserDisabledShowContext ()

Added in API level 23

Return which show context flags have been disabled by the user through the system settings UI, so the session will never get this data. Returned flags are any combination of VoiceInteractionSession.SHOW_WITH_ASSIST and VoiceInteractionSession.SHOW_WITH_SCREENSHOT. Note that this only tells you about global user settings, not about restrictions that may be applied contextual based on the current application the user is in or other transient states.

public Dialog getWindow ()

Added in API level 23

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

public void hide ()

Added in API level 23

Hide the session's UI, if currently shown. Call this when you are done with your user interaction.

public void onAssistStructureFailure (Throwable failure)

Added in API level 23

Called when there has been a failure transferring the AssistStructure to the assistant. This may happen, for example, if the data is too large and results in an out of memory exception, or the client has provided corrupt data. This will be called immediately before onHandleAssist(Bundle, AssistStructure, AssistContent) and the AssistStructure supplied there afterwards will be null.

Parameters
failure The failure exception that was thrown when building the AssistStructure.

public void onBackPressed ()

Added in API level 23

Called when the user presses the back button while focus is in the session UI. Note that this will only happen if the session UI has requested input focus in its window; otherwise, the back key will go to whatever window has focus and do whatever behavior it normally has there. The default implementation simply calls hide().

public void onCloseSystemDialogs ()

Added in API level 21

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 hide().

public void onComputeInsets (VoiceInteractionSession.Insets outInsets)

Added in API level 23

Compute the interesting insets into your UI. The default implementation sets outInsets.contentInsets.top to the height of the window, meaning it should not adjust content underneath. The default touchable insets are TOUCHABLE_INSETS_FRAME, meaning it consumes all touch events within its window frame.

Parameters
outInsets Fill in with the current UI insets.

public void onConfigurationChanged (Configuration newConfig)

Added in API level 23

Called by the system when the device configuration changes while your component is running. Note that, unlike activities, other components are never restarted when a configuration changes: they must always deal with the results of the change, such as by re-retrieving resources.

At the time that this function has been called, your Resources object will have been updated to return resource values matching the new configuration.

For more information, read Handling Runtime Changes.

Parameters
newConfig The new device configuration.

public void onCreate ()

Added in API level 23

Initiatize a new session. At this point you don't know exactly what this session will be used for; you will find that out in onShow(Bundle, int).

public View onCreateContentView ()

Added in API level 23

Hook in which to create the session's UI.

public void onDestroy ()

Added in API level 21

Last callback to the session as it is being finished.

public boolean[] onGetSupportedCommands (String[] commands)

Added in API level 23

Request to query for what extended commands the session supports.

Parameters
commands An array of commands that are being queried.
Returns
  • 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.

public void onHandleAssist (Bundle data, AssistStructure structure, AssistContent content)

Added in API level 23

Called to receive data from the application that the user was currently viewing when an assist session is started. If the original show request did not specify SHOW_WITH_ASSIST, this method will not be called.

Parameters
data Arbitrary data supplied by the app through Activity.onProvideAssistData. May be null if assist data has been disabled by the user or device policy.
structure If available, the structure definition of all windows currently displayed by the app. May be null if assist data has been disabled by the user or device policy; will be an empty stub if the application has disabled assist by marking its window as secure.
content Additional content data supplied by the app through Activity.onProvideAssistContent. May be null if assist data has been disabled by the user or device policy; will not be automatically filled in with data from the app if the app has marked its window as secure.

public void onHandleScreenshot (Bitmap screenshot)

Added in API level 23

Called to receive a screenshot of what the user was currently viewing when an assist session is started. May be null if screenshots are disabled by the user, policy, or application. If the original show request did not specify SHOW_WITH_SCREENSHOT, this method will not be called.

public void onHide ()

Added in API level 23

Called immediately after stopping to show the session UI.

public boolean onKeyDown (int keyCode, KeyEvent event)

Added in API level 21

Called when a key down event has occurred. If you return true, you can first call KeyEvent.startTracking() to have the framework track the event through its onKeyUp(int, KeyEvent) and also call your onKeyLongPress(int, KeyEvent) if it occurs.

Parameters
keyCode The value in event.getKeyCode().
event Description of the key event.
Returns
  • If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

public boolean onKeyLongPress (int keyCode, KeyEvent event)

Added in API level 21

Called when a long press has occurred. If you return true, the final key up will have FLAG_CANCELED and FLAG_CANCELED_LONG_PRESS set. Note that in order to receive this callback, someone in the event change must return true from onKeyDown(int, KeyEvent) and call startTracking() on the event.

Parameters
keyCode The value in event.getKeyCode().
event Description of the key event.
Returns
  • If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

public boolean onKeyMultiple (int keyCode, int count, KeyEvent event)

Added in API level 21

Called when multiple down/up pairs of the same key have occurred in a row.

Parameters
keyCode The value in event.getKeyCode().
count Number of pairs as returned by event.getRepeatCount().
event Description of the key event.
Returns
  • If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

public boolean onKeyUp (int keyCode, KeyEvent event)

Added in API level 21

Called when a key up event has occurred.

Parameters
keyCode The value in event.getKeyCode().
event Description of the key event.
Returns
  • If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

public void onLockscreenShown ()

Added in API level 23

Called when the lockscreen was shown.

public void onLowMemory ()

Added in API level 23

This is called when the overall system is running low on memory, and actively running processes should trim their memory usage. While the exact point at which this will be called is not defined, generally it will happen when all background process have been killed. That is, before reaching the point of killing processes hosting service and foreground UI that we would like to avoid killing.

You should implement this method to release any caches or other unnecessary resources you may be holding on to. The system will perform a garbage collection for you after returning from this method.

Preferably, you should implement onTrimMemory(int) from ComponentCallbacks2 to incrementally unload your resources based on various levels of memory demands. That API is available for API level 14 and higher, so you should only use this onLowMemory() method as a fallback for older versions, which can be treated the same as onTrimMemory(int) with the TRIM_MEMORY_COMPLETE level.

public void onRequestAbortVoice (VoiceInteractionSession.AbortVoiceRequest request)

Added in API level 23

Request to abort the voice interaction session because the voice activity can not complete its interaction using voice. Corresponds to VoiceInteractor.AbortVoiceRequest. The default implementation just sends an empty confirmation back to allow the activity to exit.

Parameters
request The active request.

public void onRequestCommand (VoiceInteractionSession.CommandRequest request)

Added in API level 23

Process an arbitrary extended command from the caller, corresponding to a VoiceInteractor.CommandRequest.

Parameters
request The active request.

public void onRequestCompleteVoice (VoiceInteractionSession.CompleteVoiceRequest request)

Added in API level 23

Request to complete the voice interaction session because the voice activity successfully completed its interaction using voice. Corresponds to VoiceInteractor.CompleteVoiceRequest. The default implementation just sends an empty confirmation back to allow the activity to exit.

Parameters
request The active request.

public void onRequestConfirmation (VoiceInteractionSession.ConfirmationRequest request)

Added in API level 23

Request to confirm with the user before proceeding with an unrecoverable operation, corresponding to a VoiceInteractor.ConfirmationRequest.

Parameters
request The active request.

public void onRequestPickOption (VoiceInteractionSession.PickOptionRequest request)

Added in API level 23

Request for the user to pick one of N options, corresponding to a VoiceInteractor.PickOptionRequest.

Parameters
request The active request.

public void onShow (Bundle args, int showFlags)

Added in API level 23

Called when the session UI is going to be shown. This is called after onCreateContentView() (if the session's content UI needed to be created) and immediately prior to the window being shown. This may be called while the window is already shown, if a show request has come in while it is shown, to allow you to update the UI to match the new show arguments.

Parameters
args The arguments that were supplied to VoiceInteractionService.showSession.
showFlags The show flags originally provided to VoiceInteractionService.showSession.

public void onTaskFinished (Intent intent, int taskId)

Added in API level 23

Called when the last activity of a task initiated by startVoiceActivity(android.content.Intent) has finished. The default implementation calls 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.

Parameters
intent The original Intent supplied to startVoiceActivity(android.content.Intent).
taskId Unique ID of the finished task.

public void onTaskStarted (Intent intent, int taskId)

Added in API level 23

Called when a task initiated by startVoiceActivity(android.content.Intent) has actually started.

Parameters
intent The original Intent supplied to startVoiceActivity(android.content.Intent).
taskId Unique ID of the now running task.

public void onTrimMemory (int level)

Added in API level 23

Called when the operating system has determined that it is a good time for a process to trim unneeded memory from its process. This will happen for example when it goes in the background and there is not enough memory to keep as many background processes running as desired. You should never compare to exact values of the level, since new intermediate values may be added -- you will typically want to compare if the value is greater or equal to a level you are interested in.

To retrieve the processes current trim level at any point, you can use ActivityManager.getMyMemoryState(RunningAppProcessInfo).

Parameters
level The context of the trim, giving a hint of the amount of trimming the application may like to perform. May be TRIM_MEMORY_COMPLETE, TRIM_MEMORY_MODERATE, TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_UI_HIDDEN, TRIM_MEMORY_RUNNING_CRITICAL, TRIM_MEMORY_RUNNING_LOW, or TRIM_MEMORY_RUNNING_MODERATE.

public void setContentView (View view)

Added in API level 21

public void setDisabledShowContext (int flags)

Added in API level 23

public void setKeepAwake (boolean keepAwake)

Added in API level 23

Set whether this session will keep the device awake while it is running a voice activity. By default, the system holds a wake lock for it while in this state, so that it can work even if the screen is off. Setting this to false removes that wake lock, allowing the CPU to go to sleep. This is typically used if the session decides it has been waiting too long for a response from the user and doesn't want to let this continue to drain the battery.

Passing false here will release the wake lock, and you can call later with true to re-acquire it. It will also be automatically re-acquired for you each time you start a new voice activity task -- that is when you call startVoiceActivity(Intent).

public void setTheme (int theme)

Added in API level 23

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

public void show (Bundle args, int flags)

Added in API level 23

Show the UI for this session. This asks the system to go through the process of showing your UI, which will eventually culminate in onShow(Bundle, int). This is similar to calling VoiceInteractionService.showSession.

Parameters
args Arbitrary arguments that will be propagated onShow(Bundle, int).
flags Indicates additional optional behavior that should be performed. May be any combination of VoiceInteractionSession.SHOW_WITH_ASSIST and VoiceInteractionSession.SHOW_WITH_SCREENSHOT to request that the system generate and deliver assist data on the current foreground app as part of showing the session UI.

public void startVoiceActivity (Intent intent)

Added in API level 23

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 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 VoiceInteractor through which it can perform voice interactions through your session. These requests for voice interactions will appear as callbacks on onGetSupportedCommands(String[]), onRequestConfirmation(VoiceInteractionSession.ConfirmationRequest), onRequestPickOption(VoiceInteractionSession.PickOptionRequest), onRequestCompleteVoice(VoiceInteractionSession.CompleteVoiceRequest), onRequestAbortVoice(VoiceInteractionSession.AbortVoiceRequest), or onRequestCommand(VoiceInteractionSession.CommandRequest)

You will receive a call to onTaskStarted(Intent, int) when the task starts up and onTaskFinished(Intent, int) when the last activity has finished.

Parameters
intent The Intent to start this voice interaction. The given Intent will always have Intent.CATEGORY_VOICE added to it, since this is part of a voice interaction.