@@ -2,25 +2,40 @@ package io.github.sds100.keymapper.system.nfc
22
33import android.content.Context
44import android.nfc.NfcManager
5+ import android.os.Build
56import androidx.core.content.getSystemService
7+ import dagger.hilt.android.qualifiers.ApplicationContext
68import io.github.sds100.keymapper.common.utils.KMResult
9+ import io.github.sds100.keymapper.sysbridge.manager.SystemBridgeConnectionManager
710import io.github.sds100.keymapper.system.root.SuAdapter
8- import dagger.hilt.android.qualifiers.ApplicationContext
911import javax.inject.Inject
1012import javax.inject.Singleton
1113
1214@Singleton
1315class AndroidNfcAdapter @Inject constructor(
1416 @ApplicationContext private val context : Context ,
1517 private val suAdapter : SuAdapter ,
18+ private val systemBridgeConnectionManager : SystemBridgeConnectionManager
1619) : NfcAdapter {
1720 private val ctx = context.applicationContext
1821
19- private val nfcManager: NfcManager by lazy { ctx.getSystemService()!! }
22+ private val nfcManager: NfcManager ? by lazy { ctx.getSystemService() }
2023
21- override fun isEnabled (): Boolean = nfcManager.defaultAdapter.isEnabled
24+ override fun isEnabled (): Boolean = nfcManager? .defaultAdapter? .isEnabled ? : false
2225
23- override fun enable (): KMResult <* > = suAdapter.execute(" svc nfc enable" )
26+ override fun enable (): KMResult <* > {
27+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
28+ return systemBridgeConnectionManager.run { bridge -> bridge.setNfcEnabled(true ) }
29+ } else {
30+ return suAdapter.execute(" svc nfc enable" )
31+ }
32+ }
2433
25- override fun disable (): KMResult <* > = suAdapter.execute(" svc nfc disable" )
34+ override fun disable (): KMResult <* > {
35+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
36+ return systemBridgeConnectionManager.run { bridge -> bridge.setNfcEnabled(false ) }
37+ } else {
38+ return suAdapter.execute(" svc nfc disable" )
39+ }
40+ }
2641}
0 commit comments