11package com.getcode.utils
22
33import android.content.Context
4+ import android.content.pm.PackageManager
5+ import android.os.Build
46import android.util.Log
7+ import com.bugsnag.android.Bugsnag
58import timber.log.Timber
69import java.io.File
710import java.io.FileOutputStream
811import java.io.OutputStreamWriter
912import java.time.Instant
1013import java.time.ZoneId
1114import java.time.format.DateTimeFormatter
15+ import java.util.Locale
16+ import java.util.TimeZone
1217
1318class FileTree (
1419 context : Context ,
@@ -17,7 +22,9 @@ class FileTree(
1722
1823 private val traceDir = File (context.filesDir, " traces" ).apply { mkdirs() }
1924 private val logFile = File (traceDir, " trace.log" )
25+ private val exportFile = File (traceDir, " trace_export.log" )
2026 private val lock = Any ()
27+ private val appContext: Context = context.applicationContext
2128
2229 private val formatter = DateTimeFormatter .ISO_LOCAL_DATE_TIME
2330 .withZone(ZoneId .systemDefault())
@@ -65,11 +72,58 @@ class FileTree(
6572 }
6673 }
6774
68- fun getLogFile (): File ? = if (logFile.exists() && logFile.length() > 0 ) logFile else null
75+ fun getLogFile (): File ? {
76+ if (! logFile.exists() || logFile.length() == 0L ) return null
77+ synchronized(lock) {
78+ exportFile.delete()
79+ exportFile.writeText(buildDeviceHeader(appContext))
80+ logFile.inputStream().use { input ->
81+ FileOutputStream (exportFile, true ).use { output ->
82+ input.copyTo(output)
83+ }
84+ }
85+ }
86+ return exportFile
87+ }
6988
7089 fun clearLogs () {
7190 synchronized(lock) {
7291 logFile.delete()
92+ exportFile.delete()
7393 }
7494 }
7595}
96+
97+ @Suppress(" DEPRECATION" )
98+ private fun buildDeviceHeader (context : Context ): String {
99+ val packageInfo = try {
100+ context.packageManager.getPackageInfo(context.packageName, 0 )
101+ } catch (_: PackageManager .NameNotFoundException ) {
102+ null
103+ }
104+
105+ val versionName = packageInfo?.versionName ? : " unknown"
106+ val versionCode = packageInfo?.let {
107+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .P ) it.longVersionCode else it.versionCode.toLong()
108+ } ? : - 1
109+
110+ val userId = if (Bugsnag .isStarted()) Bugsnag .getUser().id else null
111+
112+ return buildString {
113+ appendLine(" =" .repeat(60 ))
114+ appendLine(" DEVICE & APP INFO" )
115+ appendLine(" =" .repeat(60 ))
116+ appendLine(" App Version: $versionName ($versionCode )" )
117+ appendLine(" Package: ${context.packageName} " )
118+ appendLine(" User ID: ${userId ? : " not set" } " )
119+ appendLine(" Device: ${Build .MANUFACTURER } ${Build .MODEL } " )
120+ appendLine(" Android: ${Build .VERSION .RELEASE } (API ${Build .VERSION .SDK_INT } )" )
121+ appendLine(" Build: ${Build .DISPLAY } " )
122+ appendLine(" ABI: ${Build .SUPPORTED_ABIS .joinToString()} " )
123+ appendLine(" Locale: ${Locale .getDefault()} " )
124+ appendLine(" Timezone: ${TimeZone .getDefault().id} " )
125+ appendLine(" Exported: ${Instant .now()} " )
126+ appendLine(" =" .repeat(60 ))
127+ appendLine()
128+ }
129+ }
0 commit comments