From a3383ab4771beb123be343fd08d61a55b8135924 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sun, 11 Jul 2021 21:24:35 +0900 Subject: [PATCH] Convert CrashReporter to kotlin --- .../instagrabber/InstaGrabberApplication.kt | 3 +- .../instagrabber/utils/UpdateCheckCommon.kt | 2 +- .../main/java/awaisomereport/CrashReporter.kt | 60 +++++++++---------- 3 files changed, 29 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/InstaGrabberApplication.kt b/app/src/main/java/awais/instagrabber/InstaGrabberApplication.kt index c45f835f..38e891c6 100644 --- a/app/src/main/java/awais/instagrabber/InstaGrabberApplication.kt +++ b/app/src/main/java/awais/instagrabber/InstaGrabberApplication.kt @@ -42,8 +42,7 @@ class InstaGrabberApplication : Application() { private fun setupCrashReporter() { if (BuildConfig.DEBUG) return - CrashReporter.get(this).start() - // logCollector = new LogCollector(this); + CrashReporter.getInstance(this).start() } private fun setupCloseGuard() { diff --git a/app/src/main/java/awais/instagrabber/utils/UpdateCheckCommon.kt b/app/src/main/java/awais/instagrabber/utils/UpdateCheckCommon.kt index 926125ed..3527c1fa 100644 --- a/app/src/main/java/awais/instagrabber/utils/UpdateCheckCommon.kt +++ b/app/src/main/java/awais/instagrabber/utils/UpdateCheckCommon.kt @@ -25,7 +25,7 @@ fun showUpdateDialog( mainThread.execute { MaterialAlertDialogBuilder(context).apply { setTitle(context.getString(R.string.update_available, version)) - setNeutralButton(R.string.skip_update) { dialog: DialogInterface, which: Int -> + setNeutralButton(R.string.skip_update) { dialog: DialogInterface, _: Int -> Utils.settingsHelper.putString(Constants.SKIPPED_VERSION, version) dialog.dismiss() } diff --git a/app/src/main/java/awaisomereport/CrashReporter.kt b/app/src/main/java/awaisomereport/CrashReporter.kt index 8b61e952..030298ea 100755 --- a/app/src/main/java/awaisomereport/CrashReporter.kt +++ b/app/src/main/java/awaisomereport/CrashReporter.kt @@ -1,46 +1,40 @@ -package awaisomereport; +package awaisomereport -import android.app.Application; +import android.app.Application -import androidx.annotation.NonNull; +class CrashReporter private constructor(application: Application) : Thread.UncaughtExceptionHandler { -public final class CrashReporter implements Thread.UncaughtExceptionHandler { - private static final String TAG = CrashReporter.class.getSimpleName(); + private val crashHandler: CrashHandler? + private var startAttempted = false + private var defaultExceptionHandler: Thread.UncaughtExceptionHandler? = null - private static CrashReporter reporterInstance; - - // private final File crashLogsZip; - private final CrashHandler crashHandler; - - private boolean startAttempted = false; - private Thread.UncaughtExceptionHandler defaultEH; - - public static CrashReporter get(final Application application) { - if (reporterInstance == null) { - reporterInstance = new CrashReporter(application); - } - return reporterInstance; + init { + crashHandler = CrashHandler(application) } - private CrashReporter(@NonNull final Application application) { - crashHandler = new CrashHandler(application); - // this.crashLogsZip = new File(application.getExternalCacheDir(), "crash_logs.zip"); + fun start() { + if (startAttempted) return + defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler() + Thread.setDefaultUncaughtExceptionHandler(this) + startAttempted = true } - public void start() { - if (!startAttempted) { - defaultEH = Thread.getDefaultUncaughtExceptionHandler(); - Thread.setDefaultUncaughtExceptionHandler(this); - startAttempted = true; + override fun uncaughtException(t: Thread, exception: Throwable) { + if (crashHandler == null) { + defaultExceptionHandler?.uncaughtException(t, exception) + return } + crashHandler.uncaughtException(t, exception, defaultExceptionHandler ?: return) } - @Override - public void uncaughtException(@NonNull final Thread t, @NonNull final Throwable exception) { - if (crashHandler == null) { - defaultEH.uncaughtException(t, exception); - return; + companion object { + @Volatile + private var INSTANCE: CrashReporter? = null + + fun getInstance(application: Application): CrashReporter { + return INSTANCE ?: synchronized(this) { + CrashReporter(application).also { INSTANCE = it } + } } - crashHandler.uncaughtException(t, exception, defaultEH); } -} +} \ No newline at end of file