Skip to content

Commit

Permalink
refactor: throttle for MediaSessionProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
Mufanc committed May 24, 2024
1 parent 21b6bcf commit 70506fb
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package xyz.mufanc.applock.core.scope.provider

import android.media.session.MediaSessionManager
import android.media.session.MediaSessionManager.OnActiveSessionsChangedListener
import android.app.ActivityThread
import io.github.libxposed.api.XposedInterface
import xyz.mufanc.applock.core.util.ContextHelper
import xyz.mufanc.applock.core.util.Log
Expand All @@ -17,9 +16,17 @@ data object MediaSessionProvider : ScopeProvider() {
.getSystemService(MediaSessionManager::class.java)
}

private var hash: Int = 0
private val listener = OnActiveSessionsChangedListener { controllers ->
if (controllers != null) {
emit(controllers.map { it.packageName }.toSet())
val packages = controllers.map { it.packageName }.sorted()
val hashCode = packages.joinToString("|").hashCode()

if (hash != hashCode) {
emit(packages.toSet())
}

hash = hashCode
}
}

Expand Down

0 comments on commit 70506fb

Please sign in to comment.