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
|
2021-12-30 13:05:32 +13:00
|
|
|
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
|
2021-12-30 13:05:32 +13:00
|
|
|
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
|
2021-12-30 09:36:47 +13:00
|
|
|
val distributor = Distributor(app)
|
2021-12-30 08:33:17 +13:00
|
|
|
GlobalScope.launch(Dispatchers.IO) {
|
2021-12-30 13:05:32 +13:00
|
|
|
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()
|
2021-12-30 13:05:32 +13:00
|
|
|
val subscriberManager = SubscriberManager(app)
|
2021-12-30 11:48:06 +13:00
|
|
|
subscriberManager.refreshService(subscriptionIdsWithInstantStatus)
|
2021-12-30 13:05:32 +13:00
|
|
|
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")
|
2021-12-30 13:05:32 +13:00
|
|
|
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"
|
2021-12-30 13:05:32 +13:00
|
|
|
private const val TOPIC_LENGTH = 16
|
2021-12-30 08:33:17 +13:00
|
|
|
}
|
|
|
|
}
|