Skip to content
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
24 changes: 9 additions & 15 deletions app/src/main/java/com/infomaniak/drive/data/api/UploadTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import com.infomaniak.drive.data.services.UploadWorker
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
import com.infomaniak.drive.utils.NotificationUtils.uploadProgressNotification
import io.ktor.client.HttpClient
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.plugins.onUpload
Expand Down Expand Up @@ -93,6 +92,8 @@ class UploadTask(
private val context: Context,
private val uploadFile: UploadFile,
private val setProgress: KSuspendFunction1<Data, Unit>,
private val notificationManagerCompat: NotificationManagerCompat,
private val uploadNotificationBuilder: NotificationCompat.Builder,
) {

private val fileChunkSizeManager = FileChunkSizeManager()
Expand All @@ -104,17 +105,16 @@ class UploadTask(
it.tryEmit(Unit)
}

private lateinit var notificationManagerCompat: NotificationManagerCompat
private lateinit var uploadNotification: NotificationCompat.Builder
private var uploadNotificationElapsedTime = ELAPSED_TIME
private var uploadNotificationStartTime = 0L

suspend fun start(): Boolean {
notificationManagerCompat = NotificationManagerCompat.from(context)

uploadNotification = context.uploadProgressNotification()
uploadNotification.apply {
uploadNotificationBuilder.apply {
setContentTitle(uploadFile.fileName)
setOngoing(true)
setContentText(null)
setProgress(0, 0, false)
setSmallIcon(android.R.drawable.stat_sys_upload)
notificationManagerCompat.notifyCompat(CURRENT_UPLOAD_ID, this)
}

Expand Down Expand Up @@ -146,7 +146,6 @@ class UploadTask(
Sentry.captureException(exception) { scope -> scope.level = SentryLevel.WARNING }
} catch (exception: UploadNotTerminated) {
SentryLog.w(TAG, "upload not terminated", exception)
notificationManagerCompat.cancel(CURRENT_UPLOAD_ID)
Sentry.captureException(exception) { scope -> scope.level = SentryLevel.WARNING }
} catch (exception: QuotaExceededException) {
if (UploadFile.getAppSyncSettings()?.driveId == uploadFile.driveId) {
Expand All @@ -156,8 +155,6 @@ class UploadTask(
} catch (exception: Exception) {
exception.printStackTrace()
throw exception
} finally {
notificationManagerCompat.cancel(CURRENT_UPLOAD_ID)
}
return false
}
Expand Down Expand Up @@ -286,16 +283,14 @@ class UploadTask(
}

private suspend fun finishUpload(uri: Uri) {
uploadNotification.apply {
uploadNotificationBuilder.apply {
setOngoing(false)
setContentText("100%")
setSmallIcon(android.R.drawable.stat_sys_upload_done)
setProgress(0, 0, false)
notificationManagerCompat.notifyCompat(CURRENT_UPLOAD_ID, this)
}
shareProgress(100, true)
UploadFile.uploadFinished(uri)
notificationManagerCompat.cancel(CURRENT_UPLOAD_ID)
}

private suspend fun uploadChunk(
Expand Down Expand Up @@ -409,7 +404,6 @@ class UploadTask(
}
}
val bodyResponse = String(bytes)
notificationManagerCompat.cancel(CURRENT_UPLOAD_ID)
val apiResponse = try {
gson.fromJson(bodyResponse, ApiResponse::class.java)!! // Might be empty when http 502 Bad gateway happens
} catch (_: Exception) {
Expand All @@ -432,7 +426,7 @@ class UploadTask(
currentCoroutineContext().ensureActive()

if (uploadNotificationElapsedTime >= ELAPSED_TIME) {
uploadNotification.apply {
uploadNotificationBuilder.apply {
setContentIntent(uploadFile.progressPendingIntent())
setContentText("${progress}%")
setProgress(100, progress, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import com.infomaniak.core.legacy.utils.getFileName
import com.infomaniak.core.legacy.utils.getFileSize
import com.infomaniak.core.legacy.utils.hasPermissions
import com.infomaniak.core.network.api.ApiController.gson
import com.infomaniak.core.notifications.cancelNotification
import com.infomaniak.core.notifications.notifyCompat
import com.infomaniak.core.sentry.SentryLog
import com.infomaniak.drive.R
Expand All @@ -61,7 +62,7 @@ import com.infomaniak.drive.utils.MediaFoldersProvider.IMAGES_BUCKET_ID
import com.infomaniak.drive.utils.MediaFoldersProvider.VIDEO_BUCKET_ID
import com.infomaniak.drive.utils.NotificationUtils
import com.infomaniak.drive.utils.NotificationUtils.buildGeneralNotification
import com.infomaniak.drive.utils.NotificationUtils.cancelNotification
import com.infomaniak.drive.utils.NotificationUtils.uploadProgressNotification
import com.infomaniak.drive.utils.SyncUtils
import com.infomaniak.drive.utils.getAvailableMemory
import com.infomaniak.drive.utils.uri
Expand Down Expand Up @@ -94,6 +95,8 @@ class UploadWorker(appContext: Context, params: WorkerParameters) : CoroutineWor
private var pendingCount = 0

private val readMediaPermissions = DrivePermissions.permissionsFor(DrivePermissions.Type.ReadingMediaForSync).toTypedArray()
private val notificationManagerCompat by lazy { NotificationManagerCompat.from(applicationContext) }
private val uploadNotificationBuilder by lazy { applicationContext.uploadProgressNotification() }

override suspend fun doWork(): Result {

Expand Down Expand Up @@ -282,7 +285,6 @@ class UploadWorker(appContext: Context, params: WorkerParameters) : CoroutineWor
val uri = getUriObject()

currentUploadFile = this@upload
applicationContext.cancelNotification(NotificationUtils.CURRENT_UPLOAD_ID)
updateUploadCountNotification()

try {
Expand Down Expand Up @@ -347,7 +349,13 @@ class UploadWorker(appContext: Context, params: WorkerParameters) : CoroutineWor

SentryLog.d(TAG, "startUploadFile (size: $fileSize)")

return UploadTask(context = applicationContext, uploadFile = this, setProgress = ::setProgress).run {
return UploadTask(
context = applicationContext,
uploadFile = this,
setProgress = ::setProgress,
notificationManagerCompat = notificationManagerCompat,
uploadNotificationBuilder = uploadNotificationBuilder
).run {
currentUploadTask = this
start().also { isUploaded ->
if (isUploaded) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ object NotificationUtils : NotificationUtilsCore() {
setTicker(ticker)
setSmallIcon(icon)
setAutoCancel(true)
setContentText("0%")
setOnlyAlertOnce(true)
setProgress(100, 0, true)
}
Expand Down
Loading