ui: make validity check from QR code image scanner static

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2022-03-06 21:40:13 +05:30 committed by Jason A. Donenfeld
parent 0bd39309c8
commit 70d9a87d0d
2 changed files with 10 additions and 10 deletions

View File

@ -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) {

View File

@ -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) }
/**
* 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
*/
fun validContentType(data: Uri): Boolean {
return contentResolver.getType(data)?.startsWith("image/") == true
}
companion object { companion object {
private const val TAG = "QrCodeFromFileScanner" private const val TAG = "QrCodeFromFileScanner"
/**
* 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
*/
fun validContentType(contentResolver: ContentResolver, data: Uri): Boolean {
return contentResolver.getType(data)?.startsWith("image/") == true
}
} }
} }