Skip to content

Commit 2eab0fc

Browse files
committed
#1972 fix: expert mode works on Android 10
1 parent ca68a68 commit 2eab0fc

2 files changed

Lines changed: 111 additions & 101 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
## Fixed
1212

13-
- #1986 fix: trigger screen is usable on slightly rectangular screens with a low DPI
13+
- #1986 trigger screen is usable on slightly rectangular screens with a low DPI
14+
- #1972 Expert Mode works on Android 10
1415
- Bugs with expert mode auto starting time.
1516

1617
## [4.0.0 Beta 6](https://github.com/sds100/KeyMapper/releases/tag/v4.0.0-beta.06)

sysbridge/src/main/java/io/github/sds100/keymapper/sysbridge/service/SystemBridge.kt

Lines changed: 109 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -271,115 +271,113 @@ class SystemBridge : ISystemBridge.Stub() {
271271
}
272272
}
273273

274-
private val inputManager: IInputManager
275-
private val wifiManager: IWifiManager?
276-
private val permissionManager: IPermissionManager
277-
private val telephonyManager: ITelephony?
278-
private val packageManager: IPackageManager
279-
private val bluetoothManager: IBluetoothManager?
280-
private val nfcAdapter: INfcAdapter?
281-
private val connectivityManager: IConnectivityManager?
282-
private val tetheringConnector: ITetheringConnector?
283-
private val activityManager: IActivityManager
284-
private val activityTaskManager: IActivityTaskManager
285-
private val audioService: IAudioService?
286-
private val usbManager: IUsbManager?
287-
288-
private val processPackageName: String = when (Process.myUid()) {
289-
Process.ROOT_UID -> "root"
290-
Process.SHELL_UID -> "com.android.shell"
291-
else -> throw IllegalStateException("SystemBridge must run as root or shell user")
292-
}
293-
294-
init {
295-
if (versionCode == -1) {
296-
Log.e(TAG, "SystemBridge version code not set")
297-
throw IllegalStateException("SystemBridge version code not set")
298-
}
299-
300-
val libraryPath = System.getProperty("keymapper_sysbridge.library.path")
301-
@SuppressLint("UnsafeDynamicallyLoadedCode")
302-
System.load("$libraryPath/libevdev_manager.so")
303-
304-
Log.i(TAG, "SystemBridge starting... Version code $versionCode")
305-
306-
waitSystemService(Context.ACTIVITY_SERVICE)
307-
activityManager = IActivityManager.Stub.asInterface(
308-
ServiceManager.getService(Context.ACTIVITY_SERVICE),
309-
)
310-
311-
waitSystemService("activity_task")
312-
activityTaskManager = IActivityTaskManager.Stub.asInterface(
313-
ServiceManager.getService("activity_task"),
314-
)
315-
316-
waitSystemService(Context.USER_SERVICE)
317-
waitSystemService(Context.APP_OPS_SERVICE)
318-
319-
waitSystemService("package")
320-
packageManager = IPackageManager.Stub.asInterface(ServiceManager.getService("package"))
321-
322-
waitSystemService("permissionmgr")
323-
permissionManager =
324-
IPermissionManager.Stub.asInterface(ServiceManager.getService("permissionmgr"))
325-
274+
private val inputManager: IInputManager by lazy {
326275
waitSystemService(Context.INPUT_SERVICE)
327-
inputManager =
328-
IInputManager.Stub.asInterface(ServiceManager.getService(Context.INPUT_SERVICE))
276+
IInputManager.Stub.asInterface(ServiceManager.getService(Context.INPUT_SERVICE))
277+
}
329278

279+
private val wifiManager: IWifiManager? by lazy {
330280
if (hasSystemFeature(PackageManager.FEATURE_WIFI)) {
331281
waitSystemService(Context.WIFI_SERVICE)
332-
wifiManager =
333-
IWifiManager.Stub.asInterface(ServiceManager.getService(Context.WIFI_SERVICE))
282+
IWifiManager.Stub.asInterface(ServiceManager.getService(Context.WIFI_SERVICE))
334283
} else {
335-
wifiManager = null
284+
null
336285
}
286+
}
287+
288+
private val permissionManager: IPermissionManager? by lazy {
289+
waitSystemService("permissionmgr")
290+
IPermissionManager.Stub.asInterface(ServiceManager.getService("permissionmgr"))
291+
}
337292

293+
private val telephonyManager: ITelephony? by lazy {
338294
if (hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
339295
waitSystemService(Context.TELEPHONY_SERVICE)
340-
telephonyManager =
341-
ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE))
296+
ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE))
342297
} else {
343-
telephonyManager = null
298+
null
344299
}
300+
}
301+
302+
private val packageManager: IPackageManager by lazy {
303+
waitSystemService("package")
304+
IPackageManager.Stub.asInterface(ServiceManager.getService("package"))
305+
}
345306

307+
private val bluetoothManager: IBluetoothManager? by lazy {
346308
if (hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
347309
waitSystemService("bluetooth_manager")
348-
bluetoothManager =
349-
IBluetoothManager.Stub.asInterface(ServiceManager.getService("bluetooth_manager"))
310+
IBluetoothManager.Stub.asInterface(ServiceManager.getService("bluetooth_manager"))
350311
} else {
351-
bluetoothManager = null
312+
null
352313
}
314+
}
353315

316+
private val nfcAdapter: INfcAdapter? by lazy {
354317
if (hasSystemFeature(PackageManager.FEATURE_NFC)) {
355318
waitSystemService(Context.NFC_SERVICE)
356-
nfcAdapter =
357-
INfcAdapter.Stub.asInterface(ServiceManager.getService(Context.NFC_SERVICE))
319+
INfcAdapter.Stub.asInterface(ServiceManager.getService(Context.NFC_SERVICE))
358320
} else {
359-
nfcAdapter = null
321+
null
360322
}
323+
}
361324

362-
waitSystemService(Context.CONNECTIVITY_SERVICE)
363-
connectivityManager =
364-
IConnectivityManager.Stub.asInterface(
365-
ServiceManager.getService(Context.CONNECTIVITY_SERVICE),
366-
)
325+
private val connectivityManager: IConnectivityManager? by lazy {
367326

368-
waitSystemService(Context.AUDIO_SERVICE)
369-
audioService =
370-
IAudioService.Stub.asInterface(ServiceManager.getService(Context.AUDIO_SERVICE))
327+
waitSystemService(Context.CONNECTIVITY_SERVICE)
328+
IConnectivityManager.Stub.asInterface(
329+
ServiceManager.getService(Context.CONNECTIVITY_SERVICE),
330+
)
331+
}
332+
private val tetheringConnector: ITetheringConnector? by lazy {
371333

372334
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
373335
waitSystemService("tethering")
374-
tetheringConnector =
375-
ITetheringConnector.Stub.asInterface(ServiceManager.getService("tethering"))
336+
ITetheringConnector.Stub.asInterface(ServiceManager.getService("tethering"))
376337
} else {
377-
tetheringConnector = null
338+
null
378339
}
340+
}
341+
private val activityManager: IActivityManager
342+
private val activityTaskManager: IActivityTaskManager by lazy {
343+
waitSystemService("activity_task")
344+
IActivityTaskManager.Stub.asInterface(
345+
ServiceManager.getService("activity_task"),
346+
)
347+
}
348+
349+
private val audioService: IAudioService? by lazy {
350+
waitSystemService(Context.AUDIO_SERVICE)
351+
IAudioService.Stub.asInterface(ServiceManager.getService(Context.AUDIO_SERVICE))
352+
}
379353

354+
private val usbManager: IUsbManager? by lazy {
380355
waitSystemService(Context.USB_SERVICE)
381-
usbManager =
382-
IUsbManager.Stub.asInterface(ServiceManager.getService(Context.USB_SERVICE))
356+
IUsbManager.Stub.asInterface(ServiceManager.getService(Context.USB_SERVICE))
357+
}
358+
359+
private val processPackageName: String = when (Process.myUid()) {
360+
Process.ROOT_UID -> "root"
361+
Process.SHELL_UID -> "com.android.shell"
362+
else -> throw IllegalStateException("SystemBridge must run as root or shell user")
363+
}
364+
365+
init {
366+
if (versionCode == -1) {
367+
Log.e(TAG, "SystemBridge version code not set")
368+
throw IllegalStateException("SystemBridge version code not set")
369+
}
370+
371+
val libraryPath = System.getProperty("keymapper_sysbridge.library.path")
372+
@SuppressLint("UnsafeDynamicallyLoadedCode")
373+
System.load("$libraryPath/libevdev_manager.so")
374+
375+
Log.i(TAG, "SystemBridge starting... Version code $versionCode")
376+
377+
waitSystemService(Context.ACTIVITY_SERVICE)
378+
activityManager = IActivityManager.Stub.asInterface(
379+
ServiceManager.getService(Context.ACTIVITY_SERVICE),
380+
)
383381

384382
val applicationInfo = getKeyMapperPackageInfo()
385383

@@ -396,6 +394,9 @@ class SystemBridge : ISystemBridge.Stub() {
396394

397395
initEvdevManager()
398396

397+
waitSystemService(Context.USER_SERVICE)
398+
waitSystemService(Context.APP_OPS_SERVICE)
399+
399400
Log.i(TAG, "SystemBridge started complete. Version code $versionCode")
400401
}
401402

@@ -497,7 +498,7 @@ class SystemBridge : ISystemBridge.Stub() {
497498
throw UnsupportedOperationException("WiFi not supported")
498499
}
499500

500-
return wifiManager.setWifiEnabled(processPackageName, enable)
501+
return wifiManager!!.setWifiEnabled(processPackageName, enable)
501502
}
502503

503504
override fun writeEvdevEvent(deviceId: Int, type: Int, code: Int, value: Int): Boolean {
@@ -512,17 +513,25 @@ class SystemBridge : ISystemBridge.Stub() {
512513
return Process.myUid()
513514
}
514515

515-
@RequiresApi(Build.VERSION_CODES.R)
516516
override fun grantPermission(permission: String?, deviceId: Int) {
517517
val userId = UserHandleUtils.getCallingUserId()
518518

519-
PermissionManagerApis.grantPermission(
520-
permissionManager,
521-
systemBridgePackageName ?: return,
522-
permission ?: return,
523-
deviceId,
524-
userId,
525-
)
519+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
520+
PermissionManagerApis.grantPermission(
521+
permissionManager!!,
522+
systemBridgePackageName ?: return,
523+
permission ?: return,
524+
deviceId,
525+
userId,
526+
)
527+
} else {
528+
PermissionManagerApis.grantPermission(
529+
packageManager,
530+
systemBridgePackageName ?: return,
531+
permission ?: return,
532+
userId,
533+
)
534+
}
526535
}
527536

528537
private fun sendBinderToApp(): Boolean {
@@ -676,14 +685,14 @@ class SystemBridge : ISystemBridge.Stub() {
676685
}
677686

678687
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
679-
telephonyManager.setDataEnabledForReason(
688+
telephonyManager!!.setDataEnabledForReason(
680689
subId,
681690
DATA_ENABLED_REASON_USER,
682691
enable,
683692
processPackageName,
684693
)
685694
} else {
686-
telephonyManager.setUserDataEnabled(subId, enable)
695+
telephonyManager!!.setUserDataEnabled(subId, enable)
687696
}
688697
}
689698

@@ -709,9 +718,9 @@ class SystemBridge : ISystemBridge.Stub() {
709718
val attributionSource = attributionSourceBuilder.build()
710719

711720
if (enable) {
712-
bluetoothManager.enable(attributionSource)
721+
bluetoothManager!!.enable(attributionSource)
713722
} else {
714-
bluetoothManager.disable(attributionSource, true)
723+
bluetoothManager!!.disable(attributionSource, true)
715724
}
716725
}
717726

@@ -721,10 +730,10 @@ class SystemBridge : ISystemBridge.Stub() {
721730
}
722731

723732
if (enable) {
724-
NfcAdapterApis.enable(nfcAdapter, processPackageName)
733+
NfcAdapterApis.enable(nfcAdapter!!, processPackageName)
725734
} else {
726735
NfcAdapterApis.disable(
727-
adapter = nfcAdapter,
736+
adapter = nfcAdapter!!,
728737
saveState = true,
729738
packageName = processPackageName,
730739
)
@@ -736,7 +745,7 @@ class SystemBridge : ISystemBridge.Stub() {
736745
throw UnsupportedOperationException("ConnectivityManager not supported")
737746
}
738747

739-
connectivityManager.setAirplaneMode(enable)
748+
connectivityManager!!.setAirplaneMode(enable)
740749
}
741750

742751
override fun forceStopPackage(packageName: String?) {
@@ -767,7 +776,7 @@ class SystemBridge : ISystemBridge.Stub() {
767776
throw UnsupportedOperationException("AudioService not supported")
768777
}
769778

770-
audioService.setRingerModeInternal(ringerMode, processPackageName)
779+
audioService!!.setRingerModeInternal(ringerMode, processPackageName)
771780
}
772781

773782
@RequiresApi(Build.VERSION_CODES.R)
@@ -806,7 +815,7 @@ class SystemBridge : ISystemBridge.Stub() {
806815
// instead of keeping it registered for the lifetime of SystemBridge. This is
807816
// a safety measure in case there's a bug in the callback that could crash
808817
// the entire SystemBridge process.
809-
tetheringConnector.registerTetheringEventCallback(callback, processPackageName)
818+
tetheringConnector!!.registerTetheringEventCallback(callback, processPackageName)
810819

811820
// Wait for callback with timeout using Handler
812821
mainHandler.postDelayed(
@@ -824,7 +833,7 @@ class SystemBridge : ISystemBridge.Stub() {
824833
} catch (e: InterruptedException) {
825834
Thread.currentThread().interrupt()
826835
} finally {
827-
tetheringConnector.unregisterTetheringEventCallback(callback, processPackageName)
836+
tetheringConnector!!.unregisterTetheringEventCallback(callback, processPackageName)
828837
}
829838

830839
return result
@@ -848,9 +857,9 @@ class SystemBridge : ISystemBridge.Stub() {
848857
connectivityScope = 1
849858
}
850859

851-
tetheringConnector.startTethering(request, processPackageName, null, null)
860+
tetheringConnector!!.startTethering(request, processPackageName, null, null)
852861
} else {
853-
tetheringConnector.stopTethering(TETHERING_WIFI, processPackageName, null, null)
862+
tetheringConnector!!.stopTethering(TETHERING_WIFI, processPackageName, null, null)
854863
}
855864
}
856865

0 commit comments

Comments
 (0)