Report exceptions in notification service

pull/795/head
Alex Baker 5 years ago
parent 6469fc8bea
commit dbddc6dea7

@ -8,11 +8,15 @@ import io.reactivex.Completable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import javax.inject.Inject;
import org.tasks.R;
import org.tasks.analytics.Tracker;
import org.tasks.notifications.NotificationManager;
public abstract class InjectingService extends Service {
@Inject Tracker tracker;
private CompositeDisposable disposables;
@Override
@ -34,11 +38,21 @@ public abstract class InjectingService extends Service {
Completable.fromAction(this::doWork)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> stopSelf(startId)));
.subscribe(
() -> done(startId),
t -> {
tracker.reportException(t);
done(startId);
}));
return Service.START_NOT_STICKY;
}
private void done(int startId) {
scheduleNext();
stopSelf(startId);
}
@Override
public void onDestroy() {
super.onDestroy();
@ -66,6 +80,10 @@ public abstract class InjectingService extends Service {
.build();
}
protected void scheduleNext() {
}
protected abstract void doWork();
protected abstract void inject(ServiceComponent component);

@ -8,10 +8,8 @@ import android.os.IBinder;
import androidx.annotation.Nullable;
import java.util.List;
import javax.inject.Inject;
import org.tasks.BuildConfig;
import org.tasks.Notifier;
import org.tasks.R;
import org.tasks.analytics.Tracker;
import org.tasks.injection.InjectingService;
import org.tasks.injection.ServiceComponent;
import org.tasks.preferences.Preferences;
@ -21,7 +19,6 @@ public class NotificationService extends InjectingService {
@Inject Preferences preferences;
@Inject Notifier notifier;
@Inject NotificationQueue notificationQueue;
@Inject Tracker tracker;
@Nullable
@Override
@ -43,23 +40,20 @@ public class NotificationService extends InjectingService {
protected synchronized void doWork() {
assertNotMainThread();
try {
if (!preferences.isCurrentlyQuietHours()) {
List<? extends NotificationQueueEntry> overdueJobs = notificationQueue.getOverdueJobs();
notifier.triggerNotifications(
transform(overdueJobs, NotificationQueueEntry::toNotification));
boolean success = notificationQueue.remove(overdueJobs);
if (BuildConfig.DEBUG && !success) {
throw new RuntimeException("Failed to remove jobs from queue");
}
if (!preferences.isCurrentlyQuietHours()) {
List<? extends NotificationQueueEntry> overdueJobs = notificationQueue.getOverdueJobs();
if (!notificationQueue.remove(overdueJobs)) {
throw new RuntimeException("Failed to remove jobs from queue");
}
} catch (Exception e) {
tracker.reportException(e);
} finally {
notificationQueue.scheduleNext();
notifier.triggerNotifications(transform(overdueJobs, NotificationQueueEntry::toNotification));
}
}
@Override
protected void scheduleNext() {
notificationQueue.scheduleNext();
}
@Override
protected void inject(ServiceComponent component) {
component.inject(this);

Loading…
Cancel
Save