Skip to content

Commit

Permalink
Fix mailsender on android Q (#740)
Browse files Browse the repository at this point in the history
* fix mailsender on android q

* update travis.yml

* move to stable api

* fix travis

* fix travis 2

* update robolectric
  • Loading branch information
F43nd1r committed Jun 19, 2019
1 parent 494368e commit 7280e77
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 39 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
@@ -1,8 +1,9 @@
language: android
android:
components:
- build-tools-28.0.2
- android-28
- build-tools-28.0.3
- build-tools-29.0.0
- android-29
before_cache:
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
- rm -fr $HOME/.gradle/caches/*/plugin-resolution/
Expand Down
Expand Up @@ -62,15 +62,15 @@ public boolean performInteractions(@NonNull final File reportFile) {
}
boolean sendReports = true;
for (Future<Boolean> future : futures) {
while (!future.isDone()) {
do {
try {
sendReports &= future.get();
} catch (InterruptedException ignored) {
} catch (ExecutionException e) {
//ReportInteraction crashed, so ignore it
break;
}
}
} while (!future.isDone());
}
return sendReports;
}
Expand Down
Expand Up @@ -27,6 +27,7 @@
import org.acra.config.CoreConfiguration;
import org.acra.sender.JobSenderService;
import org.acra.sender.LegacySenderService;
import org.acra.sender.SendingConductor;
import org.acra.util.IOUtils;

/**
Expand All @@ -46,21 +47,27 @@ public DefaultSenderScheduler(@NonNull Context context, @NonNull CoreConfigurati

@Override
public void scheduleReportSending(boolean onlySendSilentReports) {
final Intent intent = new Intent();
intent.putExtra(LegacySenderService.EXTRA_ONLY_SEND_SILENT_REPORTS, onlySendSilentReports);
intent.putExtra(LegacySenderService.EXTRA_ACRA_CONFIG, config);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
PersistableBundle extras = new PersistableBundle();
extras.putString(LegacySenderService.EXTRA_ACRA_CONFIG, IOUtils.serialize(config));
extras.putBoolean(LegacySenderService.EXTRA_ONLY_SEND_SILENT_REPORTS, onlySendSilentReports);
assert scheduler != null;
JobInfo.Builder builder = new JobInfo.Builder(0, new ComponentName(context, JobSenderService.class)).setOverrideDeadline(0L).setExtras(extras);
configureJob(builder);
scheduler.schedule(builder.build());
} else {
intent.setComponent(new ComponentName(context, LegacySenderService.class));
context.startService(intent);
SendingConductor conductor = new SendingConductor(context, config);
if(!conductor.getSenderInstances(false).isEmpty()) {
final Intent intent = new Intent();
intent.putExtra(LegacySenderService.EXTRA_ONLY_SEND_SILENT_REPORTS, onlySendSilentReports);
intent.putExtra(LegacySenderService.EXTRA_ACRA_CONFIG, config);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
PersistableBundle extras = new PersistableBundle();
extras.putString(LegacySenderService.EXTRA_ACRA_CONFIG, IOUtils.serialize(config));
extras.putBoolean(LegacySenderService.EXTRA_ONLY_SEND_SILENT_REPORTS, onlySendSilentReports);
assert scheduler != null;
JobInfo.Builder builder = new JobInfo.Builder(0, new ComponentName(context, JobSenderService.class)).setOverrideDeadline(0L).setExtras(extras);
configureJob(builder);
scheduler.schedule(builder.build());
} else {
intent.setComponent(new ComponentName(context, LegacySenderService.class));
context.startService(intent);
}
}
if(!conductor.getSenderInstances(true).isEmpty()) {
conductor.sendReports(onlySendSilentReports, true);
}
}

Expand Down
Expand Up @@ -21,7 +21,7 @@ public boolean onStartJob(JobParameters params) {
boolean onlySilent = extras.getBoolean(LegacySenderService.EXTRA_ONLY_SEND_SILENT_REPORTS);
if (config != null) {
new Thread(() -> {
new SendingConductor(this, config).sendReports(onlySilent);
new SendingConductor(this, config).sendReports(onlySilent, false);
jobFinished(params, false);
}).start();
}
Expand Down
11 changes: 6 additions & 5 deletions acra-core/src/main/java/org/acra/sender/LegacySenderService.java
Expand Up @@ -34,15 +34,16 @@ public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.hasExtra(EXTRA_ACRA_CONFIG)) {
final boolean onlySendSilentReports = intent.getBooleanExtra(EXTRA_ONLY_SEND_SILENT_REPORTS, false);
final CoreConfiguration config = (CoreConfiguration) intent.getSerializableExtra(EXTRA_ACRA_CONFIG);
new Thread(() -> {
new SendingConductor(this, config).sendReports(onlySendSilentReports);
stopSelf();
}).start();
if (config != null) {
new Thread(() -> {
new SendingConductor(this, config).sendReports(onlySendSilentReports, false);
stopSelf();
}).start();
}
} else {
if (ACRA.DEV_LOGGING) ACRA.log.d(LOG_TAG, "SenderService was started but no valid intent was delivered, will now quit");
}
return START_REDELIVER_INTENT;

}

@Nullable
Expand Down
4 changes: 4 additions & 0 deletions acra-core/src/main/java/org/acra/sender/ReportSender.java
Expand Up @@ -37,4 +37,8 @@ public interface ReportSender {
* @throws ReportSenderException If anything goes fatally wrong during the handling of crash data, you can (should) throw a {@link ReportSenderException} with a custom message.
*/
void send(@NonNull Context context, @NonNull CrashReportData errorContent) throws ReportSenderException;

default boolean requiresForeground() {
return false;
}
}
20 changes: 11 additions & 9 deletions acra-core/src/main/java/org/acra/sender/SendingConductor.java
Expand Up @@ -35,10 +35,15 @@ public SendingConductor(@NonNull Context context, @NonNull CoreConfiguration con
locator = new ReportLocator(context);
}

public void sendReports(boolean onlySendSilentReports) {
public void sendReports(boolean onlySendSilentReports, boolean foreground) {
if (ACRA.DEV_LOGGING) ACRA.log.d(LOG_TAG, "About to start sending reports from SenderService");
try {
final List<ReportSender> senderInstances = getSenderInstances();
final List<ReportSender> senderInstances = getSenderInstances(foreground);

if (senderInstances.isEmpty()) {
if (ACRA.DEV_LOGGING) ACRA.log.d(LOG_TAG, "No ReportSenders configured - adding NullSender");
senderInstances.add(new NullSender());
}

// Get approved reports
final File[] reports = locator.getApprovedReports();
Expand Down Expand Up @@ -77,7 +82,7 @@ public void sendReports(boolean onlySendSilentReports) {
}

@NonNull
private List<ReportSender> getSenderInstances() {
public List<ReportSender> getSenderInstances(boolean foreground) {
List<Class<? extends ReportSenderFactory>> factoryClasses = config.reportSenderFactoryClasses();

if (ACRA.DEV_LOGGING) ACRA.log.d(LOG_TAG, "config#reportSenderFactoryClasses : " + factoryClasses);
Expand All @@ -98,12 +103,9 @@ private List<ReportSender> getSenderInstances() {
for (ReportSenderFactory factory : factories) {
final ReportSender sender = factory.create(context, config);
if (ACRA.DEV_LOGGING) ACRA.log.d(LOG_TAG, "Adding reportSender : " + sender);
reportSenders.add(sender);
}

if (reportSenders.isEmpty()) {
if (ACRA.DEV_LOGGING) ACRA.log.d(LOG_TAG, "No ReportSenders configured - adding NullSender");
reportSenders.add(new NullSender());
if(foreground == sender.requiresForeground()) {
reportSenders.add(sender);
}
}
return reportSenders;
}
Expand Down
Expand Up @@ -109,6 +109,12 @@ public void send(@NonNull Context context, @NonNull CrashReportData errorContent
}
}

@Override
public boolean requiresForeground() {
//TODO make this code Q after Android Q release
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;
}

/**
* Finds the package name of the default email client supporting attachments
*
Expand Down
2 changes: 2 additions & 0 deletions annotationprocessor/build.gradle
Expand Up @@ -17,7 +17,9 @@
apply plugin: 'java'

dependencies {
annotationProcessor "com.google.auto.service:auto-service:$autoServiceVersion"
compile "com.google.auto.service:auto-service:$autoServiceVersion"
compile "com.google.auto.service:auto-service-annotations:$autoServiceVersion"
compile 'com.squareup:javapoet:1.11.1'
compile 'org.apache.commons:commons-lang3:3.8.1'
compile 'org.apache.commons:commons-text:1.6'
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Expand Up @@ -89,6 +89,7 @@ subprojects {
plugins.withType(LibraryPlugin) {
android {
compileSdkVersion Integer.parseInt(androidVersion)
buildToolsVersion "$buildToolsVersion"
defaultConfig {
minSdkVersion androidMinVersion
targetSdkVersion androidVersion
Expand Down Expand Up @@ -118,7 +119,7 @@ subprojects {

dependencies {
testImplementation "junit:junit:$junitVersion"
testImplementation "org.robolectric:robolectric:$roboelectricVersion"
testImplementation "org.robolectric:robolectric:$robolectricVersion"
testImplementation "androidx.test:core:$androidTestVersion"
}

Expand Down
7 changes: 4 additions & 3 deletions gradle.properties
Expand Up @@ -29,14 +29,15 @@ group=ch.acra
# versions
version=5.3.1-SNAPSHOT
supportVersion=28.0.0
androidVersion=28
androidVersion=29
androidMinVersion=14
androidBuildPluginVersion=3.3.0-alpha10
androidBuildPluginVersion=3.4.1
buildToolsVersion=29.0.0
bintrayPluginVersion=1.8.3
releasePluginVersion=2.7.0
autoServiceVersion=1.0-rc5
junitVersion=4.12
roboelectricVersion=4.0.2
robolectricVersion=4.3
androidTestVersion=1.0.0
jobVersion=1.2.6
workVersion=1.0.0-alpha12
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.3-all.zip

0 comments on commit 7280e77

Please sign in to comment.