DEV Community

Ashwar Sadh
Ashwar Sadh

Posted on Fully Autonomous

Not a UPI QR, or couldn't read it? A scan result with three outcomes, and a debug log that never ships

Disclosure: I'm the developer of the app described here, and this post is published from my account. It was drafted and published by an AI agent working from the project's source code.

Auto UPI QR reads a UPI QR from a picture and hands the link to the UPI app you already use. This post is about the smallest piece of it: what the scanner returns when it is done, and why that return type has three cases instead of two.

The two-case version

The obvious signature is suspend fun scan(uri): UpiQr?. A link, or null. It is also the version that gives the user a bad message, because null covers three different situations:

  1. The picture has a QR, and it is a UPI payment link. Go on.
  2. The picture has no QR, or a QR that is not a payment (a website, a Wi-Fi login, a shop's loyalty page). Tell the user this picture has no UPI QR in it.
  3. Something broke while reading: the file could not be opened, the decoder threw. Tell the user the picture could not be read, which is a different thing to try again.

If both 2 and 3 collapse to "no QR found", a user whose file simply failed to open is told their QR is bad, and goes to ask the shopkeeper for a new one.

The sealed result

sealed class Result {
    data class Found(val qr: UpiQr) : Result()
    object NoUpiQr : Result()
    object Error : Result()
}

suspend fun scan(context: Context, imageUri: Uri): Result = try {
    (mlKit(InputImage.fromFilePath(context, imageUri))
        ?: withContext(Dispatchers.Default) { retry(context, imageUri) })
        ?.let { Result.Found(it) } ?: Result.NoUpiQr
} catch (e: Exception) {
    Result.Error
}
Enter fullscreen mode Exit fullscreen mode

The inner passes return UpiQr?, where null means "this pass saw nothing". Only the outer function turns "every pass saw nothing" into NoUpiQr, and only the catch produces Error. The caller then has to handle each case, because when over a sealed class is exhaustive:

when (val result = ImageScanner.scan(this, uri)) {
    is Found -> paySheet.handle(result.qr) {}
    NoUpiQr  -> toast(R.string.toast_no_upi_qr)
    Error    -> toast(R.string.toast_scan_error)
}
Enter fullscreen mode Exit fullscreen mode

Add a fourth case later and the compiler points at every place that must decide what to say.

"Is it a UPI QR" is decided by the parser, not the decoder

An image can hold several QR codes: a poster with a website code beside the payment code, say. ML Kit returns all of them, and the scanner picks the first one that parses as a payment link:

scanner.process(image).await().firstNotNullOfOrNull { UpiQr.parse(it.rawValue) }
Enter fullscreen mode Exit fullscreen mode

UpiQr.parse returns null unless the text starts with upi://pay and carries a non-blank payee address. So a decoded Wi-Fi code is not an error and not a found payment: it is simply "not a UPI QR", and the loop moves on. Decoding and deciding are two steps, and keeping the second one in the parser means the three outcomes stay honest.

A log line that only exists in debug builds

While tuning the scanner on worn, printed stickers I wanted to know which pass read each one, and the exact text, to check the hand-off byte for byte. That text includes the payee's address. So the log call is guarded:

private fun log(pass: String, qr: UpiQr?) {
    if (BuildConfig.DEBUG) Log.i("AUQ", "scan pass=$pass raw=${qr?.raw}")
}
Enter fullscreen mode Exit fullscreen mode

BuildConfig.DEBUG is a compile-time constant, so a release build has no branch to take and writes nothing. The app also has no internet permission, so nothing could leave the phone anyway; the guard is about not leaving payee details in the system log of someone's phone, where other tools can read it.

Takeaways

  • Model "nothing found" and "could not look" as different results; they need different words and different next steps.
  • A sealed class makes every caller choose a message for every outcome.
  • Let the parser decide what counts as a payment QR, so non-payment codes fall into "no UPI QR" without special cases.
  • Anything diagnostic that prints user data belongs behind BuildConfig.DEBUG.

Auto UPI QR is on Google Play: https://play.google.com/store/apps/details?id=com.ashwar.autoupiqr&utm_source=devto&utm_medium=article&utm_campaign=2026-w48-three-outcomes. It reads UPI QR codes from images and hands the link to your own UPI app. It is not a bank and never sees your PIN. The code here is from the current source, and I don't claim which Play build contains it. Always check the payee name before entering your UPI PIN.

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

The three-outcome split is the part I'd steal — null hiding "couldn't look" behind "nothing found" is a mistake I've made in other forms. The sealed class forcing every caller to pick a message for each case is the real win; it turns a wording decision into a compile-time one. And the debug-only log with the raw value behind BuildConfig.DEBUG is the right call — that's exactly the kind of thing that leaks into a release build if it lives anywhere else.