From 1d5e49df9b1c5e71a824386cd29cb4332bb79a0b Mon Sep 17 00:00:00 2001 From: Aristotelis Dossas Date: Tue, 1 Mar 2022 05:54:00 +0200 Subject: [PATCH] Move remove account to the Authenticator --- .../data/local/account/NewAccountManager.kt | 4 +- .../service/FireflyAccountAuthenticator.kt | 34 ++++++------- .../xyz/hisname/fireflyiii/ui/HomeActivity.kt | 2 +- .../hisname/fireflyiii/ui/HomeViewModel.kt | 48 ++++++------------- 4 files changed, 34 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/xyz/hisname/fireflyiii/data/local/account/NewAccountManager.kt b/app/src/main/java/xyz/hisname/fireflyiii/data/local/account/NewAccountManager.kt index 44df1ca5..5ca75391 100644 --- a/app/src/main/java/xyz/hisname/fireflyiii/data/local/account/NewAccountManager.kt +++ b/app/src/main/java/xyz/hisname/fireflyiii/data/local/account/NewAccountManager.kt @@ -39,9 +39,7 @@ class NewAccountManager (private val accountManager: AccountManager, get() = accountManager.getUserData(account, "AUTH_METHOD") ?: "" set(value) = accountManager.setUserData(account, "AUTH_METHOD", value) - fun destroyAccount(){ - accountManager.removeAccount(account, null, null, null) - } + fun destroyAccount() = accountManager.removeAccount(account, null, null, null) var tokenExpiry get() = accountManager.getUserData(account, "token_expires_in").toLong() diff --git a/app/src/main/java/xyz/hisname/fireflyiii/service/FireflyAccountAuthenticator.kt b/app/src/main/java/xyz/hisname/fireflyiii/service/FireflyAccountAuthenticator.kt index 5d08da62..b468ebbb 100644 --- a/app/src/main/java/xyz/hisname/fireflyiii/service/FireflyAccountAuthenticator.kt +++ b/app/src/main/java/xyz/hisname/fireflyiii/service/FireflyAccountAuthenticator.kt @@ -24,6 +24,7 @@ import android.content.Context import android.content.Intent import android.os.Bundle import androidx.core.os.bundleOf +import androidx.work.WorkManager import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking import xyz.hisname.fireflyiii.Constants @@ -65,25 +66,24 @@ class FireflyAccountAuthenticator(private val context: Context): AbstractAccount override fun getAccountRemovalAllowed(response: AccountAuthenticatorResponse, account: Account): Bundle { runBlocking(Dispatchers.IO){ - val fireflyUserDatabase = FireflyUserDatabase.getInstance(context).fireflyUserDao() - fireflyUserDatabase.deleteCurrentUser() - val customCaFile = File(context.applicationInfo.dataDir + "/" + account.name + ".pem") - val customPref = File(context.applicationInfo.dataDir + "/shared_prefs/" + account.name + "-user-preferences.xml") - if(customCaFile.exists()){ - customCaFile.delete() - } - customPref.delete() - val fireflyUsers: List - runBlocking(Dispatchers.IO) { - fireflyUsers = fireflyUserDatabase.getAllUser() - } - if(fireflyUsers.isNotEmpty()){ - runBlocking(Dispatchers.IO){ - fireflyUserDatabase.updateActiveUser(fireflyUsers[0].uniqueHash, true) + context.deleteDatabase(account.name + "-photuris.db") + context.deleteDatabase(account.name + "-temp-" + Constants.DB_NAME + "-photuris.db") + context.deleteFile(account.name + ".pem") + val sharedPrefDir = File(context.applicationInfo.dataDir + "/shared_prefs") + sharedPrefDir.resolve(account.name + "-user-preferences.xml").delete() + + val fireflyUserDao = FireflyUserDatabase.getInstance(context).fireflyUserDao() + val userToBeDeleted = fireflyUserDao.getUserByHash(account.name) + if (userToBeDeleted != null) { + fireflyUserDao.deleteUserByPrimaryKey(userToBeDeleted.id) + + val fireflyUsers = fireflyUserDao.getAllUser() + if(userToBeDeleted.activeUser && fireflyUsers.isNotEmpty()){ + fireflyUserDao.updateActiveUser(fireflyUsers[0].uniqueHash, true) } } - File(context.getDatabasePath(account.name + "-temp-" + Constants.DB_NAME).toString()).delete() - File(context.getDatabasePath(account.name + "-photuris.db").toString()).delete() + + WorkManager.getInstance(context).cancelAllWorkByTag(account.name) } return super.getAccountRemovalAllowed(response, account) } diff --git a/app/src/main/java/xyz/hisname/fireflyiii/ui/HomeActivity.kt b/app/src/main/java/xyz/hisname/fireflyiii/ui/HomeActivity.kt index 163e9792..3f86be4a 100644 --- a/app/src/main/java/xyz/hisname/fireflyiii/ui/HomeActivity.kt +++ b/app/src/main/java/xyz/hisname/fireflyiii/ui/HomeActivity.kt @@ -98,7 +98,7 @@ class HomeActivity: BaseActivity(){ authenticator = Authenticator(this, ::handleResult) authenticator.authenticate() } - //homeViewModel.deleteUnusedAccount() + homeViewModel.deleteUnusedAccount() } } diff --git a/app/src/main/java/xyz/hisname/fireflyiii/ui/HomeViewModel.kt b/app/src/main/java/xyz/hisname/fireflyiii/ui/HomeViewModel.kt index f79405b7..88de7954 100644 --- a/app/src/main/java/xyz/hisname/fireflyiii/ui/HomeViewModel.kt +++ b/app/src/main/java/xyz/hisname/fireflyiii/ui/HomeViewModel.kt @@ -88,36 +88,30 @@ class HomeViewModel(application: Application): BaseViewModel(application) { fun getFireflyUsers(): LiveData>{ val usersLiveData = MutableLiveData>() viewModelScope.launch(Dispatchers.IO){ + removeInvalidFireflyAccounts() usersLiveData.postValue(fireflyUserDatabase.getAllUser()) } return usersLiveData } + private suspend fun removeInvalidFireflyAccounts() { + fireflyUserDatabase.getAllUser() + .filter { it.userEmail.isEmpty() } + .forEach { removeFireflyAccount(it.uniqueHash) } + } + fun removeFireflyAccounts(fireflyUsers: List){ viewModelScope.launch(Dispatchers.IO){ - var isDefault = false - val allUsers = fireflyUserDatabase.getAllUser() - fireflyUsers.forEach { user -> - val fireflyUser = fireflyUserDatabase.getUserByHash(user.uniqueHash) - isDefault = fireflyUser.activeUser - File(getApplication().applicationInfo.dataDir + "/shared_prefs/" + fireflyUser.uniqueHash - + "-user-preferences.xml").delete() - fireflyUserDatabase.deleteUserByPrimaryKey(fireflyUser.id) - File(getApplication().applicationInfo.dataDir + "/databases/" + fireflyUser.uniqueHash - + "-photuris.db").delete() - File(getApplication().applicationInfo.dataDir + fireflyUser.uniqueHash + ".pem").delete() - val accountManager = NewAccountManager(AccountManager.get(getApplication()), fireflyUser.uniqueHash) - accountManager.destroyAccount() - WorkManager.getInstance(getApplication()).cancelAllWorkByTag(user.uniqueHash) - } - // Check if all users are being deleted. can't set a default user if there are no users in DB. - if(isDefault && allUsers.size != fireflyUsers.size){ - val uniqueHash = fireflyUserDatabase.getAllUser()[0].uniqueHash - fireflyUserDatabase.updateActiveUser(uniqueHash, true) - } + fireflyUsers.forEach { removeFireflyAccount(it.uniqueHash) } } } + private fun removeFireflyAccount(uuid: String) { + val app = getApplication() + val accountManager = NewAccountManager(AccountManager.get(app), uuid) + accountManager.destroyAccount().result + } + fun migrateFirefly(){ /* Migration routine: * 1. Check if old database exists(firefly.db) @@ -207,19 +201,7 @@ class HomeViewModel(application: Application): BaseViewModel(application) { userArray.add(fireflyUsers.uniqueHash) } val accountToRemove = accountArray.filterNot { userArray.contains(it) } - accountToRemove.forEach { uuid -> - NewAccountManager(AccountManager.get(getApplication()), uuid).destroyAccount() - val customCaFile = File(getApplication().applicationInfo.dataDir - + "/" + uuid + ".pem") - val customPref = File(getApplication().applicationInfo.dataDir - + "/shared_prefs/" + uuid + "-user-preferences.xml") - if(customCaFile.exists()){ - customCaFile.delete() - } - customPref.delete() - File(getApplication().getDatabasePath("$uuid-temp-" + Constants.DB_NAME).toString()).delete() - File(getApplication().getDatabasePath("$uuid-photuris.db").toString()).delete() - } + accountToRemove.forEach { removeFireflyAccount(it) } } }