Skip to content

Commit 6d3772f

Browse files
committed
fix: unbind safely if there is no connection to the service
1 parent c5940ea commit 6d3772f

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

app/src/main/java/io/github/sds100/keymapper/inputmethod/latin/KeyEventRelayServiceWrapper.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ class KeyEventRelayServiceWrapperImpl(
2727
private val servicePackageName: String,
2828
private val callback: IKeyEventRelayServiceCallback,
2929
) : KeyEventRelayServiceWrapper {
30+
31+
companion object {
32+
const val ACTION_BIND_RELAY_SERVICE = "io.github.sds100.keymapper.ACTION_BIND_RELAY_SERVICE"
33+
}
34+
3035
private val ctx: Context = context.applicationContext
3136

3237
private val keyEventRelayServiceLock: Any = Any()
38+
private var isBound = false
3339
private var keyEventRelayService: IKeyEventRelayService? = null
3440

3541
private val serviceConnection: ServiceConnection =
@@ -84,19 +90,23 @@ class KeyEventRelayServiceWrapperImpl(
8490
relayServiceIntent.setComponent(component)
8591
val isSuccess = ctx.bindService(relayServiceIntent, serviceConnection, 0)
8692

87-
if (!isSuccess) {
93+
if (isSuccess) {
94+
isBound = true
95+
} else {
8896
ctx.unbindService(serviceConnection)
97+
isBound = false
8998
}
9099
} catch (e: SecurityException) {
91100
Log.e(LatinIME.TAG, e.toString())
92101
}
93102
}
94103

95104
fun unbind() {
96-
try {
105+
// Check if it is bound because otherwise
106+
// an exception is thrown if you unbind from a service
107+
// while there is no registered connection.
108+
if (isBound) {
97109
ctx.unbindService(serviceConnection)
98-
} catch (e: DeadObjectException) {
99-
// do nothing
100110
}
101111
}
102112
}

0 commit comments

Comments
 (0)