Skip to content

Commit

Permalink
Merge pull request wewewe718#87 from lucasnz/covid_tracer
Browse files Browse the repository at this point in the history
Add support for New Zealand Covid Tracer QR codes
  • Loading branch information
wewewe718 authored Oct 7, 2021
2 parents 207b54a + 189a4c7 commit 8c81ac6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class BarcodeActivity : BaseActivity(), DeleteConfirmationDialogFragment.Listene
BarcodeSchema.VCARD -> addToContacts()
BarcodeSchema.WIFI -> connectToWifi()
BarcodeSchema.YOUTUBE -> openInYoutube()
BarcodeSchema.NZCOVIDTRACER -> openLink()
else -> return
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class ParsedBarcode(barcode: Barcode) {
BarcodeSchema.YOUTUBE -> parseYoutube()
BarcodeSchema.CRYPTOCURRENCY -> parseBitcoin()
BarcodeSchema.OTP_AUTH -> parseOtp()
BarcodeSchema.NZCOVIDTRACER -> parseNZCovidTracer()
BarcodeSchema.URL -> parseUrl()
}
}
Expand Down Expand Up @@ -204,4 +205,11 @@ class ParsedBarcode(barcode: Barcode) {
private fun parseUrl() {
url = text
}

private fun parseNZCovidTracer() {
val objNZCovidTracer = NZCovidTracer.parse(text) ?: return
//title = objNZCovidTracer.title
address = objNZCovidTracer.addr
url = "http://maps.google.com/maps?q=" + (objNZCovidTracer.addr)?.replace("\n", ", ")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.example.barcodescanner.model.schema


import com.example.barcodescanner.extension.joinToStringNotNullOrBlankWithLineSeparator
import com.example.barcodescanner.extension.removePrefixIgnoreCase
import com.example.barcodescanner.extension.startsWithIgnoreCase
import org.apache.commons.codec.binary.Base64
import org.json.JSONException
import org.json.JSONObject
import java.lang.Exception

class NZCovidTracer(
val title: String? = null,
val addr: String? = null,
private val decodedBytes: String? = null
) : Schema {
//{"gln":"9429300660232","opn":"Queens Park ","adr":"150 Gala Street \nInvercargill ","ver":"c19:1","typ":"entry"}
//{"gln":"7000000754500","ver":"c19:1","typ":"entry","opn":"Treetown Gym","adr":"1 Shakespeare Road\nBastia Hill\nWhanganui"}

companion object {
private const val PREFIX = "NZCOVIDTRACER:"

fun parse(text: String): NZCovidTracer? {
if (text.startsWithIgnoreCase(PREFIX).not()) {
return null
}

var title: String? = null
var addr: String? = null
var decodedBytes: String? = null

try {
decodedBytes = String(Base64().decode(text.removePrefixIgnoreCase(PREFIX)))
}
catch (e: Exception) {
return null
}

try {
val obj = JSONObject(decodedBytes)
title = obj.getString("opn")
addr = obj.getString("adr")
}
catch (e: JSONException) {
return null
}

addr = addr.replace("\\n", "\n")
return NZCovidTracer(title.trim(), addr.trim())
}
}

override val schema = BarcodeSchema.NZCOVIDTRACER
override fun toFormattedText(): String = listOf(title, addr).joinToStringNotNullOrBlankWithLineSeparator()
override fun toBarcodeText(): String = "$PREFIX$decodedBytes"
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum class BarcodeSchema {
VCARD,
WIFI,
YOUTUBE,
NZCOVIDTRACER,
OTHER;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ object BarcodeParser {
?: MeCard.parse(text)
?: VCard.parse(text)
?: OtpAuth.parse(text)
?: NZCovidTracer.parse(text)
?: Other(text)
}
}

0 comments on commit 8c81ac6

Please sign in to comment.