From af51511ea2ea33d95f69ec57490dfb11f88360b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Furkan=20Karc=C4=B1o=C4=9Flu?= <45714956+frknkrc44@users.noreply.github.com> Date: Wed, 18 Sep 2024 18:33:36 +0300 Subject: [PATCH] feat: Fix worker crash (#308) * fix: Try to workaround worker crash * fix: Try to get rid off NPE --- .../core/work/workers/AppsFastInitWorker.kt | 32 ++++++++++++++----- .../core/work/workers/AppsFastUpdateWorker.kt | 32 ++++++++++++++----- .../xayah/core/work/workers/AppsInitWorker.kt | 32 ++++++++++++++----- .../xayah/core/work/workers/AppsLoadWorker.kt | 32 ++++++++++++++----- .../core/work/workers/AppsUpdateWorker.kt | 32 ++++++++++++++----- .../core/work/workers/FilesLoadWorker.kt | 32 ++++++++++++++----- .../core/work/workers/FilesUpdateWorker.kt | 16 +++++++--- 7 files changed, 156 insertions(+), 52 deletions(-) diff --git a/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsFastInitWorker.kt b/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsFastInitWorker.kt index 5e7c1fd780..de9a5fe7ac 100644 --- a/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsFastInitWorker.kt +++ b/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsFastInitWorker.kt @@ -3,6 +3,7 @@ package com.xayah.core.work.workers import android.content.Context import androidx.hilt.work.HiltWorker import androidx.work.CoroutineWorker +import androidx.work.ForegroundInfo import androidx.work.OneTimeWorkRequestBuilder import androidx.work.OutOfQuotaPolicy import androidx.work.WorkerParameters @@ -24,18 +25,33 @@ internal class AppsFastInitWorker @AssistedInject constructor( private val appsRepo: AppsRepo, ) : CoroutineWorker(appContext, workerParams) { private val mNotificationBuilder by lazy { NotificationUtil.getProgressNotificationBuilder(appContext) } + private var mNotificationInfo: ForegroundInfo? = null + + override suspend fun getForegroundInfo(): ForegroundInfo { + if (mNotificationInfo == null) { + mNotificationInfo = NotificationUtil.createForegroundInfo( + appContext, + mNotificationBuilder, + appContext.getString(R.string.initializing_app_list), + "" + ) + } + + return mNotificationInfo!! + } override suspend fun doWork(): Result = withContext(defaultDispatcher) { appsRepo.fastInitialize { cur, max, content -> + mNotificationInfo = NotificationUtil.createForegroundInfo( + appContext, + mNotificationBuilder, + appContext.getString(R.string.initializing_app_list), + content, + max, + cur + ) setForeground( - NotificationUtil.createForegroundInfo( - appContext, - mNotificationBuilder, - appContext.getString(R.string.initializing_app_list), - content, - max, - cur - ) + mNotificationInfo!! ) } Result.success() diff --git a/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsFastUpdateWorker.kt b/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsFastUpdateWorker.kt index 4895200663..a23f1f74f2 100644 --- a/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsFastUpdateWorker.kt +++ b/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsFastUpdateWorker.kt @@ -3,6 +3,7 @@ package com.xayah.core.work.workers import android.content.Context import androidx.hilt.work.HiltWorker import androidx.work.CoroutineWorker +import androidx.work.ForegroundInfo import androidx.work.OneTimeWorkRequestBuilder import androidx.work.OutOfQuotaPolicy import androidx.work.WorkerParameters @@ -24,18 +25,33 @@ internal class AppsFastUpdateWorker @AssistedInject constructor( private val appsRepo: AppsRepo, ) : CoroutineWorker(appContext, workerParams) { private val mNotificationBuilder by lazy { NotificationUtil.getProgressNotificationBuilder(appContext) } + private var mNotificationInfo: ForegroundInfo? = null + + override suspend fun getForegroundInfo(): ForegroundInfo { + if (mNotificationInfo == null) { + mNotificationInfo = NotificationUtil.createForegroundInfo( + appContext, + mNotificationBuilder, + appContext.getString(R.string.updating_app_list), + "" + ) + } + + return mNotificationInfo!! + } override suspend fun doWork(): Result = withContext(defaultDispatcher) { appsRepo.fastUpdate { cur, max, content -> + mNotificationInfo = NotificationUtil.createForegroundInfo( + appContext, + mNotificationBuilder, + appContext.getString(R.string.updating_app_list), + content, + max, + cur + ) setForeground( - NotificationUtil.createForegroundInfo( - appContext, - mNotificationBuilder, - appContext.getString(R.string.updating_app_list), - content, - max, - cur - ) + mNotificationInfo!! ) } Result.success() diff --git a/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsInitWorker.kt b/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsInitWorker.kt index a6e50e7064..9bbec74c9f 100644 --- a/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsInitWorker.kt +++ b/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsInitWorker.kt @@ -3,6 +3,7 @@ package com.xayah.core.work.workers import android.content.Context import androidx.hilt.work.HiltWorker import androidx.work.CoroutineWorker +import androidx.work.ForegroundInfo import androidx.work.OneTimeWorkRequestBuilder import androidx.work.OutOfQuotaPolicy import androidx.work.WorkerParameters @@ -24,18 +25,33 @@ internal class AppsInitWorker @AssistedInject constructor( private val appsRepo: AppsRepo, ) : CoroutineWorker(appContext, workerParams) { private val mNotificationBuilder by lazy { NotificationUtil.getProgressNotificationBuilder(appContext) } + private var mNotificationInfo: ForegroundInfo? = null + + override suspend fun getForegroundInfo(): ForegroundInfo { + if (mNotificationInfo == null) { + mNotificationInfo = NotificationUtil.createForegroundInfo( + appContext, + mNotificationBuilder, + appContext.getString(R.string.initializing_app_list), + "" + ) + } + + return mNotificationInfo!! + } override suspend fun doWork(): Result = withContext(defaultDispatcher) { appsRepo.fullInitialize { cur, max, content -> + mNotificationInfo = NotificationUtil.createForegroundInfo( + appContext, + mNotificationBuilder, + appContext.getString(R.string.initializing_app_list), + content, + max, + cur + ) setForeground( - NotificationUtil.createForegroundInfo( - appContext, - mNotificationBuilder, - appContext.getString(R.string.initializing_app_list), - content, - max, - cur - ) + mNotificationInfo!! ) } Result.success() diff --git a/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsLoadWorker.kt b/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsLoadWorker.kt index a42fe2e88b..4a5e27e54c 100644 --- a/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsLoadWorker.kt +++ b/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsLoadWorker.kt @@ -3,6 +3,7 @@ package com.xayah.core.work.workers import android.content.Context import androidx.hilt.work.HiltWorker import androidx.work.CoroutineWorker +import androidx.work.ForegroundInfo import androidx.work.OneTimeWorkRequestBuilder import androidx.work.OutOfQuotaPolicy import androidx.work.WorkerParameters @@ -26,19 +27,34 @@ internal class AppsLoadWorker @AssistedInject constructor( private val appsRepo: AppsRepo, ) : CoroutineWorker(appContext, workerParams) { private val mNotificationBuilder by lazy { NotificationUtil.getProgressNotificationBuilder(appContext) } + private var mNotificationInfo: ForegroundInfo? = null + + override suspend fun getForegroundInfo(): ForegroundInfo { + if (mNotificationInfo == null) { + mNotificationInfo = NotificationUtil.createForegroundInfo( + appContext, + mNotificationBuilder, + appContext.getString(R.string.loading_backups), + "" + ) + } + + return mNotificationInfo!! + } override suspend fun doWork(): Result = withContext(defaultDispatcher) { val cloudName = inputData.getString(INPUT_DATA_KEY_CLOUD_NAME) appsRepo.load(cloudName) { cur, max, content -> + mNotificationInfo = NotificationUtil.createForegroundInfo( + appContext, + mNotificationBuilder, + appContext.getString(R.string.loading_backups), + content, + max, + cur + ) setForeground( - NotificationUtil.createForegroundInfo( - appContext, - mNotificationBuilder, - appContext.getString(R.string.loading_backups), - content, - max, - cur - ) + mNotificationInfo!! ) } Result.success() diff --git a/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsUpdateWorker.kt b/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsUpdateWorker.kt index d6fc2892b9..b58d503e88 100644 --- a/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsUpdateWorker.kt +++ b/source/core/work/src/main/kotlin/com/xayah/core/work/workers/AppsUpdateWorker.kt @@ -3,6 +3,7 @@ package com.xayah.core.work.workers import android.content.Context import androidx.hilt.work.HiltWorker import androidx.work.CoroutineWorker +import androidx.work.ForegroundInfo import androidx.work.OneTimeWorkRequestBuilder import androidx.work.OutOfQuotaPolicy import androidx.work.WorkerParameters @@ -30,6 +31,20 @@ internal class AppsUpdateWorker @AssistedInject constructor( private val settingsDataRepo: SettingsDataRepo, ) : CoroutineWorker(appContext, workerParams) { private val mNotificationBuilder by lazy { NotificationUtil.getProgressNotificationBuilder(appContext) } + private var mNotificationInfo: ForegroundInfo? = null + + override suspend fun getForegroundInfo(): ForegroundInfo { + if (mNotificationInfo == null) { + mNotificationInfo = NotificationUtil.createForegroundInfo( + appContext, + mNotificationBuilder, + appContext.getString(R.string.updating_app_list), + "" + ) + } + + return mNotificationInfo!! + } override suspend fun doWork(): Result = withContext(defaultDispatcher) { val regular = inputData.getBoolean(INPUT_DATA_KEY_REGULAR, true) @@ -39,15 +54,16 @@ internal class AppsUpdateWorker @AssistedInject constructor( if (regular.not() || hasPassedOneDay) { settingsDataRepo.setAppsUpdateTime(curTime) appsRepo.fullUpdate { cur, max, content -> + mNotificationInfo = NotificationUtil.createForegroundInfo( + appContext, + mNotificationBuilder, + appContext.getString(R.string.updating_app_list), + content, + max, + cur + ) setForeground( - NotificationUtil.createForegroundInfo( - appContext, - mNotificationBuilder, - appContext.getString(R.string.updating_app_list), - content, - max, - cur - ) + mNotificationInfo!! ) } } diff --git a/source/core/work/src/main/kotlin/com/xayah/core/work/workers/FilesLoadWorker.kt b/source/core/work/src/main/kotlin/com/xayah/core/work/workers/FilesLoadWorker.kt index fca0361f8d..06685fa818 100644 --- a/source/core/work/src/main/kotlin/com/xayah/core/work/workers/FilesLoadWorker.kt +++ b/source/core/work/src/main/kotlin/com/xayah/core/work/workers/FilesLoadWorker.kt @@ -3,6 +3,7 @@ package com.xayah.core.work.workers import android.content.Context import androidx.hilt.work.HiltWorker import androidx.work.CoroutineWorker +import androidx.work.ForegroundInfo import androidx.work.OneTimeWorkRequestBuilder import androidx.work.OutOfQuotaPolicy import androidx.work.WorkerParameters @@ -26,19 +27,34 @@ internal class FilesLoadWorker @AssistedInject constructor( private val filesRepo: FilesRepo, ) : CoroutineWorker(appContext, workerParams) { private val mNotificationBuilder by lazy { NotificationUtil.getProgressNotificationBuilder(appContext) } + private var mNotificationInfo: ForegroundInfo? = null + + override suspend fun getForegroundInfo(): ForegroundInfo { + if (mNotificationInfo == null) { + mNotificationInfo = NotificationUtil.createForegroundInfo( + appContext, + mNotificationBuilder, + appContext.getString(R.string.loading_backups), + "" + ) + } + + return mNotificationInfo!! + } override suspend fun doWork(): Result = withContext(defaultDispatcher) { val cloudName = inputData.getString(INPUT_DATA_KEY_CLOUD_NAME) filesRepo.load(cloudName) { cur, max, content -> + mNotificationInfo = NotificationUtil.createForegroundInfo( + appContext, + mNotificationBuilder, + appContext.getString(R.string.loading_backups), + content, + max, + cur + ) setForeground( - NotificationUtil.createForegroundInfo( - appContext, - mNotificationBuilder, - appContext.getString(R.string.loading_backups), - content, - max, - cur - ) + mNotificationInfo!! ) } Result.success() diff --git a/source/core/work/src/main/kotlin/com/xayah/core/work/workers/FilesUpdateWorker.kt b/source/core/work/src/main/kotlin/com/xayah/core/work/workers/FilesUpdateWorker.kt index 5f985c5484..bb41d39089 100644 --- a/source/core/work/src/main/kotlin/com/xayah/core/work/workers/FilesUpdateWorker.kt +++ b/source/core/work/src/main/kotlin/com/xayah/core/work/workers/FilesUpdateWorker.kt @@ -3,6 +3,7 @@ package com.xayah.core.work.workers import android.content.Context import androidx.hilt.work.HiltWorker import androidx.work.CoroutineWorker +import androidx.work.ForegroundInfo import androidx.work.OneTimeWorkRequestBuilder import androidx.work.OutOfQuotaPolicy import androidx.work.WorkerParameters @@ -24,16 +25,23 @@ internal class FilesUpdateWorker @AssistedInject constructor( private val filesRepo: FilesRepo, ) : CoroutineWorker(appContext, workerParams) { private val mNotificationBuilder by lazy { NotificationUtil.getProgressNotificationBuilder(appContext) } + private var mNotificationInfo: ForegroundInfo? = null - override suspend fun doWork(): Result = withContext(defaultDispatcher) { - setForeground( - NotificationUtil.createForegroundInfo( + override suspend fun getForegroundInfo(): ForegroundInfo { + if (mNotificationInfo == null) { + mNotificationInfo = NotificationUtil.createForegroundInfo( appContext, mNotificationBuilder, appContext.getString(R.string.updating_file_list), appContext.getString(R.string.wait_for_remaining_data_processing), ) - ) + } + + return mNotificationInfo!! + } + + override suspend fun doWork(): Result = withContext(defaultDispatcher) { + setForeground(getForegroundInfo()) filesRepo.initialize() Result.success() }