Skip to content

An open-source cross-platform alternative to AirDrop

fix(android): share intents not received by app

Tien Do Nam154213c1df87
diff --git a/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt b/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt
index b17633aa..491906f2 100644
--- a/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt
+++ b/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt
@@ -24,6 +24,31 @@ private const val REQUEST_CODE_PICK_FILE = 3
class MainActivity : FlutterActivity() {
private var pendingResult: MethodChannel.Result? = null
+ /// share_handler drops share intents arriving via onNewIntent while the Dart side
+ /// is not subscribed to its media stream yet, which happens when this singleTask
+ /// activity is relaunched into an existing task while the app is still starting.
+ /// Hold such intents back until Dart reports readiness ("shareIntentReady"), then
+ /// replay them through the regular plugin path.
+ private val pendingShareIntents = mutableListOf<Intent>()
+ private var shareIntentReady = false
+
+ override fun onNewIntent(intent: Intent) {
+ if (!shareIntentReady && (intent.action == Intent.ACTION_SEND || intent.action == Intent.ACTION_SEND_MULTIPLE)) {
+ pendingShareIntents.add(intent)
+ return
+ }
+ super.onNewIntent(intent)
+ }
+
+ private fun onShareIntentReady() {
+ shareIntentReady = true
+ val pending = pendingShareIntents.toList()
+ pendingShareIntents.clear()
+ for (intent in pending) {
+ super.onNewIntent(intent)
+ }
+ }
+
// Overriding the static methods we need from the Java class, as described
// in the documentation of `FlutterActivity.NewEngineIntentBuilder`
companion object {
@@ -76,6 +101,11 @@ class MainActivity : FlutterActivity() {
result.success(null)
}
+ "shareIntentReady" -> {
+ onShareIntentReady()
+ result.success(null)
+ }
+
"isAnimationsEnabled" -> {
result.success(isAnimationsEnabled())
}
diff --git a/app/assets/CHANGELOG.md b/app/assets/CHANGELOG.md
index 3f4078de..1e9c0983 100644
--- a/app/assets/CHANGELOG.md
+++ b/app/assets/CHANGELOG.md
@@ -26,6 +26,7 @@
- fix: text message content size calculation (@ew-sirenko)
- fix: save DNG files to image gallery (@ShlomoCode)
- fix(ios, android): prevent transfer error by saving unsupported media formats to folder instead of gallery (@ShlomoCode)
+- fix(android): share to LocalSend sometimes doesn't work
- fix(android): preserve location metadata when sharing media (@ShlomoCode)
- fix(macos): prevent Dock icon from briefly appearing during autostart when "Start hidden" is enabled (@ShlomoCode)
- fix(macos): Dock icon drag-and-drop and Share Extension working again (@ShlomoCode)
diff --git a/app/lib/config/init.dart b/app/lib/config/init.dart
index e0794ffe..f8867d07 100644
--- a/app/lib/config/init.dart
+++ b/app/lib/config/init.dart
@@ -29,6 +29,7 @@ import 'package:localsend_app/provider/window_dimensions_provider.dart';
import 'package:localsend_app/util/i18n.dart';
import 'package:localsend_app/util/native/autostart_helper.dart';
import 'package:localsend_app/util/native/cache_helper.dart';
+import 'package:localsend_app/util/native/channel/android_channel.dart';
import 'package:localsend_app/util/native/context_menu_helper.dart';
import 'package:localsend_app/util/native/cross_file_converters.dart';
import 'package:localsend_app/util/native/device_info_helper.dart';
@@ -267,6 +268,12 @@ Future<void> postInit(BuildContext context, Ref ref, bool appStart) async {
),
);
});
+
+ if (checkPlatform([TargetPlatform.android])) {
+ // Both messages above travel through the same messenger in order, so the stream is
+ // guaranteed to be attached natively before MainActivity replays held-back intents.
+ await flushPendingShareIntentsAndroid();
+ }
}
if (appStart && !hasInitialShare && (checkPlatformWithGallery() || checkPlatformCanReceiveShareIntent())) {
diff --git a/app/lib/util/native/channel/android_channel.dart b/app/lib/util/native/channel/android_channel.dart
index 6eb64182..e3ee7734 100644
--- a/app/lib/util/native/channel/android_channel.dart
+++ b/app/lib/util/native/channel/android_channel.dart
@@ -62,6 +62,16 @@ Future<void> openContentUri({
});
}
+/// Tells MainActivity that the Dart side is now subscribed to the share_handler media stream,
+/// so share intents that were held back during app start can be replayed.
+Future<void> flushPendingShareIntentsAndroid() async {
+ try {
+ await _methodChannel.invokeMethod('shareIntentReady');
+ } catch (e) {
+ _logger.warning('Could not flush pending share intents', e);
+ }
+}
+
Future<void> openGallery() async {
_logger.info('Opening gallery');
await _methodChannel.invokeMethod('openGallery');