-
Notifications
You must be signed in to change notification settings - Fork 4k
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
FranAguilera
wants to merge
22
commits into
square:main
Choose a base branch
from
FranAguilera:franjam/fix_broadcast_receiver_anr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+43
−11
Open
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c908c11
Fix BroadcastReceiver ANR
FranAguilera 23c0fda
Fix BroadcastReceiver ANR
FranAguilera e657cdb
PR feedback
FranAguilera 88cad44
Resolve conflicts
FranAguilera 1290103
Fix formatting
FranAguilera 5b0eb49
Fix typo
FranAguilera 4dc4a8c
PR feedback
FranAguilera 3bb0e92
Fix typo
FranAguilera c1e937d
Merge branch 'franjam/fix_broadcast_receiver_anr' of https://github.c…
FranAguilera bd72604
Handle edge case for DelayedScheduledExecutorService with start/stop/…
FranAguilera 14286c8
Minor polishes
FranAguilera dcbea4f
Minor clean up
FranAguilera 82a0b71
AnalysisJobHandler
FranAguilera a4a140d
Reduce scope of changes and go back to original approach of DelayedSc…
FranAguilera 6c97781
Revert unwanted changes at docs
FranAguilera 70bcd84
Update formatting of leak-canary-for-releases.md
FranAguilera 7c3d83c
Revert unwanted indentation
FranAguilera 85c7c3d
Convert currentJob AtomicReference to val
FranAguilera 63d563b
PR feedback
FranAguilera e88dea6
PR feedback
FranAguilera 3b192e7
Rename shouldStartAnalysis to shouldScheduleAnalysis
FranAguilera 2668668
Rename cancelScheduledAction to cancelScheduledAnalysis
FranAguilera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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 | ||
) { | ||
|
||
@Volatile | ||
private var currentJob: HeapAnalysisJob? = null | ||
private val currentJob = AtomicReference<HeapAnalysisJob?>() | ||
private val analysisHandler = Handler(Looper.getMainLooper()) | ||
private var analysisRunnable: Runnable? = null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: rename this to |
||
|
||
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 | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -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 | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 toanalysisExecutorDelay
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good callout, updated now