Skip to content

Commit

Permalink
feat: Fix worker crash (#308)
Browse files Browse the repository at this point in the history
* fix: Try to workaround worker crash

* fix: Try to get rid off NPE
  • Loading branch information
frknkrc44 authored Sep 18, 2024
1 parent b872c70 commit af51511
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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!!
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
Expand Down

0 comments on commit af51511

Please sign in to comment.