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

Commit 3c4ecfd1 authored by Fahim Salam Chowdhury's avatar Fahim Salam Chowdhury 👽
Browse files

Centralize supported accountType destribution

Currently app support multiple accountTypes (webdav, murena & google).
But there is no center util method which could provide all supported
accountTypes & all supported active accounts. So to add new account, we
need to change multiple files. This commit optimize this by introducing
these util methods in AccountsUtils object class.
parent 808c3032
Loading
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase
import at.bitfire.davdroid.R
import at.bitfire.davdroid.TextTable
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.syncadapter.AccountUtils
import at.bitfire.davdroid.ui.AccountsActivity
import at.bitfire.davdroid.ui.NotificationUtils
import dagger.Module
@@ -67,7 +68,7 @@ abstract class AppDatabase: RoomDatabase() {

                        // remove all accounts because they're unfortunately useless without database
                        val am = AccountManager.get(context)
                        for (account in am.getAccountsByType(context.getString(R.string.account_type)))
                        for (account in AccountUtils.getMainAccounts(context))
                            am.removeAccount(account, null, null)
                    }
                })
+2 −8
Original line number Diff line number Diff line
@@ -86,11 +86,7 @@ open class LocalAddressBook(
     * @return list of [mainAccount]'s address books
     */
	fun findAll(context: Context, provider: ContentProviderClient?, mainAccount: Account?): List<LocalAddressBook> {
            val accountManager = AccountManager.get(context)
            val accounts = ArrayList<Account>()
            accountManager.getAccountsByType(context.getString(R.string.account_type_eelo_address_book)).forEach { accounts.add(it) }
            accountManager.getAccountsByType(context.getString(R.string.account_type_google_address_book)).forEach { accounts.add(it) }
            accountManager.getAccountsByType(context.getString(R.string.account_type_address_book)).forEach { accounts.add(it) }
            val accounts = AccountUtils.getAddressBookAccounts(context)

            return accounts.toTypedArray().map { LocalAddressBook(context, it, provider) }
                .filter { mainAccount == null || it.mainAccount == mainAccount }
@@ -128,9 +124,7 @@ open class LocalAddressBook(
         * @throws IllegalArgumentException if the given account is not a address book account or does not have a main account
         */
        fun mainAccount(context: Context, account: Account): Account =
            if (account.type == context.getString(R.string.account_type_address_book) ||
                account.type == context.getString(R.string.account_type_eelo_address_book) ||
                account.type == context.getString(R.string.account_type_google_address_book)) {
            if (account.type in AccountUtils.getAddressBookAccountTypes(context)) {

                val manager = AccountManager.get(context)
                val accountName = manager.getUserData(account, USER_DATA_MAIN_ACCOUNT_NAME)
+13 −9
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.resource.LocalAddressBook
import at.bitfire.davdroid.resource.LocalTask
import at.bitfire.davdroid.resource.TaskUtils
import at.bitfire.davdroid.syncadapter.AccountUtils
import at.bitfire.davdroid.syncadapter.SyncUtils
import at.bitfire.ical4android.AndroidCalendar
import at.bitfire.ical4android.AndroidEvent
@@ -173,8 +174,7 @@ class AccountSettings(
            val addressBooksAuthority = context.getString(R.string.address_books_authority)
            val taskAuthority = TaskUtils.currentProvider(context)?.authority

            val am = AccountManager.get(context)
            for (account in am.getAccountsByType(context.getString(R.string.account_type)))
            for (account in AccountUtils.getMainAccounts(context))
                try {
                    val settings = AccountSettings(context, account)

@@ -220,17 +220,21 @@ class AccountSettings(
    val account: Account

    init {
        when (argAccount.type) {
            context.getString(R.string.account_type_address_book), context.getString(R.string.account_type_eelo_address_book), context.getString(R.string.account_type_google_address_book) -> {
        account = when (argAccount.type) {
            in AccountUtils.getAddressBookAccountTypes(context) -> {
                /* argAccount is an address book account, which is not a main account. However settings are
                       stored in the main account, so resolve and use the main account instead. */
                account = LocalAddressBook.mainAccount(context, argAccount)
                LocalAddressBook.mainAccount(context, argAccount)
            }
            context.getString(R.string.account_type), context.getString(R.string.google_account_type), context.getString(R.string.eelo_account_type) ->
                account = argAccount
            else ->

            in AccountUtils.getMainAccountTypes(context) -> {
                argAccount
            }

            else -> {
                throw IllegalArgumentException("Account type not supported. AccountType: ${argAccount.type}")
            }
        }

        // synchronize because account migration must only be run one time
        synchronized(AccountSettings::class.java) {
+52 −1
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ import android.accounts.Account
import android.accounts.AccountManager
import android.content.Context
import android.os.Bundle
import at.bitfire.davdroid.R
import at.bitfire.davdroid.log.Logger

object AccountUtils {
@@ -24,7 +25,12 @@ object AccountUtils {
     * @throws IllegalArgumentException when user data contains non-String values
     * @throws IllegalStateException if user data can't be set
     */
    fun createAccount(context: Context, account: Account, userData: Bundle, password: String? = null): Boolean {
    fun createAccount(
        context: Context,
        account: Account,
        userData: Bundle,
        password: String? = null
    ): Boolean {
        // validate user data
        for (key in userData.keySet()) {
            userData.get(key)?.let { entry ->
@@ -63,5 +69,50 @@ object AccountUtils {
        return true
    }

    fun getMainAccountTypes(context: Context) =
        listOf(
            context.getString(R.string.account_type),
            context.getString(R.string.eelo_account_type),
            context.getString(R.string.google_account_type)
        )

    fun getMainAccounts(context: Context): List<Account> {
        val accountManager = AccountManager.get(context)
        val accounts = mutableListOf<Account>()

        getMainAccountTypes(context)
            .forEach {
                accounts.addAll(accountManager.getAccountsByType(it))
            }

        return accounts
    }

    fun getAddressBookAccountTypes(context: Context) =
        listOf(
            context.getString(R.string.account_type_address_book),
            context.getString(R.string.account_type_eelo_address_book),
            context.getString(R.string.account_type_google_address_book)
        )

    fun getAddressBookAccounts(context: Context): List<Account> {
        val accountManager = AccountManager.get(context)
        val accounts = mutableListOf<Account>()

        getAddressBookAccountTypes(context)
            .forEach {
                accounts.addAll(accountManager.getAccountsByType(it))
            }

        return accounts
    }

    fun getAddressBookType(context: Context, mainType: String): String? {
        return when(mainType) {
            context.getString(R.string.account_type) -> context.getString(R.string.account_type_address_book)
            context.getString(R.string.eelo_account_type) -> context.getString(R.string.account_type_eelo_address_book)
            context.getString(R.string.google_account_type) -> context.getString(R.string.account_type_google_address_book)
            else -> null
        }
    }
}
 No newline at end of file
+2 −11
Original line number Diff line number Diff line
@@ -9,7 +9,6 @@ import android.accounts.AccountManager
import android.accounts.OnAccountsUpdateListener
import android.content.Context
import androidx.annotation.AnyThread
import at.bitfire.davdroid.R
import at.bitfire.davdroid.db.AppDatabase
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.resource.LocalAddressBook
@@ -86,23 +85,15 @@ class AccountsUpdatedListener private constructor(
    private fun cleanupAccounts(context: Context, accounts: Array<out Account>) {
        Logger.log.log(Level.INFO, "Cleaning up accounts. Current accounts")

        val accountManager = AccountManager.get(context)
        val accountNames = HashSet<String>()
        val accountFromManager = ArrayList<Account>()

        accountManager.getAccountsByType(context.getString(R.string.eelo_account_type)).forEach { accountFromManager.add(it) }
        accountManager.getAccountsByType(context.getString(R.string.google_account_type)).forEach { accountFromManager.add(it) }
        accountManager.getAccountsByType(context.getString(R.string.account_type)).forEach { accountFromManager.add(it) }
        val accountFromManager = AccountUtils.getMainAccounts(context)

        for (account in accountFromManager.toTypedArray()) {
            accountNames += account.name
        }

        // delete orphaned address book accounts
        val addressBookAccounts = ArrayList<Account>()
        accountManager.getAccountsByType(context.getString(R.string.account_type_eelo_address_book)).forEach { addressBookAccounts.add(it) }
        accountManager.getAccountsByType(context.getString(R.string.account_type_google_address_book)).forEach { addressBookAccounts.add(it) }
        accountManager.getAccountsByType(context.getString(R.string.account_type_address_book)).forEach { addressBookAccounts.add(it) }
        val addressBookAccounts = AccountUtils.getAddressBookAccounts(context)
        addressBookAccounts.map { LocalAddressBook(context, it, null) }
            .forEach {
                try {
Loading