2021-12-29 20:33:17 +01: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-29 23:48:06 +01:00
|
|
|
import io.heckel.ntfy.ui.SubscriberManager
|
2021-12-29 20:33:17 +01: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 = connectorToken // FIXME
|
|
|
|
val app = context!!.applicationContext as Application
|
|
|
|
val repository = app.repository
|
2021-12-29 21:36:47 +01:00
|
|
|
val distributor = Distributor(app)
|
2021-12-29 20:33:17 +01:00
|
|
|
val subscription = Subscription(
|
|
|
|
id = Random.nextLong(),
|
|
|
|
baseUrl = baseUrl,
|
|
|
|
topic = topic,
|
|
|
|
instant = true,
|
|
|
|
mutedUntil = 0,
|
|
|
|
upAppId = appId,
|
|
|
|
upConnectorToken = connectorToken,
|
|
|
|
totalCount = 0,
|
|
|
|
newCount = 0,
|
|
|
|
lastActive = Date().time/1000
|
|
|
|
)
|
|
|
|
GlobalScope.launch(Dispatchers.IO) {
|
|
|
|
repository.addSubscription(subscription)
|
2021-12-29 23:48:06 +01:00
|
|
|
val subscriptionIdsWithInstantStatus = repository.getSubscriptionIdsWithInstantStatus()
|
|
|
|
val subscriberManager = SubscriberManager(context!!)
|
|
|
|
subscriberManager.refreshService(subscriptionIdsWithInstantStatus)
|
2021-12-29 20:33:17 +01:00
|
|
|
}
|
2021-12-29 21:36:47 +01:00
|
|
|
distributor.sendEndpoint(appId, connectorToken)
|
2021-12-29 20:33:17 +01:00
|
|
|
// XXXXXXXXX
|
|
|
|
}
|
|
|
|
ACTION_UNREGISTER -> {
|
|
|
|
val connectorToken = intent.getStringExtra(EXTRA_TOKEN) ?: ""
|
|
|
|
Log.d(TAG, "Unregister: connectorToken=$connectorToken")
|
|
|
|
// XXXXXXX
|
2021-12-29 21:36:47 +01:00
|
|
|
val distributor = Distributor(context!!)
|
|
|
|
distributor.sendUnregistered("org.unifiedpush.example", connectorToken)
|
2021-12-29 20:33:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val TAG = "NtfyUpBroadcastRecv"
|
|
|
|
}
|
|
|
|
}
|