2023-06-17 15:30:23 +02:00
|
|
|
package com.cappielloantonio.tempo.util;
|
2021-07-27 12:10:28 +02:00
|
|
|
|
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
|
|
|
|
2023-06-17 15:30:23 +02:00
|
|
|
import com.cappielloantonio.tempo.App;
|
|
|
|
|
import com.cappielloantonio.tempo.subsonic.models.Child;
|
|
|
|
|
import com.cappielloantonio.tempo.subsonic.models.InternetRadioStation;
|
|
|
|
|
import com.cappielloantonio.tempo.subsonic.models.PodcastEpisode;
|
2021-07-27 12:10:28 +02:00
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2023-03-10 12:17:25 +01:00
|
|
|
@OptIn(markerClass = UnstableApi.class)
|
2021-07-27 12:10:28 +02:00
|
|
|
public class MappingUtil {
|
2023-03-12 16:34:38 +01:00
|
|
|
public static List<MediaItem> mapMediaItems(List<Child> items) {
|
|
|
|
|
ArrayList<MediaItem> mediaItems = new ArrayList<>();
|
2021-09-03 17:51:55 +02:00
|
|
|
|
2023-03-10 12:17:25 +01:00
|
|
|
for (int i = 0; i < items.size(); i++) {
|
2023-03-12 16:34:38 +01:00
|
|
|
mediaItems.add(mapMediaItem(items.get(i)));
|
2023-03-10 12:17:25 +01:00
|
|
|
}
|
2021-09-03 17:51:55 +02:00
|
|
|
|
2023-03-10 12:17:25 +01:00
|
|
|
return mediaItems;
|
2021-09-03 17:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-12 16:34:38 +01:00
|
|
|
public static MediaItem mapMediaItem(Child media) {
|
|
|
|
|
Uri uri = getUri(media);
|
2022-01-02 15:50:59 +01:00
|
|
|
|
2021-12-29 10:19:02 +01:00
|
|
|
Bundle bundle = new Bundle();
|
2023-03-08 19:19:09 +01:00
|
|
|
bundle.putString("id", media.getId());
|
|
|
|
|
bundle.putString("parentId", media.getParentId());
|
|
|
|
|
bundle.putBoolean("isDir", media.isDir());
|
|
|
|
|
bundle.putString("title", media.getTitle());
|
|
|
|
|
bundle.putString("album", media.getAlbum());
|
|
|
|
|
bundle.putString("artist", media.getArtist());
|
|
|
|
|
bundle.putInt("track", media.getTrack() != null ? media.getTrack() : 0);
|
|
|
|
|
bundle.putInt("year", media.getYear() != null ? media.getYear() : 0);
|
|
|
|
|
bundle.putString("genre", media.getGenre());
|
|
|
|
|
bundle.putString("coverArtId", media.getCoverArtId());
|
|
|
|
|
bundle.putLong("size", media.getSize() != null ? media.getSize() : 0);
|
|
|
|
|
bundle.putString("contentType", media.getContentType());
|
|
|
|
|
bundle.putString("suffix", media.getSuffix());
|
|
|
|
|
bundle.putString("transcodedContentType", media.getTranscodedContentType());
|
|
|
|
|
bundle.putString("transcodedSuffix", media.getTranscodedSuffix());
|
|
|
|
|
bundle.putInt("duration", media.getDuration() != null ? media.getDuration() : 0);
|
|
|
|
|
bundle.putInt("bitrate", media.getBitrate() != null ? media.getBitrate() : 0);
|
|
|
|
|
bundle.putString("path", media.getPath());
|
|
|
|
|
bundle.putBoolean("isVideo", media.isVideo());
|
|
|
|
|
bundle.putInt("userRating", media.getUserRating() != null ? media.getUserRating() : 0);
|
|
|
|
|
bundle.putDouble("averageRating", media.getAverageRating() != null ? media.getAverageRating() : 0);
|
2023-08-04 23:42:43 +02:00
|
|
|
bundle.putLong("playCount", media.getPlayCount() != null ? media.getPlayCount() : 0);
|
|
|
|
|
bundle.putInt("discNumber", media.getDiscNumber() != null ? media.getDiscNumber() : 0);
|
2023-03-08 19:19:09 +01:00
|
|
|
bundle.putLong("created", media.getCreated() != null ? media.getCreated().getTime() : 0);
|
|
|
|
|
bundle.putLong("starred", media.getStarred() != null ? media.getStarred().getTime() : 0);
|
|
|
|
|
bundle.putString("albumId", media.getAlbumId());
|
|
|
|
|
bundle.putString("artistId", media.getArtistId());
|
|
|
|
|
bundle.putString("type", media.getType());
|
|
|
|
|
bundle.putLong("bookmarkPosition", media.getBookmarkPosition() != null ? media.getBookmarkPosition() : 0);
|
|
|
|
|
bundle.putInt("originalWidth", media.getOriginalWidth() != null ? media.getOriginalWidth() : 0);
|
|
|
|
|
bundle.putInt("originalHeight", media.getOriginalHeight() != null ? media.getOriginalHeight() : 0);
|
2023-03-12 16:34:38 +01:00
|
|
|
bundle.putString("uri", uri.toString());
|
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()
|
2023-03-12 16:34:38 +01:00
|
|
|
.setMediaUri(uri)
|
2022-08-26 18:15:22 +02:00
|
|
|
.setExtras(bundle)
|
|
|
|
|
.build()
|
|
|
|
|
)
|
2022-01-06 11:07:39 +01:00
|
|
|
.setMimeType(MimeTypes.BASE_TYPE_AUDIO)
|
2023-03-12 16:34:38 +01:00
|
|
|
.setUri(uri)
|
2021-12-29 10:19:02 +01:00
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 16:34:38 +01:00
|
|
|
public static List<MediaItem> mapDownloads(List<Child> items) {
|
|
|
|
|
ArrayList<MediaItem> downloads = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < items.size(); i++) {
|
|
|
|
|
downloads.add(mapDownload(items.get(i)));
|
2021-12-29 10:19:02 +01:00
|
|
|
}
|
2023-03-12 16:34:38 +01:00
|
|
|
|
|
|
|
|
return downloads;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static MediaItem mapDownload(Child media) {
|
|
|
|
|
return new MediaItem.Builder()
|
|
|
|
|
.setMediaId(media.getId())
|
|
|
|
|
.setMediaMetadata(
|
|
|
|
|
new MediaMetadata.Builder()
|
|
|
|
|
.setTitle(MusicUtil.getReadableString(media.getTitle()))
|
|
|
|
|
.setTrackNumber(media.getTrack())
|
|
|
|
|
.setDiscNumber(media.getDiscNumber())
|
|
|
|
|
.setReleaseYear(media.getYear())
|
|
|
|
|
.setAlbumTitle(MusicUtil.getReadableString(media.getAlbum()))
|
|
|
|
|
.setArtist(MusicUtil.getReadableString(media.getArtist()))
|
|
|
|
|
.build()
|
|
|
|
|
)
|
|
|
|
|
.setRequestMetadata(
|
|
|
|
|
new MediaItem.RequestMetadata.Builder()
|
|
|
|
|
.setMediaUri(MusicUtil.getDownloadUri(media.getId()))
|
|
|
|
|
.build()
|
|
|
|
|
)
|
|
|
|
|
.setMimeType(MimeTypes.BASE_TYPE_AUDIO)
|
|
|
|
|
.setUri(MusicUtil.getDownloadUri(media.getId()))
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-07 17:11:34 +02:00
|
|
|
public static MediaItem mapInternetRadioStation(InternetRadioStation internetRadioStation) {
|
|
|
|
|
Uri uri = Uri.parse(internetRadioStation.getStreamUrl());
|
|
|
|
|
|
|
|
|
|
Bundle bundle = new Bundle();
|
|
|
|
|
bundle.putString("id", internetRadioStation.getId());
|
|
|
|
|
bundle.putString("title", internetRadioStation.getName());
|
|
|
|
|
bundle.putString("artist", uri.toString());
|
|
|
|
|
bundle.putString("uri", uri.toString());
|
|
|
|
|
bundle.putString("type", Constants.MEDIA_TYPE_RADIO);
|
|
|
|
|
|
|
|
|
|
return new MediaItem.Builder()
|
|
|
|
|
.setMediaId(internetRadioStation.getId())
|
|
|
|
|
.setMediaMetadata(
|
|
|
|
|
new MediaMetadata.Builder()
|
|
|
|
|
.setTitle(internetRadioStation.getName())
|
|
|
|
|
.setArtist(internetRadioStation.getStreamUrl())
|
|
|
|
|
.setExtras(bundle)
|
|
|
|
|
.build()
|
|
|
|
|
)
|
|
|
|
|
.setRequestMetadata(
|
|
|
|
|
new MediaItem.RequestMetadata.Builder()
|
|
|
|
|
.setMediaUri(uri)
|
|
|
|
|
.setExtras(bundle)
|
|
|
|
|
.build()
|
|
|
|
|
)
|
|
|
|
|
.setMimeType(MimeTypes.BASE_TYPE_AUDIO)
|
|
|
|
|
.setUri(uri)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-07 23:43:36 +02:00
|
|
|
public static MediaItem mapMediaItem(PodcastEpisode podcastEpisode) {
|
|
|
|
|
Uri uri = getUri(podcastEpisode);
|
|
|
|
|
|
|
|
|
|
Bundle bundle = new Bundle();
|
|
|
|
|
bundle.putString("id", podcastEpisode.getId());
|
|
|
|
|
bundle.putString("parentId", podcastEpisode.getParentId());
|
|
|
|
|
bundle.putBoolean("isDir", podcastEpisode.isDir());
|
|
|
|
|
bundle.putString("title", podcastEpisode.getTitle());
|
|
|
|
|
bundle.putString("album", podcastEpisode.getAlbum());
|
|
|
|
|
bundle.putString("artist", podcastEpisode.getArtist());
|
|
|
|
|
bundle.putInt("track", podcastEpisode.getTrack() != null ? podcastEpisode.getTrack() : 0);
|
|
|
|
|
bundle.putInt("year", podcastEpisode.getYear() != null ? podcastEpisode.getYear() : 0);
|
|
|
|
|
bundle.putString("genre", podcastEpisode.getGenre());
|
|
|
|
|
bundle.putString("coverArtId", podcastEpisode.getCoverArtId());
|
|
|
|
|
bundle.putLong("size", podcastEpisode.getSize() != null ? podcastEpisode.getSize() : 0);
|
|
|
|
|
bundle.putString("contentType", podcastEpisode.getContentType());
|
|
|
|
|
bundle.putString("suffix", podcastEpisode.getSuffix());
|
|
|
|
|
bundle.putString("transcodedContentType", podcastEpisode.getTranscodedContentType());
|
|
|
|
|
bundle.putString("transcodedSuffix", podcastEpisode.getTranscodedSuffix());
|
|
|
|
|
bundle.putInt("duration", podcastEpisode.getDuration() != null ? podcastEpisode.getDuration() : 0);
|
|
|
|
|
bundle.putInt("bitrate", podcastEpisode.getBitrate() != null ? podcastEpisode.getBitrate() : 0);
|
|
|
|
|
bundle.putString("path", podcastEpisode.getPath());
|
|
|
|
|
bundle.putBoolean("isVideo", podcastEpisode.isVideo());
|
|
|
|
|
bundle.putInt("userRating", podcastEpisode.getUserRating() != null ? podcastEpisode.getUserRating() : 0);
|
|
|
|
|
bundle.putDouble("averageRating", podcastEpisode.getAverageRating() != null ? podcastEpisode.getAverageRating() : 0);
|
2023-08-05 11:57:22 +02:00
|
|
|
bundle.putLong("playCount", podcastEpisode.getPlayCount() != null ? podcastEpisode.getPlayCount() : 0);
|
|
|
|
|
bundle.putInt("discNumber", podcastEpisode.getDiscNumber() != null ? podcastEpisode.getDiscNumber() : 0);
|
2023-05-07 23:43:36 +02:00
|
|
|
bundle.putLong("created", podcastEpisode.getCreated() != null ? podcastEpisode.getCreated().getTime() : 0);
|
|
|
|
|
bundle.putLong("starred", podcastEpisode.getStarred() != null ? podcastEpisode.getStarred().getTime() : 0);
|
|
|
|
|
bundle.putString("albumId", podcastEpisode.getAlbumId());
|
|
|
|
|
bundle.putString("artistId", podcastEpisode.getArtistId());
|
|
|
|
|
bundle.putString("type", podcastEpisode.getType());
|
|
|
|
|
bundle.putLong("bookmarkPosition", podcastEpisode.getBookmarkPosition() != null ? podcastEpisode.getBookmarkPosition() : 0);
|
|
|
|
|
bundle.putInt("originalWidth", podcastEpisode.getOriginalWidth() != null ? podcastEpisode.getOriginalWidth() : 0);
|
|
|
|
|
bundle.putInt("originalHeight", podcastEpisode.getOriginalHeight() != null ? podcastEpisode.getOriginalHeight() : 0);
|
|
|
|
|
bundle.putString("uri", uri.toString());
|
|
|
|
|
|
|
|
|
|
return new MediaItem.Builder()
|
|
|
|
|
.setMediaId(podcastEpisode.getId())
|
|
|
|
|
.setMediaMetadata(
|
|
|
|
|
new MediaMetadata.Builder()
|
|
|
|
|
.setTitle(MusicUtil.getReadableString(podcastEpisode.getTitle()))
|
|
|
|
|
.setTrackNumber(podcastEpisode.getTrack())
|
|
|
|
|
.setDiscNumber(podcastEpisode.getDiscNumber())
|
|
|
|
|
.setReleaseYear(podcastEpisode.getYear())
|
|
|
|
|
.setAlbumTitle(MusicUtil.getReadableString(podcastEpisode.getAlbum()))
|
|
|
|
|
.setArtist(MusicUtil.getReadableString(podcastEpisode.getArtist()))
|
|
|
|
|
.setExtras(bundle)
|
|
|
|
|
.build()
|
|
|
|
|
)
|
|
|
|
|
.setRequestMetadata(
|
|
|
|
|
new MediaItem.RequestMetadata.Builder()
|
|
|
|
|
.setMediaUri(uri)
|
|
|
|
|
.setExtras(bundle)
|
|
|
|
|
.build()
|
|
|
|
|
)
|
|
|
|
|
.setMimeType(MimeTypes.BASE_TYPE_AUDIO)
|
|
|
|
|
.setUri(uri)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 16:34:38 +01:00
|
|
|
private static Uri getUri(Child media) {
|
|
|
|
|
return DownloadUtil.getDownloadTracker(App.getContext()).isDownloaded(media.getId())
|
|
|
|
|
? MusicUtil.getDownloadUri(media.getId())
|
|
|
|
|
: MusicUtil.getStreamUri(media.getId());
|
2021-12-29 10:19:02 +01:00
|
|
|
}
|
2023-05-07 23:43:36 +02:00
|
|
|
|
|
|
|
|
private static Uri getUri(PodcastEpisode podcastEpisode) {
|
|
|
|
|
return DownloadUtil.getDownloadTracker(App.getContext()).isDownloaded(podcastEpisode.getId())
|
|
|
|
|
? MusicUtil.getDownloadUri(podcastEpisode.getId())
|
|
|
|
|
: MusicUtil.getStreamUri(podcastEpisode.getId());
|
|
|
|
|
}
|
2021-07-27 12:10:28 +02:00
|
|
|
}
|