Skip to content

Commit

Permalink
Refactor accounts manager screen
Browse files Browse the repository at this point in the history
- Refactor remove account dialog logic
- Display icon, name in accounts list
- Use lifecyclescope instead coroutinescope
- Others thinks
  • Loading branch information
WSTxda committed Jun 5, 2024
1 parent b7204d9 commit 04811fd
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 50 deletions.
122 changes: 74 additions & 48 deletions play-services-core/src/main/java/org/microg/gms/ui/AccountsFragment.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,101 +14,98 @@ 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
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() {

private val tag = AccountsFragment::class.java.simpleName

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<PreferenceCategory>("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<PreferenceCategory>("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)

Expand All @@ -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))
}
}
}
Expand Down Expand Up @@ -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()
}

}
1 change: 0 additions & 1 deletion play-services-core/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ Isso pode levar alguns minutos."</string>
<string name="pref_accounts_add_account_title">Adicionar conta</string>
<string name="pref_accounts_manage_accounts_title">Gerenciador de contas</string>
<string name="pref_accounts_manage_accounts_summary">Abrir o gerenciador de contas do dispositivo</string>
<string name="pref_accounts_default">Conta padrão</string>

<!-- Account remove dialog-->

Expand Down
1 change: 0 additions & 1 deletion play-services-core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ This can take a couple of minutes"</string>
<string name="pref_accounts_add_account_title">Add account</string>
<string name="pref_accounts_manage_accounts_title">Manage accounts</string>
<string name="pref_accounts_manage_accounts_summary">Open device accounts manager</string>
<string name="pref_accounts_default">Default account</string>

<!-- Account remove dialog-->

Expand Down

0 comments on commit 04811fd

Please sign in to comment.