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

Commit d9264a68 authored by dev-12's avatar dev-12 Committed by Mohammed Althaf T
Browse files

refactor: standardize actions string and permission across events

- Ensure proper mail account removal
- we send our own custom broadcast which is guarded with permission and
  only sent to mail app, system broadcast is not reliable for app that is
  not the owner for the account (i.e mail app for sso account which is
  owned by account manager).
- send account removed and mail account added event to other eos apps
- remove special action for the mail app
parent 997dd8c4
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -175,7 +175,7 @@ abstract class BaseSyncWorker(
                }
            }
            SyncDataType.EMAIL -> {
                AccountHelper.notifyMailAccountAdded(applicationContext)
                AccountHelper.notifyAccountAdded(applicationContext, account.name)
                return Result.success()
            }
            SyncDataType.MEDIA,
+1 −1
Original line number Diff line number Diff line
@@ -235,7 +235,7 @@ class AccountSettingsModel @AssistedInject constructor(

    fun updateCredentials(credentials: Credentials) = CoroutineScope(defaultDispatcher).launch {
        accountSettings.credentials(credentials)
        AccountHelper.notifyEApps(context, account.name)
        AccountHelper.notifyAccountAdded(context, account.name)
        reload()
    }

+2 −1
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ class AccountReceiver : BroadcastReceiver() {
            logger.log(Level.INFO, "Account change detected for ${account.name}")
            val authState = accountManager.getUserData(account, AccountSettings.KEY_AUTH_STATE)
            AuthStatePrefUtils.saveAuthState(context, account, authState)
            AccountHelper.notifyEApps(context, account.name)
            AccountHelper.notifyAccountAdded(context, account.name)
        }
    }

@@ -68,6 +68,7 @@ class AccountReceiver : BroadcastReceiver() {
            return
        }

        AccountHelper.notifyAccountRemoved(context, intent)
        clearOidcSession(context, accountName, accountType)

        val accountManager = AccountManager.get(context)
+17 −24
Original line number Diff line number Diff line
@@ -21,20 +21,19 @@ import android.accounts.Account
import android.accounts.AccountManager
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import at.bitfire.davdroid.BuildConfig
import at.bitfire.davdroid.settings.AccountSettings
import foundation.e.accountmanager.AccountTypes
import foundation.e.accountmanager.sync.SyncBroadcastReceiver
import java.util.concurrent.TimeUnit

object AccountHelper {
    private const val MAIL_PACKAGE = "foundation.e.mail"
    private const val MAIL_RECEIVER_CLASS = "com.fsck.k9.account.AccountSyncReceiver"
    private const val MAIL_ACTION_PREFIX = "foundation.e.accountmanager.account."
    private const val ACTION_ADD_ACCOUNT = "foundation.e.accountmanager.action.ACCOUNT_ADDED"
    private const val PERMISSION_ADD_ACCOUNT = "foundation.e.accountmanager.permission.ADD_ACCOUNT"
    private const val ACTION_PREFIX = "${BuildConfig.APPLICATION_ID}.action"
    const val ACCOUNT_EVENTS_PERMISSION = "${BuildConfig.APPLICATION_ID}.permission.ACCOUNT_EVENTS"
    const val ACTION_ACCOUNT_REMOVED = "$ACTION_PREFIX.ACCOUNT_REMOVED"
    const val ACTION_ACCOUNT_ADDED = "$ACTION_PREFIX.ACCOUNT_ADDED"

    fun getAllAccounts(accountManager: AccountManager): Array<Account> {
        val allAccounts = mutableListOf<Account>()
@@ -74,12 +73,19 @@ object AccountHelper {
        return null
    }

    fun notifyMailAccountAdded(context: Context) {
        val intent = Intent()
    fun notifyAccountAdded(context: Context, accountName: String) {
        val intent = Intent(ACTION_ACCOUNT_ADDED)
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
        intent.component = ComponentName(MAIL_PACKAGE, MAIL_RECEIVER_CLASS)
        intent.action = MAIL_ACTION_PREFIX + "create"
        context.sendBroadcast(intent)
        intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, accountName)
        intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, AccountTypes.Murena.accountType)
        context.sendBroadcast(intent, ACCOUNT_EVENTS_PERMISSION)
    }

    fun notifyAccountRemoved(context: Context, intent: Intent) {
        val intent = Intent(ACTION_ACCOUNT_REMOVED)
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
        intent.putExtras(intent)
        context.sendBroadcast(intent, ACCOUNT_EVENTS_PERMISSION)
    }

    fun scheduleSyncWithDelay(context: Context) {
@@ -98,17 +104,4 @@ object AccountHelper {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent)
        }
    }

    private fun notifyListeners(context: Context, name: String) {
        val intent = Intent(ACTION_ADD_ACCOUNT)
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
        intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, name)
        intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, AccountTypes.Murena.accountType)
        context.sendBroadcast(intent, PERMISSION_ADD_ACCOUNT)
    }

    fun notifyEApps(context: Context, name: String) {
        notifyListeners(context, name)
        notifyMailAccountAdded(context)
    }
}
+5 −3
Original line number Diff line number Diff line
@@ -17,9 +17,11 @@
    <uses-permission android:name="android.permission.START_ACTIVITIES_FROM_BACKGROUND"
            tools:ignore="ProtectedPermissions" />

    <!-- permission to use to be notified about account being added -->
    <permission android:name="foundation.e.accountmanager.permission.ADD_ACCOUNT"
    <!-- permission for broadcast related to account changes event (added/removed) -->
    <permission
            android:name="${applicationId}.permission.ACCOUNT_EVENTS"
            android:protectionLevel="signature"/>
    <uses-permission android:name="${applicationId}.permission.ACCOUNT_EVENTS"/>

    <application>