Added in API level 21

HardwareSession


abstract class HardwareSession : TvInputService.Session

Base class for a TV input session which represents an external device connected to a hardware TV input.

This class is for an input which provides channels for the external set-top box to the application. Once a TV input returns an implementation of this class on onCreateSession(java.lang.String), the framework will create a separate session for a hardware TV Input (e.g. HDMI 1) and forward the application's surface to the session so that the user can see the screen of the hardware TV Input when she tunes to a channel from this TV input. The implementation of this class is expected to change the channel of the external set-top box via a proprietary protocol when android.media.tv.TvInputService.HardwareSession#onTune is requested by the application.

Note that this class is not for inputs for internal hardware like built-in tuner and HDMI 1.

Summary

Public constructors

Creates a new HardwareSession.

Public methods
abstract String!

Returns the hardware TV input ID the external device is connected to.

open Unit

Called when the underlying hardware TV input session calls TvInputService.Session.notifyVideoAvailable().

open Unit

Called when the underlying hardware TV input session calls TvInputService.Session.notifyVideoUnavailable(int).

Boolean
onSetSurface(surface: Surface?)

This method will not be called in HardwareSession.

Inherited functions
Unit layoutSurface(left: Int, top: Int, right: Int, bottom: Int)

Assigns a size and position to the surface passed in onSetSurface. The position is relative to the overlay view that sits on top of this surface.

Unit notifyAdBufferConsumed(buffer: AdBuffer)

Notifies the advertisement buffer is consumed.

Unit notifyAdResponse(response: AdResponse)

Notifies response for advertisement.

Unit notifyAitInfoUpdated(aitInfo: AitInfo)

Informs the app that the AIT (Application Information Table) is updated.

This method should also be called when onSetInteractiveAppNotificationEnabled(boolean) is called to send the first AIT info.

Unit notifyAudioPresentationChanged(audioPresentations: MutableList<AudioPresentation!>)

Sends an updated list of all audio presentations available from a Next Generation Audio service. This is used by the framework to maintain the audio presentation information for a given track of TvTrackInfo.TYPE_AUDIO, which in turn is used by TvView.getAudioPresentations for the application to retrieve metadata for the current audio track. The TV input service must call this method as soon as the audio track presentation information becomes available or is updated. Note that in a case where a part of the information for the current track is updated, it is not necessary to create a new TvTrackInfo object with a different track ID.

Unit notifyAudioPresentationSelected(presentationId: Int, programId: Int)

Sends the presentation and program IDs of the selected audio presentation. This is used to inform the application that a specific audio presentation is selected. The TV input service must call this method as soon as an audio presentation is selected either by default or in response to a call to onSelectTrack. The selected audio presentation ID for a currently selected audio track is maintained in the framework until the next call to this method even after the entire audio presentation list for the track is updated (but is reset when the session is tuned to a new channel), so care must be taken not to result in an obsolete track audio presentation ID.

Unit notifyAvailableSpeeds(speeds: FloatArray)

Informs the app available speeds for time-shifting.

This should be called when time-shifting is enabled.

Unit notifyBroadcastInfoResponse(response: BroadcastInfoResponse)

Notifies response for broadcast info.

Unit notifyChannelRetuned(channelUri: Uri!)

Informs the application that the current channel is re-tuned for some reason and the session now displays the content from a new channel. This is used to handle special cases such as when the current channel becomes unavailable, it is necessary to send the user to a certain channel or the user changes channel in some other way (e.g. by using a dedicated remote).

Unit notifyContentAllowed()

Informs the application that the user is allowed to watch the current program content.

Each TV input service is required to query the system whether the user is allowed to watch the current program before showing it to the user if the parental controls is enabled (i.e. TvInputManager.isParentalControlsEnabled() returns true). Whether the TV input service should block the content or not is determined by invoking TvInputManager.isRatingBlocked(TvContentRating) with the content rating for the current program. Then the TvInputManager makes a judgment based on the user blocked ratings stored in the secure settings and returns the result. If the rating in question turns out to be allowed by the user, the TV input service must call this method to notify the application that is permitted to show the content.

Each TV input service also needs to continuously listen to any changes made to the parental controls settings by registering a broadcast receiver to receive TvInputManager.ACTION_BLOCKED_RATINGS_CHANGED and TvInputManager.ACTION_PARENTAL_CONTROLS_ENABLED_CHANGED and immediately reevaluate the current program with the new parental controls settings.

Unit notifyContentBlocked(rating: TvContentRating)

Informs the application that the current program content is blocked by parent controls.

Each TV input service is required to query the system whether the user is allowed to watch the current program before showing it to the user if the parental controls is enabled (i.e. TvInputManager.isParentalControlsEnabled() returns true). Whether the TV input service should block the content or not is determined by invoking TvInputManager.isRatingBlocked(TvContentRating) with the content rating for the current program or TvContentRating.UNRATED in case the rating information is missing. Then the TvInputManager makes a judgment based on the user blocked ratings stored in the secure settings and returns the result. If the rating in question turns out to be blocked, the TV input service must immediately block the content and call this method with the content rating of the current program to prompt the PIN verification screen.

Each TV input service also needs to continuously listen to any changes made to the parental controls settings by registering a broadcast receiver to receive TvInputManager.ACTION_BLOCKED_RATINGS_CHANGED and TvInputManager.ACTION_PARENTAL_CONTROLS_ENABLED_CHANGED and immediately reevaluate the current program with the new parental controls settings.

Unit notifyCueingMessageAvailability(available: Boolean)

Informs the application that cueing message is available or unavailable.

The cueing message is used for digital program insertion, based on the standard ANSI/SCTE 35 2019r1.

Unit notifySignalStrength(strength: Int)

Notifies signal strength.

Unit notifyTimeShiftMode(mode: Int)

Informs the app that the time shift mode is set or updated.

Unit notifyTimeShiftStatusChanged(status: Int)

Informs the application that the time shift status is changed.

Prior to calling this method, the application assumes the status TvInputManager.TIME_SHIFT_STATUS_UNKNOWN. Right after the session is created, it is important to invoke the method with the status TvInputManager.TIME_SHIFT_STATUS_AVAILABLE if the implementation does support time shifting, or TvInputManager.TIME_SHIFT_STATUS_UNSUPPORTED otherwise. Failure to notifying the current status change immediately might result in an undesirable behavior in the application such as hiding the play controls.

If the status TvInputManager.TIME_SHIFT_STATUS_AVAILABLE is reported, the application assumes it can pause/resume playback, seek to a specified time position and set playback rate and audio mode. The implementation should override onTimeShiftPause, onTimeShiftResume, onTimeShiftSeekTo, onTimeShiftGetStartPosition, onTimeShiftGetCurrentPosition and onTimeShiftSetPlaybackParams.

Unit notifyTrackSelected(type: Int, trackId: String!)

Sends the type and ID of a selected track. This is used to inform the application that a specific track is selected. The TV input service must call this method as soon as a track is selected either by default or in response to a call to onSelectTrack. The selected track ID for a given type is maintained in the framework until the next call to this method even after the entire track list is updated (but is reset when the session is tuned to a new channel), so care must be taken not to result in an obsolete track ID.

Unit notifyTracksChanged(tracks: MutableList<TvTrackInfo!>!)

Sends the list of all audio/video/subtitle tracks. The is used by the framework to maintain the track information for a given session, which in turn is used by TvView.getTracks for the application to retrieve metadata for a given track type. The TV input service must call this method as soon as the track information becomes available or is updated. Note that in a case where a part of the information for a certain track is updated, it is not necessary to create a new TvTrackInfo object with a different track ID.

Unit notifyTuned(channelUri: Uri)

Informs the application that this session has been tuned to the given channel.

Unit notifyTvMessage(type: Int, data: Bundle)

Sends the raw data from the received TV message as well as the type of message received.

Unit notifyVideoAvailable()

Informs the application that the video is now available for watching. Video is blocked until this method is called.

The TV input service must call this method as soon as the content rendered onto its surface is ready for viewing. This method must be called each time #onTune is called.

Unit notifyVideoFreezeUpdated(isFrozen: Boolean)

Informs the application that the video freeze state has been updated.

When true, the video is frozen on the last frame but audio playback remains active.

Unit notifyVideoUnavailable(reason: Int)

Informs the application that the video became unavailable for some reason. This is primarily used to signal the application to block the screen not to show any intermittent video artifacts.

Unit onAdBufferReady(buffer: AdBuffer)

Called when an advertisement buffer is ready for playback.

Unit onAppPrivateCommand(action: String, data: Bundle!)

Processes a private command sent from the application to the TV input. This can be used to provide domain-specific features that are only known between certain TV inputs and their clients.

View! onCreateOverlayView()

Called when the application requests to create an overlay view. Each session implementation can override this method and return its own view.

Boolean onGenericMotionEvent(event: MotionEvent!)

Implement this method to handle generic motion events on the current input session.

Boolean onKeyDown(keyCode: Int, event: KeyEvent!)

Default implementation of KeyEvent.Callback.onKeyDown(): always returns false (doesn't handle the event).

Override this to intercept key down events before they are processed by the application. If you return true, the application will not process the event itself. If you return false, the normal application processing will occur as if the TV input had not seen the event at all.

Boolean onKeyLongPress(keyCode: Int, event: KeyEvent!)

Default implementation of KeyEvent.Callback.onKeyLongPress(): always returns false (doesn't handle the event).

Override this to intercept key long press events before they are processed by the application. If you return true, the application will not process the event itself. If you return false, the normal application processing will occur as if the TV input had not seen the event at all.

Boolean onKeyMultiple(keyCode: Int, count: Int, event: KeyEvent!)

Default implementation of KeyEvent.Callback.onKeyMultiple(): always returns false (doesn't handle the event).

Override this to intercept special key multiple events before they are processed by the application. If you return true, the application will not itself process the event. If you return false, the normal application processing will occur as if the TV input had not seen the event at all.

Boolean onKeyUp(keyCode: Int, event: KeyEvent!)

Default implementation of KeyEvent.Callback.onKeyUp(): always returns false (doesn't handle the event).

Override this to intercept key up events before they are processed by the application. If you return true, the application will not itself process the event. If you return false, the normal application processing will occur as if the TV input had not seen the event at all.

Unit onOverlayViewSizeChanged(width: Int, height: Int)

Called when the size of the overlay view is changed by the application.

This is always called at least once when the session is created regardless of whether the overlay view is enabled or not. The overlay view size is the same as the containing TvView. Note that the size of the underlying surface can be different if the surface was changed by calling layoutSurface.

Unit onRelease()

Called when the session is released.

Unit onRemoveBroadcastInfo(requestId: Int)

Called when broadcast info is removed.

Unit onRequestAd(request: AdRequest)

Called when advertisement request is received.

Unit onRequestBroadcastInfo(request: BroadcastInfoRequest)

Called when broadcast info is requested.

Unit onResumePlayback()

Resumes playback of the Audio, Video, and CC streams.

Note that this is different form #timeShiftResume() as this is intended to be used after stopping playback. This is used to resume playback from the current position in the live broadcast.

Boolean onSelectAudioPresentation(presentationId: Int, programId: Int)

Selects an audio presentation.

On successfully selecting the audio presentation, notifyAudioPresentationSelected is invoked to provide updated information about the selected audio presentation to applications.

Boolean onSelectTrack(type: Int, trackId: String?)

Selects a given track.

If this is done successfully, the implementation should call notifyTrackSelected to help applications maintain the up-to-date list of the selected tracks.

Unit onSetCaptionEnabled(enabled: Boolean)

Enables or disables the caption.

The locale for the user's preferred captioning language can be obtained by calling CaptioningManager.getLocale().

Unit onSetInteractiveAppNotificationEnabled(enabled: Boolean)

Enables or disables interactive app notification.

This method enables or disables the event detection from the corresponding TV input. When it's enabled, the TV input service detects events related to interactive app, such as AIT (Application Information Table) and sends to TvView or the linked TV interactive app service.

Unit onSetStreamVolume(volume: Float)

Sets the relative stream volume of the current TV input session.

The implementation should honor this request in order to handle audio focus changes or mute the current session when multiple sessions, possibly from different inputs are active. If the method has not yet been called, the implementation should assume the default value of 1.0f.

Unit onSetTvMessageEnabled(type: Int, enabled: Boolean)

Called when the application enables or disables the detection of the specified message type.

Unit onStopPlayback(mode: Int)

Called when the application requests playback of the Audio, Video, and CC streams to be stopped, but the metadata should continue to be filtered.

The metadata that will continue to be filtered includes the PSI (Program specific information) and SI (Service Information), part of ISO/IEC 13818-1.

Note that this is different form #timeShiftPause() as should release the stream, making it impossible to resume from this position again.

Unit onSurfaceChanged(format: Int, width: Int, height: Int)

Called after any structural changes (format or size) have been made to the surface passed in onSetSurface. This method is always called at least once, after onSetSurface is called with non-null surface.

Long onTimeShiftGetCurrentPosition()

Returns the current position for time shifting, in milliseconds since the epoch. Returns TvInputManager.TIME_SHIFT_INVALID_TIME if the position is unknown at the moment.

The current position for time shifting is the same as the current position of playback. It should be equal to or greater than the start position reported by onTimeShiftGetStartPosition(). When playback is completed, the current position should stay where the playback ends, in other words, the returned value of this mehtod should be equal to the start position plus the duration of the program.

Long onTimeShiftGetStartPosition()

Returns the start position for time shifting, in milliseconds since the epoch. Returns TvInputManager.TIME_SHIFT_INVALID_TIME if the position is unknown at the moment.

The start position for time shifting indicates the earliest possible time the user can seek to. Initially this is equivalent to the time when the implementation starts recording. Later it may be adjusted because there is insufficient space or the duration of recording is limited by the implementation. The application does not allow the user to seek to a position earlier than the start position.

For playback of a recorded program initiated by onTimeShiftPlay(android.net.Uri), the start position should be 0 and does not change.

Unit onTimeShiftPause()

Called when the application requests to pause playback.

Unit onTimeShiftPlay(recordedProgramUri: Uri!)

Called when the application requests to play a given recorded TV program.

Unit onTimeShiftResume()

Called when the application requests to resume playback.

Unit onTimeShiftSeekTo(timeMs: Long)

Called when the application requests to seek to a specified time position. Normally, the position is given within range between the start and the current time, inclusively. The implementation is expected to seek to the nearest time position if the given position is not in the range.

Unit onTimeShiftSetMode(mode: Int)

Called when the application sets time shift mode.

Unit onTimeShiftSetPlaybackParams(params: PlaybackParams!)

Called when the application sets playback parameters containing the speed and audio mode.

Once the playback parameters are set, the implementation should honor the current settings until the next tune request. Pause/resume/seek request does not reset the parameters previously set.

Boolean onTouchEvent(event: MotionEvent!)

Implement this method to handle touch screen motion events on the current input session.

Boolean onTrackballEvent(event: MotionEvent!)

Implement this method to handle trackball events on the current input session.

Boolean onTune(channelUri: Uri!)

Tunes to a given channel.

No video will be displayed until notifyVideoAvailable() is called. Also, notifyVideoUnavailable(int) should be called when the TV input cannot continue playing the given channel.

Boolean onTune(channelUri: Uri!, params: Bundle!)

Tunes to a given channel. Override this method in order to handle domain-specific features that are only known between certain TV inputs and their clients.

The default implementation calls onTune(android.net.Uri).

Unit onTvAdSessionData(type: String, data: Bundle)

Called when data from the linked android.media.tv.ad.TvAdService is received.

Unit onTvMessage(type: Int, data: Bundle)

Called when a TV message is received

Unit onUnblockContent(unblockedRating: TvContentRating!)

Requests to unblock the content according to the given rating.

The implementation should unblock the content. TV input service has responsibility to decide when/how the unblock expires while it can keep previously unblocked ratings in order not to ask a user to unblock whenever a content rating is changed. Therefore an unblocked rating can be valid for a channel, a program, or certain amount of time depending on the implementation.

Unit sendTvInputSessionData(type: String, data: Bundle)

Sends data related to this session to corresponding linked android.media.tv.ad.TvAdService object via TvAdView.

Methods like notifyBroadcastInfoResponse(android.media.tv.BroadcastInfoResponse) sends the related data to linked android.media.tv.interactive.TvInteractiveAppService, but don't work for android.media.tv.ad.TvAdService. The method is used specifically for android.media.tv.ad.TvAdService use cases.

Unit setOverlayViewEnabled(enable: Boolean)

Enables or disables the overlay view.

By default, the overlay view is disabled. Must be called explicitly after the session is created to enable the overlay view.

The TV input service can disable its overlay view when the size of the overlay view is insufficient to display the whole information, such as when used in Picture-in-picture. Override onOverlayViewSizeChanged to get the size of the overlay view, which then can be used to determine whether to enable/disable the overlay view.

Public constructors

HardwareSession

Added in API level 21
HardwareSession(context: Context!)

Creates a new HardwareSession.

Parameters
context Context!: The context of the application

Public methods

getHardwareInputId

Added in API level 21
abstract fun getHardwareInputId(): String!

Returns the hardware TV input ID the external device is connected to.

TV input is expected to provide android.R.attr#setupActivity so that the application can launch it before using this TV input. The setup activity may let the user select the hardware TV input to which the external device is connected. The ID of the selected one should be stored in the TV input so that it can be returned here.

onHardwareVideoAvailable

Added in API level 21
open fun onHardwareVideoAvailable(): Unit

Called when the underlying hardware TV input session calls TvInputService.Session.notifyVideoAvailable().

onHardwareVideoUnavailable

Added in API level 21
open fun onHardwareVideoUnavailable(reason: Int): Unit

Called when the underlying hardware TV input session calls TvInputService.Session.notifyVideoUnavailable(int).

onSetSurface

Added in API level 21
fun onSetSurface(surface: Surface?): Boolean

This method will not be called in HardwareSession. Framework will forward the application's surface to the hardware TV input.

Parameters
surface Surface?: The surface to be used for video rendering. Can be null.
Return
Boolean true if the surface was set successfully, false otherwise.