From 5b5ce7575eb2cdf3beafa96b0b28156b3b3802a1 Mon Sep 17 00:00:00 2001 From: sim Date: Mon, 28 Oct 2024 14:36:36 +0000 Subject: [PATCH] Split onReceive Introduce ConnectorTokenValidity --- .../distributor/ConnectorTokenValidity.kt | 13 ++ .../nextpush/distributor/Distributor.kt | 17 ++- .../receivers/RegisterBroadcastReceiver.kt | 141 ++++++++++-------- 3 files changed, 98 insertions(+), 73 deletions(-) create mode 100644 app/src/main/java/org/unifiedpush/distributor/nextpush/distributor/ConnectorTokenValidity.kt diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/distributor/ConnectorTokenValidity.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/distributor/ConnectorTokenValidity.kt new file mode 100644 index 0000000..4430c79 --- /dev/null +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/distributor/ConnectorTokenValidity.kt @@ -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, +} \ No newline at end of file diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/distributor/Distributor.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/distributor/Distributor.kt index 0c4f7b1..5353467 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/distributor/Distributor.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/distributor/Distributor.kt @@ -16,10 +16,6 @@ import org.unifiedpush.distributor.nextpush.utils.TAG 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) { val db = getDb(context) val connectorToken = db.getConnectorToken(appToken) ?: return @@ -94,15 +90,20 @@ object Distributor { 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) if (connectorToken !in db.listTokens()) { - return TOKEN_NEW + return ConnectorTokenValidity.TOKEN_NEW } 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 = {}) { diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/receivers/RegisterBroadcastReceiver.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/receivers/RegisterBroadcastReceiver.kt index 1bd39ef..18d12fe 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/receivers/RegisterBroadcastReceiver.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/receivers/RegisterBroadcastReceiver.kt @@ -13,9 +13,6 @@ import org.unifiedpush.distributor.nextpush.AppCompanion import org.unifiedpush.distributor.nextpush.Database.Companion.getDb 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.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.createApp import org.unifiedpush.distributor.nextpush.distributor.Distributor.deleteApp @@ -114,72 +111,12 @@ class RegisterBroadcastReceiver : BroadcastReceiver() { Log.w(TAG, "Trying to register an app without packageName") return } - if (!AppCompanion.createQueue.containsTokenElseAdd(connectorToken)) { - when (checkToken(context, connectorToken, application)) { - TOKEN_REGISTERED_OK -> { - sendEndpoint(context, connectorToken) - AppCompanion.createQueue.removeToken(connectorToken) - } - TOKEN_NOK -> { - sendRegistrationFailed( - context, - application, - connectorToken - ) - AppCompanion.createQueue.removeToken(connectorToken) - } - TOKEN_NEW -> { - val appName = context.getApplicationName(application) ?: application - if (!isConnected(context)) { - sendRegistrationFailed( - context, - application, - connectorToken, - message = "NextPush is not connected" - ) - Toast.makeText( - context, - "Cannot register $appName, NextPush is not connected.", - Toast.LENGTH_SHORT - ).show() - return - } - createApp( - context, - application, - connectorToken - ) { - sendEndpoint(context, connectorToken) - AppCompanion.createQueue.removeToken(connectorToken) - Toast.makeText( - context, - "$appName registered.", - Toast.LENGTH_SHORT - ).show() - } - } - } - } else { - Log.d(TAG, "Already registering this token") - } + onRegister(context, connectorToken, application) } ACTION_UNREGISTER -> { Log.i(TAG, "UNREGISTER") val connectorToken = intent.getStringExtra(EXTRA_TOKEN) ?: "" - getDb(context).getPackageName(connectorToken) ?: return - - if (!AppCompanion.delQueue.containsTokenElseAdd(connectorToken)) { - try { - deleteApp(context, connectorToken) { - Log.d(TAG, "Unregistration is finished") - AppCompanion.delQueue.removeToken(connectorToken) - } - } catch (e: Exception) { - Log.d(TAG, "Could not delete app") - } - } else { - Log.d(TAG, "Already deleting this token") - } + onUnregister(context, connectorToken) } } wakeLock?.let { @@ -188,4 +125,78 @@ class RegisterBroadcastReceiver : BroadcastReceiver() { } } } + + /** + * 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)) { + when (checkToken(context, connectorToken, application)) { + ConnectorTokenValidity.TOKEN_REGISTERED_OK -> { + sendEndpoint(context, connectorToken) + AppCompanion.createQueue.removeToken(connectorToken) + } + ConnectorTokenValidity.TOKEN_NOK -> { + sendRegistrationFailed( + context, + application, + connectorToken + ) + AppCompanion.createQueue.removeToken(connectorToken) + } + ConnectorTokenValidity.TOKEN_NEW -> { + val appName = context.getApplicationName(application) ?: application + if (!isConnected(context)) { + sendRegistrationFailed( + context, + application, + connectorToken, + message = "NextPush is not connected" + ) + Toast.makeText( + context, + "Cannot register $appName, NextPush is not connected.", + Toast.LENGTH_SHORT + ).show() + return + } + createApp( + context, + application, + connectorToken + ) { + sendEndpoint(context, connectorToken) + AppCompanion.createQueue.removeToken(connectorToken) + Toast.makeText( + context, + "$appName registered.", + Toast.LENGTH_SHORT + ).show() + } + } + } + } else { + Log.d(TAG, "Already registering this token") + } + } + + /** + * 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)) { + try { + deleteApp(context, connectorToken) { + Log.d(TAG, "Unregistration is finished") + AppCompanion.delQueue.removeToken(connectorToken) + } + } catch (e: Exception) { + // TODO: remove later + Log.d(TAG, "Could not delete app") + } + } else { + Log.d(TAG, "Already deleting this token") + } + } }