diff --git a/app/src/main/java/ru/vtosters/lite/concurrent/VTExecutors.java b/app/src/main/java/ru/vtosters/lite/concurrent/VTExecutors.java index ecd93ebf8..a20eaa1ed 100644 --- a/app/src/main/java/ru/vtosters/lite/concurrent/VTExecutors.java +++ b/app/src/main/java/ru/vtosters/lite/concurrent/VTExecutors.java @@ -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; + } + } } diff --git a/app/src/main/java/ru/vtosters/lite/hooks/ui/MusicBottomSheetHook.java b/app/src/main/java/ru/vtosters/lite/hooks/ui/MusicBottomSheetHook.java index 747ce9dff..eb7caa6d3 100644 --- a/app/src/main/java/ru/vtosters/lite/hooks/ui/MusicBottomSheetHook.java +++ b/app/src/main/java/ru/vtosters/lite/hooks/ui/MusicBottomSheetHook.java @@ -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 hook(ArrayList 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; } diff --git a/app/src/main/java/ru/vtosters/lite/music/M3UDownloader.java b/app/src/main/java/ru/vtosters/lite/music/M3UDownloader.java index c05748152..8d30bc9b2 100644 --- a/app/src/main/java/ru/vtosters/lite/music/M3UDownloader.java +++ b/app/src/main/java/ru/vtosters/lite/music/M3UDownloader.java @@ -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; diff --git a/app/src/main/java/ru/vtosters/lite/music/downloader/TrackDownloader.java b/app/src/main/java/ru/vtosters/lite/music/downloader/TrackDownloader.java index 384c0e6b5..c6414bda0 100644 --- a/app/src/main/java/ru/vtosters/lite/music/downloader/TrackDownloader.java +++ b/app/src/main/java/ru/vtosters/lite/music/downloader/TrackDownloader.java @@ -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) + ); } }