xref: /aosp_15_r20/external/leakcanary2/leakcanary-android-core/src/main/java/leakcanary/EventListener.kt (revision d9e8da70d8c9df9a41d7848ae506fb3115cae6e6)
1 package leakcanary
2 
3 import android.content.Intent
4 import java.io.File
5 import java.io.Serializable
6 import leakcanary.internal.SerializableIntent
7 import shark.HeapAnalysis
8 import shark.HeapAnalysisFailure
9 import shark.HeapAnalysisSuccess
10 import shark.OnAnalysisProgressListener.Step
11 
interfacenull12 fun interface EventListener {
13 
14   /**
15    * Note: [Event] is [Serializable] for convenience but we currently make no guarantee
16    * that the Serialization is backward / forward compatible across LeakCanary versions, so plan
17    * accordingly. This is convenient for passing events around processes, and shouldn't be used
18    * to store them.
19    */
20   sealed class Event(
21     /**
22      * Unique identifier for a related chain of event. The identifier for the events that run
23      * before [HeapDump] gets reset right before [HeapDump] is sent.
24      */
25     val uniqueId: String
26   ) : Serializable {
27     /**
28      * Sent from the "LeakCanary-Heap-Dump" HandlerThread.
29      */
30     class DumpingHeap(uniqueId: String) : Event(uniqueId)
31 
32     /**
33      * Sent from the "LeakCanary-Heap-Dump" HandlerThread.
34      */
35     class HeapDump(
36       uniqueId: String,
37       val file: File,
38       val durationMillis: Long,
39       val reason: String
40     ) : Event(uniqueId)
41 
42     /**
43      * Sent from the "LeakCanary-Heap-Dump" HandlerThread.
44      */
45     class HeapDumpFailed(
46       uniqueId: String,
47       val exception: Throwable,
48       val willRetryLater: Boolean
49     ) : Event(uniqueId)
50 
51     /**
52      * [progressPercent] is a value between [0..1]
53      *
54      * Sent from the thread performing the analysis.
55      */
56     class HeapAnalysisProgress(
57       uniqueId: String,
58       val step: Step,
59       val progressPercent: Double
60     ) : Event(uniqueId)
61 
62     /**
63      * Sent from the thread performing the analysis.
64      */
65     sealed class HeapAnalysisDone<T : HeapAnalysis>(
66       uniqueId: String,
67       val heapAnalysis: T,
68       showIntent: Intent
69     ) : Event(uniqueId) {
70 
71       private val serializableShowIntent = SerializableIntent(showIntent)
72 
73       val showIntent: Intent
74         get() = serializableShowIntent.intent
75 
76       class HeapAnalysisSucceeded(
77         uniqueId: String,
78         heapAnalysis: HeapAnalysisSuccess,
79         val unreadLeakSignatures: Set<String>,
80         showIntent: Intent
81       ) : HeapAnalysisDone<HeapAnalysisSuccess>(uniqueId, heapAnalysis, showIntent)
82 
83       class HeapAnalysisFailed(
84         uniqueId: String,
85         heapAnalysis: HeapAnalysisFailure,
86         showIntent: Intent
87       ) : HeapAnalysisDone<HeapAnalysisFailure>(uniqueId, heapAnalysis, showIntent)
88     }
89   }
90 
91   /**
92    * [onEvent] is always called from the thread the events are emitted from, which is documented
93    * for each event. This enables you to potentially block a chain of events, waiting for some
94    * pre work to be done.
95    */
96   fun onEvent(event: Event)
97 }
98