Optimize registration flow

This commit is contained in:
sim 2022-01-17 20:53:03 +01:00
parent 02bb2c0318
commit aee6c597f3
3 changed files with 37 additions and 33 deletions

View File

@ -172,13 +172,7 @@ private fun cCreateApp(context: Context,
appName: String, appName: String,
connectorToken: String, connectorToken: String,
callback: ()->Unit) { callback: ()->Unit) {
val db = getDb(context) // The unity of connector token must already be checked here
if (db.isRegistered(appName, connectorToken)) {
Log.i("RegisterService","$appName already registered")
createQueue.remove(connectorToken)
callback()
return
}
val parameters = mutableMapOf( val parameters = mutableMapOf(
"deviceId" to getDeviceId(context)!!, "deviceId" to getDeviceId(context)!!,
"appName" to appName "appName" to appName
@ -199,6 +193,7 @@ private fun cCreateApp(context: Context,
* It is printed and not thrown by SQLiteDatabase.java * It is printed and not thrown by SQLiteDatabase.java
* So we can't catch it * So we can't catch it
*/ */
val db = getDb(context)
db.registerApp(appName, connectorToken, response.token) db.registerApp(appName, connectorToken, response.token)
} else { } else {
Log.d(TAG, "An error occurred while creating the application.") Log.d(TAG, "An error occurred while creating the application.")

View File

@ -11,6 +11,10 @@ import org.unifiedpush.distributor.nextpush.account.getUrl
private const val TAG = "DistributorUtils" private const val TAG = "DistributorUtils"
const val TOKEN_NEW = "token_new"
const val TOKEN_REGISTERED_OK = "token_registered_ok"
const val TOKEN_NOK = "token_nok"
private var db : MessagingDatabase? = null private var db : MessagingDatabase? = null
fun getDb(context: Context): MessagingDatabase { fun getDb(context: Context): MessagingDatabase {
@ -94,10 +98,13 @@ fun getEndpoint(context: Context, connectorToken: String): String {
return "${getUrl(context)}/push/$appToken" return "${getUrl(context)}/push/$appToken"
} }
fun isTokenOk(context: Context, connectorToken: String, app: String): Boolean { fun checkToken(context: Context, connectorToken: String, app: String): String {
val db = getDb(context) val db = getDb(context)
if (connectorToken !in db.listTokens()) { if (connectorToken !in db.listTokens()) {
return true return TOKEN_NEW
} }
return db.getPackageName(connectorToken) == app if (db.isRegistered(app, connectorToken)) {
return TOKEN_REGISTERED_OK
}
return TOKEN_NOK
} }

View File

@ -33,7 +33,15 @@ 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
} }
if (!isConnected(context!!, showDialog = false)) { when (checkToken(context!!, connectorToken, application)) {
TOKEN_REGISTERED_OK -> sendEndpoint(context.applicationContext, connectorToken)
TOKEN_NOK -> sendRegistrationFailed(
context,
application,
connectorToken
)
TOKEN_NEW -> {
if (!isConnected(context, showDialog = false)) {
sendRegistrationFailed( sendRegistrationFailed(
context, context,
application, application,
@ -42,14 +50,6 @@ class RegisterBroadcastReceiver : BroadcastReceiver() {
) )
return return
} }
if (!isTokenOk(context, connectorToken, application)) {
sendRegistrationFailed(
context,
application,
connectorToken
)
return
}
if (connectorToken !in createQueue) { if (connectorToken !in createQueue) {
createQueue.add(connectorToken) createQueue.add(connectorToken)
apiCreateApp( apiCreateApp(
@ -63,6 +63,8 @@ class RegisterBroadcastReceiver : BroadcastReceiver() {
Log.d(TAG, "Already registering $connectorToken") Log.d(TAG, "Already registering $connectorToken")
} }
} }
}
}
ACTION_UNREGISTER ->{ ACTION_UNREGISTER ->{
Log.i(TAG,"UNREGISTER") Log.i(TAG,"UNREGISTER")
val connectorToken = intent.getStringExtra(EXTRA_TOKEN)?: "" val connectorToken = intent.getStringExtra(EXTRA_TOKEN)?: ""