feat: crash report | various small fixes

This commit is contained in:
rebelonion 2024-05-01 14:45:08 -05:00
parent 31c509f88c
commit 8a0224e6b0
12 changed files with 250 additions and 88 deletions

View file

@ -5,16 +5,20 @@ import android.content.Intent
import android.os.Build
import android.util.Log
import androidx.core.content.FileProvider
import ani.dantotsu.App
import ani.dantotsu.BuildConfig
import ani.dantotsu.connections.crashlytics.CrashlyticsInterface
import ani.dantotsu.others.CrashActivity
import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.snackString
import ani.dantotsu.util.Logger.getDeviceAndAppInfo
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.io.File
import java.util.Date
import java.util.concurrent.Executors
import kotlin.system.exitProcess
object Logger {
var file: File? = null
@ -25,7 +29,7 @@ object Logger {
if (!PrefManager.getVal<Boolean>(PrefName.LogToFile) || file != null) return
file = File(context.getExternalFilesDir(null), "log.txt")
if (file?.exists() == true) {
if (file!!.length() > 1024 * 1024 * 10) { // 10MB
if (file!!.length() > 1024 * 1024 * 5) { // 5 MB
file?.delete()
file?.createNewFile()
}
@ -33,52 +37,8 @@ object Logger {
file?.createNewFile()
}
file?.appendText("log started\n")
file?.appendText("date/time: ${Date()}\n")
file?.appendText("device: ${Build.MODEL}\n")
file?.appendText("os version: ${Build.VERSION.RELEASE}\n")
file?.appendText(
"app version: ${
context.packageManager.getPackageInfo(
context.packageName,
0
).versionName
}\n"
)
file?.appendText(
"app version code: ${
context.packageManager.getPackageInfo(
context.packageName,
0
).run {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
longVersionCode
else
@Suppress("DEPRECATION") versionCode
file?.appendText(getDeviceAndAppInfo(context))
}
}\n"
)
file?.appendText("sdk version: ${Build.VERSION.SDK_INT}\n")
file?.appendText("manufacturer: ${Build.MANUFACTURER}\n")
file?.appendText("brand: ${Build.BRAND}\n")
file?.appendText("product: ${Build.PRODUCT}\n")
file?.appendText("device: ${Build.DEVICE}\n")
file?.appendText("hardware: ${Build.HARDWARE}\n")
file?.appendText("host: ${Build.HOST}\n")
file?.appendText("id: ${Build.ID}\n")
file?.appendText("type: ${Build.TYPE}\n")
file?.appendText("user: ${Build.USER}\n")
file?.appendText("tags: ${Build.TAGS}\n")
file?.appendText("time: ${Build.TIME}\n")
file?.appendText("radio: ${Build.getRadioVersion()}\n")
file?.appendText("bootloader: ${Build.BOOTLOADER}\n")
file?.appendText("board: ${Build.BOARD}\n")
file?.appendText("fingerprint: ${Build.FINGERPRINT}\n")
file?.appendText("supported_abis: ${Build.SUPPORTED_ABIS.joinToString()}\n")
file?.appendText("supported_32_bit_abis: ${Build.SUPPORTED_32_BIT_ABIS.joinToString()}\n")
file?.appendText("supported_64_bit_abis: ${Build.SUPPORTED_64_BIT_ABIS.joinToString()}\n")
file?.appendText("is emulator: ${Build.FINGERPRINT.contains("generic")}\n")
file?.appendText("--------------------------------\n")
} catch (e: Exception) {
Injekt.get<CrashlyticsInterface>().logException(e)
file = null
@ -155,15 +115,81 @@ object Logger {
file?.delete()
file = null
}
fun getDeviceAndAppInfo(context: Context): String {
val pm = context.packageManager
val pkgInfo = pm.getPackageInfo(context.packageName, 0)
val versionName = pkgInfo.versionName
val versionCode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
pkgInfo.longVersionCode
} else {
@Suppress("DEPRECATION")
pkgInfo.versionCode
}
return buildString {
append("Date/time: ${Date()}\n")
append("Device: ${Build.MODEL}\n")
append("OS version: ${Build.VERSION.RELEASE}\n")
append("App version: $versionName\n")
append("App version code: $versionCode\n")
append("SDK version: ${Build.VERSION.SDK_INT}\n")
append("Manufacturer: ${Build.MANUFACTURER}\n")
append("Brand: ${Build.BRAND}\n")
append("Product: ${Build.PRODUCT}\n")
append("Device: ${Build.DEVICE}\n")
append("Hardware: ${Build.HARDWARE}\n")
append("Host: ${Build.HOST}\n")
append("ID: ${Build.ID}\n")
append("Type: ${Build.TYPE}\n")
append("User: ${Build.USER}\n")
append("Tags: ${Build.TAGS}\n")
append("Time: ${Build.TIME}\n")
append("Radio: ${Build.getRadioVersion()}\n")
append("Bootloader: ${Build.BOOTLOADER}\n")
append("Board: ${Build.BOARD}\n")
append("Fingerprint: ${Build.FINGERPRINT}\n")
append("Supported ABIs: ${Build.SUPPORTED_ABIS.joinToString()}\n")
append("Supported 32 bit ABIs: ${Build.SUPPORTED_32_BIT_ABIS.joinToString()}\n")
append("Supported 64 bit ABIs: ${Build.SUPPORTED_64_BIT_ABIS.joinToString()}\n")
append("Is emulator: ${Build.FINGERPRINT.contains("generic")}\n")
append("--------------------------------\n")
}
}
}
class FinalExceptionHandler : Thread.UncaughtExceptionHandler {
private val defaultUEH: Thread.UncaughtExceptionHandler? =
Thread.getDefaultUncaughtExceptionHandler()
private val defaultUEH = Thread.getDefaultUncaughtExceptionHandler()
private val MAX_STACK_TRACE_SIZE = 131071 //128 KB - 1
override fun uncaughtException(t: Thread, e: Throwable) {
Logger.uncaughtException(t, e)
val stackTraceString = Log.getStackTraceString(e)
Injekt.get<CrashlyticsInterface>().logException(e)
if (App.instance?.applicationContext != null) {
App.instance?.applicationContext?.let {
val report = StringBuilder()
report.append(getDeviceAndAppInfo(it))
report.append("Thread: ${t.name}\n")
report.append("Exception: ${e.message}\n")
report.append("Stack trace:\n")
report.append(stackTraceString)
val reportString = report.toString()
Logger.uncaughtException(t, Error(reportString))
val intent = Intent(it, CrashActivity::class.java)
if (reportString.length > MAX_STACK_TRACE_SIZE) {
val subStr = reportString.substring(0, MAX_STACK_TRACE_SIZE)
intent.putExtra("stackTrace", subStr)
} else intent.putExtra("stackTrace", reportString)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
it.startActivity(intent)
}
} else {
Logger.log("App context is null")
Logger.uncaughtException(t, e)
}
defaultUEH?.uncaughtException(t, e)
android.os.Process.killProcess(android.os.Process.myPid())
exitProcess(10)
}
}