|
1 | 1 | package live.mehiz.mpvkt.presentation.crash |
2 | 2 |
|
| 3 | +import android.app.Activity |
3 | 4 | import android.content.ClipData |
4 | 5 | import android.content.ClipboardManager |
5 | 6 | import android.content.Context |
@@ -80,55 +81,72 @@ class CrashActivity : ComponentActivity() { |
80 | 81 | } |
81 | 82 | } |
82 | 83 |
|
83 | | - private fun collectLogcat(): String { |
84 | | - val process = Runtime.getRuntime() |
85 | | - val reader = BufferedReader(InputStreamReader(process.exec("logcat -d").inputStream)) |
86 | | - val logcat = StringBuilder() |
87 | | - // reader.lines() looks much nicer so why not use it on devices that support it? |
88 | | - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
89 | | - reader.lines().forEach(logcat::appendLine) |
90 | | - } else { |
91 | | - reader.readLines().forEach(logcat::appendLine) |
| 84 | + companion object { |
| 85 | + suspend fun shareLogs( |
| 86 | + deviceInfo: String, |
| 87 | + exceptionString: String? = null, |
| 88 | + logcat: String, |
| 89 | + activity: Activity, |
| 90 | + ) { |
| 91 | + withContext(NonCancellable) { |
| 92 | + val file = File(activity.cacheDir, "mpvKt_logs.txt") |
| 93 | + if (file.exists()) file.delete() |
| 94 | + file.createNewFile() |
| 95 | + file.appendText(concatLogs(deviceInfo, exceptionString, logcat)) |
| 96 | + val uri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", file) |
| 97 | + val intent = Intent(Intent.ACTION_SEND) |
| 98 | + intent.putExtra(Intent.EXTRA_STREAM, uri) |
| 99 | + intent.clipData = ClipData.newRawUri(null, uri) |
| 100 | + intent.type = "text/plain" |
| 101 | + intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 102 | + activity.startActivity( |
| 103 | + Intent.createChooser(intent, activity.getString(R.string.crash_screen_share)), |
| 104 | + ) |
| 105 | + } |
92 | 106 | } |
93 | | - // clear logcat so it doesn't pollute subsequent crashes |
94 | | - process.exec("logcat -c") |
95 | | - return logcat.toString() |
96 | | - } |
97 | 107 |
|
98 | | - private fun concatLogs( |
99 | | - deviceInfo: String, |
100 | | - crashLogs: String, |
101 | | - logcat: String, |
102 | | - ): String { |
103 | | - return """ |
104 | | - $deviceInfo |
105 | | - |
106 | | - Exception: |
107 | | - $crashLogs |
108 | | - |
109 | | - Logcat: |
110 | | - $logcat |
111 | | - """.trimIndent() |
112 | | - } |
| 108 | + fun concatLogs( |
| 109 | + deviceInfo: String, |
| 110 | + crashLogs: String? = null, |
| 111 | + logcat: String, |
| 112 | + ): String { |
| 113 | + return StringBuilder().apply { |
| 114 | + appendLine(deviceInfo) |
| 115 | + appendLine() |
| 116 | + if (!crashLogs.isNullOrBlank()) { |
| 117 | + appendLine("Exception:") |
| 118 | + appendLine(crashLogs) |
| 119 | + appendLine() |
| 120 | + } |
| 121 | + appendLine("Logcat:") |
| 122 | + appendLine(logcat) |
| 123 | + }.toString() |
| 124 | + } |
113 | 125 |
|
114 | | - private suspend fun dumpLogs( |
115 | | - exceptionString: String, |
116 | | - logcat: String, |
117 | | - ) { |
118 | | - withContext(NonCancellable) { |
119 | | - val file = File(applicationContext.cacheDir, "mpvKt_logs.txt") |
120 | | - if (file.exists()) file.delete() |
121 | | - file.createNewFile() |
122 | | - file.appendText(concatLogs(collectDeviceInfo(), exceptionString, logcat)) |
123 | | - val uri = FileProvider.getUriForFile(applicationContext, BuildConfig.APPLICATION_ID + ".provider", file) |
124 | | - val intent = Intent(Intent.ACTION_SEND) |
125 | | - intent.putExtra(Intent.EXTRA_STREAM, uri) |
126 | | - intent.clipData = ClipData.newRawUri(null, uri) |
127 | | - intent.type = "text/plain" |
128 | | - intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION |
129 | | - this@CrashActivity.startActivity( |
130 | | - Intent.createChooser(intent, applicationContext.getString(R.string.crash_screen_share)), |
131 | | - ) |
| 126 | + fun collectLogcat(): String { |
| 127 | + val process = Runtime.getRuntime() |
| 128 | + val reader = BufferedReader(InputStreamReader(process.exec("logcat -d").inputStream)) |
| 129 | + val logcat = StringBuilder() |
| 130 | + // reader.lines() looks much nicer so why not use it on devices that support it? |
| 131 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 132 | + reader.lines().forEach(logcat::appendLine) |
| 133 | + } else { |
| 134 | + reader.readLines().forEach(logcat::appendLine) |
| 135 | + } |
| 136 | + return logcat.toString() |
| 137 | + } |
| 138 | + |
| 139 | + fun collectDeviceInfo(): String { |
| 140 | + return """ |
| 141 | + App version: ${BuildConfig.VERSION_NAME} (${BuildConfig.GIT_SHA}) |
| 142 | + Android version: ${Build.VERSION.RELEASE} (${Build.VERSION.SDK_INT}) |
| 143 | + Device brand: ${Build.BRAND} |
| 144 | + Device manufacturer: ${Build.MANUFACTURER} |
| 145 | + Device model: ${Build.MODEL} (${Build.DEVICE}) |
| 146 | + MPV version: ${Utils.VERSIONS.mpv} |
| 147 | + ffmpeg version: ${Utils.VERSIONS.ffmpeg} |
| 148 | + libplacebo version: ${Utils.VERSIONS.libPlacebo} |
| 149 | + """.trimIndent() |
132 | 150 | } |
133 | 151 | } |
134 | 152 |
|
@@ -162,7 +180,7 @@ class CrashActivity : ComponentActivity() { |
162 | 180 | Button( |
163 | 181 | onClick = { |
164 | 182 | scope.launch(Dispatchers.IO) { |
165 | | - dumpLogs(exceptionString, logcat) |
| 183 | + shareLogs(collectDeviceInfo(), exceptionString, logcat, this@CrashActivity) |
166 | 184 | } |
167 | 185 | }, |
168 | 186 | modifier = Modifier.weight(1f), |
@@ -252,16 +270,3 @@ class CrashActivity : ComponentActivity() { |
252 | 270 | } |
253 | 271 | } |
254 | 272 | } |
255 | | - |
256 | | -fun collectDeviceInfo(): String { |
257 | | - return """ |
258 | | - App version: ${BuildConfig.VERSION_NAME} (${BuildConfig.GIT_SHA}) |
259 | | - Android version: ${Build.VERSION.RELEASE} (${Build.VERSION.SDK_INT}) |
260 | | - Device brand: ${Build.BRAND} |
261 | | - Device manufacturer: ${Build.MANUFACTURER} |
262 | | - Device model: ${Build.MODEL} (${Build.DEVICE}) |
263 | | - MPV version: ${Utils.VERSIONS.mpv} |
264 | | - ffmpeg version: ${Utils.VERSIONS.ffmpeg} |
265 | | - libplacebo version: ${Utils.VERSIONS.libPlacebo} |
266 | | - """.trimIndent() |
267 | | -} |
|
0 commit comments