intents for cast player activity

This commit is contained in:
Domingos Lopes 2016-04-17 02:10:54 -04:00
parent 982142634f
commit 376ffed56c
2 changed files with 36 additions and 23 deletions

View File

@ -5,6 +5,7 @@ import android.content.Intent;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.AudioplayerActivity; import de.danoeh.antennapod.activity.AudioplayerActivity;
import de.danoeh.antennapod.activity.CastplayerActivity;
import de.danoeh.antennapod.activity.VideoplayerActivity; import de.danoeh.antennapod.activity.VideoplayerActivity;
import de.danoeh.antennapod.core.PlaybackServiceCallbacks; import de.danoeh.antennapod.core.PlaybackServiceCallbacks;
import de.danoeh.antennapod.core.feed.MediaType; import de.danoeh.antennapod.core.feed.MediaType;
@ -14,8 +15,7 @@ public class PlaybackServiceCallbacksImpl implements PlaybackServiceCallbacks {
@Override @Override
public Intent getPlayerActivityIntent(Context context, MediaType mediaType, boolean remotePlayback) { public Intent getPlayerActivityIntent(Context context, MediaType mediaType, boolean remotePlayback) {
if (remotePlayback) { if (remotePlayback) {
// TODO possibly switch to a proper cast activity return new Intent(context, CastplayerActivity.class);
return new Intent(context, AudioplayerActivity.class);
} }
if (mediaType == MediaType.VIDEO) { if (mediaType == MediaType.VIDEO) {
return new Intent(context, VideoplayerActivity.class); return new Intent(context, VideoplayerActivity.class);

View File

@ -27,6 +27,7 @@ import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat; import android.support.v4.media.session.PlaybackStateCompat;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v7.app.NotificationCompat; import android.support.v7.app.NotificationCompat;
import android.support.v7.media.MediaRouter;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
@ -204,6 +205,9 @@ public class PlaybackService extends Service {
private PlaybackServiceMediaPlayer mediaPlayer; private PlaybackServiceMediaPlayer mediaPlayer;
private PlaybackServiceTaskManager taskManager; private PlaybackServiceTaskManager taskManager;
private CastManager mCastMgr;
private MediaRouter mMediaRouter;
/** /**
* Only used for Lollipop notifications. * Only used for Lollipop notifications.
*/ */
@ -277,20 +281,10 @@ public class PlaybackService extends Service {
registerReceiver(pauseResumeCurrentEpisodeReceiver, new IntentFilter( registerReceiver(pauseResumeCurrentEpisodeReceiver, new IntentFilter(
ACTION_RESUME_PLAY_CURRENT_EPISODE)); ACTION_RESUME_PLAY_CURRENT_EPISODE));
taskManager = new PlaybackServiceTaskManager(this, taskManagerCallback); taskManager = new PlaybackServiceTaskManager(this, taskManagerCallback);
mMediaRouter = MediaRouter.getInstance(getApplicationContext());
PreferenceManager.getDefaultSharedPreferences(this) PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(prefListener); .registerOnSharedPreferenceChangeListener(prefListener);
CastManager castMgr = CastManager.getInstance();
castMgr.addCastConsumer(castConsumer);
isCasting = castMgr.isConnected();
if (isCasting) {
if (UserPreferences.isCastEnabled()) {
onCastAppConnected(false);
} else {
castMgr.disconnect();
}
} else {
mediaPlayer = new LocalPSMP(this, mediaPlayerCallback);
}
ComponentName eventReceiver = new ComponentName(getApplicationContext(), ComponentName eventReceiver = new ComponentName(getApplicationContext(),
MediaButtonReceiver.class); MediaButtonReceiver.class);
@ -303,7 +297,6 @@ public class PlaybackService extends Service {
try { try {
mediaSession.setCallback(sessionCallback); mediaSession.setCallback(sessionCallback);
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setActive(true);
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
// on some devices (Huawei) setting active can cause a NullPointerException // on some devices (Huawei) setting active can cause a NullPointerException
// even with correct use of the api. // even with correct use of the api.
@ -312,6 +305,21 @@ public class PlaybackService extends Service {
Log.e(TAG, "NullPointerException while setting up MediaSession"); Log.e(TAG, "NullPointerException while setting up MediaSession");
npe.printStackTrace(); npe.printStackTrace();
} }
mCastMgr = CastManager.getInstance();
mCastMgr.addCastConsumer(castConsumer);
isCasting = mCastMgr.isConnected();
if (isCasting) {
if (UserPreferences.isCastEnabled()) {
onCastAppConnected(false);
} else {
mCastMgr.disconnect();
}
} else {
mediaPlayer = new LocalPSMP(this, mediaPlayerCallback);
}
mediaSession.setActive(true);
} }
@Override @Override
@ -336,7 +344,7 @@ public class PlaybackService extends Service {
unregisterReceiver(skipCurrentEpisodeReceiver); unregisterReceiver(skipCurrentEpisodeReceiver);
unregisterReceiver(pausePlayCurrentEpisodeReceiver); unregisterReceiver(pausePlayCurrentEpisodeReceiver);
unregisterReceiver(pauseResumeCurrentEpisodeReceiver); unregisterReceiver(pauseResumeCurrentEpisodeReceiver);
CastManager.getInstance().removeCastConsumer(castConsumer); mCastMgr.removeCastConsumer(castConsumer);
unregisterWifiBroadcastReceiver(); unregisterWifiBroadcastReceiver();
mediaPlayer.shutdown(); mediaPlayer.shutdown();
taskManager.shutdown(); taskManager.shutdown();
@ -379,7 +387,7 @@ public class PlaybackService extends Service {
sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, 0); sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, 0);
//If the user asks to play External Media, the casting session, if on, should end. //If the user asks to play External Media, the casting session, if on, should end.
if (playable instanceof ExternalMedia) { if (playable instanceof ExternalMedia) {
CastManager.getInstance().disconnect(); mCastMgr.disconnect();
} }
mediaPlayer.playMediaObject(playable, stream, startWhenPrepared, prepareImmediately); mediaPlayer.playMediaObject(playable, stream, startWhenPrepared, prepareImmediately);
} }
@ -1346,6 +1354,10 @@ public class PlaybackService extends Service {
return currentMediaType; return currentMediaType;
} }
public static boolean isCasting() {
return isCasting;
}
public void resume() { public void resume() {
mediaPlayer.resume(); mediaPlayer.resume();
} }
@ -1603,6 +1615,7 @@ public class PlaybackService extends Service {
Log.d(TAG, "Cast session disconnected, but no current media"); Log.d(TAG, "Cast session disconnected, but no current media");
sendNotificationBroadcast(NOTIFICATION_TYPE_PLAYBACK_END, 0); sendNotificationBroadcast(NOTIFICATION_TYPE_PLAYBACK_END, 0);
} }
mMediaRouter.setMediaSessionCompat(null);
unregisterWifiBroadcastReceiver(); unregisterWifiBroadcastReceiver();
} }
}; };
@ -1624,6 +1637,8 @@ public class PlaybackService extends Service {
new PlaybackServiceMediaPlayer.PSMPInfo(PlayerStatus.STOPPED, null), new PlaybackServiceMediaPlayer.PSMPInfo(PlayerStatus.STOPPED, null),
wasLaunched); wasLaunched);
sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, EXTRA_CODE_CAST); sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, EXTRA_CODE_CAST);
// hardware volume buttons control the remote device volume
mMediaRouter.setMediaSessionCompat(mediaSession);
registerWifiBroadcastReceiver(); registerWifiBroadcastReceiver();
} }
@ -1665,9 +1680,8 @@ public class PlaybackService extends Service {
//apparently this method gets called twice when a change happens, but one run is enough. //apparently this method gets called twice when a change happens, but one run is enough.
if (isConnected && !mWifiConnectivity) { if (isConnected && !mWifiConnectivity) {
mWifiConnectivity = true; mWifiConnectivity = true;
CastManager castMgr = CastManager.getInstance(); mCastMgr.startCastDiscovery();
castMgr.startCastDiscovery(); mCastMgr.reconnectSessionIfPossible(RECONNECTION_ATTEMPT_PERIOD_S, NetworkUtils.getWifiSsid());
castMgr.reconnectSessionIfPossible(RECONNECTION_ATTEMPT_PERIOD_S, NetworkUtils.getWifiSsid());
} else { } else {
mWifiConnectivity = isConnected; mWifiConnectivity = isConnected;
} }
@ -1689,10 +1703,9 @@ public class PlaybackService extends Service {
(sharedPreferences, key) -> { (sharedPreferences, key) -> {
if (UserPreferences.PREF_CAST_ENABLED.equals(key)) { if (UserPreferences.PREF_CAST_ENABLED.equals(key)) {
if (!UserPreferences.isCastEnabled()) { if (!UserPreferences.isCastEnabled()) {
CastManager castManager = CastManager.getInstance(); if (mCastMgr.isConnecting() || mCastMgr.isConnected()) {
if (castManager.isConnecting() || castManager.isConnected()) {
Log.d(TAG, "Disconnecting cast device due to a change in user preferences"); Log.d(TAG, "Disconnecting cast device due to a change in user preferences");
castManager.disconnect(); mCastMgr.disconnect();
} }
} }
} else if (UserPreferences.PREF_LOCKSCREEN_BACKGROUND.equals(key)) { } else if (UserPreferences.PREF_LOCKSCREEN_BACKGROUND.equals(key)) {