main

square/leakcanary

Last updated at: 29/12/2023 09:38

AndroidBuildMirror.kt

TLDR

The AndroidBuildMirror class in the shark package caches values from the android.os.Build class in the heap dump. It provides a way to retrieve cached instances of the AndroidBuildMirror class.

Classes

AndroidBuildMirror

The AndroidBuildMirror class caches values from the android.os.Build class in the heap dump. It has the following properties:

  • manufacturer: String - The value of android.os.Build.MANUFACTURER.
  • sdkInt: Int - The value of android.os.Build.VERSION.SDK_INT.
  • id: String - The value of android.os.Build.ID.

It also provides a companion object with the following method:

  • fromHeapGraph(graph: HeapGraph): AndroidBuildMirror - Returns a cached instance of the AndroidBuildMirror class by retrieving values from the provided HeapGraph object. If no cached instance exists, it creates a new instance using the values obtained from the HeapGraph.
package shark

/**
 * Caches values from the android.os.Build class in the heap dump.
 * Retrieve a cached instances via [fromHeapGraph].
 */
class AndroidBuildMirror(
  /**
   * Value of android.os.Build.MANUFACTURER
   */
  val manufacturer: String,
  /**
   * Value of android.os.Build.VERSION.SDK_INT
   */
  val sdkInt: Int,

  /**
   * Value of android.os.Build.ID
   */
  val id: String
) {
  companion object {
    /**
     * @see AndroidBuildMirror
     */
    fun fromHeapGraph(graph: HeapGraph): AndroidBuildMirror {
      return graph.context.getOrPut(AndroidBuildMirror::class.java.name) {
        val buildClass = graph.findClassByName("android.os.Build")!!
        val versionClass = graph.findClassByName("android.os.Build\$VERSION")!!
        val manufacturer = buildClass["MANUFACTURER"]!!.value.readAsJavaString()!!
        val sdkInt = versionClass["SDK_INT"]!!.value.asInt!!
        val id = buildClass["ID"]!!.value.readAsJavaString()!!
        AndroidBuildMirror(manufacturer, sdkInt, id)
      }
    }
  }
}