From 04811fd22f41f7dd60dbe6222da29151a922204c Mon Sep 17 00:00:00 2001 From: WSTxda Date: Wed, 5 Jun 2024 14:40:49 -0300 Subject: [PATCH] Refactor accounts manager screen - Refactor remove account dialog logic - Display icon, name in accounts list - Use lifecyclescope instead coroutinescope - Others thinks --- .../org/microg/gms/ui/AccountsFragment.kt | 122 +++++++++++------- .../src/main/res/values-pt-rBR/strings.xml | 1 - .../src/main/res/values/strings.xml | 1 - 3 files changed, 74 insertions(+), 50 deletions(-) diff --git a/play-services-core/src/main/java/org/microg/gms/ui/AccountsFragment.kt b/play-services-core/src/main/java/org/microg/gms/ui/AccountsFragment.kt index 2b89e46a57..7ee7d70b35 100644 --- a/play-services-core/src/main/java/org/microg/gms/ui/AccountsFragment.kt +++ b/play-services-core/src/main/java/org/microg/gms/ui/AccountsFragment.kt @@ -1,8 +1,10 @@ package org.microg.gms.ui +import android.accounts.Account import android.accounts.AccountManager import android.content.ActivityNotFoundException import android.content.Intent +import android.graphics.Bitmap import android.os.Bundle import android.provider.Settings import android.util.Log @@ -12,7 +14,8 @@ import android.view.MenuItem import android.view.View import android.widget.Toast import androidx.appcompat.app.AlertDialog -import androidx.core.content.ContextCompat +import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory +import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.findNavController import androidx.preference.Preference import androidx.preference.PreferenceCategory @@ -20,12 +23,13 @@ import androidx.preference.PreferenceFragmentCompat import com.google.android.gms.R import com.google.android.material.color.MaterialColors import com.google.android.material.transition.platform.MaterialSharedAxis -import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.microg.gms.auth.AuthConstants import org.microg.gms.auth.login.LoginActivity +import org.microg.gms.people.DatabaseHelper +import org.microg.gms.people.PeopleManager class AccountsFragment : PreferenceFragmentCompat() { @@ -33,80 +37,75 @@ class AccountsFragment : PreferenceFragmentCompat() { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.preferences_accounts) - updateAccountList() + updateSettings() } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enterTransition = MaterialSharedAxis(MaterialSharedAxis.X, true) reenterTransition = MaterialSharedAxis(MaterialSharedAxis.X, false) -} + } override fun onResume() { super.onResume() - updateAccountList() + updateSettings() } - private fun updateAccountList() { - val accountManager = AccountManager.get(requireContext()) - val accounts = accountManager.getAccountsByType(AuthConstants.DEFAULT_ACCOUNT_TYPE) - + private fun clearAccountPreferences() { val preferenceCategory = findPreference("prefcat_current_accounts") + preferenceCategory?.removeAll() + } - if (accounts.isEmpty()) { - preferenceCategory?.isVisible = false - } else { - preferenceCategory?.isVisible = true - preferenceCategory?.removeAll() + private fun updateSettings() { + val context = requireContext() - var isFirstAccount = true + val accountManager = AccountManager.get(context) + val accounts = accountManager.getAccountsByType(AuthConstants.DEFAULT_ACCOUNT_TYPE) + + clearAccountPreferences() - accounts.forEach { account -> - val newPreference = Preference(requireContext()).apply { - title = account.name - icon = ContextCompat.getDrawable(requireContext(), R.drawable.ic_google_logo) - preferenceCategory?.addPreference(this) + val preferenceCategory = findPreference("prefcat_current_accounts") - setOnPreferenceClickListener { - showConfirmationDialog(account.name) - true - } + accounts.forEach { account -> + val photo = PeopleManager.getOwnerAvatarBitmap(context, account.name, false) + val newPreference = Preference(requireContext()).apply { + title = getDisplayName(account) + summary = account.name + icon = getCircleBitmapDrawable(photo) + key = "account:${account.name}" + order = 0 + + setOnPreferenceClickListener { + showConfirmationDialog(account.name) + true } + } - if (isFirstAccount) { - isFirstAccount = false - newPreference.summary = getString(R.string.pref_accounts_default) + if (photo == null) { + lifecycleScope.launch(Dispatchers.IO) { + withContext(Dispatchers.IO) { + PeopleManager.getOwnerAvatarBitmap(context, account.name, true) + }?.let { newPreference.icon = getCircleBitmapDrawable(it) } } - - preferenceCategory?.addPreference(newPreference) } + + preferenceCategory?.addPreference(newPreference) } } private fun showConfirmationDialog(accountName: String) { - val alertDialogBuilder = AlertDialog.Builder(requireContext(), R.style.AppTheme_Dialog_Account) - alertDialogBuilder.apply { - setTitle(getString(R.string.dialog_title_remove_account)) - setMessage(getString(R.string.dialog_message_remove_account)) - setPositiveButton(getString(R.string.dialog_confirm_button)) { _, _ -> + AlertDialog.Builder(requireContext(), R.style.AppTheme_Dialog_Account) + .setTitle(getString(R.string.dialog_title_remove_account)) + .setMessage(getString(R.string.dialog_message_remove_account)) + .setPositiveButton(getString(R.string.dialog_confirm_button)) { _, _ -> removeAccount(accountName) - val toastMessage = getString(R.string.toast_remove_account_success, accountName) - showToast(toastMessage) - updateAccountList() - } - setNegativeButton(getString(R.string.dialog_cancel_button)) { dialog, _ -> + }.setNegativeButton(getString(R.string.dialog_cancel_button)) { dialog, _ -> dialog.dismiss() - } - create().show() - } - } - - private fun showToast(message: String) { - Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show() + }.create().show() } private fun removeAccount(accountName: String) { - CoroutineScope(Dispatchers.Main).launch { + lifecycleScope.launch(Dispatchers.Main) { val accountManager = AccountManager.get(requireContext()) val accounts = accountManager.getAccountsByType(AuthConstants.DEFAULT_ACCOUNT_TYPE) @@ -117,10 +116,14 @@ class AccountsFragment : PreferenceFragmentCompat() { accountManager.removeAccountExplicitly(it) } if (removedSuccessfully) { - updateAccountList() + updateSettings() + val toastMessage = + getString(R.string.toast_remove_account_success, accountName) + showToast(toastMessage) } } catch (e: Exception) { Log.e(tag, "Error removing account: $accountName", e) + showToast(getString(R.string.toast_remove_account_success)) } } } @@ -168,4 +171,27 @@ class AccountsFragment : PreferenceFragmentCompat() { else -> super.onOptionsItemSelected(item) } } + + private fun getDisplayName(account: Account): String? { + val databaseHelper = DatabaseHelper(requireContext()) + val cursor = databaseHelper.getOwner(account.name) + return try { + if (cursor.moveToNext()) { + cursor.getColumnIndex("display_name").takeIf { it >= 0 } + ?.let { cursor.getString(it) }?.takeIf { it.isNotBlank() } + } else null + } finally { + cursor.close() + databaseHelper.close() + } + } + + private fun getCircleBitmapDrawable(bitmap: Bitmap?) = + if (bitmap != null) RoundedBitmapDrawableFactory.create(resources, bitmap) + .also { it.isCircular = true } else null + + private fun showToast(message: String) { + Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show() + } + } diff --git a/play-services-core/src/main/res/values-pt-rBR/strings.xml b/play-services-core/src/main/res/values-pt-rBR/strings.xml index d5a396ffde..09db6584a5 100644 --- a/play-services-core/src/main/res/values-pt-rBR/strings.xml +++ b/play-services-core/src/main/res/values-pt-rBR/strings.xml @@ -169,7 +169,6 @@ Isso pode levar alguns minutos." Adicionar conta Gerenciador de contas Abrir o gerenciador de contas do dispositivo - Conta padrĂ£o diff --git a/play-services-core/src/main/res/values/strings.xml b/play-services-core/src/main/res/values/strings.xml index af786f0638..446df8a341 100644 --- a/play-services-core/src/main/res/values/strings.xml +++ b/play-services-core/src/main/res/values/strings.xml @@ -180,7 +180,6 @@ This can take a couple of minutes" Add account Manage accounts Open device accounts manager - Default account