Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to fix worker crash #308

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
XayahSuSuSu marked this conversation as resolved.
Show resolved Hide resolved
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
Loading