From 0c70ae860eb7e51cd70831f12e5bbdcdb9008f7a Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Mon, 5 Apr 2021 15:52:26 +0800 Subject: [PATCH] If we are unable to generate a barcode, just show nothing --- .../com/geeksville/mesh/model/ChannelSet.kt | 34 +++++++++++-------- .../com/geeksville/mesh/ui/ChannelFragment.kt | 4 ++- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/model/ChannelSet.kt b/app/src/main/java/com/geeksville/mesh/model/ChannelSet.kt index 31a26aec..3e803bcd 100644 --- a/app/src/main/java/com/geeksville/mesh/model/ChannelSet.kt +++ b/app/src/main/java/com/geeksville/mesh/model/ChannelSet.kt @@ -3,6 +3,7 @@ package com.geeksville.mesh.model import android.graphics.Bitmap import android.net.Uri import android.util.Base64 +import com.geeksville.android.Logging import com.geeksville.mesh.AppOnlyProtos import com.google.zxing.BarcodeFormat import com.google.zxing.MultiFormatWriter @@ -11,7 +12,7 @@ import java.net.MalformedURLException data class ChannelSet( val protobuf: AppOnlyProtos.ChannelSet = AppOnlyProtos.ChannelSet.getDefaultInstance() -) { +) : Logging { companion object { const val prefix = "https://www.meshtastic.org/d/#" @@ -62,18 +63,23 @@ data class ChannelSet( return Uri.parse("$p$enc") } - fun getChannelQR(): Bitmap { - val multiFormatWriter = MultiFormatWriter() + val qrCode + get(): Bitmap? = try { + val multiFormatWriter = MultiFormatWriter() - // We encode as UPPER case for the QR code URL because QR codes are more efficient for that special case - val bitMatrix = - multiFormatWriter.encode( - getChannelUrl(true).toString(), - BarcodeFormat.QR_CODE, - 192, - 192 - ) - val barcodeEncoder = BarcodeEncoder() - return barcodeEncoder.createBitmap(bitMatrix) - } + // We encode as UPPER case for the QR code URL because QR codes are more efficient for that special case + val bitMatrix = + multiFormatWriter.encode( + getChannelUrl(true).toString(), + BarcodeFormat.QR_CODE, + 192, + 192 + ) + val barcodeEncoder = BarcodeEncoder() + barcodeEncoder.createBitmap(bitMatrix) + } catch (ex: Throwable) { + errormsg("URL was too complex to render as barcode") + null + } } + diff --git a/app/src/main/java/com/geeksville/mesh/ui/ChannelFragment.kt b/app/src/main/java/com/geeksville/mesh/ui/ChannelFragment.kt index e006ef03..c3a74531 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/ChannelFragment.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/ChannelFragment.kt @@ -98,7 +98,9 @@ class ChannelFragment : ScreenFragment("Channel"), Logging { // doesn't cache radioconfig writes. binding.editableCheckbox.isEnabled = connected - binding.qrView.setImageBitmap(channels.getChannelQR()) + val bitmap = channels.qrCode + if (bitmap != null) + binding.qrView.setImageBitmap(bitmap) val modemConfig = channel.modemConfig val channelOption = ChannelOption.fromConfig(modemConfig)