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

Code before:
  intent.type = "application/octet-stream"
  intent.putExtra(Intent.EXTRA_STREAM, uri)
  activity.startActivity(
      Intent.createChooser(intent, resources.getString(R.string.leak_canary_share_with))
  )
}

internal fun View.shareToStackOverflow(content: String) {
  val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
  // AsyncTask was needed here due to setPrimaryClip making a disk write which
  // violated StrictMode if on the main thread
  AsyncTask.execute {
    clipboard.setPrimaryClip(
        ClipData.newPlainText(
            context.getString(R.string.leak_canary_leak_clipdata_label),
            "```\n$content```"
        )
    )
  }
  Toast.makeText(context, R.string.leak_canary_leak_copied, Toast.LENGTH_LONG)
      .show()
  val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(STACKOVERFLOW_QUESTION_URL))
  try {
    activity.startActivity(browserIntent)
  } catch (e: ActivityNotFoundException) {
    Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
  }
}

internal fun View.shareToGitHubIssue(failure: HeapAnalysisFailure) {
  val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
  // AsyncTask was needed here due to setPrimaryClip making a disk write which
  // violated StrictMode if on the main thread
  AsyncTask.execute {
    clipboard.setPrimaryClip(
        ClipData.newPlainText(
            context.getString(R.string.leak_canary_failure_clipdata_label),
            """```
          |${failure.exception}
          |Build.VERSION.SDK_INT: ${Build.VERSION.SDK_INT}
          |Build.MANUFACTURER: ${Build.MANUFACTURER}
          |LeakCanary version: ${BuildConfig.LIBRARY_VERSION}
          |Analysis duration: ${failure.analysisDurationMillis} ms
          |Heap dump file path: ${failure.heapDumpFile.absolutePath}
          |Heap dump timestamp: ${failure.createdAtTimeMillis}
          |```
        """.trimMargin()
        )
    )
  }
  Toast.makeText(context, R.string.leak_canary_failure_copied, Toast.LENGTH_LONG)

Review comment (suggestion):
We still should provide a custom error message. It's better to say that `Browser not found` than `ActivityNotFoundException: ....`


Code after:
  intent.type = "application/octet-stream"
  intent.putExtra(Intent.EXTRA_STREAM, uri)
  activity.startActivity(
      Intent.createChooser(intent, resources.getString(R.string.leak_canary_share_with))
  )
}

internal fun View.shareToStackOverflow(content: String) {
  val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
  // AsyncTask was needed here due to setPrimaryClip making a disk write which
  // violated StrictMode if on the main thread
  AsyncTask.execute {
    clipboard.setPrimaryClip(
        ClipData.newPlainText(
            context.getString(R.string.leak_canary_leak_clipdata_label),
            "```\n$content```"
        )
    )
  }
  Toast.makeText(context, R.string.leak_canary_leak_copied, Toast.LENGTH_LONG)
      .show()
  val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(STACKOVERFLOW_QUESTION_URL))
  try {
    activity.startActivity(browserIntent)
  } catch (e: ActivityNotFoundException) {
    Toast.makeText(context, R.string.leak_canary_leak_missing_browser_error, Toast.LENGTH_LONG)
            .show();
  }
}

internal fun View.shareToGitHubIssue(failure: HeapAnalysisFailure) {
  val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
  // AsyncTask was needed here due to setPrimaryClip making a disk write which
  // violated StrictMode if on the main thread
  AsyncTask.execute {
    clipboard.setPrimaryClip(
        ClipData.newPlainText(
            context.getString(R.string.leak_canary_failure_clipdata_label),
            """```
          |${failure.exception}
          |Build.VERSION.SDK_INT: ${Build.VERSION.SDK_INT}
          |Build.MANUFACTURER: ${Build.MANUFACTURER}
          |LeakCanary version: ${BuildConfig.LIBRARY_VERSION}
          |Analysis duration: ${failure.analysisDurationMillis} ms
          |Heap dump file path: ${failure.heapDumpFile.absolutePath}
          |Heap dump timestamp: ${failure.createdAtTimeMillis}
          |```
        """.trimMargin()
        )
    )
  }