Split onReceive

Introduce ConnectorTokenValidity
This commit is contained in:
sim 2024-10-28 14:36:36 +00:00
parent df40581e07
commit 5b5ce7575e
3 changed files with 98 additions and 73 deletions

View File

@ -0,0 +1,13 @@
package org.unifiedpush.distributor.nextpush.distributor
/**
* Validity of a connection token received during registration.
*/
enum class ConnectorTokenValidity {
/** This is a new token. */
TOKEN_NEW,
/** This is a known token, which match the provided application. */
TOKEN_REGISTERED_OK,
/** This is a known token, but it doesn't match the application. */
TOKEN_NOK,
}

View File

@ -16,10 +16,6 @@ import org.unifiedpush.distributor.nextpush.utils.TAG
object Distributor { object Distributor {
const val TOKEN_NEW = "token_new"
const val TOKEN_REGISTERED_OK = "token_registered_ok"
const val TOKEN_NOK = "token_nok"
fun sendMessage(context: Context, appToken: String, message: ByteArray) { fun sendMessage(context: Context, appToken: String, message: ByteArray) {
val db = getDb(context) val db = getDb(context)
val connectorToken = db.getConnectorToken(appToken) ?: return val connectorToken = db.getConnectorToken(appToken) ?: return
@ -94,15 +90,20 @@ object Distributor {
return "${getAccount(context)?.url}${mApiEndpoint}push/$appToken" return "${getAccount(context)?.url}${mApiEndpoint}push/$appToken"
} }
fun checkToken(context: Context, connectorToken: String, app: String): String { /**
* Check if the [connectorToken] is known and correspond to the [app].
*
* @return [ConnectorTokenValidity]
*/
fun checkToken(context: Context, connectorToken: String, app: String): ConnectorTokenValidity {
val db = getDb(context) val db = getDb(context)
if (connectorToken !in db.listTokens()) { if (connectorToken !in db.listTokens()) {
return TOKEN_NEW return ConnectorTokenValidity.TOKEN_NEW
} }
if (db.isRegistered(app, connectorToken)) { if (db.isRegistered(app, connectorToken)) {
return TOKEN_REGISTERED_OK return ConnectorTokenValidity.TOKEN_REGISTERED_OK
} }
return TOKEN_NOK return ConnectorTokenValidity.TOKEN_NOK
} }
fun deleteDevice(context: Context, block: () -> Unit = {}) { fun deleteDevice(context: Context, block: () -> Unit = {}) {

View File

@ -13,9 +13,6 @@ import org.unifiedpush.distributor.nextpush.AppCompanion
import org.unifiedpush.distributor.nextpush.Database.Companion.getDb import org.unifiedpush.distributor.nextpush.Database.Companion.getDb
import org.unifiedpush.distributor.nextpush.account.Account.isConnected import org.unifiedpush.distributor.nextpush.account.Account.isConnected
import org.unifiedpush.distributor.nextpush.distributor.* // ktlint-disable no-wildcard-imports import org.unifiedpush.distributor.nextpush.distributor.* // ktlint-disable no-wildcard-imports
import org.unifiedpush.distributor.nextpush.distributor.Distributor.TOKEN_NEW
import org.unifiedpush.distributor.nextpush.distributor.Distributor.TOKEN_NOK
import org.unifiedpush.distributor.nextpush.distributor.Distributor.TOKEN_REGISTERED_OK
import org.unifiedpush.distributor.nextpush.distributor.Distributor.checkToken import org.unifiedpush.distributor.nextpush.distributor.Distributor.checkToken
import org.unifiedpush.distributor.nextpush.distributor.Distributor.createApp import org.unifiedpush.distributor.nextpush.distributor.Distributor.createApp
import org.unifiedpush.distributor.nextpush.distributor.Distributor.deleteApp import org.unifiedpush.distributor.nextpush.distributor.Distributor.deleteApp
@ -114,13 +111,32 @@ class RegisterBroadcastReceiver : BroadcastReceiver() {
Log.w(TAG, "Trying to register an app without packageName") Log.w(TAG, "Trying to register an app without packageName")
return return
} }
onRegister(context, connectorToken, application)
}
ACTION_UNREGISTER -> {
Log.i(TAG, "UNREGISTER")
val connectorToken = intent.getStringExtra(EXTRA_TOKEN) ?: ""
onUnregister(context, connectorToken)
}
}
wakeLock?.let {
if (it.isHeld) {
it.release()
}
}
}
/**
* Register the app on the server and send the new endpoint
*/
private fun onRegister(context: Context, connectorToken: String, application: String) {
if (!AppCompanion.createQueue.containsTokenElseAdd(connectorToken)) { if (!AppCompanion.createQueue.containsTokenElseAdd(connectorToken)) {
when (checkToken(context, connectorToken, application)) { when (checkToken(context, connectorToken, application)) {
TOKEN_REGISTERED_OK -> { ConnectorTokenValidity.TOKEN_REGISTERED_OK -> {
sendEndpoint(context, connectorToken) sendEndpoint(context, connectorToken)
AppCompanion.createQueue.removeToken(connectorToken) AppCompanion.createQueue.removeToken(connectorToken)
} }
TOKEN_NOK -> { ConnectorTokenValidity.TOKEN_NOK -> {
sendRegistrationFailed( sendRegistrationFailed(
context, context,
application, application,
@ -128,7 +144,7 @@ class RegisterBroadcastReceiver : BroadcastReceiver() {
) )
AppCompanion.createQueue.removeToken(connectorToken) AppCompanion.createQueue.removeToken(connectorToken)
} }
TOKEN_NEW -> { ConnectorTokenValidity.TOKEN_NEW -> {
val appName = context.getApplicationName(application) ?: application val appName = context.getApplicationName(application) ?: application
if (!isConnected(context)) { if (!isConnected(context)) {
sendRegistrationFailed( sendRegistrationFailed(
@ -163,11 +179,12 @@ class RegisterBroadcastReceiver : BroadcastReceiver() {
Log.d(TAG, "Already registering this token") Log.d(TAG, "Already registering this token")
} }
} }
ACTION_UNREGISTER -> {
Log.i(TAG, "UNREGISTER")
val connectorToken = intent.getStringExtra(EXTRA_TOKEN) ?: ""
getDb(context).getPackageName(connectorToken) ?: return
/**
* Unregister the application from the server and remove from the local db
*/
private fun onUnregister(context: Context, connectorToken: String) {
getDb(context).getPackageName(connectorToken) ?: return
if (!AppCompanion.delQueue.containsTokenElseAdd(connectorToken)) { if (!AppCompanion.delQueue.containsTokenElseAdd(connectorToken)) {
try { try {
deleteApp(context, connectorToken) { deleteApp(context, connectorToken) {
@ -175,6 +192,7 @@ class RegisterBroadcastReceiver : BroadcastReceiver() {
AppCompanion.delQueue.removeToken(connectorToken) AppCompanion.delQueue.removeToken(connectorToken)
} }
} catch (e: Exception) { } catch (e: Exception) {
// TODO: remove later
Log.d(TAG, "Could not delete app") Log.d(TAG, "Could not delete app")
} }
} else { } else {
@ -182,10 +200,3 @@ class RegisterBroadcastReceiver : BroadcastReceiver() {
} }
} }
} }
wakeLock?.let {
if (it.isHeld) {
it.release()
}
}
}
}