diff --git a/app/src/main/java/org/tasks/injection/InjectingService.kt b/app/src/main/java/org/tasks/injection/InjectingService.kt index 223e43c62..49db6151f 100644 --- a/app/src/main/java/org/tasks/injection/InjectingService.kt +++ b/app/src/main/java/org/tasks/injection/InjectingService.kt @@ -4,35 +4,37 @@ 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 kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.launch import org.tasks.R import org.tasks.analytics.Firebase import org.tasks.notifications.NotificationManager import javax.inject.Inject abstract class InjectingService : Service() { + private val job = SupervisorJob() + private val scope = CoroutineScope(Dispatchers.IO + job) + @Inject lateinit var firebase: Firebase - private lateinit var disposables: CompositeDisposable override fun onCreate() { super.onCreate() startForeground() - disposables = CompositeDisposable() } override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { startForeground() - disposables.add( - Completable.fromAction { doWork() } - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ done(startId) }) { t: Throwable -> - firebase.reportException(t) - done(startId) - }) + scope.launch { + try { + doWork() + } catch (e: Exception) { + firebase.reportException(e) + } finally { + done(startId) + } + } return START_NOT_STICKY } @@ -44,7 +46,7 @@ abstract class InjectingService : Service() { override fun onDestroy() { super.onDestroy() stopForeground(true) - disposables.dispose() + job.cancel() } private fun startForeground() { @@ -65,5 +67,6 @@ abstract class InjectingService : Service() { } protected open fun scheduleNext() {} - protected abstract fun doWork() + + protected abstract suspend fun doWork() } \ No newline at end of file diff --git a/app/src/main/java/org/tasks/jobs/NotificationService.java b/app/src/main/java/org/tasks/jobs/NotificationService.java deleted file mode 100644 index 9a75eebbf..000000000 --- a/app/src/main/java/org/tasks/jobs/NotificationService.java +++ /dev/null @@ -1,57 +0,0 @@ -package org.tasks.jobs; - -import static com.google.common.collect.Lists.transform; -import static com.todoroo.andlib.utility.AndroidUtilities.assertNotMainThread; - -import android.content.Intent; -import android.os.IBinder; -import androidx.annotation.Nullable; -import dagger.hilt.android.AndroidEntryPoint; -import java.util.List; -import javax.inject.Inject; -import org.tasks.Notifier; -import org.tasks.R; -import org.tasks.injection.InjectingService; -import org.tasks.preferences.Preferences; - -@AndroidEntryPoint -public class NotificationService extends InjectingService { - - @Inject Preferences preferences; - @Inject Notifier notifier; - @Inject NotificationQueue notificationQueue; - - @Nullable - @Override - public IBinder onBind(Intent intent) { - return null; - } - - @Override - protected int getNotificationId() { - return -1; - } - - @Override - protected int getNotificationBody() { - return R.string.building_notifications; - } - - @Override - protected synchronized void doWork() { - assertNotMainThread(); - - if (!preferences.isCurrentlyQuietHours()) { - List overdueJobs = notificationQueue.getOverdueJobs(); - if (!notificationQueue.remove(overdueJobs)) { - throw new RuntimeException("Failed to remove jobs from queue"); - } - notifier.triggerNotifications(transform(overdueJobs, NotificationQueueEntry::toNotification)); - } - } - - @Override - protected void scheduleNext() { - notificationQueue.scheduleNext(); - } -} diff --git a/app/src/main/java/org/tasks/jobs/NotificationService.kt b/app/src/main/java/org/tasks/jobs/NotificationService.kt new file mode 100644 index 000000000..45cd5b7d8 --- /dev/null +++ b/app/src/main/java/org/tasks/jobs/NotificationService.kt @@ -0,0 +1,39 @@ +package org.tasks.jobs + +import android.content.Intent +import android.os.IBinder +import com.google.common.collect.Lists +import com.todoroo.andlib.utility.AndroidUtilities +import dagger.hilt.android.AndroidEntryPoint +import org.tasks.Notifier +import org.tasks.R +import org.tasks.injection.InjectingService +import org.tasks.preferences.Preferences +import javax.inject.Inject + +@AndroidEntryPoint +class NotificationService : InjectingService() { + @Inject lateinit var preferences: Preferences + @Inject lateinit var notifier: Notifier + @Inject lateinit var notificationQueue: NotificationQueue + + override fun onBind(intent: Intent): IBinder? = null + + override val notificationId = -1 + + override val notificationBody = R.string.building_notifications + + @Synchronized + override suspend fun doWork() { + AndroidUtilities.assertNotMainThread() + if (!preferences.isCurrentlyQuietHours) { + val overdueJobs = notificationQueue.overdueJobs + if (!notificationQueue.remove(overdueJobs)) { + throw RuntimeException("Failed to remove jobs from queue") + } + notifier.triggerNotifications(Lists.transform(overdueJobs) { obj: NotificationQueueEntry? -> obj!!.toNotification() }) + } + } + + override fun scheduleNext() = notificationQueue.scheduleNext() +} \ No newline at end of file