From 797b18a434b7de411000ae8d1be3744ff32d56e5 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Wed, 6 Jul 2022 19:13:38 +0600 Subject: [PATCH 1/2] 5560-r-sync_account_with_mail_app issue: https://gitlab.e.foundation/e/backlog/-/issues/5560 call Mail app's accountSyncBroadcast receiver when account is removed from the accountManager. --- .../accounts/MailAccountSyncHelper.java | 61 +++++++++++++++++++ .../RemoveAccountPreferenceController.java | 6 +- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/com/android/settings/accounts/MailAccountSyncHelper.java diff --git a/src/com/android/settings/accounts/MailAccountSyncHelper.java b/src/com/android/settings/accounts/MailAccountSyncHelper.java new file mode 100644 index 00000000000..c43ab2b747e --- /dev/null +++ b/src/com/android/settings/accounts/MailAccountSyncHelper.java @@ -0,0 +1,61 @@ +/* + * Copyright ECORP SAS 2022 + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.android.settings.accounts; + +import android.annotation.TargetApi; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.os.Build; + +public class MailAccountSyncHelper { + + private static final String MAIL_PACKAGE = "foundation.e.mail"; + private static final String MAIL_RECEIVER_CLASS = "com.fsck.k9.account.AccountSyncReceiver"; + private static final String ACTION_PREFIX = "foundation.e.accountmanager.account."; + + public static void accountLoggedIn(Context applicationContext) { + if (applicationContext == null) { + return; + } + Intent intent = getIntent(); + intent.setAction(ACTION_PREFIX + "create"); + applicationContext.sendBroadcast(intent); + } + + public static void accountLoggedOut(Context applicationContext, String email) { + if (applicationContext == null || email == null || !email.contains("@")) { + return; + } + Intent intent = getIntent(); + intent.setAction(ACTION_PREFIX + "remove"); + intent.putExtra("account", email); + applicationContext.sendBroadcast(intent); + } + + @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) + private static Intent getIntent() { + Intent intent = new Intent(); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + intent.setComponent(new ComponentName(MAIL_PACKAGE, MAIL_RECEIVER_CLASS)); + return intent; + } + + + private MailAccountSyncHelper() { + } +} diff --git a/src/com/android/settings/accounts/RemoveAccountPreferenceController.java b/src/com/android/settings/accounts/RemoveAccountPreferenceController.java index 037f5846a88..7885234c586 100644 --- a/src/com/android/settings/accounts/RemoveAccountPreferenceController.java +++ b/src/com/android/settings/accounts/RemoveAccountPreferenceController.java @@ -158,7 +158,10 @@ public class RemoveAccountPreferenceController extends AbstractPreferenceControl @Override public void onClick(DialogInterface dialog, int which) { Activity activity = getTargetFragment().getActivity(); - AccountManager.get(activity).removeAccountAsUser(mAccount, activity, + AccountManager accountManager = AccountManager.get(activity); + String email = accountManager.getUserData(mAccount, "email_address"); + + accountManager.removeAccountAsUser(mAccount, activity, future -> { final Activity targetActivity = getTargetFragment().getActivity(); if (targetActivity == null || targetActivity.isFinishing()) { @@ -179,6 +182,7 @@ public class RemoveAccountPreferenceController extends AbstractPreferenceControl if (failed) { RemoveAccountFailureDialog.show(getTargetFragment()); } else { + MailAccountSyncHelper.accountLoggedOut(targetActivity.getApplicationContext(), email); targetActivity.finish(); } }, null, mUserHandle); -- GitLab From 15538e0e265760a85e038a9ff087be640f506145 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Thu, 7 Jul 2022 13:34:03 +0600 Subject: [PATCH 2/2] implement proper singleton for MailAccountSyncHelper --- .../accounts/MailAccountSyncHelper.java | 19 ++++++++++++++++--- .../RemoveAccountPreferenceController.java | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/com/android/settings/accounts/MailAccountSyncHelper.java b/src/com/android/settings/accounts/MailAccountSyncHelper.java index c43ab2b747e..c48886752d3 100644 --- a/src/com/android/settings/accounts/MailAccountSyncHelper.java +++ b/src/com/android/settings/accounts/MailAccountSyncHelper.java @@ -28,7 +28,9 @@ public class MailAccountSyncHelper { private static final String MAIL_RECEIVER_CLASS = "com.fsck.k9.account.AccountSyncReceiver"; private static final String ACTION_PREFIX = "foundation.e.accountmanager.account."; - public static void accountLoggedIn(Context applicationContext) { + private static MailAccountSyncHelper instance = null; + + public void accountLoggedIn(Context applicationContext) { if (applicationContext == null) { return; } @@ -37,7 +39,7 @@ public class MailAccountSyncHelper { applicationContext.sendBroadcast(intent); } - public static void accountLoggedOut(Context applicationContext, String email) { + public void accountLoggedOut(Context applicationContext, String email) { if (applicationContext == null || email == null || !email.contains("@")) { return; } @@ -48,7 +50,7 @@ public class MailAccountSyncHelper { } @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) - private static Intent getIntent() { + private Intent getIntent() { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.setComponent(new ComponentName(MAIL_PACKAGE, MAIL_RECEIVER_CLASS)); @@ -56,6 +58,17 @@ public class MailAccountSyncHelper { } + public static MailAccountSyncHelper getInstance() { + if (instance == null) { + synchronized (MailAccountSyncHelper.class) { + if (instance == null) { + instance = new MailAccountSyncHelper(); + } + } + } + return instance; + } + private MailAccountSyncHelper() { } } diff --git a/src/com/android/settings/accounts/RemoveAccountPreferenceController.java b/src/com/android/settings/accounts/RemoveAccountPreferenceController.java index 7885234c586..5858f9e1c5e 100644 --- a/src/com/android/settings/accounts/RemoveAccountPreferenceController.java +++ b/src/com/android/settings/accounts/RemoveAccountPreferenceController.java @@ -182,7 +182,7 @@ public class RemoveAccountPreferenceController extends AbstractPreferenceControl if (failed) { RemoveAccountFailureDialog.show(getTargetFragment()); } else { - MailAccountSyncHelper.accountLoggedOut(targetActivity.getApplicationContext(), email); + MailAccountSyncHelper.getInstance().accountLoggedOut(targetActivity.getApplicationContext(), email); targetActivity.finish(); } }, null, mUserHandle); -- GitLab