forked from wewewe718/QrAndBarcodeScanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wewewe718#87 from lucasnz/covid_tracer
Add support for New Zealand Covid Tracer QR codes
- Loading branch information
Showing
5 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
app/src/main/java/com/example/barcodescanner/model/schema/NZCovidTracer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ enum class BarcodeSchema { | |
VCARD, | ||
WIFI, | ||
YOUTUBE, | ||
NZCOVIDTRACER, | ||
OTHER; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters