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

Commit 827a3e59 authored by dev-12's avatar dev-12 Committed by pratyush
Browse files

refactor: standardize actions string and permission across events

- send account removed and mail account added event to other eos apps
- remove special action for the mail app
parent 33c0e58d
Loading
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -29,10 +29,6 @@
    <uses-permission android:name="android.permission.READ_CALENDAR"/>
    <uses-permission android:name="android.permission.WRITE_CALENDAR"/>

    <!-- permission to use to be notified about account being added -->
    <permission android:name="foundation.e.accountmanager.permission.ADD_ACCOUNT"
        android:protectionLevel="signature" />

    <!-- permission for broadcast related to account changes event (added/removed) -->
    <permission
        android:name="foundation.e.accountmanager.permission.ACCOUNT_EVENTS"
+20 −19
Original line number Diff line number Diff line
@@ -17,34 +17,35 @@

package at.bitfire.davdroid

import android.content.ComponentName
import android.accounts.AccountManager
import android.content.Context
import android.content.Intent

object AccountSyncHelper {

    private const val MAIL_PACKAGE = "foundation.e.mail"
    private const val MAIL_RECEIVER_CLASS = "com.fsck.k9.account.AccountSyncReceiver"
    private const val ACTION_PREFIX = "foundation.e.accountmanager.account."
    private const val ACCOUNT_EVENTS_PERMISSION = "foundation.e.accountmanager.permission.ACCOUNT_EVENTS"
    private const val ACTION_PREFIX = "foundation.e.accountmanager.action"
    const val ACCOUNT_EVENTS_PERMISSION = "foundation.e.accountmanager.permission.ACCOUNT_EVENTS"
    const val ACTION_ACCOUNT_REMOVED = "$ACTION_PREFIX.ACCOUNT_REMOVED"
    const val ACTION_ACCOUNT_ADDED = "$ACTION_PREFIX.ACCOUNT_ADDED"

    fun syncMailAccounts(applicationContext: Context?) {
        val intent = getIntent()
        intent.action = ACTION_PREFIX + "create"
        applicationContext?.sendBroadcast(intent, ACCOUNT_EVENTS_PERMISSION)
    fun notifyAccountAdded(context: Context, accountName: String) {
        val intent = Intent(ACTION_ACCOUNT_ADDED).apply {
            addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
            val accountType = context.getString(R.string.eelo_account_type)
            putExtra(AccountManager.KEY_ACCOUNT_NAME, accountName)
            putExtra(AccountManager.KEY_ACCOUNT_TYPE, accountType)
        }

    fun notifyAccountRemoved(context: Context, accountRemovedIntent: Intent) {
        val intent = getIntent()
        intent.action = ACTION_PREFIX + "removed"
        intent.putExtras(accountRemovedIntent)
        context.sendBroadcast(intent, ACCOUNT_EVENTS_PERMISSION)
    }

    private fun getIntent() : Intent {
        val intent = Intent()
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
        intent.component = ComponentName(MAIL_PACKAGE, MAIL_RECEIVER_CLASS)
        return intent
    fun notifyAccountRemoved(context: Context, accountRemovedIntent: Intent) {
        context.sendBroadcast(
            Intent(ACTION_ACCOUNT_REMOVED).apply {
                addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
                putExtras(accountRemovedIntent)
            },
            ACCOUNT_EVENTS_PERMISSION
        )
    }

}
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -32,6 +32,6 @@ class MailSyncer (context: Context): Syncer(context) {
        provider: ContentProviderClient,
        syncResult: SyncResult
    ) {
        AccountSyncHelper.syncMailAccounts(context.applicationContext)
        AccountSyncHelper.notifyAccountAdded(context.applicationContext, account.name)
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -574,7 +574,7 @@ class SettingsActivity: AppCompatActivity() {
            accountSettings?.credentials(credentials)
            clearOwnCloudData()
            reload()
            AccountSyncHelper.syncMailAccounts(getApplication())
            AccountSyncHelper.notifyAccountAdded(getApplication(), account.name)
        }

        private fun clearOwnCloudData() {
+6 −11
Original line number Diff line number Diff line
@@ -202,7 +202,7 @@ class AccountDetailsFragment : Fragment() {
                                MurenaSsoMigrationPreferences.isSsoMigrationRunning(requireContext())

                            if (isSsoMigrationRunning) {
                                handlePostMurenaSsoMigrationOperations()
                                handlePostMurenaSsoMigrationOperations(name)
                            } else {
                                notifyListeners(name)
                            }
@@ -242,7 +242,7 @@ class AccountDetailsFragment : Fragment() {
        return account.name.substringBefore("@") == accountName.substringBefore("@")
    }

    private fun handlePostMurenaSsoMigrationOperations() {
    private fun handlePostMurenaSsoMigrationOperations(name: String) {
        val authState = requireActivity().intent.getStringExtra(LoginActivity.AUTH_STATE)
        val isMigratedToMurenaSso = !authState.isNullOrBlank()

@@ -251,12 +251,12 @@ class AccountDetailsFragment : Fragment() {
            MurenaSsoMigrationPreferences.updateSsoMigrationStatus(requireContext(), Completed)
            Logger.log.info("Murena SSO migration is complete.")

            syncMailToUseOAuth()
            syncMailToUseOAuth(name)
        }
    }

    private fun syncMailToUseOAuth() {
        AccountSyncHelper.syncMailAccounts(requireContext())
    private fun syncMailToUseOAuth(name: String) {
        AccountSyncHelper.notifyAccountAdded(requireContext(), name)
    }

    private fun stopMurenaSsoMigrationService() {
@@ -265,12 +265,7 @@ class AccountDetailsFragment : Fragment() {
    }

    private fun notifyListeners(name: String) {
        val intent = Intent("foundation.e.accountmanager.action.ACCOUNT_ADDED")
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)

        intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, name)
        intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, getString(R.string.eelo_account_type))
        requireActivity().sendBroadcast(intent, "foundation.e.accountmanager.permission.ADD_ACCOUNT")
        AccountSyncHelper.notifyAccountAdded(requireContext(), name)
    }