<lambda>null1 package leakcanary
2 
3 import android.content.res.Resources
4 import android.content.res.Resources.NotFoundException
5 import leakcanary.HeapAnalysisInterceptor.Chain
6 import leakcanary.HeapAnalysisJob.Result
7 import shark.AndroidResourceIdNames
8 
9 /**
10  * Interceptor that saves the names of R.id.* entries and their associated int values to a static
11  * field that can then be read from the heap dump.
12  */
13 class SaveResourceIdsInterceptor(private val resources: Resources) : HeapAnalysisInterceptor {
14   override fun intercept(chain: Chain): Result {
15     saveResourceIdNamesToMemory()
16     return chain.proceed()
17   }
18 
19   private fun saveResourceIdNamesToMemory() {
20     AndroidResourceIdNames.saveToMemory(
21       getResourceTypeName = { id ->
22         try {
23           resources.getResourceTypeName(id)
24         } catch (e: NotFoundException) {
25           null
26         }
27       },
28       getResourceEntryName = { id ->
29         try {
30           resources.getResourceEntryName(id)
31         } catch (e: NotFoundException) {
32           null
33         }
34       })
35   }
36 }