mirror of https://github.com/tasks/tasks
AlarmManager + service for notifications
parent
eabdc75c8f
commit
1e86be28ee
@ -0,0 +1,40 @@
|
||||
package org.tasks.injection;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import io.reactivex.Completable;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public abstract class InjectingService extends Service {
|
||||
|
||||
@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();
|
||||
|
||||
return Service.START_NOT_STICKY;
|
||||
}
|
||||
|
||||
private void stop() {
|
||||
stopForeground(true);
|
||||
stopSelf();
|
||||
}
|
||||
|
||||
protected abstract int getNotificationId();
|
||||
|
||||
protected abstract Notification getNotification();
|
||||
|
||||
protected abstract void doWork(@Nonnull Intent intent);
|
||||
|
||||
protected abstract void inject(ServiceComponent component);
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
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;
|
||||
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 {
|
||||
|
||||
@Inject Preferences preferences;
|
||||
@Inject Notifier notifier;
|
||||
@Inject NotificationQueue notificationQueue;
|
||||
@Inject Tracker tracker;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getNotificationId() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doWork(@Nonnull Intent intent) {
|
||||
try {
|
||||
if (!preferences.isCurrentlyQuietHours()) {
|
||||
List<? extends NotificationQueueEntry> overdueJobs = notificationQueue.getOverdueJobs();
|
||||
notifier.triggerTaskNotifications(overdueJobs);
|
||||
boolean success = notificationQueue.remove(overdueJobs);
|
||||
if (BuildConfig.DEBUG && !success) {
|
||||
throw new RuntimeException("Failed to remove jobs from queue");
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
tracker.reportException(e);
|
||||
} finally {
|
||||
notificationQueue.scheduleNext();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void inject(ServiceComponent component) {
|
||||
component.inject(this);
|
||||
}
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
package org.tasks.jobs;
|
||||
|
||||
import android.content.Context;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.work.WorkerParameters;
|
||||
import java.util.List;
|
||||
import javax.inject.Inject;
|
||||
import org.tasks.BuildConfig;
|
||||
import org.tasks.Notifier;
|
||||
import org.tasks.injection.JobComponent;
|
||||
import org.tasks.preferences.Preferences;
|
||||
|
||||
public class NotificationWork extends RepeatingWorker {
|
||||
|
||||
@Inject Preferences preferences;
|
||||
@Inject Notifier notifier;
|
||||
@Inject NotificationQueue notificationQueue;
|
||||
|
||||
public NotificationWork(@NonNull Context context, @NonNull WorkerParameters workerParams) {
|
||||
super(context, workerParams);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Result run() {
|
||||
if (!preferences.isCurrentlyQuietHours()) {
|
||||
List<? extends NotificationQueueEntry> overdueJobs = notificationQueue.getOverdueJobs();
|
||||
notifier.triggerTaskNotifications(overdueJobs);
|
||||
boolean success = notificationQueue.remove(overdueJobs);
|
||||
if (BuildConfig.DEBUG && !success) {
|
||||
throw new RuntimeException("Failed to remove jobs from queue");
|
||||
}
|
||||
}
|
||||
return Result.SUCCESS;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void inject(JobComponent component) {
|
||||
component.inject(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void scheduleNext() {
|
||||
notificationQueue.scheduleNext();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue