|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2015-2026 NewPipe contributors <https://newpipe.net> |
| 3 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +package org.schabi.newpipe.error |
| 7 | + |
| 8 | +import android.content.Context |
| 9 | +import android.content.Intent |
| 10 | +import android.os.Build |
| 11 | +import android.util.Log |
| 12 | +import androidx.core.net.toUri |
| 13 | +import com.grack.nanojson.JsonWriter |
| 14 | +import org.schabi.newpipe.BuildConfig |
| 15 | +import org.schabi.newpipe.R |
| 16 | +import org.schabi.newpipe.util.Localization |
| 17 | +import org.schabi.newpipe.util.external_communication.ShareUtils |
| 18 | +import java.time.ZonedDateTime |
| 19 | +import java.time.format.DateTimeFormatter |
| 20 | + |
| 21 | +/** |
| 22 | + * Pure utility functions for building and sending error reports. |
| 23 | + * No Activity dependency — only requires a [Context]. |
| 24 | + */ |
| 25 | +object ErrorReportHelper { |
| 26 | + |
| 27 | + private val TAG = ErrorReportHelper::class.java.simpleName |
| 28 | + |
| 29 | + private const val ERROR_EMAIL_ADDRESS = "crashreport@newpipe.schabi.org" |
| 30 | + private const val ERROR_EMAIL_SUBJECT = "Exception in " |
| 31 | + const val ERROR_GITHUB_ISSUE_URL = "https://github.com/TeamNewPipe/NewPipe/issues" |
| 32 | + |
| 33 | + fun buildJson(context: Context, errorInfo: ErrorInfo, comment: String): String { |
| 34 | + try { |
| 35 | + return JsonWriter.string() |
| 36 | + .`object`() |
| 37 | + .value("user_action", errorInfo.userAction.message) |
| 38 | + .value("request", errorInfo.request) |
| 39 | + .value("content_language", getContentLanguage(context)) |
| 40 | + .value("content_country", getContentCountry(context)) |
| 41 | + .value("app_language", getAppLanguage()) |
| 42 | + .value("service", errorInfo.getServiceName()) |
| 43 | + .value("package", context.packageName) |
| 44 | + .value("version", BuildConfig.VERSION_NAME) |
| 45 | + .value("os", getOsString()) |
| 46 | + .value("time", getCurrentTimestamp()) |
| 47 | + .array("exceptions", errorInfo.stackTraces.toList()) |
| 48 | + .value("user_comment", comment) |
| 49 | + .end() |
| 50 | + .done() |
| 51 | + } catch (e: Exception) { |
| 52 | + Log.e(TAG, "Could not build json", e) |
| 53 | + } |
| 54 | + return "" |
| 55 | + } |
| 56 | + |
| 57 | + fun buildMarkdown(context: Context, errorInfo: ErrorInfo, comment: String): String { |
| 58 | + try { |
| 59 | + return buildString(1024) { |
| 60 | + if (comment.isNotEmpty()) { |
| 61 | + appendLine(comment) |
| 62 | + } |
| 63 | + |
| 64 | + appendLine("## Exception") |
| 65 | + appendLine("* __User Action:__ ${errorInfo.userAction.message}") |
| 66 | + appendLine("* __Request:__ ${errorInfo.request}") |
| 67 | + appendLine("* __Content Country:__ ${getContentCountry(context)}") |
| 68 | + appendLine("* __Content Language:__ ${getContentLanguage(context)}") |
| 69 | + appendLine("* __App Language:__ ${getAppLanguage()}") |
| 70 | + appendLine("* __Service:__ ${errorInfo.getServiceName()}") |
| 71 | + appendLine("* __Timestamp:__ ${getCurrentTimestamp()}") |
| 72 | + appendLine("* __Package:__ ${context.packageName}") |
| 73 | + appendLine("* __Version:__ ${BuildConfig.VERSION_NAME}") |
| 74 | + appendLine("* __OS:__ ${getOsString()}") |
| 75 | + |
| 76 | + if (errorInfo.stackTraces.size > 1) { |
| 77 | + append("<details><summary><b>Exceptions (") |
| 78 | + append(errorInfo.stackTraces.size) |
| 79 | + append(")</b></summary><p>\n") |
| 80 | + } |
| 81 | + |
| 82 | + errorInfo.stackTraces.forEachIndexed { index, stacktrace -> |
| 83 | + append("<details><summary><b>Crash log ") |
| 84 | + if (errorInfo.stackTraces.size > 1) { |
| 85 | + append(index + 1) |
| 86 | + } |
| 87 | + append("</b></summary><p>\n") |
| 88 | + append("\n```\n$stacktrace\n```\n") |
| 89 | + append("</details>\n") |
| 90 | + } |
| 91 | + |
| 92 | + if (errorInfo.stackTraces.size > 1) { |
| 93 | + append("</p></details>\n") |
| 94 | + } |
| 95 | + |
| 96 | + append("<hr>\n") |
| 97 | + } |
| 98 | + } catch (e: Exception) { |
| 99 | + Log.e(TAG, "Could not build markdown", e) |
| 100 | + return "" |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + fun sendErrorEmail(context: Context, errorInfo: ErrorInfo, comment: String) { |
| 105 | + val subject = "$ERROR_EMAIL_SUBJECT${context.getString(R.string.app_name)} ${BuildConfig.VERSION_NAME}" |
| 106 | + val intent = Intent(Intent.ACTION_SENDTO) |
| 107 | + .setData("mailto:".toUri()) |
| 108 | + .putExtra(Intent.EXTRA_EMAIL, arrayOf(ERROR_EMAIL_ADDRESS)) |
| 109 | + .putExtra(Intent.EXTRA_SUBJECT, subject) |
| 110 | + .putExtra(Intent.EXTRA_TEXT, buildJson(context, errorInfo, comment)) |
| 111 | + ShareUtils.openIntentInApp(context, intent) |
| 112 | + } |
| 113 | + |
| 114 | + fun shareError(context: Context, errorInfo: ErrorInfo, comment: String) { |
| 115 | + ShareUtils.shareText( |
| 116 | + context, |
| 117 | + context.getString(R.string.error_report_title), |
| 118 | + buildJson(context, errorInfo, comment) |
| 119 | + ) |
| 120 | + } |
| 121 | + |
| 122 | + fun copyForGitHub(context: Context, errorInfo: ErrorInfo, comment: String) { |
| 123 | + ShareUtils.copyToClipboard(context, buildMarkdown(context, errorInfo, comment)) |
| 124 | + } |
| 125 | + |
| 126 | + fun openGitHubIssues(context: Context) { |
| 127 | + ShareUtils.openUrlInApp(context, ERROR_GITHUB_ISSUE_URL) |
| 128 | + } |
| 129 | + |
| 130 | + fun openPrivacyPolicy(context: Context) { |
| 131 | + ShareUtils.openUrlInApp(context, context.getString(R.string.privacy_policy_url)) |
| 132 | + } |
| 133 | + |
| 134 | + private fun getContentLanguage(context: Context): String = |
| 135 | + Localization.getPreferredLocalization(context).localizationCode |
| 136 | + |
| 137 | + private fun getContentCountry(context: Context): String = |
| 138 | + Localization.getPreferredContentCountry(context).countryCode |
| 139 | + |
| 140 | + private fun getAppLanguage(): String = |
| 141 | + Localization.getAppLocale().toString() |
| 142 | + |
| 143 | + private fun getOsString(): String { |
| 144 | + val name = System.getProperty("os.name")!! |
| 145 | + val osBase = Build.VERSION.BASE_OS.ifEmpty { "Android" } |
| 146 | + return "$name $osBase ${Build.VERSION.RELEASE} - ${Build.VERSION.SDK_INT}" |
| 147 | + } |
| 148 | + |
| 149 | + private fun getCurrentTimestamp(): String = |
| 150 | + ZonedDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME) |
| 151 | +} |
| 152 | + |
0 commit comments