1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-12-22 21:16:57 +00:00

Convert CrashHandler to kotlin

This commit is contained in:
Ammar Githam 2021-07-11 21:35:17 +09:00
parent 6c26bea23f
commit a1344f82c9
3 changed files with 54 additions and 101 deletions

View File

@ -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;
// }
}
}

View File

@ -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)
}
}
}

View File

@ -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) {