From d514de5765855fc41674a3f0d4d48d057c8528e7 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sun, 16 Feb 2014 09:55:37 -0800 Subject: #280 Fix MediaRoute state not being restored --- .../dsub/service/DownloadServiceImpl.java | 44 ++++++++++++++++++++-- src/github/daneren2005/dsub/util/Constants.java | 1 + .../daneren2005/dsub/util/MediaRouteManager.java | 32 +++++++++++++++- 3 files changed, 71 insertions(+), 6 deletions(-) (limited to 'src/github/daneren2005') diff --git a/src/github/daneren2005/dsub/service/DownloadServiceImpl.java b/src/github/daneren2005/dsub/service/DownloadServiceImpl.java index 6ea98302..d28c5abf 100644 --- a/src/github/daneren2005/dsub/service/DownloadServiceImpl.java +++ b/src/github/daneren2005/dsub/service/DownloadServiceImpl.java @@ -18,6 +18,7 @@ */ package github.daneren2005.dsub.service; +import static android.support.v7.media.MediaRouter.RouteInfo; import static github.daneren2005.dsub.domain.PlayerState.COMPLETED; import static github.daneren2005.dsub.domain.PlayerState.DOWNLOADING; import static github.daneren2005.dsub.domain.PlayerState.IDLE; @@ -65,6 +66,7 @@ import android.os.IBinder; import android.os.Looper; import android.os.PowerManager; import android.support.v7.media.MediaRouteSelector; +import android.support.v7.media.MediaRouter; import android.util.Log; import android.support.v4.util.LruCache; import java.net.URLEncoder; @@ -204,6 +206,8 @@ public class DownloadServiceImpl extends Service implements DownloadService { keepScreenOn = prefs.getBoolean(Constants.PREFERENCES_KEY_KEEP_SCREEN_ON, false); + mediaRouter = new MediaRouteManager(this); + instance = this; lifecycleSupport.onCreate(); @@ -214,8 +218,6 @@ public class DownloadServiceImpl extends Service implements DownloadService { getVisualizerController(); showVisualization = true; } - - mediaRouter = new MediaRouteManager(this); } @Override @@ -373,7 +375,8 @@ public class DownloadServiceImpl extends Service implements DownloadService { SharedPreferences prefs = Util.getPreferences(this); remoteState = RemoteControlState.values()[prefs.getInt(Constants.PREFERENCES_KEY_CONTROL_MODE, 0)]; if(remoteState != RemoteControlState.LOCAL) { - setRemoteState(remoteState, null); + String id = prefs.getString(Constants.PREFERENCES_KEY_CONTROL_ID, null); + setRemoteState(remoteState, null, id); } boolean startShufflePlay = prefs.getBoolean(Constants.PREFERENCES_KEY_SHUFFLE_MODE, false); download(songs, false, false, false, false); @@ -936,7 +939,10 @@ public class DownloadServiceImpl extends Service implements DownloadService { bufferTask.cancel(); } try { - setPlayerState(IDLE); + // Only set to idle if it's not being killed to start RemoteController + if(remoteState == RemoteControlState.LOCAL) { + setPlayerState(IDLE); + } mediaPlayer.setOnErrorListener(null); mediaPlayer.setOnCompletionListener(null); mediaPlayer.reset(); @@ -1143,11 +1149,18 @@ public class DownloadServiceImpl extends Service implements DownloadService { public void setRemoteEnabled(RemoteControlState newState, Object ref) { setRemoteState(newState, ref); + RouteInfo info = mediaRouter.getSelectedRoute(); + String routeId = info.getId(); + SharedPreferences.Editor editor = Util.getPreferences(this).edit(); editor.putInt(Constants.PREFERENCES_KEY_CONTROL_MODE, newState.getValue()); + editor.putString(Constants.PREFERENCES_KEY_CONTROL_ID, routeId); editor.commit(); } private void setRemoteState(RemoteControlState newState, Object ref) { + setRemoteState(newState, ref, null); + } + private void setRemoteState(final RemoteControlState newState, final Object ref, final String routeId) { if(remoteController != null) { remoteController.stop(); setPlayerState(PlayerState.IDLE); @@ -1187,6 +1200,23 @@ public class DownloadServiceImpl extends Service implements DownloadService { } else { Util.hidePlayingNotification(this, this, handler); } + + if(routeId != null) { + handler.post(new Runnable() { + @Override + public void run() { + RouteInfo info = mediaRouter.getRouteForId(routeId); + if(info == null) { + setRemoteState(RemoteControlState.LOCAL, null); + } else if(newState == RemoteControlState.CHROMECAST) { + RemoteController controller = mediaRouter.getRemoteController(info); + if(controller != null) { + setRemoteState(RemoteControlState.CHROMECAST, controller); + } + } + } + }); + } } @Override @@ -1301,6 +1331,12 @@ public class DownloadServiceImpl extends Service implements DownloadService { nextMediaPlayer.release(); nextMediaPlayer = null; } + + // Exit when using remote controllers + if(remoteState != RemoteControlState.LOCAL) { + return; + } + nextMediaPlayer = new MediaPlayer(); nextMediaPlayer.setWakeMode(DownloadServiceImpl.this, PowerManager.PARTIAL_WAKE_LOCK); try { diff --git a/src/github/daneren2005/dsub/util/Constants.java b/src/github/daneren2005/dsub/util/Constants.java index 2b96ac4c..1818c266 100644 --- a/src/github/daneren2005/dsub/util/Constants.java +++ b/src/github/daneren2005/dsub/util/Constants.java @@ -119,6 +119,7 @@ public final class Constants { public static final String PREFERENCES_KEY_CHAT_ENABLED = "chatEnabled"; public static final String PREFERENCES_KEY_VIDEO_PLAYER = "videoPlayer"; public static final String PREFERENCES_KEY_CONTROL_MODE = "remoteControlMode"; + public static final String PREFERENCES_KEY_CONTROL_ID = "remoteControlId"; public static final String PREFERENCES_KEY_SYNC_ENABLED = "syncEnabled"; public static final String PREFERENCES_KEY_SYNC_INTERVAL = "syncInterval"; public static final String PREFERENCES_KEY_SYNC_WIFI = "syncWifi"; diff --git a/src/github/daneren2005/dsub/util/MediaRouteManager.java b/src/github/daneren2005/dsub/util/MediaRouteManager.java index 9d68f014..a1bd9b6b 100644 --- a/src/github/daneren2005/dsub/util/MediaRouteManager.java +++ b/src/github/daneren2005/dsub/util/MediaRouteManager.java @@ -30,6 +30,8 @@ import github.daneren2005.dsub.service.DownloadServiceImpl; import github.daneren2005.dsub.service.RemoteController; import github.daneren2005.dsub.util.compat.CastCompat; +import static android.support.v7.media.MediaRouter.RouteInfo; + /** * Created by owner on 2/8/14. */ @@ -65,7 +67,7 @@ public class MediaRouteManager extends MediaRouter.Callback { } @Override - public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo info) { + public void onRouteSelected(MediaRouter router, RouteInfo info) { if(castAvailable) { RemoteController controller = CastCompat.getController(downloadService, info); if(controller != null) { @@ -74,7 +76,7 @@ public class MediaRouteManager extends MediaRouter.Callback { } } @Override - public void onRouteUnselected(MediaRouter router, MediaRouter.RouteInfo info) { + public void onRouteUnselected(MediaRouter router, RouteInfo info) { downloadService.setRemoteEnabled(RemoteControlState.LOCAL); } @@ -89,6 +91,32 @@ public class MediaRouteManager extends MediaRouter.Callback { return selector; } + public RouteInfo getSelectedRoute() { + return router.getSelectedRoute(); + } + public RouteInfo getRouteForId(String id) { + if(id == null) { + return null; + } + + // Try to find matching id + for(RouteInfo info: router.getRoutes()) { + if(id.equals(info.getId())) { + router.selectRoute(info); + return info; + } + } + + return null; + } + public RemoteController getRemoteController(RouteInfo info) { + if(castAvailable) { + return CastCompat.getController(downloadService, info); + } else { + return null; + } + } + private void addProviders() { JukeboxRouteProvider routeProvider = new JukeboxRouteProvider(downloadService); router.addProvider(routeProvider); -- cgit v1.2.3