-
-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1218 feat: create key event relay service
- Loading branch information
Showing
11 changed files
with
376 additions
and
109 deletions.
There are no files selected for viewing
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
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
74 changes: 74 additions & 0 deletions
74
app/src/main/java/io/github/sds100/keymapper/api/KeyEventRelayServiceWrapper.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,74 @@ | ||
package io.github.sds100.keymapper.api | ||
|
||
import android.app.Service | ||
import android.content.ComponentName | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.ServiceConnection | ||
import android.os.DeadObjectException | ||
import android.os.IBinder | ||
import android.view.KeyEvent | ||
|
||
/** | ||
* This handles connecting to the relay service and exposes an interface | ||
* so other parts of the app can get a reference to the service even when it isn't | ||
* bound yet. | ||
*/ | ||
class KeyEventRelayServiceWrapperImpl( | ||
context: Context, | ||
private val callback: IKeyEventRelayServiceCallback, | ||
) : KeyEventRelayServiceWrapper { | ||
private val ctx: Context = context.applicationContext | ||
|
||
private val keyEventRelayServiceLock: Any = Any() | ||
private var keyEventRelayService: IKeyEventRelayService? = null | ||
|
||
private val keyEventReceiverConnection: ServiceConnection = object : ServiceConnection { | ||
override fun onServiceConnected(name: ComponentName?, service: IBinder?) { | ||
synchronized(keyEventRelayServiceLock) { | ||
keyEventRelayService = IKeyEventRelayService.Stub.asInterface(service) | ||
keyEventRelayService?.registerCallback(callback) | ||
} | ||
} | ||
|
||
override fun onServiceDisconnected(name: ComponentName?) { | ||
synchronized(keyEventRelayServiceLock) { | ||
keyEventRelayService?.unregisterCallback() | ||
keyEventRelayService = null | ||
} | ||
} | ||
} | ||
|
||
override fun sendKeyEvent(event: KeyEvent?, targetPackageName: String?): Boolean { | ||
synchronized(keyEventRelayServiceLock) { | ||
if (keyEventRelayService == null) { | ||
return false | ||
} | ||
|
||
try { | ||
return keyEventRelayService!!.sendKeyEvent(event, targetPackageName) | ||
} catch (e: DeadObjectException) { | ||
keyEventRelayService = null | ||
return false | ||
} | ||
} | ||
} | ||
|
||
fun bind() { | ||
Intent(ctx, KeyEventRelayService::class.java).also { intent -> | ||
ctx.bindService(intent, keyEventReceiverConnection, Service.BIND_AUTO_CREATE) | ||
} | ||
} | ||
|
||
fun unbind() { | ||
try { | ||
ctx.unbindService(keyEventReceiverConnection) | ||
} catch (e: DeadObjectException) { | ||
// do nothing | ||
} | ||
} | ||
} | ||
|
||
interface KeyEventRelayServiceWrapper { | ||
fun sendKeyEvent(event: KeyEvent?, targetPackageName: String?): Boolean | ||
} |
Oops, something went wrong.