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) {
ReportUninstallWorker.enqueue(
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
DatabaseBackup.with(appLogic.context).tryCreateDatabaseBackupAsync()
PeriodicSyncInBackgroundWorker.disable()
CheckUpdateWorker.deschedule()
PeriodicSyncInBackgroundWorker.disable(appLogic.context)
CheckUpdateWorker.deschedule(appLogic.context)
}
suspend fun dangerousRemoteReset() {

View file

@ -99,9 +99,9 @@ class BackgroundTaskLogic(val appLogic: AppLogic) {
appLogic.database.config().getEnableBackgroundSyncAsync().ignoreUnchanged().observeForever {
if (it) {
PeriodicSyncInBackgroundWorker.enable()
PeriodicSyncInBackgroundWorker.enable(appLogic.context)
} 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
* 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()
SyncInBackgroundWorker.deschedule()
SyncInBackgroundWorker.deschedule(logic.context)
lastSyncExceptionInternal.postValue(null)
// wait 2 to 3 seconds before any next sync (debounce)
@ -150,7 +150,7 @@ class SyncUtil (private val logic: AppLogic) {
importantSyncRequested.postValue(true)
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
* it under the terms of the GNU General Public License as published by
@ -34,6 +34,8 @@ object ManageDeviceBackgroundSync {
activityViewModel: ActivityViewModel,
fragmentManager: FragmentManager
) {
val context = view.root.context
view.titleView.setOnClickListener {
HelpDialogFragment.newInstance(
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
if (isChecked) {
PeriodicSyncInBackgroundWorker.enable()
PeriodicSyncInBackgroundWorker.enable(context)
} else {
PeriodicSyncInBackgroundWorker.disable()
PeriodicSyncInBackgroundWorker.disable(context)
}
} else {
view.checkbox.isChecked = enable

View file

@ -123,7 +123,7 @@ class SetupParentModeModel(application: Application): AndroidViewModel(applicati
DatabaseBackup.with(getApplication()).tryCreateDatabaseBackupAsync()
if (enableBackgroundSync) {
PeriodicSyncInBackgroundWorker.enable()
PeriodicSyncInBackgroundWorker.enable(getApplication())
}
UpdateUtil.setEnableChecks(getApplication(), enableUpdateChecks)
@ -194,7 +194,7 @@ class SetupParentModeModel(application: Application): AndroidViewModel(applicati
DatabaseBackup.with(getApplication()).tryCreateDatabaseBackupAsync()
if (enableBackgroundSync) {
PeriodicSyncInBackgroundWorker.enable()
PeriodicSyncInBackgroundWorker.enable(getApplication())
}
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
* it under the terms of the GNU General Public License as published by
@ -48,7 +48,7 @@ object UpdateUtil {
database.config().setUpdatesEnabledSync(true)
}
CheckUpdateWorker.schedule()
CheckUpdateWorker.schedule(context)
}
fun disableChecks(context: Context) {
@ -58,7 +58,7 @@ object UpdateUtil {
database.config().setUpdatesEnabledSync(false)
}
CheckUpdateWorker.deschedule()
CheckUpdateWorker.deschedule(context)
}
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
* 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 {
private const val UNIQUE_WORK_NAME = "CheckUpdateWorker"
fun schedule() {
WorkManager.getInstance().enqueueUniquePeriodicWork(
fun schedule(context: Context) {
WorkManager.getInstance(context).enqueueUniquePeriodicWork(
UNIQUE_WORK_NAME,
ExistingPeriodicWorkPolicy.KEEP,
PeriodicWorkRequestBuilder<CheckUpdateWorker>(1, TimeUnit.DAYS)
@ -42,8 +42,8 @@ class CheckUpdateWorker(private val context: Context, workerParameters: WorkerPa
)
}
fun deschedule() {
WorkManager.getInstance().cancelUniqueWork(UNIQUE_WORK_NAME)
fun deschedule(context: Context) {
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
import android.content.Context
@ -15,12 +30,12 @@ class PeriodicSyncInBackgroundWorker(private val context: Context, workerParamet
private const val LOG_TAG = "PeriodicBackgroundSync"
private const val UNIQUE_WORK_NAME = "PeriodicSyncInBackgroundWorker"
fun enable() {
fun enable(context: Context) {
if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "enable()")
}
WorkManager.getInstance().enqueueUniquePeriodicWork(
WorkManager.getInstance(context).enqueueUniquePeriodicWork(
UNIQUE_WORK_NAME,
ExistingPeriodicWorkPolicy.KEEP,
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) {
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
* 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 LOG_TAG = "ReportUninstallWorker"
fun enqueue(deviceAuthToken: String, customServerUrl: String) {
fun enqueue(deviceAuthToken: String, customServerUrl: String, context: Context) {
if (deviceAuthToken.isEmpty()) {
return
}
@ -40,7 +40,7 @@ class ReportUninstallWorker(val context: Context, workerParameters: WorkerParame
Log.d(LOG_TAG, "enqueue()")
}
WorkManager.getInstance().enqueue(
WorkManager.getInstance(context).enqueue(
OneTimeWorkRequest.Builder(ReportUninstallWorker::class.java)
.setConstraints(
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
* 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 UNIQUE_WORK_NAME = "SyncInBackgroundWork"
fun enqueueDelayed() {
fun enqueueDelayed(context: Context) {
if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "enqueueDelayed")
}
WorkManager.getInstance().beginUniqueWork(
WorkManager.getInstance(context).beginUniqueWork(
UNIQUE_WORK_NAME,
ExistingWorkPolicy.KEEP,
OneTimeWorkRequest.Builder(SyncInBackgroundWorker::class.java)
@ -54,8 +54,8 @@ class SyncInBackgroundWorker(val context: Context, workerParameters: WorkerParam
).enqueue()
}
fun deschedule() {
WorkManager.getInstance().cancelUniqueWork(UNIQUE_WORK_NAME)
fun deschedule(context: Context) {
WorkManager.getInstance(context).cancelUniqueWork(UNIQUE_WORK_NAME)
}
}