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

Commit 2ae5a7ac authored by Nihar Thakkar's avatar Nihar Thakkar Committed by Sumit Pundir
Browse files

Check if account is still signed-in and enabled on the device during background syncs

parent f8434f36
Loading
Loading
Loading
Loading
+104 −1
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import android.accounts.AccountManager;
import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
@@ -116,6 +117,11 @@ import timber.log.Timber;
import static io.eelo.mail.K9.MAX_SEND_ATTEMPTS;
import static io.eelo.mail.mail.Flag.X_REMOTE_COPY_STARTED;

import static io.eelo.mail.account.OAuthConstants.ACCOUNT_EMAIL_ADDRESS_KEY;
import static io.eelo.mail.account.OAuthConstants.EELO_ACCOUNT_TYPE;
import static io.eelo.mail.account.OAuthConstants.GOOGLE_ACCOUNT_TYPE;
import static io.eelo.mail.account.OAuthConstants.MAIL_CONTENT_AUTHORITY;


/**
 * Starts a long running (application) Thread that will run through commands
@@ -3443,6 +3449,95 @@ public class MessagingController {
        context.startActivity(Intent.createChooser(msg, context.getString(R.string.send_alternate_chooser_title)));
    }

    private android.accounts.Account[] getEeloAccountsOnDevice(AccountManager accountManager) {
        return accountManager.getAccountsByType(
                EELO_ACCOUNT_TYPE);
    }

    private android.accounts.Account[] getGoogleAccountsOnDevice(AccountManager accountManager) {
        return accountManager.getAccountsByType(
                GOOGLE_ACCOUNT_TYPE);
    }

    private boolean isAccountSignedInOnDevice(AccountManager accountManager, Account account){
        try {
            android.accounts.Account[] eeloAccounts = getEeloAccountsOnDevice(accountManager);
            android.accounts.Account[] googleAccounts = getGoogleAccountsOnDevice(accountManager);

            if (account.isDeviceAccount()) {
                boolean accountIsSignedInOnDevice = false;
                for (android.accounts.Account eeloAccount : eeloAccounts) {
                    String emailId = accountManager.getUserData(eeloAccount,
                            ACCOUNT_EMAIL_ADDRESS_KEY);
                    if (account.getEmail().equals(emailId)) {
                        accountIsSignedInOnDevice = true;
                        break;
                    }
                }
                for (android.accounts.Account googleAccount : googleAccounts) {
                    String emailId = accountManager.getUserData(googleAccount,
                            ACCOUNT_EMAIL_ADDRESS_KEY);
                    if (account.getEmail().equals(emailId)) {
                        accountIsSignedInOnDevice = true;
                        break;
                    }
                }

                if (accountIsSignedInOnDevice) {
                    return true;
                }
            }
        }
        catch (SecurityException e) {
            e.printStackTrace();
        }
        return false;
    }

    private boolean isSyncEnabled(AccountManager accountManager, Account account) {
        try {
            android.accounts.Account[] eeloAccounts = getEeloAccountsOnDevice(accountManager);
            android.accounts.Account[] googleAccounts = getGoogleAccountsOnDevice(accountManager);

            if (account.isDeviceAccount()) {
                for (android.accounts.Account eeloAccount : eeloAccounts) {
                    String emailId = accountManager.getUserData(eeloAccount,
                            ACCOUNT_EMAIL_ADDRESS_KEY);
                    if (account.getEmail().equals(emailId)) {
                        if (ContentResolver.getSyncAutomatically(eeloAccount,
                                MAIL_CONTENT_AUTHORITY)) {
                            account.setAutomaticCheckIntervalMinutes(5);
                            return true;
                        }
                        else {
                            account.setAutomaticCheckIntervalMinutes(-1);
                            return false;
                        }
                    }
                }
                for (android.accounts.Account googleAccount : googleAccounts) {
                    String emailId = accountManager.getUserData(googleAccount,
                            ACCOUNT_EMAIL_ADDRESS_KEY);
                    if (account.getEmail().equals(emailId)) {
                        if (ContentResolver.getSyncAutomatically(googleAccount,
                                MAIL_CONTENT_AUTHORITY)) {
                            account.setAutomaticCheckIntervalMinutes(5);
                            return true;
                        }
                        else {
                            account.setAutomaticCheckIntervalMinutes(-1);
                            return false;
                        }
                    }
                }
            }
        }
        catch (SecurityException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * Checks mail for one or multiple accounts. If account is null all accounts
     * are checked.
@@ -3483,8 +3578,16 @@ public class MessagingController {
                    }

                    for (final Account account : accounts) {
		        if (account.isDeviceAccount()) {
                            if (isAccountSignedInOnDevice(AccountManager.get(context), account) &&
                                    isSyncEnabled(AccountManager.get(context), account)) {
                                checkMailForAccount(context, account, ignoreLastCheckedTime, listener);
                            }
                        }
                        else {
                            checkMailForAccount(context, account, ignoreLastCheckedTime, listener);
                        }
                    }

                } catch (Exception e) {
                    Timber.e(e, "Unable to synchronize mail");