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 contentResolver = activity.contentResolver ?: return@registerForActivityResult
activity.lifecycleScope.launch {
val qrCodeFromFileScanner = QrCodeFromFileScanner(contentResolver, QRCodeReader())
if (qrCodeFromFileScanner.validContentType(data)) {
if (QrCodeFromFileScanner.validContentType(contentResolver, data)) {
try {
val qrCodeFromFileScanner = QrCodeFromFileScanner(contentResolver, QRCodeReader())
val result = qrCodeFromFileScanner.scan(data)
TunnelImporter.importTunnel(parentFragmentManager, result.text) { showSnackbar(it) }
} catch (e: Exception) {

View File

@ -96,15 +96,15 @@ class QrCodeFromFileScanner(
*/
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 {
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
}
}
}