Remove usage of the deprecated WorkManager.getInstance()

This commit is contained in:
Jonas Lochmann 2022-03-28 02:00:00 +02:00
parent f4d3741289
commit ef0e0cf075
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
10 changed files with 51 additions and 33 deletions

View file

@ -251,7 +251,8 @@ class AppSetupLogic(private val appLogic: AppLogic) {
if (server.hasAuthToken) { if (server.hasAuthToken) {
ReportUninstallWorker.enqueue( ReportUninstallWorker.enqueue(
deviceAuthToken = server.deviceAuthToken, deviceAuthToken = server.deviceAuthToken,
customServerUrl = server.customServerUrl customServerUrl = server.customServerUrl,
context = appLogic.context
) )
} }
@ -267,8 +268,8 @@ class AppSetupLogic(private val appLogic: AppLogic) {
// delete the old config // delete the old config
DatabaseBackup.with(appLogic.context).tryCreateDatabaseBackupAsync() DatabaseBackup.with(appLogic.context).tryCreateDatabaseBackupAsync()
PeriodicSyncInBackgroundWorker.disable() PeriodicSyncInBackgroundWorker.disable(appLogic.context)
CheckUpdateWorker.deschedule() CheckUpdateWorker.deschedule(appLogic.context)
} }
suspend fun dangerousRemoteReset() { suspend fun dangerousRemoteReset() {

View file

@ -99,9 +99,9 @@ class BackgroundTaskLogic(val appLogic: AppLogic) {
appLogic.database.config().getEnableBackgroundSyncAsync().ignoreUnchanged().observeForever { appLogic.database.config().getEnableBackgroundSyncAsync().ignoreUnchanged().observeForever {
if (it) { if (it) {
PeriodicSyncInBackgroundWorker.enable() PeriodicSyncInBackgroundWorker.enable(appLogic.context)
} else { } else {
PeriodicSyncInBackgroundWorker.disable() PeriodicSyncInBackgroundWorker.disable(appLogic.context)
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* TimeLimit Copyright <C> 2019 - 2021 Jonas Lochmann * TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -121,7 +121,7 @@ class SyncUtil (private val logic: AppLogic) {
lastSync.value = logic.timeApi.getCurrentUptimeInMillis() lastSync.value = logic.timeApi.getCurrentUptimeInMillis()
SyncInBackgroundWorker.deschedule() SyncInBackgroundWorker.deschedule(logic.context)
lastSyncExceptionInternal.postValue(null) lastSyncExceptionInternal.postValue(null)
// wait 2 to 3 seconds before any next sync (debounce) // wait 2 to 3 seconds before any next sync (debounce)
@ -150,7 +150,7 @@ class SyncUtil (private val logic: AppLogic) {
importantSyncRequested.postValue(true) importantSyncRequested.postValue(true)
if (enqueueIfOffline) { if (enqueueIfOffline) {
SyncInBackgroundWorker.enqueueDelayed() SyncInBackgroundWorker.enqueueDelayed(logic.context)
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* TimeLimit Copyright <C> 2019 - 2020 Jonas Lochmann * TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -34,6 +34,8 @@ object ManageDeviceBackgroundSync {
activityViewModel: ActivityViewModel, activityViewModel: ActivityViewModel,
fragmentManager: FragmentManager fragmentManager: FragmentManager
) { ) {
val context = view.root.context
view.titleView.setOnClickListener { view.titleView.setOnClickListener {
HelpDialogFragment.newInstance( HelpDialogFragment.newInstance(
title = R.string.device_background_sync_title, title = R.string.device_background_sync_title,
@ -56,9 +58,9 @@ object ManageDeviceBackgroundSync {
// for some reason, the observing of the config value does not work correctly -> do it manually here // for some reason, the observing of the config value does not work correctly -> do it manually here
if (isChecked) { if (isChecked) {
PeriodicSyncInBackgroundWorker.enable() PeriodicSyncInBackgroundWorker.enable(context)
} else { } else {
PeriodicSyncInBackgroundWorker.disable() PeriodicSyncInBackgroundWorker.disable(context)
} }
} else { } else {
view.checkbox.isChecked = enable view.checkbox.isChecked = enable

View file

@ -123,7 +123,7 @@ class SetupParentModeModel(application: Application): AndroidViewModel(applicati
DatabaseBackup.with(getApplication()).tryCreateDatabaseBackupAsync() DatabaseBackup.with(getApplication()).tryCreateDatabaseBackupAsync()
if (enableBackgroundSync) { if (enableBackgroundSync) {
PeriodicSyncInBackgroundWorker.enable() PeriodicSyncInBackgroundWorker.enable(getApplication())
} }
UpdateUtil.setEnableChecks(getApplication(), enableUpdateChecks) UpdateUtil.setEnableChecks(getApplication(), enableUpdateChecks)
@ -194,7 +194,7 @@ class SetupParentModeModel(application: Application): AndroidViewModel(applicati
DatabaseBackup.with(getApplication()).tryCreateDatabaseBackupAsync() DatabaseBackup.with(getApplication()).tryCreateDatabaseBackupAsync()
if (enableBackgroundSync) { if (enableBackgroundSync) {
PeriodicSyncInBackgroundWorker.enable() PeriodicSyncInBackgroundWorker.enable(getApplication())
} }
UpdateUtil.setEnableChecks(getApplication(), enableUpdateChecks) UpdateUtil.setEnableChecks(getApplication(), enableUpdateChecks)

View file

@ -1,5 +1,5 @@
/* /*
* TimeLimit Copyright <C> 2019 - 2020 Jonas Lochmann * TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -48,7 +48,7 @@ object UpdateUtil {
database.config().setUpdatesEnabledSync(true) database.config().setUpdatesEnabledSync(true)
} }
CheckUpdateWorker.schedule() CheckUpdateWorker.schedule(context)
} }
fun disableChecks(context: Context) { fun disableChecks(context: Context) {
@ -58,7 +58,7 @@ object UpdateUtil {
database.config().setUpdatesEnabledSync(false) database.config().setUpdatesEnabledSync(false)
} }
CheckUpdateWorker.deschedule() CheckUpdateWorker.deschedule(context)
} }
suspend fun doUpdateCheck(context: Context, database: Database, enableNotifications: Boolean): Boolean { suspend fun doUpdateCheck(context: Context, database: Database, enableNotifications: Boolean): Boolean {

View file

@ -1,5 +1,5 @@
/* /*
* TimeLimit Copyright <C> 2019 - 2020 Jonas Lochmann * TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -27,8 +27,8 @@ class CheckUpdateWorker(private val context: Context, workerParameters: WorkerPa
companion object { companion object {
private const val UNIQUE_WORK_NAME = "CheckUpdateWorker" private const val UNIQUE_WORK_NAME = "CheckUpdateWorker"
fun schedule() { fun schedule(context: Context) {
WorkManager.getInstance().enqueueUniquePeriodicWork( WorkManager.getInstance(context).enqueueUniquePeriodicWork(
UNIQUE_WORK_NAME, UNIQUE_WORK_NAME,
ExistingPeriodicWorkPolicy.KEEP, ExistingPeriodicWorkPolicy.KEEP,
PeriodicWorkRequestBuilder<CheckUpdateWorker>(1, TimeUnit.DAYS) PeriodicWorkRequestBuilder<CheckUpdateWorker>(1, TimeUnit.DAYS)
@ -42,8 +42,8 @@ class CheckUpdateWorker(private val context: Context, workerParameters: WorkerPa
) )
} }
fun deschedule() { fun deschedule(context: Context) {
WorkManager.getInstance().cancelUniqueWork(UNIQUE_WORK_NAME) WorkManager.getInstance(context).cancelUniqueWork(UNIQUE_WORK_NAME)
} }
} }

View file

@ -1,3 +1,18 @@
/*
* TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.timelimit.android.work package io.timelimit.android.work
import android.content.Context import android.content.Context
@ -15,12 +30,12 @@ class PeriodicSyncInBackgroundWorker(private val context: Context, workerParamet
private const val LOG_TAG = "PeriodicBackgroundSync" private const val LOG_TAG = "PeriodicBackgroundSync"
private const val UNIQUE_WORK_NAME = "PeriodicSyncInBackgroundWorker" private const val UNIQUE_WORK_NAME = "PeriodicSyncInBackgroundWorker"
fun enable() { fun enable(context: Context) {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "enable()") Log.d(LOG_TAG, "enable()")
} }
WorkManager.getInstance().enqueueUniquePeriodicWork( WorkManager.getInstance(context).enqueueUniquePeriodicWork(
UNIQUE_WORK_NAME, UNIQUE_WORK_NAME,
ExistingPeriodicWorkPolicy.KEEP, ExistingPeriodicWorkPolicy.KEEP,
PeriodicWorkRequestBuilder<PeriodicSyncInBackgroundWorker>(1, TimeUnit.HOURS) PeriodicWorkRequestBuilder<PeriodicSyncInBackgroundWorker>(1, TimeUnit.HOURS)
@ -34,12 +49,12 @@ class PeriodicSyncInBackgroundWorker(private val context: Context, workerParamet
) )
} }
fun disable() { fun disable(context: Context) {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "disable()") Log.d(LOG_TAG, "disable()")
} }
WorkManager.getInstance().cancelUniqueWork(UNIQUE_WORK_NAME) WorkManager.getInstance(context).cancelUniqueWork(UNIQUE_WORK_NAME)
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* TimeLimit Copyright <C> 2019 - 2021 Jonas Lochmann * TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -31,7 +31,7 @@ class ReportUninstallWorker(val context: Context, workerParameters: WorkerParame
private const val DATA_CUSTOM_SERVER_URL = "customServerUrl" private const val DATA_CUSTOM_SERVER_URL = "customServerUrl"
private const val LOG_TAG = "ReportUninstallWorker" private const val LOG_TAG = "ReportUninstallWorker"
fun enqueue(deviceAuthToken: String, customServerUrl: String) { fun enqueue(deviceAuthToken: String, customServerUrl: String, context: Context) {
if (deviceAuthToken.isEmpty()) { if (deviceAuthToken.isEmpty()) {
return return
} }
@ -40,7 +40,7 @@ class ReportUninstallWorker(val context: Context, workerParameters: WorkerParame
Log.d(LOG_TAG, "enqueue()") Log.d(LOG_TAG, "enqueue()")
} }
WorkManager.getInstance().enqueue( WorkManager.getInstance(context).enqueue(
OneTimeWorkRequest.Builder(ReportUninstallWorker::class.java) OneTimeWorkRequest.Builder(ReportUninstallWorker::class.java)
.setConstraints( .setConstraints(
Constraints.Builder() Constraints.Builder()

View file

@ -1,5 +1,5 @@
/* /*
* TimeLimit Copyright <C> 2019 - 2021 Jonas Lochmann * TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -34,12 +34,12 @@ class SyncInBackgroundWorker(val context: Context, workerParameters: WorkerParam
private const val LOG_TAG = "SyncInBackground" private const val LOG_TAG = "SyncInBackground"
private const val UNIQUE_WORK_NAME = "SyncInBackgroundWork" private const val UNIQUE_WORK_NAME = "SyncInBackgroundWork"
fun enqueueDelayed() { fun enqueueDelayed(context: Context) {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "enqueueDelayed") Log.d(LOG_TAG, "enqueueDelayed")
} }
WorkManager.getInstance().beginUniqueWork( WorkManager.getInstance(context).beginUniqueWork(
UNIQUE_WORK_NAME, UNIQUE_WORK_NAME,
ExistingWorkPolicy.KEEP, ExistingWorkPolicy.KEEP,
OneTimeWorkRequest.Builder(SyncInBackgroundWorker::class.java) OneTimeWorkRequest.Builder(SyncInBackgroundWorker::class.java)
@ -54,8 +54,8 @@ class SyncInBackgroundWorker(val context: Context, workerParameters: WorkerParam
).enqueue() ).enqueue()
} }
fun deschedule() { fun deschedule(context: Context) {
WorkManager.getInstance().cancelUniqueWork(UNIQUE_WORK_NAME) WorkManager.getInstance(context).cancelUniqueWork(UNIQUE_WORK_NAME)
} }
} }