public abstract class

Session

extends Object
java.lang.Object
   ↳ com.google.android.gms.cast.framework.Session
Known Direct Subclasses

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:

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

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 Context: The application Context of the calling app.
category String: The category string for this Session.
sessionId String: 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.

Returns
String
Throws
IllegalStateException If this method is not called on the main thread.

public final String getSessionId ()

Returns the unique ID for this session.

Returns
String
Throws
IllegalStateException If this method is not called on the main thread.

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.

Returns
long
Throws
IllegalStateException If this method is not called on the main thread.

public boolean isConnected ()

Returns true if the session is connected.

Returns
boolean
Throws
IllegalStateException If this method is not called on the main thread.

public boolean isConnecting ()

Returns true if the session is connecting or resuming.

Returns
boolean
Throws
IllegalStateException If this method is not called on the main thread.

public boolean isDisconnected ()

Returns true if the session is disconnected.

Returns
boolean
Throws
IllegalStateException If this method is not called on the main thread.

public boolean isDisconnecting ()

Returns true if the session is disconnecting.

Returns
boolean
Throws
IllegalStateException If this method is not called on the main thread.

public boolean isResuming ()

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

Returns
boolean
Throws
IllegalStateException If this method is not called on the main thread.

public boolean isSuspended ()

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

Returns
boolean
Throws
IllegalStateException If this method is not called on the main thread.

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 boolean: 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.

Parameters
error int

protected final void notifyFailedToStartSession (int error)

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

Parameters
error int

protected final void notifySessionEnded (int error)

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

Parameters
error int

protected final void notifySessionResumed (boolean wasSuspended)

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

Parameters
wasSuspended boolean

protected final void notifySessionStarted (String sessionId)

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

Parameters
sessionId String

protected final void notifySessionSuspended (int reason)

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

Parameters
reason int

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 Bundle: 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 Bundle: The extra field in the MediaRouter.RouteInfo.