mirror of
https://codeberg.org/NextPush/nextpush-android.git
synced 2024-12-27 10:12:29 +01:00
Split onReceive
Introduce ConnectorTokenValidity
This commit is contained in:
parent
df40581e07
commit
5b5ce7575e
@ -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,
|
||||
}
|
@ -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 = {}) {
|
||||
|
@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user