Use coroutines in injecting service

pull/1055/head
Alex Baker 4 years ago
parent 8a30fde2f2
commit e2dffbe71a

@ -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()
}

@ -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<? extends NotificationQueueEntry> 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();
}
}

@ -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()
}
Loading…
Cancel
Save