SessionManager

public classSessionManagerextendsObject

A class that managesSession instances. The application can attach aSessionManagerListener to be notified of session events.

SessionManager works with AndroidMediaRouteron managing session lifecycle. The current session always uses the current selected route (which corresponds toMediaRouter.getSelectedRoute()).

SessionManager listens to route changes fromMediaRoutervia MediaRouter.addCallback(MediaRouteSelector, MediaRouter.Callback).For each session type it supports, either a Cast session type or a custom session type as specified by aSessionProvider, aMediaRouter.Callback will be added toMediaRouterwith a correspondingMediaRouteSelector generated using the category from SessionProvider.getCategory().For Cast sessions, the category is generated according to CastOptions.getReceiverApplicationId().

When a route is selected inMediaRouter,the corresponding callback will be executed andSessionManager will call SessionProvider.createSession(String)to create a session. Then it will try to start a new session or resume the session that was shutdown incorrectly. When a session has ended, failed on start or failed on resume,SessionManager will unselect the corresponding route.

When a route is unselected inMediaRouter,the corresponding callback will be executed andSessionManager will end the current session if it is using the unselected route.

BecauseSessionManager listens to route selection signals to start and end sessions, Cast dialog must make sure to update the route selection state in AndroidMediaRouter.The device chooser dialog must call MediaRouter.selectRoute(MediaRouter.RouteInfo)orMediaRouter.RouteInfo.select() when the user selects a device. The route controller dialog must callMediaRouter.unselect(int) (or endCurrentSession(boolean)when the user clicks the "Stop Casting" (or "Disconnect" ) button.

Based on how an app creates Cast dialogs, the following actions needs to be done:

Public Method Summary

void
addSessionManagerListener(SessionManagerListener<Session> listener)
Adds a SessionManagerListenerto monitor events from any type of Session instance.
<T extendsSession> void
addSessionManagerListener(SessionManagerListener<T> listener,Class<T> sessionClass)
Adds a SessionManagerListenerto monitor events from aSession instance whose class issessionClass.
void
endCurrentSession(boolean stopCasting)
Ends the current session.
CastSession
getCurrentCastSession()
Returns the current session if it is an instance ofCastSession, otherwise returnsnull.
Session
getCurrentSession()
Returns the currently active session.
void
<T extendsSession> void
void
startSession(Intent startSessionIntent)
Starts a session.

Inherited Method Summary

Public Methods

public voidaddSessionManagerListener(SessionManagerListener<Session> listener)

Adds aSessionManagerListener to monitor events from any type ofSession instance.

Throws
NullPointerException If listener isnull.
IllegalStateException If this method is not called on the main thread.

public voidaddSessionManagerListener(SessionManagerListener<T> listener,Class<T> sessionClass)

Adds aSessionManagerListener to monitor events from aSession instance whose class issessionClass.

Throws
NullPointerException IflistenerorsessionClassare null.
IllegalStateException If this method is not called on the main thread.

public voidendCurrentSession(boolean stopCasting)

Ends the current session.

Parameters
stopCasting Should the receiver application be stopped when ending the current Session.
Throws
IllegalStateException If this method is not called on the main thread.

publicCastSession getCurrentCastSession()

Returns the current session if it is an instance ofCastSession, otherwise returnsnull.

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

publicSession getCurrentSession()

Returns the currently active session. Returnsnullif no session is active.

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

public voidremoveSessionManagerListener(SessionManagerListener<Session> listener)

Parameters
listener The SessionManagerListenerto be removed.
Throws
IllegalStateException If this method is not called on the main thread.

public voidremoveSessionManagerListener(SessionManagerListener<T> listener,Class<T> sessionClass)

Parameters
listener The SessionManagerListenerto be removed.
sessionClass
Throws
NullPointerException IfsessionClassisnull.
IllegalStateException If this method is not called on the main thread.

public voidstartSession(Intent startSessionIntent)

Starts a session. The sender app should call this method after the app is launched by an implicit intent fired by a component outside the app, such as Google Home app. We recommended calling this method in onResume() instead of onCreate() because onCreate() is called only when the app launches. If the app is running when the intent is fired, it will be brought to the foreground and onCreate() will not be called.

public class MyActivity extends Activity {
...
@Override
protected void onResume() {
...
Intent intent = getIntent();
// You need define your own intent filter and figure out how to check if the intent is
// for joining a cast session.
if (intentIsForStartingSession(intent)) {
CastContext castContext = CastContext.getSharedInstance(this);
castContext.getSessionManager().startSession(intent);
}
...
}
}
Parameters
startSessionIntent The intent that is used to start the app and ask it to join a cast session. The intent contains necessary information for starting a session, such as the route ID, device name, session ID, etc. This information is set by the component that fires the intent.