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;
|
2022-02-07 17:38:47 +01:00
|
|
|
import android.net.Uri;
|
2021-12-29 10:19:02 +01:00
|
|
|
import android.os.Bundle;
|
|
|
|
|
|
2022-12-28 14:25:59 +01:00
|
|
|
import androidx.annotation.OptIn;
|
2021-12-29 10:19:02 +01:00
|
|
|
import androidx.media3.common.MediaItem;
|
|
|
|
|
import androidx.media3.common.MediaMetadata;
|
2022-01-06 11:07:39 +01:00
|
|
|
import androidx.media3.common.MimeTypes;
|
2022-12-28 14:25:59 +01:00
|
|
|
import androidx.media3.common.util.UnstableApi;
|
2021-12-29 10:19:02 +01:00
|
|
|
|
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;
|
2022-02-07 17:38:47 +01:00
|
|
|
import com.cappielloantonio.play.model.Media;
|
2021-07-29 14:56:00 +02:00
|
|
|
import com.cappielloantonio.play.model.Playlist;
|
2021-07-27 12:10:28 +02:00
|
|
|
import com.cappielloantonio.play.subsonic.models.Child;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class MappingUtil {
|
2021-08-28 16:54:12 +02:00
|
|
|
public static ArrayList<Album> mapDownloadToAlbum(List<Download> downloads) {
|
|
|
|
|
ArrayList<Album> albums = new ArrayList();
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
// TODO
|
|
|
|
|
/* for (Download download : downloads) {
|
2021-08-28 16:54:12 +02:00
|
|
|
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);
|
|
|
|
|
}
|
2023-03-06 21:59:10 +01:00
|
|
|
} */
|
2021-08-28 16:54:12 +02:00
|
|
|
|
|
|
|
|
return albums;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ArrayList<Artist> mapDownloadToArtist(List<Download> downloads) {
|
|
|
|
|
ArrayList<Artist> artists = new ArrayList();
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
// TODO
|
|
|
|
|
/* for (Download download : downloads) {
|
2021-08-28 16:54:12 +02:00
|
|
|
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);
|
|
|
|
|
}
|
2023-03-06 21:59:10 +01:00
|
|
|
} */
|
2021-08-28 16:54:12 +02:00
|
|
|
|
|
|
|
|
return artists;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-03 17:51:55 +02:00
|
|
|
public static ArrayList<Playlist> mapDownloadToPlaylist(List<Download> downloads) {
|
|
|
|
|
ArrayList<Playlist> playlists = new ArrayList();
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
// TODO
|
|
|
|
|
/*for (Download download : downloads) {
|
2023-03-02 10:48:30 +01:00
|
|
|
playlists.add(new Playlist(download.getPlaylistId(), download.getPlaylistName(), null, 0, 0, null));
|
2023-03-06 21:59:10 +01:00
|
|
|
}*/
|
2021-09-03 17:51:55 +02:00
|
|
|
|
|
|
|
|
return playlists;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public static ArrayList<Download> mapDownload(List<Child> media, String playlistId, String playlistName) {
|
2021-07-29 17:12:55 +02:00
|
|
|
ArrayList<Download> downloads = new ArrayList();
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
// TODO
|
|
|
|
|
/* for (Child item : media) {
|
|
|
|
|
Download download = (Download) item;
|
|
|
|
|
download.setMediaID();
|
|
|
|
|
download.setServer();
|
|
|
|
|
download.setPlaylistId();
|
|
|
|
|
downloads.add(download);
|
|
|
|
|
} */
|
2021-07-29 17:12:55 +02:00
|
|
|
|
|
|
|
|
return downloads;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public static Download mapDownload(Child media, String playlistId, String playlistName) {
|
|
|
|
|
// TODO
|
|
|
|
|
//return new Download(media, playlistId, playlistName);
|
2021-07-31 18:43:40 +02:00
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
return null;
|
2021-07-31 18:43:40 +02:00
|
|
|
}
|
2021-12-29 10:19:02 +01:00
|
|
|
|
2022-12-28 14:25:59 +01:00
|
|
|
@OptIn(markerClass = UnstableApi.class)
|
2023-03-06 21:59:10 +01:00
|
|
|
public static MediaItem mapMediaItem(Context context, Child media, boolean stream) {
|
2022-02-07 17:38:47 +01:00
|
|
|
boolean isDownloaded = DownloadUtil.getDownloadTracker(context).isDownloaded(MusicUtil.getDownloadUri(media.getId()));
|
2022-01-02 15:50:59 +01:00
|
|
|
|
2021-12-29 10:19:02 +01:00
|
|
|
Bundle bundle = new Bundle();
|
2023-03-06 21:59:10 +01:00
|
|
|
bundle.putParcelable("child", media);
|
2021-12-29 10:19:02 +01:00
|
|
|
|
|
|
|
|
return new MediaItem.Builder()
|
2022-02-07 17:38:47 +01:00
|
|
|
.setMediaId(media.getId())
|
2021-12-29 10:19:02 +01:00
|
|
|
.setMediaMetadata(
|
|
|
|
|
new MediaMetadata.Builder()
|
2022-02-07 17:38:47 +01:00
|
|
|
.setTitle(MusicUtil.getReadableString(media.getTitle()))
|
2023-03-06 21:59:10 +01:00
|
|
|
.setTrackNumber(media.getTrack())
|
2022-02-07 17:38:47 +01:00
|
|
|
.setDiscNumber(media.getDiscNumber())
|
|
|
|
|
.setReleaseYear(media.getYear())
|
2023-03-06 21:59:10 +01:00
|
|
|
.setAlbumTitle(MusicUtil.getReadableString(media.getAlbum()))
|
|
|
|
|
.setArtist(MusicUtil.getReadableString(media.getArtist()))
|
2021-12-29 10:19:02 +01:00
|
|
|
.setExtras(bundle)
|
|
|
|
|
.build()
|
|
|
|
|
)
|
2022-08-26 18:15:22 +02:00
|
|
|
.setRequestMetadata(
|
|
|
|
|
new MediaItem.RequestMetadata.Builder()
|
|
|
|
|
.setMediaUri(getUri(context, media, stream && !isDownloaded))
|
|
|
|
|
.setExtras(bundle)
|
|
|
|
|
.build()
|
|
|
|
|
)
|
2022-01-06 11:07:39 +01:00
|
|
|
.setMimeType(MimeTypes.BASE_TYPE_AUDIO)
|
2022-02-07 17:38:47 +01:00
|
|
|
.setUri(getUri(context, media, stream && !isDownloaded))
|
2021-12-29 10:19:02 +01:00
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
private static Uri getUri(Context context, Child media, boolean stream) {
|
2022-02-07 17:38:47 +01:00
|
|
|
switch (media.getType()) {
|
|
|
|
|
case Media.MEDIA_TYPE_MUSIC:
|
|
|
|
|
if (stream) {
|
|
|
|
|
return MusicUtil.getStreamUri(context, media.getId());
|
2022-03-25 22:27:29 +01:00
|
|
|
} else {
|
2022-02-07 17:38:47 +01:00
|
|
|
return MusicUtil.getDownloadUri(media.getId());
|
|
|
|
|
}
|
|
|
|
|
case Media.MEDIA_TYPE_PODCAST:
|
2022-03-25 22:27:29 +01:00
|
|
|
if (stream) {
|
2023-03-06 21:59:10 +01:00
|
|
|
// TODO
|
|
|
|
|
// return MusicUtil.getStreamUri(context, media.getStreamId());
|
2022-03-25 22:27:29 +01:00
|
|
|
} else {
|
2023-03-06 21:59:10 +01:00
|
|
|
// TODO
|
|
|
|
|
// return MusicUtil.getDownloadUri(media.getStreamId());
|
2022-02-07 17:38:47 +01:00
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return MusicUtil.getStreamUri(context, media.getId());
|
|
|
|
|
}
|
2022-02-05 18:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public static ArrayList<MediaItem> mapMediaItems(Context context, List<Child> items, boolean stream) {
|
2021-12-29 10:19:02 +01:00
|
|
|
ArrayList<MediaItem> mediaItems = new ArrayList();
|
|
|
|
|
|
2022-02-07 17:38:47 +01:00
|
|
|
for (int i = 0; i < items.size(); i++) {
|
|
|
|
|
mediaItems.add(mapMediaItem(context, items.get(i), stream));
|
2021-12-29 10:19:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mediaItems;
|
|
|
|
|
}
|
2021-07-27 12:10:28 +02:00
|
|
|
}
|