Add opt-in network map uploads and online map view

- Opt-in upload of header-only packet records and repeater/room server
  adverts to map.meshcoreopen.org about once a minute; payloads, channel
  and DM hashes and transport codes are stripped on the phone, locations
  snapped to a ~2 km grid, install ID rotates daily
- One-time opt-in dialog on first radio connection; toggle and status in
  App settings
- Local / Online / Both switch on the map screen with online repeater
  hexagons, links, add-to-contacts and manage
- Node purchase banner on the connect screen (affiliate link, disclosed)
- Privacy policy and network map documentation
- Translations for all locales
This commit is contained in:
zjs81 committed 2026-10-11 21:54:16 -07:00
1 parent d30744aa31
commit bc14de586f
68 files changed
+6719 -76

No files matched your search

+98
View File
@@ -0,0 +1,98 @@
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import '../l10n/l10n.dart';
import '../theme/mesh_theme.dart';
/// Affiliate link for the SenseCAP MeshTracker X1.
const getNodeUrl =
'https://www.seeedstudio.com/sensecap-meshtracker-x1-meshtastic-gps-tracker-p-6935.html'
'?sensecap_affiliate=JFWID0g&referring_service=link';
/// Bottom banner on the connect screen pointing new users at a node to buy.
class GetNodeBanner extends StatelessWidget {
const GetNodeBanner({super.key});
void _open() =>
launchUrl(Uri.parse(getNodeUrl), mode: LaunchMode.externalApplication);
@override
Widget build(BuildContext context) {
final l10n = context.l10n;
final scheme = Theme.of(context).colorScheme;
final textTheme = Theme.of(context).textTheme;
return SafeArea(
top: false,
child: Padding(
padding: const EdgeInsets.fromLTRB(12, 4, 12, 12),
child: Material(
color: scheme.primaryContainer,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(MeshRadii.md),
side: BorderSide(color: scheme.primary.withValues(alpha: 0.4)),
),
clipBehavior: Clip.antiAlias,
child: InkWell(
onTap: _open,
child: Padding(
padding: const EdgeInsets.fromLTRB(14, 12, 14, 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
Icon(
Icons.router_outlined,
size: 28,
color: scheme.onPrimaryContainer,
),
const SizedBox(width: 10),
Expanded(
child: Text(
l10n.scanner_getNodeTitle,
style: textTheme.titleMedium?.copyWith(
color: scheme.onPrimaryContainer,
fontWeight: FontWeight.w700,
),
),
),
],
),
const SizedBox(height: 6),
Text(
l10n.scanner_getNodeBody,
style: textTheme.bodyMedium?.copyWith(
color: scheme.onPrimaryContainer,
height: 1.35,
),
),
const SizedBox(height: 10),
SizedBox(
width: double.infinity,
child: FilledButton.icon(
onPressed: _open,
icon: const Icon(Icons.shopping_cart_outlined, size: 22),
label: Text(l10n.scanner_getNodeAction),
),
),
const SizedBox(height: 6),
Center(
child: Text(
l10n.scanner_getNodeDisclosure,
textAlign: TextAlign.center,
style: textTheme.bodySmall?.copyWith(
color: scheme.onPrimaryContainer.withValues(alpha: 0.8),
),
),
),
],
),
),
),
),
),
);
}
}
+493
View File
@@ -0,0 +1,493 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
import '../connector/meshcore_connector.dart';
import '../connector/meshcore_protocol.dart';
import '../helpers/snack_bar_builder.dart';
import '../l10n/l10n.dart';
import '../models/contact.dart';
import '../services/network_map/network_map_api_client.dart';
import '../theme/mesh_theme.dart';
import 'mesh_ui.dart';
enum NetworkMapLoadState { idle, loading, ready, zoomIn, error }
/// Fetches the online network map for the visible area.
///
/// Requests are debounced while the map moves; a response that arrives after
/// a newer request started is dropped.
class NetworkMapOnlineController extends ChangeNotifier {
static const Duration _debounce = Duration(milliseconds: 600);
final NetworkMapApiClient _client;
final LayerHitNotifier<NetworkMapRepeater> hitNotifier = ValueNotifier(null);
List<NetworkMapRepeater> _repeaters = const [];
List<NetworkMapLink> _links = const [];
NetworkMapLoadState _state = NetworkMapLoadState.idle;
Timer? _timer;
int _generation = 0;
bool _disposed = false;
NetworkMapOnlineController({NetworkMapApiClient? client})
: _client = client ?? NetworkMapApiClient();
List<NetworkMapRepeater> get repeaters => _repeaters;
List<NetworkMapLink> get links => _links;
NetworkMapLoadState get state => _state;
void requestBounds(LatLngBounds bounds) {
_timer?.cancel();
_timer = Timer(_debounce, () => _load(bounds));
}
/// Drops any pending or in-flight request (the online layer was hidden).
void cancel() {
_timer?.cancel();
_generation++;
}
Future<void> _load(LatLngBounds bounds) async {
final bbox = NetworkMapBbox(
minLon: bounds.west.clamp(-180.0, 180.0),
minLat: bounds.south.clamp(-90.0, 90.0),
maxLon: bounds.east.clamp(-180.0, 180.0),
maxLat: bounds.north.clamp(-90.0, 90.0),
);
final generation = ++_generation;
if (!bbox.isWithinLimit) {
_setState(NetworkMapLoadState.zoomIn);
return;
}
_setState(NetworkMapLoadState.loading);
try {
final results = await Future.wait([
_client.fetchRepeaters(bbox),
_client.fetchLinks(bbox),
]);
if (_disposed || generation != _generation) return;
_repeaters = results[0] as List<NetworkMapRepeater>;
_links = results[1] as List<NetworkMapLink>;
_setState(NetworkMapLoadState.ready);
} catch (_) {
if (_disposed || generation != _generation) return;
_setState(NetworkMapLoadState.error);
}
}
void _setState(NetworkMapLoadState value) {
_state = value;
if (!_disposed) notifyListeners();
}
@override
void dispose() {
_disposed = true;
_timer?.cancel();
hitNotifier.dispose();
_client.close();
super.dispose();
}
}
Color networkMapStatusColor(NetworkMapRepeaterStatus status) {
return switch (status) {
NetworkMapRepeaterStatus.active => MapPalette.online,
NetworkMapRepeaterStatus.recent => MapPalette.stale,
NetworkMapRepeaterStatus.stale => MapPalette.offline,
};
}
/// Map layers for the online network: links, repeater hexagons and a
/// tappable center marker so small cells stay visible when zoomed out.
///
/// Keys in [localMarkerKeys] are already drawn by the local layer, so only
/// their hexagon outline is added.
List<Widget> buildNetworkMapOnlineLayers({
required NetworkMapOnlineController controller,
required Set<String> localMarkerKeys,
required ValueChanged<NetworkMapRepeater> onTap,
}) {
final repeaters = controller.repeaters;
return [
PolylineLayer(
polylines: [
for (final link in controller.links)
Polyline(
points: [link.aCell.center, link.bCell.center],
strokeWidth: link.confirmed ? 3.5 : 2.5,
color: MapPalette.router.withValues(
alpha: link.confirmed ? 0.85 : 0.6,
),
borderStrokeWidth: 1,
borderColor: MapPalette.markerOutline.withValues(alpha: 0.7),
pattern: link.confirmed
? const StrokePattern.solid()
: StrokePattern.dashed(segments: const [10, 8]),
),
],
),
GestureDetector(
onTap: () {
final hits = controller.hitNotifier.value?.hitValues;
if (hits != null && hits.isNotEmpty) onTap(hits.first);
},
child: PolygonLayer<NetworkMapRepeater>(
hitNotifier: controller.hitNotifier,
polygons: [
for (final repeater in repeaters)
if (repeater.cell.boundary.length >= 3)
Polygon<NetworkMapRepeater>(
points: repeater.cell.boundary,
hitValue: repeater,
color: localMarkerKeys.contains(repeater.key)
? null
: networkMapStatusColor(
repeater.status,
).withValues(alpha: 0.22),
borderColor: networkMapStatusColor(repeater.status),
borderStrokeWidth: 2.5,
pattern: repeater.confirmed
? const StrokePattern.solid()
: StrokePattern.dashed(segments: const [8, 6]),
),
],
),
),
MarkerLayer(
markers: [
for (final repeater in repeaters)
if (!localMarkerKeys.contains(repeater.key))
Marker(
point: repeater.cell.center,
width: 34,
height: 34,
child: GestureDetector(
onTap: () => onTap(repeater),
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
color: networkMapStatusColor(repeater.status),
border: Border.all(
color: MapPalette.markerOutline,
width: 2.5,
),
boxShadow: const [
BoxShadow(
color: MapPalette.markerShadow,
blurRadius: 6,
offset: Offset(0, 2),
),
],
),
child: Icon(
repeater.type == advTypeRoom
? Icons.meeting_room
: Icons.router,
color: MapPalette.markerOutline,
size: 20,
),
),
),
),
],
),
];
}
enum NetworkMapReach { heard, nearby, far }
/// Distance under which a repeater counts as "in your area".
const double networkMapNearbyKm = 50;
NetworkMapReach networkMapReachFor({
required bool heardByRadio,
required LatLng? self,
required LatLng cell,
}) {
if (heardByRadio) return NetworkMapReach.heard;
if (self != null &&
const Distance().as(LengthUnit.Kilometer, self, cell) <=
networkMapNearbyKm) {
return NetworkMapReach.nearby;
}
return NetworkMapReach.far;
}
/// Details for a repeater from the online map, with "Add to contacts" and
/// "Manage". [onManage] runs the regular login → repeater hub flow.
Future<void> showNetworkMapRepeaterSheet(
BuildContext context, {
required MeshCoreConnector connector,
required NetworkMapRepeater repeater,
required void Function(Contact contact) onManage,
}) {
return showMeshSheet<void>(
context,
builder: (sheetContext) => _NetworkMapRepeaterSheet(
connector: connector,
repeater: repeater,
onManage: (contact) {
Navigator.pop(sheetContext);
onManage(contact);
},
),
);
}
class _NetworkMapRepeaterSheet extends StatefulWidget {
final MeshCoreConnector connector;
final NetworkMapRepeater repeater;
final void Function(Contact contact) onManage;
const _NetworkMapRepeaterSheet({
required this.connector,
required this.repeater,
required this.onManage,
});
@override
State<_NetworkMapRepeaterSheet> createState() =>
_NetworkMapRepeaterSheetState();
}
class _NetworkMapRepeaterSheetState extends State<_NetworkMapRepeaterSheet> {
static const Duration _recentlyHeard = Duration(hours: 24);
bool _busy = false;
NetworkMapRepeater get _repeater => widget.repeater;
Contact? get _savedContact =>
widget.connector.getContactByPubKeyHex(_repeater.key);
bool get _heardByRadio {
if (_savedContact != null) return true;
final cutoff = DateTime.now().subtract(_recentlyHeard);
return widget.connector.discoveredContacts.any(
(c) => c.publicKeyHex == _repeater.key && c.lastSeen.isAfter(cutoff),
);
}
/// The saved contact, adding it first if needed. Null (with a message) when
/// it cannot be added.
Future<Contact?> _ensureContact() async {
final existing = _savedContact;
if (existing != null) return existing;
final connector = widget.connector;
final l10n = context.l10n;
if (!connector.isConnected) {
_snack(l10n.settings_notConnectedError);
return null;
}
if (connector.contactsStorageFull ||
connector.contacts.length >= connector.maxContacts) {
_snack(l10n.contacts_storageFull);
return null;
}
// Flood route, no location (the cell is only an approximation) and an
// unknown advert time so the firmware's replay guard accepts the next
// real advert.
final contact = Contact(
publicKey: hexToPubKey(_repeater.key),
name: _repeater.name ?? _repeater.key.substring(0, 8),
type: _repeater.type,
pathLength: -1,
path: Uint8List(0),
lastSeen: DateTime.fromMillisecondsSinceEpoch(0, isUtc: true),
);
setState(() => _busy = true);
final ok = await connector.importDiscoveredContact(contact);
if (!mounted) return null;
setState(() => _busy = false);
if (!ok) {
_snack(l10n.networkMap_addFailed);
return null;
}
return _savedContact ?? contact;
}
void _snack(String message) {
showDismissibleSnackBar(context, content: Text(message));
}
Future<void> _add() async {
final contact = await _ensureContact();
if (contact != null && mounted) {
_snack(context.l10n.networkMap_addedToContacts);
}
}
Future<void> _manage() async {
final contact = await _ensureContact();
if (contact != null && mounted) widget.onManage(contact);
}
@override
Widget build(BuildContext context) {
final l10n = context.l10n;
final scheme = Theme.of(context).colorScheme;
final textTheme = Theme.of(context).textTheme;
final repeater = _repeater;
final inContacts = _savedContact != null;
final statusColor = networkMapStatusColor(repeater.status);
final selfLat = widget.connector.selfLatitude;
final selfLon = widget.connector.selfLongitude;
final reach = networkMapReachFor(
heardByRadio: _heardByRadio,
self: selfLat != null && selfLon != null && (selfLat != 0 || selfLon != 0)
? LatLng(selfLat, selfLon)
: null,
cell: repeater.cell.center,
);
final (reachLabel, reachColor, reachIcon) = switch (reach) {
NetworkMapReach.heard => (
l10n.networkMap_heardByRadio,
MapPalette.online,
Icons.hearing,
),
NetworkMapReach.nearby => (
l10n.networkMap_inYourArea,
MapPalette.selected,
Icons.near_me,
),
NetworkMapReach.far => (
l10n.networkMap_farAway,
MapPalette.stale,
Icons.warning_amber_rounded,
),
};
final statusLabel = switch (repeater.status) {
NetworkMapRepeaterStatus.active => l10n.networkMap_statusActive,
NetworkMapRepeaterStatus.recent => l10n.networkMap_statusRecent,
NetworkMapRepeaterStatus.stale => l10n.networkMap_statusStale,
};
final lastSeen = repeater.lastSeen;
Widget row(String label, String value) => Padding(
padding: const EdgeInsets.symmetric(vertical: 6),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
style: textTheme.bodyMedium?.copyWith(
color: scheme.onSurfaceVariant,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 2),
Text(
value,
style: textTheme.bodyLarge?.copyWith(color: scheme.onSurface),
),
],
),
);
return SafeArea(
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
BottomSheetHeader(
title: repeater.name ?? l10n.networkMap_unnamed,
subtitle: repeater.type == advTypeRoom
? l10n.contact_typeRoom
: l10n.contact_typeRepeater,
trailing: Icon(
repeater.type == advTypeRoom
? Icons.meeting_room
: Icons.router,
color: statusColor,
size: 26,
),
),
Padding(
padding: const EdgeInsets.fromLTRB(16, 4, 16, 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Wrap(
spacing: 8,
runSpacing: 8,
children: [
StatusChip(
label: reachLabel,
color: reachColor,
icon: reachIcon,
fontSize: 15,
),
if (inContacts)
StatusChip(
label: l10n.networkMap_inYourContacts,
color: MapPalette.repeater,
icon: Icons.check_circle,
fontSize: 15,
),
],
),
const SizedBox(height: 8),
row(l10n.networkMap_status, statusLabel),
row(
l10n.networkMap_reports,
repeater.confirmed
? l10n.networkMap_confirmed
: l10n.networkMap_singleSource,
),
if (lastSeen != null)
row(
l10n.map_lastSeen,
networkMapTimeAgo(context, lastSeen),
),
row(
l10n.map_location,
repeater.isEstimated
? l10n.networkMap_locationEstimated
: l10n.networkMap_locationAdvertised,
),
const SizedBox(height: 16),
if (!inContacts) ...[
SizedBox(
width: double.infinity,
child: FilledButton.tonalIcon(
onPressed: _busy ? null : _add,
icon: const Icon(Icons.person_add_alt_1, size: 22),
label: Text(l10n.networkMap_addToContacts),
),
),
const SizedBox(height: 10),
],
SizedBox(
width: double.infinity,
child: FilledButton.icon(
onPressed: _busy ? null : _manage,
icon: const Icon(Icons.admin_panel_settings, size: 22),
label: Text(l10n.networkMap_manage),
),
),
],
),
),
],
),
),
);
}
}
/// "Just now", "5m ago", "3h ago", "2d ago".
String networkMapTimeAgo(BuildContext context, DateTime time) {
final l10n = context.l10n;
final difference = DateTime.now().difference(time);
if (difference.inSeconds < 60) return l10n.time_justNow;
if (difference.inMinutes < 60) {
return l10n.time_minutesAgo(difference.inMinutes);
}
if (difference.inHours < 24) return l10n.time_hoursAgo(difference.inHours);
return l10n.time_daysAgo(difference.inDays);
}
@@ -0,0 +1,95 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';
import '../connector/meshcore_connector.dart';
import '../l10n/l10n.dart';
import '../services/app_settings_service.dart';
import '../services/network_map/network_map_api_client.dart';
/// Asks once whether to share what the radio hears with the network map.
///
/// Shows nothing when uploading is already on, the prompt was shown before,
/// or the app is in review mode. The prompt counts as shown as soon as it
/// opens, so dismissing it never brings it back.
Future<void> maybeShowNetworkMapOptInDialog(BuildContext context) async {
final settingsService = context.read<AppSettingsService>();
final connector = context.read<MeshCoreConnector>();
final settings = settingsService.settings;
if (settings.networkMapPromptShown ||
settings.networkMapUploadEnabled ||
connector.isReviewMode ||
!connector.isConnected) {
return;
}
await settingsService.setNetworkMapPromptShown(true);
if (!context.mounted) return;
final accepted = await showDialog<bool>(
context: context,
builder: (dialogContext) => const _NetworkMapOptInDialog(),
);
if (accepted == true) {
await settingsService.setNetworkMapUploadEnabled(true);
}
}
class _NetworkMapOptInDialog extends StatelessWidget {
const _NetworkMapOptInDialog();
@override
Widget build(BuildContext context) {
final l10n = context.l10n;
final textTheme = Theme.of(context).textTheme;
final scheme = Theme.of(context).colorScheme;
return AlertDialog(
icon: Icon(Icons.public, size: 36, color: scheme.primary),
title: Text(l10n.networkMap_promptTitle),
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
l10n.networkMap_promptBody,
style: textTheme.bodyLarge?.copyWith(height: 1.4),
),
const SizedBox(height: 12),
Text(
l10n.networkMap_promptSettingsHint,
style: textTheme.bodyMedium?.copyWith(
color: scheme.onSurfaceVariant,
height: 1.4,
),
),
const SizedBox(height: 4),
Align(
alignment: AlignmentDirectional.centerStart,
child: TextButton.icon(
style: TextButton.styleFrom(padding: EdgeInsets.zero),
icon: const Icon(Icons.open_in_new, size: 20),
label: Text(l10n.networkMap_whatWeCollect),
onPressed: () => launchUrl(
Uri.parse('$networkMapBaseUrl/privacy'),
mode: LaunchMode.externalApplication,
),
),
),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: Text(l10n.networkMap_promptDecline),
),
FilledButton(
onPressed: () => Navigator.pop(context, true),
child: Text(l10n.networkMap_promptAccept),
),
],
);
}
}
@@ -0,0 +1,75 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';
import '../l10n/l10n.dart';
import '../services/network_map/network_map_api_client.dart';
import '../services/network_map/network_map_upload_service.dart';
import 'mesh_nav_tile.dart';
import 'network_map_online_layer.dart';
/// Opt-in toggle, plain-language explanation and status for network map
/// uploads.
class NetworkMapSettingsSection extends StatelessWidget {
const NetworkMapSettingsSection({super.key});
@override
Widget build(BuildContext context) {
final service = context.watch<NetworkMapUploadService>();
final l10n = context.l10n;
final textTheme = Theme.of(context).textTheme;
final scheme = Theme.of(context).colorScheme;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SwitchListTile(
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 4,
),
secondary: const Icon(Icons.public, size: 24),
title: Text(l10n.networkMap_uploadTitle),
subtitle: Text(_status(context, service)),
value: service.enabled,
onChanged: service.setEnabled,
),
Padding(
padding: const EdgeInsets.fromLTRB(16, 4, 16, 8),
child: Text(
l10n.networkMap_uploadExplanation,
style: textTheme.bodyMedium?.copyWith(
color: scheme.onSurface,
height: 1.4,
),
),
),
const Divider(height: 1, indent: 16),
MeshNavTile(
icon: Icons.privacy_tip_outlined,
title: l10n.networkMap_whatWeCollect,
trailing: const Icon(Icons.open_in_new, size: 22),
showChevron: false,
onTap: () => launchUrl(
Uri.parse('$networkMapBaseUrl/privacy'),
mode: LaunchMode.externalApplication,
),
),
],
);
}
String _status(BuildContext context, NetworkMapUploadService service) {
final l10n = context.l10n;
if (!service.enabled) return l10n.networkMap_statusOff;
final last = service.lastUploadAt;
if (last != null) {
return l10n.networkMap_statusLastSent(
networkMapTimeAgo(context, last),
service.packetsSentToday,
);
}
if (!service.isActive) return l10n.networkMap_statusWaiting;
return l10n.networkMap_statusNothingSent;
}
}