diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index cf7472db..efbf7ed6 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -51,6 +51,16 @@ + + + + + + + + diff --git a/ios/Runner/Info.plist b/ios/Runner/Info.plist index 0088b76d..2c299446 100644 --- a/ios/Runner/Info.plist +++ b/ios/Runner/Info.plist @@ -22,8 +22,21 @@ $(FLUTTER_BUILD_NAME) CFBundleSignature ???? + CFBundleURLTypes + + + CFBundleURLName + app.offband.meshcore.share + CFBundleURLSchemes + + meshcore + + + CFBundleVersion $(FLUTTER_BUILD_NUMBER) + FlutterDeepLinkingEnabled + ITSAppUsesNonExemptEncryption LSApplicationQueriesSchemes diff --git a/lib/main.dart b/lib/main.dart index e806f7f1..c1beed97 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,6 @@ import 'dart:async'; +import 'package:app_links/app_links.dart'; import 'package:flutter/material.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter/foundation.dart'; @@ -442,8 +443,9 @@ class MeshCoreApp extends StatelessWidget { }, home: (PlatformInfo.isWeb && !PlatformInfo.isChrome) ? const ChromeRequiredScreen() - : const LinkRouterHost( - child: IosOnMacNoticeGate( + : LinkRouterHost( + osLinks: AppLinks().uriLinkStream, + child: const IosOnMacNoticeGate( child: SponsorPromptGate(child: ScannerScreen()), ), ), diff --git a/lib/services/link_router.dart b/lib/services/link_router.dart index f44f13b2..466f50c8 100644 --- a/lib/services/link_router.dart +++ b/lib/services/link_router.dart @@ -91,6 +91,15 @@ class LinkRouter extends ChangeNotifier { return link != null; } + /// Takes a link the OS opened the app with (#1291). Only share links are + /// acted on: the OS also reports ordinary launches, and on web the page's + /// own address arrives as the "initial link", neither of which is the + /// user asking to add anything, so they are ignored rather than reported. + void handleFromOs(Uri uri) { + if (uri.scheme.toLowerCase() != 'meshcore') return; + handle(uri.toString()); + } + /// The pending link if its confirmation can be shown now, removing it. /// /// Adding anything needs a connected radio. A channel also waits for the diff --git a/lib/widgets/link_router_host.dart b/lib/widgets/link_router_host.dart index de37df52..282ea65c 100644 --- a/lib/widgets/link_router_host.dart +++ b/lib/widgets/link_router_host.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; @@ -8,6 +10,7 @@ import '../helpers/channel_share_add.dart'; import '../helpers/snack_bar_builder.dart'; import '../l10n/l10n.dart'; import '../services/link_router.dart'; +import '../utils/app_logger.dart'; import 'add_contact_by_key_dialog.dart'; import 'contact_add_feedback.dart'; @@ -17,10 +20,14 @@ import 'contact_add_feedback.dart'; /// It wraps the home route, so it sits below the Navigator and its dialogs /// and messages open over whatever screen is on top. class LinkRouterHost extends StatefulWidget { - const LinkRouterHost({super.key, required this.child}); + const LinkRouterHost({super.key, required this.child, this.osLinks}); final Widget child; + /// Links the OS opened the app with: the one that launched it, then any + /// that arrive while it runs (#1291). Null in tests that do not need it. + final Stream? osLinks; + @override State createState() => _LinkRouterHostState(); } @@ -28,6 +35,7 @@ class LinkRouterHost extends StatefulWidget { class _LinkRouterHostState extends State { late final LinkRouter _router; late final MeshCoreConnector _connector; + StreamSubscription? _osLinks; bool _scheduled = false; bool _showing = false; @@ -38,11 +46,17 @@ class _LinkRouterHostState extends State { _connector = context.read(); _router.addListener(_schedule); _connector.addListener(_schedule); + _osLinks = widget.osLinks?.listen( + _router.handleFromOs, + onError: (Object e) => + appLogger.warn('OS link stream error: $e', tag: 'LinkRouter'), + ); _schedule(); } @override void dispose() { + _osLinks?.cancel(); _router.removeListener(_schedule); _connector.removeListener(_schedule); super.dispose(); diff --git a/linux/flutter/generated_plugin_registrant.cc b/linux/flutter/generated_plugin_registrant.cc index 6cf6d508..8b17840d 100644 --- a/linux/flutter/generated_plugin_registrant.cc +++ b/linux/flutter/generated_plugin_registrant.cc @@ -6,12 +6,16 @@ #include "generated_plugin_registrant.h" +#include #include #include #include #include void fl_register_plugins(FlPluginRegistry* registry) { + g_autoptr(FlPluginRegistrar) app_links_linux_registrar = + fl_plugin_registry_get_registrar_for_plugin(registry, "AppLinksPluginLinux"); + app_links_plugin_linux_register_with_registrar(app_links_linux_registrar); g_autoptr(FlPluginRegistrar) file_selector_linux_registrar = fl_plugin_registry_get_registrar_for_plugin(registry, "FileSelectorPlugin"); file_selector_plugin_register_with_registrar(file_selector_linux_registrar); diff --git a/linux/flutter/generated_plugins.cmake b/linux/flutter/generated_plugins.cmake index b86d669d..ce25e14c 100644 --- a/linux/flutter/generated_plugins.cmake +++ b/linux/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + app_links_linux file_selector_linux screen_retriever_linux url_launcher_linux diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index 6a868e65..d48ef348 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,6 +5,7 @@ import FlutterMacOS import Foundation +import app_links import file_selector_macos import flutter_blue_plus_darwin import flutter_local_notifications @@ -20,6 +21,7 @@ import wakelock_plus import window_manager func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + AppLinksMacosPlugin.register(with: registry.registrar(forPlugin: "AppLinksMacosPlugin")) FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin")) FlutterBluePlusPlugin.register(with: registry.registrar(forPlugin: "FlutterBluePlusPlugin")) FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "FlutterLocalNotificationsPlugin")) diff --git a/macos/Runner/Info.plist b/macos/Runner/Info.plist index c55e9d73..8e24db1d 100644 --- a/macos/Runner/Info.plist +++ b/macos/Runner/Info.plist @@ -18,6 +18,17 @@ APPL CFBundleShortVersionString $(FLUTTER_BUILD_NAME) + CFBundleURLTypes + + + CFBundleURLName + app.offband.meshcore.share + CFBundleURLSchemes + + meshcore + + + CFBundleVersion $(FLUTTER_BUILD_NUMBER) LSMinimumSystemVersion diff --git a/pubspec.lock b/pubspec.lock index d0c8005c..058abef0 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -25,6 +25,38 @@ packages: url: "https://pub.dev" source: hosted version: "12.1.0" + app_links: + dependency: "direct main" + description: + name: app_links + sha256: "08ba88bf19d9edea33130cb71c8ced2cb982b4e00b82efef4a434d1357bc69c3" + url: "https://pub.dev" + source: hosted + version: "7.2.2" + app_links_linux: + dependency: transitive + description: + name: app_links_linux + sha256: "734aa57a2f65d1b35c3643384ccfbe668730157a08479bfa488e10a9da847853" + url: "https://pub.dev" + source: hosted + version: "1.0.5" + app_links_platform_interface: + dependency: transitive + description: + name: app_links_platform_interface + sha256: "7546f09a6e93f4a2df2fe2bd40a5c6c64310ac461b036d82b43033be7a59f809" + url: "https://pub.dev" + source: hosted + version: "2.0.4" + app_links_web: + dependency: transitive + description: + name: app_links_web + sha256: "382d32c6f486f15b1d7f5d6a37a70a481a4fc08382eef419d373169921a3e765" + url: "https://pub.dev" + source: hosted + version: "1.0.5" archive: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 1cb112ad..a13492f3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -62,6 +62,7 @@ dependencies: qr_flutter: ^4.1.0 # QR code generation url_launcher: ^6.3.0 # Launch URLs in system browser flutter_linkify: ^6.0.0 # Auto-detect and linkify URLs in text + app_links: ^7.2.2 # meshcore:// and offband.app share links from the OS (#1280) gpx: ^2.3.0 yaml: ^3.1.2 # Parse importable device config profiles (#403) path_provider: ^2.1.6 diff --git a/test/platform/share_link_registration_test.dart b/test/platform/share_link_registration_test.dart new file mode 100644 index 00000000..85a73836 --- /dev/null +++ b/test/platform/share_link_registration_test.dart @@ -0,0 +1,59 @@ +import 'dart:io'; + +import 'package:flutter_test/flutter_test.dart'; + +// #1291: the OS only hands a meshcore:// link to Offband if the app declares +// the scheme. Before this, none of the platforms did, so a tapped link went +// nowhere. Flutter's own deep-link routing must also be off where app_links +// needs it, or Flutter treats "channel/add" as a screen to navigate to. + +String _read(String path) => File(path).readAsStringSync(); + +/// The `` blocks in the Android manifest. +Iterable _intentFilters(String manifest) => RegExp( + r'([\s\S]*?)', +).allMatches(manifest).map((m) => m[1]!); + +/// True if a `CFBundleURLSchemes` array in [plist] lists [scheme]. +bool _plistDeclaresScheme(String plist, String scheme) => RegExp( + r'CFBundleURLSchemes\s*[\s\S]*?' + + RegExp.escape(scheme) + + r'[\s\S]*?', +).hasMatch(plist); + +void main() { + test('Android opens meshcore:// links in Offband', () { + final manifest = _read('android/app/src/main/AndroidManifest.xml'); + final filter = _intentFilters( + manifest, + ).where((f) => f.contains('android:scheme="meshcore"')); + expect(filter, hasLength(1), reason: 'one meshcore intent filter'); + expect(filter.single, contains('android.intent.action.VIEW')); + expect(filter.single, contains('android.intent.category.BROWSABLE')); + expect(filter.single, contains('android.intent.category.DEFAULT')); + expect( + manifest, + matches( + RegExp( + r'android:name="flutter_deeplinking_enabled"\s+android:value="false"', + ), + ), + ); + }); + + test('iOS opens meshcore:// links in Offband', () { + final plist = _read('ios/Runner/Info.plist'); + expect(_plistDeclaresScheme(plist, 'meshcore'), isTrue); + expect( + plist, + matches(RegExp(r'FlutterDeepLinkingEnabled\s*')), + ); + }); + + test('macOS opens meshcore:// links in Offband', () { + expect( + _plistDeclaresScheme(_read('macos/Runner/Info.plist'), 'meshcore'), + isTrue, + ); + }); +} diff --git a/test/services/link_router_test.dart b/test/services/link_router_test.dart index 675272cd..1f9edf80 100644 --- a/test/services/link_router_test.dart +++ b/test/services/link_router_test.dart @@ -101,6 +101,21 @@ void main() { expect(router.pendingAnnounced, isFalse); }); + test('from the OS, only share links are acted on (#1291)', () { + final router = LinkRouter(); + // An ordinary launch, and on web the page's own address. + router.handleFromOs(Uri.parse('https://offband.app/')); + router.handleFromOs(Uri.parse('offband://open')); + expect(router.pending, isNull); + expect(router.takeRejected(), isNull, reason: 'not reported either'); + + router.handleFromOs(Uri.parse(_hashtagLink)); + expect(router.pending, isA()); + + router.handleFromOs(Uri.parse('MESHCORE://channel/add?name=nope')); + expect(router.takeRejected(), isNotNull, reason: 'a broken share link'); + }); + test('a malformed link does not replace a held one', () { final router = LinkRouter()..handle(_channelLink); router.handle('meshcore://nope'); diff --git a/test/widgets/link_router_host_test.dart b/test/widgets/link_router_host_test.dart index 1ccc4cc1..87979712 100644 --- a/test/widgets/link_router_host_test.dart +++ b/test/widgets/link_router_host_test.dart @@ -1,6 +1,7 @@ // #1289: a share link opens the existing confirmation, waits for a radio // when none is connected, and never adds anything the user did not confirm. +import 'dart:async'; import 'dart:typed_data'; import 'package:flutter/material.dart'; @@ -149,6 +150,35 @@ void main() { expect(find.textContaining("isn't a channel or contact"), findsOne); }); + testWidgets('a link from the OS opens the confirmation (#1291)', ( + tester, + ) async { + final radio = _Radio()..connected = true; + final os = StreamController(); + addTearDown(os.close); + await tester.pumpWidget( + MultiProvider( + providers: [ + ChangeNotifierProvider.value(value: radio), + ChangeNotifierProvider(create: (_) => LinkRouter()), + ], + child: MaterialApp( + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: LinkRouterHost(osLinks: os.stream, child: const Scaffold()), + ), + ), + ); + os.add(Uri.parse('https://offband.app/')); + await tester.pumpAndSettle(); + expect(find.byType(AlertDialog), findsNothing); + expect(find.textContaining("isn't a channel"), findsNothing); + + os.add(Uri.parse(_link)); + await tester.pumpAndSettle(); + expect(find.text('Add channel "#example"?'), findsOne); + }); + testWidgets('tapping a meshcore:// link in a message routes it here', ( tester, ) async { diff --git a/windows/flutter/generated_plugin_registrant.cc b/windows/flutter/generated_plugin_registrant.cc index 58ed7ef3..7382e393 100644 --- a/windows/flutter/generated_plugin_registrant.cc +++ b/windows/flutter/generated_plugin_registrant.cc @@ -6,6 +6,7 @@ #include "generated_plugin_registrant.h" +#include #include #include #include @@ -14,6 +15,8 @@ #include void RegisterPlugins(flutter::PluginRegistry* registry) { + AppLinksPluginCApiRegisterWithRegistrar( + registry->GetRegistrarForPlugin("AppLinksPluginCApi")); FileSelectorWindowsRegisterWithRegistrar( registry->GetRegistrarForPlugin("FileSelectorWindows")); FlutterBluePlusPluginRegisterWithRegistrar( diff --git a/windows/flutter/generated_plugins.cmake b/windows/flutter/generated_plugins.cmake index e615315f..1fec65fc 100644 --- a/windows/flutter/generated_plugins.cmake +++ b/windows/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + app_links file_selector_windows flutter_blue_plus_winrt screen_retriever_windows