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

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

Sync account addition/removal with Account Manager

parent 5d854181
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -66,7 +66,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">

+14 −0
Original line number Diff line number Diff line
@@ -286,6 +286,8 @@ public class Account implements BaseAccount, StoreConfig {
        TEXT, HTML, AUTO
    }

    private boolean isDeviceAccount;

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

        searchableFolders = Searchable.ALL;

@@ -508,6 +511,8 @@ public class Account implements BaseAccount, StoreConfig {
        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
@@ -607,6 +612,7 @@ public class Account implements BaseAccount, StoreConfig {
        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());
        }
@@ -2040,4 +2046,12 @@ public class Account implements BaseAccount, StoreConfig {
        }
        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;
import io.eelo.mail.activity.setup.AccountSetupActivity;
@@ -400,6 +401,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();
@@ -473,18 +478,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 : account) {
                    if (emailId.equals(account.getEmail())) {
                        accountIsSignedIn = true;
                        break;
                    }
@@ -500,8 +499,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 : account) {
                    if (emailId.equals(account.getEmail())) {
                        accountIsSignedIn = true;
                        break;
                    }
@@ -522,6 +521,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);
        actionBar.setCustomView(R.layout.actionbar_custom);
@@ -1117,8 +1167,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) {
        AccountSettings.actionSettings(this, 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