Skip to content
This repository was archived by the owner on Jul 8, 2025. It is now read-only.

Commit 39fa286

Browse files
committed
Migrate uAmp to Cast SDK v3.
Replace Cast Companion Library w/ Cast SDK v3 Sender API. Change-Id: I11be5625b43579146898955878bb2672142e8c4c
1 parent 19c444e commit 39fa286

File tree

8 files changed

+254
-188
lines changed

8 files changed

+254
-188
lines changed

mobile/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,12 @@ repositories {
4949
}
5050

5151
dependencies {
52-
compile 'com.google.android.gms:play-services-cast:9.0.0'
52+
compile 'com.google.android.gms:play-services-cast-framework:9.4.0'
5353
compile 'com.google.android.support:wearable:1.3.0'
5454
compile 'com.android.support:appcompat-v7:23.4.0'
5555
compile 'com.android.support:cardview-v7:23.4.0'
5656
compile 'com.android.support:mediarouter-v7:23.4.0'
5757
compile 'com.android.support:leanback-v17:23.4.0'
58-
compile 'com.google.android.libraries.cast.companionlibrary:ccl:2.8.4'
5958
compile 'com.android.support:design:23.4.0'
6059
testCompile 'junit:junit:4.12'
6160
testCompile 'org.mockito:mockito-core:1.10.19'

mobile/src/main/AndroidManifest.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
android:required="false" />
3434

3535
<application
36-
android:name=".UAMPApplication"
3736
android:allowBackup="true"
3837
android:icon="@drawable/ic_launcher"
3938
android:label="@string/app_name"
@@ -138,9 +137,12 @@
138137
</intent-filter>
139138
</service>
140139

141-
<!-- Service that keeps track of reconnecting to Cast when wifi is enabled. -->
142-
<service
143-
android:name="com.google.android.libraries.cast.companionlibrary.cast.reconnection.ReconnectionService"/>
140+
<!--
141+
(REQUIRED) use this meta data to to declare the app OptionsProvider.
142+
-->
143+
<meta-data
144+
android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
145+
android:value="com.example.android.uamp.CastOptionsProvider" />
144146

145147
<!-- A full screen activity showing controls and a seek bar for
146148
the current playing music -->
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (C) 2016 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.uamp;
18+
19+
import android.content.Context;
20+
21+
import com.google.android.gms.cast.framework.CastOptions;
22+
import com.google.android.gms.cast.framework.OptionsProvider;
23+
import com.google.android.gms.cast.framework.SessionProvider;
24+
25+
import java.util.List;
26+
27+
/**
28+
* Specify receiver application ID for cast
29+
*/
30+
public class CastOptionsProvider implements OptionsProvider {
31+
32+
@Override
33+
public CastOptions getCastOptions(Context context) {
34+
return new CastOptions.Builder()
35+
.setReceiverApplicationId(context.getString(R.string.cast_application_id))
36+
.build();
37+
}
38+
39+
@Override
40+
public List<SessionProvider> getAdditionalSessionProviders(Context context) {
41+
return null;
42+
}
43+
}

mobile/src/main/java/com/example/android/uamp/MusicService.java

Lines changed: 84 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@
4343
import com.example.android.uamp.ui.NowPlayingActivity;
4444
import com.example.android.uamp.utils.CarHelper;
4545
import com.example.android.uamp.utils.LogHelper;
46+
import com.example.android.uamp.utils.TvHelper;
4647
import 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;
5152
import java.lang.ref.WeakReference;
5253
import 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
}

mobile/src/main/java/com/example/android/uamp/UAMPApplication.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)