Inject before setting foreground

pull/795/head
Alex Baker 6 years ago
parent 1e86be28ee
commit 0fe5f82367

@ -3,38 +3,57 @@ package org.tasks.injection;
import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import androidx.core.app.NotificationCompat;
import io.reactivex.Completable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import javax.annotation.Nonnull;
import org.tasks.R;
import org.tasks.notifications.NotificationManager;
public abstract class InjectingService extends Service {
private CompositeDisposable disposables = new CompositeDisposable();
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(getNotificationId(), getNotification());
inject(((InjectingApplication) getApplication()).getComponent().plus(new ServiceModule()));
Completable.fromAction(() -> doWork(intent))
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.doFinally(this::stop)
.subscribe();
startForeground(getNotificationId(), buildNotification());
disposables.add(
Completable.fromAction(this::doWork)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::stopSelf));
return Service.START_NOT_STICKY;
}
private void stop() {
@Override
public void onDestroy() {
super.onDestroy();
stopForeground(true);
stopSelf();
disposables.dispose();
}
protected abstract int getNotificationId();
protected abstract Notification getNotification();
protected abstract int getNotificationBody();
private Notification buildNotification() {
return new NotificationCompat.Builder(
this, NotificationManager.NOTIFICATION_CHANNEL_MISCELLANEOUS)
.setSound(null)
.setSmallIcon(R.drawable.ic_check_white_24dp)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(getNotificationBody()))
.build();
}
protected abstract void doWork(@Nonnull Intent intent);
protected abstract void doWork();
protected abstract void inject(ServiceComponent component);
}

@ -1,12 +1,9 @@
package org.tasks.jobs;
import android.app.Notification;
import android.content.Intent;
import android.os.IBinder;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import java.util.List;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import org.tasks.BuildConfig;
import org.tasks.Notifier;
@ -14,7 +11,6 @@ import org.tasks.R;
import org.tasks.analytics.Tracker;
import org.tasks.injection.InjectingService;
import org.tasks.injection.ServiceComponent;
import org.tasks.notifications.NotificationManager;
import org.tasks.preferences.Preferences;
public class NotificationService extends InjectingService {
@ -36,18 +32,12 @@ public class NotificationService extends InjectingService {
}
@Override
protected Notification getNotification() {
return new NotificationCompat.Builder(
this, NotificationManager.NOTIFICATION_CHANNEL_MISCELLANEOUS)
.setSound(null)
.setSmallIcon(R.drawable.ic_check_white_24dp)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.building_notifications))
.build();
protected int getNotificationBody() {
return R.string.building_notifications;
}
@Override
protected void doWork(@Nonnull Intent intent) {
protected void doWork() {
try {
if (!preferences.isCurrentlyQuietHours()) {
List<? extends NotificationQueueEntry> overdueJobs = notificationQueue.getOverdueJobs();

@ -55,7 +55,7 @@ import timber.log.Timber;
@ApplicationScope
public class NotificationManager {
public static final String NOTIFICATION_CHANNEL_DEFAULT = "notifications";
private static final String NOTIFICATION_CHANNEL_DEFAULT = "notifications";
public static final String NOTIFICATION_CHANNEL_TASKER = "notifications_tasker";
public static final String NOTIFICATION_CHANNEL_TIMERS = "notifications_timers";
public static final String NOTIFICATION_CHANNEL_MISCELLANEOUS = "notifications_miscellaneous";

Loading…
Cancel
Save