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

Commit 471f3c26 authored by Nihar Thakkar's avatar Nihar Thakkar
Browse files

Detect new email addresses even when there are accounts already signed-in

parent 2f2a864d
Loading
Loading
Loading
Loading
Loading
+74 −45
Original line number Diff line number Diff line
@@ -145,6 +145,8 @@ public class Accounts extends K9ListActivity implements OnItemClickListener
    private boolean exportGlobalSettings;
    private ArrayList<String> exportAccountUuids;

    private AccountManager accountManager;

    /**
     * Contains information about objects that need to be retained on configuration changes.
     *
@@ -460,52 +462,20 @@ public class Accounts extends K9ListActivity implements OnItemClickListener
        Intent intent = getIntent();
        //onNewIntent(intent);

        // see if we should show the welcome message
        accountManager = AccountManager.get(this);
        if (addNewAccountsAutomatically(accounts)) {
            Accounts.listAccounts(this);
            finish();
            return;
        }

        if (ACTION_IMPORT_SETTINGS.equals(intent.getAction()))
        {
            onImport();
        }
        else if (accounts.size() < 1)
        {
            AccountManager accountManager = AccountManager.get(this);

            try
            {
                android.accounts.Account[] eeloAccounts = accountManager.getAccountsByType(
                        EELO_ACCOUNT_TYPE);
                android.accounts.Account[] googleAccounts = accountManager.getAccountsByType(
                        GOOGLE_ACCOUNT_TYPE);

                if (eeloAccounts.length > 0 || googleAccounts.length > 0)
                {
                    for (android.accounts.Account eeloAccount : eeloAccounts) {
                        String emailId = accountManager.getUserData(eeloAccount,
                                ACCOUNT_EMAIL_ADDRESS_KEY);
                        String password = accountManager.getPassword(eeloAccount);
                        EeloAccountCreator.createAccount(this, emailId, password);
                    }

                    for (android.accounts.Account googleAccount : googleAccounts) {
                        String emailId = accountManager.getUserData(googleAccount,
                                ACCOUNT_EMAIL_ADDRESS_KEY);
                        GoogleAccountCreator.createAccount(this, emailId);
                    }

                    Toast.makeText(this, "Found accounts signed-in on device",
                            Toast.LENGTH_LONG).show();

                    Accounts.listAccounts(this);
                }
                else
        {
            AccountSetupActivity.actionNewAccount(this);
                }
            }
            catch (SecurityException e)
            {
                AccountSetupActivity.actionNewAccount(this);
            }

            finish();
            return;
        }
@@ -555,12 +525,71 @@ public class Accounts extends K9ListActivity implements OnItemClickListener
        {
            nonConfigurationInstance.restore(this);
        }
    }

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

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

    private boolean addNewAccountsAutomatically(List<Account> accounts) {
        try
        {
            android.accounts.Account[] eeloAccounts = getEeloAccountsOnDevice();
            android.accounts.Account[] googleAccounts = getGoogleAccountsOnDevice();
            boolean accountWasAdded = false;

            // Check if there are any new accounts to be added
            ArrayList<String> signedInEmailAddresses = new ArrayList<>();
            for (int i = 0; i < accounts.size(); i++) {
                signedInEmailAddresses.add(accounts.get(i).getEmail());
            }

            for (android.accounts.Account eeloAccount : eeloAccounts) {
                String emailId = accountManager.getUserData(eeloAccount,
                        ACCOUNT_EMAIL_ADDRESS_KEY);
                boolean accountIsSignedIn = false;
                for (String signedInEmail : signedInEmailAddresses) {
                    if (emailId.equals(signedInEmail)) {
                        accountIsSignedIn = true;
                        break;
                    }
                }
                if (!accountIsSignedIn) {
                    String password = accountManager.getPassword(eeloAccount);
                    EeloAccountCreator.createAccount(this, emailId, password);
                    accountWasAdded = true;
                }
            }

        // Remove the ChangeLog
        /*ChangeLog cl = new ChangeLog(this);
        if (cl.isFirstRun()) {
            cl.getLogDialog().show();
        }*/
            for (android.accounts.Account googleAccount : googleAccounts) {
                String emailId = accountManager.getUserData(googleAccount,
                        ACCOUNT_EMAIL_ADDRESS_KEY);
                boolean accountIsSignedIn = false;
                for (String signedInEmail : signedInEmailAddresses) {
                    if (emailId.equals(signedInEmail)) {
                        accountIsSignedIn = true;
                        break;
                    }
                }
                if (!accountIsSignedIn) {
                    GoogleAccountCreator.createAccount(this, emailId);
                    accountWasAdded = true;
                }
            }

            return accountWasAdded;
        }
        catch (SecurityException e)
        {
            e.printStackTrace();
            return false;
        }
    }

    private void initializeActionBar()