@@ -15,13 +15,16 @@ import dagger.Lazy
1515import dagger.hilt.android.AndroidEntryPoint
1616import kotlinx.coroutines.Dispatchers
1717import kotlinx.coroutines.runBlocking
18+ import org.jetbrains.annotations.TestOnly
1819import org.unifiedpush.android.connector.FailedReason
1920import org.unifiedpush.android.connector.PushService
2021import org.unifiedpush.android.connector.data.PushEndpoint
2122import org.unifiedpush.android.connector.data.PushMessage
2223import java.util.logging.Level
2324import java.util.logging.Logger
2425import javax.inject.Inject
26+ import kotlin.reflect.KMutableProperty
27+ import kotlin.reflect.jvm.isAccessible
2528
2629/* *
2730 * Entry point for UnifiedPush.
@@ -59,10 +62,10 @@ class UnifiedPushService : PushService() {
5962
6063 override fun onNewEndpoint (endpoint : PushEndpoint , instance : String ) {
6164 val serviceId = instance.toLongOrNull() ? : return
62- logger.log( Level . FINE , " Got UnifiedPush endpoint for service $serviceId " , endpoint.url)
65+ logger.warning( " Got UnifiedPush endpoint for service $serviceId : ${ endpoint.url} " )
6366
6467 // register new endpoint at CalDAV/CardDAV servers
65- runBlocking {
68+ runBlocking( Dispatchers . Default ) {
6669 pushRegistrationManager.processSubscription(serviceId, endpoint)
6770 }
6871 }
@@ -72,7 +75,7 @@ class UnifiedPushService : PushService() {
7275 logger.warning(" UnifiedPush registration failed for service $serviceId : $reason " )
7376
7477 // unregister subscriptions
75- runBlocking {
78+ runBlocking( Dispatchers . Default ) {
7679 pushRegistrationManager.removeSubscription(serviceId)
7780 }
7881 }
@@ -81,7 +84,7 @@ class UnifiedPushService : PushService() {
8184 val serviceId = instance.toLongOrNull() ? : return
8285 logger.warning(" UnifiedPush unregistered for service $serviceId " )
8386
84- runBlocking {
87+ runBlocking( Dispatchers . Default ) {
8588 pushRegistrationManager.removeSubscription(serviceId)
8689 }
8790 }
@@ -145,4 +148,33 @@ class UnifiedPushService : PushService() {
145148 }
146149 }
147150
151+
152+ companion object {
153+
154+ /* *
155+ * We need to reset PushService::Companion.binder to null before creating a new PushService with a new binder. The
156+ * current implementation caches the binder, which will always use the first UnifiedPushService that is created during the first
157+ * test. All following test will fail because the wrong binder is used by PushService.
158+ *
159+ * This method resets the binder using reflection, because it's not accessible directly.
160+ *
161+ * See https://codeberg.org/UnifiedPush/android-connector/issues/8
162+ */
163+ @TestOnly
164+ fun resetBinder () {
165+ // requires kotlin-reflection
166+ val pushServiceClass = PushService ::class
167+ val companionClass = pushServiceClass.nestedClasses.first { it.isCompanion }
168+
169+ val binderProperty = companionClass.members
170+ .filterIsInstance(KMutableProperty ::class .java)
171+ .first { it.name.contains(" binder" ) }
172+ binderProperty.isAccessible = true
173+ val binderSetter = binderProperty.setter
174+
175+ binderSetter.call(companionClass, null )
176+ }
177+
178+ }
179+
148180}
0 commit comments