|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Meshtastic LLC |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | +@file:Suppress("MagicNumber") |
| 18 | + |
| 19 | +package org.meshtastic.app.map.discovery |
| 20 | + |
| 21 | +import android.graphics.Paint |
| 22 | +import androidx.compose.runtime.Composable |
| 23 | +import androidx.compose.runtime.LaunchedEffect |
| 24 | +import androidx.compose.runtime.getValue |
| 25 | +import androidx.compose.runtime.mutableStateOf |
| 26 | +import androidx.compose.runtime.remember |
| 27 | +import androidx.compose.runtime.setValue |
| 28 | +import androidx.compose.ui.Modifier |
| 29 | +import androidx.compose.ui.graphics.toArgb |
| 30 | +import androidx.compose.ui.platform.LocalContext |
| 31 | +import androidx.compose.ui.platform.LocalDensity |
| 32 | +import androidx.compose.ui.unit.dp |
| 33 | +import androidx.compose.ui.viewinterop.AndroidView |
| 34 | +import org.meshtastic.app.map.addCopyright |
| 35 | +import org.meshtastic.app.map.addScaleBarOverlay |
| 36 | +import org.meshtastic.app.map.model.CustomTileSource |
| 37 | +import org.meshtastic.app.map.rememberMapViewWithLifecycle |
| 38 | +import org.meshtastic.app.map.zoomIn |
| 39 | +import org.meshtastic.core.ui.theme.DiscoveryMapColors |
| 40 | +import org.meshtastic.core.ui.util.DiscoveryMapNode |
| 41 | +import org.meshtastic.core.ui.util.DiscoveryNeighborType |
| 42 | +import org.osmdroid.util.BoundingBox |
| 43 | +import org.osmdroid.util.GeoPoint |
| 44 | +import org.osmdroid.views.overlay.Marker |
| 45 | +import org.osmdroid.views.overlay.Polyline |
| 46 | + |
| 47 | +private const val SINGLE_POINT_ZOOM = 14.0 |
| 48 | +private const val ZOOM_OUT_LEVELS = 0.5 |
| 49 | + |
| 50 | +/** |
| 51 | + * OSMDroid implementation of the discovery map. Renders discovered node markers color-coded by neighbor type (green = |
| 52 | + * direct, blue = mesh) with polylines from the user position to direct neighbors. Auto-zooms to fit all markers. |
| 53 | + */ |
| 54 | +@Composable |
| 55 | +fun DiscoveryOsmMap( |
| 56 | + userLatitude: Double, |
| 57 | + userLongitude: Double, |
| 58 | + nodes: List<DiscoveryMapNode>, |
| 59 | + modifier: Modifier = Modifier, |
| 60 | +) { |
| 61 | + val context = LocalContext.current |
| 62 | + val density = LocalDensity.current |
| 63 | + val hasValidUserPosition = userLatitude != 0.0 || userLongitude != 0.0 |
| 64 | + val userGeoPoint = remember(userLatitude, userLongitude) { GeoPoint(userLatitude, userLongitude) } |
| 65 | + val validNodes = remember(nodes) { nodes.filter { it.latitude != 0.0 || it.longitude != 0.0 } } |
| 66 | + |
| 67 | + // Build bounding box from all points |
| 68 | + val allGeoPoints = |
| 69 | + remember(validNodes, hasValidUserPosition) { |
| 70 | + buildList { |
| 71 | + if (hasValidUserPosition) add(userGeoPoint) |
| 72 | + validNodes.forEach { add(GeoPoint(it.latitude, it.longitude)) } |
| 73 | + } |
| 74 | + } |
| 75 | + val initialBounds = |
| 76 | + remember(allGeoPoints) { |
| 77 | + if (allGeoPoints.isEmpty()) BoundingBox() else BoundingBox.fromGeoPoints(allGeoPoints) |
| 78 | + } |
| 79 | + |
| 80 | + var hasCentered by remember { mutableStateOf(false) } |
| 81 | + |
| 82 | + val mapView = |
| 83 | + rememberMapViewWithLifecycle( |
| 84 | + applicationId = context.packageName, |
| 85 | + box = initialBounds, |
| 86 | + tileSource = CustomTileSource.getTileSource(0), |
| 87 | + ) |
| 88 | + |
| 89 | + // Camera auto-center once |
| 90 | + LaunchedEffect(allGeoPoints) { |
| 91 | + if (hasCentered || allGeoPoints.isEmpty()) return@LaunchedEffect |
| 92 | + if (allGeoPoints.size == 1) { |
| 93 | + mapView.controller.setCenter(allGeoPoints.first()) |
| 94 | + mapView.controller.setZoom(SINGLE_POINT_ZOOM) |
| 95 | + } else { |
| 96 | + mapView.zoomToBoundingBox(BoundingBox.fromGeoPoints(allGeoPoints).zoomIn(-ZOOM_OUT_LEVELS), true) |
| 97 | + } |
| 98 | + hasCentered = true |
| 99 | + } |
| 100 | + |
| 101 | + AndroidView( |
| 102 | + modifier = modifier, |
| 103 | + factory = { mapView.apply { setDestroyMode(false) } }, |
| 104 | + update = { map -> |
| 105 | + map.overlays.clear() |
| 106 | + map.addCopyright() |
| 107 | + map.addScaleBarOverlay(density) |
| 108 | + |
| 109 | + // User position marker |
| 110 | + if (hasValidUserPosition) { |
| 111 | + val userMarker = |
| 112 | + Marker(map).apply { |
| 113 | + position = userGeoPoint |
| 114 | + setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM) |
| 115 | + title = "Your Position" |
| 116 | + icon = context.getDrawable(android.R.drawable.ic_menu_mylocation) |
| 117 | + } |
| 118 | + map.overlays.add(userMarker) |
| 119 | + } |
| 120 | + |
| 121 | + // Node markers |
| 122 | + validNodes.forEach { node -> |
| 123 | + val nodeGeoPoint = GeoPoint(node.latitude, node.longitude) |
| 124 | + val marker = |
| 125 | + Marker(map).apply { |
| 126 | + position = nodeGeoPoint |
| 127 | + setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM) |
| 128 | + title = node.longName ?: node.shortName ?: "Unknown" |
| 129 | + snippet = "SNR: ${node.snr} dB / RSSI: ${node.rssi} dBm" |
| 130 | + |
| 131 | + val drawableId = |
| 132 | + if (node.isSensorNode) { |
| 133 | + org.meshtastic.app.R.drawable.ic_thermostat |
| 134 | + } else { |
| 135 | + org.meshtastic.app.R.drawable.ic_person |
| 136 | + } |
| 137 | + icon = context.getDrawable(drawableId) |
| 138 | + |
| 139 | + // Default OSM marker handles color tinting via icon overlay or custom drawables if needed, |
| 140 | + // but setting the icon directly overrides the default teardrop pin. |
| 141 | + } |
| 142 | + map.overlays.add(marker) |
| 143 | + } |
| 144 | + |
| 145 | + // Polylines from user to direct neighbors |
| 146 | + if (hasValidUserPosition) { |
| 147 | + validNodes |
| 148 | + .filter { it.neighborType == DiscoveryNeighborType.DIRECT } |
| 149 | + .forEach { node -> |
| 150 | + val polyline = |
| 151 | + Polyline().apply { |
| 152 | + setPoints(listOf(userGeoPoint, GeoPoint(node.latitude, node.longitude))) |
| 153 | + outlinePaint.apply { |
| 154 | + color = DiscoveryMapColors.DirectLine.toArgb() |
| 155 | + strokeWidth = with(density) { 3.dp.toPx() } |
| 156 | + strokeCap = Paint.Cap.ROUND |
| 157 | + style = Paint.Style.STROKE |
| 158 | + } |
| 159 | + } |
| 160 | + map.overlays.add(polyline) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + map.invalidate() |
| 165 | + }, |
| 166 | + ) |
| 167 | +} |
0 commit comments