4343import com .example .android .uamp .ui .NowPlayingActivity ;
4444import com .example .android .uamp .utils .CarHelper ;
4545import com .example .android .uamp .utils .LogHelper ;
46+ import com .example .android .uamp .utils .TvHelper ;
4647import com .example .android .uamp .utils .WearHelper ;
47- import com .google .android .gms .cast .ApplicationMetadata ;
48- import com .google .android .libraries .cast .companionlibrary . cast . VideoCastManager ;
49- import com .google .android .libraries .cast .companionlibrary . cast . callbacks . VideoCastConsumerImpl ;
50-
48+ import com .google .android .gms .cast .framework . CastContext ;
49+ import com .google .android .gms .cast .framework . CastSession ;
50+ import com .google .android .gms .cast .framework . SessionManager ;
51+ import com . google . android . gms . cast . framework . SessionManagerListener ;
5152import java .lang .ref .WeakReference ;
5253import java .util .List ;
5354
@@ -140,50 +141,12 @@ public class MusicService extends MediaBrowserServiceCompat implements
140141 private final DelayedStopHandler mDelayedStopHandler = new DelayedStopHandler (this );
141142 private MediaRouter mMediaRouter ;
142143 private PackageValidator mPackageValidator ;
144+ private SessionManager mCastSessionManager ;
145+ private SessionManagerListener <CastSession > mCastSessionManagerListener ;
143146
144147 private boolean mIsConnectedToCar ;
145148 private BroadcastReceiver mCarConnectionReceiver ;
146149
147- /**
148- * Consumer responsible for switching the Playback instances depending on whether
149- * it is connected to a remote player.
150- */
151- private final VideoCastConsumerImpl mCastConsumer = new VideoCastConsumerImpl () {
152-
153- @ Override
154- public void onApplicationConnected (ApplicationMetadata appMetadata , String sessionId ,
155- boolean wasLaunched ) {
156- // In case we are casting, send the device name as an extra on MediaSession metadata.
157- mSessionExtras .putString (EXTRA_CONNECTED_CAST ,
158- VideoCastManager .getInstance ().getDeviceName ());
159- mSession .setExtras (mSessionExtras );
160- // Now we can switch to CastPlayback
161- Playback playback = new CastPlayback (mMusicProvider );
162- mMediaRouter .setMediaSessionCompat (mSession );
163- mPlaybackManager .switchToPlayback (playback , true );
164- }
165-
166- @ Override
167- public void onDisconnectionReason (int reason ) {
168- LogHelper .d (TAG , "onDisconnectionReason" );
169- // This is our final chance to update the underlying stream position
170- // In onDisconnected(), the underlying CastPlayback#mVideoCastConsumer
171- // is disconnected and hence we update our local value of stream position
172- // to the latest position.
173- mPlaybackManager .getPlayback ().updateLastKnownStreamPosition ();
174- }
175-
176- @ Override
177- public void onDisconnected () {
178- LogHelper .d (TAG , "onDisconnected" );
179- mSessionExtras .remove (EXTRA_CONNECTED_CAST );
180- mSession .setExtras (mSessionExtras );
181- Playback playback = new LocalPlayback (MusicService .this , mMusicProvider );
182- mMediaRouter .setMediaSessionCompat (null );
183- mPlaybackManager .switchToPlayback (playback , false );
184- }
185- };
186-
187150 /*
188151 * (non-Javadoc)
189152 * @see android.app.Service#onCreate()
@@ -258,7 +221,14 @@ public void onQueueUpdated(String title,
258221 } catch (RemoteException e ) {
259222 throw new IllegalStateException ("Could not create a MediaNotificationManager" , e );
260223 }
261- VideoCastManager .getInstance ().addVideoCastConsumer (mCastConsumer );
224+
225+ if (!TvHelper .isTvUiMode (this )) {
226+ mCastSessionManager = CastContext .getSharedInstance (this ).getSessionManager ();
227+ mCastSessionManagerListener = new CastSessionManagerListener ();
228+ mCastSessionManager .addSessionManagerListener (mCastSessionManagerListener ,
229+ CastSession .class );
230+ }
231+
262232 mMediaRouter = MediaRouter .getInstance (getApplicationContext ());
263233
264234 registerCarConnectionReceiver ();
@@ -277,7 +247,7 @@ public int onStartCommand(Intent startIntent, int flags, int startId) {
277247 if (CMD_PAUSE .equals (command )) {
278248 mPlaybackManager .handlePauseRequest ();
279249 } else if (CMD_STOP_CASTING .equals (command )) {
280- VideoCastManager . getInstance (). disconnect ( );
250+ CastContext . getSharedInstance ( this ). getSessionManager (). endCurrentSession ( true );
281251 }
282252 } else {
283253 // Try to handle the intent as a media button event wrapped by MediaButtonReceiver
@@ -302,7 +272,12 @@ public void onDestroy() {
302272 // Service is being killed, so make sure we release our resources
303273 mPlaybackManager .handleStopRequest (null );
304274 mMediaNotificationManager .stopNotification ();
305- VideoCastManager .getInstance ().removeVideoCastConsumer (mCastConsumer );
275+
276+ if (mCastSessionManager != null ) {
277+ mCastSessionManager .removeSessionManagerListener (mCastSessionManagerListener ,
278+ CastSession .class );
279+ }
280+
306281 mDelayedStopHandler .removeCallbacksAndMessages (null );
307282 mSession .release ();
308283 }
@@ -439,4 +414,66 @@ public void handleMessage(Message msg) {
439414 }
440415 }
441416 }
417+
418+ /**
419+ * Session Manager Listener responsible for switching the Playback instances
420+ * depending on whether it is connected to a remote player.
421+ */
422+ private class CastSessionManagerListener implements SessionManagerListener <CastSession > {
423+
424+ @ Override
425+ public void onSessionEnded (CastSession session , int error ) {
426+ LogHelper .d (TAG , "onSessionEnded" );
427+ mSessionExtras .remove (EXTRA_CONNECTED_CAST );
428+ mSession .setExtras (mSessionExtras );
429+ Playback playback = new LocalPlayback (MusicService .this , mMusicProvider );
430+ mMediaRouter .setMediaSessionCompat (null );
431+ mPlaybackManager .switchToPlayback (playback , false );
432+ }
433+
434+ @ Override
435+ public void onSessionResumed (CastSession session , boolean wasSuspended ) {
436+ }
437+
438+ @ Override
439+ public void onSessionStarted (CastSession session , String sessionId ) {
440+ // In case we are casting, send the device name as an extra on MediaSession metadata.
441+ mSessionExtras .putString (EXTRA_CONNECTED_CAST ,
442+ session .getCastDevice ().getFriendlyName ());
443+ mSession .setExtras (mSessionExtras );
444+ // Now we can switch to CastPlayback
445+ Playback playback = new CastPlayback (mMusicProvider , MusicService .this );
446+ mMediaRouter .setMediaSessionCompat (mSession );
447+ mPlaybackManager .switchToPlayback (playback , true );
448+ }
449+
450+ @ Override
451+ public void onSessionStarting (CastSession session ) {
452+ }
453+
454+ @ Override
455+ public void onSessionStartFailed (CastSession session , int error ) {
456+ }
457+
458+ @ Override
459+ public void onSessionEnding (CastSession session ) {
460+ // This is our final chance to update the underlying stream position
461+ // In onSessionEnded(), the underlying CastPlayback#mRemoteMediaClient
462+ // is disconnected and hence we update our local value of stream position
463+ // to the latest position.
464+ mPlaybackManager .getPlayback ().updateLastKnownStreamPosition ();
465+ }
466+
467+ @ Override
468+ public void onSessionResuming (CastSession session , String sessionId ) {
469+ }
470+
471+ @ Override
472+ public void onSessionResumeFailed (CastSession session , int error ) {
473+ }
474+
475+ @ Override
476+ public void onSessionSuspended (CastSession session , int reason ) {
477+ }
478+ }
442479}
0 commit comments