From 97e131503ba04a060b4686a1289a9f29f72e9bb0 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Wed, 20 Mar 2024 11:44:39 +0600 Subject: [PATCH] fix: RefreshCollectionWorker throws IllegalArgException cause app unable to use In RefreshCollectionWorker serviceEntity is pulled at the class init state, if return nothing it throws IllegalArgException. As it is thrown in the init stage, not inside the doWork method, the worker is not canceled & it crashes the app. After restarting the app (event after device reboot or app version upgrade) the worker retrigger & crash the app again, makes the app unusuable. This worker is called right after the user loggedIn. If user able to logout before the worker trigger by the os, the above mentioned scenario is observed. issue: https://gitlab.e.foundation/e/backlog/-/issues/7421 --- .../RefreshCollectionsWorker.kt | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/at/bitfire/davdroid/servicedetection/RefreshCollectionsWorker.kt b/app/src/main/kotlin/at/bitfire/davdroid/servicedetection/RefreshCollectionsWorker.kt index 8efd63168..53e5abee4 100644 --- a/app/src/main/kotlin/at/bitfire/davdroid/servicedetection/RefreshCollectionsWorker.kt +++ b/app/src/main/kotlin/at/bitfire/davdroid/servicedetection/RefreshCollectionsWorker.kt @@ -168,11 +168,16 @@ class RefreshCollectionsWorker @AssistedInject constructor( } val serviceId: Long = inputData.getLong(ARG_SERVICE_ID, -1) - val service = db.serviceDao().get(serviceId) - ?: throw IllegalArgumentException("Service #$serviceId not found") - val account = Account(service.accountName, service.accountType) override fun doWork(): Result { + val service = db.serviceDao().get(serviceId) + if (service == null) { + Logger.log.severe("Service #$serviceId not found") + return Result.failure() + } + + val account = Account(service.accountName, service.accountType) + try { Logger.log.info("Refreshing ${service.type} collections of service #$service") @@ -213,7 +218,8 @@ class RefreshCollectionsWorker @AssistedInject constructor( .putExtra(SettingsActivity.EXTRA_ACCOUNT, account) notifyRefreshError( applicationContext.getString(R.string.sync_error_authentication_failed), - settingsIntent + settingsIntent, + account.name ) return Result.failure() } catch (e: Exception) { @@ -225,7 +231,8 @@ class RefreshCollectionsWorker @AssistedInject constructor( .build() notifyRefreshError( applicationContext.getString(R.string.refresh_collections_worker_refresh_couldnt_refresh), - debugIntent + debugIntent, + account.name ) return Result.failure() } @@ -250,7 +257,7 @@ class RefreshCollectionsWorker @AssistedInject constructor( completer.set(ForegroundInfo(NotificationUtils.NOTIFY_SYNC_EXPEDITED, notification)) } - private fun notifyRefreshError(contentText: String, contentIntent: Intent) { + private fun notifyRefreshError(contentText: String, contentIntent: Intent, accountName: String) { val notify = NotificationUtils.newBuilder(applicationContext, NotificationUtils.CHANNEL_GENERAL) .setSmallIcon(R.drawable.ic_sync_problem_notify) @@ -264,7 +271,7 @@ class RefreshCollectionsWorker @AssistedInject constructor( PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE ) ) - .setSubText(account.name) + .setSubText(accountName) .setCategory(NotificationCompat.CATEGORY_ERROR) .build() NotificationManagerCompat.from(applicationContext) -- GitLab