• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package leakcanary.internal.tv
2 
3 import android.app.Application
4 import com.squareup.leakcanary.core.R
5 import leakcanary.LeakCanary
6 import leakcanary.internal.InternalLeakCanary
7 import leakcanary.internal.OnRetainInstanceListener
8 import leakcanary.internal.RetainInstanceEvent
9 import leakcanary.internal.RetainInstanceEvent.CountChanged.BelowThreshold
10 import leakcanary.internal.RetainInstanceEvent.CountChanged.DumpHappenedRecently
11 import leakcanary.internal.RetainInstanceEvent.CountChanged.DumpingDisabled
12 import leakcanary.internal.RetainInstanceEvent.NoMoreObjects
13 import leakcanary.internal.friendly.mainHandler
14 import shark.SharkLog
15 
16 /**
17  * [OnRetainInstanceListener] implementation for Android TV devices which displays a helpful
18  * Toast message on current state of retained instances.
19  * It will notify user when heap dump is going to happen, so that users who set the
20  * [LeakCanary.Config.retainedVisibleThreshold] to any value other than 1 will be aware of retain
21  * instances changes. Additionally, it notifies about debugger being attached and whether heap dump
22  * happened recently.
23  */
24 internal class TvOnRetainInstanceListener(private val application: Application) :
25   OnRetainInstanceListener {
26 
onEventnull27   override fun onEvent(event: RetainInstanceEvent) {
28     val message = when (event) {
29       NoMoreObjects -> {
30         application.getString(R.string.leak_canary_notification_no_retained_object_title)
31       }
32       is BelowThreshold -> {
33         application.getString(
34           R.string.leak_canary_tv_toast_retained_objects,
35           event.retainedCount,
36           LeakCanary.config.retainedVisibleThreshold
37         )
38       }
39       is DumpingDisabled -> {
40         event.reason
41       }
42       is DumpHappenedRecently -> {
43         application.getString(R.string.leak_canary_notification_retained_dump_wait)
44       }
45     }
46     SharkLog.d { message }
47 
48     mainHandler.post {
49       val resumedActivity = InternalLeakCanary.resumedActivity ?: return@post
50       TvToast.makeText(resumedActivity, message).show()
51     }
52   }
53 }
54