diff --git a/app/src/main/java/de/kuschku/quasseldroid/util/service/BackendServiceConnection.kt b/app/src/main/java/de/kuschku/quasseldroid/util/service/BackendServiceConnection.kt index 35ce1b6740e4b8da9cf73ce67a5bec061477bea2..111d27191a0ce563d3d457215ddf5b705280b55b 100644 --- a/app/src/main/java/de/kuschku/quasseldroid/util/service/BackendServiceConnection.kt +++ b/app/src/main/java/de/kuschku/quasseldroid/util/service/BackendServiceConnection.kt @@ -35,14 +35,19 @@ class BackendServiceConnection : ServiceConnection { var context: Context? = null - private var bound: Boolean = false + enum class State { + UNBOUND, + BINDING, + BOUND, + UNBINDING + } + + private var state: State = State.UNBOUND override fun onServiceDisconnected(component: ComponentName?) { - when (component) { - ComponentName(context, QuasselService::class.java) -> { - bound = false - backend.onNext(Optional.empty()) - } + synchronized(this@BackendServiceConnection) { + state = State.UNBOUND + backend.onNext(Optional.empty()) } } @@ -50,8 +55,10 @@ class BackendServiceConnection : ServiceConnection { when (component) { ComponentName(context, QuasselService::class.java) -> if (binder is QuasselBinder) { - bound = true - backend.onNext(Optional.of(binder.backend)) + synchronized(this@BackendServiceConnection) { + state = State.BOUND + backend.onNext(Optional.of(binder.backend)) + } } } } @@ -62,7 +69,10 @@ class BackendServiceConnection : ServiceConnection { @Synchronized fun bind(intent: Intent = QuasselService.intent(context!!), flags: Int = 0) { - context?.bindService(intent, this, flags) + if (state == State.UNBOUND || state == State.UNBINDING) { + state = State.BINDING + context?.bindService(intent, this, flags) + } } fun stop(intent: Intent = QuasselService.intent(context!!)) { @@ -71,6 +81,9 @@ class BackendServiceConnection : ServiceConnection { @Synchronized fun unbind() { - if (bound) context?.unbindService(this) + if (state == State.BOUND || state == State.BINDING) { + state = State.UNBINDING + context?.unbindService(this) + } } } diff --git a/malheur/src/main/java/de/kuschku/malheur/CrashHandler.kt b/malheur/src/main/java/de/kuschku/malheur/CrashHandler.kt index 9b81bf5491db7e7fe48d7f41a02fa2c913b0e6bc..32afbe1fdd31d05cc29f414f72b64baf13693588 100644 --- a/malheur/src/main/java/de/kuschku/malheur/CrashHandler.kt +++ b/malheur/src/main/java/de/kuschku/malheur/CrashHandler.kt @@ -20,6 +20,7 @@ package de.kuschku.malheur import android.app.Application +import android.os.Build import android.os.Handler import android.os.HandlerThread import android.util.Log @@ -85,11 +86,16 @@ object CrashHandler { } } catch (e: Throwable) { e.printStackTrace() - originalHandler?.uncaughtException(activeThread, throwable) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + throwable.addSuppressed(e) + } } }.start() } - Thread.setDefaultUncaughtExceptionHandler(myHandler) + Thread.setDefaultUncaughtExceptionHandler { currentThread, throwable -> + myHandler?.uncaughtException(currentThread, throwable) + originalHandler?.uncaughtException(currentThread, throwable) + } } fun handle(throwable: Throwable) {