diff options
author | Scott Jackson <daneren2005@gmail.com> | 2015-10-22 17:52:02 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2015-10-22 17:52:02 -0700 |
commit | 105e63e74994004c510fdada63d03086bbb56752 (patch) | |
tree | 3cb9284deb90e47da8f7d85d699c4718219a4a0e /app/src/main | |
parent | 15e0bf9982b5235b3934c05093ff6b517524d86b (diff) | |
download | dsub-105e63e74994004c510fdada63d03086bbb56752.tar.gz dsub-105e63e74994004c510fdada63d03086bbb56752.tar.bz2 dsub-105e63e74994004c510fdada63d03086bbb56752.zip |
#480 Add playlists browsing from Auto
Diffstat (limited to 'app/src/main')
3 files changed, 191 insertions, 22 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/service/AutoMediaBrowserService.java b/app/src/main/java/github/daneren2005/dsub/service/AutoMediaBrowserService.java index 4d21d793..d579ef54 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/AutoMediaBrowserService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/AutoMediaBrowserService.java @@ -22,9 +22,9 @@ import android.annotation.TargetApi; import android.content.Intent; import android.media.MediaDescription; import android.media.browse.MediaBrowser; -import android.media.session.MediaSession; import android.os.Build; import android.os.Bundle; +import android.os.Handler; import android.service.media.MediaBrowserService; import android.support.annotation.Nullable; import android.util.Log; @@ -32,6 +32,12 @@ import android.util.Log; import java.util.ArrayList; import java.util.List; +import github.daneren2005.dsub.R; +import github.daneren2005.dsub.domain.Playlist; +import github.daneren2005.dsub.domain.ServerInfo; +import github.daneren2005.dsub.util.Constants; +import github.daneren2005.dsub.util.SilentBackgroundTask; +import github.daneren2005.dsub.util.SilentServiceTask; import github.daneren2005.dsub.util.Util; import github.daneren2005.dsub.util.compat.RemoteControlClientLP; @@ -39,18 +45,22 @@ import github.daneren2005.dsub.util.compat.RemoteControlClientLP; public class AutoMediaBrowserService extends MediaBrowserService { private static final String TAG = AutoMediaBrowserService.class.getSimpleName(); private static final String BROWSER_ROOT = "root"; - MediaSession mediaSession; + private static final String BROWSER_ALBUM_LISTS = "albumLists"; + private static final String BROWSER_LIBRARY = "library"; + private static final String BROWSER_PLAYLISTS = "playlists"; + private static final String PLAYLIST_PREFIX = "pl-"; + private static final String ALBUM_TYPE_PREFIX = "ty-"; + + private DownloadService downloadService; + private Handler handler = new Handler(); @Override public void onCreate() { super.onCreate(); - - DownloadService downloadService = getDownloadService(); - RemoteControlClientLP remoteControlClient = (RemoteControlClientLP) downloadService.getRemoteControlClient(); - setSessionToken(remoteControlClient.getMediaSession().getSessionToken()); + getDownloadService(); } - @Nullable + @Nullable @Override public BrowserRoot onGetRoot(String clientPackageName, int clientUid, Bundle rootHints) { BrowserRoot root = new BrowserRoot(BROWSER_ROOT, null); @@ -61,34 +71,161 @@ public class AutoMediaBrowserService extends MediaBrowserService { public void onLoadChildren(String parentId, Result<List<MediaBrowser.MediaItem>> result) { if(BROWSER_ROOT.equals(parentId)) { getRootFolders(result); + } else if(BROWSER_ALBUM_LISTS.equals(parentId)) { + getAlbumLists(result); + } else if(parentId.startsWith(ALBUM_TYPE_PREFIX)) { + int id = Integer.valueOf(parentId.substring(ALBUM_TYPE_PREFIX.length())); + getAlbumList(result, id); + } else if(BROWSER_LIBRARY.equals(parentId)) { + getLibrary(result); + } else if(BROWSER_PLAYLISTS.equals(parentId)) { + getPlaylists(result); + } else if(parentId.startsWith(PLAYLIST_PREFIX)) { + getPlayOptions(result, parentId.substring(PLAYLIST_PREFIX.length()), Constants.INTENT_EXTRA_NAME_PLAYLIST_ID); } else { - + // No idea what it is, send empty result + result.sendResult(new ArrayList<MediaBrowser.MediaItem>()); } } private void getRootFolders(Result<List<MediaBrowser.MediaItem>> result) { List<MediaBrowser.MediaItem> mediaItems = new ArrayList<>(); + /*MediaDescription.Builder albumLists = new MediaDescription.Builder(); + albumLists.setTitle(downloadService.getString(R.string.main_albums_title)) + .setMediaId(BROWSER_ALBUM_LISTS); + mediaItems.add(new MediaBrowser.MediaItem(albumLists.build(), MediaBrowser.MediaItem.FLAG_BROWSABLE)); + MediaDescription.Builder library = new MediaDescription.Builder(); - library.setDescription("Library") - .setMediaId("library"); - mediaItems.add(new MediaBrowser.MediaItem(library.build(), MediaBrowser.MediaItem.FLAG_BROWSABLE)); + library.setTitle(downloadService.getString(R.string.button_bar_browse)) + .setMediaId(BROWSER_LIBRARY); + mediaItems.add(new MediaBrowser.MediaItem(library.build(), MediaBrowser.MediaItem.FLAG_BROWSABLE));*/ + + MediaDescription.Builder playlists = new MediaDescription.Builder(); + playlists.setTitle(downloadService.getString(R.string.button_bar_playlists)) + .setMediaId(BROWSER_PLAYLISTS); + mediaItems.add(new MediaBrowser.MediaItem(playlists.build(), MediaBrowser.MediaItem.FLAG_BROWSABLE)); result.sendResult(mediaItems); } - public DownloadService getDownloadService() { - // If service is not available, request it to start and wait for it. - for (int i = 0; i < 5; i++) { - DownloadService downloadService = DownloadService.getInstance(); - if (downloadService != null) { - break; + private void getAlbumLists(Result<List<MediaBrowser.MediaItem>> result) { + List<Integer> albums = new ArrayList<>(); + albums.add(R.string.main_albums_newest); + albums.add(R.string.main_albums_random); + if(ServerInfo.checkServerVersion(downloadService, "1.8")) { + albums.add(R.string.main_albums_alphabetical); + } + if(!Util.isTagBrowsing(downloadService)) { + albums.add(R.string.main_albums_highest); + } + // albums.add(R.string.main_albums_starred); + // albums.add(R.string.main_albums_genres); + // albums.add(R.string.main_albums_year); + albums.add(R.string.main_albums_recent); + albums.add(R.string.main_albums_frequent); + + List<MediaBrowser.MediaItem> mediaItems = new ArrayList<>(); + + for(Integer id: albums) { + MediaDescription description = new MediaDescription.Builder() + .setTitle(downloadService.getResources().getString(id)) + .setMediaId(ALBUM_TYPE_PREFIX + id) + .build(); + + mediaItems.add(new MediaBrowser.MediaItem(description, MediaBrowser.MediaItem.FLAG_BROWSABLE)); + } + + result.sendResult(mediaItems); + } + private void getAlbumList(Result<List<MediaBrowser.MediaItem>> result, int id) { + + } + + private void getLibrary(Result<List<MediaBrowser.MediaItem>> result) { + + } + + private void getPlaylists(final Result<List<MediaBrowser.MediaItem>> result) { + new SilentServiceTask<List<Playlist>>(downloadService) { + @Override + protected List<Playlist> doInBackground(MusicService musicService) throws Throwable { + return musicService.getPlaylists(false, downloadService, null); } - Log.w(TAG, "DownloadService not running. Attempting to start it."); + + @Override + protected void done(List<Playlist> playlists) { + List<MediaBrowser.MediaItem> mediaItems = new ArrayList<>(); + + for(Playlist playlist: playlists) { + MediaDescription description = new MediaDescription.Builder() + .setTitle(playlist.getName()) + .setMediaId(PLAYLIST_PREFIX + playlist.getId()) + .build(); + + mediaItems.add(new MediaBrowser.MediaItem(description, MediaBrowser.MediaItem.FLAG_BROWSABLE)); + } + + result.sendResult(mediaItems); + } + }.execute(); + + result.detach(); + } + private void getPlayOptions(Result<List<MediaBrowser.MediaItem>> result, String id, String idConstant) { + List<MediaBrowser.MediaItem> mediaItems = new ArrayList<>(); + + Bundle playAllExtras = new Bundle(); + playAllExtras.putString(idConstant, id); + + MediaDescription.Builder playAll = new MediaDescription.Builder(); + playAll.setTitle(downloadService.getString(R.string.menu_play)) + .setMediaId("play-" + id) + .setExtras(playAllExtras); + mediaItems.add(new MediaBrowser.MediaItem(playAll.build(), MediaBrowser.MediaItem.FLAG_PLAYABLE)); + + Bundle shuffleExtras = new Bundle(); + shuffleExtras.putString(idConstant, id); + shuffleExtras.putBoolean(Constants.INTENT_EXTRA_NAME_SHUFFLE, true); + + MediaDescription.Builder shuffle = new MediaDescription.Builder(); + shuffle.setTitle(downloadService.getString(R.string.menu_shuffle)) + .setMediaId("shuffle-" + id) + .setExtras(shuffleExtras); + mediaItems.add(new MediaBrowser.MediaItem(shuffle.build(), MediaBrowser.MediaItem.FLAG_PLAYABLE)); + + /*Bundle playLastExtras = new Bundle(); + playLastExtras.putString(idConstant, id); + playLastExtras.putBoolean(Constants.INTENT_EXTRA_PLAY_LAST, true); + + MediaDescription.Builder playLast = new MediaDescription.Builder(); + playLast.setTitle(downloadService.getString(R.string.menu_play_last)) + .setMediaId("playLast-" + id) + .setExtras(playLastExtras); + mediaItems.add(new MediaBrowser.MediaItem(playLast.build(), MediaBrowser.MediaItem.FLAG_PLAYABLE));*/ + + result.sendResult(mediaItems); + } + + public void getDownloadService() { + if(DownloadService.getInstance() == null) { startService(new Intent(this, DownloadService.class)); - Util.sleepQuietly(50L); } - return DownloadService.getInstance(); + waitForDownloadService(); + } + public void waitForDownloadService() { + downloadService = DownloadService.getInstance(); + if(downloadService == null) { + handler.postDelayed(new Runnable() { + @Override + public void run() { + waitForDownloadService(); + } + }, 100); + } else { + RemoteControlClientLP remoteControlClient = (RemoteControlClientLP) downloadService.getRemoteControlClient(); + setSessionToken(remoteControlClient.getMediaSession().getSessionToken()); + } } } diff --git a/app/src/main/java/github/daneren2005/dsub/util/Constants.java b/app/src/main/java/github/daneren2005/dsub/util/Constants.java index 22463d9d..34cfac45 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/Constants.java +++ b/app/src/main/java/github/daneren2005/dsub/util/Constants.java @@ -66,6 +66,7 @@ public final class Constants { public static final String INTENT_EXTRA_SEARCH_SONG = "searchSong"; public static final String INTENT_EXTRA_TOP_TRACKS = "topTracks"; public static final String INTENT_EXTRA_SHOW_ALL = "showAll"; + public static final String INTENT_EXTRA_PLAY_LAST = "playLast"; // Preferences keys. public static final String PREFERENCES_KEY_SERVER_KEY = "server"; diff --git a/app/src/main/java/github/daneren2005/dsub/util/compat/RemoteControlClientLP.java b/app/src/main/java/github/daneren2005/dsub/util/compat/RemoteControlClientLP.java index c9f8cab8..456446f3 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/compat/RemoteControlClientLP.java +++ b/app/src/main/java/github/daneren2005/dsub/util/compat/RemoteControlClientLP.java @@ -338,14 +338,31 @@ public class RemoteControlClientLP extends RemoteControlClientBase { }.execute(); } + private void playPlaylist(final Playlist playlist, final boolean shuffle, final boolean append) { + new SilentServiceTask<Void>(downloadService) { + @Override + protected Void doInBackground(MusicService musicService) throws Throwable { + MusicDirectory musicDirectory = musicService.getPlaylist(false, playlist.getId(), playlist.getName(), downloadService, null); + playSongs(musicDirectory.getChildren(), shuffle, append); + + return null; + } + }.execute(); + } + private void playSong(MusicDirectory.Entry entry) { List<MusicDirectory.Entry> entries = new ArrayList<>(); entries.add(entry); playSongs(entries); } private void playSongs(List<MusicDirectory.Entry> entries) { - downloadService.clear(); - downloadService.download(entries, false, true, false, false); + playSongs(entries, false, false); + } + private void playSongs(List<MusicDirectory.Entry> entries, boolean shuffle, boolean append) { + if(!append) { + downloadService.clear(); + } + downloadService.download(entries, false, true, false, shuffle); } private void noResults() { @@ -450,6 +467,20 @@ public class RemoteControlClientLP extends RemoteControlClientBase { } } + public void onPlayFromMediaId (String mediaId, Bundle extras) { + if(extras == null) { + return; + } + + boolean shuffle = extras.getBoolean(Constants.INTENT_EXTRA_NAME_SHUFFLE, false); + boolean playLast = extras.getBoolean(Constants.INTENT_EXTRA_PLAY_LAST, false); + String playlistId = extras.getString(Constants.INTENT_EXTRA_NAME_PLAYLIST_ID, null); + if(playlistId != null) { + Playlist playlist = new Playlist(playlistId, null); + playPlaylist(playlist, shuffle, playLast); + } + } + @Override public void onCustomAction(String action, Bundle extras) { if(CUSTOM_ACTION_THUMBS_UP.equals(action)) { |