Browse Source

Convert CrashHandler to kotlin

renovate/androidx.fragment-fragment-ktx-1.x
Ammar Githam 4 years ago
parent
commit
a1344f82c9
  1. 66
      app/src/fdroid/java/awaisomereport/CrashHandler.kt
  2. 91
      app/src/github/java/awaisomereport/CrashHandler.kt
  3. 2
      app/src/main/java/awaisomereport/CrashReporter.kt

66
app/src/fdroid/java/awaisomereport/CrashHandler.kt

@ -1,56 +1,14 @@
package awaisomereport; package awaisomereport
import android.app.Application
import android.app.Application; class CrashHandler(private val application: Application) : ICrashHandler {
override fun uncaughtException(
import androidx.annotation.NonNull; t: Thread,
exception: Throwable,
public class CrashHandler implements ICrashHandler { defaultExceptionHandler: Thread.UncaughtExceptionHandler
private static final String TAG = CrashHandler.class.getSimpleName(); ) {
CrashReporterHelper.startErrorReporterActivity(application, exception)
private final Application application; defaultExceptionHandler.uncaughtException(t, exception)
public CrashHandler(@NonNull final Application application) {
this.application = application;
}
@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;
// }
} }

91
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 androidx.annotation.NonNull; import awais.instagrabber.utils.Utils
import io.sentry.SentryEvent
import awais.instagrabber.BuildConfig; import io.sentry.SentryLevel
import awais.instagrabber.fragments.settings.PreferenceKeys; import io.sentry.SentryOptions.BeforeSendCallback
import io.sentry.SentryLevel; import io.sentry.android.core.SentryAndroid
import io.sentry.android.core.SentryAndroid; import io.sentry.android.core.SentryAndroidOptions
import io.sentry.protocol.Contexts; class CrashHandler(private val application: Application) : ICrashHandler {
import io.sentry.protocol.Device; private var enabled = false
init {
import static awais.instagrabber.utils.Utils.settingsHelper; enabled = if (!Utils.settingsHelper.hasPreference(PreferenceKeys.PREF_ENABLE_SENTRY)) {
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)) {
// disabled by default (change to true if we need enabled by default) // disabled by default (change to true if we need enabled by default)
enabled = false; false
} else { } else {
enabled = settingsHelper.getBoolean(PreferenceKeys.PREF_ENABLE_SENTRY); Utils.settingsHelper.getBoolean(PreferenceKeys.PREF_ENABLE_SENTRY)
} }
if (!enabled) return; if (enabled) {
SentryAndroid.init(application, options -> { SentryAndroid.init(application) { options: SentryAndroidOptions ->
options.setDsn(BuildConfig.dsn); options.dsn = BuildConfig.dsn
options.setDiagnosticLevel(SentryLevel.ERROR); options.setDiagnosticLevel(SentryLevel.ERROR)
options.setBeforeSend((event, hint) -> { options.beforeSend = BeforeSendCallback { event: SentryEvent, _: Any? ->
// Removing unneeded info from event // Removing unneeded info from event
final Contexts contexts = event.getContexts(); event.contexts.device?.apply {
final Device device = contexts.getDevice(); name = null
device.setName(null); timezone = null
device.setTimezone(null); isCharging = null
device.setCharging(null); bootTime = null
device.setBootTime(null); freeStorage = null
device.setFreeStorage(null); batteryTemperature = null
device.setBatteryTemperature(null); }
return event; event
}); }
}); }
}
} }
@Override override fun uncaughtException(
public void uncaughtException(@NonNull final Thread t, t: Thread,
@NonNull final Throwable exception, exception: Throwable,
@NonNull final Thread.UncaughtExceptionHandler defaultEH) { defaultExceptionHandler: Thread.UncaughtExceptionHandler
) {
// When enabled, Sentry auto captures unhandled exceptions // When enabled, Sentry auto captures unhandled exceptions
if (!enabled) { if (!enabled) {
CrashReporterHelper.startErrorReporterActivity(application, exception); CrashReporterHelper.startErrorReporterActivity(application, exception)
} }
defaultEH.uncaughtException(t, exception); defaultExceptionHandler.uncaughtException(t, exception)
} }
} }

2
app/src/main/java/awaisomereport/CrashReporter.kt

@ -14,9 +14,9 @@ class CrashReporter private constructor(application: Application) : Thread.Uncau
fun start() { fun start() {
if (startAttempted) return if (startAttempted) return
startAttempted = true
defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler() defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler(this) Thread.setDefaultUncaughtExceptionHandler(this)
startAttempted = true
} }
override fun uncaughtException(t: Thread, exception: Throwable) { override fun uncaughtException(t: Thread, exception: Throwable) {

|||||||
100:0
Loading…
Cancel
Save