Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 08b12d86 authored by Jonathan Klee's avatar Jonathan Klee
Browse files

fix: Prevent from adding upload tasks if quota reached

parent fb415141
Loading
Loading
Loading
Loading
Loading
+27 −1
Original line number Diff line number Diff line
@@ -7,12 +7,16 @@
 */
package foundation.e.drive.synchronization

import android.accounts.AccountManager
import android.app.Application
import android.content.Context
import foundation.e.drive.EdriveApplication
import foundation.e.drive.R
import foundation.e.drive.database.FailedSyncPrefsManager
import foundation.e.drive.models.SyncRequest
import foundation.e.drive.models.SyncWrapper
import foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_RELATIVE_QUOTA_KEY
import foundation.e.drive.utils.CommonUtils
import foundation.e.drive.work.WorkLauncher
import timber.log.Timber
import java.util.concurrent.ConcurrentHashMap
@@ -53,6 +57,8 @@ interface SyncManager {
 * @author Vincent Bourgmayer
 */
object SyncProxy: SyncRequestCollector, SyncManager {
    private const val MAX_RELATIVE_QUOTA = 99.0

    private val syncRequestQueue: ConcurrentLinkedQueue<SyncRequest> = ConcurrentLinkedQueue() //could we use channel instead ?
    private val startedRequests: ConcurrentHashMap<String, SyncWrapper> = ConcurrentHashMap()

@@ -71,6 +77,8 @@ object SyncProxy: SyncRequestCollector, SyncManager {
            return false
        }

        if (skipUploadIfQuotaReached(request, context)) return false

        if (skipBecauseOfPreviousFail(request, context)) return false

        syncRequestQueue.remove(request)
@@ -95,7 +103,8 @@ object SyncProxy: SyncRequestCollector, SyncManager {
        }

        requests.removeIf { request: SyncRequest? ->
            skipBecauseOfPreviousFail(request!!, context)
            skipUploadIfQuotaReached(request!!, context) ||
            skipBecauseOfPreviousFail(request, context)
        }

        syncRequestQueue.removeAll(requests.toSet())
@@ -207,6 +216,23 @@ object SyncProxy: SyncRequestCollector, SyncManager {
        return timeStamp < failedSyncPref.getLastFailureTimeForFile(fileStateId) + delay
    }

    private fun skipUploadIfQuotaReached(request: SyncRequest, context: Context): Boolean {
        if (request.operationType != SyncRequest.Type.UPLOAD) {
            return false
        }

        val accountManager = AccountManager.get(context)
        val account = CommonUtils.getAccount(
            context.getString(R.string.eelo_account_type),
            accountManager
        ) ?: return true

        val relativeQuota = accountManager.getUserData(account, ACCOUNT_DATA_RELATIVE_QUOTA_KEY)
            ?: "$MAX_RELATIVE_QUOTA"

        return relativeQuota.toFloat() >= MAX_RELATIVE_QUOTA
    }

    override fun pollSyncRequest(): SyncRequest? {
        return syncRequestQueue.poll()
    }