Potential fix for #3247

This commit is contained in:
Koen J
2026-04-22 10:23:40 +02:00
parent 80c9b27d48
commit fcab0f5ee5
@@ -28,6 +28,7 @@ import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import java.net.InetAddress import java.net.InetAddress
@@ -41,22 +42,16 @@ class DownloadService : Service() {
private val TAG = "DownloadService"; private val TAG = "DownloadService";
private val DOWNLOAD_NOTIF_ID = 3; private val DOWNLOAD_NOTIF_ID = 3;
private val DOWNLOAD_NOTIF_TAG = "download";
private val DOWNLOAD_NOTIF_CHANNEL_ID = "downloadChannel"; private val DOWNLOAD_NOTIF_CHANNEL_ID = "downloadChannel";
private val DOWNLOAD_NOTIF_CHANNEL_NAME = "Downloads"; private val DOWNLOAD_NOTIF_CHANNEL_NAME = "Downloads";
//Context //Context
private val _scope: CoroutineScope = CoroutineScope(Dispatchers.Default); private val _scope: CoroutineScope = CoroutineScope(Dispatchers.IO);
private var _notificationManager: NotificationManager? = null; private var _notificationManager: NotificationManager? = null;
private var _notificationChannel: NotificationChannel? = null; private var _notificationChannel: NotificationChannel? = null;
private var _isForeground = false private var _isForeground = false
private val _client = ManagedHttpClient(OkHttpClient.Builder() private lateinit var _client: ManagedHttpClient
//.proxy(Proxy(Proxy.Type.HTTP, InetSocketAddress(InetAddress.getByName("192.168.1.175"), 8081)))
.readTimeout(Duration.ofMinutes(0))
.writeTimeout(Duration.ofMinutes(0))
.connectTimeout(Duration.ofSeconds(100))
.callTimeout(Duration.ofMinutes(0)))
private var _started = false; private var _started = false;
@@ -76,11 +71,23 @@ class DownloadService : Service() {
setupNotificationRequirements(); setupNotificationRequirements();
notifyDownload(null); notifyDownload(null);
if (StateDownloads.instance.getDownloading().isEmpty()) {
Logger.i(TAG, "No downloads queued, stopping service")
closeDownloadSession()
return START_NOT_STICKY
}
_callOnStarted?.invoke(this); _callOnStarted?.invoke(this);
_instance = this; _instance = this;
_scope.launch { _scope.launch {
try { try {
_client = ManagedHttpClient(OkHttpClient.Builder()
//.proxy(Proxy(Proxy.Type.HTTP, InetSocketAddress(InetAddress.getByName("192.168.1.175"), 8081)))
.readTimeout(Duration.ofMinutes(0))
.writeTimeout(Duration.ofMinutes(0))
.connectTimeout(Duration.ofSeconds(100))
.callTimeout(Duration.ofMinutes(0)))
doDownloading(); doDownloading();
} }
catch(ex: Throwable) { catch(ex: Throwable) {
@@ -167,7 +174,7 @@ class DownloadService : Service() {
ignore.add(currentVideo); ignore.add(currentVideo);
//Give it a sec //Give it a sec
Thread.sleep(500); delay(500);
} }
catch(ex: Throwable) { catch(ex: Throwable) {
//if(ex is ScriptReloadRequiredException) //if(ex is ScriptReloadRequiredException)
@@ -200,7 +207,7 @@ class DownloadService : Service() {
} }
//Give it a sec //Give it a sec
Thread.sleep(500); delay(500);
} }
StateDownloads.instance.updateDownloading(currentVideo); StateDownloads.instance.updateDownloading(currentVideo);
@@ -338,6 +345,8 @@ class DownloadService : Service() {
fun getOrCreateService(context: Context, handle: ((DownloadService)->Unit)? = null) { fun getOrCreateService(context: Context, handle: ((DownloadService)->Unit)? = null) {
if(!FragmentedStorage.isInitialized) if(!FragmentedStorage.isInitialized)
return; return;
if(StateDownloads.instance.getDownloading().isEmpty())
return
if(_instance == null) { if(_instance == null) {
_callOnStarted = handle; _callOnStarted = handle;
val intent = Intent(context, DownloadService::class.java); val intent = Intent(context, DownloadService::class.java);