From 49d6f04f48b10789bb4b39f21eed917cfd60977a Mon Sep 17 00:00:00 2001 From: Adolfo Santiago Date: Sat, 25 Jun 2022 10:14:30 +0200 Subject: [PATCH] End the application when the crash happens --- .../tusky/core/logging/CrashHandler.kt | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/husky/app/src/main/java/com/keylesspalace/tusky/core/logging/CrashHandler.kt b/husky/app/src/main/java/com/keylesspalace/tusky/core/logging/CrashHandler.kt index cd0cdd6..e2a1942 100644 --- a/husky/app/src/main/java/com/keylesspalace/tusky/core/logging/CrashHandler.kt +++ b/husky/app/src/main/java/com/keylesspalace/tusky/core/logging/CrashHandler.kt @@ -25,6 +25,8 @@ import android.content.Intent import android.net.Uri import android.os.Build.VERSION import android.os.Bundle +import android.os.Process +import android.os.Process.killProcess import androidx.core.content.FileProvider import com.keylesspalace.tusky.BuildConfig import com.keylesspalace.tusky.R @@ -34,6 +36,7 @@ import java.io.FileOutputStream import java.io.IOException import java.lang.Thread.UncaughtExceptionHandler import javax.inject.Inject +import kotlin.system.exitProcess import timber.log.Timber class CrashHandler @Inject constructor( @@ -66,7 +69,13 @@ class CrashHandler @Inject constructor( } catch(e: IOException) { Timber.e("CrashHandler Exception[${e.message}]") } finally { - lastActivity?.finish() ?: defaultHandler?.uncaughtException(thread, throwable) + lastActivity?.let { activity -> + killApp { + activity.finish() + } + } ?: killApp { + defaultHandler?.uncaughtException(thread, throwable) + } } } @@ -137,6 +146,13 @@ class CrashHandler @Inject constructor( ) } + private fun killApp(listener: () -> Unit = {}) { + listener() + + killProcess(Process.myPid()) + exitProcess(10) + } + fun setAsDefaultHandler() { val handler = defaultHandler?.let { this@CrashHandler