Skip to content

Commit

Permalink
Merge pull request #750 from enaboapps/OwenMcGirr/fix-notification-ca…
Browse files Browse the repository at this point in the history
…llback

Prevent the notification manager from sending the callback more than once within 0.5s
  • Loading branch information
OwenMcGirr authored Nov 3, 2024
2 parents 90a25ca + 00add47 commit 06aa769
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class NotificationManager(private val accessibilityService: AccessibilityService
private var currentNotification: NotificationInfo? = null
private val handler = Handler(Looper.getMainLooper())
private val isNotificationActive = AtomicBoolean(false)
private var lastNotificationTime: Long = 0

// Configurable timeout duration in milliseconds
var timeoutDuration: Long = 5000 // Default 5 seconds
Expand All @@ -44,6 +45,11 @@ class NotificationManager(private val accessibilityService: AccessibilityService

fun handleAccessibilityEvent(event: AccessibilityEvent) {
if (event.eventType == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
val currentTime = System.currentTimeMillis()
if (currentTime - lastNotificationTime < 500) {
return
}

val notification = event.parcelableData as? Notification ?: return
val packageName = event.packageName?.toString() ?: return

Expand All @@ -60,6 +66,7 @@ class NotificationManager(private val accessibilityService: AccessibilityService
startNotificationTimeout()

listener?.onNotificationReceived(notificationInfo)
lastNotificationTime = currentTime
}
}

Expand Down Expand Up @@ -98,4 +105,4 @@ class NotificationManager(private val accessibilityService: AccessibilityService
openNotification()
}
}
}
}

0 comments on commit 06aa769

Please sign in to comment.