Skip to content

Commit 9f17b82

Browse files
committed
fix: do not store whether the service is bound because the state can be inconsistent and it will not rebind in response to the broadcast
1 parent 0b8e6fb commit 9f17b82

1 file changed

Lines changed: 12 additions & 22 deletions

File tree

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

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class KeyEventRelayServiceWrapperImpl(
4141
private val ctx: Context = context.applicationContext
4242

4343
private val keyEventRelayServiceLock: Any = Any()
44-
private var isBound = false
4544
private var keyEventRelayService: IKeyEventRelayService? = null
4645

4746
private val serviceConnection: ServiceConnection =
@@ -117,22 +116,15 @@ class KeyEventRelayServiceWrapperImpl(
117116

118117
private fun bind() {
119118
Log.d(LatinIME.TAG, "Bind $servicePackageName")
120-
if (isBound) {
121-
return
122-
}
123-
124119
try {
125120
val relayServiceIntent = Intent()
126121
val component =
127122
ComponentName(servicePackageName, "io.github.sds100.keymapper.api.KeyEventRelayService")
128123
relayServiceIntent.setComponent(component)
129124
val isSuccess = ctx.bindService(relayServiceIntent, serviceConnection, 0)
130125

131-
if (isSuccess) {
132-
isBound = true
133-
} else {
126+
if (!isSuccess) {
134127
ctx.unbindService(serviceConnection)
135-
isBound = false
136128
}
137129
} catch (e: SecurityException) {
138130
Log.e(LatinIME.TAG, e.toString())
@@ -141,20 +133,18 @@ class KeyEventRelayServiceWrapperImpl(
141133

142134
private fun unbind() {
143135
Log.d(LatinIME.TAG, "Unbind $servicePackageName")
144-
// Check if it is bound because otherwise
145-
// an exception is thrown if you unbind from a service
146-
// while there is no registered connection.
147-
if (isBound) {
148-
// Unregister the callback if this input method is unbinding
149-
// from the relay service. This should not happen in onServiceDisconnected
150-
// because the connection is already broken at that point and it
151-
// will fail.
152-
try {
153-
keyEventRelayService?.unregisterCallback()
154-
} catch (e: RemoteException) {
155-
// do nothing
156-
}
136+
// Unregister the callback if this input method is unbinding
137+
// from the relay service. This should not happen in onServiceDisconnected
138+
// because the connection is already broken at that point and it
139+
// will fail.
140+
try {
141+
keyEventRelayService?.unregisterCallback()
157142
ctx.unbindService(serviceConnection)
143+
} catch (e: RemoteException) {
144+
// do nothing
145+
} catch (e: IllegalArgumentException) {
146+
// an exception is thrown if you unbind from a service
147+
// while there is no registered connection.
158148
}
159149
}
160150

0 commit comments

Comments
 (0)