Skip to content

Commit

Permalink
Set foreground service type for download worker service
Browse files Browse the repository at this point in the history
  • Loading branch information
cemrich committed Jul 21, 2023
1 parent beb5228 commit 5bfffa8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />

<uses-feature
android:name="android.hardware.touchscreen"
Expand Down Expand Up @@ -133,6 +134,11 @@
android:value="true" />
</service>

<service
android:name="androidx.work.impl.foreground.SystemForegroundService"
android:foregroundServiceType="dataSync"
tools:node="merge" />

<receiver
android:name=".app.mediathek.controller.downloads.RetryDownloadBroadcastReceiver"
android:exported="false" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package de.christinecoenen.code.zapp.app.mediathek.controller.downloads
import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Context
import android.content.pm.ServiceInfo
import android.os.Build
import androidx.core.app.NotificationManagerCompat
import androidx.work.*
import de.christinecoenen.code.zapp.app.mediathek.controller.downloads.notifications.DownloadCompletedEventNotification
Expand Down Expand Up @@ -237,10 +239,18 @@ class DownloadWorker(appContext: Context, workerParams: WorkerParameters) :
}

override suspend fun getForegroundInfo() =
ForegroundInfo(
id.hashCode(),
downloadProgressNotification.build(progress, downloadedBytes, totalBytes)
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ForegroundInfo(
id.hashCode(),
downloadProgressNotification.build(progress, downloadedBytes, totalBytes),
ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
)
} else {
ForegroundInfo(
id.hashCode(),
downloadProgressNotification.build(progress, downloadedBytes, totalBytes)
)
}

private suspend fun reportProgress() {
val update = workDataOf(ProgressKey to progress)
Expand All @@ -256,16 +266,21 @@ class DownloadWorker(appContext: Context, workerParams: WorkerParameters) :
HttpURLConnection.HTTP_NOT_FOUND,
HttpURLConnection.HTTP_GONE ->
ErrorType.FileNotFound

HttpURLConnection.HTTP_UNAUTHORIZED,
HttpURLConnection.HTTP_FORBIDDEN,
451 ->
ErrorType.FileForbidden

429 ->
ErrorType.TooManyRequests

in 400..499 ->
ErrorType.ClientError

in 500..600 ->
ErrorType.ServerError

else ->
ErrorType.Unknown
}
Expand Down

0 comments on commit 5bfffa8

Please sign in to comment.