Created
April 17, 2020 03:31
-
-
Save esabook/7176d7bc2ad397304311b108aa942c7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.auzen.app | |
import android.app.Activity | |
import android.app.Application | |
import android.content.Intent | |
import android.os.Handler | |
import android.os.Looper | |
import androidx.appcompat.app.AlertDialog | |
import timber.log.Timber | |
import java.util.concurrent.atomic.AtomicReference | |
/** | |
* Usage: | |
* | |
* GlobalExceptionHandler.install(app, activity) | |
* | |
*/ | |
class GlobalExceptionHandler private constructor( | |
val app: Application, | |
val activity: AtomicReference<Activity> | |
) : Thread.UncaughtExceptionHandler { | |
companion object { | |
private lateinit var instance: GlobalExceptionHandler | |
fun install(app: Application, activity: AtomicReference<Activity>) { | |
instance = GlobalExceptionHandler(app, activity) | |
instance.initLooperException() | |
} | |
} | |
/** | |
* | |
*/ | |
override fun uncaughtException(t: Thread, e: Throwable) { | |
handleException(e) | |
} | |
/** | |
* | |
*/ | |
fun initLooperException() { | |
Handler(Looper.getMainLooper()).post { | |
while (true) { | |
try { | |
Looper.loop() | |
} catch (ex: Exception) { | |
handleException(ex) | |
} | |
} | |
} | |
Thread.setDefaultUncaughtExceptionHandler(this) | |
} | |
/** | |
* | |
*/ | |
fun handleException(e: Throwable) { | |
Timber.e(e) | |
activity.get()?.let { | |
Thread { | |
Looper.prepare() | |
showFatalExceptionAlert(it) | |
Looper.loop() | |
Timber.d("finish showFatalExceptionAlert()") | |
}.start() | |
} | |
} | |
private fun showFatalExceptionAlert(activity: Activity) { | |
Handler(Looper.getMainLooper()).post { | |
val alert = AlertDialog.Builder(activity) | |
.setTitle("Auzen mengalami masalah") | |
.setMessage("Silakan hubungi kami melalui kontak customer service Auzen.") | |
.setPositiveButton("Restart") { _, _ -> | |
reloadApplication(activity) | |
} | |
.setNeutralButton("Exit") { _, _ -> | |
exitApplication() | |
}.setNegativeButton("Dismiss") { d, _ -> | |
d.dismiss() | |
} | |
.create() | |
alert.show() | |
Timber.d("Finish") | |
} | |
} | |
fun exitApplication() { | |
android.os.Process.killProcess(android.os.Process.myPid()) | |
System.exit(0) | |
Runtime.getRuntime().exit(0) | |
Timber.d("Finish") | |
} | |
fun reloadApplication(activity: Activity) { | |
val defaultIntent = | |
app.packageManager.getLaunchIntentForPackage(app.packageName) | |
defaultIntent?.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | |
activity.startActivity(defaultIntent) | |
activity.finish() | |
Runtime.getRuntime().exit(0) // Kill kill kill! | |
Timber.d("Finish") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment