Skip to content

Commit

Permalink
Migrate uAmp to Cast SDK v3.
Browse files Browse the repository at this point in the history
Replace Cast Companion Library w/ Cast SDK v3 Sender API.

Change-Id: I11be5625b43579146898955878bb2672142e8c4c
  • Loading branch information
smishra2 committed Aug 4, 2016
1 parent 19c444e commit 39fa286
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 188 deletions.
3 changes: 1 addition & 2 deletions mobile/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ repositories {
}

dependencies {
compile 'com.google.android.gms:play-services-cast:9.0.0'
compile 'com.google.android.gms:play-services-cast-framework:9.4.0'
compile 'com.google.android.support:wearable:1.3.0'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.android.support:mediarouter-v7:23.4.0'
compile 'com.android.support:leanback-v17:23.4.0'
compile 'com.google.android.libraries.cast.companionlibrary:ccl:2.8.4'
compile 'com.android.support:design:23.4.0'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
Expand Down
10 changes: 6 additions & 4 deletions mobile/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
android:required="false" />

<application
android:name=".UAMPApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
Expand Down Expand Up @@ -138,9 +137,12 @@
</intent-filter>
</service>

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

<!-- A full screen activity showing controls and a seek bar for
the current playing music -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.uamp;

import android.content.Context;

import com.google.android.gms.cast.framework.CastOptions;
import com.google.android.gms.cast.framework.OptionsProvider;
import com.google.android.gms.cast.framework.SessionProvider;

import java.util.List;

/**
* Specify receiver application ID for cast
*/
public class CastOptionsProvider implements OptionsProvider {

@Override
public CastOptions getCastOptions(Context context) {
return new CastOptions.Builder()
.setReceiverApplicationId(context.getString(R.string.cast_application_id))
.build();
}

@Override
public List<SessionProvider> getAdditionalSessionProviders(Context context) {
return null;
}
}
131 changes: 84 additions & 47 deletions mobile/src/main/java/com/example/android/uamp/MusicService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@
import com.example.android.uamp.ui.NowPlayingActivity;
import com.example.android.uamp.utils.CarHelper;
import com.example.android.uamp.utils.LogHelper;
import com.example.android.uamp.utils.TvHelper;
import com.example.android.uamp.utils.WearHelper;
import com.google.android.gms.cast.ApplicationMetadata;
import com.google.android.libraries.cast.companionlibrary.cast.VideoCastManager;
import com.google.android.libraries.cast.companionlibrary.cast.callbacks.VideoCastConsumerImpl;

import com.google.android.gms.cast.framework.CastContext;
import com.google.android.gms.cast.framework.CastSession;
import com.google.android.gms.cast.framework.SessionManager;
import com.google.android.gms.cast.framework.SessionManagerListener;
import java.lang.ref.WeakReference;
import java.util.List;

Expand Down Expand Up @@ -140,50 +141,12 @@ public class MusicService extends MediaBrowserServiceCompat implements
private final DelayedStopHandler mDelayedStopHandler = new DelayedStopHandler(this);
private MediaRouter mMediaRouter;
private PackageValidator mPackageValidator;
private SessionManager mCastSessionManager;
private SessionManagerListener<CastSession> mCastSessionManagerListener;

private boolean mIsConnectedToCar;
private BroadcastReceiver mCarConnectionReceiver;

/**
* Consumer responsible for switching the Playback instances depending on whether
* it is connected to a remote player.
*/
private final VideoCastConsumerImpl mCastConsumer = new VideoCastConsumerImpl() {

@Override
public void onApplicationConnected(ApplicationMetadata appMetadata, String sessionId,
boolean wasLaunched) {
// In case we are casting, send the device name as an extra on MediaSession metadata.
mSessionExtras.putString(EXTRA_CONNECTED_CAST,
VideoCastManager.getInstance().getDeviceName());
mSession.setExtras(mSessionExtras);
// Now we can switch to CastPlayback
Playback playback = new CastPlayback(mMusicProvider);
mMediaRouter.setMediaSessionCompat(mSession);
mPlaybackManager.switchToPlayback(playback, true);
}

@Override
public void onDisconnectionReason(int reason) {
LogHelper.d(TAG, "onDisconnectionReason");
// This is our final chance to update the underlying stream position
// In onDisconnected(), the underlying CastPlayback#mVideoCastConsumer
// is disconnected and hence we update our local value of stream position
// to the latest position.
mPlaybackManager.getPlayback().updateLastKnownStreamPosition();
}

@Override
public void onDisconnected() {
LogHelper.d(TAG, "onDisconnected");
mSessionExtras.remove(EXTRA_CONNECTED_CAST);
mSession.setExtras(mSessionExtras);
Playback playback = new LocalPlayback(MusicService.this, mMusicProvider);
mMediaRouter.setMediaSessionCompat(null);
mPlaybackManager.switchToPlayback(playback, false);
}
};

/*
* (non-Javadoc)
* @see android.app.Service#onCreate()
Expand Down Expand Up @@ -258,7 +221,14 @@ public void onQueueUpdated(String title,
} catch (RemoteException e) {
throw new IllegalStateException("Could not create a MediaNotificationManager", e);
}
VideoCastManager.getInstance().addVideoCastConsumer(mCastConsumer);

if (!TvHelper.isTvUiMode(this)) {
mCastSessionManager = CastContext.getSharedInstance(this).getSessionManager();
mCastSessionManagerListener = new CastSessionManagerListener();
mCastSessionManager.addSessionManagerListener(mCastSessionManagerListener,
CastSession.class);
}

mMediaRouter = MediaRouter.getInstance(getApplicationContext());

registerCarConnectionReceiver();
Expand All @@ -277,7 +247,7 @@ public int onStartCommand(Intent startIntent, int flags, int startId) {
if (CMD_PAUSE.equals(command)) {
mPlaybackManager.handlePauseRequest();
} else if (CMD_STOP_CASTING.equals(command)) {
VideoCastManager.getInstance().disconnect();
CastContext.getSharedInstance(this).getSessionManager().endCurrentSession(true);
}
} else {
// Try to handle the intent as a media button event wrapped by MediaButtonReceiver
Expand All @@ -302,7 +272,12 @@ public void onDestroy() {
// Service is being killed, so make sure we release our resources
mPlaybackManager.handleStopRequest(null);
mMediaNotificationManager.stopNotification();
VideoCastManager.getInstance().removeVideoCastConsumer(mCastConsumer);

if (mCastSessionManager != null) {
mCastSessionManager.removeSessionManagerListener(mCastSessionManagerListener,
CastSession.class);
}

mDelayedStopHandler.removeCallbacksAndMessages(null);
mSession.release();
}
Expand Down Expand Up @@ -439,4 +414,66 @@ public void handleMessage(Message msg) {
}
}
}

/**
* Session Manager Listener responsible for switching the Playback instances
* depending on whether it is connected to a remote player.
*/
private class CastSessionManagerListener implements SessionManagerListener<CastSession> {

@Override
public void onSessionEnded(CastSession session, int error) {
LogHelper.d(TAG, "onSessionEnded");
mSessionExtras.remove(EXTRA_CONNECTED_CAST);
mSession.setExtras(mSessionExtras);
Playback playback = new LocalPlayback(MusicService.this, mMusicProvider);
mMediaRouter.setMediaSessionCompat(null);
mPlaybackManager.switchToPlayback(playback, false);
}

@Override
public void onSessionResumed(CastSession session, boolean wasSuspended) {
}

@Override
public void onSessionStarted(CastSession session, String sessionId) {
// In case we are casting, send the device name as an extra on MediaSession metadata.
mSessionExtras.putString(EXTRA_CONNECTED_CAST,
session.getCastDevice().getFriendlyName());
mSession.setExtras(mSessionExtras);
// Now we can switch to CastPlayback
Playback playback = new CastPlayback(mMusicProvider, MusicService.this);
mMediaRouter.setMediaSessionCompat(mSession);
mPlaybackManager.switchToPlayback(playback, true);
}

@Override
public void onSessionStarting(CastSession session) {
}

@Override
public void onSessionStartFailed(CastSession session, int error) {
}

@Override
public void onSessionEnding(CastSession session) {
// This is our final chance to update the underlying stream position
// In onSessionEnded(), the underlying CastPlayback#mRemoteMediaClient
// is disconnected and hence we update our local value of stream position
// to the latest position.
mPlaybackManager.getPlayback().updateLastKnownStreamPosition();
}

@Override
public void onSessionResuming(CastSession session, String sessionId) {
}

@Override
public void onSessionResumeFailed(CastSession session, int error) {
}

@Override
public void onSessionSuspended(CastSession session, int reason) {
}
}
}
42 changes: 0 additions & 42 deletions mobile/src/main/java/com/example/android/uamp/UAMPApplication.java

This file was deleted.

Loading

3 comments on commit 39fa286

@loki666
Copy link

@loki666 loki666 commented on 39fa286 Dec 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @smishra2 , are you still maintining this project?

@smishra2
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. I am one of many maintainers on this project.

@loki666
Copy link

@loki666 loki666 commented on 39fa286 Dec 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I was wondering, because Issues are accumulating on this project, but no one seems to answer or try to resolve them.

Please sign in to comment.