A class that managesSession
instances. The application can attach aSessionManagerListener
to be notified of session events.
SessionManager
works with AndroidMediaRouter
on
managing session lifecycle. The current session always uses the current selected route (which
corresponds toMediaRouter.getSelectedRoute()
).
SessionManager
listens to route changes fromMediaRouter
via
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 toMediaRouter
with
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:
- If the app creates Cast dialogs using
MediaRouteChooserDialog
andMediaRouteControllerDialog
, then these dialogs will update route selection inMediaRouter
automatically, so nothing needs to be done. - If the app sets up the Cast button using
CastButtonFactory.setUpMediaRouteButton(Context, Menu, int)
orCastButtonFactory.setUpMediaRouteButton(Context, MediaRouteButton)
,then the dialogs are actually created usingMediaRouteChooserDialog
andMediaRouteControllerDialog
, so nothing needs to be done. - For other cases, the app will be creating custom Cast dialogs, so it needs to follow
the above instructions to update the route selection state in
MediaRouter
.
Public Method Summary
void |
addSessionManagerListener(SessionManagerListener<Session>
listener)
Adds a
SessionManagerListener to monitor events from any type of
Session
instance.
|
<T extendsSession> void | |
void |
endCurrentSession(boolean stopCasting)
Ends the current session.
|
CastSession |
getCurrentCastSession()
Returns the current session if it is an instance of
CastSession ,
otherwise returnsnull .
|
Session |
getCurrentSession()
Returns the currently active session.
|
void |
removeSessionManagerListener(SessionManagerListener<Session>
listener)
Removes the
SessionManagerListener .
|
<T extendsSession> void |
removeSessionManagerListener(SessionManagerListener<T>
listener,Class<T>
sessionClass)
Removes the
SessionManagerListener .
|
void |
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 | Iflistener orsessionClass are
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. Returnsnull
if no session is
active.
Throws
IllegalStateException | If this method is not called on the main thread. |
---|
public voidremoveSessionManagerListener(SessionManagerListener<Session> listener)
Removes theSessionManagerListener
.
Parameters
listener | The
SessionManagerListener to be removed. |
---|
Throws
IllegalStateException | If this method is not called on the main thread. |
---|
public voidremoveSessionManagerListener(SessionManagerListener<T> listener,Class<T> sessionClass)
Removes theSessionManagerListener
.
Parameters
listener | The
SessionManagerListener to be removed. |
---|---|
sessionClass |
Throws
NullPointerException | IfsessionClass isnull . |
---|---|
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. |
---|