main

square/leakcanary

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

SerializableIntent.kt

TLDR

This file contains the definition of the SerializableIntent class, which is used to wrap an Intent and serialize it as its URI string.

Classes

SerializableIntent

This class wraps an Intent object and serializes it as its URI string. It implements the Serializable interface to enable serialization. The class has the following properties and methods:

  • uri: This property stores the URI string representation of the wrapped Intent.
  • _intent: This property is transient and holds the reference to the original Intent object. It is not serialized.
  • intent: This property returns the deserialized Intent object. If the _intent property is null, it parses the uri string and initializes _intent before returning it.
package leakcanary.internal

import android.content.Intent
import java.io.Serializable

/**
 * Wraps an Intent to serialize it as its URI string.
 */
internal class SerializableIntent(intent: Intent) : Serializable {

  private val uri = intent.toUri(0)

  // Intent is not Serializable
  @Transient
  private var _intent: Intent? = intent

  val intent: Intent
    get() = _intent.run {
      this ?: Intent.parseUri(uri, 0)
        .apply { _intent = this }
    }
}