Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix(mobile): fix lost and duplicated Android shares after process death

Mohamed Basseme7fbaab7fc83
diff --git a/patches/[email protected] b/patches/[email protected]
new file mode 100644
index 00000000..13ddacf2
--- /dev/null
+++ b/patches/[email protected]
@@ -0,0 +1,173 @@
+diff --git a/android/src/main/java/expo/modules/shareintent/ExpoShareIntentModule.kt b/android/src/main/java/expo/modules/shareintent/ExpoShareIntentModule.kt
+index ff5b1001e7bb3443482727cec48c22579465c73b..1ae6de08f1c8481121521d637f621ad86c073775 100644
+--- a/android/src/main/java/expo/modules/shareintent/ExpoShareIntentModule.kt
++++ b/android/src/main/java/expo/modules/shareintent/ExpoShareIntentModule.kt
+@@ -41,6 +41,9 @@ class ExpoShareIntentModule : Module() {
+ private val currentActivity: Activity?
+ get() = appContext.currentActivity
+
++ @Volatile
++ private var hasJsListeners = false
++
+ companion object {
+ private var instance: ExpoShareIntentModule? = null
+
+@@ -180,24 +183,40 @@ class ExpoShareIntentModule : Module() {
+ Events("onChange", "onStateChange", "onError")
+
+ AsyncFunction("getShareIntent") { _: String ->
+- // get the Intent from onCreate activity (app not running in background)
++ // get the Intent stored by the activity lifecycle listener (onCreate / onNewIntent)
+ ExpoShareIntentSingleton.isPending = false
+- if (ExpoShareIntentSingleton.intent?.type != null) {
+- handleShareIntent(ExpoShareIntentSingleton.intent!!);
+- ExpoShareIntentSingleton.intent = null
++ ExpoShareIntentSingleton.takePendingIntent(appContext.reactContext)?.let {
++ handleShareIntent(it)
+ }
+ }
+
+ Function("clearShareIntent") { _: String ->
+- ExpoShareIntentSingleton.intent = null
++ ExpoShareIntentSingleton.clearPendingIntent()
+ }
+
+ Function("hasShareIntent") { _: String ->
+ ExpoShareIntentSingleton.isPending
+ }
+
++ OnStartObserving("onChange") {
++ hasJsListeners = true
++ }
++
++ OnStopObserving("onChange") {
++ hasJsListeners = false
++ }
++
+ OnNewIntent {
+- handleShareIntent(it)
++ if (it.type != null) {
++ ExpoShareIntentSingleton.setPendingIntent(it, null)
++ }
++ // Events sent before JS subscribes are dropped. In that case leave the intent pending,
++ // JS fetches it with getShareIntent once it's mounted.
++ if (hasJsListeners) {
++ ExpoShareIntentSingleton.takePendingIntent(appContext.reactContext)?.let {
++ handleShareIntent(it)
++ }
++ }
+ }
+
+ OnCreate {
+diff --git a/android/src/main/java/expo/modules/shareintent/ExpoShareIntentReactActivityLifecycleListener.kt b/android/src/main/java/expo/modules/shareintent/ExpoShareIntentReactActivityLifecycleListener.kt
+index db573ecbd560de3c71aa443b7ad88d1b5e1c8dc7..17a0efcb3839b883814c35697fde8b2759bfea19 100644
+--- a/android/src/main/java/expo/modules/shareintent/ExpoShareIntentReactActivityLifecycleListener.kt
++++ b/android/src/main/java/expo/modules/shareintent/ExpoShareIntentReactActivityLifecycleListener.kt
+@@ -13,10 +13,20 @@ import expo.modules.core.interfaces.ReactActivityLifecycleListener
+ class ExpoShareIntentReactActivityLifecycleListener(activityContext: Context) : ReactActivityLifecycleListener {
+
+ override fun onCreate(activity: Activity?, savedInstanceState: Bundle?) {
++ val intent = activity?.intent ?: return
+ // only store when the new intent is not empty
+- if (activity?.intent?.type != null) {
+- ExpoShareIntentSingleton.intent = activity?.intent
+- ExpoShareIntentSingleton.isPending = true
++ if (intent.type == null) return
++ if (ExpoShareIntentSingleton.isReplayedLaunchIntent(activity, activity.taskId, intent)) return
++ ExpoShareIntentSingleton.setPendingIntent(intent, activity.taskId)
++ }
++
++ // When the process was killed in the background, sharing recreates the activity with the task's
++ // original intent and delivers the share through onNewIntent right away, before the native
++ // module exists. Store it here so JS can still pick it up with getShareIntent.
++ override fun onNewIntent(intent: Intent?): Boolean {
++ if (intent?.type != null) {
++ ExpoShareIntentSingleton.setPendingIntent(intent, null)
+ }
++ return false
+ }
+ }
+diff --git a/android/src/main/java/expo/modules/shareintent/ExpoShareIntentSingleton.kt b/android/src/main/java/expo/modules/shareintent/ExpoShareIntentSingleton.kt
+index 7f93abe410557d8f909148e57d7311cabbe75b36..538c6be0274b9d8cc022e00c11e0efb1d5c5b2c4 100644
+--- a/android/src/main/java/expo/modules/shareintent/ExpoShareIntentSingleton.kt
++++ b/android/src/main/java/expo/modules/shareintent/ExpoShareIntentSingleton.kt
+@@ -1,5 +1,6 @@
+ package expo.modules.shareintent
+
++import android.content.Context
+ import android.content.Intent
+ import expo.modules.core.interfaces.SingletonModule
+
+@@ -9,7 +10,70 @@ object ExpoShareIntentSingleton : SingletonModule {
+ return "ExpoShareIntent"
+ }
+
+- // members to store the initial launch intent
+- var intent: Intent? = null
++ private const val PREFS_NAME = "expo-share-intent"
++ private const val PREFS_CONSUMED_LAUNCH_INTENT = "consumedLaunchIntent"
++
++ // share intent waiting for JS to ask for it (getShareIntent)
++ private var intent: Intent? = null
++ // id of the task this intent launched, null when it came through onNewIntent
++ private var launchTaskId: Int? = null
+ var isPending: Boolean = false
++
++ @Synchronized
++ fun setPendingIntent(intent: Intent, launchTaskId: Int?) {
++ this.intent = intent
++ this.launchTaskId = launchTaskId
++ isPending = true
++ }
++
++ @Synchronized
++ fun clearPendingIntent() {
++ intent = null
++ launchTaskId = null
++ }
++
++ /**
++ * Returns the pending intent (if any) and clears it. If the intent is the one that launched the
++ * task, remember it so that Android re-delivering it later is not treated as a new share.
++ */
++ @Synchronized
++ fun takePendingIntent(context: Context?): Intent? {
++ val pending = intent ?: return null
++ val taskId = launchTaskId
++ clearPendingIntent()
++ if (taskId != null && context != null) {
++ context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
++ .edit()
++ .putString(PREFS_CONSUMED_LAUNCH_INTENT, consumedKey(taskId, pending))
++ .apply()
++ }
++ return pending
++ }
++
++ /**
++ * Android keeps the intent that started a task and hands it to the activity again whenever the
++ * activity is recreated in that task (e.g. after the process was killed in the background and the
++ * user comes back from the launcher or recents). Such a share was already handled.
++ */
++ fun isReplayedLaunchIntent(context: Context, taskId: Int, intent: Intent): Boolean {
++ if (intent.flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY != 0) {
++ return true
++ }
++ val consumed = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
++ .getString(PREFS_CONSUMED_LAUNCH_INTENT, null)
++ return consumed == consumedKey(taskId, intent)
++ }
++
++ private fun consumedKey(taskId: Int, intent: Intent): String {
++ @Suppress("DEPRECATION")
++ val stream = intent.extras?.get(Intent.EXTRA_STREAM)
++ val fingerprint = listOf(
++ intent.action,
++ intent.type,
++ intent.dataString,
++ intent.getStringExtra(Intent.EXTRA_TEXT),
++ stream?.toString(),
++ ).joinToString("\u0000")
++ return "$taskId:${fingerprint.hashCode()}"
++ }
+ }
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 67fb0ffa..549bde0c 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -20,6 +20,7 @@ overrides:
patchedDependencies:
[email protected]: 0de91114675edaf8158061b65f08995617cd872a813892f81e28394ded741a77
+ [email protected]: b6e86be022ff16e70e0e69bcca5ad3191471b7020bf8c4edaa69fed488ad1680
[email protected]: 7d74c7fbd33e9f3f3f8a8ba47a013acfc098948e39c7faab1232e6edca358ac1
[email protected]: d7b79c3ae412d8e700a8c176df42ee89112c9bdc91d2aad0ebb7d5b011b49993
[email protected]: 725863c0591d89ca053226c49fe7bc321f320391ec5f78b6c2e8e41ab1868805
@@ -448,7 +449,7 @@ importers:
version: 57.0.4([email protected])
expo-share-intent:
specifier: ^8.0.1
- version: 8.0.1([email protected])([email protected])([email protected])([email protected](patch_hash=7d74c7fbd33e9f3f3f8a8ba47a013acfc098948e39c7faab1232e6edca358ac1)(@babel/[email protected])(@react-native/[email protected](@babel/[email protected]))(@types/[email protected])([email protected]))([email protected])([email protected])
+ version: 8.0.1(patch_hash=b6e86be022ff16e70e0e69bcca5ad3191471b7020bf8c4edaa69fed488ad1680)([email protected])([email protected])([email protected])([email protected](patch_hash=7d74c7fbd33e9f3f3f8a8ba47a013acfc098948e39c7faab1232e6edca358ac1)(@babel/[email protected])(@react-native/[email protected](@babel/[email protected]))(@types/[email protected])([email protected]))([email protected])([email protected])
expo-sharing:
specifier: ~57.0.22
version: 57.0.22([email protected])([email protected](patch_hash=7d74c7fbd33e9f3f3f8a8ba47a013acfc098948e39c7faab1232e6edca358ac1)(@babel/[email protected])(@react-native/[email protected](@babel/[email protected]))(@types/[email protected])([email protected]))([email protected])([email protected])
@@ -28125,7 +28126,7 @@ snapshots:
+ [email protected](patch_hash=b6e86be022ff16e70e0e69bcca5ad3191471b7020bf8c4edaa69fed488ad1680)([email protected])([email protected])([email protected])([email protected](patch_hash=7d74c7fbd33e9f3f3f8a8ba47a013acfc098948e39c7faab1232e6edca358ac1)(@babel/[email protected])(@react-native/[email protected](@babel/[email protected]))(@types/[email protected])([email protected]))([email protected])([email protected]):
dependencies:
'@expo/config-plugins': 57.0.9([email protected])
expo: 57.0.25(6b33b2113934110f2459ad3be3e8fb0f)
diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml
index 19f573e2..b3e319d9 100644
--- a/pnpm-workspace.yaml
+++ b/pnpm-workspace.yaml
@@ -30,6 +30,9 @@ patchedDependencies:
# Disables React 19.2's dev "component performance track" prop serialization,
# which crashes when a changed prop is a tRPC proxy (RN 0.86 + new arch).
+ # Android: don't drop shares that arrive while the app is being recreated after
+ # process death, and don't replay the task's launch share on reopen (#513).
# Docker builds can prune packages that use one of the root patches.
allowUnusedPatches: true