Commit 720a12e4 authored by Fahim Salam Chowdhury's avatar Fahim Salam Chowdhury 👽
Browse files

add remove account broadcast call & refactor add account broadcast call

parent 0ef94b59
Pipeline #200057 passed with stage
in 6 minutes and 50 seconds
package foundation.e.accountmanager
import android.accounts.Account
import android.accounts.AccountManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.util.Log
import foundation.e.accountmanager.settings.AccountSettings
object MailAccountSyncHelper {
fun accountLoggedIn(applicationContext : Context?) {
val intent = Intent()
intent.action = "foundation.e.accountmanager.account.create"
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
intent.component = ComponentName("foundation.e.mail", "com.fsck.k9.OnAccountCreationReceiver")
applicationContext?.sendBroadcast(intent)
}
fun accountLoggedOut(applicationContext: Context?, email: String?) {
email?.let {
if (!it.contains("@")) {
return@let
}
val intent = Intent()
intent.action = "foundation.e.accountmanager.account.remove"
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
intent.component = ComponentName("foundation.e.mail", "com.fsck.k9.OnAccountRemovalReceiver")
intent.putExtra("account", it)
applicationContext?.sendBroadcast(intent)
}
}
}
\ No newline at end of file
......@@ -10,20 +10,20 @@ package foundation.e.accountmanager.resource
import android.accounts.Account
import android.accounts.AccountManager
import android.content.*
import android.os.Build
import android.os.Bundle
import android.os.RemoteException
import android.os.*
import android.provider.ContactsContract
import android.provider.ContactsContract.CommonDataKinds.GroupMembership
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 +243,30 @@ 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)
accountManager.removeAccount(account, null, { future ->
try {
if (future.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)
else
accountManager.removeAccount(account, null, null)
accountManager.removeAccount(account, { future ->
try {
if (future.result)
Handler(Looper.getMainLooper()).post {
MailAccountSyncHelper.accountLoggedOut(context.applicationContext, email)
}
} catch (e: Exception) {
Logger.log.log(Level.SEVERE, "Couldn't remove account", e)
}
}, null)
}
......
......@@ -45,12 +45,6 @@ class AccountsActivity: AppCompatActivity(), NavigationView.OnNavigationItemSele
super.onCreate(savedInstanceState)
settings = Settings.getInstance(this)
val intent = Intent()
intent.action = "foundation.e.accountmanager.account.create"
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
intent.component = ComponentName("foundation.e.mail", "com.fsck.k9.OnAccountCreationReceiver")
applicationContext.sendBroadcast(intent)
setContentView(R.layout.activity_accounts)
setSupportActionBar(toolbar)
......
......@@ -11,6 +11,7 @@ import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import androidx.annotation.MainThread
......@@ -30,6 +31,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,12 +127,14 @@ 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 {
MailAccountSyncHelper.accountLoggedOut(applicationContext, email)
finish()
}
} catch(e: Exception) {
......@@ -141,6 +146,7 @@ class AccountActivity: AppCompatActivity() {
try {
if (future.result)
Handler(Looper.getMainLooper()).post {
MailAccountSyncHelper.accountLoggedOut(applicationContext, email)
finish()
}
} catch (e: Exception) {
......
......@@ -24,10 +24,6 @@ import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.*
import foundation.e.accountmanager.Constants
import foundation.e.accountmanager.DavService
import foundation.e.accountmanager.InvalidAccountException
import foundation.e.accountmanager.R
import foundation.e.accountmanager.databinding.LoginAccountDetailsBinding
import foundation.e.accountmanager.log.Logger
import foundation.e.accountmanager.model.AppDatabase
......@@ -40,6 +36,8 @@ import foundation.e.accountmanager.settings.Settings
import foundation.e.ical4android.TaskProvider
import foundation.e.vcard4android.GroupMethod
import com.google.android.material.snackbar.Snackbar
import foundation.e.accountmanager.*
import foundation.e.accountmanager.R
import java.util.logging.Level
import kotlin.concurrent.thread
......@@ -155,6 +153,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(
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment