[0.2.2] Android still have duplicated screenshot events
Hi! 👋
Firstly, thanks for your work on this project! 🙂
Today I used patch-package to patch [email protected] for the project I'm working on.
I was having screenshot duplicated issues on Android and noticed that you had a new version 0.2.2 that was tackling this issue. But I found that there was still an edge case where it was having duplicated events. On a Pixel 2 XL on Android 11 when a screenshot is done it generates the following files:
/storage/emulated/0/Pictures/Screenshots/.pending-1654277201-Screenshot_20220527-132641.png
/storage/emulated/0/Pictures/Screenshots/Screenshot_20220527-132641.png
As you can see, your current fix implementation does not work in this case since one of the filename starts with .pending-xxx-. So, I fixed it on my end by checking instead if the previous path was containing the same filename excluding was comes before Screenshot_20220527-132641.png.
Here is the diff that solved my problem:
diff --git a/node_modules/react-native-detector/android/src/main/java/com/.DS_Store b/node_modules/react-native-detector/android/src/main/java/com/.DS_Store
new file mode 100644
index 0000000..e69de29
diff --git a/node_modules/react-native-detector/android/src/main/java/com/reactnativedetector/ScreenshotDetectionDelegate.kt b/node_modules/react-native-detector/android/src/main/java/com/reactnativedetector/ScreenshotDetectionDelegate.kt
index b8964d3..325d802 100644
--- a/node_modules/react-native-detector/android/src/main/java/com/reactnativedetector/ScreenshotDetectionDelegate.kt
+++ b/node_modules/react-native-detector/android/src/main/java/com/reactnativedetector/ScreenshotDetectionDelegate.kt
@@ -28,7 +28,8 @@ class ScreenshotDetectionDelegate(val context: Context, val listener: Screenshot
if (isReadExternalStoragePermissionGranted() && uri != null) {
val path = getFilePathFromContentResolver(context, uri)
if (path != null && isScreenshotPath(path)) {
- previousPath = path
+ previousPath = path.toLowerCase().substring(path.toLowerCase().lastIndexOf("screenshot"))
onScreenCaptured(path!!)
}
} else {
@@ -57,7 +58,7 @@ class ScreenshotDetectionDelegate(val context: Context, val listener: Screenshot
}
private fun isScreenshotPath(path: String?): Boolean {
- return path != null && path.toLowerCase().contains("screenshots") && previousPath != path
+ return path != null && path.toLowerCase().contains("screenshots") && (previousPath == "" || !path.toLowerCase().contains(previousPath))
}
private fun getFilePathFromContentResolver(context: Context, uri: Uri): String? {
This issue body was partially generated by patch-package.
@hubastard I think you could add debounce to your javascript code.
Thank you @hubastard, can you create a PR for the fix?