Browse Source

Convert CrashHandler to kotlin

renovate/androidx.fragment-fragment-ktx-1.x
Ammar Githam 3 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;
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;
}
@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);
package awaisomereport
import android.app.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)
} }
// 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;
import android.app.Application;
import androidx.annotation.NonNull;
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)) {
package awaisomereport
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
class CrashHandler(private val application: Application) : ICrashHandler {
private var enabled = false
init {
enabled = if (!Utils.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;
SentryAndroid.init(application, options -> {
options.setDsn(BuildConfig.dsn);
options.setDiagnosticLevel(SentryLevel.ERROR);
options.setBeforeSend((event, hint) -> {
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 // 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;
});
});
event.contexts.device?.apply {
name = null
timezone = null
isCharging = null
bootTime = null
freeStorage = null
batteryTemperature = null
}
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 // 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) {

Loading…
Cancel
Save