End the application when the crash happens

This commit is contained in:
Adolfo Santiago 2022-06-25 10:14:30 +02:00
parent 6ac7a18bf5
commit 49d6f04f48
No known key found for this signature in database
GPG key ID: 244D6F9A317B4A65

View file

@ -25,6 +25,8 @@ import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Build.VERSION import android.os.Build.VERSION
import android.os.Bundle import android.os.Bundle
import android.os.Process
import android.os.Process.killProcess
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import com.keylesspalace.tusky.BuildConfig import com.keylesspalace.tusky.BuildConfig
import com.keylesspalace.tusky.R import com.keylesspalace.tusky.R
@ -34,6 +36,7 @@ import java.io.FileOutputStream
import java.io.IOException import java.io.IOException
import java.lang.Thread.UncaughtExceptionHandler import java.lang.Thread.UncaughtExceptionHandler
import javax.inject.Inject import javax.inject.Inject
import kotlin.system.exitProcess
import timber.log.Timber import timber.log.Timber
class CrashHandler @Inject constructor( class CrashHandler @Inject constructor(
@ -66,7 +69,13 @@ class CrashHandler @Inject constructor(
} catch(e: IOException) { } catch(e: IOException) {
Timber.e("CrashHandler Exception[${e.message}]") Timber.e("CrashHandler Exception[${e.message}]")
} finally { } 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() { fun setAsDefaultHandler() {
val handler = defaultHandler?.let { val handler = defaultHandler?.let {
this@CrashHandler this@CrashHandler