Prevent crash in CoreFileTransferService if Core's thread is busy and doesn't trigger notification fast enough

This commit is contained in:
Sylvain Berfini 2024-08-26 10:40:38 +02:00
parent 4a75315240
commit ce1c3dad65

View file

@ -23,6 +23,7 @@ import android.Manifest
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.IBinder import android.os.IBinder
import androidx.annotation.AnyThread
import androidx.annotation.MainThread import androidx.annotation.MainThread
import androidx.annotation.WorkerThread import androidx.annotation.WorkerThread
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
@ -92,6 +93,7 @@ class CoreFileTransferService : FileTransferService() {
.setOngoing(true) .setOngoing(true)
.setProgress(0, 0, true) .setProgress(0, 0, true)
.build() .build()
postNotification()
coreContext.postOnCoreThread { core -> coreContext.postOnCoreThread { core ->
val downloadingFilesCount = core.remainingDownloadFileCount val downloadingFilesCount = core.remainingDownloadFileCount
@ -128,17 +130,26 @@ class CoreFileTransferService : FileTransferService() {
} else if (uploadingFilesCount > 0) { } else if (uploadingFilesCount > 0) {
uploadText uploadText
} else { } else {
"" getString(R.string.notification_file_transfer_title)
} }
mServiceNotification = builder.setContentText(message).build() mServiceNotification = builder.setContentText(message).build()
postNotification()
}
@AnyThread
private fun postNotification() {
val notificationsManager = NotificationManagerCompat.from(this) val notificationsManager = NotificationManagerCompat.from(this)
if (ActivityCompat.checkSelfPermission( if (ActivityCompat.checkSelfPermission(
this, this,
Manifest.permission.POST_NOTIFICATIONS Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED ) == PackageManager.PERMISSION_GRANTED
) { ) {
notificationsManager.notify(SERVICE_NOTIF_ID, mServiceNotification) if (mServiceNotification != null) {
notificationsManager.notify(SERVICE_NOTIF_ID, mServiceNotification)
} else {
Log.e("$TAG Notification content hasn't been computed yet!")
}
} else { } else {
Log.e("$TAG POST_NOTIFICATIONS permission wasn't granted!") Log.e("$TAG POST_NOTIFICATIONS permission wasn't granted!")
} }