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
This commit is contained in:
parent
0a94cb517c
commit
35c072abf6
@ -49,6 +49,8 @@ class CameraFragment: Fragment() {
|
|||||||
private var _binding: FragmentCameraBinding? = null
|
private var _binding: FragmentCameraBinding? = null
|
||||||
private val binding get() = _binding!!
|
private val binding get() = _binding!!
|
||||||
|
|
||||||
|
private var secretScanned = false
|
||||||
|
|
||||||
private val screenAspectRatio: Int
|
private val screenAspectRatio: Int
|
||||||
get() {
|
get() {
|
||||||
val activity = requireActivity()
|
val activity = requireActivity()
|
||||||
@ -179,15 +181,26 @@ class CameraFragment: Fragment() {
|
|||||||
val inputImage =
|
val inputImage =
|
||||||
InputImage.fromMediaImage(imageProxy.image!!, imageProxy.imageInfo.rotationDegrees)
|
InputImage.fromMediaImage(imageProxy.image!!, imageProxy.imageInfo.rotationDegrees)
|
||||||
|
|
||||||
|
var blockingScanned = false
|
||||||
|
|
||||||
barcodeScanner.process(inputImage)
|
barcodeScanner.process(inputImage)
|
||||||
.addOnSuccessListener { barcodes ->
|
.addOnSuccessListener { barcodes ->
|
||||||
barcodes.forEach {
|
if(blockingScanned) {
|
||||||
Log.d(TAG, it.rawValue!!)
|
return@addOnSuccessListener
|
||||||
val bump = BumpProcessor.getInstance(requireContext())
|
|
||||||
bump.addSecret(requireContext(), it.rawValue!!)
|
|
||||||
findNavController().navigateUp()
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
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 {
|
.addOnFailureListener {
|
||||||
Log.e(TAG, it.message ?: it.toString())
|
Log.e(TAG, it.message ?: it.toString())
|
||||||
|
@ -13,14 +13,11 @@ import java.io.File
|
|||||||
class BumpProcessor constructor(context: Context) {
|
class BumpProcessor constructor(context: Context) {
|
||||||
|
|
||||||
private var rest: RestSingleton = RestSingleton.getInstance(context)
|
private var rest: RestSingleton = RestSingleton.getInstance(context)
|
||||||
private var secret: String? = null
|
|
||||||
private lateinit var log: JSONArray
|
private lateinit var log: JSONArray
|
||||||
private var messenger: MessageProcessor? = null
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
secret = getSecret(context)
|
|
||||||
log = getLog(context)
|
log = getLog(context)
|
||||||
messenger = secret?.let { MessageProcessor(it) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
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? {
|
private fun getSecret(context: Context):String? {
|
||||||
val local = LocalData(context)
|
val local = LocalData(context)
|
||||||
return local.code
|
return local.code
|
||||||
@ -76,7 +66,10 @@ class BumpProcessor constructor(context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun updateFromServer(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) {
|
private fun addToLog(context: Context, line: JSONObject) {
|
||||||
@ -84,12 +77,21 @@ class BumpProcessor constructor(context: Context) {
|
|||||||
logFile.appendText(line.toString())
|
logFile.appendText(line.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addSecret(context: Context, newCode: String) {
|
fun addSecret(context: Context, newCode: String): Boolean {
|
||||||
val local = LocalData(context)
|
return if(!checkCodeValidity(newCode)) {
|
||||||
local.code = newCode
|
false
|
||||||
secret = getSecret(context)
|
} 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 {
|
private fun getLog(context: Context): JSONArray {
|
||||||
@ -104,11 +106,11 @@ class BumpProcessor constructor(context: Context) {
|
|||||||
return log
|
return log
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setFirebaseToken() {
|
private fun setFirebaseToken(context: Context) {
|
||||||
val tokenTask = FirebaseMessaging.getInstance().token
|
val tokenTask = FirebaseMessaging.getInstance().token
|
||||||
tokenTask.addOnSuccessListener { token -> (
|
val secret = LocalData(context).code
|
||||||
messenger?.let { rest.firebase(it.sender, token) {result -> Log.d("result", result.toString())}})
|
val messenger = secret?.let { MessageProcessor(it) }
|
||||||
}
|
tokenTask.addOnSuccessListener { token -> ( messenger?.sender?.let { rest.firebase(it, token) { result -> Log.d("result", result.toString())} } ) }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user