diff --git a/HISTORY.md b/HISTORY.md index 58d3d34..e577712 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,9 @@ ## 更新日誌 +### v1.3.7.6 + +* 增加錯誤上報 + ### v1.3.7 * 時間顯示秒 diff --git a/app/src/main/java/com/lizongying/mytv0/MyTVApplication.kt b/app/src/main/java/com/lizongying/mytv0/MyTVApplication.kt index 362c29e..92e8b54 100644 --- a/app/src/main/java/com/lizongying/mytv0/MyTVApplication.kt +++ b/app/src/main/java/com/lizongying/mytv0/MyTVApplication.kt @@ -63,6 +63,8 @@ class MyTVApplication : Application() { shouldHeight = height shouldWidth = (height * 16.0 / 9.0).toInt() } + + Thread.setDefaultUncaughtExceptionHandler(MyTVExceptionHandler(this)) } fun getDisplayMetrics(): DisplayMetrics { diff --git a/app/src/main/java/com/lizongying/mytv0/MyTVExceptionHandler.kt b/app/src/main/java/com/lizongying/mytv0/MyTVExceptionHandler.kt new file mode 100644 index 0000000..8f42b37 --- /dev/null +++ b/app/src/main/java/com/lizongying/mytv0/MyTVExceptionHandler.kt @@ -0,0 +1,75 @@ +package com.lizongying.mytv0 + +import android.content.Context +import android.os.Build +import android.util.Log +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.RequestBody.Companion.toRequestBody +import kotlin.system.exitProcess + +class MyTVExceptionHandler(val context: Context) : Thread.UncaughtExceptionHandler { + override fun uncaughtException(t: Thread, e: Throwable) { + val crashInfo = + "APP: ${context.appVersionName}, PRODUCT: ${Build.PRODUCT}, DEVICE: ${Build.DEVICE}, SUPPORTED_ABIS: ${Build.SUPPORTED_ABIS.joinToString()}, BOARD: ${Build.BOARD}, MANUFACTURER: ${Build.MANUFACTURER}, MODEL: ${Build.MODEL}, VERSION: ${Build.VERSION.SDK_INT}\nThread: ${t.name}\nException: ${e.message}\nStackTrace: ${ + Log.getStackTraceString( + e + ) + }\n" + + CoroutineScope(Dispatchers.IO).launch { + saveCrashInfoToFile(crashInfo) + + withContext(Dispatchers.Main) { + android.os.Process.killProcess(android.os.Process.myPid()) + exitProcess(1) + } + } + } + + private suspend fun saveCrashInfoToFile(crashInfo: String) { + if (isLimit()) { + Log.e(TAG, crashInfo) + } else { + try { + saveLog(crashInfo) + } catch (e: Exception) { + e.printStackTrace() + } + } + } + + private fun isLimit(): Boolean { + if (context.appVersionName != SP.version) { + SP.version = context.appVersionName + SP.logTimes = SP.DEFAULT_LOG_TIMES + return false + } else { + SP.logTimes-- + return SP.logTimes < 0 + } + } + + private suspend fun saveLog(crashInfo: String) { + withContext(Dispatchers.IO) { + val client = okhttp3.OkHttpClient.Builder().build() + val request = okhttp3.Request.Builder() + .url("https://lyrics.run/my-tv-0/v1/log") + .method("POST", crashInfo.toRequestBody("text/plain".toMediaType())) + .build() + try { + client.newCall(request).execute() + Log.i(TAG, "log success") + } catch (e: Exception) { + e.printStackTrace() + } + } + } + + companion object { + private const val TAG = "MyTVException" + } +} \ No newline at end of file diff --git a/app/src/main/java/com/lizongying/mytv0/SP.kt b/app/src/main/java/com/lizongying/mytv0/SP.kt index 74803b3..1edb8f8 100644 --- a/app/src/main/java/com/lizongying/mytv0/SP.kt +++ b/app/src/main/java/com/lizongying/mytv0/SP.kt @@ -45,12 +45,17 @@ object SP { private const val KEY_EPG = "epg" + private const val KEY_VERSION = "version" + + private const val KEY_LOG_TIMES = "log_times" + const val DEFAULT_CONFIG_URL = "" const val DEFAULT_EPG = "https://live.fanmingming.com/e.xml" const val DEFAULT_CHANNEL = 0 const val DEFAULT_SHOW_ALL_CHANNELS = false const val DEFAULT_COMPACT_MENU = true const val DEFAULT_DISPLAY_SECONDS = false + const val DEFAULT_LOG_TIMES = 10 private lateinit var sp: SharedPreferences @@ -151,4 +156,12 @@ object SP { var epg: String? get() = sp.getString(KEY_EPG, DEFAULT_EPG) set(value) = sp.edit().putString(KEY_EPG, value).apply() + + var version: String? + get() = sp.getString(KEY_VERSION, "") + set(value) = sp.edit().putString(KEY_VERSION, value).apply() + + var logTimes: Int + get() = sp.getInt(KEY_LOG_TIMES, DEFAULT_LOG_TIMES) + set(value) = sp.edit().putInt(KEY_LOG_TIMES, value).apply() } \ No newline at end of file diff --git a/app/src/main/java/com/lizongying/mytv0/Utils.kt b/app/src/main/java/com/lizongying/mytv0/Utils.kt index 0dfa712..59f1bb3 100644 --- a/app/src/main/java/com/lizongying/mytv0/Utils.kt +++ b/app/src/main/java/com/lizongying/mytv0/Utils.kt @@ -84,7 +84,7 @@ object Utils { } init { - CoroutineScope(Dispatchers.IO).launch(Dispatchers.IO) { + CoroutineScope(Dispatchers.IO).launch { init() } } diff --git a/version.json b/version.json index ebded80..5f11bd9 100644 --- a/version.json +++ b/version.json @@ -1 +1 @@ -{"version_code": 16975621, "version_name": "v1.3.7.5"} +{"version_code": 16975622, "version_name": "v1.3.7.6"}