diff --git a/app/src/main/java/ru/vtosters/lite/downloaders/AudioDownloader.java b/app/src/main/java/ru/vtosters/lite/downloaders/AudioDownloader.java index b29356756..a5d75f705 100644 --- a/app/src/main/java/ru/vtosters/lite/downloaders/AudioDownloader.java +++ b/app/src/main/java/ru/vtosters/lite/downloaders/AudioDownloader.java @@ -18,6 +18,7 @@ import java.io.File; import java.util.ArrayList; import java.util.concurrent.Executor; import java.util.concurrent.Executors; +import java.util.stream.Collectors; import bruhcollective.itaysonlab.libvkx.client.LibVKXClient; import java8.util.concurrent.CompletableFuture; @@ -43,19 +44,24 @@ public class AudioDownloader { public static void downloadPlaylist(Playlist playlist) { var tracks = PlaylistConverter.getPlaylist(playlist); + var tracksWithUrls = tracks.stream().filter(track -> !track.D.isEmpty()).collect(Collectors.toList()); + var playlistName = IOUtils.getValidFileName(playlist.g); var musicPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath(); var downloadPath = musicPath + File.separator + playlistName; - PlaylistDownloader.downloadPlaylist(tracks, IOUtils.getValidFileName(playlist.g), downloadPath); + var notificationId = playlistName.hashCode(); + var notification = buildPlaylistDownloadNotification(playlistName, notificationId); + + PlaylistDownloader.downloadPlaylist( + tracksWithUrls, + IOUtils.getValidFileName(playlist.g), + downloadPath, + buildPlaylistCallback(tracksWithUrls.size(), notification, notificationId) + ); } - private static void cachePlaylist(ArrayList playlist) { - for (MusicTrack musicTrack : playlist) { - CompletableFuture.runAsync(() -> cacheTrack(musicTrack), executor); - } - } public static void downloadAudio(MusicTrack track) { downloadM3U8(track, false); @@ -72,7 +78,11 @@ public class AudioDownloader { var downloadPath = musicPath + File.separator; var notification = buildDownloadNotification(track, tempId); - TrackDownloader.downloadTrack(track, downloadPath, new Callback() { + TrackDownloader.downloadTrack(track, downloadPath, buildOneTrackCallback(tempId, notification), cache); + } + + private static Callback buildOneTrackCallback(int notificationId, NotificationCompat.Builder notification) { + return new Callback() { long startTime = 0; long elapsedTime = 0; @@ -80,7 +90,7 @@ public class AudioDownloader { public void onProgress(int progress) { if (elapsedTime > 1000) { notification.setProgress(100, progress, false); - notificationManager.notify(tempId, notification.build()); + notificationManager.notify(notificationId, notification.build()); startTime = System.currentTimeMillis(); elapsedTime = 0; @@ -96,7 +106,7 @@ public class AudioDownloader { .setContentText(AndroidUtils.getString("player_download_finished")) .setProgress(0, 0, false) .setSmallIcon(android.R.drawable.stat_sys_download_done); - notificationManager.notify(tempId, notification.build()); + notificationManager.notify(notificationId, notification.build()); } catch (UnsatisfiedLinkError e) { Log.e("AudioDownloader", "native libs"); Log.e("AudioDownloader", e.getMessage()); @@ -107,14 +117,46 @@ public class AudioDownloader { @Override public void onFailure() { notification.setContentText(AndroidUtils.getString("load_audio_error")).setProgress(0, 0, false); - notificationManager.notify(tempId, notification.build()); + notificationManager.notify(notificationId, notification.build()); } @Override public void onSizeReceived(long size, long header) { } - }, cache); + }; + } + + private static Callback buildPlaylistCallback(int playlistSize, NotificationCompat.Builder notification, int notificationId) { + return new Callback() { + @Override + public void onProgress(int progress) { + if (progress == playlistSize) { + onSuccess(); + } else { + notification.setContentText("Downloaded " + progress + " of " + playlistSize + " tracks"); + notification.setProgress(playlistSize, progress, false); + } + notificationManager.notify(notificationId, notification.build()); + } + + @Override + public void onSuccess() { + notification.setContentText("Download completed"); + notification.setProgress(0, 0, false); + } + + @Override + public void onFailure() { + + } + + @Override + public void onSizeReceived(long size, long header) { + + } + }); + }; } public static void cacheTrack(MusicTrack track) { @@ -133,6 +175,12 @@ public class AudioDownloader { NotificationChannels.getNotificationManager().cancel(trackId.hashCode()); } + private static void cachePlaylist(ArrayList playlist) { + for (MusicTrack musicTrack : playlist) { + CompletableFuture.runAsync(() -> cacheTrack(musicTrack), executor); + } + } + private static void notifySavingToCache(MusicTrack track) { Notification.Builder builder; @@ -161,5 +209,16 @@ public class AudioDownloader { return notificationBuilder; } - + private static NotificationCompat.Builder buildPlaylistDownloadNotification(String playlistName, int id) { + var notificationBuilder = new NotificationCompat.Builder(AndroidUtils.getGlobalContext(), NotificationChannels.MUSIC_PLAYLIST_DOWNLOAD_CHANNEL_ID) + .setSmallIcon(android.R.drawable.stat_sys_download) + .setContentTitle("Загрузка плейлиста " + '«' + playlistName + '»') + .setContentText(playlistName) + .setPriority(NotificationCompat.PRIORITY_LOW) + .setCategory(NotificationCompat.CATEGORY_PROGRESS) + .setAutoCancel(true) + .setProgress(100, 0, false); + notificationManager.notify(id, notificationBuilder.build()); + return notificationBuilder; + } } diff --git a/app/src/main/java/ru/vtosters/lite/music/downloader/PlaylistDownloader.java b/app/src/main/java/ru/vtosters/lite/music/downloader/PlaylistDownloader.java index 02cee4fb1..6e2c2cd3c 100644 --- a/app/src/main/java/ru/vtosters/lite/music/downloader/PlaylistDownloader.java +++ b/app/src/main/java/ru/vtosters/lite/music/downloader/PlaylistDownloader.java @@ -2,7 +2,6 @@ package ru.vtosters.lite.music.downloader; import android.util.Log; -import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; import com.vk.dto.music.MusicTrack; @@ -10,30 +9,24 @@ import com.vk.dto.music.MusicTrack; import java.io.File; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; -import java.util.stream.Collectors; import ru.vtosters.lite.downloaders.notifications.NotificationChannels; import ru.vtosters.lite.music.Callback; -import ru.vtosters.lite.utils.AndroidUtils; public class PlaylistDownloader { private static NotificationManagerCompat notificationManager = NotificationChannels.getNotificationManager(); - public static void downloadPlaylist(List playlist, String playlistName, String path) { + public static void downloadPlaylist(List playlist, String playlistName, String path, Callback callback) { File outDir = new File(path); if (!outDir.exists()) if (outDir.mkdirs()) - Log.v("TrackDownloader", "Directory created"); + Log.v("PlaylistDownloader", "Directory created"); else - Log.e("TrackDownloader", "Directory creation failed"); - - var notificationId = playlistName.hashCode(); - var notification = buildDownloadNotification(playlistName, notificationId); + Log.e("PlaylistDownloader", "Directory creation failed"); var downloadedTracks = new AtomicInteger(0); - var tracksWithUrls = playlist.stream().filter(track -> !track.D.isEmpty()).collect(Collectors.toList()); - for (MusicTrack musicTrack : tracksWithUrls) { + for (MusicTrack musicTrack : playlist) { TrackDownloader.downloadTrack(musicTrack, path, new Callback() { @Override public void onProgress(int progress) {} @@ -41,15 +34,7 @@ public class PlaylistDownloader { @Override public void onSuccess() { var currentProgress = downloadedTracks.incrementAndGet(); - - if (currentProgress == tracksWithUrls.size()) { - notification.setContentText("Download complete"); - notification.setProgress(0, 0, false); - } else { - notification.setContentText("Downloaded " + currentProgress + " of " + tracksWithUrls.size() + "tracks"); - notification.setProgress(tracksWithUrls.size(), currentProgress, false); - } - notificationManager.notify(notificationId, notification.build()); + callback.onProgress(currentProgress); } @Override @@ -60,17 +45,4 @@ public class PlaylistDownloader { }, false); } } - - private static NotificationCompat.Builder buildDownloadNotification(String playlistName, int id) { - var notificationBuilder = new NotificationCompat.Builder(AndroidUtils.getGlobalContext(), NotificationChannels.MUSIC_PLAYLIST_DOWNLOAD_CHANNEL_ID) - .setSmallIcon(android.R.drawable.stat_sys_download) - .setContentTitle("Загрузка плейлиста " + '«' + playlistName + '»') - .setContentText(playlistName) - .setPriority(NotificationCompat.PRIORITY_LOW) - .setCategory(NotificationCompat.CATEGORY_PROGRESS) - .setAutoCancel(true) - .setProgress(100, 0, false); - notificationManager.notify(id, notificationBuilder.build()); - return notificationBuilder; - } } \ No newline at end of file