From a1344f82c9eda9462ae94b37c8e3bfcd459e2020 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sun, 11 Jul 2021 21:35:17 +0900 Subject: [PATCH] Convert CrashHandler to kotlin --- .../java/awaisomereport/CrashHandler.kt | 64 +++---------- .../java/awaisomereport/CrashHandler.kt | 89 +++++++++---------- .../main/java/awaisomereport/CrashReporter.kt | 2 +- 3 files changed, 54 insertions(+), 101 deletions(-) diff --git a/app/src/fdroid/java/awaisomereport/CrashHandler.kt b/app/src/fdroid/java/awaisomereport/CrashHandler.kt index 2f27d1c3..02f28fa8 100644 --- a/app/src/fdroid/java/awaisomereport/CrashHandler.kt +++ b/app/src/fdroid/java/awaisomereport/CrashHandler.kt @@ -1,56 +1,14 @@ -package awaisomereport; +package awaisomereport -import android.app.Application; +import android.app.Application -import androidx.annotation.NonNull; - -public class CrashHandler implements ICrashHandler { - private static final String TAG = CrashHandler.class.getSimpleName(); - - private final Application application; - - public CrashHandler(@NonNull final Application application) { - this.application = application; +class CrashHandler(private val application: Application) : ICrashHandler { + override fun uncaughtException( + t: Thread, + exception: Throwable, + defaultExceptionHandler: Thread.UncaughtExceptionHandler + ) { + CrashReporterHelper.startErrorReporterActivity(application, exception) + defaultExceptionHandler.uncaughtException(t, exception) } - - @Override - public void uncaughtException(@NonNull final Thread t, - @NonNull final Throwable exception, - @NonNull final Thread.UncaughtExceptionHandler defaultEH) { - CrashReporterHelper.startErrorReporterActivity(application, exception); - // zipLogs(); - defaultEH.uncaughtException(t, exception); - } - - // public synchronized CrashReporter zipLogs() { - // final File logDir = Utils.logCollector != null ? Utils.logCollector.getLogDir() : - // new File(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? application.getDataDir() : application.getFilesDir(), "crashlogs"); - // - // try (final FileOutputStream fos = new FileOutputStream(crashLogsZip); - // final ZipOutputStream zos = new ZipOutputStream(fos)) { - // - // final File[] files = logDir.listFiles(); - // - // if (files != null) { - // zos.setLevel(5); - // byte[] buffer; - // for (final File file : files) { - // if (file != null && file.length() > 0) { - // buffer = new byte[1024]; - // try (final FileInputStream fis = new FileInputStream(file)) { - // zos.putNextEntry(new ZipEntry(file.getName())); - // int length; - // while ((length = fis.read(buffer)) > 0) zos.write(buffer, 0, length); - // zos.closeEntry(); - // } - // } - // } - // } - // - // } catch (final Exception e) { - // if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e); - // } - // - // return this; - // } -} +} \ No newline at end of file diff --git a/app/src/github/java/awaisomereport/CrashHandler.kt b/app/src/github/java/awaisomereport/CrashHandler.kt index cd28eaa2..66484dce 100644 --- a/app/src/github/java/awaisomereport/CrashHandler.kt +++ b/app/src/github/java/awaisomereport/CrashHandler.kt @@ -1,59 +1,54 @@ -package awaisomereport; +package awaisomereport -import android.app.Application; +import android.app.Application +import awais.instagrabber.BuildConfig +import awais.instagrabber.fragments.settings.PreferenceKeys +import awais.instagrabber.utils.Utils +import io.sentry.SentryEvent +import io.sentry.SentryLevel +import io.sentry.SentryOptions.BeforeSendCallback +import io.sentry.android.core.SentryAndroid +import io.sentry.android.core.SentryAndroidOptions -import androidx.annotation.NonNull; +class CrashHandler(private val application: Application) : ICrashHandler { + private var enabled = false -import awais.instagrabber.BuildConfig; -import awais.instagrabber.fragments.settings.PreferenceKeys; -import io.sentry.SentryLevel; -import io.sentry.android.core.SentryAndroid; -import io.sentry.protocol.Contexts; -import io.sentry.protocol.Device; - -import static awais.instagrabber.utils.Utils.settingsHelper; - -public class CrashHandler implements ICrashHandler { - private static final String TAG = CrashHandler.class.getSimpleName(); - - private final Application application; - private final boolean enabled; - - public CrashHandler(@NonNull final Application application) { - this.application = application; - if (!settingsHelper.hasPreference(PreferenceKeys.PREF_ENABLE_SENTRY)) { + init { + enabled = if (!Utils.settingsHelper.hasPreference(PreferenceKeys.PREF_ENABLE_SENTRY)) { // disabled by default (change to true if we need enabled by default) - enabled = false; + false } else { - enabled = settingsHelper.getBoolean(PreferenceKeys.PREF_ENABLE_SENTRY); + Utils.settingsHelper.getBoolean(PreferenceKeys.PREF_ENABLE_SENTRY) + } + if (enabled) { + SentryAndroid.init(application) { options: SentryAndroidOptions -> + options.dsn = BuildConfig.dsn + options.setDiagnosticLevel(SentryLevel.ERROR) + options.beforeSend = BeforeSendCallback { event: SentryEvent, _: Any? -> + // Removing unneeded info from event + event.contexts.device?.apply { + name = null + timezone = null + isCharging = null + bootTime = null + freeStorage = null + batteryTemperature = null + } + event + } + } } - if (!enabled) return; - SentryAndroid.init(application, options -> { - options.setDsn(BuildConfig.dsn); - options.setDiagnosticLevel(SentryLevel.ERROR); - options.setBeforeSend((event, hint) -> { - // Removing unneeded info from event - final Contexts contexts = event.getContexts(); - final Device device = contexts.getDevice(); - device.setName(null); - device.setTimezone(null); - device.setCharging(null); - device.setBootTime(null); - device.setFreeStorage(null); - device.setBatteryTemperature(null); - return event; - }); - }); } - @Override - public void uncaughtException(@NonNull final Thread t, - @NonNull final Throwable exception, - @NonNull final Thread.UncaughtExceptionHandler defaultEH) { + override fun uncaughtException( + t: Thread, + exception: Throwable, + defaultExceptionHandler: Thread.UncaughtExceptionHandler + ) { // When enabled, Sentry auto captures unhandled exceptions if (!enabled) { - CrashReporterHelper.startErrorReporterActivity(application, exception); + CrashReporterHelper.startErrorReporterActivity(application, exception) } - defaultEH.uncaughtException(t, exception); + defaultExceptionHandler.uncaughtException(t, exception) } -} +} \ No newline at end of file diff --git a/app/src/main/java/awaisomereport/CrashReporter.kt b/app/src/main/java/awaisomereport/CrashReporter.kt index 030298ea..e6cf782d 100755 --- a/app/src/main/java/awaisomereport/CrashReporter.kt +++ b/app/src/main/java/awaisomereport/CrashReporter.kt @@ -14,9 +14,9 @@ class CrashReporter private constructor(application: Application) : Thread.Uncau fun start() { if (startAttempted) return + startAttempted = true defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler() Thread.setDefaultUncaughtExceptionHandler(this) - startAttempted = true } override fun uncaughtException(t: Thread, exception: Throwable) {