Skip to content

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.

fix(review): set all proactive defaults to true, check settings & blacklist before notification broadcast, evict stale map entries > 60s, and refactor SettingsToggleRow

Rishabh Bajpai185cc49be55f
diff --git a/app/src/main/java/com/gotcha/data/SettingsRepository.kt b/app/src/main/java/com/gotcha/data/SettingsRepository.kt
index 908f2a5..bfc4a90 100644
--- a/app/src/main/java/com/gotcha/data/SettingsRepository.kt
+++ b/app/src/main/java/com/gotcha/data/SettingsRepository.kt
@@ -47,7 +47,7 @@ data class Settings(
val proactiveScanClipboard: Boolean = true,
val proactiveScanNotifications: Boolean = true,
val proactiveOtpEnabled: Boolean = true,
- val proactiveAutoCopyOtp: Boolean = false,
+ val proactiveAutoCopyOtp: Boolean = true,
val proactiveAppBlacklist: Set<String> = emptySet()
) {
/** True when the active provider has everything it needs to make requests. */
@@ -129,7 +129,7 @@ class SettingsRepository(context: Context) {
proactiveScanClipboard = prefs.getBoolean(KEY_PROACTIVE_SCAN_CLIPBOARD, true),
proactiveScanNotifications = prefs.getBoolean(KEY_PROACTIVE_SCAN_NOTIFICATIONS, true),
proactiveOtpEnabled = prefs.getBoolean(KEY_PROACTIVE_OTP_ENABLED, true),
- proactiveAutoCopyOtp = prefs.getBoolean(KEY_PROACTIVE_AUTO_COPY_OTP, false),
+ proactiveAutoCopyOtp = prefs.getBoolean(KEY_PROACTIVE_AUTO_COPY_OTP, true),
proactiveAppBlacklist = prefs.getStringSet(KEY_PROACTIVE_BLACKLIST, emptySet()) ?: emptySet()
)
diff --git a/app/src/main/java/com/gotcha/service/GotchaNotificationListenerService.kt b/app/src/main/java/com/gotcha/service/GotchaNotificationListenerService.kt
index b93f572..b2c300f 100644
--- a/app/src/main/java/com/gotcha/service/GotchaNotificationListenerService.kt
+++ b/app/src/main/java/com/gotcha/service/GotchaNotificationListenerService.kt
@@ -38,12 +38,22 @@ class GotchaNotificationListenerService : NotificationListenerService() {
override fun onNotificationPosted(sbn: StatusBarNotification?) {
if (sbn == null) return
val pkg = sbn.packageName ?: ""
+ val context = applicationContext
+
+ // Check settings & blacklist FIRST before processing or broadcasting
+ val settings = runCatching { SettingsRepository(context).load() }.getOrNull() ?: return
+ if (!settings.proactiveEnabled || !settings.proactiveScanNotifications) return
+ if (settings.proactiveAppBlacklist.contains(pkg)) return
+
+ // Evict stale entries older than 60s to prevent unbounded memory growth
val now = System.currentTimeMillis()
+ lastNotificationTimeMap.entries.removeIf { now - it.value > 60_000L }
+
+ // Debounce duplicate notifications from the same package within 10s
val lastTime = lastNotificationTimeMap[pkg] ?: 0L
if (now - lastTime < 10_000L) return
lastNotificationTimeMap[pkg] = now
- val context = applicationContext
val extras = sbn.notification?.extras
val title = extras?.getCharSequence("android.title")?.toString() ?: ""
val text = extras?.getCharSequence("android.text")?.toString() ?: ""
@@ -56,10 +66,6 @@ class GotchaNotificationListenerService : NotificationListenerService() {
}
context.sendBroadcast(intent)
- val settings = runCatching { SettingsRepository(context).load() }.getOrNull() ?: return
- if (!settings.proactiveEnabled || !settings.proactiveScanNotifications) return
- if (settings.proactiveAppBlacklist.contains(pkg)) return
-
val fullText = "$title $text".trim()
if (fullText.isBlank()) return
diff --git a/app/src/main/java/com/gotcha/ui/SettingsScreen.kt b/app/src/main/java/com/gotcha/ui/SettingsScreen.kt
index f67482e..cd448fe 100644
--- a/app/src/main/java/com/gotcha/ui/SettingsScreen.kt
+++ b/app/src/main/java/com/gotcha/ui/SettingsScreen.kt
@@ -825,58 +825,38 @@ fun SettingsScreen(
)
AnimatedVisibility(visible = proactiveExpanded) {
Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
- Row(
- modifier = Modifier.fillMaxWidth(),
- horizontalArrangement = Arrangement.SpaceBetween,
- verticalAlignment = Alignment.CenterVertically
- ) {
- Text("Master Proactive Offers", style = MaterialTheme.typography.bodyLarge)
- Switch(checked = proactiveEnabled, onCheckedChange = { proactiveEnabled = it })
- }
+ SettingsToggleRow(
+ label = "Master Proactive Offers",
+ checked = proactiveEnabled,
+ onCheckedChange = { proactiveEnabled = it },
+ isLarge = true
+ )
if (proactiveEnabled) {
- Row(
- modifier = Modifier.fillMaxWidth(),
- horizontalArrangement = Arrangement.SpaceBetween,
- verticalAlignment = Alignment.CenterVertically
- ) {
- Text("Scan Screen Content", style = MaterialTheme.typography.bodyMedium)
- Switch(checked = proactiveScanScreen, onCheckedChange = { proactiveScanScreen = it })
- }
- Row(
- modifier = Modifier.fillMaxWidth(),
- horizontalArrangement = Arrangement.SpaceBetween,
- verticalAlignment = Alignment.CenterVertically
- ) {
- Text("Scan Clipboard", style = MaterialTheme.typography.bodyMedium)
- Switch(checked = proactiveScanClipboard, onCheckedChange = { proactiveScanClipboard = it })
- }
- Row(
- modifier = Modifier.fillMaxWidth(),
- horizontalArrangement = Arrangement.SpaceBetween,
- verticalAlignment = Alignment.CenterVertically
- ) {
- Text("Scan Notifications", style = MaterialTheme.typography.bodyMedium)
- Switch(
- checked = proactiveScanNotifications,
- onCheckedChange = { proactiveScanNotifications = it }
- )
- }
- Row(
- modifier = Modifier.fillMaxWidth(),
- horizontalArrangement = Arrangement.SpaceBetween,
- verticalAlignment = Alignment.CenterVertically
- ) {
- Text("Detect OTP / Codes", style = MaterialTheme.typography.bodyMedium)
- Switch(checked = proactiveOtpEnabled, onCheckedChange = { proactiveOtpEnabled = it })
- }
- Row(
- modifier = Modifier.fillMaxWidth(),
- horizontalArrangement = Arrangement.SpaceBetween,
- verticalAlignment = Alignment.CenterVertically
- ) {
- Text("Auto-Copy OTP to Clipboard", style = MaterialTheme.typography.bodyMedium)
- Switch(checked = proactiveAutoCopyOtp, onCheckedChange = { proactiveAutoCopyOtp = it })
- }
+ SettingsToggleRow(
+ label = "Scan Screen Content",
+ checked = proactiveScanScreen,
+ onCheckedChange = { proactiveScanScreen = it }
+ )
+ SettingsToggleRow(
+ label = "Scan Clipboard",
+ checked = proactiveScanClipboard,
+ onCheckedChange = { proactiveScanClipboard = it }
+ )
+ SettingsToggleRow(
+ label = "Scan Notifications",
+ checked = proactiveScanNotifications,
+ onCheckedChange = { proactiveScanNotifications = it }
+ )
+ SettingsToggleRow(
+ label = "Detect OTP / Codes",
+ checked = proactiveOtpEnabled,
+ onCheckedChange = { proactiveOtpEnabled = it }
+ )
+ SettingsToggleRow(
+ label = "Auto-Copy OTP to Clipboard",
+ checked = proactiveAutoCopyOtp,
+ onCheckedChange = { proactiveAutoCopyOtp = it }
+ )
}
Button(
onClick = {
@@ -970,3 +950,23 @@ private fun SectionHeader(
)
}
}
+
+@Composable
+private fun SettingsToggleRow(
+ label: String,
+ checked: Boolean,
+ onCheckedChange: (Boolean) -> Unit,
+ isLarge: Boolean = false
+) {
+ Row(
+ modifier = Modifier.fillMaxWidth(),
+ horizontalArrangement = Arrangement.SpaceBetween,
+ verticalAlignment = Alignment.CenterVertically
+ ) {
+ Text(
+ label,
+ style = if (isLarge) MaterialTheme.typography.bodyLarge else MaterialTheme.typography.bodyMedium
+ )
+ Switch(checked = checked, onCheckedChange = onCheckedChange)
+ }
+}