Skip to content

Commit

Permalink
transport-crash-report-creation (#383)
Browse files Browse the repository at this point in the history
Transport implementation of ACRA
  • Loading branch information
03fernanda-palacios authored Dec 5, 2024
1 parent f68fd88 commit c422d9d
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
1 change: 1 addition & 0 deletions BundleClient/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@
<string name="usb_device_attached_checking_for_storage_volumes">USB device attached, checking for storage volumes</string>
<string name="usb_connection_detected">USB connection detected</string>
<string name="usb_device_not_connected">USB device not connected</string>
<string name="acra_toast_text">Acra creating crash report!</string>
</resources>
11 changes: 11 additions & 0 deletions BundleTransport/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,15 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.6.1'

//dependencies for ACRA

//version
def acraVersion = '5.12.0'
//custom sender (custom destination)
implementation "ch.acra:acra-core:$acraVersion"
//here's where we could add dialog interaction (user notification and description of crash)
//annotation to load sender onto acra config
annotationProcessor("com.google.auto.service:auto-service:1.1.1")
compileOnly("com.google.auto.service:auto-service-annotations:1.1.1")
}
2 changes: 1 addition & 1 deletion BundleTransport/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


<application
android:name=".utils.acraUtils"
android:icon="@drawable/bundletransport_icon"
android:label="@string/app_name"
android:roundIcon="@drawable/bundletransport_icon"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package net.discdd.bundletransport.utils;

import android.content.Context;

import com.google.auto.service.AutoService;
import org.acra.config.CoreConfiguration;
import org.acra.data.CrashReportData;
import org.acra.sender.ReportSender;
import org.acra.sender.ReportSenderException;
import org.acra.sender.ReportSenderFactory;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileWriter;
import java.nio.file.Path;

public class LocalReportSender implements ReportSender {
CoreConfiguration config;

public LocalReportSender(CoreConfiguration coreConfiguration) {
config = coreConfiguration;
}

@Override
public void send(Context context, CrashReportData errorContent) throws ReportSenderException {
// Iterate over the CrashReportData instance and do whatever
// you need with each pair of ReportField key / String value

//declare root dir and append target dir (toServer)
Path rootDir = context.getApplicationContext().getExternalFilesDir(null).toPath();
Path destDir = rootDir.resolve("BundleTransmission/server");
if (!destDir.toFile().exists()) {
destDir.toFile().mkdir();
}
File logFile = new File(String.valueOf(destDir), "crash_report.txt");
try {
// Use the core ReportFormat configuration
String reportText = config.getReportFormat()
.toFormattedString(errorContent,
config.getReportContent(), "\n", "\n\t", false);

// Overwrite last report
FileWriter writer = new FileWriter(logFile, false);
writer.append(reportText);
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}

@AutoService(ReportSenderFactory.class)
public static class MySenderFactory implements ReportSenderFactory {
@NotNull
@Override
public ReportSender create(@NotNull Context context, @NotNull CoreConfiguration coreConfiguration) {
return new LocalReportSender(coreConfiguration);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.discdd.bundletransport.utils;

import static org.acra.ACRA.log;

import android.app.Application;
import android.content.Context;

import org.acra.ACRA;
import org.acra.BuildConfig;
import org.acra.config.CoreConfigurationBuilder;
import org.acra.data.CrashReportData;
import org.acra.data.StringFormat;
import org.acra.sender.ReportSender;
import org.acra.sender.ReportSenderException;

public class acraUtils extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
ACRA.DEV_LOGGING = true;
log.e("ACRA Enabled dev logging", "ACRA OK");
ACRA.init(this, new CoreConfigurationBuilder()
//core configuration:
.withBuildConfigClass(BuildConfig.class)
.withReportFormat(StringFormat.KEY_VALUE_LIST)
);
}
}

0 comments on commit c422d9d

Please sign in to comment.