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

Commit 5d854181 authored by Nihar Thakkar's avatar Nihar Thakkar Committed by Sumit Pundir
Browse files

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

parent a0c68d98
Loading
Loading
Loading
Loading
+75 −46
Original line number Diff line number Diff line
@@ -399,49 +399,17 @@ 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;
        }
@@ -486,11 +454,72 @@ public class Accounts extends K9ListActivity implements OnItemClickListener {
            nonConfigurationInstance.restore(this);
        }

        // Remove the ChangeLog
        /*ChangeLog cl = new ChangeLog(this);
        if (cl.isFirstRun()) {
            cl.getLogDialog().show();
        }*/
    }

    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;
                }
            }

            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() {