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

Commit 832ac044 authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊
Browse files

mail: Handle background mail sync

parent 0e2a4daa
Loading
Loading
Loading
Loading
+63 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 MURENA SAS
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 *
 */
package app.k9mail.feature.account.accountmanager

import android.accounts.Account
import android.accounts.AccountManager
import android.content.ContentResolver
import android.content.Context
import com.fsck.k9.logging.Timber
import com.fsck.k9.mail.AuthType
@@ -148,4 +166,49 @@ object AccountManagerHelper {

        return null
    }

    /**
     * @return syncEnable or not. If account not found in accountManager accounts,
     * means account is setup by user... so return true by default
     */
    fun isSyncEnable(context: Context, email: String): Boolean {
        val accountManager = AccountManager.get(context)

        var syncStatus = true

        AccountManagerConstants.ACCOUNT_TYPES.forEach {
            val accounts = accountManager.getAccountsByType(it)
            val syncEnable = isSyncEnable(accountManager, accounts, email)
            if (syncEnable.isAccountFound()) {
                syncStatus = syncEnable.getStatus()
                return@forEach
            }
        }

        return syncStatus
    }

    private fun isSyncEnable(accountManager: AccountManager, accounts: Array<out Account>, email: String): Syncable {
        accounts.forEach {
            try {
                val accountEmail: String =
                    accountManager.getUserData(it, AccountManagerConstants.ACCOUNT_EMAIL_ADDRESS_KEY)
                if (accountEmail == email) {
                    // if master sync disable, then account sync is disable
                    return if (!ContentResolver.getMasterSyncAutomatically()) {
                        Syncable.NOT_SYNCABLE
                    } else Syncable.getSyncable(
                        ContentResolver.getSyncAutomatically(
                            it,
                            AccountManagerConstants.MAIL_CONTENT_AUTHORITY,
                        ),
                    )
                }
            } catch (e: Throwable) {
                timber.log.Timber.e(e)
            }
        }

        return Syncable.ACCOUNT_NOT_FOUND
    }
}
+32 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 MURENA SAS
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 *
 */
package app.k9mail.feature.account.accountmanager

enum class Syncable {
    SYNCABLE,
    NOT_SYNCABLE,
    ACCOUNT_NOT_FOUND;

    companion object {
        fun getSyncable(status: Boolean) = if (status) SYNCABLE else NOT_SYNCABLE
    }

    fun getStatus() = this == SYNCABLE

    fun isAccountFound() = this != ACCOUNT_NOT_FOUND
}
+1 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ internal val controllerPushModule = module {
            connectivityManager = get(),
            accountPushControllerFactory = get(),
            folderRepository = get(),
            context = get(),
        )
    }

+7 −1
Original line number Diff line number Diff line
package com.fsck.k9.controller.push

import android.content.Context
import app.k9mail.feature.account.accountmanager.AccountManagerHelper
import app.k9mail.legacy.account.Account
import app.k9mail.legacy.account.AccountManager
import app.k9mail.legacy.mailstore.FolderRepository
@@ -46,6 +48,7 @@ class PushController internal constructor(
    private val folderRepository: FolderRepository,
    private val coroutineScope: CoroutineScope = GlobalScope,
    private val coroutineDispatcher: CoroutineDispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher(),
    private val context: Context,
) {
    private val lock = Any()
    private var initializationStarted = false
@@ -249,7 +252,10 @@ class PushController internal constructor(
    private fun getPushAccounts(): Set<Account> {
        return getPushCapableAccounts()
            .asSequence()
            .filter { account -> folderRepository.hasPushEnabledFolder(account) }
            .filter { account ->
                folderRepository.hasPushEnabledFolder(account) &&
                    AccountManagerHelper.isSyncEnable(context, account.email)
            }
            .toSet()
    }

+5 −5
Original line number Diff line number Diff line
@@ -26,17 +26,17 @@ class MailSyncWorker(

        Timber.d("Executing periodic mail sync for account %s", accountUuid)

        if (isBackgroundSyncDisabled()) {
            Timber.d("Background sync is disabled. Skipping mail sync.")
            return Result.success()
        }

        val account = preferences.getAccount(accountUuid)
        if (account == null) {
            Timber.e("Account %s not found. Can't perform mail sync.", accountUuid)
            return Result.failure()
        }

        if (isBackgroundSyncDisabled() || !AccountManagerHelper.isSyncEnable(context, account.email)) {
            Timber.d("Background sync is disabled. Skipping mail sync.")
            return Result.success()
        }

        if (account.isPeriodicMailSyncDisabled) {
            Timber.d("Periodic mail sync has been disabled for this account. Skipping mail sync.")
            return Result.success()