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

Commit 7d8959c6 authored by Nihar Thakkar's avatar Nihar Thakkar
Browse files

Sync account addition/removal with Account Manager

parent 471f3c26
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -67,7 +67,7 @@
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.K9.Startup"
        android:theme="@android:style/Theme.Material.Light.NoActionBar"
        android:resizeableActivity="true"
        android:allowBackup="false">

+16 −0
Original line number Diff line number Diff line
@@ -277,6 +277,8 @@ public class Account implements BaseAccount, AccountConfig {
        TEXT, HTML, AUTO
    }

    private boolean isDeviceAccount;

    protected Account(Context context) {
        accountUuid = UUID.randomUUID().toString();
        localStorageProviderId = StorageManager.getInstance(context).getDefaultProviderId();
@@ -323,6 +325,7 @@ public class Account implements BaseAccount, AccountConfig {
        isEnabled = true;
        markMessageAsReadOnView = true;
        alwaysShowCcBcc = false;
        isDeviceAccount = false;

        searchableFolders = Searchable.ALL;

@@ -499,6 +502,8 @@ public class Account implements BaseAccount, AccountConfig {
        markMessageAsReadOnView = storage.getBoolean(accountUuid + ".markMessageAsReadOnView", true);
        alwaysShowCcBcc = storage.getBoolean(accountUuid + ".alwaysShowCcBcc", false);

        isDeviceAccount = storage.getBoolean(accountUuid + ".isDeviceAccount", false);

        cacheChips();

        // Use email address as account description if necessary
@@ -598,6 +603,7 @@ public class Account implements BaseAccount, AccountConfig {
        editor.remove(accountUuid + ".messageFormat");
        editor.remove(accountUuid + ".messageReadReceipt");
        editor.remove(accountUuid + ".notifyMailCheck");
        editor.remove(accountUuid + ".isDeviceAccount");
        for (NetworkType type : NetworkType.values()) {
            editor.remove(accountUuid + ".useCompression." + type.name());
        }
@@ -775,6 +781,8 @@ public class Account implements BaseAccount, AccountConfig {
        editor.putBoolean(accountUuid + ".led", notificationSetting.isLedEnabled());
        editor.putInt(accountUuid + ".ledColor", notificationSetting.getLedColor());

        editor.putBoolean(accountUuid + ".isDeviceAccount", isDeviceAccount);

        for (NetworkType type : NetworkType.values()) {
            Boolean useCompression = compressionMap.get(type);
            if (useCompression != null) {
@@ -2023,4 +2031,12 @@ public class Account implements BaseAccount, AccountConfig {
        }
        return name;
    }

    public boolean isDeviceAccount() {
        return this.isDeviceAccount;
    }

    public void setDeviceAccount(boolean deviceAccount) {
        this.isDeviceAccount = deviceAccount;
    }
}
+66 −11
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.text.Editable;
import android.text.TextWatcher;

@@ -463,6 +464,10 @@ public class Accounts extends K9ListActivity implements OnItemClickListener
        //onNewIntent(intent);

        accountManager = AccountManager.get(this);

        removeOldAccountsAutomatically(accounts);
        accounts = Preferences.getPreferences(this).getAccounts();

        if (addNewAccountsAutomatically(accounts)) {
            Accounts.listAccounts(this);
            finish();
@@ -544,18 +549,12 @@ public class Accounts extends K9ListActivity implements OnItemClickListener
            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)) {
                for (Account account : accounts) {
                    if (emailId.equals(account.getEmail())) {
                        accountIsSignedIn = true;
                        break;
                    }
@@ -571,8 +570,8 @@ public class Accounts extends K9ListActivity implements OnItemClickListener
                String emailId = accountManager.getUserData(googleAccount,
                        ACCOUNT_EMAIL_ADDRESS_KEY);
                boolean accountIsSignedIn = false;
                for (String signedInEmail : signedInEmailAddresses) {
                    if (emailId.equals(signedInEmail)) {
                for (Account account : accounts) {
                    if (emailId.equals(account.getEmail())) {
                        accountIsSignedIn = true;
                        break;
                    }
@@ -592,6 +591,57 @@ public class Accounts extends K9ListActivity implements OnItemClickListener
        }
    }

    private void removeOldAccountsAutomatically(List<Account> accounts){
        try {
            android.accounts.Account[] eeloAccounts = getEeloAccountsOnDevice();
            android.accounts.Account[] googleAccounts = getGoogleAccountsOnDevice();

            for (Account account : accounts) {
                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) {
                        selectedContextAccount = account;

                        Account realAccount = (Account) selectedContextAccount;
                        try
                        {
                            realAccount.getLocalStore().delete();
                        }
                        catch (Exception e)
                        {
                            // Ignore, this may lead to localStores on sd-cards that
                            // are currently not inserted to be left
                        }
                        MessagingController.getInstance(getApplication())
                                .deleteAccount(realAccount);
                        Preferences.getPreferences(Accounts.this)
                                .deleteAccount(realAccount);
                    }
                }
            }
        }
        catch (SecurityException e) {
            e.printStackTrace();
        }
    }

    private void initializeActionBar()
    {
        actionBar.setDisplayShowCustomEnabled(true);
@@ -1298,8 +1348,13 @@ public class Accounts extends K9ListActivity implements OnItemClickListener
    private void onDeleteAccount(Account account)
    {
        selectedContextAccount = account;
        if (account.isDeviceAccount()) {
            startActivity(new Intent(Settings.ACTION_SYNC_SETTINGS));
        }
        else {
            showDialog(DIALOG_REMOVE_ACCOUNT);
        }
    }

    private void onEditAccount(Account account)
    {
+1 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ public class EeloAccountCreator {
                ":" + password + "@mail.eelo.io");

        Account account = preferences.newAccount();
        account.setDeviceAccount(true);
        account.loadConfig(accountConfig);

        MessagingController.getInstance(context).listFoldersSynchronous(account, true, null);
+1 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ public class GoogleAccountCreator {
                "::XOAUTH2@smtp.gmail.com");

        Account account = preferences.newAccount();
        account.setDeviceAccount(true);
        account.loadConfig(accountConfig);

        MessagingController.getInstance(context).listFoldersSynchronous(account, true, null);
Loading