diff options
author | ivorodr <ivorodr> | 2021-05-21 18:10:48 +0100 |
---|---|---|
committer | ivorodr <ivorodr> | 2021-05-21 18:10:48 +0100 |
commit | cf859c58624d80d25efad5a0f23952d57965a446 (patch) | |
tree | fb8204539c18b8b54dd394cd050b4a3c2257920f /app | |
parent | f786c6322d379db837d9d5315c25ef62924ce0c3 (diff) | |
download | dsub-cf859c58624d80d25efad5a0f23952d57965a446.tar.gz dsub-cf859c58624d80d25efad5a0f23952d57965a446.tar.bz2 dsub-cf859c58624d80d25efad5a0f23952d57965a446.zip |
Fixed Android Auto library browsing if Browse By Tag is enabled
Diffstat (limited to 'app')
-rw-r--r-- | app/src/main/java/github/daneren2005/dsub/service/AutoMediaBrowserService.java | 128 |
1 files changed, 127 insertions, 1 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 b0f0f399..d2378ccb 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/AutoMediaBrowserService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/AutoMediaBrowserService.java @@ -61,6 +61,8 @@ public class AutoMediaBrowserService extends MediaBrowserServiceCompat { private static final String MUSIC_DIRECTORY_PREFIX = "md-"; private static final String MUSIC_FOLDER_PREFIX = "mf-"; private static final String MUSIC_DIRECTORY_CONTENTS_PREFIX = "mdc-"; + private static final String ARTIST_CONTENTS_PREFIX = "art-"; + private static final String ALBUM_CONTENTS_PREFIX = "alb-"; private DownloadService downloadService; private Handler handler = new Handler(); @@ -91,13 +93,23 @@ public class AutoMediaBrowserService extends MediaBrowserServiceCompat { String id = parentId.substring(MUSIC_DIRECTORY_PREFIX.length()); getPlayOptions(result, id, Constants.INTENT_EXTRA_NAME_ID); } else if(BROWSER_LIBRARY.equals(parentId)) { - getLibrary(result); + if(Util.isTagBrowsing(downloadService)) { + getArtists(result); + } else { + getLibrary(result); + } } else if(parentId.startsWith(MUSIC_FOLDER_PREFIX)) { String id = parentId.substring(MUSIC_FOLDER_PREFIX.length()); getIndexes(result, id); } else if(parentId.startsWith(MUSIC_DIRECTORY_CONTENTS_PREFIX)) { String id = parentId.substring(MUSIC_DIRECTORY_CONTENTS_PREFIX.length()); getMusicDirectory(result, id); + } else if(parentId.startsWith(ARTIST_CONTENTS_PREFIX)) { + String id = parentId.substring(ARTIST_CONTENTS_PREFIX.length()); + getArtist(result, id); + } else if(parentId.startsWith(ALBUM_CONTENTS_PREFIX)) { + String id = parentId.substring(ALBUM_CONTENTS_PREFIX.length()); + getAlbum(result, id); } else if(BROWSER_PLAYLISTS.equals(parentId)) { getPlaylists(result); } else if(parentId.startsWith(PLAYLIST_PREFIX)) { @@ -351,6 +363,120 @@ public class AutoMediaBrowserService extends MediaBrowserServiceCompat { result.detach(); } + private void getArtists(final Result<List<MediaBrowserCompat.MediaItem>> result) { + new SilentServiceTask<Indexes>(downloadService) { + @Override + protected Indexes doInBackground(MusicService musicService) throws Throwable { + return musicService.getIndexes(null, false, downloadService, null); + } + + @Override + protected void done(Indexes indexes) { + List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>(); + + // music directories + for(Artist artist : indexes.getArtists()) { + MediaDescriptionCompat description = new MediaDescriptionCompat.Builder() + .setTitle(artist.getName()) + .setMediaId(ARTIST_CONTENTS_PREFIX + artist.getId()) + .build(); + + mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE)); + } + + result.sendResult(mediaItems); + } + }.execute(); + + result.detach(); + } + + private void getAlbum(final Result<List<MediaBrowserCompat.MediaItem>> result, final String musicFolderId) { + new SilentServiceTask<MusicDirectory>(downloadService) { + @Override + protected MusicDirectory doInBackground(MusicService musicService) throws Throwable { + return musicService.getAlbum(musicFolderId, "", false, downloadService, null); + } + + @Override + protected void done(MusicDirectory songs) { + List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>(); + + addPlayOptions(mediaItems, musicFolderId, Constants.INTENT_EXTRA_NAME_ID); + + // songs + for(Entry entry : songs.getSongs()) { + try { + Bundle extras = new Bundle(); + extras.putByteArray(Constants.INTENT_EXTRA_ENTRY_BYTES, entry.toByteArray()); + extras.putString(Constants.INTENT_EXTRA_NAME_CHILD_ID, entry.getId()); + + MediaDescriptionCompat description = new MediaDescriptionCompat.Builder() + .setTitle(entry.getTitle()) + .setMediaId(entry.getId()) + .setExtras(extras) + .build(); + + mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_PLAYABLE)); + } catch(IOException e) { + Log.e(TAG, "Failed to add entry", e); + } + } + + result.sendResult(mediaItems); + } + }.execute(); + + result.detach(); + } + + private void getArtist(final Result<List<MediaBrowserCompat.MediaItem>> result, final String musicDirectoryId) { + new SilentServiceTask<MusicDirectory>(downloadService) { + @Override + protected MusicDirectory doInBackground(MusicService musicService) throws Throwable { + return musicService.getArtist(musicDirectoryId, "", false, downloadService, null); + } + + @Override + protected void done(MusicDirectory directory) { + List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>(); + + for(Entry entry : directory.getChildren()) { + MediaDescriptionCompat description; + if (entry.isDirectory()) { + // browse deeper + description = new MediaDescriptionCompat.Builder() + .setTitle(entry.getTitle()) + .setMediaId(ALBUM_CONTENTS_PREFIX + entry.getId()) + .build(); + + mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE)); + } else { + try { + // mark individual songs as directly playable + Bundle extras = new Bundle(); + extras.putByteArray(Constants.INTENT_EXTRA_ENTRY_BYTES, entry.toByteArray()); + extras.putString(Constants.INTENT_EXTRA_NAME_CHILD_ID, entry.getId()); + + description = new MediaDescriptionCompat.Builder() + .setTitle(entry.getTitle()) + .setMediaId(entry.getId()) + .setExtras(extras) + .build(); + + mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_PLAYABLE)); + } catch (IOException e) { + Log.e(TAG, "Failed to add entry", e); + } + } + } + result.sendResult(mediaItems); + } + }.execute(); + + result.detach(); + } + private void getPlaylists(final Result<List<MediaBrowserCompat.MediaItem>> result) { new SilentServiceTask<List<Playlist>>(downloadService) { @Override |