Skip to content

Commit 5e3eada

Browse files
committed
feat(Android): add device state listeners
1 parent d29cfcf commit 5e3eada

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.freeraspreactnative
2+
3+
import com.aheaditec.talsec_security.security.api.ThreatListener
4+
5+
internal object FreeraspDeviceStateListener: ThreatListener.DeviceState {
6+
7+
internal var listener: DeviceStateListener? = null
8+
9+
override fun onUnlockedDeviceDetected() {
10+
"unlockedDevice".let {
11+
listener?.deviceStateChangeDetected("passcode")
12+
}
13+
}
14+
15+
override fun onHardwareBackedKeystoreNotAvailableDetected() {
16+
"hardwareBackedKeystoreNotAvailable".let {
17+
listener?.deviceStateChangeDetected(it)
18+
}
19+
}
20+
21+
internal interface DeviceStateListener {
22+
fun deviceStateChangeDetected(threatType: String)
23+
}
24+
25+
}

android/src/main/java/com/freeraspreactnative/FreeraspReactNativeModule.kt

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import com.facebook.react.bridge.ReadableMap
1212
import com.facebook.react.modules.core.DeviceEventManagerModule
1313

1414
class FreeraspReactNativeModule(val reactContext: ReactApplicationContext) :
15-
ReactContextBaseJavaModule(reactContext), ThreatListener.ThreatDetected {
15+
ReactContextBaseJavaModule(reactContext), ThreatListener.ThreatDetected, FreeraspDeviceStateListener.DeviceStateListener {
1616

17-
private val listener = ThreatListener(this)
17+
private val listener = ThreatListener(this, FreeraspDeviceStateListener)
1818

1919
override fun getName(): String {
2020
return NAME
@@ -27,8 +27,8 @@ class FreeraspReactNativeModule(val reactContext: ReactApplicationContext) :
2727

2828
try {
2929
val config = parseTalsecConfig(options)
30+
FreeraspDeviceStateListener.listener = this
3031
listener.registerListener(reactContext)
31-
ThreatListener( this,deviceStateListener).registerListener(reactContext)
3232
Talsec.start(reactContext, config)
3333
sendOngoingPluginResult("started", null)
3434

@@ -78,15 +78,8 @@ class FreeraspReactNativeModule(val reactContext: ReactApplicationContext) :
7878
sendOngoingPluginResult("device binding", null)
7979
}
8080

81-
// This is optional. Use only if you are interested in device state information like device lock and HW backed keystore state
82-
private val deviceStateListener = object : ThreatListener.DeviceState {
83-
override fun onUnlockedDeviceDetected() {
84-
sendOngoingPluginResult("unlockedDevice", null)
85-
}
86-
87-
override fun onHardwareBackedKeystoreNotAvailableDetected() {
88-
sendOngoingPluginResult("hardwareBackedKeystoreNotAvailable", null)
89-
}
81+
override fun deviceStateChangeDetected(threatType: String) {
82+
sendOngoingPluginResult(threatType, null)
9083
}
9184

9285
private fun sendOngoingPluginResult(eventName: String, params: WritableMap?) {
@@ -95,7 +88,6 @@ class FreeraspReactNativeModule(val reactContext: ReactApplicationContext) :
9588
.emit(eventName, params)
9689
}
9790

98-
9991
private fun parseTalsecConfig(config: ReadableMap): TalsecConfig {
10092
val androidConfig = config.getMap("androidConfig")!!
10193
val packageName = androidConfig.getString("packageName")!!

0 commit comments

Comments
 (0)