ui: make validity check from QR code image scanner static
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
0bd39309c8
commit
70d9a87d0d
@ -54,9 +54,9 @@ class TunnelListFragment : BaseFragment() {
|
|||||||
val activity = activity ?: return@registerForActivityResult
|
val activity = activity ?: return@registerForActivityResult
|
||||||
val contentResolver = activity.contentResolver ?: return@registerForActivityResult
|
val contentResolver = activity.contentResolver ?: return@registerForActivityResult
|
||||||
activity.lifecycleScope.launch {
|
activity.lifecycleScope.launch {
|
||||||
val qrCodeFromFileScanner = QrCodeFromFileScanner(contentResolver, QRCodeReader())
|
if (QrCodeFromFileScanner.validContentType(contentResolver, data)) {
|
||||||
if (qrCodeFromFileScanner.validContentType(data)) {
|
|
||||||
try {
|
try {
|
||||||
|
val qrCodeFromFileScanner = QrCodeFromFileScanner(contentResolver, QRCodeReader())
|
||||||
val result = qrCodeFromFileScanner.scan(data)
|
val result = qrCodeFromFileScanner.scan(data)
|
||||||
TunnelImporter.importTunnel(parentFragmentManager, result.text) { showSnackbar(it) }
|
TunnelImporter.importTunnel(parentFragmentManager, result.text) { showSnackbar(it) }
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
@ -96,15 +96,15 @@ class QrCodeFromFileScanner(
|
|||||||
*/
|
*/
|
||||||
suspend fun scan(data: Uri) = withContext(Dispatchers.Default) { doScan(data) }
|
suspend fun scan(data: Uri) = withContext(Dispatchers.Default) { doScan(data) }
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val TAG = "QrCodeFromFileScanner"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given a reference to a file, check if this file could be parsed by this class
|
* Given a reference to a file, check if this file could be parsed by this class
|
||||||
* @return true if the file can be parsed, false if not
|
* @return true if the file can be parsed, false if not
|
||||||
*/
|
*/
|
||||||
fun validContentType(data: Uri): Boolean {
|
fun validContentType(contentResolver: ContentResolver, data: Uri): Boolean {
|
||||||
return contentResolver.getType(data)?.startsWith("image/") == true
|
return contentResolver.getType(data)?.startsWith("image/") == true
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
|
||||||
private const val TAG = "QrCodeFromFileScanner"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user