Summary: Protected Ctors | Methods | Protected Methods | Inherited Methods | [Expand All]
public abstract class

# Session

extends Object

java.lang.Object
↳ | com.google.android.gms.cast.framework.Session

Known Direct Subclasses CastSession
CastSession | An implementation of `Session` for managing connections to a Cast receiver device.

## Class Overview

An abstract base class representing a session with a receiver application. A session is an end-to-end connection from a sender application to a receiver application. When the user selects a receiver device from the media route chooser dialog, a session is created by the `SessionManager` using its corresponding `SessionProvider`.

A subclass must override `start(Bundle)`, `resume(Bundle)` and `end(boolean)`, so that `SessionManager` can call them to start, resume or end the session. Subclass must also call the appropriate notifier methods to notify the state of the session. For example, call `notifySessionStarted(String)` when the session has been successfully started, so that the `SessionManager` will notify the rest of the app that the session has been started.

An implementation of this base class, `CastSession`, is provided by the SDK to manage connection to a Cast receiver device. This Session base class is provided so that if a sender application supports a receiver device that is not a Cast device, it can implement a custom session to communicate with that device, and still be able to let the SDK create the session when the user selects that device from the media route chooser dialog.

All methods must be called from the main thread.

##### See Also

- `CastSession`

## Summary

Protected Constructors
| Session(Context applicationContext, String category, String sessionId) Constructs a `Session` with a category string.

Public Methods
final String | getCategory() Returns the category associated with this `Session`.
final String | getSessionId() Returns the unique ID for this session.
long | getSessionRemainingTimeMs() Subclass can override this method to provide a hint of how long the current session will live.
boolean | isConnected() Returns `true` if the session is connected.
boolean | isConnecting() Returns `true` if the session is connecting or resuming.
boolean | isDisconnected() Returns `true` if the session is disconnected.
boolean | isDisconnecting() Returns `true` if the session is disconnecting.
boolean | isResuming() Returns `true` if the session is resuming.
boolean | isSuspended() Returns `true` if the session is suspended.

Protected Methods
abstract void | end(boolean stopCasting) Subclass should override this method to provide business logic of ending a session.
final void | notifyFailedToResumeSession(int error) Subclass should call this to notify the SDK that a session has failed to resume.
final void | notifyFailedToStartSession(int error) Subclass should call this to notify the SDK that a session has failed to start.
final void | notifySessionEnded(int error) Subclass should call this to notify the SDK that a session has ended.
final void | notifySessionResumed(boolean wasSuspended) Subclass should call this to notify the SDK that a session has been successfully resumed.
final void | notifySessionStarted(String sessionId) Subclass should call this to notify the SDK that a session has been successfully started.
final void | notifySessionSuspended(int reason) Subclass should call this to notify the SDK that a session has been suspended.
abstract void | resume(Bundle routeInfoExtra) Subclass should override this method to provide business logic of resuming a session.
abstract void | start(Bundle routeInfoExtra) Subclass should override this method to provide business logic of starting a session.

[Expand] Inherited Methods
From class java.lang.Object
Object | clone()
boolean | equals(Object arg0)
void | finalize()
final Class<?> | getClass()
int | hashCode()
final void | notify()
final void | notifyAll()
String | toString()
final void | wait(long arg0, int arg1)
final void | wait(long arg0)
final void | wait()

## Protected Constructors

#### protected Session (Context applicationContext, String category, String sessionId)

Constructs a `Session` with a category string. The category uniquely identifies a `Session`.

##### Parameters

applicationContext | The application Context of the calling app.
category | The category string for this `Session`.
sessionId | The session ID of an existing session, if this object will be used to resume a session; otherwise `null` if it will be used to start a new session.

## Public Methods

#### public final String getCategory ()

Returns the category associated with this `Session`.

#### public final String getSessionId ()

Returns the unique ID for this session.

#### public long getSessionRemainingTimeMs ()

Subclass can override this method to provide a hint of how long the current session will live. The SDK may call this method at any time to decide how long it should try to automatically resume the session if it got disconnected unexpectedly. The default return value is `0`.

#### public boolean isConnected ()

Returns `true` if the session is connected.

#### public boolean isConnecting ()

Returns `true` if the session is connecting or resuming.

#### public boolean isDisconnected ()

Returns `true` if the session is disconnected.

#### public boolean isDisconnecting ()

Returns `true` if the session is disconnecting.

#### public boolean isResuming ()

Returns `true` if the session is resuming. A session is considered connecting when resuming.

#### public boolean isSuspended ()

Returns `true` if the session is suspended. A session is considered connecting when is suspended.

## Protected Methods

#### protected abstract void end (boolean stopCasting)

Subclass should override this method to provide business logic of ending a session. This method is called by the SDK when ending an active session. This method is called on the main thread and must not block.

##### Parameters

stopCasting | Should the receiver app be stopped when the current session ends.

#### protected final void notifyFailedToResumeSession (int error)

Subclass should call this to notify the SDK that a session has failed to resume.

#### protected final void notifyFailedToStartSession (int error)

Subclass should call this to notify the SDK that a session has failed to start.

#### protected final void notifySessionEnded (int error)

Subclass should call this to notify the SDK that a session has ended.

#### protected final void notifySessionResumed (boolean wasSuspended)

Subclass should call this to notify the SDK that a session has been successfully resumed.

#### protected final void notifySessionStarted (String sessionId)

Subclass should call this to notify the SDK that a session has been successfully started.

#### protected final void notifySessionSuspended (int reason)

Subclass should call this to notify the SDK that a session has been suspended.

#### protected abstract void resume (Bundle routeInfoExtra)

Subclass should override this method to provide business logic of resuming a session. This method is called by the SDK when resuming a session. This method is called on the main thread and must not block.

##### Parameters

routeInfoExtra | The extra field in the MediaRouter.RouteInfo.

#### protected abstract void start (Bundle routeInfoExtra)

Subclass should override this method to provide business logic of starting a session. This method is called by the SDK when starting a session. This method is called on the main thread and must not block.

##### Parameters

routeInfoExtra | The extra field in the MediaRouter.RouteInfo.