A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
diff --git a/android/src/main/java/expo/modules/shareintent/ExpoShareIntentModule.kt b/android/src/main/java/expo/modules/shareintent/ExpoShareIntentModule.kt
index ff5b1001e7bb3443482727cec48c22579465c73b..e9c385108f6cebd7cc987d0a65a734f3e8305786 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
@@ -112,15 +115,9 @@ class ExpoShareIntentModule : Module() {
}
fun handleShareIntent(intent: Intent) {
- val activity = instance?.currentActivity
- if (activity != null && !activity.isTaskRoot) {
- val newIntent = Intent(intent).apply {
- addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
- }
- activity.startActivity(newIntent)
- activity.finish()
- return
- }
+ // Shares from the share sheet reach the main activity in the app's own task through
+ // ExpoShareIntentTrampolineActivity. The main activity is still exported, so another app
+ // can start it directly (e.g. a share target it remembered from an older version).
if (intent.type == null) return
if (intent.type!!.startsWith("text/plain")) {
// text / urls
@@ -180,24 +177,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 {
@@ -223,15 +236,9 @@ class ExpoShareIntentModule : Module() {
if (DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
- val docId = DocumentsContract.getDocumentId(uri)
- val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
- val type = split[0]
-
- return if ("primary".equals(type, ignoreCase = true)) {
- Environment.getExternalStorageDirectory().toString() + "/" + split[1]
- } else {
- getDataColumn(uri, null, null)
- }
+ // Copy it like any other content uri: with scoped storage the app can't read
+ // the file path of a document in shared storage, even on the primary volume.
+ return getDataColumn(uri, null, null)
} else if (isDownloadsDocument(uri)) {
return try {
val id = DocumentsContract.getDocumentId(uri)
diff --git a/android/src/main/java/expo/modules/shareintent/ExpoShareIntentReactActivityLifecycleListener.kt b/android/src/main/java/expo/modules/shareintent/ExpoShareIntentReactActivityLifecycleListener.kt
index db573ecbd560de3c71aa443b7ad88d1b5e1c8dc7..4d90556a8640a6d269056d4546591c850e9e8595 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
}
}
\ No newline at end of file
diff --git a/android/src/main/java/expo/modules/shareintent/ExpoShareIntentSingleton.kt b/android/src/main/java/expo/modules/shareintent/ExpoShareIntentSingleton.kt
index 7f93abe410557d8f909148e57d7311cabbe75b36..809143015a733701ea93dbce90c65f1335b2e119 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()}"
+ }
}
\ No newline at end of file
diff --git a/android/src/main/java/expo/modules/shareintent/ExpoShareIntentTrampolineActivity.kt b/android/src/main/java/expo/modules/shareintent/ExpoShareIntentTrampolineActivity.kt
new file mode 100644
index 0000000000000000000000000000000000000000..8258d653884938fe14eb99e3c16bb512aa109a12
--- /dev/null
+++ b/android/src/main/java/expo/modules/shareintent/ExpoShareIntentTrampolineActivity.kt
@@ -0,0 +1,39 @@
+package expo.modules.shareintent
+
+import android.app.Activity
+import android.content.Intent
+import android.os.Bundle
+import android.util.Log
+
+/**
+ * Receives the share intents (the config plugin puts the intent filters here instead of on the
+ * main activity) and forwards them to the main activity in the app's own task.
+ *
+ * Android launches the share target in the sharing app's task when the share sheet was started for
+ * a result (e.g. by apps using Flutter's share_plus), even if the target is singleTask. If that
+ * target were the main activity, a second react surface would mount in that task and get torn down
+ * (taking the main activity's surface with it) as soon as the share is moved to the app's task.
+ * This activity has no UI, so it doesn't matter which task it starts in.
+ */
+class ExpoShareIntentTrampolineActivity : Activity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ val target = packageManager.getLaunchIntentForPackage(packageName)?.component
+ if (target != null) {
+ val grantFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
+ Intent.FLAG_GRANT_WRITE_URI_PERMISSION or
+ Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION or
+ Intent.FLAG_GRANT_PREFIX_URI_PERMISSION
+ val forward = Intent(intent).apply {
+ component = target
+ // Drop the original launch flags (FORWARD_RESULT, NEW_DOCUMENT, MULTIPLE_TASK, ...),
+ // keeping only the uri grants so the main activity can read the shared files.
+ flags = (intent.flags and grantFlags) or Intent.FLAG_ACTIVITY_NEW_TASK
+ }
+ startActivity(forward)
+ } else {
+ Log.w("ExpoShareIntent", "No launch activity to forward the share to, dropping it")
+ }
+ finish()
+ }
+}
diff --git a/plugin/build/android/withAndroidIntentFilters.js b/plugin/build/android/withAndroidIntentFilters.js
index 8814220cab965a7393bc422873cfdf6be29b13a6..9c1f09acf391de61c6565cff8640c41e4a6f9ccd 100644
--- a/plugin/build/android/withAndroidIntentFilters.js
+++ b/plugin/build/android/withAndroidIntentFilters.js
@@ -46,7 +46,7 @@ function renderIntentFilterCategory(category) {
}));
}
function addIntentFilters(androidManifest, currentIntentFilters, filters, multiFilters) {
- const mainActivity = config_plugins_1.AndroidConfig.Manifest.getMainActivityOrThrow(androidManifest);
+ const application = config_plugins_1.AndroidConfig.Manifest.getMainApplicationOrThrow(androidManifest);
// DEFAULT VALUE (text and url)
const newFilters = filters || ["text/*"];
console.warn(`[expo-share-intent] add android filters (${newFilters.join(" ")}) and multi-filters (${multiFilters ? multiFilters.join(" ") : ""})`);
@@ -74,8 +74,26 @@ function addIntentFilters(androidManifest, currentIntentFilters, filters, multiF
...newIntentFilters,
...newMultiIntentFilters,
]);
- // adds them properly to the manifest
- mainActivity["intent-filter"] = mainActivity["intent-filter"]?.concat(renderedNewIntentFilters);
+ // Shares go through a trampoline activity that forwards them to the main activity in the app's
+ // own task, see ExpoShareIntentTrampolineActivity. Drop the share filters older versions of
+ // this plugin put on the main activity (left behind by a prebuild without --clean), or it'd
+ // show up as a second share target that bypasses the trampoline.
+ const mainActivity = config_plugins_1.AndroidConfig.Manifest.getMainActivityOrThrow(androidManifest);
+ const shareActions = ["android.intent.action.SEND", "android.intent.action.SEND_MULTIPLE"];
+ mainActivity["intent-filter"] = mainActivity["intent-filter"]?.filter((filter) => !filter.action?.some((action) => shareActions.includes(action.$?.["android:name"])));
+ const trampolineName = "expo.modules.shareintent.ExpoShareIntentTrampolineActivity";
+ application["activity"] = (application["activity"] ?? []).filter((activity) => activity.$["android:name"] !== trampolineName);
+ application["activity"].push({
+ $: {
+ "android:name": trampolineName,
+ "android:exported": "true",
+ "android:excludeFromRecents": "true",
+ "android:noHistory": "true",
+ "android:taskAffinity": "",
+ "android:theme": "@android:style/Theme.NoDisplay",
+ },
+ "intent-filter": renderedNewIntentFilters,
+ });
return androidManifest;
}
const withAndroidIntentFilters = (config, parameters) => {