From 911bffea3200e9c733bf62320799257cf5fb5e6a Mon Sep 17 00:00:00 2001 From: Conny Duck Date: Tue, 17 Apr 2018 20:07:14 +0200 Subject: [PATCH] fix problem where send notification would stay on forever --- .../tusky/service/SendTootService.kt | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/service/SendTootService.kt b/app/src/main/java/com/keylesspalace/tusky/service/SendTootService.kt index cf919219..c065f74d 100644 --- a/app/src/main/java/com/keylesspalace/tusky/service/SendTootService.kt +++ b/app/src/main/java/com/keylesspalace/tusky/service/SendTootService.kt @@ -46,6 +46,7 @@ class SendTootService: Service(), Injectable { private val timer = Timer() + private val notificationManager by lazy { getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager } override fun onCreate() { AndroidInjection.inject(this) @@ -67,8 +68,6 @@ class SendTootService: Service(), Injectable { throw IllegalStateException("SendTootService started without $KEY_TOOT extra") } - val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel(CHANNEL_ID, getString(R.string.send_toot_notification_channel_name), NotificationManager.IMPORTANCE_LOW) notificationManager.createNotificationChannel(channel) @@ -124,6 +123,8 @@ class SendTootService: Service(), Injectable { if(account == null) { tootsToSend.remove(tootId) + notificationManager.cancel(tootId) + stopSelfWhenDone() return } @@ -148,7 +149,6 @@ class SendTootService: Service(), Injectable { override fun onResponse(call: Call, response: Response) { tootsToSend.remove(tootId) - val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager if (response.isSuccessful) { @@ -160,10 +160,7 @@ class SendTootService: Service(), Injectable { saveTootHelper.deleteDraft(tootToSend.savedTootUid) } - if (tootsToSend.isEmpty()) { - ServiceCompat.stopForeground(this@SendTootService, ServiceCompat.STOP_FOREGROUND_REMOVE) - stopSelf() - } + stopSelfWhenDone() notificationManager.cancel(tootId) @@ -179,10 +176,7 @@ class SendTootService: Service(), Injectable { notificationManager.notify(tootId, builder.build()) - if (tootsToSend.isEmpty()) { - ServiceCompat.stopForeground(this@SendTootService, ServiceCompat.STOP_FOREGROUND_DETACH) - stopSelf() - } + stopSelfWhenDone() } } @@ -205,6 +199,13 @@ class SendTootService: Service(), Injectable { } + private fun stopSelfWhenDone() { + if (tootsToSend.isEmpty()) { + ServiceCompat.stopForeground(this@SendTootService, ServiceCompat.STOP_FOREGROUND_DETACH) + stopSelf() + } + } + private fun cancelSending(tootId: Int) { val tootToCancel = tootsToSend.remove(tootId) if(tootToCancel != null) { @@ -213,8 +214,6 @@ class SendTootService: Service(), Injectable { saveTootToDrafts(tootToCancel) - val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - val builder = NotificationCompat.Builder(this@SendTootService, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notify) .setContentTitle(getString(R.string.send_toot_notification_cancel_title))