feat: Support the `add` export method on channel url/qr (#2934)

Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
Co-authored-by: Benjamin Faershtein <119711889+RCGV1@users.noreply.github.com>
pull/2946/head
James Rich 2025-09-02 14:12:32 -05:00 zatwierdzone przez GitHub
rodzic 251aa6cabd
commit 76ddd29114
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
2 zmienionych plików z 57 dodań i 38 usunięć

Wyświetl plik

@ -30,72 +30,61 @@ import kotlin.jvm.Throws
private const val MESHTASTIC_HOST = "meshtastic.org" private const val MESHTASTIC_HOST = "meshtastic.org"
private const val CHANNEL_PATH = "/e/" private const val CHANNEL_PATH = "/e/"
internal const val URL_PREFIX = "https://$MESHTASTIC_HOST$CHANNEL_PATH#" internal const val URL_PREFIX = "https://$MESHTASTIC_HOST$CHANNEL_PATH"
private const val BASE64FLAGS = Base64.URL_SAFE + Base64.NO_WRAP + Base64.NO_PADDING private const val BASE64FLAGS = Base64.URL_SAFE + Base64.NO_WRAP + Base64.NO_PADDING
/** /**
* Return a [ChannelSet] that represents the ChannelSet encoded by the URL. * Return a [ChannelSet] that represents the ChannelSet encoded by the URL.
*
* @throws MalformedURLException when not recognized as a valid Meshtastic URL * @throws MalformedURLException when not recognized as a valid Meshtastic URL
*/ */
@Throws(MalformedURLException::class) @Throws(MalformedURLException::class)
fun Uri.toChannelSet(): ChannelSet { fun Uri.toChannelSet(): ChannelSet {
if (fragment.isNullOrBlank() || if (fragment.isNullOrBlank() || !host.equals(MESHTASTIC_HOST, true) || !path.equals(CHANNEL_PATH, true)) {
!host.equals(MESHTASTIC_HOST, true) ||
!path.equals(CHANNEL_PATH, true)
) {
throw MalformedURLException("Not a valid Meshtastic URL: ${toString().take(40)}") throw MalformedURLException("Not a valid Meshtastic URL: ${toString().take(40)}")
} }
// Older versions of Meshtastic clients (Apple/web) included `?add=true` within the URL fragment. // Older versions of Meshtastic clients (Apple/web) included `?add=true` within the URL fragment.
// This gracefully handles those cases until the newer version are generally available/used. // This gracefully handles those cases until the newer version are generally available/used.
val url = ChannelSet.parseFrom(Base64.decode(fragment!!.substringBefore('?'), BASE64FLAGS)) val url = ChannelSet.parseFrom(Base64.decode(fragment!!.substringBefore('?'), BASE64FLAGS))
val shouldAdd = fragment?.substringAfter('?', "") val shouldAdd =
?.takeUnless { it.isBlank() } fragment?.substringAfter('?', "")?.takeUnless { it.isBlank() }?.equals("add=true")
?.equals("add=true") ?: getBooleanQueryParameter("add", false)
?: getBooleanQueryParameter("add", false)
return url.toBuilder().apply { if (shouldAdd) clearLoraConfig() }.build() return url.toBuilder().apply { if (shouldAdd) clearLoraConfig() }.build()
} }
/** /** @return A list of globally unique channel IDs usable with MQTT subscribe() */
* @return A list of globally unique channel IDs usable with MQTT subscribe()
*/
val ChannelSet.subscribeList: List<String> val ChannelSet.subscribeList: List<String>
get() = settingsList.filter { it.downlinkEnabled }.map { Channel(it, loraConfig).name } get() = settingsList.filter { it.downlinkEnabled }.map { Channel(it, loraConfig).name }
fun ChannelSet.getChannel(index: Int): Channel? = fun ChannelSet.getChannel(index: Int): Channel? =
if (settingsCount > index) Channel(getSettings(index), loraConfig) else null if (settingsCount > index) Channel(getSettings(index), loraConfig) else null
/** /** Return the primary channel info */
* Return the primary channel info val ChannelSet.primaryChannel: Channel?
*/ get() = getChannel(0)
val ChannelSet.primaryChannel: Channel? get() = getChannel(0)
/** /**
* Return a URL that represents the [ChannelSet] * Return a URL that represents the [ChannelSet]
*
* @param upperCasePrefix portions of the URL can be upper case to make for more efficient QR codes * @param upperCasePrefix portions of the URL can be upper case to make for more efficient QR codes
*/ */
fun ChannelSet.getChannelUrl(upperCasePrefix: Boolean = false): Uri { fun ChannelSet.getChannelUrl(upperCasePrefix: Boolean = false, shouldAdd: Boolean = false): Uri {
val channelBytes = this.toByteArray() ?: ByteArray(0) // if unset just use empty val channelBytes = this.toByteArray() ?: ByteArray(0) // if unset just use empty
val enc = Base64.encodeToString(channelBytes, BASE64FLAGS) val enc = Base64.encodeToString(channelBytes, BASE64FLAGS)
val p = if (upperCasePrefix) URL_PREFIX.uppercase() else URL_PREFIX val p = if (upperCasePrefix) URL_PREFIX.uppercase() else URL_PREFIX
return Uri.parse("$p$enc") val query = if (shouldAdd) "?add=true" else ""
return Uri.parse("$p$query#$enc")
} }
val ChannelSet.qrCode: Bitmap? fun ChannelSet.qrCode(shouldAdd: Boolean): Bitmap? = try {
get() = try { val multiFormatWriter = MultiFormatWriter()
val multiFormatWriter = MultiFormatWriter() val bitMatrix =
multiFormatWriter.encode(getChannelUrl(false, shouldAdd).toString(), BarcodeFormat.QR_CODE, 960, 960)
val bitMatrix = val barcodeEncoder = BarcodeEncoder()
multiFormatWriter.encode( barcodeEncoder.createBitmap(bitMatrix)
getChannelUrl(false).toString(), } catch (ex: Throwable) {
BarcodeFormat.QR_CODE, errormsg("URL was too complex to render as barcode")
960, null
960 }
)
val barcodeEncoder = BarcodeEncoder()
barcodeEncoder.createBitmap(bitMatrix)
} catch (ex: Throwable) {
errormsg("URL was too complex to render as barcode")
null
}

Wyświetl plik

@ -50,6 +50,9 @@ import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.SegmentedButton
import androidx.compose.material3.SegmentedButtonDefaults
import androidx.compose.material3.SingleChoiceSegmentedButtonRow
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.material3.TextButton import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
@ -139,6 +142,8 @@ fun ChannelScreen(
var showResetDialog by remember { mutableStateOf(false) } var showResetDialog by remember { mutableStateOf(false) }
var shouldAddChannelsState by remember { mutableStateOf(true) }
/* Animate waiting for the configurations */ /* Animate waiting for the configurations */
var isWaiting by remember { mutableStateOf(false) } var isWaiting by remember { mutableStateOf(false) }
if (isWaiting) { if (isWaiting) {
@ -269,6 +274,7 @@ fun ChannelScreen(
channelSet = channelSet, channelSet = channelSet,
modemPresetName = modemPresetName, modemPresetName = modemPresetName,
channelSelections = channelSelections, channelSelections = channelSelections,
shouldAddChannel = shouldAddChannelsState,
onClick = { onClick = {
isWaiting = true isWaiting = true
radioConfigViewModel.setResponseStateLoading(ConfigRoute.CHANNELS) radioConfigViewModel.setResponseStateLoading(ConfigRoute.CHANNELS)
@ -276,10 +282,26 @@ fun ChannelScreen(
) )
EditChannelUrl( EditChannelUrl(
enabled = enabled, enabled = enabled,
channelUrl = selectedChannelSet.getChannelUrl(), channelUrl = selectedChannelSet.getChannelUrl(shouldAdd = shouldAddChannelsState),
onConfirm = viewModel::requestChannelUrl, onConfirm = viewModel::requestChannelUrl,
) )
} }
item {
SingleChoiceSegmentedButtonRow(modifier = Modifier.fillMaxWidth().padding(8.dp)) {
SegmentedButton(
label = { Text(text = stringResource(R.string.replace)) },
onClick = { shouldAddChannelsState = false },
selected = !shouldAddChannelsState,
shape = SegmentedButtonDefaults.itemShape(0, 2),
)
SegmentedButton(
label = { Text(text = stringResource(R.string.add)) },
onClick = { shouldAddChannelsState = true },
selected = shouldAddChannelsState,
shape = SegmentedButtonDefaults.itemShape(1, 2),
)
}
}
item { item {
ModemPresetInfo( ModemPresetInfo(
modemPresetName = modemPresetName, modemPresetName = modemPresetName,
@ -401,9 +423,15 @@ private fun EditChannelUrl(enabled: Boolean, channelUrl: Uri, modifier: Modifier
} }
@Composable @Composable
private fun QrCodeImage(enabled: Boolean, channelSet: ChannelSet, modifier: Modifier = Modifier) = Image( private fun QrCodeImage(
enabled: Boolean,
channelSet: ChannelSet,
modifier: Modifier = Modifier,
shouldAddChannel: Boolean = false,
) = Image(
painter = painter =
channelSet.qrCode?.let { BitmapPainter(it.asImageBitmap()) } ?: painterResource(id = R.drawable.qrcode), channelSet.qrCode(shouldAddChannel)?.let { BitmapPainter(it.asImageBitmap()) }
?: painterResource(id = R.drawable.qrcode),
contentDescription = stringResource(R.string.qr_code), contentDescription = stringResource(R.string.qr_code),
modifier = modifier, modifier = modifier,
contentScale = ContentScale.Inside, contentScale = ContentScale.Inside,
@ -417,6 +445,7 @@ private fun ChannelListView(
channelSet: ChannelSet, channelSet: ChannelSet,
modemPresetName: String, modemPresetName: String,
channelSelections: SnapshotStateList<Boolean>, channelSelections: SnapshotStateList<Boolean>,
shouldAddChannel: Boolean = false,
onClick: () -> Unit = {}, onClick: () -> Unit = {},
) { ) {
val selectedChannelSet = val selectedChannelSet =
@ -459,6 +488,7 @@ private fun ChannelListView(
enabled = enabled, enabled = enabled,
channelSet = selectedChannelSet, channelSet = selectedChannelSet,
modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp), modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp),
shouldAddChannel = shouldAddChannel,
) )
}, },
) )