use an executor to download tracks

This commit is contained in:
aliveoutside
2022-10-13 02:52:23 +03:00
parent ede17b31a2
commit 9d4bffd8aa
4 changed files with 23 additions and 10 deletions
@@ -3,10 +3,14 @@ package ru.vtosters.lite.concurrent;
import com.vk.core.concurrent.VkExecutors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import io.reactivex.Scheduler;
public class VTExecutors {
public static ExecutorService getMusicDownloadExecutor() {
return VTMusicDownloadExecutor.getInstance();
}
public static ExecutorService getIoExecutor() {
return VkExecutors.x.e();
@@ -19,4 +23,16 @@ public class VTExecutors {
public static Scheduler getSlowTasksScheduler() {
return VkExecutors.x.q();
}
static class VTMusicDownloadExecutor {
private VTMusicDownloadExecutor() {}
private static class LazyHolder {
public static final ExecutorService INSTANCE = Executors.newSingleThreadExecutor();
}
public static ExecutorService getInstance() {
return LazyHolder.INSTANCE;
}
}
}
@@ -19,15 +19,11 @@ import com.vk.music.common.MusicPlaybackLaunchContext;
import com.vtosters.lite.R;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import bruhcollective.itaysonlab.libvkx.client.LibVKXClientImpl;
import ru.vtosters.lite.downloaders.AudioDownloader;
public class MusicBottomSheetHook {
private static final ExecutorService executor = Executors.newCachedThreadPool();
public static ArrayList<MusicAction> hook(ArrayList<MusicAction> actions, MusicTrack musicTrack) {
if (musicTrack.F1()) return actions;
@@ -149,7 +145,7 @@ public class MusicBottomSheetHook {
return tryPlayInVKX(null, null, playlist);
if (actionId == R.id.add_to_cache) {
executor.submit(() -> AudioDownloader.cachePlaylist(playlist));
AudioDownloader.cachePlaylist(playlist);
return true;
}
@@ -183,7 +179,7 @@ public class MusicBottomSheetHook {
}
});
} else {
executor.submit(() -> AudioDownloader.downloadPlaylist(playlist));
AudioDownloader.downloadPlaylist(playlist);
}
return true;
}
@@ -10,8 +10,6 @@ import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import javax.crypto.NoSuchPaddingException;
@@ -28,7 +26,6 @@ import ru.vtosters.lite.utils.IOUtils;
public class M3UDownloader implements ITrackDownloader {
private static final OkHttpClient client = new OkHttpClient();
private ExecutorService executorService = Executors.newSingleThreadExecutor();
public static M3UDownloader getInstance() {
return Holder.INSTANCE;
@@ -7,6 +7,7 @@ import com.vk.dto.music.MusicTrack;
import java.io.File;
import bruhcollective.itaysonlab.libvkx.client.LibVKXClient;
import ru.vtosters.lite.concurrent.VTExecutors;
import ru.vtosters.lite.music.Callback;
import ru.vtosters.lite.music.M3UDownloader;
import ru.vtosters.lite.music.cache.CacheDatabaseDelegate;
@@ -36,6 +37,9 @@ public class TrackDownloader {
Log.v("TrackDownloader", "Directory created");
else
Log.e("TrackDownloader", "Directory creation failed");
downloader.downloadTrack(track, outDir, callback, cache);
VTExecutors.getMusicDownloadExecutor().execute(
() -> downloader.downloadTrack(track, outDir, callback, cache)
);
}
}