0
0
mirror of https://github.com/mediathekview/zapp.git synced 2024-09-20 04:12:14 +02:00

Check notification permissions before posting notifications

This commit is contained in:
Christine Coenen 2023-03-12 15:13:24 +01:00
parent ed787e2b34
commit a5ec13e710
3 changed files with 33 additions and 1 deletions

View File

@ -1,5 +1,6 @@
package de.christinecoenen.code.zapp.app.mediathek.controller.downloads
import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Context
import androidx.core.app.NotificationManagerCompat
@ -8,6 +9,7 @@ import de.christinecoenen.code.zapp.app.mediathek.controller.downloads.notificat
import de.christinecoenen.code.zapp.app.mediathek.controller.downloads.notifications.DownloadFailedEventNotification
import de.christinecoenen.code.zapp.app.mediathek.controller.downloads.notifications.DownloadProgressNotification
import de.christinecoenen.code.zapp.models.shows.Quality
import de.christinecoenen.code.zapp.utils.system.NotificationHelper
import kotlinx.coroutines.*
import okhttp3.OkHttpClient
import okhttp3.Request
@ -158,7 +160,12 @@ class DownloadWorker(appContext: Context, workerParams: WorkerParameters) :
return@withContext success()
}
@SuppressLint("MissingPermission")
private fun success(): Result {
if (!NotificationHelper.hasNotificationPermissionGranted(applicationContext)) {
return Result.success()
}
MainScope().launch {
delay(NotificationDelay)
@ -167,13 +174,19 @@ class DownloadWorker(appContext: Context, workerParams: WorkerParameters) :
title,
persistedShowId
)
notificationManager.notify(notificationId, notification.build())
}
return Result.success()
}
@SuppressLint("MissingPermission")
private fun failure(errorType: ErrorType): Result {
if (!NotificationHelper.hasNotificationPermissionGranted(applicationContext)) {
return Result.failure()
}
MainScope().launch {
delay(NotificationDelay)

View File

@ -1,5 +1,6 @@
package de.christinecoenen.code.zapp.app.mediathek.controller.downloads
import android.annotation.SuppressLint
import android.content.Context
import android.net.ConnectivityManager
import android.net.Uri
@ -224,11 +225,14 @@ class WorkManagerDownloadController(
}
}
@SuppressLint("MissingPermission")
private fun showStatusChangeNotificationIfNeeded(
workInfo: WorkInfo,
show: PersistedMediathekShow
) {
if (!notificationManager.areNotificationsEnabled()) {
if (!notificationManager.areNotificationsEnabled() ||
!NotificationHelper.hasNotificationPermissionGranted(applicationContext)
) {
return
}

View File

@ -1,8 +1,12 @@
package de.christinecoenen.code.zapp.utils.system
import android.Manifest
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.app.ActivityCompat
import de.christinecoenen.code.zapp.R
import timber.log.Timber
@ -14,6 +18,17 @@ object NotificationHelper {
const val CHANNEL_ID_DOWNLOAD_PROGRESS = "download_progress"
const val CHANNEL_ID_DOWNLOAD_EVENT = "download_event"
fun hasNotificationPermissionGranted(context: Context): Boolean {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
return true
}
return ActivityCompat.checkSelfPermission(
context,
Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED;
}
@JvmStatic
fun createBackgroundPlaybackChannel(context: Context) {
createNotificationChannel(