Fix concurrent (un)registration
Using registerQueue and delQueue, lists of connectorTokens
This commit is contained in:
parent
77f7901024
commit
85ed92357f
|
@ -22,6 +22,9 @@ import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
private const val TAG = "ApiUtils"
|
private const val TAG = "ApiUtils"
|
||||||
|
|
||||||
|
val createQueue = emptyList<String>().toMutableList()
|
||||||
|
val delQueue = emptyList<String>().toMutableList()
|
||||||
|
|
||||||
class ApiUtils {
|
class ApiUtils {
|
||||||
private lateinit var mApi: ProviderApi
|
private lateinit var mApi: ProviderApi
|
||||||
private lateinit var nextcloudAPI: NextcloudAPI
|
private lateinit var nextcloudAPI: NextcloudAPI
|
||||||
|
@ -187,6 +190,11 @@ class ApiUtils {
|
||||||
override fun onNext(response: ApiResponse) {
|
override fun onNext(response: ApiResponse) {
|
||||||
if (response.success) {
|
if (response.success) {
|
||||||
Log.d(TAG, "App successfully created.")
|
Log.d(TAG, "App successfully created.")
|
||||||
|
/**
|
||||||
|
* Ignore printed error for SQLiteContstraintException.
|
||||||
|
* It is printed and not thrown by SQLiteDatabase.java
|
||||||
|
* So we can't catch it
|
||||||
|
*/
|
||||||
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.")
|
||||||
|
@ -194,24 +202,27 @@ class ApiUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onError(e: Throwable) {
|
override fun onError(e: Throwable) {
|
||||||
|
createQueue.remove(connectorToken)
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onComplete() {
|
override fun onComplete() {
|
||||||
|
createQueue.remove(connectorToken)
|
||||||
callback()
|
callback()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fun deleteApp(context: Context, appToken: String, callback: ()->Unit) {
|
fun deleteApp(context: Context, connectorToken: String, callback: ()->Unit) {
|
||||||
cApi(context) {
|
cApi(context) {
|
||||||
cDeleteApp(appToken) {
|
cDeleteApp(context, connectorToken) {
|
||||||
callback()
|
callback()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun cDeleteApp(appToken: String, callback: ()->Unit) {
|
private fun cDeleteApp(context: Context, connectorToken: String, callback: ()->Unit) {
|
||||||
|
val appToken = getDb(context).getAppToken(connectorToken)
|
||||||
mApi.deleteApp(appToken)
|
mApi.deleteApp(appToken)
|
||||||
?.subscribeOn(Schedulers.newThread())
|
?.subscribeOn(Schedulers.newThread())
|
||||||
?.observeOn(Schedulers.newThread())
|
?.observeOn(Schedulers.newThread())
|
||||||
|
@ -229,10 +240,12 @@ class ApiUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onError(e: Throwable) {
|
override fun onError(e: Throwable) {
|
||||||
|
delQueue.remove(connectorToken)
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onComplete() {
|
override fun onComplete() {
|
||||||
|
delQueue.remove(connectorToken)
|
||||||
callback()
|
callback()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -6,6 +6,8 @@ import android.content.Intent
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import org.unifiedpush.distributor.nextpush.distributor.*
|
import org.unifiedpush.distributor.nextpush.distributor.*
|
||||||
import org.unifiedpush.distributor.nextpush.api.ApiUtils
|
import org.unifiedpush.distributor.nextpush.api.ApiUtils
|
||||||
|
import org.unifiedpush.distributor.nextpush.api.createQueue
|
||||||
|
import org.unifiedpush.distributor.nextpush.api.delQueue
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* THIS SERVICE IS USED BY OTHER APPS TO REGISTER
|
* THIS SERVICE IS USED BY OTHER APPS TO REGISTER
|
||||||
|
@ -25,9 +27,18 @@ 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
|
||||||
}
|
}
|
||||||
ApiUtils().createApp(context!!.applicationContext, application, connectorToken) {
|
if (connectorToken !in createQueue) {
|
||||||
|
createQueue.add(connectorToken)
|
||||||
|
ApiUtils().createApp(
|
||||||
|
context!!.applicationContext,
|
||||||
|
application,
|
||||||
|
connectorToken
|
||||||
|
) {
|
||||||
sendEndpoint(context.applicationContext, connectorToken)
|
sendEndpoint(context.applicationContext, connectorToken)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "Already registering $connectorToken")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ACTION_UNREGISTER ->{
|
ACTION_UNREGISTER ->{
|
||||||
Log.i(TAG,"UNREGISTER")
|
Log.i(TAG,"UNREGISTER")
|
||||||
|
@ -36,12 +47,16 @@ class RegisterBroadcastReceiver : BroadcastReceiver() {
|
||||||
if (application.isBlank()) {
|
if (application.isBlank()) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (connectorToken !in delQueue) {
|
||||||
|
delQueue.add(connectorToken)
|
||||||
sendUnregistered(context!!.applicationContext, connectorToken)
|
sendUnregistered(context!!.applicationContext, connectorToken)
|
||||||
|
ApiUtils().deleteApp(context.applicationContext, connectorToken) {
|
||||||
val db = getDb(context.applicationContext)
|
val db = getDb(context.applicationContext)
|
||||||
val appToken = db.getAppToken(connectorToken)
|
|
||||||
db.unregisterApp(connectorToken)
|
db.unregisterApp(connectorToken)
|
||||||
ApiUtils().deleteApp(context.applicationContext, appToken) {
|
Log.d(TAG, "Unregistration is finished")
|
||||||
Log.d(TAG,"Unregistration is finished")
|
}
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "Already deleting $connectorToken")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue