From f6f3cf5c2058271f5e68c0117e1f3f84e11acdf6 Mon Sep 17 00:00:00 2001 From: emp3r0r7 <118127968+emp3r0r7@users.noreply.github.com> Date: Mon, 26 May 2025 14:09:56 +0200 Subject: [PATCH] =?UTF-8?q?fix=20:=20prevent=20crash=20on=20geo:=20intent?= =?UTF-8?q?=20when=20no=20compatible=20map=20app=20is=20inst=E2=80=A6=20(#?= =?UTF-8?q?1936)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: James Rich <2199651+jamesarich@users.noreply.github.com> --- app/src/main/AndroidManifest.xml | 8 ++ .../geeksville/mesh/ui/LinkedCoordinates.kt | 107 ++++++++++++------ 2 files changed, 78 insertions(+), 37 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 49905d152..6baeb822d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -86,6 +86,14 @@ android:name="android.hardware.usb.host" android:required="false" /> + + + + + + + + GPSFormat.toDEC(latitude, longitude) - GpsCoordinateFormat.DMS_VALUE -> GPSFormat.toDMS(latitude, longitude) - GpsCoordinateFormat.UTM_VALUE -> GPSFormat.toUTM(latitude, longitude) - GpsCoordinateFormat.MGRS_VALUE -> GPSFormat.toMGRS(latitude, longitude) - else -> GPSFormat.toDEC(latitude, longitude) - } - append(gpsString) - } - pop() - } - val clipboard: Clipboard = LocalClipboard.current - val coroutineScope = rememberCoroutineScope() + + val annotatedString = rememberAnnotatedString(latitude, longitude, format, nodeName, style) + Text( modifier = modifier.combinedClickable( onClick = { - annotatedString.getStringAnnotations( - tag = "gps", - start = 0, - end = annotatedString.length - ).firstOrNull()?.let { - try { - uriHandler.openUri(it.item) - } catch (ex: ActivityNotFoundException) { - debug("No application found: $ex") - } - } + handleClick(context, annotatedString) }, onLongClick = { coroutineScope.launch { - clipboard.setClipEntry(ClipEntry(ClipData.newPlainText("", annotatedString))) + clipboard.setClipEntry( + ClipEntry( + ClipData.newPlainText("", annotatedString) + ) + ) debug("Copied to clipboard") } } @@ -109,6 +88,60 @@ fun LinkedCoordinates( ) } +@Composable +private fun rememberAnnotatedString( + latitude: Double, + longitude: Double, + format: Int, + nodeName: String, + style: SpanStyle +) = buildAnnotatedString { + pushStringAnnotation( + tag = "gps", + annotation = "geo:0,0?q=$latitude,$longitude&z=17&label=${ + URLEncoder.encode(nodeName, "utf-8") + }" + ) + withStyle(style = style) { + val gpsString = when (format) { + GpsCoordinateFormat.DEC_VALUE -> GPSFormat.toDEC(latitude, longitude) + GpsCoordinateFormat.DMS_VALUE -> GPSFormat.toDMS(latitude, longitude) + GpsCoordinateFormat.UTM_VALUE -> GPSFormat.toUTM(latitude, longitude) + GpsCoordinateFormat.MGRS_VALUE -> GPSFormat.toMGRS(latitude, longitude) + else -> GPSFormat.toDEC(latitude, longitude) + } + append(gpsString) + } + pop() +} + +private fun handleClick(context: android.content.Context, annotatedString: androidx.compose.ui.text.AnnotatedString) { + annotatedString.getStringAnnotations( + tag = "gps", + start = 0, + end = annotatedString.length + ).firstOrNull()?.let { + val uri = it.item.toUri() + val intent = Intent(Intent.ACTION_VIEW, uri).apply { + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + } + + try { + if (intent.resolveActivity(context.packageManager) != null) { + context.startActivity(intent) + } else { + Toast.makeText( + context, + "No application available to open this location!", + Toast.LENGTH_LONG + ).show() + } + } catch (ex: ActivityNotFoundException) { + debug("Failed to open geo intent: $ex") + } + } +} + @PreviewLightDark @Composable fun LinkedCoordinatesPreview(