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

Fix invalid foreground service type exception #1039

Merged
merged 3 commits into from
Oct 12, 2023
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 @@ -32,6 +32,7 @@ import com.infomaniak.drive.data.models.MqttNotification
import com.infomaniak.drive.utils.NotificationUtils.notifyCompat
import com.infomaniak.lib.core.utils.Utils.createRefreshTimer
import java.util.Date
import java.util.UUID

class BulkOperationWorker(context: Context, workerParams: WorkerParameters) : ListenableWorker(context, workerParams) {

Expand All @@ -44,7 +45,7 @@ class BulkOperationWorker(context: Context, workerParams: WorkerParameters) : Li
private lateinit var timer: CountDownTimer
private lateinit var lastReception: Date

private var notificationId: Int = 0
private var notificationId: Int = UUID.randomUUID().hashCode()
private var totalFiles: Int = 0

override fun startWork(): ListenableFuture<Result> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class DownloadWorker(context: Context, workerParams: WorkerParameters) : Corouti
}
}
}.also {
file?.id?.let(notificationManagerCompat::cancel)
Log.i(TAG, "Work finished")
}
}
Expand All @@ -112,25 +113,33 @@ class DownloadWorker(context: Context, workerParams: WorkerParameters) : Corouti
if (offlineFile?.exists() == true) offlineFile?.delete()
if (cacheFile?.exists() == true) cacheFile.delete()


if (file == null || offlineFile == null) getFileFromRemote()

return startOfflineDownload()
}

private fun DownloadWorker.downloadNotification(): NotificationCompat.Builder {
val cancelPendingIntent = WorkManager.getInstance(applicationContext).createCancelPendingIntent(id)
val cancelAction =
NotificationCompat.Action(null, applicationContext.getString(R.string.buttonCancel), cancelPendingIntent)
val downloadNotification = applicationContext.downloadProgressNotification().apply {
val cancelAction = NotificationCompat.Action(
/* icon = */ null,
/* title = */ applicationContext.getString(R.string.buttonCancel),
/* intent = */ cancelPendingIntent
)
return applicationContext.downloadProgressNotification().apply {
setOngoing(true)
setContentTitle(fileName)
addAction(cancelAction)
setForeground(ForegroundInfo(fileId, build()))
}
}

if (file == null || offlineFile == null) getFileFromRemote()

return startOfflineDownload(downloadNotification)
override suspend fun getForegroundInfo(): ForegroundInfo {
return ForegroundInfo(fileId, downloadNotification().build())
}

private suspend fun startOfflineDownload(
downloadNotification: NotificationCompat.Builder,
): Result = withContext(Dispatchers.IO) {
private suspend fun startOfflineDownload(): Result = withContext(Dispatchers.IO) {
val (file, offlineFile) = file!! to offlineFile!!
val downloadNotification = downloadNotification()
val lastUpdate = workDataOf(PROGRESS to 100, FILE_ID to file.id)
val okHttpClient = AccountUtils.getHttpClient(userDrive.userId, null)
val response = downloadFileResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ object BulkOperationsUtils {
.build()
).get()?.isNotEmpty() == true
}
}
}
8 changes: 8 additions & 0 deletions app/src/main/java/com/infomaniak/drive/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.work.OneTimeWorkRequest
import androidx.work.OutOfQuotaPolicy
import coil.load
import coil.request.Disposable
import com.google.android.material.card.MaterialCardView
Expand Down Expand Up @@ -376,3 +378,9 @@ fun <T> ApiResponse<ArrayList<T>>.isLastPage() = (data?.size ?: 0) < itemsPerPag
fun Context.getInfomaniakLogin(): InfomaniakLogin {
return InfomaniakLogin(this, appUID = BuildConfig.APPLICATION_ID, clientID = BuildConfig.CLIENT_ID)
}

//region Worker
fun OneTimeWorkRequest.Builder.setExpeditedIfAvailable() = apply {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
}
//endregion
4 changes: 1 addition & 3 deletions app/src/main/java/com/infomaniak/drive/utils/SyncUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ 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)
}
.setExpeditedIfAvailable()
.setInputData(data)
.build()
WorkManager.getInstance(this).enqueueUniqueWork(UploadWorker.TAG, ExistingWorkPolicy.REPLACE, request)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/infomaniak/drive/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ object Utils {
.addTag(file.getWorkerTag())
.setInputData(inputData)
.setConstraints(constraints)
.setExpeditedIfAvailable()
.build()

workManager.enqueueUniqueWork(DownloadWorker.TAG, ExistingWorkPolicy.APPEND_OR_REPLACE, downloadRequest)
Expand Down