From be9219c4610a31f7a74ba9889a21cc1e0daff2a7 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sat, 22 Feb 2014 11:06:44 -0800 Subject: #285 Add support for offline ChromeCast --- .../daneren2005/dsub/fragments/MainFragment.java | 4 ++ .../dsub/service/ChromeCastController.java | 59 +++++++++++++++++++--- .../daneren2005/dsub/service/DownloadService.java | 9 ++++ .../daneren2005/dsub/util/MediaRouteManager.java | 19 +++++-- 4 files changed, 82 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/fragments/MainFragment.java b/src/github/daneren2005/dsub/fragments/MainFragment.java index 1905e77a..20d4d9a5 100644 --- a/src/github/daneren2005/dsub/fragments/MainFragment.java +++ b/src/github/daneren2005/dsub/fragments/MainFragment.java @@ -208,6 +208,10 @@ public class MainFragment extends SubsonicFragment { boolean isOffline = Util.isOffline(context); Util.setOffline(context, !isOffline); context.invalidate(); + DownloadService service = getDownloadService(); + if (service != null) { + service.setOnline(isOffline); + } if(isOffline) { int scrobblesCount = Util.offlineScrobblesCount(context); diff --git a/src/github/daneren2005/dsub/service/ChromeCastController.java b/src/github/daneren2005/dsub/service/ChromeCastController.java index dc360797..3cfde4c6 100644 --- a/src/github/daneren2005/dsub/service/ChromeCastController.java +++ b/src/github/daneren2005/dsub/service/ChromeCastController.java @@ -15,6 +15,7 @@ package github.daneren2005.dsub.service; +import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; import android.util.Log; @@ -32,14 +33,17 @@ import com.google.android.gms.common.api.ResultCallback; import com.google.android.gms.common.api.Status; import com.google.android.gms.common.images.WebImage; +import java.io.File; import java.io.IOException; import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.MusicDirectory; import github.daneren2005.dsub.domain.PlayerState; import github.daneren2005.dsub.util.Constants; +import github.daneren2005.dsub.util.FileUtil; import github.daneren2005.dsub.util.Util; import github.daneren2005.dsub.util.compat.CastCompat; +import github.daneren2005.serverproxy.FileProxy; /** * Created by owner on 2/9/14. @@ -58,12 +62,17 @@ public class ChromeCastController extends RemoteController { private boolean error = false; private boolean ignoreNextPaused = false; + private FileProxy proxy; + private String rootLocation; private RemoteMediaPlayer mediaPlayer; private double gain = 0.5; public ChromeCastController(DownloadService downloadService, CastDevice castDevice) { this.downloadService = downloadService; this.castDevice = castDevice; + + SharedPreferences prefs = Util.getPreferences(downloadService); + rootLocation = prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null); } @Override @@ -154,6 +163,11 @@ public class ChromeCastController extends RemoteController { apiClient.disconnect(); } apiClient = null; + + if(proxy != null) { + proxy.stop(); + proxy = null; + } } @Override @@ -209,9 +223,30 @@ public class ChromeCastController extends RemoteController { try { MusicService musicService = MusicServiceFactory.getMusicService(downloadService); - String url = song.isVideo() ? musicService.getHlsUrl(song.getId(), currentPlaying.getBitRate(), downloadService) : musicService.getMusicUrl(downloadService, song, currentPlaying.getBitRate()); - // Use separate profile for Chromecast so users can do ogg on phone, mp3 for CC - url = url.replace(Constants.REST_CLIENT_ID, Constants.CHROMECAST_CLIENT_ID); + String url; + // Offline, use file proxy + if(Util.isOffline(downloadService) || song.getId().indexOf(rootLocation) != -1) { + if(proxy == null) { + proxy = new FileProxy(downloadService); + proxy.start(); + } + + url = proxy.getPublicAddress(song.getId()); + } else { + if(proxy != null) { + proxy.stop(); + proxy = null; + } + + if(song.isVideo()) { + url = musicService.getHlsUrl(song.getId(), currentPlaying.getBitRate(), downloadService); + } else { + url = musicService.getMusicUrl(downloadService, song, currentPlaying.getBitRate()); + } + + // Use separate profile for Chromecast so users can do ogg on phone, mp3 for CC + url = url.replace(Constants.REST_CLIENT_ID, Constants.CHROMECAST_CLIENT_ID); + } // Setup song/video information MediaMetadata meta = new MediaMetadata(song.isVideo() ? MediaMetadata.MEDIA_TYPE_MOVIE : MediaMetadata.MEDIA_TYPE_MUSIC_TRACK); @@ -223,8 +258,18 @@ public class ChromeCastController extends RemoteController { meta.putString(MediaMetadata.KEY_ARTIST, song.getArtist()); meta.putString(MediaMetadata.KEY_ALBUM_ARTIST, song.getArtist()); meta.putString(MediaMetadata.KEY_ALBUM_TITLE, song.getAlbum()); - String coverArt = musicService.getCoverArtUrl(downloadService, song); - meta.addImage(new WebImage(Uri.parse(coverArt))); + + String coverArt; + if(proxy == null) { + coverArt = musicService.getCoverArtUrl(downloadService, song); + meta.addImage(new WebImage(Uri.parse(coverArt))); + } else { + File coverArtFile = FileUtil.getAlbumArtFile(downloadService, song); + if(coverArtFile != null && coverArtFile.exists()) { + coverArt = coverArtFile.getPath(); + meta.addImage(new WebImage(Uri.parse(proxy.getPublicAddress(coverArt)))); + } + } } String contentType; @@ -233,8 +278,10 @@ public class ChromeCastController extends RemoteController { } else if(song.getTranscodedContentType() != null) { contentType = song.getTranscodedContentType(); - } else { + } else if(song.getContentType() != null) { contentType = song.getContentType(); + } else { + contentType = "audio/mpeg"; } // Load it into a MediaInfo wrapper diff --git a/src/github/daneren2005/dsub/service/DownloadService.java b/src/github/daneren2005/dsub/service/DownloadService.java index 5e02641a..b3a989ed 100644 --- a/src/github/daneren2005/dsub/service/DownloadService.java +++ b/src/github/daneren2005/dsub/service/DownloadService.java @@ -523,6 +523,15 @@ public class DownloadService extends Service { updateJukeboxPlaylist(); } + public synchronized void setOnline(boolean online) { + if(online) { + mediaRouter.addOfflineProviders(); + } else { + mediaRouter.removeOfflineProviders(); + clearIncomplete(); + } + } + public synchronized int size() { return downloadList.size(); } diff --git a/src/github/daneren2005/dsub/util/MediaRouteManager.java b/src/github/daneren2005/dsub/util/MediaRouteManager.java index 4a124402..d41a5516 100644 --- a/src/github/daneren2005/dsub/util/MediaRouteManager.java +++ b/src/github/daneren2005/dsub/util/MediaRouteManager.java @@ -42,6 +42,7 @@ public class MediaRouteManager extends MediaRouter.Callback { private MediaRouter router; private MediaRouteSelector selector; private List providers = new ArrayList(); + private List offlineProviders = new ArrayList(); static { try { @@ -116,10 +117,22 @@ public class MediaRouteManager extends MediaRouter.Callback { } } + public void addOfflineProviders() { + JukeboxRouteProvider jukeboxProvider = new JukeboxRouteProvider(downloadService); + router.addProvider(jukeboxProvider); + providers.add(jukeboxProvider); + offlineProviders.add(jukeboxProvider); + } + public void removeOfflineProviders() { + for(MediaRouteProvider provider: offlineProviders) { + router.removeProvider(provider); + } + } + private void addProviders() { - JukeboxRouteProvider routeProvider = new JukeboxRouteProvider(downloadService); - router.addProvider(routeProvider); - providers.add(routeProvider); + if(!Util.isOffline(downloadService)) { + addOfflineProviders(); + } } private void buildSelector() { MediaRouteSelector.Builder builder = new MediaRouteSelector.Builder(); -- cgit v1.2.3