Optimize registration flow
This commit is contained in:
parent
02bb2c0318
commit
aee6c597f3
|
@ -172,13 +172,7 @@ private fun cCreateApp(context: Context,
|
|||
appName: String,
|
||||
connectorToken: String,
|
||||
callback: ()->Unit) {
|
||||
val db = getDb(context)
|
||||
if (db.isRegistered(appName, connectorToken)) {
|
||||
Log.i("RegisterService","$appName already registered")
|
||||
createQueue.remove(connectorToken)
|
||||
callback()
|
||||
return
|
||||
}
|
||||
// The unity of connector token must already be checked here
|
||||
val parameters = mutableMapOf(
|
||||
"deviceId" to getDeviceId(context)!!,
|
||||
"appName" to appName
|
||||
|
@ -199,6 +193,7 @@ private fun cCreateApp(context: Context,
|
|||
* It is printed and not thrown by SQLiteDatabase.java
|
||||
* So we can't catch it
|
||||
*/
|
||||
val db = getDb(context)
|
||||
db.registerApp(appName, connectorToken, response.token)
|
||||
} else {
|
||||
Log.d(TAG, "An error occurred while creating the application.")
|
||||
|
|
|
@ -11,6 +11,10 @@ import org.unifiedpush.distributor.nextpush.account.getUrl
|
|||
|
||||
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
|
||||
|
||||
fun getDb(context: Context): MessagingDatabase {
|
||||
|
@ -94,10 +98,13 @@ fun getEndpoint(context: Context, connectorToken: String): String {
|
|||
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)
|
||||
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
|
||||
}
|
||||
|
|
|
@ -33,34 +33,36 @@ class RegisterBroadcastReceiver : BroadcastReceiver() {
|
|||
Log.w(TAG,"Trying to register an app without packageName")
|
||||
return
|
||||
}
|
||||
if (!isConnected(context!!, showDialog = false)) {
|
||||
sendRegistrationFailed(
|
||||
context,
|
||||
application,
|
||||
connectorToken,
|
||||
message = "NextPush is not connected"
|
||||
)
|
||||
return
|
||||
}
|
||||
if (!isTokenOk(context, connectorToken, application)) {
|
||||
sendRegistrationFailed(
|
||||
when (checkToken(context!!, connectorToken, application)) {
|
||||
TOKEN_REGISTERED_OK -> sendEndpoint(context.applicationContext, connectorToken)
|
||||
TOKEN_NOK -> sendRegistrationFailed(
|
||||
context,
|
||||
application,
|
||||
connectorToken
|
||||
)
|
||||
return
|
||||
}
|
||||
if (connectorToken !in createQueue) {
|
||||
createQueue.add(connectorToken)
|
||||
apiCreateApp(
|
||||
context.applicationContext,
|
||||
application,
|
||||
connectorToken
|
||||
) {
|
||||
sendEndpoint(context.applicationContext, connectorToken)
|
||||
TOKEN_NEW -> {
|
||||
if (!isConnected(context, showDialog = false)) {
|
||||
sendRegistrationFailed(
|
||||
context,
|
||||
application,
|
||||
connectorToken,
|
||||
message = "NextPush is not connected"
|
||||
)
|
||||
return
|
||||
}
|
||||
if (connectorToken !in createQueue) {
|
||||
createQueue.add(connectorToken)
|
||||
apiCreateApp(
|
||||
context.applicationContext,
|
||||
application,
|
||||
connectorToken
|
||||
) {
|
||||
sendEndpoint(context.applicationContext, connectorToken)
|
||||
}
|
||||
} else {
|
||||
Log.d(TAG, "Already registering $connectorToken")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Log.d(TAG, "Already registering $connectorToken")
|
||||
}
|
||||
}
|
||||
ACTION_UNREGISTER ->{
|
||||
|
|
Loading…
Reference in New Issue