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 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) {
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user