SaveResourceIdsInterceptor.kt
TLDR
The SaveResourceIdsInterceptor
class is an interceptor used in the LeakCanary library to save the names of R.id.*
entries and their associated integer values to a static field. This allows the information to be read from a heap dump later.
Methods
intercept
This method is an implementation of the HeapAnalysisInterceptor
interface. It intercepts the Chain
of interceptors and proceeds with the analysis by calling chain.proceed()
. Before proceeding, it also saves the resource ID names to memory by calling the saveResourceIdNamesToMemory
method.
saveResourceIdNamesToMemory
This private method saves the names of R.id.*
entries and their associated integer values to memory by using the AndroidResourceIdNames.saveToMemory
static method. It retrieves the resource type name and resource entry name for each resource ID, catching any NotFoundException
that may occur.
Classes
There are no additional classes in this file.
package leakcanary
import android.content.res.Resources
import android.content.res.Resources.NotFoundException
import leakcanary.HeapAnalysisInterceptor.Chain
import leakcanary.HeapAnalysisJob.Result
import shark.AndroidResourceIdNames
/**
* Interceptor that saves the names of R.id.* entries and their associated int values to a static
* field that can then be read from the heap dump.
*/
class SaveResourceIdsInterceptor(private val resources: Resources) : HeapAnalysisInterceptor {
override fun intercept(chain: Chain): Result {
saveResourceIdNamesToMemory()
return chain.proceed()
}
private fun saveResourceIdNamesToMemory() {
AndroidResourceIdNames.saveToMemory(
getResourceTypeName = { id ->
try {
resources.getResourceTypeName(id)
} catch (e: NotFoundException) {
null
}
},
getResourceEntryName = { id ->
try {
resources.getResourceEntryName(id)
} catch (e: NotFoundException) {
null
}
})
}
}