Ensure RiotX is able to scan binary QrCodes

This commit is contained in:
Benoit Marty 2020-02-19 14:26:45 +01:00
parent 66a2958c39
commit 35b10daef1
2 changed files with 49 additions and 5 deletions

View file

@ -37,6 +37,7 @@ import im.vector.riotx.core.utils.toast
import im.vector.riotx.features.debug.sas.DebugSasEmojiActivity
import im.vector.riotx.features.qrcode.QrCodeScannerActivity
import kotlinx.android.synthetic.debug.activity_debug_menu.*
import timber.log.Timber
import javax.inject.Inject
class DebugMenuActivity : VectorBaseActivity() {
@ -50,8 +51,19 @@ class DebugMenuActivity : VectorBaseActivity() {
injector.inject(this)
}
private lateinit var buffer: ByteArray
override fun initUiAndData() {
renderQrCode("https://www.example.org")
// renderQrCode("https://www.example.org")
buffer = ByteArray(256)
for (i in buffer.indices) {
buffer[i] = i.toByte()
}
val string = buffer.toString(Charsets.ISO_8859_1)
renderQrCode(string)
}
private fun renderQrCode(text: String) {
@ -194,7 +206,20 @@ class DebugMenuActivity : VectorBaseActivity() {
toast("QrCode: " + QrCodeScannerActivity.getResultText(data) + " is QRCode: " + QrCodeScannerActivity.getResultIsQrCode(data))
// Also update the current QR Code (reverse operation)
renderQrCode(QrCodeScannerActivity.getResultText(data) ?: "")
// renderQrCode(QrCodeScannerActivity.getResultText(data) ?: "")
val result = QrCodeScannerActivity.getResultText(data)!!
if (result.length != buffer.size) {
Timber.e("Error, length are not the same")
} else {
// Convert to ByteArray
val byteArrayResult = result.toByteArray(Charsets.ISO_8859_1)
for (i in byteArrayResult.indices) {
if (buffer[i] != byteArrayResult[i]) {
Timber.e("Error for byte $i, expecting ${buffer[i]} and get ${byteArrayResult[i]}")
}
}
}
}
}
}

View file

@ -22,6 +22,7 @@ import android.os.Bundle
import androidx.fragment.app.Fragment
import com.google.zxing.BarcodeFormat
import com.google.zxing.Result
import com.google.zxing.ResultMetadataType
import im.vector.riotx.R
import im.vector.riotx.core.di.ScreenComponent
import im.vector.riotx.core.extensions.replaceFragment
@ -43,15 +44,33 @@ class QrCodeScannerActivity : VectorBaseActivity() {
}
fun setResultAndFinish(result: Result?) {
result?.let {
if (result != null) {
val rawBytes = getRawBytes(result)
val rawBytesStr = rawBytes?.toString(Charsets.ISO_8859_1)
setResult(RESULT_OK, Intent().apply {
putExtra(EXTRA_OUT_TEXT, it.text)
putExtra(EXTRA_OUT_IS_QR_CODE, it.barcodeFormat == BarcodeFormat.QR_CODE)
putExtra(EXTRA_OUT_TEXT, rawBytesStr ?: result.text)
putExtra(EXTRA_OUT_IS_QR_CODE, result.barcodeFormat == BarcodeFormat.QR_CODE)
})
}
finish()
}
// Copied from https://github.com/markusfisch/BinaryEye/blob/
// 9d57889b810dcaa1a91d7278fc45c262afba1284/app/src/main/kotlin/de/markusfisch/android/binaryeye/activity/CameraActivity.kt#L434
private fun getRawBytes(result: Result): ByteArray? {
val metadata = result.resultMetadata ?: return null
val segments = metadata[ResultMetadataType.BYTE_SEGMENTS] ?: return null
var bytes = ByteArray(0)
@Suppress("UNCHECKED_CAST")
for (seg in segments as Iterable<ByteArray>) {
bytes += seg
}
// byte segments can never be shorter than the text.
// Zxing cuts off content prefixes like "WIFI:"
return if (bytes.size >= result.text.length) bytes else null
}
companion object {
private const val EXTRA_OUT_TEXT = "EXTRA_OUT_TEXT"
private const val EXTRA_OUT_IS_QR_CODE = "EXTRA_OUT_IS_QR_CODE"