ntfy-android/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt

88 lines
4 KiB
Kotlin
Raw Normal View History

2021-12-30 08:33:17 +13:00
package io.heckel.ntfy.up
import android.content.Context
import android.content.Intent
import android.util.Log
import io.heckel.ntfy.R
import io.heckel.ntfy.app.Application
import io.heckel.ntfy.data.Subscription
2021-12-30 11:48:06 +13:00
import io.heckel.ntfy.ui.SubscriberManager
import io.heckel.ntfy.util.randomString
import io.heckel.ntfy.util.topicUrlUp
2021-12-30 08:33:17 +13:00
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.util.*
import kotlin.random.Random
class BroadcastReceiver : android.content.BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
when (intent!!.action) {
ACTION_REGISTER -> {
val appId = intent.getStringExtra(EXTRA_APPLICATION) ?: ""
val connectorToken = intent.getStringExtra(EXTRA_TOKEN) ?: ""
Log.d(TAG, "Register: app=$appId, connectorToken=$connectorToken")
if (appId.isBlank()) {
Log.w(TAG, "Trying to register an app without packageName")
return
}
val baseUrl = context!!.getString(R.string.app_base_url) // FIXME
val topic = "up" + randomString(TOPIC_LENGTH)
val endpoint = topicUrlUp(baseUrl, topic)
2021-12-30 08:33:17 +13:00
val app = context!!.applicationContext as Application
val repository = app.repository
val distributor = Distributor(app)
2021-12-30 08:33:17 +13:00
GlobalScope.launch(Dispatchers.IO) {
val existingSubscription = repository.getSubscriptionByConnectorToken(connectorToken)
if (existingSubscription != null) {
distributor.sendRegistrationRefused(appId, connectorToken)
return@launch
}
val subscription = Subscription(
id = Random.nextLong(),
baseUrl = baseUrl,
topic = topic,
instant = true, // No Firebase, always instant!
mutedUntil = 0,
upAppId = appId,
upConnectorToken = connectorToken,
totalCount = 0,
newCount = 0,
lastActive = Date().time/1000
)
2021-12-30 08:33:17 +13:00
repository.addSubscription(subscription)
2021-12-30 11:48:06 +13:00
val subscriptionIdsWithInstantStatus = repository.getSubscriptionIdsWithInstantStatus()
val subscriberManager = SubscriberManager(app)
2021-12-30 11:48:06 +13:00
subscriberManager.refreshService(subscriptionIdsWithInstantStatus)
distributor.sendEndpoint(appId, connectorToken, endpoint)
2021-12-30 08:33:17 +13:00
}
}
ACTION_UNREGISTER -> {
val connectorToken = intent.getStringExtra(EXTRA_TOKEN) ?: ""
Log.d(TAG, "Unregister: connectorToken=$connectorToken")
val app = context!!.applicationContext as Application
val repository = app.repository
val distributor = Distributor(app)
GlobalScope.launch(Dispatchers.IO) {
val existingSubscription = repository.getSubscriptionByConnectorToken(connectorToken)
if (existingSubscription == null) {
return@launch
}
repository.removeSubscription(existingSubscription.id)
val subscriptionIdsWithInstantStatus = repository.getSubscriptionIdsWithInstantStatus()
val subscriberManager = SubscriberManager(app)
subscriberManager.refreshService(subscriptionIdsWithInstantStatus)
existingSubscription.upAppId?.let { appId ->
distributor.sendUnregistered(appId, connectorToken)
}
}
2021-12-30 08:33:17 +13:00
}
}
}
companion object {
private const val TAG = "NtfyUpBroadcastRecv"
private const val TOPIC_LENGTH = 16
2021-12-30 08:33:17 +13:00
}
}