2021-07-27 12:10:28 +02:00
|
|
|
package com.cappielloantonio.play.util;
|
|
|
|
|
|
2021-12-29 10:19:02 +01:00
|
|
|
import android.content.Context;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
|
|
|
|
|
import androidx.media3.common.MediaItem;
|
|
|
|
|
import androidx.media3.common.MediaMetadata;
|
|
|
|
|
|
2021-07-27 12:10:28 +02:00
|
|
|
import com.cappielloantonio.play.model.Album;
|
2021-07-27 16:58:38 +02:00
|
|
|
import com.cappielloantonio.play.model.Artist;
|
2021-07-29 17:12:55 +02:00
|
|
|
import com.cappielloantonio.play.model.Download;
|
2021-07-29 14:56:00 +02:00
|
|
|
import com.cappielloantonio.play.model.Playlist;
|
2021-07-28 15:28:32 +02:00
|
|
|
import com.cappielloantonio.play.model.Queue;
|
2021-07-27 12:10:28 +02:00
|
|
|
import com.cappielloantonio.play.model.Song;
|
|
|
|
|
import com.cappielloantonio.play.subsonic.models.AlbumID3;
|
2021-08-02 18:27:45 +02:00
|
|
|
import com.cappielloantonio.play.subsonic.models.AlbumInfo;
|
2021-07-31 16:37:41 +02:00
|
|
|
import com.cappielloantonio.play.subsonic.models.AlbumWithSongsID3;
|
2021-07-27 16:58:38 +02:00
|
|
|
import com.cappielloantonio.play.subsonic.models.ArtistID3;
|
2021-07-30 14:09:50 +02:00
|
|
|
import com.cappielloantonio.play.subsonic.models.ArtistInfo2;
|
2021-07-29 14:19:19 +02:00
|
|
|
import com.cappielloantonio.play.subsonic.models.ArtistWithAlbumsID3;
|
2021-07-27 12:10:28 +02:00
|
|
|
import com.cappielloantonio.play.subsonic.models.Child;
|
2021-07-31 18:43:40 +02:00
|
|
|
import com.cappielloantonio.play.subsonic.models.Genre;
|
2021-08-02 09:15:04 +02:00
|
|
|
import com.cappielloantonio.play.subsonic.models.SimilarArtistID3;
|
2021-07-27 12:10:28 +02:00
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class MappingUtil {
|
|
|
|
|
public static ArrayList<Song> mapSong(List<Child> children) {
|
|
|
|
|
ArrayList<Song> songs = new ArrayList();
|
|
|
|
|
|
2021-07-31 16:37:41 +02:00
|
|
|
for (Child child : children) {
|
2021-07-27 12:10:28 +02:00
|
|
|
songs.add(new Song(child));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return songs;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-13 17:29:37 +02:00
|
|
|
public static Song mapSong(Child child) {
|
|
|
|
|
return new Song(child);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 12:10:28 +02:00
|
|
|
public static ArrayList<Album> mapAlbum(List<AlbumID3> albumID3List) {
|
|
|
|
|
ArrayList<Album> albums = new ArrayList();
|
|
|
|
|
|
2021-07-31 16:37:41 +02:00
|
|
|
for (AlbumID3 albumID3 : albumID3List) {
|
2021-07-27 12:10:28 +02:00
|
|
|
albums.add(new Album(albumID3));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return albums;
|
|
|
|
|
}
|
2021-07-27 16:58:38 +02:00
|
|
|
|
2021-07-31 16:37:41 +02:00
|
|
|
public static Album mapAlbum(AlbumWithSongsID3 albumWithSongsID3) {
|
|
|
|
|
return new Album(albumWithSongsID3);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-02 18:27:45 +02:00
|
|
|
public static Album mapAlbum(AlbumInfo albumInfo) {
|
|
|
|
|
return new Album(albumInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 14:19:19 +02:00
|
|
|
public static ArrayList<Artist> mapArtist(List<ArtistID3> artistID3List) {
|
2021-07-27 16:58:38 +02:00
|
|
|
ArrayList<Artist> artists = new ArrayList();
|
|
|
|
|
|
2021-07-31 16:37:41 +02:00
|
|
|
for (ArtistID3 artistID3 : artistID3List) {
|
2021-07-27 16:58:38 +02:00
|
|
|
artists.add(new Artist(artistID3));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return artists;
|
|
|
|
|
}
|
2021-07-28 15:28:32 +02:00
|
|
|
|
2021-07-30 14:09:50 +02:00
|
|
|
public static Artist mapArtist(ArtistInfo2 artistInfo2) {
|
|
|
|
|
return new Artist(artistInfo2);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-31 16:37:41 +02:00
|
|
|
public static Artist mapArtist(ArtistWithAlbumsID3 artistWithAlbumsID3) {
|
|
|
|
|
return new Artist(artistWithAlbumsID3);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 14:19:19 +02:00
|
|
|
public static Artist mapArtistWithAlbum(ArtistWithAlbumsID3 artistWithAlbumsID3) {
|
|
|
|
|
return new Artist(artistWithAlbumsID3);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-02 09:15:04 +02:00
|
|
|
public static ArrayList<Artist> mapSimilarArtist(List<SimilarArtistID3> similarArtistID3s) {
|
|
|
|
|
ArrayList<Artist> artists = new ArrayList();
|
|
|
|
|
|
|
|
|
|
for (SimilarArtistID3 similarArtistID3 : similarArtistID3s) {
|
|
|
|
|
artists.add(new Artist(similarArtistID3));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return artists;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-28 15:28:32 +02:00
|
|
|
public static ArrayList<Song> mapQueue(List<Queue> queueList) {
|
|
|
|
|
ArrayList<Song> songs = new ArrayList();
|
|
|
|
|
|
2021-07-31 16:37:41 +02:00
|
|
|
for (Queue item : queueList) {
|
2021-07-28 15:28:32 +02:00
|
|
|
songs.add(new Song(item));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return songs;
|
|
|
|
|
}
|
2021-07-29 14:56:00 +02:00
|
|
|
|
2021-12-30 18:13:50 +01:00
|
|
|
public static Queue mapSongToQueue(Song song, int trackOrder) {
|
|
|
|
|
return new Queue(trackOrder, song.getId(), song.getTitle(), song.getAlbumId(), song.getAlbumName(), song.getArtistId(), song.getArtistName(), song.getPrimary(), song.getDuration(), 0);
|
2021-12-30 13:48:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<Queue> mapSongsToQueue(List<Song> songs) {
|
|
|
|
|
List<Queue> queue = new ArrayList<>();
|
|
|
|
|
|
2021-12-30 18:13:50 +01:00
|
|
|
for (int counter = 0; counter < songs.size(); counter++) {
|
|
|
|
|
queue.add(mapSongToQueue(songs.get(counter), counter));
|
2021-12-30 13:48:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return queue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 14:56:00 +02:00
|
|
|
public static ArrayList<Playlist> mapPlaylist(List<com.cappielloantonio.play.subsonic.models.Playlist> playlists) {
|
|
|
|
|
ArrayList<Playlist> playlist = new ArrayList();
|
|
|
|
|
|
2021-07-31 16:37:41 +02:00
|
|
|
for (com.cappielloantonio.play.subsonic.models.Playlist item : playlists) {
|
2021-07-29 14:56:00 +02:00
|
|
|
playlist.add(new Playlist(item));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return playlist;
|
|
|
|
|
}
|
2021-07-29 17:12:55 +02:00
|
|
|
|
2021-08-28 16:54:12 +02:00
|
|
|
public static ArrayList<Song> mapDownloadToSong(List<Download> downloads) {
|
2021-07-29 17:12:55 +02:00
|
|
|
ArrayList<Song> songs = new ArrayList();
|
|
|
|
|
|
2021-07-31 16:37:41 +02:00
|
|
|
for (Download download : downloads) {
|
2021-08-28 16:54:12 +02:00
|
|
|
Song song = new Song(download);
|
2021-09-04 16:14:10 +02:00
|
|
|
if (!songs.contains(song)) {
|
2021-08-28 16:54:12 +02:00
|
|
|
songs.add(song);
|
|
|
|
|
}
|
2021-07-29 17:12:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return songs;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-28 16:54:12 +02:00
|
|
|
public static ArrayList<Album> mapDownloadToAlbum(List<Download> downloads) {
|
|
|
|
|
ArrayList<Album> albums = new ArrayList();
|
|
|
|
|
|
|
|
|
|
for (Download download : downloads) {
|
|
|
|
|
Album album = new Album(download);
|
2021-09-04 16:14:10 +02:00
|
|
|
if (!albums.contains(album)) {
|
2021-08-28 16:54:12 +02:00
|
|
|
albums.add(album);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return albums;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ArrayList<Artist> mapDownloadToArtist(List<Download> downloads) {
|
|
|
|
|
ArrayList<Artist> artists = new ArrayList();
|
|
|
|
|
|
|
|
|
|
for (Download download : downloads) {
|
|
|
|
|
Artist artist = new Artist(download);
|
2021-09-04 16:14:10 +02:00
|
|
|
if (!artists.contains(artist)) {
|
2021-08-28 16:54:12 +02:00
|
|
|
artists.add(artist);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return artists;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-03 17:51:55 +02:00
|
|
|
public static ArrayList<Playlist> mapDownloadToPlaylist(List<Download> downloads) {
|
|
|
|
|
ArrayList<Playlist> playlists = new ArrayList();
|
|
|
|
|
|
|
|
|
|
for (Download download : downloads) {
|
|
|
|
|
playlists.add(new Playlist(download.getPlaylistId(), download.getPlaylistName()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return playlists;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-02 15:50:59 +01:00
|
|
|
public static ArrayList<Download> mapDownload(List<Song> songs, String playlistId, String playlistName) {
|
2021-07-29 17:12:55 +02:00
|
|
|
ArrayList<Download> downloads = new ArrayList();
|
|
|
|
|
|
2021-07-31 16:37:41 +02:00
|
|
|
for (Song song : songs) {
|
2022-01-02 15:50:59 +01:00
|
|
|
downloads.add(new Download(song, playlistId, playlistName));
|
2021-07-29 17:12:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return downloads;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-02 11:30:16 +01:00
|
|
|
public static Download mapDownload(Song song, String playlistId, String playlistName) {
|
2021-09-03 17:51:55 +02:00
|
|
|
return new Download(song, playlistId, playlistName);
|
2021-07-29 17:12:55 +02:00
|
|
|
}
|
2021-07-31 18:43:40 +02:00
|
|
|
|
|
|
|
|
public static ArrayList<com.cappielloantonio.play.model.Genre> mapGenre(List<Genre> genreList) {
|
|
|
|
|
ArrayList<com.cappielloantonio.play.model.Genre> genres = new ArrayList();
|
|
|
|
|
|
|
|
|
|
for (Genre genre : genreList) {
|
|
|
|
|
genres.add(new com.cappielloantonio.play.model.Genre(genre));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return genres;
|
|
|
|
|
}
|
2021-12-29 10:19:02 +01:00
|
|
|
|
2022-01-01 22:34:42 +01:00
|
|
|
public static MediaItem mapMediaItem(Context context, Song song, boolean stream) {
|
2022-01-02 15:50:59 +01:00
|
|
|
boolean isDownloaded = DownloadUtil.getDownloadTracker(context).isDownloaded(MusicUtil.getSongDownloadUri(song));
|
|
|
|
|
|
2021-12-29 10:19:02 +01:00
|
|
|
Bundle bundle = new Bundle();
|
|
|
|
|
bundle.putString("id", song.getId());
|
|
|
|
|
bundle.putString("albumId", song.getAlbumId());
|
|
|
|
|
bundle.putString("artistId", song.getArtistId());
|
|
|
|
|
|
|
|
|
|
return new MediaItem.Builder()
|
|
|
|
|
.setMediaId(song.getId())
|
|
|
|
|
.setMediaMetadata(
|
|
|
|
|
new MediaMetadata.Builder()
|
2022-01-02 15:50:59 +01:00
|
|
|
.setMediaUri(stream && !isDownloaded ? MusicUtil.getSongStreamUri(context, song) : MusicUtil.getSongDownloadUri(song))
|
2021-12-30 09:19:49 +01:00
|
|
|
.setTitle(MusicUtil.getReadableString(song.getTitle()))
|
2021-12-29 10:19:02 +01:00
|
|
|
.setTrackNumber(song.getTrackNumber())
|
|
|
|
|
.setDiscNumber(song.getDiscNumber())
|
|
|
|
|
.setReleaseYear(song.getYear())
|
2021-12-30 09:19:49 +01:00
|
|
|
.setAlbumTitle(MusicUtil.getReadableString(song.getAlbumName()))
|
|
|
|
|
.setArtist(MusicUtil.getReadableString(song.getArtistName()))
|
2021-12-29 10:19:02 +01:00
|
|
|
.setExtras(bundle)
|
|
|
|
|
.build()
|
|
|
|
|
)
|
2022-01-02 15:50:59 +01:00
|
|
|
.setUri(stream && !isDownloaded ? MusicUtil.getSongStreamUri(context, song) : MusicUtil.getSongDownloadUri(song))
|
2021-12-29 10:19:02 +01:00
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-01 22:34:42 +01:00
|
|
|
public static ArrayList<MediaItem> mapMediaItems(Context context, List<Song> songs, boolean stream) {
|
2021-12-29 10:19:02 +01:00
|
|
|
ArrayList<MediaItem> mediaItems = new ArrayList();
|
|
|
|
|
|
|
|
|
|
for (Song song : songs) {
|
2022-01-01 22:34:42 +01:00
|
|
|
mediaItems.add(mapMediaItem(context, song, stream));
|
2021-12-29 10:19:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mediaItems;
|
|
|
|
|
}
|
2021-07-27 12:10:28 +02:00
|
|
|
}
|