From e5fb7c05b044ee85af633be00e84960a92776b77 Mon Sep 17 00:00:00 2001 From: Samantha Lau Date: Thu, 29 May 2025 10:53:22 +0800 Subject: [PATCH] fix: adds Android 14+ implementation and fixes native module cannot be null error --- .../com/reactnativedetector/DetectorModule.kt | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/android/src/main/java/com/reactnativedetector/DetectorModule.kt b/android/src/main/java/com/reactnativedetector/DetectorModule.kt index 189e51f..5c1b50e 100644 --- a/android/src/main/java/com/reactnativedetector/DetectorModule.kt +++ b/android/src/main/java/com/reactnativedetector/DetectorModule.kt @@ -4,29 +4,34 @@ import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactContextBaseJavaModule import com.facebook.react.bridge.ReactMethod import com.facebook.react.modules.core.DeviceEventManagerModule - - +import android.app.Activity class DetectorModule(val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext), ScreenshotDetectionListener { - private val screenshotDetectionDelegate = ScreenshotDetectionDelegate(reactContext, this) + private var screenshotDetectionDelegate: ScreenshotDetectionDelegate? = null + override fun getName(): String { return "Detector" } @ReactMethod fun startScreenshotDetection() { - screenshotDetectionDelegate.startScreenshotDetection() + val currentActivity = reactContext.currentActivity + if (currentActivity != null) { + screenshotDetectionDelegate = ScreenshotDetectionDelegate(currentActivity, this) + screenshotDetectionDelegate?.startScreenshotDetection() + } } @ReactMethod fun stopScreenshotDetection() { - screenshotDetectionDelegate.stopScreenshotDetection() + screenshotDetectionDelegate?.stopScreenshotDetection() + screenshotDetectionDelegate = null; } override fun onScreenCaptured(path: String) { reactContext - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) - .emit("UIApplicationUserDidTakeScreenshotNotification", null) + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) + .emit("UIApplicationUserDidTakeScreenshotNotification", null) } override fun onScreenCapturedWithDeniedPermission() {