Skip to content

Commit 9f5d37a

Browse files
committed
fix(downloads): switch to foreground service
1 parent 9988671 commit 9f5d37a

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

app/src/main/java/org/shirabox/app/ui/activity/downloads/DownloadsViewModel.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.shirabox.app.ui.activity.downloads
22

33
import android.content.Context
4-
import android.content.Intent
54
import android.os.Handler
65
import android.os.Looper
76
import android.widget.Toast
@@ -125,7 +124,7 @@ class DownloadsViewModel @Inject constructor(@ApplicationContext context: Contex
125124
launch { db.downloadDao().deleteDownload(contentAndDownload.second) }
126125
}
127126

128-
context.startService(Intent(context, MediaDownloadsService::class.java))
127+
Util.startForegroundService(context, MediaDownloadsService::class.java)
129128
}
130129
}
131130

app/src/main/java/org/shirabox/app/ui/activity/resource/ResourceViewModel.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.shirabox.app.ui.activity.resource
22

33
import android.content.Context
4-
import android.content.Intent
54
import android.widget.Toast
65
import androidx.compose.runtime.mutableLongStateOf
76
import androidx.compose.runtime.mutableStateListOf
@@ -252,7 +251,7 @@ class ResourceViewModel @Inject constructor(@ApplicationContext context: Context
252251
)
253252
}
254253

255-
context.startService(Intent(context, MediaDownloadsService::class.java))
254+
Util.startForegroundService(context, MediaDownloadsService::class.java)
256255
DownloadsServiceHelper.enqueue(db = db, tasks)
257256
}
258257
}

core/src/main/java/org/shirabox/core/util/Util.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class Util {
177177
}
178178

179179
packageInfo.versionName
180-
} catch (e: Exception) {
180+
} catch (_: Exception) {
181181
null
182182
}
183183
}
@@ -206,5 +206,13 @@ class Util {
206206
}
207207
return false
208208
}
209+
210+
fun startForegroundService(context: Context, clazz: Class<*>) {
211+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
212+
context.startForegroundService(Intent(context, clazz))
213+
} else {
214+
context.startService(Intent(context, clazz))
215+
}
216+
}
209217
}
210218
}

0 commit comments

Comments
 (0)