Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.
refactor: extract barcode action generation to helper method and add support for UPI payment QR codes
Rishabh Bajpai217e08399136
diff --git a/app/src/main/java/com/gotcha/service/QrCodeScanner.kt b/app/src/main/java/com/gotcha/service/QrCodeScanner.ktindex 171e23c..b2fa9b5 100644--- a/app/src/main/java/com/gotcha/service/QrCodeScanner.kt+++ b/app/src/main/java/com/gotcha/service/QrCodeScanner.kt@@ -32,138 +32,15 @@ object QrCodeScanner {}}- @Suppress("LongMethod", "CyclomaticComplexMethod")fun barcodeToEntity(barcode: Barcode): DetectedEntity? {- val rawValue = barcode.rawValue ?: return null+ val rawValue = barcode.rawValue ?: barcode.displayValue ?: return nullif (rawValue.isBlank()) return nullval isQr = barcode.format == Barcode.FORMAT_QR_CODEval type = if (isQr) EntityType.QR_CODE else EntityType.BARCODEval actions = mutableListOf<SmartAction>()- val displayTitle = when (barcode.valueType) {- Barcode.TYPE_URL -> {- val url = barcode.url?.url ?: rawValue- val domain = runCatching { android.net.Uri.parse(url).host }.getOrNull()- ?: SmartActionDetector.snippet(url, 20)- actions.add(- SmartAction(- label = "🌐 Open QR Link: $domain",- prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_VIEW, url),- actionType = ActionType.NATIVE_BROWSE,- isPrimary = true- )- )- actions.add(- SmartAction(- label = "📝 Summarize",- prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_FETCH, url),- actionType = ActionType.LLM_SUMMARIZE- )- )- actions.add(- SmartAction(- label = "📋 Copy URL",- prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_COPY, url),- actionType = ActionType.NATIVE_COPY- )- )- "QR Link: $domain"- }- Barcode.TYPE_WIFI -> {- val ssid = barcode.wifi?.ssid ?: "Wi-Fi"- val pwd = barcode.wifi?.password ?: ""- val info = "SSID: $ssid, Password: $pwd"- actions.add(- SmartAction(- label = "📶 Wi-Fi: $ssid",- prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_COPY, info),- actionType = ActionType.NATIVE_COPY,- isPrimary = true- )- )- "Wi-Fi QR: $ssid"- }- Barcode.TYPE_PHONE -> {- val phone = barcode.phone?.number ?: rawValue- actions.add(- SmartAction(- label = "📞 Call: ${SmartActionDetector.snippet(phone, 15)}",- prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_DIAL, phone),- actionType = ActionType.NATIVE_DIAL,- isPrimary = true- )- )- "QR Phone: $phone"- }- else -> {- if (rawValue.startsWith("http://", ignoreCase = true) ||- rawValue.startsWith("https://", ignoreCase = true)- ) {- val domain = runCatching { android.net.Uri.parse(rawValue).host }.getOrNull()- ?: SmartActionDetector.snippet(rawValue, 20)- actions.add(- SmartAction(- label = "🌐 Open QR Link: $domain",- prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_VIEW, rawValue),- actionType = ActionType.NATIVE_BROWSE,- isPrimary = true- )- )- actions.add(- SmartAction(- label = "📝 Summarize",- prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_FETCH, rawValue),- actionType = ActionType.LLM_SUMMARIZE- )- )- actions.add(- SmartAction(- label = "📋 Copy Link",- prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_COPY, rawValue),- actionType = ActionType.NATIVE_COPY- )- )- "QR Link: $domain"- } else if (!isQr) {- val searchUrl = "https://www.google.com/search?q=${android.net.Uri.encode(rawValue)}"- actions.add(- SmartAction(- label = "🔍 Search Barcode: ${SmartActionDetector.snippet(rawValue, 16)}",- prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_VIEW, searchUrl),- actionType = ActionType.NATIVE_BROWSE,- isPrimary = true- )- )- actions.add(- SmartAction(- label = "📋 Copy Barcode",- prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_COPY, rawValue),- actionType = ActionType.NATIVE_COPY- )- )- "Barcode: ${SmartActionDetector.snippet(rawValue, 20)}"- } else {- actions.add(- SmartAction(- label = "📷 QR: ${SmartActionDetector.snippet(rawValue, 20)}",- prompt = "Here is the content of a QR code detected on screen:\n\n$rawValue\n\n" +- "Please process or act on this QR code content.",- actionType = ActionType.LLM_GENERAL,- isPrimary = true- )- )- actions.add(- SmartAction(- label = "📋 Copy QR Content",- prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_COPY, rawValue),- actionType = ActionType.NATIVE_COPY- )- )- "QR: ${SmartActionDetector.snippet(rawValue, 20)}"- }- }- }+ val displayTitle = buildQrActions(barcode, rawValue, isQr, actions)return DetectedEntity(type = type,@@ -174,4 +51,216 @@ object QrCodeScanner {actions = actions)}++ @Suppress("LongMethod", "CyclomaticComplexMethod")+ private fun buildQrActions(+ barcode: Barcode,+ rawValue: String,+ isQr: Boolean,+ actions: MutableList<SmartAction>+ ): String {+ val clean = rawValue.trim()++ // 1. ML Kit recognized URL+ if (barcode.valueType == Barcode.TYPE_URL) {+ val url = barcode.url?.url ?: clean+ val domain = runCatching { android.net.Uri.parse(url).host }.getOrNull()+ ?.ifBlank { null } ?: SmartActionDetector.snippet(url, 24)+ actions.add(+ SmartAction(+ label = "🌐 Open QR Link: $domain",+ prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_VIEW, url),+ actionType = ActionType.NATIVE_BROWSE,+ isPrimary = true+ )+ )+ actions.add(+ SmartAction(+ label = "📝 Summarize",+ prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_FETCH, url),+ actionType = ActionType.LLM_SUMMARIZE+ )+ )+ actions.add(+ SmartAction(+ label = "📋 Copy URL",+ prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_COPY, url),+ actionType = ActionType.NATIVE_COPY+ )+ )+ return "QR Link: $domain"+ }++ // 2. ML Kit recognized Wi-Fi+ if (barcode.valueType == Barcode.TYPE_WIFI) {+ val ssid = barcode.wifi?.ssid ?: "Wi-Fi"+ val pwd = barcode.wifi?.password ?: ""+ val info = "SSID: $ssid, Password: $pwd"+ actions.add(+ SmartAction(+ label = "📶 Wi-Fi: $ssid",+ prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_COPY, info),+ actionType = ActionType.NATIVE_COPY,+ isPrimary = true+ )+ )+ return "Wi-Fi QR: $ssid"+ }++ // 3. ML Kit recognized Phone+ if (barcode.valueType == Barcode.TYPE_PHONE) {+ val phone = barcode.phone?.number ?: clean+ actions.add(+ SmartAction(+ label = "📞 Call: ${SmartActionDetector.snippet(phone, 16)}",+ prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_DIAL, phone),+ actionType = ActionType.NATIVE_DIAL,+ isPrimary = true+ )+ )+ return "QR Phone: $phone"+ }++ // 4. UPI Payment scheme (upi://pay?pa=...)+ if (clean.startsWith("upi://", ignoreCase = true)) {+ val paMatch = Regex("pa=([^&]+)", RegexOption.IGNORE_CASE).find(clean)+ val payee = paMatch?.groupValues?.getOrNull(1)+ ?.let { android.net.Uri.decode(it) } ?: SmartActionDetector.snippet(clean, 20)+ actions.add(+ SmartAction(+ label = "💳 Pay via UPI: $payee",+ prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_VIEW, clean),+ actionType = ActionType.NATIVE_BROWSE,+ isPrimary = true+ )+ )+ actions.add(+ SmartAction(+ label = "📋 Copy UPI Link",+ prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_COPY, clean),+ actionType = ActionType.NATIVE_COPY+ )+ )+ return "UPI Payment: $payee"+ }++ // 5. Generic URI scheme (e.g. intent://, paytm://, phonepe://, geo:, mailto:, smsto:, market://)+ val hasScheme = Regex("^[a-zA-Z][a-zA-Z0-9+.-]*://").containsMatchIn(clean) ||+ Regex("^(geo|mailto|tel|smsto|intent|market):", RegexOption.IGNORE_CASE).containsMatchIn(clean)++ if (hasScheme) {+ val schemeName = clean.substringBefore("://").substringBefore(":").lowercase()+ val displayDomain = runCatching { android.net.Uri.parse(clean).host }.getOrNull()+ ?.ifBlank { null } ?: schemeName+ actions.add(+ SmartAction(+ label = "🌐 Open QR Link: $displayDomain",+ prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_VIEW, clean),+ actionType = ActionType.NATIVE_BROWSE,+ isPrimary = true+ )+ )+ actions.add(+ SmartAction(+ label = "📋 Copy Link",+ prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_COPY, clean),+ actionType = ActionType.NATIVE_COPY+ )+ )+ return "QR Link: $displayDomain"+ }++ // 6. Web URL without scheme (e.g. www.example.com/page, domain.com, sub.domain.org/path)+ val domainPattern = Regex(+ "^(www\\.|[a-zA-Z0-9-]+\\.(com|org|net|io|dev|app|co|in|uk|ca|au|de|fr|ai|me|info|gov|edu))(/[\\S]*)?$",+ RegexOption.IGNORE_CASE+ )+ if (domainPattern.matches(clean) || clean.startsWith("www.", ignoreCase = true)) {+ val fullUrl = if (clean.startsWith("http://", ignoreCase = true) ||+ clean.startsWith("https://", ignoreCase = true)+ ) {+ clean+ } else {+ "https://$clean"+ }+ val domain = runCatching { android.net.Uri.parse(fullUrl).host }.getOrNull()+ ?: SmartActionDetector.snippet(clean, 20)+ actions.add(+ SmartAction(+ label = "🌐 Open QR Link: $domain",+ prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_VIEW, fullUrl),+ actionType = ActionType.NATIVE_BROWSE,+ isPrimary = true+ )+ )+ actions.add(+ SmartAction(+ label = "📝 Summarize",+ prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_FETCH, fullUrl),+ actionType = ActionType.LLM_SUMMARIZE+ )+ )+ actions.add(+ SmartAction(+ label = "📋 Copy Link",+ prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_COPY, fullUrl),+ actionType = ActionType.NATIVE_COPY+ )+ )+ return "QR Link: $domain"+ }++ // 7. If 1D Barcode+ if (!isQr) {+ val searchUrl = "https://www.google.com/search?q=${android.net.Uri.encode(clean)}"+ actions.add(+ SmartAction(+ label = "🔍 Search Barcode: ${SmartActionDetector.snippet(clean, 16)}",+ prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_VIEW, searchUrl),+ actionType = ActionType.NATIVE_BROWSE,+ isPrimary = true+ )+ )+ actions.add(+ SmartAction(+ label = "📋 Copy Barcode",+ prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_COPY, clean),+ actionType = ActionType.NATIVE_COPY+ )+ )+ return "Barcode: ${SmartActionDetector.snippet(clean, 20)}"+ }++ // 8. General QR content fallback:+ // ALWAYS provide "🌐 Open QR Link" via TYPE_VIEW as PRIMARY action!+ val searchUrl = if (clean.startsWith("http://") || clean.startsWith("https://")) {+ clean+ } else {+ "https://www.google.com/search?q=${android.net.Uri.encode(clean)}"+ }+ actions.add(+ SmartAction(+ label = "🌐 Open QR Link: ${SmartActionDetector.snippet(clean, 18)}",+ prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_VIEW, searchUrl),+ actionType = ActionType.NATIVE_BROWSE,+ isPrimary = true+ )+ )+ actions.add(+ SmartAction(+ label = "🤖 Ask Assistant",+ prompt = "Here is the content of a QR code detected on screen:\n\n$clean\n\n" ++ "Please process or act on this QR code content.",+ actionType = ActionType.LLM_GENERAL+ )+ )+ actions.add(+ SmartAction(+ label = "📋 Copy QR Content",+ prompt = SmartActionDetector.encode(SmartActionDetector.TYPE_COPY, clean),+ actionType = ActionType.NATIVE_COPY+ )+ )+ return "QR: ${SmartActionDetector.snippet(clean, 20)}"+ }}