Skip to content

Commit

Permalink
fix: Since android 12, setForeground throw an exception
Browse files Browse the repository at this point in the history
Fixes KDRIVE-ANDROID-K1, fixes KDRIVE-ANDROID-PM, fixes KDRIVE-ANDROID-FK
  • Loading branch information
sirambd committed Sep 19, 2023
1 parent 624f8a0 commit bdb4b8c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 32 deletions.
10 changes: 0 additions & 10 deletions app/src/main/java/com/infomaniak/drive/data/api/UploadTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -315,7 +314,6 @@ class UploadTask(
ensureActive()

if (uploadNotificationElapsedTime >= ELAPSED_TIME) {
updatePendingCountNotificationIfNeeded()
uploadNotification.apply {
setContentIntent(uploadFile.progressPendingIntent(context))
setContentText("${currentProgress}%")
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@ 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.*
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.*
Expand All @@ -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 {

Expand All @@ -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 }
Expand All @@ -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? {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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 -> {
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/infomaniak/drive/utils/SyncUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ object SyncUtils {
if (!isSyncActive() || force) {
val request = OneTimeWorkRequestBuilder<UploadWorker>()
.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)
Expand Down

0 comments on commit bdb4b8c

Please sign in to comment.