<commit_before>package leakcanary.internal.tv

import android.app.Application
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import leakcanary.internal.RetainInstanceChange
import leakcanary.LeakCanary
import leakcanary.internal.OnRetainInstanceListener
import leakcanary.internal.RetainInstanceChange.CountChanged
import leakcanary.internal.RetainInstanceChange.Reset

/**
 * [OnRetainInstanceListener] implementation for Android TV devices which keeps track of the current
 * retained instances count and displays a helpful Toast message on current state of it.
 * It will notify user when heap dump is going to happen, so that users who set the
 * [LeakCanary.Config.retainedVisibleThreshold] to any value other than 1 will be aware of retain
 * instances changes. If user set threshold for heap dump to 1 then this listener becomes no-op,
 * as any retain instance change will trigger the heap dump with its own toast.
 *
 */
internal class TvOnRetainInstanceListener(private val application: Application) : OnRetainInstanceListener {

  private var lastRetainedCount = 0
  private val handler = Handler(Looper.getMainLooper())

  override fun onChange(change: RetainInstanceChange) {
    when (change) {
      is CountChanged -> onCountChanged(change.retainedCount)
      Reset -> onReset()
    }
  }

  private fun onCountChanged(retainedCount: Int) {
    // Do nothing if count hasn't changed
    if (retainedCount == lastRetainedCount) return

    // Remember new retained count
    lastRetainedCount = retainedCount

    // Construct message: either about dumping a heap or on when it will happen
    val retainedVisibleThreshold = LeakCanary.config.retainedVisibleThreshold

    // Don't display any toast if single retain instance triggers a heap dump
    if (retainedVisibleThreshold == 1) return

    var text = "Retained objects: $retainedCount."
    text +=
      if (retainedCount >= retainedVisibleThreshold)
        " Dumping the heap..."
      else
        "Heap dump threshold is $retainedVisibleThreshold" +
            "\nBackground the app to trigger heap dump immediately"

    // Post the Toast into main thread and wrap it with try-catch in case Toast crashes (it happens)
    handler.post {
      try {
        Toast.makeText(application, text, Toast.LENGTH_LONG).show()
      } catch (exception: Exception) {
        // Toasts are prone to crashing, ignore
      }<commit_msg>- Added a button to leak activity in the sample app<commit_after>package leakcanary.internal.tv

import android.app.Application
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import leakcanary.internal.RetainInstanceChange
import leakcanary.LeakCanary
import leakcanary.internal.OnRetainInstanceListener
import leakcanary.internal.RetainInstanceChange.CountChanged
import leakcanary.internal.RetainInstanceChange.Reset

/**
 * [OnRetainInstanceListener] implementation for Android TV devices which keeps track of the current
 * retained instances count and displays a helpful Toast message on current state of it.
 * It will notify user when heap dump is going to happen, so that users who set the
 * [LeakCanary.Config.retainedVisibleThreshold] to any value other than 1 will be aware of retain
 * instances changes. If user set threshold for heap dump to 1 then this listener becomes no-op,
 * as any retain instance change will trigger the heap dump with its own toast.
 *
 */
internal class TvOnRetainInstanceListener(private val application: Application) : OnRetainInstanceListener {

  private var lastRetainedCount = 0
  private val handler = Handler(Looper.getMainLooper())

  override fun onChange(change: RetainInstanceChange) {
    when (change) {
      is CountChanged -> onCountChanged(change.retainedCount)
      Reset -> onReset()
    }
  }

  private fun onCountChanged(retainedCount: Int) {
    // Do nothing if count hasn't changed
    if (retainedCount == lastRetainedCount) return

    // Remember new retained count
    lastRetainedCount = retainedCount

    // Construct message: either about dumping a heap or on when it will happen
    val retainedVisibleThreshold = LeakCanary.config.retainedVisibleThreshold

    // Don't display any toast if single retain instance triggers a heap dump
    if (retainedVisibleThreshold == 1) return

    var text = "Retained objects: $retainedCount."
    text +=
      if (retainedCount >= retainedVisibleThreshold) {
        " Dumping the heap..."
      } else {
        "Heap dump threshold is $retainedVisibleThreshold" +
            "\nBackground the app to trigger heap dump immediately"
      }

    // Post the Toast into main thread and wrap it with try-catch in case Toast crashes (it happens)
    handler.post {
      try {
        Toast.makeText(application, text, Toast.LENGTH_LONG).show()
      } catch (exception: Exception) {
        // Toasts are prone to crashing, ignore
      }
{'fraction_non_alphanumeric': 0.0639723801787165, 'fraction_numerical': 0.0016246953696181965, 'mean_word_length': 3.763056092843327, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7594487', 'n_tokens_mistral': 1278, 'n_tokens_neox': 1228, 'n_words': 490}