Repository: square/leakcanary
File: leakcanary-android-core/src/main/java/leakcanary/DefaultOnHeapAnalyzedListener.kt (Kotlin)

Code before:
        R.id.leak_canary_notification_analysis_result,
        LEAKCANARY_MAX
    )
  }

  /**
   * Android TV devices do not have notifications, therefore the only easy and non-invasive way
   * to communicate with user is via Toast messages. These are used just to grab user attention and
   * to direct them to Logcat where a much more detailed report will be printed.
   */
  private fun showToast(heapAnalysis: HeapAnalysis) {
    // Post the Toast into main thread and wrap it with try-catch in case Toast crashes (it happens)
    handler.post {
      val toastText: String = when (heapAnalysis) {
        is HeapAnalysisSuccess -> {
          val leakTypeCount = heapAnalysis.applicationLeaks.size + heapAnalysis.libraryLeaks.size
          application.getString(
              R.string.leak_canary_tv_analysis_success,
              leakTypeCount,
              heapAnalysis.libraryLeaks.size
          )
        }
        is HeapAnalysisFailure -> application.getString(R.string.leak_canary_tv_analysis_failure)
      }
      Toast.makeText(
          application,
          toastText,
          Toast.LENGTH_LONG
      ).show()
    }
  }

  /**
   * Android TV with API 26+ has a bug where launcher icon doesn't appear, so users won't know how
   * to launch LeakCanary Activity.
   * This method would print an adb command that launched LeakCanary into the logcat
   */
  private fun printIntentInfo() {
    val leakClass = LeakActivity::class.java
    val message = """====================================
ANDROID TV LAUNCH INTENT
====================================
Run the following adb command to display the list of leaks:

adb shell am start -n "${application.packageName}/${leakClass.name.replace(leakClass.simpleName, "LeakLauncherActivity")}"
//===================================="""
    SharkLog.d { message }
  }

  companion object {
    fun create(): OnHeapAnalyzedListener =

Review comment (bug):
See leakcanary.internal.AndroidHeapDumper#showToast

Toasting when no activity is resumed may lead to a crash.

Code after:
        R.id.leak_canary_notification_analysis_result,
        LEAKCANARY_MAX
    )
  }

  /**
   * Android TV devices do not have notifications, therefore the only easy and non-invasive way
   * to communicate with user is via Toast messages. These are used just to grab user attention and
   * to direct them to Logcat where a much more detailed report will be printed.
   */
  private fun showToast(heapAnalysis: HeapAnalysis) {
    mainHandler.post {
      val resumedActivity = InternalLeakCanary.resumedActivity ?: return@post
      val message: String = when (heapAnalysis) {
        is HeapAnalysisSuccess -> {
          application.getString(
              R.string.leak_canary_tv_analysis_success,
              heapAnalysis.applicationLeaks.size,
              heapAnalysis.libraryLeaks.size
          )
        }
        is HeapAnalysisFailure -> application.getString(R.string.leak_canary_tv_analysis_failure)
      }
      TvToast.makeText(resumedActivity, message).show()
    }
  }

  /**
   * Android TV with API 26+ has a bug where the launcher icon doesn't appear, so users won't know how
   * to launch LeakCanary Activity.
   * This method prints an adb command that launched LeakCanary into the logcat
   */
  private fun printIntentInfo() {
    val leakClass = LeakActivity::class.java
    SharkLog.d {
      """====================================
  ANDROID TV LAUNCH INTENT
  ====================================
  Run the following adb command to display the list of leaks:
  
  adb shell am start -n "${application.packageName}/${leakClass.`package`?.name}.LeakLauncherActivity"
  ===================================="""
    }
  }

  companion object {
    fun create(): OnHeapAnalyzedListener =
      DefaultOnHeapAnalyzedListener(InternalLeakCanary.application)
  }
}