Package com.castlabs.sdk.mediasession

# Class SessionCallbackBuilder.DefaultAllowedCommandProvider

java.lang.Object
com.castlabs.sdk.mediasession.SessionCallbackBuilder.DefaultAllowedCommandProvider

All Implemented Interfaces:
  `SessionCallbackBuilder.AllowedCommandProvider`

Enclosing class:
  SessionCallbackBuilder

public static final class SessionCallbackBuilder.DefaultAllowedCommandProvider extends Object implements SessionCallbackBuilder.AllowedCommandProvider
Default implementation of `SessionCallbackBuilder.AllowedCommandProvider` that behaves as follows:

- Accepts connection requests from controller if any of the following conditions are met:
  - Controller is in the same package as the session.
  - Controller is allowed via `setTrustedPackageNames(List)`.
  - Controller has package name `MediaSessionManager.RemoteUserInfo.LEGACY_CONTROLLER`. See `package name limitation` for details.
  - Controller is trusted (i.e. has MEDIA_CONTENT_CONTROL permission or has enabled notification manager).
- Allows all commands that the current player can handle.
- Accepts all command requests for allowed commands.

Note: this implementation matches the behavior of the ExoPlayer MediaSession extension and `MediaSessionCompat`.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `DefaultAllowedCommandProvider(Context context)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `acceptConnection(androidx.media2.session.MediaSession session, androidx.media2.session.MediaSession.ControllerInfo controllerInfo)`
  Called to query whether to allow connection from the controller.
  `androidx.media2.session.SessionCommandGroup`
  `getAllowedCommands(androidx.media2.session.MediaSession session, androidx.media2.session.MediaSession.ControllerInfo controllerInfo, androidx.media2.session.SessionCommandGroup baseAllowedSessionCommands)`
  Called to query allowed commands in following cases: A `MediaController` requests to connect, and allowed commands is required to tell initial allowed commands.
  `int`
  `onCommandRequest(androidx.media2.session.MediaSession session, androidx.media2.session.MediaSession.ControllerInfo controllerInfo, androidx.media2.session.SessionCommand command)`
  Called when a `MediaController` has called an API that controls `SessionPlayer` set to the `MediaSession`.
  `void`
  `setTrustedPackageNames(List<String> packageNames)`
  Sets the package names from which the session will accept incoming connections.

  ### Methods inherited from class java.lang.Object

  `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### DefaultAllowedCommandProvider

    public DefaultAllowedCommandProvider(Context context)

- ## Method Details

  - ### acceptConnection

    public boolean acceptConnection(androidx.media2.session.MediaSession session, androidx.media2.session.MediaSession.ControllerInfo controllerInfo)
    Description copied from interface: `SessionCallbackBuilder.AllowedCommandProvider`
    Called to query whether to allow connection from the controller.

    If it returns `true` to accept connection, then `SessionCallbackBuilder.AllowedCommandProvider.getAllowedCommands(androidx.media2.session.MediaSession, androidx.media2.session.MediaSession.ControllerInfo, androidx.media2.session.SessionCommandGroup)` will be immediately followed to return initial allowed command.

    Prefer use `SessionCallbackBuilder.PostConnectCallback` for any extra initialization about controller, where controller is connected and session can send commands to the controller.

    Specified by:
      `acceptConnection` in interface `SessionCallbackBuilder.AllowedCommandProvider`
    Parameters:
      `session` - The media session.
      `controllerInfo` - The `MediaSession.ControllerInfo` for the controller that is requesting connect.
    Returns:
      `true` to accept connection. `false` otherwise.

  - ### getAllowedCommands

    public androidx.media2.session.SessionCommandGroup getAllowedCommands(androidx.media2.session.MediaSession session, androidx.media2.session.MediaSession.ControllerInfo controllerInfo, androidx.media2.session.SessionCommandGroup baseAllowedSessionCommands)
    Description copied from interface: `SessionCallbackBuilder.AllowedCommandProvider`
    Called to query allowed commands in following cases:
    - A `MediaController` requests to connect, and allowed commands is required to tell initial allowed commands.
    - Underlying `SessionPlayer` state changes, and allowed commands may be updated via `MediaSession.setAllowedCommands(androidx.media2.session.MediaSession.ControllerInfo, androidx.media2.session.SessionCommandGroup)`.

    The provided `baseAllowedSessionCommand` is built automatically based on the state of the `SessionPlayer`, `SessionCallbackBuilder.RatingCallback`, `SessionCallbackBuilder.MediaItemProvider`, `SessionCallbackBuilder.CustomCommandProvider`, and `SessionCallbackBuilder.SkipCallback` so may be a useful starting point for any required customizations.

    Specified by:
      `getAllowedCommands` in interface `SessionCallbackBuilder.AllowedCommandProvider`
    Parameters:
      `session` - The media session.
      `controllerInfo` - The `MediaSession.ControllerInfo` for the controller for which allowed commands are being queried.
      `baseAllowedSessionCommands` - Base allowed session commands for customization.
    Returns:
      The allowed commands for the controller.
    See Also:
      - `MediaSession.SessionCallback.onConnect(MediaSession, ControllerInfo)`

  - ### onCommandRequest

    public int onCommandRequest(androidx.media2.session.MediaSession session, androidx.media2.session.MediaSession.ControllerInfo controllerInfo, androidx.media2.session.SessionCommand command)
    Description copied from interface: `SessionCallbackBuilder.AllowedCommandProvider`
    Called when a `MediaController` has called an API that controls `SessionPlayer` set to the `MediaSession`.
    Specified by:
      `onCommandRequest` in interface `SessionCallbackBuilder.AllowedCommandProvider`
    Parameters:
      `session` - The media session.
      `controllerInfo` - A `MediaSession.ControllerInfo` that needs allowed command update.
      `command` - A `SessionCommand` from the controller.
    Returns:
      A session result code defined in `SessionResult`.
    See Also:
      - `MediaSession.SessionCallback.onCommandRequest(androidx.media2.session.MediaSession, androidx.media2.session.MediaSession.ControllerInfo, androidx.media2.session.SessionCommand)`

  - ### setTrustedPackageNames

    public void setTrustedPackageNames(@Nullable List<String> packageNames)
    Sets the package names from which the session will accept incoming connections.

    Apps that have `android.Manifest.permission.MEDIA_CONTENT_CONTROL`, packages listed in enabled_notification_listeners and the current package are always trusted, even if they are not specified here.

    Parameters:
      `packageNames` - Package names from which the session will accept incoming connections.
    See Also:
      - `MediaSession.SessionCallback.onConnect(MediaSession, MediaSession.ControllerInfo)`
      - `MediaSessionManager.isTrustedForMediaControl(RemoteUserInfo)`