-
-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1237 from keymapperorg/1218-delay-when-inputting-…
…key-events-through-input-method-on-android-14 Delay when inputting key events through input method on android 14
- Loading branch information
Showing
18 changed files
with
406 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
app/src/main/aidl/io/github/sds100/keymapper/api/IKeyEventReceiver.aidl
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
app/src/main/aidl/io/github/sds100/keymapper/api/IKeyEventReceiverCallback.aidl
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
app/src/main/aidl/io/github/sds100/keymapper/api/IKeyEventRelayService.aidl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.github.sds100.keymapper.api; | ||
|
||
import android.view.KeyEvent; | ||
import io.github.sds100.keymapper.api.IKeyEventRelayServiceCallback; | ||
|
||
interface IKeyEventRelayService { | ||
/** | ||
* Send a key event to the target package that is registered with | ||
* a callback. | ||
*/ | ||
boolean sendKeyEvent(in KeyEvent event, in String targetPackageName); | ||
|
||
/** | ||
* Register a callback to receive key events from this relay service. The service | ||
* checks the process uid of the caller to this method and only permits certain applications | ||
* from connecting. | ||
*/ | ||
void registerCallback(IKeyEventRelayServiceCallback client); | ||
|
||
/** | ||
* Unregister a callback to receive key events from this relay service. The service | ||
* checks the process uid of the caller to this method and only permits certain applications | ||
* from connecting. | ||
*/ | ||
void unregisterCallback(); | ||
} |
7 changes: 7 additions & 0 deletions
7
app/src/main/aidl/io/github/sds100/keymapper/api/IKeyEventRelayServiceCallback.aidl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.github.sds100.keymapper.api; | ||
|
||
import android.view.KeyEvent; | ||
|
||
interface IKeyEventRelayServiceCallback { | ||
boolean onKeyEvent(in KeyEvent event, in String sourcePackageName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 0 additions & 52 deletions
52
app/src/main/java/io/github/sds100/keymapper/api/KeyEventReceiver.kt
This file was deleted.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
app/src/main/java/io/github/sds100/keymapper/api/KeyEventRelayService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package io.github.sds100.keymapper.api | ||
|
||
import android.app.Service | ||
import android.content.Intent | ||
import android.os.Binder | ||
import android.os.DeadObjectException | ||
import android.os.IBinder | ||
import android.view.KeyEvent | ||
import io.github.sds100.keymapper.system.inputmethod.KeyMapperImeHelper | ||
import timber.log.Timber | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
/** | ||
* This service is used as a relay between the accessibility service and input method service to pass | ||
* key events back and forth. A separate service has to be used because you can't bind to an | ||
* accessibility or input method service. | ||
* | ||
* This is used for key event actions. The input method service registers a callback | ||
* and the accessibility service sends the key events. | ||
* | ||
* This was implemented in issue #850 for the action to answer phone calls because Android doesn't | ||
* pass volume down key events to the accessibility service when the phone is ringing or it is | ||
* in a phone call. The accessibility service registers a callback and the input method service | ||
* sends the key events. | ||
*/ | ||
class KeyEventRelayService : Service() { | ||
val permittedPackages = KeyMapperImeHelper.KEY_MAPPER_IME_PACKAGE_LIST | ||
|
||
private val binderInterface: IKeyEventRelayService = object : IKeyEventRelayService.Stub() { | ||
override fun sendKeyEvent(event: KeyEvent?, targetPackageName: String?): Boolean { | ||
synchronized(callbackLock) { | ||
Timber.d("KeyEventRelayService: onKeyEvent ${event?.keyCode}") | ||
|
||
val callback = callbacks[targetPackageName] | ||
|
||
if (callback == null) { | ||
return false | ||
} else { | ||
try { | ||
// Get the process ID of the app that called this service. | ||
val sourcePackageName = getCallerPackageName() ?: return false | ||
|
||
if (!permittedPackages.contains(sourcePackageName)) { | ||
Timber.d("An unrecognized package $sourcePackageName tried to send a key event.") | ||
|
||
return false | ||
} | ||
|
||
return callback.onKeyEvent(event, targetPackageName) | ||
} catch (e: DeadObjectException) { | ||
// If the application is no longer connected then delete the callback. | ||
callbacks.remove(targetPackageName) | ||
return false | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun registerCallback(client: IKeyEventRelayServiceCallback?) { | ||
val sourcePackageName = getCallerPackageName() ?: return | ||
|
||
if (client == null || !permittedPackages.contains(sourcePackageName)) { | ||
Timber.d("An unrecognized package $sourcePackageName tried to register a key event relay callback.") | ||
return | ||
} | ||
|
||
synchronized(callbackLock) { | ||
Timber.d("Package $sourcePackageName registered a key event relay callback.") | ||
callbacks[sourcePackageName] = client | ||
} | ||
} | ||
|
||
override fun unregisterCallback() { | ||
synchronized(callbackLock) { | ||
val sourcePackageName = getCallerPackageName() ?: return | ||
|
||
Timber.d("Package $sourcePackageName unregistered a key event relay callback.") | ||
|
||
callbacks.remove(sourcePackageName) | ||
} | ||
} | ||
} | ||
|
||
private val callbackLock: Any = Any() | ||
private var callbacks: ConcurrentHashMap<String, IKeyEventRelayServiceCallback> = | ||
ConcurrentHashMap() | ||
|
||
override fun onBind(intent: Intent?): IBinder? = binderInterface.asBinder() | ||
|
||
private fun getCallerPackageName(): String? { | ||
val sourceUid = Binder.getCallingUid() | ||
return applicationContext.packageManager.getNameForUid(sourceUid) | ||
} | ||
} |
Oops, something went wrong.