Skip to content

Commit

Permalink
Merge pull request #77 from akallabeth/custom_crash_data_collector
Browse files Browse the repository at this point in the history
Implemented custom crash data collector.
  • Loading branch information
Ereza authored May 29, 2022
2 parents 0da0e89 + d74e43d commit 496e1ae
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public final class CustomActivityOnCrash {

//Extras passed to the error activity
private static final String EXTRA_CONFIG = "cat.ereza.customactivityoncrash.EXTRA_CONFIG";
private static final String EXTRA_CUSTOM_CRASH_DATA = "cat.ereza.customactivityoncrash.EXTRA_CUSTOM_CRASH_DATA";
private static final String EXTRA_STACK_TRACE = "cat.ereza.customactivityoncrash.EXTRA_STACK_TRACE";
private static final String EXTRA_ACTIVITY_LOG = "cat.ereza.customactivityoncrash.EXTRA_ACTIVITY_LOG";

Expand Down Expand Up @@ -153,6 +154,11 @@ public static void install(@Nullable final Context context) {
}
intent.putExtra(EXTRA_STACK_TRACE, stackTraceString);

CustomCrashDataCollector collector = config.getCustomCrashDataCollector();
if (collector != null) {
intent.putExtra(EXTRA_CUSTOM_CRASH_DATA, collector.onCrash(intent));
}

if (config.isTrackActivities()) {
StringBuilder activityLogStringBuilder = new StringBuilder();
while (!activityLog.isEmpty()) {
Expand Down Expand Up @@ -277,6 +283,17 @@ public static String getStackTraceFromIntent(@NonNull Intent intent) {
return intent.getStringExtra(CustomActivityOnCrash.EXTRA_STACK_TRACE);
}

/**
* Given an Intent, returns the custom collector trace extra from it.
*
* @param intent The Intent. Must not be null.
* @return The custom collector trace, or null if not provided.
*/
@Nullable
public static String getCustomCrashDataFromIntent(@NonNull Intent intent) {
return intent.getStringExtra(CustomActivityOnCrash.EXTRA_CUSTOM_CRASH_DATA);
}

/**
* Given an Intent, returns the config extra from it.
*
Expand Down Expand Up @@ -348,6 +365,13 @@ public static String getAllErrorDetailsFromIntent(@NonNull Context context, @Non
errorDetails += "\nUser actions: \n";
errorDetails += activityLog;
}

String customTrace = getCustomCrashDataFromIntent(intent);
if (customTrace != null) {
errorDetails += "\nCustom trace: \n";
errorDetails += customTrace;
}

return errorDetails;
}

Expand Down Expand Up @@ -729,4 +753,11 @@ public interface EventListener extends Serializable {

void onCloseAppFromErrorActivity();
}

/**
* Interface to be called to register a custom crash data collector
*/
public interface CustomCrashDataCollector extends Serializable {
String onCrash(Intent intent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class CaocConfig implements Serializable {
private Integer errorDrawable = null;
private Class<? extends Activity> errorActivityClass = null;
private Class<? extends Activity> restartActivityClass = null;
private CustomActivityOnCrash.CustomCrashDataCollector customCrashDataCollector = null;
private CustomActivityOnCrash.EventListener eventListener = null;

@BackgroundMode
Expand Down Expand Up @@ -129,6 +130,15 @@ public void setErrorActivityClass(@Nullable Class<? extends Activity> errorActiv
this.errorActivityClass = errorActivityClass;
}

@Nullable
public CustomActivityOnCrash.CustomCrashDataCollector getCustomCrashDataCollector() {
return customCrashDataCollector;
}

public void setCustomCrashDataCollector(@Nullable CustomActivityOnCrash.CustomCrashDataCollector collector) {
this.customCrashDataCollector = collector;
}

@Nullable
public Class<? extends Activity> getRestartActivityClass() {
return restartActivityClass;
Expand Down Expand Up @@ -165,6 +175,7 @@ public static Builder create() {
config.minTimeBetweenCrashesMs = currentConfig.minTimeBetweenCrashesMs;
config.errorDrawable = currentConfig.errorDrawable;
config.errorActivityClass = currentConfig.errorActivityClass;
config.customCrashDataCollector = currentConfig.customCrashDataCollector;
config.restartActivityClass = currentConfig.restartActivityClass;
config.eventListener = currentConfig.eventListener;

Expand Down Expand Up @@ -284,6 +295,23 @@ public Builder errorActivity(@Nullable Class<? extends Activity> errorActivityCl
return this;
}

/**
* Sets the custom error data collector class to launch when a crash occurs.
* If null, the default error activity will be used.
*
* @param collector The data collector.
* @throws IllegalArgumentException if the collector is an inner or anonymous class
*/
@NonNull
public Builder customCrashDataCollector(@Nullable CustomActivityOnCrash.CustomCrashDataCollector collector) {
if (collector != null && collector.getClass().getEnclosingClass() != null && !Modifier.isStatic(collector.getClass().getModifiers())) {
throw new IllegalArgumentException("The event listener cannot be an inner or anonymous class, because it will need to be serialized. Change it to a class of its own, or make it a static inner class.");
} else {
config.customCrashDataCollector = collector;
}
return this;
}

/**
* Sets the main activity class that the error activity must launch when a crash occurs.
* If not set or set to null, the default launch activity will be used.
Expand Down

0 comments on commit 496e1ae

Please sign in to comment.