Compare commits

...

2 commits

Author SHA1 Message Date
54b4f61fd7 Improve notification channel creation
All checks were successful
/ build-debug (push) Successful in 5m22s
2024-09-29 21:50:47 -04:00
7661b4b84f Output any foreground task error to Log 2024-09-29 21:45:55 -04:00

View file

@ -4,6 +4,7 @@ import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Binder import android.os.Binder
import android.os.IBinder import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationChannelCompat import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
@ -49,6 +50,7 @@ import net.typeblog.lpac_jni.ProfileDownloadCallback
*/ */
class EuiccChannelManagerService : LifecycleService(), OpenEuiccContextMarker { class EuiccChannelManagerService : LifecycleService(), OpenEuiccContextMarker {
companion object { companion object {
private const val TAG = "EuiccChannelManagerService"
private const val CHANNEL_ID = "tasks" private const val CHANNEL_ID = "tasks"
private const val FOREGROUND_ID = 1000 private const val FOREGROUND_ID = 1000
} }
@ -105,12 +107,18 @@ class EuiccChannelManagerService : LifecycleService(), OpenEuiccContextMarker {
} }
private suspend fun updateForegroundNotification(title: String, iconRes: Int) { private suspend fun updateForegroundNotification(title: String, iconRes: Int) {
val channel = val nm = NotificationManagerCompat.from(this)
NotificationChannelCompat.Builder(CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_LOW) if (nm.getNotificationChannelCompat(CHANNEL_ID) == null) {
.setName(getString(R.string.task_notification)) val channel =
.setVibrationEnabled(false) NotificationChannelCompat.Builder(
.build() CHANNEL_ID,
NotificationManagerCompat.from(this).createNotificationChannel(channel) NotificationManagerCompat.IMPORTANCE_LOW
)
.setName(getString(R.string.task_notification))
.setVibrationEnabled(false)
.build()
nm.createNotificationChannel(channel)
}
val state = foregroundTaskState.value val state = foregroundTaskState.value
@ -127,7 +135,7 @@ class EuiccChannelManagerService : LifecycleService(), OpenEuiccContextMarker {
if (state.progress == 0) { if (state.progress == 0) {
startForeground(FOREGROUND_ID, notification) startForeground(FOREGROUND_ID, notification)
} else if (checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) { } else if (checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
NotificationManagerCompat.from(this).notify(FOREGROUND_ID, notification) nm.notify(FOREGROUND_ID, notification)
} }
// Yield out so that the main looper can handle the notification event // Yield out so that the main looper can handle the notification event
@ -192,6 +200,8 @@ class EuiccChannelManagerService : LifecycleService(), OpenEuiccContextMarker {
// This update will be sent by the subscriber (as shown below) // This update will be sent by the subscriber (as shown below)
foregroundTaskState.value = ForegroundTaskState.Done(null) foregroundTaskState.value = ForegroundTaskState.Done(null)
} catch (t: Throwable) { } catch (t: Throwable) {
Log.e(TAG, "Foreground task encountered an error")
Log.e(TAG, Log.getStackTraceString(t))
foregroundTaskState.value = ForegroundTaskState.Done(t) foregroundTaskState.value = ForegroundTaskState.Done(t)
} finally { } finally {
stopSelf() stopSelf()