Remove workers. Fix #71

This commit is contained in:
Tommaso Berlose
2020-05-06 21:25:23 +02:00
parent 7076311e94
commit ea372dd76d
11 changed files with 61 additions and 322 deletions

View File

@ -1,154 +0,0 @@
package com.tommasoberlose.anotherwidget.services
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.work.*
import com.tommasoberlose.anotherwidget.db.EventRepository
import com.tommasoberlose.anotherwidget.global.Actions
import com.tommasoberlose.anotherwidget.models.Event
import com.tommasoberlose.anotherwidget.receivers.UpdatesReceiver
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget
import org.joda.time.Period
import java.util.*
import java.util.concurrent.TimeUnit
class UpdatesWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
override fun doWork(): Result {
Log.d("ciao1", "update ok: $inputData")
val repo = EventRepository(applicationContext)
val event = repo.getEventByEventId(inputData.getLong(EVENT_ID, -1))
event?.let {
scheduleEventUpdate(applicationContext, it)
}
MainWidget.updateWidget(applicationContext)
return Result.success()
}
companion object {
private const val UPDATES_JOB_TAG = "UPDATES_WORKER"
private const val EVENT_ID = "event_id"
fun setUpdates(context: Context) {
removeUpdates(context)
EventRepository(context).getEvents().forEach { event ->
scheduleEventUpdate(context, event)
}
}
private fun scheduleEventUpdate(context: Context, event: Event) {
val workManager = WorkManager.getInstance(context)
val now = Calendar.getInstance().apply {
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
}
if (event.startDate > now.timeInMillis) {
val diff = Period(now.timeInMillis, event.startDate)
workManager.enqueueUniqueWork(
"UNIQUE_${event.eventID}",
ExistingWorkPolicy.REPLACE,
OneTimeWorkRequestBuilder<UpdatesWorker>()
.setInputData(Data.Builder().putLong(EVENT_ID, event.eventID).build())
.setInitialDelay(if (diff.minutes > 0) diff.minutes.toLong() else 60L, TimeUnit.MINUTES)
.addTag(UPDATES_JOB_TAG)
.build()
)
} else if (event.endDate > now.timeInMillis) {
// Update the widget one second after the event is finished
workManager.enqueueUniqueWork(
"UNIQUE_${event.eventID}",
ExistingWorkPolicy.REPLACE,
OneTimeWorkRequestBuilder<UpdatesWorker>()
.setInitialDelay(event.endDate - now.timeInMillis - 10 * 60 * 1000, TimeUnit.MILLISECONDS)
.addTag(UPDATES_JOB_TAG)
.build()
)
}
}
fun removeUpdates(context: Context) {
// WorkManager.getInstance(context).cancelAllWorkByTag(UPDATES_JOB_TAG)
}
}
}
//
//class UpdatesWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
//
// override fun doWork(): Result {
// Log.d("ciao1", "update ok: $inputData")
// MainWidget.updateWidget(applicationContext)
// scheduleNextEventUpdate(applicationContext)
// return Result.success()
// }
//
// companion object {
// private const val JOB_TAG = "UPDATES_WORKER"
// private const val ONE_HOUR = 60 * 60 * 1000
//
// fun setUpdates(context: Context) {
// removeUpdates(context)
//
// scheduleNextEventUpdate(context)
// }
//
// private fun scheduleNextEventUpdate(context: Context) {
// val workManager = WorkManager.getInstance(context)
// val now = Calendar.getInstance().apply {
// set(Calendar.SECOND, 0)
// set(Calendar.MILLISECOND, 0)
// }
//
// val firstEvent = EventRepository(context).getEvents().sort("startDate").first()
// firstEvent?.let { event ->
// if (event.startDate < now.timeInMillis) {
// // Update the widget one second after the event is finished
// workManager.enqueue(OneTimeWorkRequestBuilder<UpdatesWorker>()
// .setInitialDelay((if (event.endDate - now.timeInMillis < ONE_HOUR) event.endDate - now.timeInMillis else ONE_HOUR).toLong(), TimeUnit.MILLISECONDS)
// .addTag(JOB_TAG)
// .build()
// )
//
// } else {
// val diff = Period(now.timeInMillis, event.startDate)
// workManager.enqueue(OneTimeWorkRequestBuilder<UpdatesWorker>()
// .setInitialDelay(if (diff.minutes > 0) diff.minutes.toLong() else 60L, TimeUnit.MINUTES)
// .addTag(JOB_TAG)
// .build()
// )
// Log.d("ciao1", "next update ${Date(now.timeInMillis + (if (diff.minutes > 0) diff.minutes.toLong() else 60L) * 60 * 1000)}")
// }
// }
//
//
//// if (event.startDate > now.timeInMillis) {
//// val diff = Period(now.timeInMillis, event.startDate)
//// workManager.enqueue(OneTimeWorkRequestBuilder<WeatherWorker>()
//// .setInputData(Data.Builder().putLong(EVENT_ID, event.eventID).build())
//// .setInitialDelay(if (diff.minutes > 0) diff.minutes.toLong() else 60L, TimeUnit.MINUTES)
//// .addTag(JOB_TAG)
//// .build()
//// )
//// Log.d("ciao1", "next update ${Date(now.timeInMillis + (if (diff.minutes > 0) diff.minutes.toLong() else 60L) * 60 * 1000)}")
//// } else if (event.endDate > now.timeInMillis) {
//// // Update the widget one second after the event is finished
//// workManager.enqueueUniqueWork("UPDATES_JOB_ONE_TIME_END_${event.eventID}", ExistingWorkPolicy.REPLACE, OneTimeWorkRequestBuilder<WeatherWorker>()
//// .setInitialDelay(event.endDate - now.timeInMillis, TimeUnit.MILLISECONDS)
//// .addTag(JOB_TAG)
//// .build()
//// )
//// }
// }
//
// fun removeUpdates(context: Context) {
// WorkManager.getInstance(context).cancelAllWorkByTag(JOB_TAG)
// }
// }
//}

View File

@ -1,77 +0,0 @@
package com.tommasoberlose.anotherwidget.services
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.work.*
import com.tommasoberlose.anotherwidget.global.Actions
import com.tommasoberlose.anotherwidget.global.Preferences
import com.tommasoberlose.anotherwidget.helpers.WeatherHelper
import com.tommasoberlose.anotherwidget.receivers.WeatherReceiver
import java.util.*
import java.util.concurrent.TimeUnit
class WeatherWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
override fun doWork(): Result {
Log.d("ciao1", "weather ok")
WeatherHelper.updateWeather(applicationContext)
return Result.success()
}
companion object {
private const val JOB_TAG = "WEATHER_WORKER"
fun setUpdates(context: Context) {
removeUpdates(context)
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
if (Preferences.showWeather && Preferences.weatherProviderApi != "") {
WeatherHelper.updateWeather(context)
WorkManager.getInstance(context).enqueueUniquePeriodicWork(
"WEATHER_JOB_PERIODIC",
ExistingPeriodicWorkPolicy.KEEP,
PeriodicWorkRequestBuilder<WeatherWorker>(
when (Preferences.weatherRefreshPeriod) {
0 -> 30
1 -> 60
2 -> 60L * 3
3 -> 60L * 6
4 -> 60L * 12
5 -> 60L * 24
else -> 60
}
, TimeUnit.MINUTES
)
.addTag(JOB_TAG)
.setConstraints(constraints)
.build()
)
}
}
fun setOneTimeUpdate(context: Context) {
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val workManager = WorkManager.getInstance(context)
listOf(10L, 20L, 30L).forEach {
workManager.enqueueUniqueWork("WEATHER_JOB_ONE_TIME_$it", ExistingWorkPolicy.KEEP,
OneTimeWorkRequestBuilder<WeatherWorker>()
.setInitialDelay(it, TimeUnit.MINUTES)
.setConstraints(constraints)
.addTag(JOB_TAG)
.build()
)
}
}
fun removeUpdates(context: Context) {
WorkManager.getInstance(context).cancelAllWorkByTag(JOB_TAG)
}
}
}