You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
5.0 KiB
Java
100 lines
5.0 KiB
Java
1 year ago
|
package com.fox2code.mmm.utils.sentry;
|
||
2 years ago
|
|
||
1 year ago
|
import android.annotation.SuppressLint;
|
||
1 year ago
|
import android.content.Context;
|
||
|
import android.content.SharedPreferences;
|
||
2 years ago
|
import android.net.Uri;
|
||
2 years ago
|
|
||
|
import com.fox2code.mmm.MainApplication;
|
||
2 years ago
|
import com.fox2code.mmm.androidacy.AndroidacyUtil;
|
||
|
|
||
1 year ago
|
import java.util.Objects;
|
||
2 years ago
|
|
||
|
import io.sentry.Sentry;
|
||
|
import io.sentry.android.core.SentryAndroid;
|
||
|
import io.sentry.android.fragment.FragmentLifecycleIntegration;
|
||
|
|
||
|
public class SentryMain {
|
||
|
public static final boolean IS_SENTRY_INSTALLED = true;
|
||
|
private static final String TAG = "SentryMain";
|
||
1 year ago
|
private static boolean sentryEnabled = false;
|
||
2 years ago
|
|
||
2 years ago
|
/**
|
||
|
* Initialize Sentry
|
||
|
* Sentry is used for crash reporting and performance monitoring. The SDK is explcitly configured not to send PII, and server side scrubbing of sensitive data is enabled (which also removes IP addresses)
|
||
|
*/
|
||
1 year ago
|
@SuppressLint({"RestrictedApi", "UnspecifiedImmutableFlag"})
|
||
2 years ago
|
public static void initialize(final MainApplication mainApplication) {
|
||
1 year ago
|
// If first_launch pref is not false, refuse to initialize Sentry
|
||
|
SharedPreferences sharedPreferences = MainApplication.getSharedPreferences();
|
||
1 year ago
|
if (sharedPreferences.getBoolean("first_time_user", true)) {
|
||
1 year ago
|
return;
|
||
|
}
|
||
1 year ago
|
Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
|
||
1 year ago
|
SharedPreferences.Editor editor = mainApplication.getSharedPreferences("sentry", Context.MODE_PRIVATE).edit();
|
||
1 year ago
|
editor.putString("lastExitReason", "crash");
|
||
1 year ago
|
editor.putLong("lastExitTime", System.currentTimeMillis());
|
||
1 year ago
|
editor.apply();
|
||
|
// If we just let the default uncaught exception handler handle the
|
||
|
// exception, the app will hang and never close.
|
||
|
// So we need to kill the app ourselves.
|
||
|
System.exit(1);
|
||
|
});
|
||
2 years ago
|
SentryAndroid.init(mainApplication, options -> {
|
||
|
// If crash reporting is disabled, stop here.
|
||
|
if (!MainApplication.isCrashReportingEnabled()) {
|
||
|
options.setDsn("");
|
||
|
} else {
|
||
1 year ago
|
sentryEnabled = true; // Set sentry state to enabled
|
||
2 years ago
|
options.addIntegration(new FragmentLifecycleIntegration(mainApplication, true, true));
|
||
1 year ago
|
options.setCollectAdditionalContext(true);
|
||
|
options.setAttachThreads(true);
|
||
|
options.setAttachStacktrace(true);
|
||
|
options.setEnableNdk(true);
|
||
|
// Intercept okhttp requests to add sentry headers
|
||
|
options.addInAppInclude("com.fox2code.mmm");
|
||
1 year ago
|
options.addInAppInclude("com.fox2code.mmm.debug");
|
||
|
options.addInAppInclude("com.fox2code.mmm.fdroid");
|
||
1 year ago
|
options.addInAppExclude("com.fox2code.mmm.utils.sentry.SentryMain");
|
||
2 years ago
|
// Sentry sends ABSOLUTELY NO Personally Identifiable Information (PII) by default.
|
||
|
// Already set to false by default, just set it again to make peoples feel safer.
|
||
|
options.setSendDefaultPii(false);
|
||
2 years ago
|
// It just tell if sentry should ping the sentry dsn to tell the app is running. Useful for performance and profiling.
|
||
|
options.setEnableAutoSessionTracking(true);
|
||
2 years ago
|
// A screenshot of the app itself is only sent if the app crashes, and it only shows the last activity
|
||
|
// Add a callback that will be used before the event is sent to Sentry.
|
||
|
// With this callback, you can modify the event or, when returning null, also discard the event.
|
||
|
options.setBeforeSend((event, hint) -> {
|
||
1 year ago
|
// Save lastEventId to private shared preferences
|
||
1 year ago
|
SharedPreferences sentryPrefs = MainApplication.getINSTANCE().getSharedPreferences("sentry", Context.MODE_PRIVATE);
|
||
1 year ago
|
String lastEventId = Objects.requireNonNull(event.getEventId()).toString();
|
||
1 year ago
|
SharedPreferences.Editor editor = sentryPrefs.edit();
|
||
1 year ago
|
editor.putString("lastEventId", lastEventId);
|
||
|
editor.apply();
|
||
|
return event;
|
||
2 years ago
|
});
|
||
1 year ago
|
// Filter breadrcrumb content from crash report.
|
||
|
options.setBeforeBreadcrumb((breadcrumb, hint) -> {
|
||
|
String url = (String) breadcrumb.getData("url");
|
||
|
if (url == null || url.isEmpty()) return breadcrumb;
|
||
1 year ago
|
if ("cloudflare-dns.com".equals(Uri.parse(url).getHost())) return null;
|
||
1 year ago
|
if (AndroidacyUtil.isAndroidacyLink(url)) {
|
||
|
breadcrumb.setData("url", AndroidacyUtil.hideToken(url));
|
||
|
}
|
||
|
return breadcrumb;
|
||
|
});
|
||
2 years ago
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
1 year ago
|
public static void addSentryBreadcrumb(SentryBreadcrumb sentryBreadcrumb) {
|
||
2 years ago
|
if (MainApplication.isCrashReportingEnabled()) {
|
||
|
Sentry.addBreadcrumb(sentryBreadcrumb.breadcrumb);
|
||
|
}
|
||
|
}
|
||
1 year ago
|
|
||
|
public static boolean isSentryEnabled() {
|
||
|
return sentryEnabled;
|
||
|
}
|
||
2 years ago
|
}
|