Skip to content

Commit

Permalink
Avoid blocking SharedPref read on config update
Browse files Browse the repository at this point in the history
Fixes #2117
  • Loading branch information
pyricau committed Jan 9, 2022
1 parent caa8f5b commit d9afdb1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ object LeakCanary {
val previousConfig = field
field = newConfig
logConfigChange(previousConfig, newConfig)
HeapDumpControl.updateICanHasHeap()
HeapDumpControl.updateICanHasHeapInBackground()
}

private fun logConfigChange(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package leakcanary.internal

import android.app.Application
import android.os.Handler
import android.os.HandlerThread
import com.squareup.leakcanary.core.R
import leakcanary.AppWatcher
import leakcanary.LeakCanary
Expand Down Expand Up @@ -42,6 +44,12 @@ internal object HeapDumpControl {
}
}

private val backgroundUpdateHandler by lazy {
val handlerThread = HandlerThread("LeakCanary-Background-iCanHasHeap-Updater")
handlerThread.start()
Handler(handlerThread.looper)
}

private const val leakAssertionsClassName = "leakcanary.LeakAssertions"

private val hasLeakAssertionsClass by lazy {
Expand All @@ -53,8 +61,10 @@ internal object HeapDumpControl {
}
}

fun updateICanHasHeap() {
iCanHasHeap()
fun updateICanHasHeapInBackground() {
backgroundUpdateHandler.post {
iCanHasHeap()
}
}

fun iCanHasHeap(): ICanHazHeap {
Expand All @@ -80,8 +90,8 @@ internal object HeapDumpControl {
)
}
} else if (!config.dumpHeapWhenDebugging && DebuggerControl.isDebuggerAttached) {
mainHandler.postDelayed({
updateICanHasHeap()
backgroundUpdateHandler.postDelayed({
iCanHasHeap()
}, 20_000L)
NotifyingNope { app.getString(R.string.leak_canary_notification_retained_debugger_attached) }
} else Yup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ internal class AboutScreen : Screen() {
is Nope -> view.resources.getString(
R.string.leak_canary_heap_dump_disabled_text, iCanHasHeap.reason()
)

}
}
}

0 comments on commit d9afdb1

Please sign in to comment.