diff --git a/app/src/main/java/foundation/e/accountmanager/MailAccountSyncHelper.kt b/app/src/main/java/foundation/e/accountmanager/MailAccountSyncHelper.kt new file mode 100644 index 0000000000000000000000000000000000000000..b93fcfa2b5986f54ad2acf145e2b1bbe1d49c781 --- /dev/null +++ b/app/src/main/java/foundation/e/accountmanager/MailAccountSyncHelper.kt @@ -0,0 +1,53 @@ +/* + * 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 foundation.e.accountmanager + +import android.content.ComponentName +import android.content.Context +import android.content.Intent + +object MailAccountSyncHelper { + + 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." + + fun accountLoggedIn(applicationContext : Context?) { + val intent = getIntent() + intent.action = ACTION_PREFIX + "create" + applicationContext?.sendBroadcast(intent) + } + + fun accountLoggedOut(applicationContext: Context?, email: String?) { + email?.let { + if (!it.contains("@")) { + return@let + } + val intent = getIntent() + intent.action = ACTION_PREFIX + "remove" + intent.putExtra("account", it) + applicationContext?.sendBroadcast(intent) + } + } + + private fun getIntent() : Intent { + val intent = Intent() + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) + intent.component = ComponentName(MAIL_PACKAGE, MAIL_RECEIVER_CLASS) + return intent + } +} \ No newline at end of file diff --git a/app/src/main/java/foundation/e/accountmanager/resource/LocalAddressBook.kt b/app/src/main/java/foundation/e/accountmanager/resource/LocalAddressBook.kt index b36a1b7013752d7dd802bdb3895899bfdb0a101b..2d9fe422ecf383a82bb19b909277d0e0df5047c7 100644 --- a/app/src/main/java/foundation/e/accountmanager/resource/LocalAddressBook.kt +++ b/app/src/main/java/foundation/e/accountmanager/resource/LocalAddressBook.kt @@ -12,6 +12,8 @@ import android.accounts.AccountManager import android.content.* import android.os.Build import android.os.Bundle +import android.os.Handler +import android.os.Looper import android.os.RemoteException import android.provider.ContactsContract import android.provider.ContactsContract.CommonDataKinds.GroupMembership @@ -19,11 +21,13 @@ import android.provider.ContactsContract.Groups import android.provider.ContactsContract.RawContacts import android.util.Base64 import foundation.e.accountmanager.DavUtils +import foundation.e.accountmanager.MailAccountSyncHelper import foundation.e.accountmanager.R import foundation.e.accountmanager.log.Logger import foundation.e.accountmanager.model.Collection import foundation.e.accountmanager.model.AppDatabase import foundation.e.accountmanager.model.SyncState +import foundation.e.accountmanager.settings.AccountSettings import foundation.e.vcard4android.* import java.io.ByteArrayOutputStream import java.util.* @@ -243,11 +247,43 @@ class LocalAddressBook( fun delete() { val accountManager = AccountManager.get(context) + val email = accountManager.getUserData(account, AccountSettings.KEY_EMAIL_ADDRESS) + @Suppress("DEPRECATION") - if (Build.VERSION.SDK_INT >= 22) - accountManager.removeAccount(account, null, null, null) - else - accountManager.removeAccount(account, null, null) + if (Build.VERSION.SDK_INT >= 22) { + removeAccount(accountManager, email) + } + else { + removeAccountForOlderSdk(accountManager, email) + } + } + + private fun removeAccountForOlderSdk(accountManager: AccountManager, email: String?) { + accountManager.removeAccount(account, { + try { + if (it.result) { + Handler(Looper.getMainLooper()).post { + MailAccountSyncHelper.accountLoggedOut(context.applicationContext, email) + } + } + } catch (e: Exception) { + Logger.log.log(Level.SEVERE, "Couldn't remove account", e) + } + }, null) + } + + private fun removeAccount(accountManager: AccountManager, email: String?) { + accountManager.removeAccount(account, null, { + try { + if (it.result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) { + Handler(Looper.getMainLooper()).post { + MailAccountSyncHelper.accountLoggedOut(context.applicationContext, email) + } + } + } catch (e: Exception) { + Logger.log.log(Level.SEVERE, "Couldn't remove account", e) + } + }, null) } diff --git a/app/src/main/java/foundation/e/accountmanager/ui/account/AccountActivity.kt b/app/src/main/java/foundation/e/accountmanager/ui/account/AccountActivity.kt index 0abe8d1404c3dac64425bc85e27b9e1bfc2233e3..d7070bd9ea63b45a853493b2b541c22721bc688a 100644 --- a/app/src/main/java/foundation/e/accountmanager/ui/account/AccountActivity.kt +++ b/app/src/main/java/foundation/e/accountmanager/ui/account/AccountActivity.kt @@ -30,6 +30,8 @@ import foundation.e.accountmanager.resource.LocalTaskList import foundation.e.ical4android.TaskProvider import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.snackbar.Snackbar +import foundation.e.accountmanager.MailAccountSyncHelper +import foundation.e.accountmanager.settings.AccountSettings import kotlinx.android.synthetic.main.activity_account.* import java.util.concurrent.Executors import java.util.logging.Level @@ -124,29 +126,40 @@ class AccountActivity: AppCompatActivity() { private fun deleteAccount() { val accountManager = AccountManager.get(this) + val email = accountManager.getUserData(model.account, AccountSettings.KEY_EMAIL_ADDRESS) if (Build.VERSION.SDK_INT >= 22) - accountManager.removeAccount(model.account, this, { future -> - try { - if (future.result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) - Handler(Looper.getMainLooper()).post { - finish() - } - } catch(e: Exception) { - Logger.log.log(Level.SEVERE, "Couldn't remove account", e) - } - }, null) + removeAccount(accountManager, email) else - accountManager.removeAccount(model.account, { future -> - try { - if (future.result) - Handler(Looper.getMainLooper()).post { - finish() - } - } catch (e: Exception) { - Logger.log.log(Level.SEVERE, "Couldn't remove account", e) - } - }, null) + removeAccountForOlderSdk(accountManager, email) + } + + private fun removeAccountForOlderSdk(accountManager: AccountManager, email: String?) { + accountManager.removeAccount(model.account, { future -> + try { + if (future.result) + Handler(Looper.getMainLooper()).post { + MailAccountSyncHelper.accountLoggedOut(applicationContext, email) + finish() + } + } catch (e: Exception) { + Logger.log.log(Level.SEVERE, "Couldn't remove account", e) + } + }, null) + } + + private fun removeAccount(accountManager: AccountManager, email: String?) { + accountManager.removeAccount(model.account, this, { future -> + try { + if (future.result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) + Handler(Looper.getMainLooper()).post { + MailAccountSyncHelper.accountLoggedOut(applicationContext, email) + finish() + } + } catch (e: Exception) { + Logger.log.log(Level.SEVERE, "Couldn't remove account", e) + } + }, null) } diff --git a/app/src/main/java/foundation/e/accountmanager/ui/setup/AccountDetailsFragment.kt b/app/src/main/java/foundation/e/accountmanager/ui/setup/AccountDetailsFragment.kt index b5ffc66b60cabc6812bd9b08ad2184a5594eec90..cefb05f1e4ff983e6d77c983b885097710e23cd5 100644 --- a/app/src/main/java/foundation/e/accountmanager/ui/setup/AccountDetailsFragment.kt +++ b/app/src/main/java/foundation/e/accountmanager/ui/setup/AccountDetailsFragment.kt @@ -27,6 +27,7 @@ import androidx.lifecycle.* import foundation.e.accountmanager.Constants import foundation.e.accountmanager.DavService import foundation.e.accountmanager.InvalidAccountException +import foundation.e.accountmanager.MailAccountSyncHelper import foundation.e.accountmanager.R import foundation.e.accountmanager.databinding.LoginAccountDetailsBinding import foundation.e.accountmanager.log.Logger @@ -155,6 +156,7 @@ class AccountDetailsFragment : Fragment() { if (success) { Toast.makeText(context, "Added account successfully", Toast.LENGTH_LONG) .show() + MailAccountSyncHelper.accountLoggedIn(context?.applicationContext) requireActivity().setResult(Activity.RESULT_OK) requireActivity().finish() if (requireActivity().intent.hasExtra(