From bdb4b8ce7e1ed67f8b247f84ffc3bedba3af63c8 Mon Sep 17 00:00:00 2001 From: Abdourahamane Boinaidi Date: Tue, 19 Sep 2023 18:43:39 +0200 Subject: [PATCH] fix: Since android 12, setForeground throw an exception Fixes KDRIVE-ANDROID-K1, fixes KDRIVE-ANDROID-PM, fixes KDRIVE-ANDROID-FK --- .../infomaniak/drive/data/api/UploadTask.kt | 10 ----- .../drive/data/services/UploadWorker.kt | 40 +++++++++---------- .../data/services/UploadWorkerThrowable.kt | 1 + .../com/infomaniak/drive/utils/SyncUtils.kt | 3 ++ 4 files changed, 22 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/com/infomaniak/drive/data/api/UploadTask.kt b/app/src/main/java/com/infomaniak/drive/data/api/UploadTask.kt index 92ed26ae0e..35b035d134 100644 --- a/app/src/main/java/com/infomaniak/drive/data/api/UploadTask.kt +++ b/app/src/main/java/com/infomaniak/drive/data/api/UploadTask.kt @@ -31,7 +31,6 @@ import com.infomaniak.drive.data.models.drive.Drive import com.infomaniak.drive.data.models.upload.UploadSession import com.infomaniak.drive.data.models.upload.ValidChunks import com.infomaniak.drive.data.services.UploadWorker -import com.infomaniak.drive.data.services.UploadWorker.Companion.updateUploadCountNotification import com.infomaniak.drive.data.sync.UploadNotifications.progressPendingIntent import com.infomaniak.drive.utils.NotificationUtils.CURRENT_UPLOAD_ID import com.infomaniak.drive.utils.NotificationUtils.ELAPSED_TIME @@ -315,7 +314,6 @@ class UploadTask( ensureActive() if (uploadNotificationElapsedTime >= ELAPSED_TIME) { - updatePendingCountNotificationIfNeeded() uploadNotification.apply { setContentIntent(uploadFile.progressPendingIntent(context)) setContentText("${currentProgress}%") @@ -336,14 +334,6 @@ class UploadTask( ) } - private fun CoroutineScope.updatePendingCountNotificationIfNeeded() { - val latestPendingCount = UploadFile.getAllPendingUploadsCount() - if (worker.pendingCount != latestPendingCount) { - updateUploadCountNotification(context, latestPendingCount) - worker.pendingCount = latestPendingCount - } - } - private suspend fun shareProgress(progress: Int = 0, isUploaded: Boolean = false) { worker.setProgress( workDataOf( diff --git a/app/src/main/java/com/infomaniak/drive/data/services/UploadWorker.kt b/app/src/main/java/com/infomaniak/drive/data/services/UploadWorker.kt index 21c8c5ba79..ab5e138d7b 100644 --- a/app/src/main/java/com/infomaniak/drive/data/services/UploadWorker.kt +++ b/app/src/main/java/com/infomaniak/drive/data/services/UploadWorker.kt @@ -39,7 +39,6 @@ import com.infomaniak.drive.data.models.UploadFile import com.infomaniak.drive.data.services.UploadWorkerThrowable.runUploadCatching import com.infomaniak.drive.data.sync.UploadNotifications import com.infomaniak.drive.data.sync.UploadNotifications.NOTIFICATION_FILES_LIMIT -import com.infomaniak.drive.data.sync.UploadNotifications.setupCurrentUploadNotification import com.infomaniak.drive.data.sync.UploadNotifications.showUploadedFilesNotification import com.infomaniak.drive.data.sync.UploadNotifications.syncSettingsActivityPendingIntent import com.infomaniak.drive.utils.* @@ -47,7 +46,6 @@ import com.infomaniak.drive.utils.MediaFoldersProvider.IMAGES_BUCKET_ID import com.infomaniak.drive.utils.MediaFoldersProvider.VIDEO_BUCKET_ID import com.infomaniak.drive.utils.NotificationUtils.buildGeneralNotification import com.infomaniak.drive.utils.NotificationUtils.cancelNotification -import com.infomaniak.drive.utils.NotificationUtils.uploadServiceNotification import com.infomaniak.drive.utils.SyncUtils.syncImmediately import com.infomaniak.lib.core.api.ApiController import com.infomaniak.lib.core.utils.* @@ -68,7 +66,7 @@ class UploadWorker(appContext: Context, params: WorkerParameters) : CoroutineWor var currentUploadFile: UploadFile? = null var currentUploadTask: UploadTask? = null var uploadedCount = 0 - var pendingCount = 0 + private var pendingCount = 0 override suspend fun doWork(): Result { @@ -78,8 +76,6 @@ class UploadWorker(appContext: Context, params: WorkerParameters) : CoroutineWor // Checks if the maximum number of retry allowed is reached if (runAttemptCount >= MAX_RETRY_COUNT) return Result.failure() - moveServiceToForeground() - return runUploadCatching { // Check if we have the required permissions before continuing checkPermissions()?.let { return@runUploadCatching it } @@ -100,11 +96,10 @@ class UploadWorker(appContext: Context, params: WorkerParameters) : CoroutineWor } } - private suspend fun moveServiceToForeground() { - applicationContext.uploadServiceNotification().apply { - setContentTitle(applicationContext.getString(R.string.notificationUploadServiceChannelName)) - setForeground(ForegroundInfo(NotificationUtils.UPLOAD_SERVICE_ID, build())) - } + override suspend fun getForegroundInfo(): ForegroundInfo { + val pendingCount = if (this.pendingCount > 0) this.pendingCount else UploadFile.getAllPendingUploadsCount() + val currentUploadNotification = UploadNotifications.getCurrentUploadNotification(applicationContext, pendingCount) + return ForegroundInfo(NotificationUtils.UPLOAD_SERVICE_ID, currentUploadNotification.build()) } private fun checkPermissions(): Result? { @@ -144,7 +139,7 @@ class UploadWorker(appContext: Context, params: WorkerParameters) : CoroutineWor for (uploadFile in uploadFiles) { Log.d(TAG, "startSyncFiles> upload ${uploadFile.fileName}") - if (uploadFile.initUpload(pendingCount)) { + if (uploadFile.initUpload()) { successNames.add(uploadFile.fileName) successCount++ } else { @@ -178,12 +173,12 @@ class UploadWorker(appContext: Context, params: WorkerParameters) : CoroutineWor } } - private suspend fun UploadFile.initUpload(pendingCount: Int) = withContext(Dispatchers.IO) { + private suspend fun UploadFile.initUpload() = withContext(Dispatchers.IO) { val uri = getUriObject() currentUploadFile = this@initUpload applicationContext.cancelNotification(NotificationUtils.CURRENT_UPLOAD_ID) - updateUploadCountNotification(applicationContext, pendingCount) + updateUploadCountNotification(applicationContext) try { if (uri.scheme.equals(ContentResolver.SCHEME_FILE)) { @@ -242,6 +237,16 @@ class UploadWorker(appContext: Context, params: WorkerParameters) : CoroutineWor } } + private var uploadCountNotificationJob: Job? = null + private fun CoroutineScope.updateUploadCountNotification(context: Context) { + uploadCountNotificationJob?.cancel() + uploadCountNotificationJob = launch { + // We wait a little otherwise it is too fast and the notification may not be updated + delay(NotificationUtils.ELAPSED_TIME) + if (isActive) UploadNotifications.setupCurrentUploadNotification(context, pendingCount) + } + } + private fun UploadFile.handleException(exception: Exception) { when (exception) { is SecurityException, is IllegalStateException, is IllegalArgumentException -> { @@ -458,15 +463,6 @@ class UploadWorker(appContext: Context, params: WorkerParameters) : CoroutineWor .build() } - fun CoroutineScope.updateUploadCountNotification(context: Context, pendingCount: Int) { - launch { - // We wait a little otherwise it is too fast and the notification may not be updated - delay(NotificationUtils.ELAPSED_TIME) - ensureActive() - setupCurrentUploadNotification(context, pendingCount) - } - } - @SuppressLint("MissingPermission") fun Context.showSyncConfigNotification() { val pendingIntent = syncSettingsActivityPendingIntent() diff --git a/app/src/main/java/com/infomaniak/drive/data/services/UploadWorkerThrowable.kt b/app/src/main/java/com/infomaniak/drive/data/services/UploadWorkerThrowable.kt index 983bbb5505..11ae06608f 100644 --- a/app/src/main/java/com/infomaniak/drive/data/services/UploadWorkerThrowable.kt +++ b/app/src/main/java/com/infomaniak/drive/data/services/UploadWorkerThrowable.kt @@ -114,6 +114,7 @@ object UploadWorkerThrowable { } private fun UploadWorker.cancelUploadNotification() { + applicationContext.cancelNotification(NotificationUtils.UPLOAD_SERVICE_ID) applicationContext.cancelNotification(NotificationUtils.CURRENT_UPLOAD_ID) Sentry.addBreadcrumb(Breadcrumb().apply { category = UploadWorker.BREADCRUMB_TAG diff --git a/app/src/main/java/com/infomaniak/drive/utils/SyncUtils.kt b/app/src/main/java/com/infomaniak/drive/utils/SyncUtils.kt index ac9133be66..3ea64e529c 100644 --- a/app/src/main/java/com/infomaniak/drive/utils/SyncUtils.kt +++ b/app/src/main/java/com/infomaniak/drive/utils/SyncUtils.kt @@ -84,6 +84,9 @@ object SyncUtils { if (!isSyncActive() || force) { val request = OneTimeWorkRequestBuilder() .setConstraints(UploadWorker.workConstraints()) + .apply { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) + } .setInputData(data) .build() WorkManager.getInstance(this).enqueueUniqueWork(UploadWorker.TAG, ExistingWorkPolicy.REPLACE, request)