From 35c072abf63b308c41bccc80f7344376d327d568 Mon Sep 17 00:00:00 2001 From: raphael Date: Tue, 4 Jan 2022 10:21:30 +0100 Subject: [PATCH] checks if secret from qr code is valid - secret size must be greater than 32 chars an only contain letters and '-' - context check in camera fragment now fixed --- .../java/com/maenle/bump/ui/CameraFragment.kt | 25 ++++++++--- .../com/maenle/bump/util/BumpProcessor.kt | 44 ++++++++++--------- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/maenle/bump/ui/CameraFragment.kt b/app/src/main/java/com/maenle/bump/ui/CameraFragment.kt index 9275f1f..a1b0dd3 100644 --- a/app/src/main/java/com/maenle/bump/ui/CameraFragment.kt +++ b/app/src/main/java/com/maenle/bump/ui/CameraFragment.kt @@ -49,6 +49,8 @@ class CameraFragment: Fragment() { private var _binding: FragmentCameraBinding? = null private val binding get() = _binding!! + private var secretScanned = false + private val screenAspectRatio: Int get() { val activity = requireActivity() @@ -179,15 +181,26 @@ class CameraFragment: Fragment() { val inputImage = InputImage.fromMediaImage(imageProxy.image!!, imageProxy.imageInfo.rotationDegrees) + var blockingScanned = false + barcodeScanner.process(inputImage) .addOnSuccessListener { barcodes -> - barcodes.forEach { - Log.d(TAG, it.rawValue!!) - val bump = BumpProcessor.getInstance(requireContext()) - bump.addSecret(requireContext(), it.rawValue!!) - findNavController().navigateUp() - + if(blockingScanned) { + return@addOnSuccessListener } + blockingScanned = true + barcodes.forEach { barcode -> + if(!secretScanned) { + context?.let { + val bump = BumpProcessor.getInstance(it) + if (bump.addSecret(requireContext(), barcode.rawValue!!)) { + secretScanned = true + findNavController().navigateUp() + } + } + } + } + blockingScanned = false } .addOnFailureListener { Log.e(TAG, it.message ?: it.toString()) diff --git a/app/src/main/java/com/maenle/bump/util/BumpProcessor.kt b/app/src/main/java/com/maenle/bump/util/BumpProcessor.kt index 5b50be1..178a3c8 100644 --- a/app/src/main/java/com/maenle/bump/util/BumpProcessor.kt +++ b/app/src/main/java/com/maenle/bump/util/BumpProcessor.kt @@ -13,14 +13,11 @@ import java.io.File class BumpProcessor constructor(context: Context) { private var rest: RestSingleton = RestSingleton.getInstance(context) - private var secret: String? = null private lateinit var log: JSONArray - private var messenger: MessageProcessor? = null + init { - secret = getSecret(context) log = getLog(context) - messenger = secret?.let { MessageProcessor(it) } } companion object { @@ -50,13 +47,6 @@ class BumpProcessor constructor(context: Context) { } } - fun hasSecret(): Boolean { - secret?.let { - return true - } - return false - } - private fun getSecret(context: Context):String? { val local = LocalData(context) return local.code @@ -76,7 +66,10 @@ class BumpProcessor constructor(context: Context) { } private fun updateFromServer(context: Context) { - messenger?.let {rest.list(it.sender) {list -> updateLog(context, list)}} + LocalData(context).code?.let { + val messenger = MessageProcessor(it) + rest.list(messenger.sender) { list -> updateLog(context, list) } + } } private fun addToLog(context: Context, line: JSONObject) { @@ -84,12 +77,21 @@ class BumpProcessor constructor(context: Context) { logFile.appendText(line.toString()) } - fun addSecret(context: Context, newCode: String) { - val local = LocalData(context) - local.code = newCode - secret = getSecret(context) + fun addSecret(context: Context, newCode: String): Boolean { + return if(!checkCodeValidity(newCode)) { + false + } else { + val local = LocalData(context) + local.code = newCode - setFirebaseToken() + setFirebaseToken(context) + true + } + } + + private fun checkCodeValidity(newCode: String): Boolean { + val validChars = newCode.filter{ it in 'A'..'Z' || it in 'a'..'z' || it == '-'}.length + return newCode.length == validChars && newCode.length >= 32 } private fun getLog(context: Context): JSONArray { @@ -104,11 +106,11 @@ class BumpProcessor constructor(context: Context) { return log } - private fun setFirebaseToken() { + private fun setFirebaseToken(context: Context) { val tokenTask = FirebaseMessaging.getInstance().token - tokenTask.addOnSuccessListener { token -> ( - messenger?.let { rest.firebase(it.sender, token) {result -> Log.d("result", result.toString())}}) - } + val secret = LocalData(context).code + val messenger = secret?.let { MessageProcessor(it) } + tokenTask.addOnSuccessListener { token -> ( messenger?.sender?.let { rest.firebase(it, token) { result -> Log.d("result", result.toString())} } ) } } } \ No newline at end of file