Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2661] Fix BroadcastReceiver ANR at ScreenOffTrigger #2732

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import android.content.Intent.ACTION_SCREEN_OFF
import android.content.Intent.ACTION_SCREEN_ON
import android.content.IntentFilter
import android.os.Build
import android.os.Handler
import android.os.Looper
import java.util.concurrent.Executor
import java.util.concurrent.atomic.AtomicReference
import leakcanary.internal.friendly.checkMainThread
import shark.SharkLog

Expand All @@ -32,30 +35,39 @@ class ScreenOffTrigger(
private val analysisCallback: (HeapAnalysisJob.Result) -> Unit = { result ->
SharkLog.d { "$result" }
},

/**
* Initial delay to wait for analysisExecutor to start analysis
*
* If not specified, the initial delay is 500 ms
*/
private val analysisExecutorDelayMillis: Long = INITIAL_EXECUTOR_DELAY_MILLIS
Copy link
Member

@pyricau pyricau Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API nit: take in a Duration instead, rename the field to analysisExecutorDelay

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good callout, updated now

) {

@Volatile
private var currentJob: HeapAnalysisJob? = null
private val currentJob = AtomicReference<HeapAnalysisJob?>()
private val analysisHandler = Handler(Looper.getMainLooper())
private var analysisRunnable: Runnable? = null
Copy link
Member

@pyricau pyricau Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: rename this to submitAnalysisToExecutor?


private val screenReceiver = object : BroadcastReceiver() {
override fun onReceive(
context: Context,
intent: Intent
) {
if (intent.action == ACTION_SCREEN_OFF) {
if (currentJob == null) {
val job =
analysisClient.newJob(JobContext(ScreenOffTrigger::class))
currentJob = job
analysisExecutor.execute {
if (intent.shouldStartAnalysis()) {
val job =
analysisClient.newJob(JobContext(ScreenOffTrigger::class))
if (currentJob.compareAndSet(null, job)) {
schedule {
val result = job.execute()
currentJob = null
analysisCallback(result)
}
}
} else {
currentJob?.cancel("screen on again")
currentJob = null
cancelScheduledAction()
currentJob.getAndUpdate { job ->
job?.cancel("screen on again")
null
}
}
}
}
Expand All @@ -78,4 +90,22 @@ class ScreenOffTrigger(
checkMainThread()
application.unregisterReceiver(screenReceiver)
}

private fun Intent.shouldStartAnalysis(): Boolean {
return this.action == ACTION_SCREEN_OFF && currentJob.get() == null
}

private fun schedule(action: Runnable) {
analysisRunnable = Runnable {
analysisExecutor.execute(action)
}.also { analysisHandler.postDelayed(it, analysisExecutorDelayMillis) }
}

private fun cancelScheduledAction() {
analysisRunnable?.let { analysisHandler.removeCallbacks(it) }
}

companion object {
private const val INITIAL_EXECUTOR_DELAY_MILLIS = 500L
}
}
Loading