add to db from dl success
This commit is contained in:
@@ -2,13 +2,13 @@ package ru.vtosters.lite.downloaders;
|
||||
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import bruhcollective.itaysonlab.libvkx.client.LibVKXClient;
|
||||
import com.vk.core.util.ToastUtils;
|
||||
import com.vk.dto.music.MusicTrack;
|
||||
import com.vk.dto.music.Playlist;
|
||||
import com.vtosters.lite.R;
|
||||
|
||||
import java8.util.concurrent.CompletableFuture;
|
||||
import ru.vtosters.hooks.music.MusicCacheFilesHook;
|
||||
import ru.vtosters.hooks.other.Preferences;
|
||||
import ru.vtosters.lite.music.cache.MusicCacheImpl;
|
||||
@@ -18,12 +18,14 @@ import ru.vtosters.lite.music.converter.playlist.PlaylistConverter;
|
||||
import ru.vtosters.lite.music.downloader.AudioGet;
|
||||
import ru.vtosters.lite.music.downloader.PlaylistDownloader;
|
||||
import ru.vtosters.lite.music.downloader.TrackDownloader;
|
||||
import ru.vtosters.lite.music.interfaces.Callback;
|
||||
import ru.vtosters.lite.music.notification.MusicNotificationBuilder;
|
||||
import ru.vtosters.lite.utils.AccountManagerUtils;
|
||||
import ru.vtosters.lite.utils.AndroidUtils;
|
||||
import ru.vtosters.lite.utils.IOUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import static ru.vtosters.lite.utils.AndroidUtils.getString;
|
||||
@@ -37,18 +39,18 @@ import static ru.vtosters.lite.utils.AndroidUtils.getString;
|
||||
public class AudioDownloader {
|
||||
public static final String dlpath = Preferences.getBoolValue("dldir", false) ? Environment.DIRECTORY_DOWNLOADS : Environment.DIRECTORY_MUSIC;
|
||||
|
||||
public static CompletableFuture<Void> downloadPlaylist(Playlist playlist) {
|
||||
var tracks = PlaylistConverter.getPlaylist(playlist);
|
||||
public static void downloadPlaylist(Playlist playlist) {
|
||||
List<MusicTrack> tracks = PlaylistConverter.getPlaylist(playlist);
|
||||
|
||||
var playlistName = IOUtils.getValidFileName(playlist.g);
|
||||
String playlistName = IOUtils.getValidFileName(playlist.g);
|
||||
|
||||
var musicPath = Environment.getExternalStoragePublicDirectory(dlpath).getAbsolutePath();
|
||||
var downloadPath = musicPath + File.separator + playlistName;
|
||||
String musicPath = Environment.getExternalStoragePublicDirectory(dlpath).getAbsolutePath();
|
||||
String downloadPath = musicPath + File.separator + playlistName;
|
||||
|
||||
var notificationId = playlistName.hashCode();
|
||||
var notification = MusicNotificationBuilder.buildPlaylistDownloadNotification(playlistName, notificationId);
|
||||
int notificationId = playlistName.hashCode();
|
||||
NotificationCompat.Builder notification = MusicNotificationBuilder.buildPlaylistDownloadNotification(playlistName, notificationId);
|
||||
|
||||
return PlaylistDownloader.downloadPlaylist(
|
||||
PlaylistDownloader.downloadPlaylist(
|
||||
tracks,
|
||||
IOUtils.getValidFileName(playlist.g),
|
||||
downloadPath,
|
||||
@@ -62,30 +64,31 @@ public class AudioDownloader {
|
||||
}
|
||||
|
||||
public static void cacheTrack(MusicTrack track) {
|
||||
var trackId = LibVKXClient.asId(track);
|
||||
String trackId = LibVKXClient.asId(track);
|
||||
|
||||
if (MusicCacheImpl.isCachedTrack(trackId)) {
|
||||
MusicCacheImpl.removeTrack(trackId);
|
||||
AndroidUtils.sendToast(AndroidUtils.getString("audio_deleted_from_cache"));
|
||||
} else {
|
||||
var trackFile = MusicCacheFilesHook.getTrackFile(trackId);
|
||||
if (!trackFile.exists())
|
||||
File trackFile = MusicCacheFilesHook.getTrackFile(trackId);
|
||||
if (!trackFile.exists()) {
|
||||
trackFile.getParentFile().mkdirs();
|
||||
}
|
||||
downloadM3U8(track, true);
|
||||
}
|
||||
}
|
||||
|
||||
public static CompletableFuture<Void> cachePlaylist(Playlist playlist) {
|
||||
var tracks = PlaylistConverter.getPlaylist(playlist);
|
||||
public static void cachePlaylist(Playlist playlist) {
|
||||
List<MusicTrack> tracks = PlaylistConverter.getPlaylist(playlist);
|
||||
|
||||
var notificationId = playlist.g.hashCode();
|
||||
var notification = MusicNotificationBuilder.buildPlaylistDownloadNotification(playlist.g, notificationId);
|
||||
int notificationId = playlist.g.hashCode();
|
||||
NotificationCompat.Builder notification = MusicNotificationBuilder.buildPlaylistDownloadNotification(playlist.g, notificationId);
|
||||
|
||||
PlaylistCacheDbDelegate.addPlaylist(AndroidUtils.getGlobalContext(), playlist);
|
||||
|
||||
Log.d("Playlist", "adding to cache " + playlist.a);
|
||||
|
||||
return PlaylistDownloader.cachePlaylist(
|
||||
PlaylistDownloader.cachePlaylist(
|
||||
tracks,
|
||||
MusicCallbackBuilder.buildPlaylistCallback(tracks.size(), notification, notificationId),
|
||||
playlist.v1()
|
||||
@@ -93,10 +96,14 @@ public class AudioDownloader {
|
||||
}
|
||||
|
||||
public static void cacheAllAudios() {
|
||||
var tracks = AudioGet.getAudios();
|
||||
List<MusicTrack> tracks = AudioGet.getAudios();
|
||||
|
||||
var notificationId = AccountManagerUtils.getUserId();
|
||||
var notification = MusicNotificationBuilder.buildAllAudiosDownloadNotification(notificationId);
|
||||
int notificationId = AccountManagerUtils.getUserId();
|
||||
NotificationCompat.Builder notification = MusicNotificationBuilder.buildAllAudiosDownloadNotification(notificationId);
|
||||
|
||||
if (tracks == null || tracks.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
PlaylistDownloader.cachePlaylist(
|
||||
tracks,
|
||||
@@ -106,15 +113,19 @@ public class AudioDownloader {
|
||||
}
|
||||
|
||||
public static void downloadAllAudios() {
|
||||
var tracks = AudioGet.getAudios();
|
||||
List<MusicTrack> tracks = AudioGet.getAudios();
|
||||
|
||||
var notificationId = AccountManagerUtils.getUserId();
|
||||
var notification = MusicNotificationBuilder.buildAllAudiosDownloadNotification(notificationId);
|
||||
int notificationId = AccountManagerUtils.getUserId();
|
||||
NotificationCompat.Builder notification = MusicNotificationBuilder.buildAllAudiosDownloadNotification(notificationId);
|
||||
|
||||
var playlistName = "Audios of " + AccountManagerUtils.getUserId();
|
||||
String playlistName = "Audios of " + AccountManagerUtils.getUserId();
|
||||
|
||||
var musicPath = Environment.getExternalStoragePublicDirectory(dlpath).getAbsolutePath();
|
||||
var downloadPath = musicPath + File.separator + playlistName;
|
||||
String musicPath = Environment.getExternalStoragePublicDirectory(dlpath).getAbsolutePath();
|
||||
String downloadPath = musicPath + File.separator + playlistName;
|
||||
|
||||
if (tracks == null || tracks.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
PlaylistDownloader.downloadPlaylist(
|
||||
tracks,
|
||||
@@ -124,21 +135,19 @@ public class AudioDownloader {
|
||||
);
|
||||
}
|
||||
|
||||
private static void
|
||||
downloadM3U8(MusicTrack track, boolean cache) {
|
||||
private static void downloadM3U8(MusicTrack track, boolean cache) {
|
||||
Objects.requireNonNull(track.D, () -> {
|
||||
ToastUtils.a(getString(R.string.link_audio_error));
|
||||
return "link must not be null";
|
||||
});
|
||||
var musicPath = Environment.getExternalStoragePublicDirectory(dlpath).getAbsolutePath();
|
||||
var tempId = track.d;
|
||||
var downloadPath = musicPath + File.separator;
|
||||
var notification = MusicNotificationBuilder.buildDownloadNotification(track, tempId);
|
||||
String musicPath = Environment.getExternalStoragePublicDirectory(dlpath).getAbsolutePath();
|
||||
int tempId = track.d;
|
||||
String downloadPath = musicPath + File.separator;
|
||||
NotificationCompat.Builder notification = MusicNotificationBuilder.buildDownloadNotification(track, tempId);
|
||||
|
||||
var cb = MusicCallbackBuilder.buildOneTrackCallback(tempId, notification);
|
||||
Callback cb = MusicCallbackBuilder.buildOneTrackCallback(tempId, notification);
|
||||
if (cache) {
|
||||
TrackDownloader.cacheTrack(track, cb);
|
||||
PlaylistCacheDbDelegate.addTrackToPlaylist(AndroidUtils.getGlobalContext(), AccountManagerUtils.getUserId() + "_-1", track.y1()); // TODO Move to download success
|
||||
TrackDownloader.cacheTrack(track, cb, AccountManagerUtils.getUserId() + "_-1");
|
||||
} else {
|
||||
TrackDownloader.downloadTrack(track, downloadPath, cb);
|
||||
}
|
||||
|
||||
@@ -96,4 +96,4 @@ public class MusicCacheImpl {
|
||||
MusicCacheDbDelegate.drop(AndroidUtils.getGlobalContext());
|
||||
MusicCacheStorageUtils.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,10 @@ package ru.vtosters.lite.music.downloader;
|
||||
import com.vk.dto.music.MusicTrack;
|
||||
|
||||
import ru.vtosters.lite.music.cache.MusicCacheImpl;
|
||||
import ru.vtosters.lite.music.cache.delegate.PlaylistCacheDbDelegate;
|
||||
import ru.vtosters.lite.music.interfaces.Callback;
|
||||
import ru.vtosters.lite.music.interfaces.ITrackDownloader;
|
||||
import ru.vtosters.lite.utils.AndroidUtils;
|
||||
|
||||
public final class CachedDownloader implements ITrackDownloader {
|
||||
|
||||
@@ -14,9 +16,8 @@ public final class CachedDownloader implements ITrackDownloader {
|
||||
this.origin = origin;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void download(MusicTrack track, Callback callback) {
|
||||
public void download(MusicTrack track, Callback callback, String playlistId) {
|
||||
origin.download(track, new Callback() {
|
||||
@Override
|
||||
public void onProgress(int progress) {
|
||||
@@ -26,17 +27,25 @@ public final class CachedDownloader implements ITrackDownloader {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
new ThumbnailDownloader().download(track, new Callback() {
|
||||
@Override public void onProgress(int progress) {}
|
||||
@Override public void onProgress(int progress) {
|
||||
|
||||
@Override public void onSuccess() {}
|
||||
}
|
||||
|
||||
@Override public void onSuccess() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable e) {
|
||||
callback.onFailure(e);
|
||||
}
|
||||
|
||||
@Override public void onSizeReceived(long size, long header) {}
|
||||
});
|
||||
@Override public void onSizeReceived(long size, long header) {
|
||||
|
||||
}
|
||||
}, null);
|
||||
|
||||
PlaylistCacheDbDelegate.addTrackToPlaylist(AndroidUtils.getGlobalContext(), playlistId, track.y1());
|
||||
MusicCacheImpl.addTrack(track);
|
||||
callback.onSuccess();
|
||||
}
|
||||
@@ -50,6 +59,6 @@ public final class CachedDownloader implements ITrackDownloader {
|
||||
public void onSizeReceived(long size, long header) {
|
||||
callback.onSizeReceived(size, header);
|
||||
}
|
||||
});
|
||||
}, playlistId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ public final class Mp3Downloader implements ITrackDownloader {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void download(MusicTrack track, Callback callback) {
|
||||
public void download(MusicTrack track, Callback callback, String playlistId) {
|
||||
String uri = track.D;
|
||||
if (Objects.requireNonNull(uri).isEmpty()) {
|
||||
String msg = "link error: " + track.y1() + ", title: " + Mp3Downloader.getTitle(track);
|
||||
|
||||
@@ -12,7 +12,6 @@ import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import java8.util.concurrent.CompletableFuture;
|
||||
import ru.vtosters.lite.music.interfaces.Callback;
|
||||
|
||||
public class PlaylistDownloader {
|
||||
public static CompletableFuture<Void>
|
||||
@@ -38,10 +37,8 @@ public class PlaylistDownloader {
|
||||
return CompletableFuture.allOf(playlist
|
||||
.stream()
|
||||
.map(x -> {
|
||||
Callback.CompletableFutureCallback d = new
|
||||
Callback.CompletableFutureCallback(delegate);
|
||||
TrackDownloader.cacheTrack(x, d);
|
||||
PlaylistCacheDbDelegate.addTrackToPlaylist(AndroidUtils.getGlobalContext(), playlistId, x.y1());
|
||||
Callback.CompletableFutureCallback d = new Callback.CompletableFutureCallback(delegate);
|
||||
TrackDownloader.cacheTrack(x, d, playlistId);
|
||||
return d;
|
||||
})
|
||||
.toArray(CompletableFuture[]::new));
|
||||
|
||||
@@ -17,7 +17,7 @@ import ru.vtosters.lite.utils.music.MusicCacheStorageUtils;
|
||||
public final class ThumbnailDownloader implements ITrackDownloader {
|
||||
|
||||
@Override
|
||||
public void download(MusicTrack track, Callback callback) {
|
||||
public void download(MusicTrack track, Callback callback, String playlistId) {
|
||||
JSONObject json = track.J();
|
||||
JSONObject album = json.optJSONObject("album");
|
||||
if (album == null) return;
|
||||
|
||||
@@ -21,25 +21,24 @@ public final class TrackDownloader {
|
||||
public static void downloadTrack(MusicTrack track, String path, Callback callback) {
|
||||
File outDir = new File(path);
|
||||
if (!outDir.exists()) {
|
||||
if (outDir.mkdir())
|
||||
if (outDir.mkdir()) {
|
||||
Log.v("TrackDownloader", "Directory created");
|
||||
else
|
||||
} else {
|
||||
Log.e("TrackDownloader", "Directory creation failed");
|
||||
}
|
||||
}
|
||||
var outputFile = new File(outDir, IOUtils.getValidFileName(
|
||||
MusicTrackUtils.getArtists(track) + " - " + Mp3Downloader.getTitle(track)) + ".mp3");
|
||||
File outputFile = new File(outDir, IOUtils.getValidFileName(MusicTrackUtils.getArtists(track) + " - " + Mp3Downloader.getTitle(track)) + ".mp3");
|
||||
|
||||
VTExecutors.getMusicDownloadExecutor().submit(() -> new Mp3Downloader(outputFile).download(track, callback));
|
||||
VTExecutors.getMusicDownloadExecutor().submit(() -> new Mp3Downloader(outputFile).download(track, callback, null));
|
||||
}
|
||||
|
||||
public static void cacheTrack(MusicTrack track, Callback callback) {
|
||||
if (MusicCacheImpl.isCachedTrack(LibVKXClient.asId(track)))
|
||||
public static void cacheTrack(MusicTrack track, Callback callback, String playlistId) {
|
||||
if (MusicCacheImpl.isCachedTrack(LibVKXClient.asId(track))) {
|
||||
return;
|
||||
var outputFile = MusicCacheStorageUtils
|
||||
.getTrackFile(LibVKXClient.asId(track));
|
||||
}
|
||||
|
||||
VTExecutors.getMusicDownloadExecutor().submit(() -> new CachedDownloader(
|
||||
new Mp3Downloader(outputFile)
|
||||
).download(track, callback));
|
||||
File outputFile = MusicCacheStorageUtils.getTrackFile(LibVKXClient.asId(track));
|
||||
|
||||
VTExecutors.getMusicDownloadExecutor().submit(() -> new CachedDownloader(new Mp3Downloader(outputFile)).download(track, callback, playlistId));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,9 @@ public interface ITrackDownloader {
|
||||
/**
|
||||
* Download track
|
||||
*
|
||||
* @param track Music track
|
||||
* @param callback Callback to call when download status changes
|
||||
* @param track Music track
|
||||
* @param callback Callback to call when download status changes
|
||||
* @param playlistId
|
||||
*/
|
||||
void download(MusicTrack track, Callback callback);
|
||||
void download(MusicTrack track, Callback callback, String playlistId);
|
||||
}
|
||||
Reference in New Issue
Block a user