Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix success toast shown on failure #639

Merged
merged 1 commit into from
Feb 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions acra-core/src/main/java/org/acra/sender/ReportDistributor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;

import org.acra.ACRA;
import org.acra.data.CrashReportData;
import org.acra.config.CoreConfiguration;
import org.acra.config.DefaultRetryPolicy;
import org.acra.config.RetryPolicy;
import org.acra.data.CrashReportData;
import org.acra.file.CrashReportPersister;
import org.acra.util.IOUtils;
import org.acra.util.InstanceCreator;
Expand Down Expand Up @@ -52,9 +51,9 @@ final class ReportDistributor {
/**
* Creates a new {@link ReportDistributor} to try sending pending reports.
*
* @param context ApplicationContext in which the reports are being sent.
* @param config Configuration to use while sending.
* @param reportSenders List of ReportSender to use to send the crash reports.
* @param context ApplicationContext in which the reports are being sent.
* @param config Configuration to use while sending.
* @param reportSenders List of ReportSender to use to send the crash reports.
*/
ReportDistributor(@NonNull Context context, @NonNull CoreConfiguration config, @NonNull List<ReportSender> reportSenders) {
this.context = context;
Expand All @@ -65,16 +64,18 @@ final class ReportDistributor {
/**
* Send report via all senders.
*
* @param reportFile Report to send.
* @param reportFile Report to send.
* @return if distributing was successful
*/
public void distribute(@NonNull File reportFile) {
public boolean distribute(@NonNull File reportFile) {

ACRA.log.i(LOG_TAG, "Sending report " + reportFile );
ACRA.log.i(LOG_TAG, "Sending report " + reportFile);
try {
final CrashReportPersister persister = new CrashReportPersister();
final CrashReportData previousCrashReport = persister.load(reportFile);
sendCrashReport(previousCrashReport);
IOUtils.deleteFile(reportFile);
return true;
} catch (RuntimeException e) {
ACRA.log.e(LOG_TAG, "Failed to send crash reports for " + reportFile, e);
IOUtils.deleteFile(reportFile);
Expand All @@ -84,20 +85,21 @@ public void distribute(@NonNull File reportFile) {
} catch (JSONException e) {
ACRA.log.e(LOG_TAG, "Failed to load crash report for " + reportFile, e);
IOUtils.deleteFile(reportFile);
}catch (ReportSenderException e) {
} catch (ReportSenderException e) {
ACRA.log.e(LOG_TAG, "Failed to send crash report for " + reportFile, e);
// An issue occurred while sending this report but we can still try to
// send other reports. Report sending is limited by ACRAConstants.MAX_SEND_REPORTS
// so there's not much to fear about overloading a failing server.
}
return false;
}

/**
* Sends the report with all configured ReportSenders. If at least one
* sender completed its job, the report is considered as sent and will not
* be sent again for failing senders.
*
* @param errorContent Crash data.
* @param errorContent Crash data.
* @throws ReportSenderException if unable to send the crash report.
*/
private void sendCrashReport(@NonNull CrashReportData errorContent) throws ReportSenderException {
Expand Down
10 changes: 5 additions & 5 deletions acra-core/src/main/java/org/acra/sender/SenderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import android.widget.Toast;

import org.acra.ACRA;
import org.acra.ACRAConstants;
import org.acra.config.CoreConfiguration;
Expand Down Expand Up @@ -52,7 +51,7 @@ public SenderService() {
@Override
protected void onHandleWork(@NonNull Intent intent) {
if (!intent.hasExtra(EXTRA_ACRA_CONFIG)) {
if(ACRA.DEV_LOGGING) ACRA.log.d(LOG_TAG, "SenderService was started but no valid intent was delivered, will now quit");
if (ACRA.DEV_LOGGING) ACRA.log.d(LOG_TAG, "SenderService was started but no valid intent was delivered, will now quit");
return;
}

Expand Down Expand Up @@ -89,12 +88,13 @@ protected void onHandleWork(@NonNull Intent intent) {
break; // send only a few reports to avoid overloading the network
}

reportDistributor.distribute(report);
reportsSentCount++;
if (reportDistributor.distribute(report)) {
reportsSentCount++;
}
}
final String toast = reportsSentCount > 0 ? config.reportSendSuccessToast() : config.reportSendFailureToast();
if (toast != null) {
new Handler(Looper.getMainLooper()).post(() -> ToastSender.sendToast(SenderService.this, toast, Toast.LENGTH_LONG));
new Handler(Looper.getMainLooper()).post(() -> ToastSender.sendToast(this, toast, Toast.LENGTH_LONG));
}
} catch (Exception e) {
ACRA.log.e(LOG_TAG, "", e);
Expand Down