@@ -9,7 +9,6 @@ import com.aheaditec.talsec_security.security.api.SuspiciousAppInfo
99import com.aheaditec.talsec_security.security.api.Talsec
1010import com.aheaditec.talsec_security.security.api.TalsecConfig
1111import com.aheaditec.talsec_security.security.api.TalsecMode
12- import com.aheaditec.talsec_security.security.api.ThreatListener
1312import com.facebook.react.bridge.Arguments
1413import com.facebook.react.bridge.LifecycleEventListener
1514import com.facebook.react.bridge.Promise
@@ -19,8 +18,8 @@ import com.facebook.react.bridge.ReactMethod
1918import com.facebook.react.bridge.ReadableMap
2019import com.facebook.react.bridge.UiThreadUtil.runOnUiThread
2120import com.facebook.react.bridge.WritableArray
21+ import com.facebook.react.bridge.WritableMap
2222import com.facebook.react.modules.core.DeviceEventManagerModule
23- import com.freeraspreactnative.events.BaseRaspEvent
2423import com.freeraspreactnative.events.RaspExecutionStateEvent
2524import com.freeraspreactnative.events.ThreatEvent
2625import com.freeraspreactnative.interfaces.PluginExecutionStateListener
@@ -38,19 +37,25 @@ class FreeraspReactNativeModule(private val reactContext: ReactApplicationContex
3837
3938 private val lifecycleListener = object : LifecycleEventListener {
4039 override fun onHostResume () {
40+ PluginThreatHandler .threatDispatcher.onResume()
41+ PluginThreatHandler .executionStateDispatcher.onResume()
4142 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .UPSIDE_DOWN_CAKE ) {
4243 reactContext.currentActivity?.let { ScreenProtector .register(it) }
4344 }
4445 }
4546
4647 override fun onHostPause () {
48+ PluginThreatHandler .threatDispatcher.onPause()
49+ PluginThreatHandler .executionStateDispatcher.onPause()
4750 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .UPSIDE_DOWN_CAKE ) {
4851 reactContext.currentActivity?.let { ScreenProtector .unregister(it) }
4952 }
5053 }
5154
5255 override fun onHostDestroy () {
5356 backgroundHandlerThread.quitSafely()
57+ PluginThreatHandler .threatDispatcher.unregisterListener()
58+ PluginThreatHandler .executionStateDispatcher.unregisterListener()
5459 }
5560 }
5661
@@ -61,6 +66,7 @@ class FreeraspReactNativeModule(private val reactContext: ReactApplicationContex
6166 init {
6267 reactContext.addLifecycleEventListener(lifecycleListener)
6368 initializeEventKeys()
69+ PluginThreatHandler .initializeDispatchers(PluginListener (reactContext))
6470 }
6571
6672 @ReactMethod
@@ -149,10 +155,10 @@ class FreeraspReactNativeModule(private val reactContext: ReactApplicationContex
149155 @ReactMethod
150156 fun addListener (eventName : String ) {
151157 if (eventName == ThreatEvent .CHANNEL_NAME ) {
152- PluginThreatHandler .threatDispatcher.listener = PluginListener (reactContext )
158+ PluginThreatHandler .threatDispatcher.registerListener( )
153159 }
154160 if (eventName == RaspExecutionStateEvent .CHANNEL_NAME ) {
155- PluginThreatHandler .executionStateDispatcher.listener = PluginListener (reactContext )
161+ PluginThreatHandler .executionStateDispatcher.registerListener( )
156162 }
157163 }
158164
@@ -165,10 +171,10 @@ class FreeraspReactNativeModule(private val reactContext: ReactApplicationContex
165171 @ReactMethod
166172 fun removeListenerForEvent (eventName : String , promise : Promise ) {
167173 if (eventName == ThreatEvent .CHANNEL_NAME ) {
168- PluginThreatHandler .threatDispatcher.listener = null
174+ PluginThreatHandler .threatDispatcher.unregisterListener()
169175 }
170176 if (eventName == RaspExecutionStateEvent .CHANNEL_NAME ) {
171- PluginThreatHandler .executionStateDispatcher.listener = null
177+ PluginThreatHandler .executionStateDispatcher.unregisterListener()
172178 }
173179 promise.resolve(" Listener unregistered" )
174180 }
@@ -302,48 +308,40 @@ class FreeraspReactNativeModule(private val reactContext: ReactApplicationContex
302308 private val mainHandler = Handler (Looper .getMainLooper())
303309
304310 internal var talsecStarted = false
311+ }
312+
313+ internal class PluginListener (private val reactContext : ReactApplicationContext ) :
314+ PluginThreatListener , PluginExecutionStateListener {
305315
306- private fun notifyEvent ( event : BaseRaspEvent , appReactContext : ReactApplicationContext ) {
316+ override fun threatDetected ( threatEventType : ThreatEvent ) {
307317 val params = Arguments .createMap()
308- params.putInt(event.channelKey, event.value)
309- appReactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
310- .emit(event.channelName, params)
318+ params.putInt(threatEventType.channelKey, threatEventType.value)
319+ notifyEvent(ThreatEvent .CHANNEL_NAME , params)
311320 }
312321
313- /* *
314- * Sends malware detected event to React Native
315- */
316- private fun notifyMalware (suspiciousApps : MutableList <SuspiciousAppInfo >, appReactContext : ReactApplicationContext ) {
317- // Perform the malware encoding on a background thread
322+ override fun malwareDetected (suspiciousApps : MutableList <SuspiciousAppInfo >) {
318323 backgroundHandler.post {
319-
320- val encodedSuspiciousApps = suspiciousApps.toEncodedWritableArray(appReactContext)
321-
324+ val encodedSuspiciousApps = suspiciousApps.toEncodedWritableArray(reactContext)
322325 mainHandler.post {
323326 val params = Arguments .createMap()
324327 params.putInt(ThreatEvent .CHANNEL_KEY , ThreatEvent .Malware .value)
325328 params.putArray(
326329 ThreatEvent .MALWARE_CHANNEL_KEY , encodedSuspiciousApps
327330 )
328-
329- appReactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
330- .emit(ThreatEvent .CHANNEL_NAME , params)
331+ notifyEvent(ThreatEvent .CHANNEL_NAME , params)
331332 }
332333 }
333334 }
334- }
335335
336- internal class PluginListener (private val reactContext : ReactApplicationContext ) : PluginThreatListener, PluginExecutionStateListener {
337- override fun threatDetected (threatEventType : ThreatEvent ) {
338- notifyEvent(threatEventType, reactContext)
339- }
340-
341- override fun malwareDetected (suspiciousApps : MutableList <SuspiciousAppInfo >) {
342- notifyMalware(suspiciousApps, reactContext)
336+ override fun raspExecutionStateChanged (event : RaspExecutionStateEvent ) {
337+ val params = Arguments .createMap()
338+ params.putInt(event.channelKey, event.value)
339+ notifyEvent(event.channelName, params)
343340 }
344341
345- override fun raspExecutionStateChanged (event : RaspExecutionStateEvent ) {
346- notifyEvent(event, reactContext)
342+ private fun notifyEvent (eventName : String , params : WritableMap ) {
343+ reactContext.getJSModule(DeviceEventManagerModule .RCTDeviceEventEmitter ::class .java)
344+ .emit(eventName, params)
347345 }
348346 }
349- }
347+ }
0 commit comments