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 18 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 @@ -9,6 +9,11 @@ import android.content.Intent.ACTION_SCREEN_ON
import android.content.IntentFilter
import android.os.Build
import java.util.concurrent.Executor
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicReference
import leakcanary.internal.friendly.checkMainThread
import shark.SharkLog

Expand All @@ -19,7 +24,7 @@ class ScreenOffTrigger(
* The executor on which the analysis is performed and on which [analysisCallback] is called.
* This should likely be a single thread executor with a background thread priority.
*/
private val analysisExecutor: Executor,
analysisExecutor: Executor,
FranAguilera marked this conversation as resolved.
Show resolved Hide resolved

/**
* Called back with a [HeapAnalysisJob.Result] after the screen went off and a
Expand All @@ -32,30 +37,38 @@ class ScreenOffTrigger(
private val analysisCallback: (HeapAnalysisJob.Result) -> Unit = { result ->
SharkLog.d { "$result" }
},

/**
* Initial delay to wait for analysisExecutor to start analysis
*
* If not provided, initial delay is 500ms
*/
private val analysisExecutorDelayMillis: Long = INITIAL_EXECUTOR_DELAY_IN_MILLI
) {

@Volatile
private var currentJob: HeapAnalysisJob? = null
private val currentJob = AtomicReference<HeapAnalysisJob?>()

private val delayedScheduledExecutorService = DelayedScheduledExecutorService()
FranAguilera marked this conversation as resolved.
Show resolved Hide resolved
private val screenReceiver = object : BroadcastReceiver() {
override fun onReceive(
context: Context,
intent: Intent
) {
if (intent.action == ACTION_SCREEN_OFF) {
if (currentJob == null) {
if (intent.shouldStartAnalysis()) {
val job =
analysisClient.newJob(JobContext(ScreenOffTrigger::class))
currentJob = job
analysisExecutor.execute {
val result = job.execute()
currentJob = null
analysisCallback(result)
if(currentJob.compareAndSet(null, job)){
delayedScheduledExecutorService.schedule {
Copy link
Member

Choose a reason for hiding this comment

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

Couldn't this have been a simple Handler(Looper.getMainLooper()).postDelayed({}, 500) ?

Copy link
Member

Choose a reason for hiding this comment

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

(you can cancel runnables on handler just fine)

Copy link
Author

Choose a reason for hiding this comment

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

Good point, simplified to use Handler

val result = job.execute()
analysisCallback(result)
}
}
}
} else {
currentJob?.cancel("screen on again")
currentJob = null
delayedScheduledExecutorService.cancel()
currentJob.getAndUpdate { job ->
job?.cancel("screen on again")
null
}
}
}
}
Expand All @@ -78,4 +91,38 @@ class ScreenOffTrigger(
checkMainThread()
application.unregisterReceiver(screenReceiver)
}

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

private class DelayedScheduledExecutorService(
private val analysisExecutorDelayMillis:Long,
private val analysisExecutor: Executor){
FranAguilera marked this conversation as resolved.
Show resolved Hide resolved

private var scheduledFuture:ScheduledFuture<*>?=null

private val scheduledExecutorService:ScheduledExecutorService by lazy {
Executors.newScheduledThreadPool(1)
}

/** Runs the specified [action] after an initial [analysisExecutorDelayMillis] */
fun schedule(action:Runnable){
scheduledFuture =
scheduledExecutorService.schedule(
{ analysisExecutor.execute(action)},
analysisExecutorDelayMillis,
TimeUnit.MILLISECONDS
)
}

/** Cancels prior ScheduledExecutorService */
fun cancel(){
scheduledFuture?.cancel(true)
}
}

companion object{
const val INITIAL_EXECUTOR_DELAY_IN_MILLI = 500L
FranAguilera marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading