From 7dbbf12c9903c4648e1bcc04f86088d940773f65 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Wed, 29 Dec 2021 23:48:06 +0100 Subject: [PATCH] Start instant delivery notification --- .../main/java/io/heckel/ntfy/data/Repository.kt | 6 ++++++ .../main/java/io/heckel/ntfy/ui/MainActivity.kt | 2 +- .../java/io/heckel/ntfy/ui/SubscriberManager.kt | 16 +++++++++------- .../java/io/heckel/ntfy/up/BroadcastReceiver.kt | 5 ++++- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/heckel/ntfy/data/Repository.kt b/app/src/main/java/io/heckel/ntfy/data/Repository.kt index e953911..fa6797a 100644 --- a/app/src/main/java/io/heckel/ntfy/data/Repository.kt +++ b/app/src/main/java/io/heckel/ntfy/data/Repository.kt @@ -36,6 +36,12 @@ class Repository(private val sharedPrefs: SharedPreferences, private val subscri return toSubscriptionList(subscriptionDao.list()) } + fun getSubscriptionIdsWithInstantStatus(): Set> { + return subscriptionDao + .list() + .map { Pair(it.id, it.instant) }.toSet() + } + @Suppress("RedundantSuspendModifier") @WorkerThread suspend fun getSubscription(subscriptionId: Long): Subscription? { diff --git a/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt index 7bd46c6..3a3e813 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt @@ -103,7 +103,7 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback, AddFragment.Subsc } } - // React to changes in fast delivery setting + // React to changes in instant delivery setting viewModel.listIdsWithInstantStatus().observe(this) { subscriberManager?.refreshService(it) } diff --git a/app/src/main/java/io/heckel/ntfy/ui/SubscriberManager.kt b/app/src/main/java/io/heckel/ntfy/ui/SubscriberManager.kt index a348d8a..141096e 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/SubscriberManager.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/SubscriberManager.kt @@ -1,5 +1,6 @@ package io.heckel.ntfy.ui +import android.content.Context import android.content.Intent import android.os.Build import android.util.Log @@ -7,16 +8,17 @@ import androidx.activity.ComponentActivity import androidx.lifecycle.lifecycleScope import io.heckel.ntfy.msg.SubscriberService import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch /** * This class only manages the SubscriberService, i.e. it starts or stops it. * It's used in multiple activities. */ -class SubscriberManager(private val activity: ComponentActivity) { - fun refreshService(subscriptionIdsWithInstantStatus: Set>) { +class SubscriberManager(private val context: Context) { + fun refreshService(subscriptionIdsWithInstantStatus: Set>) { // Set IsInstant> Log.d(MainActivity.TAG, "Triggering subscriber service refresh") - activity.lifecycleScope.launch(Dispatchers.IO) { + GlobalScope.launch(Dispatchers.IO) { val instantSubscriptions = subscriptionIdsWithInstantStatus.toList().filter { (_, instant) -> instant }.size if (instantSubscriptions == 0) { performActionOnSubscriberService(SubscriberService.Actions.STOP) @@ -27,18 +29,18 @@ class SubscriberManager(private val activity: ComponentActivity) { } private fun performActionOnSubscriberService(action: SubscriberService.Actions) { - val serviceState = SubscriberService.readServiceState(activity) + val serviceState = SubscriberService.readServiceState(context) if (serviceState == SubscriberService.ServiceState.STOPPED && action == SubscriberService.Actions.STOP) { return } - val intent = Intent(activity, SubscriberService::class.java) + val intent = Intent(context, SubscriberService::class.java) intent.action = action.name if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Log.d(MainActivity.TAG, "Performing SubscriberService action: ${action.name} (as foreground service, API >= 26)") - activity.startForegroundService(intent) + context.startForegroundService(intent) } else { Log.d(MainActivity.TAG, "Performing SubscriberService action: ${action.name} (as background service, API >= 26)") - activity.startService(intent) + context.startService(intent) } } } diff --git a/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt b/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt index 7e56035..71c36bb 100644 --- a/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt +++ b/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt @@ -6,6 +6,7 @@ import android.util.Log import io.heckel.ntfy.R import io.heckel.ntfy.app.Application import io.heckel.ntfy.data.Subscription +import io.heckel.ntfy.ui.SubscriberManager import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch @@ -43,8 +44,10 @@ class BroadcastReceiver : android.content.BroadcastReceiver() { ) GlobalScope.launch(Dispatchers.IO) { repository.addSubscription(subscription) + val subscriptionIdsWithInstantStatus = repository.getSubscriptionIdsWithInstantStatus() + val subscriberManager = SubscriberManager(context!!) + subscriberManager.refreshService(subscriptionIdsWithInstantStatus) } - distributor.sendEndpoint(appId, connectorToken) // XXXXXXXXX }