Skip to content

Commit

Permalink
Merge pull request #566 from F43nd1r/attachment
Browse files Browse the repository at this point in the history
Send report as attachment instead of body
  • Loading branch information
F43nd1r committed Apr 3, 2017
2 parents 5c84bfc + 3fe02cb commit cfc60aa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
6 changes: 6 additions & 0 deletions acra/src/main/java/org/acra/annotation/ReportsCrashes.java
Original file line number Diff line number Diff line change
Expand Up @@ -660,4 +660,10 @@
* @since 4.9.3
*/
@NonNull Class<? extends AttachmentUriProvider> attachmentUriProvider() default DefaultAttachmentProvider.class;

/**
*
* @return if the report should be an attachment instead of plain text. Supported for email mode.
*/
boolean reportAsFile() default false;
}
18 changes: 6 additions & 12 deletions acra/src/main/java/org/acra/attachment/AcraContentProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
Expand Down Expand Up @@ -48,23 +47,19 @@
public class AcraContentProvider extends ContentProvider {
private static final String[] COLUMNS = {
OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE};
private static final String ANY_MATCH = "/*";
private UriMatcher uriMatcher;
private String authority;

@Override
public boolean onCreate() {
final String authority = getContext().getPackageName() + ".acra";
authority = getContext().getPackageName() + ".acra";
if (ACRA.DEV_LOGGING) ACRA.log.d(ACRA.LOG_TAG, "Registered content provider for authority " + authority);
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
for (Directory directory : Directory.values()) {
uriMatcher.addURI(authority, directory.name().toLowerCase() + ANY_MATCH, directory.ordinal());
}
return true;
}

@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
if (ACRA.DEV_LOGGING) ACRA.log.d(ACRA.LOG_TAG, "Query: " + uri);
final File file = getFileForUri(uri);
if (file == null) {
return null;
Expand All @@ -87,8 +82,7 @@ public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable S

@Nullable
private File getFileForUri(Uri uri) {
final int match = uriMatcher.match(uri);
if (match == UriMatcher.NO_MATCH) {
if(!"content".equals(uri.getScheme()) || !authority.equals(uri.getAuthority())){
return null;
}
final List<String> segments = new ArrayList<String>(uri.getPathSegments());
Expand Down Expand Up @@ -129,10 +123,10 @@ public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable St
public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException {
final File file = getFileForUri(uri);
if (file == null || !file.exists()) throw new FileNotFoundException("File represented by uri " + uri + " could not be found");
if(ACRA.DEV_LOGGING) {
if (ACRA.DEV_LOGGING) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
ACRA.log.d(ACRA.LOG_TAG, getCallingPackage() + " opened " + file.getPath());
}else {
} else {
ACRA.log.d(ACRA.LOG_TAG, file.getPath() + " was opened by an application");
}
}
Expand Down
15 changes: 14 additions & 1 deletion acra/src/main/java/org/acra/sender/EmailIntentSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@
import org.acra.collections.ImmutableSet;
import org.acra.collector.CrashReportData;
import org.acra.config.ACRAConfiguration;
import org.acra.file.CrashReportPersister;
import org.acra.model.Element;
import org.acra.util.InstanceCreator;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -65,6 +68,16 @@ public void send(@NonNull Context context, @NonNull CrashReportData errorContent
final String body = buildBody(errorContent);
final InstanceCreator instanceCreator = new InstanceCreator();
final ArrayList<Uri> attachments = instanceCreator.create(config.attachmentUriProvider(), new DefaultAttachmentProvider()).getAttachments(context, config);
boolean contentAttached = false;
if (config.reportAsFile()) {
final File cache = new File(context.getCacheDir(), "ACRA-report" + ACRAConstants.REPORTFILE_EXTENSION);
try {
new CrashReportPersister().store(errorContent, cache);
attachments.add(Uri.parse("content://" + context.getPackageName() + ".acra/root" + cache.getPath()));
contentAttached = true;
} catch (IOException ignored) {
}
}

final Intent resolveIntent = new Intent(android.content.Intent.ACTION_SENDTO);
resolveIntent.setData(Uri.fromParts("mailto", config.mailTo(), null));
Expand All @@ -81,7 +94,7 @@ public void send(@NonNull Context context, @NonNull CrashReportData errorContent
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{config.mailTo()});
emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
if (!contentAttached) emailIntent.putExtra(Intent.EXTRA_TEXT, body);
emailIntent.setType("message/rfc822");
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachments);
if (packageName.equals("android")) {
Expand Down

0 comments on commit cfc60aa

Please sign in to comment.