[music] move callback and notification builders to their own packages

This commit is contained in:
aliveoutside
2022-09-18 18:59:19 +03:00
parent 4cc0b04b68
commit 4443f69056
3 changed files with 140 additions and 113 deletions
@@ -6,10 +6,6 @@ import static ru.vtosters.lite.utils.AndroidUtils.getString;
import android.app.Notification;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.vk.core.util.ToastUtils;
import com.vk.dto.music.MusicTrack;
@@ -23,12 +19,13 @@ import java.util.stream.Collectors;
import bruhcollective.itaysonlab.libvkx.client.LibVKXClient;
import java8.util.concurrent.CompletableFuture;
import ru.vtosters.lite.downloaders.notifications.NotificationChannels;
import ru.vtosters.lite.music.Callback;
import ru.vtosters.lite.music.cache.CacheDatabaseDelegate;
import ru.vtosters.lite.music.cache.FileCacheImplementation;
import ru.vtosters.lite.music.callback.MusicCallbackBuilder;
import ru.vtosters.lite.music.converter.playlist.PlaylistConverter;
import ru.vtosters.lite.music.downloader.PlaylistDownloader;
import ru.vtosters.lite.music.downloader.TrackDownloader;
import ru.vtosters.lite.music.notification.MusicNotificationBuilder;
import ru.vtosters.lite.utils.AndroidUtils;
import ru.vtosters.lite.utils.IOUtils;
@@ -40,7 +37,6 @@ import ru.vtosters.lite.utils.IOUtils;
public class AudioDownloader {
private static final Executor executor = Executors.newFixedThreadPool(4);
private static NotificationManagerCompat notificationManager = NotificationChannels.getNotificationManager();
public static void downloadPlaylist(Playlist playlist) {
var tracks = PlaylistConverter.getPlaylist(playlist);
@@ -52,13 +48,13 @@ public class AudioDownloader {
var downloadPath = musicPath + File.separator + playlistName;
var notificationId = playlistName.hashCode();
var notification = buildPlaylistDownloadNotification(playlistName, notificationId);
var notification = MusicNotificationBuilder.buildPlaylistDownloadNotification(playlistName, notificationId);
PlaylistDownloader.downloadPlaylist(
tracksWithUrls,
IOUtils.getValidFileName(playlist.g),
downloadPath,
buildPlaylistCallback(tracksWithUrls.size(), notification, notificationId)
MusicCallbackBuilder.buildPlaylistCallback(tracksWithUrls.size(), notification, notificationId)
);
}
@@ -78,86 +74,9 @@ public class AudioDownloader {
: Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath();
var tempId = track.d;
var downloadPath = musicPath + File.separator;
var notification = buildDownloadNotification(track, tempId);
var notification = MusicNotificationBuilder.buildDownloadNotification(track, tempId);
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;
@Override
public void onProgress(int progress) {
if (elapsedTime > 1000) {
notification.setProgress(100, progress, false);
notificationManager.notify(notificationId, notification.build());
startTime = System.currentTimeMillis();
elapsedTime = 0;
} else {
elapsedTime = System.currentTimeMillis() - startTime;
}
}
@Override
public void onSuccess() {
try {
notification
.setContentText(getString("player_download_finished"))
.setProgress(0, 0, false)
.setSmallIcon(android.R.drawable.stat_sys_download_done);
notificationManager.notify(notificationId, notification.build());
} catch (UnsatisfiedLinkError e) {
Log.e("AudioDownloader", "native libs");
Log.e("AudioDownloader", e.getMessage());
onFailure();
}
}
@Override
public void onFailure() {
notification.setContentText(getString("load_audio_error")).setProgress(0, 0, false);
notificationManager.notify(notificationId, notification.build());
}
@Override
public void onSizeReceived(long size, long header) {
}
};
}
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(String.format(getString("playlist_download_notf"), progress, playlistSize));
notification.setProgress(playlistSize, progress, false);
}
notificationManager.notify(notificationId, notification.build());
}
@Override
public void onSuccess() {
notification.setContentText(getString("player_download_finished"));
notification.setProgress(0, 0, false);
}
@Override
public void onFailure() {
}
@Override
public void onSizeReceived(long size, long header) {
}
};
TrackDownloader.downloadTrack(track, downloadPath, MusicCallbackBuilder.buildOneTrackCallback(tempId, notification), cache);
}
public static void cacheTrack(MusicTrack track) {
@@ -199,30 +118,4 @@ public class AudioDownloader {
NotificationChannels.getNotificationManager().notify(track.y1().hashCode(), builder.build());
}
private static NotificationCompat.Builder buildDownloadNotification(MusicTrack track, int id) {
var notificationBuilder = new NotificationCompat.Builder(AndroidUtils.getGlobalContext(), NotificationChannels.MUSIC_DOWNLOAD_CHANNEL_ID)
.setSmallIcon(android.R.drawable.stat_sys_download)
.setContentTitle(getString("audio_downloading"))
.setContentText(track.toString())
.setPriority(NotificationCompat.PRIORITY_LOW)
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
.setAutoCancel(true)
.setProgress(100, 0, false);
notificationManager.notify(id, notificationBuilder.build());
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(getString("cached_playlist_loading") + " «" + playlistName + "»")
.setContentText(playlistName)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
.setAutoCancel(true)
.setProgress(100, 0, false);
notificationManager.notify(id, notificationBuilder.build());
return notificationBuilder;
}
}
@@ -0,0 +1,93 @@
package ru.vtosters.lite.music.callback;
import static ru.vtosters.lite.utils.AndroidUtils.getString;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import ru.vtosters.lite.downloaders.notifications.NotificationChannels;
import ru.vtosters.lite.music.Callback;
public class MusicCallbackBuilder {
private static NotificationManagerCompat notificationManager = NotificationChannels.getNotificationManager();
public static Callback buildOneTrackCallback(int notificationId, NotificationCompat.Builder notification) {
return new Callback() {
long startTime = 0;
long elapsedTime = 0;
@Override
public void onProgress(int progress) {
if (elapsedTime > 1000) {
notification.setProgress(100, progress, false);
notificationManager.notify(notificationId, notification.build());
startTime = System.currentTimeMillis();
elapsedTime = 0;
} else {
elapsedTime = System.currentTimeMillis() - startTime;
}
}
@Override
public void onSuccess() {
try {
notification
.setContentText(getString("player_download_finished"))
.setProgress(0, 0, false)
.setSmallIcon(android.R.drawable.stat_sys_download_done);
notificationManager.notify(notificationId, notification.build());
} catch (UnsatisfiedLinkError e) {
Log.e("AudioDownloader", "native libs");
Log.e("AudioDownloader", e.getMessage());
onFailure();
}
}
@Override
public void onFailure() {
notification.setContentText(getString("load_audio_error")).setProgress(0, 0, false);
notificationManager.notify(notificationId, notification.build());
}
@Override
public void onSizeReceived(long size, long header) {
}
};
}
public 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(String.format(getString("playlist_download_notf"), progress, playlistSize));
notification.setProgress(playlistSize, progress, false);
}
notificationManager.notify(notificationId, notification.build());
}
@Override
public void onSuccess() {
notification.setContentText(getString("player_download_finished"));
notification.setProgress(0, 0, false);
}
@Override
public void onFailure() {
}
@Override
public void onSizeReceived(long size, long header) {
}
};
}
}
@@ -0,0 +1,41 @@
package ru.vtosters.lite.music.notification;
import static ru.vtosters.lite.utils.AndroidUtils.getString;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.vk.dto.music.MusicTrack;
import ru.vtosters.lite.downloaders.notifications.NotificationChannels;
import ru.vtosters.lite.utils.AndroidUtils;
public class MusicNotificationBuilder {
private static NotificationManagerCompat notificationManager = NotificationChannels.getNotificationManager();
public static NotificationCompat.Builder buildDownloadNotification(MusicTrack track, int id) {
var notificationBuilder = new NotificationCompat.Builder(AndroidUtils.getGlobalContext(), NotificationChannels.MUSIC_DOWNLOAD_CHANNEL_ID)
.setSmallIcon(android.R.drawable.stat_sys_download)
.setContentTitle(getString("audio_downloading"))
.setContentText(track.toString())
.setPriority(NotificationCompat.PRIORITY_LOW)
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
.setAutoCancel(true)
.setProgress(100, 0, false);
notificationManager.notify(id, notificationBuilder.build());
return notificationBuilder;
}
public 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(getString("cached_playlist_loading") + " «" + playlistName + "»")
.setContentText(playlistName)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
.setAutoCancel(true)
.setProgress(100, 0, false);
notificationManager.notify(id, notificationBuilder.build());
return notificationBuilder;
}
}