From 37207dab70bd10e00ad93f212233722668945f22 Mon Sep 17 00:00:00 2001 From: davinci9196 Date: Tue, 3 Dec 2024 15:43:06 +0800 Subject: [PATCH] Auth: New abnormal account processing --- .../gms/auth/signin/AssistedSignInFragment.kt | 96 ++++++++++--------- .../src/main/res/values-zh-rCN/strings.xml | 1 + .../src/main/res/values/strings.xml | 1 + 3 files changed, 55 insertions(+), 43 deletions(-) diff --git a/play-services-core/src/main/kotlin/org/microg/gms/auth/signin/AssistedSignInFragment.kt b/play-services-core/src/main/kotlin/org/microg/gms/auth/signin/AssistedSignInFragment.kt index 673197229f..e335252d97 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/auth/signin/AssistedSignInFragment.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/auth/signin/AssistedSignInFragment.kt @@ -8,6 +8,7 @@ package org.microg.gms.auth.signin import android.accounts.Account import android.app.Dialog import android.content.DialogInterface +import android.graphics.Color import android.os.Bundle import android.util.Log import android.view.KeyEvent @@ -19,6 +20,8 @@ import android.widget.ImageView import android.widget.LinearLayout import android.widget.ProgressBar import android.widget.TextView +import androidx.collection.ArraySet +import androidx.collection.arraySetOf import androidx.lifecycle.lifecycleScope import com.google.android.gms.R import com.google.android.gms.auth.api.identity.BeginSignInRequest @@ -55,6 +58,7 @@ class AssistedSignInFragment( private var container: FrameLayout? = null private var loginJob: Job? = null private var isSigningIn = false + private val authStatusList = arraySetOf>() private var lastChooseAccount: Account? = null private var lastChooseAccountPermitted = false @@ -79,39 +83,32 @@ class AssistedSignInFragment( } } - private fun filterAccountsLogin(multiMethod: (List) -> Unit, loginMethod: (Account, Boolean) -> Unit) { - lifecycleScope.launch { - val allowAutoLoginAccounts = mutableListOf() - runCatching { - accounts.forEach { account -> - val authStatus = checkAccountAuthStatus(requireContext(), clientPackageName, options.scopes, account) - if (authStatus) { - allowAutoLoginAccounts.add(account) - } - } - }.onFailure { - Log.d(TAG, "filterAccountsLogin: error", it) - errorBlock(Status(CommonStatusCodes.INTERNAL_ERROR, "auth error")) - return@launch - } - if (accounts.size == 1) { - loginMethod(accounts.first(), allowAutoLoginAccounts.isNotEmpty()) - return@launch + private suspend fun filterAccountsLogin(multiMethod: (ArraySet>) -> Unit, loginMethod: (Account, Boolean) -> Unit) { + accounts.forEach { account -> + val authStatus = try { + checkAccountAuthStatus(requireContext(), clientPackageName, options.scopes, account) + } catch (e: Exception) { + Log.d(TAG, "checkAccountAuthStatus: account:${account.name} auth error ", e) + null } - val filterByAuthorizedAccounts = beginSignInRequest.googleIdTokenRequestOptions.filterByAuthorizedAccounts() - if (!filterByAuthorizedAccounts) { - multiMethod(allowAutoLoginAccounts) - return@launch - } - if (allowAutoLoginAccounts.size == 1) { - loginMethod(allowAutoLoginAccounts.first(), true) - return@launch - } - multiMethod(allowAutoLoginAccounts) + authStatusList.add(Pair(account, authStatus)) + } + Log.d(TAG, "filterAccountsLogin: authStatusList: $authStatusList") + val checkAccounts = authStatusList.filter { it.second != null } + if (checkAccounts.size == 1) { + loginMethod(checkAccounts.first().first, checkAccounts.first().second!!) + return } + val filterByAuthorizedAccounts = beginSignInRequest.googleIdTokenRequestOptions.filterByAuthorizedAccounts() + val authorizedAccounts = authStatusList.filter { it.second == true } + if (filterByAuthorizedAccounts && authorizedAccounts.isNotEmpty()) { + loginMethod(checkAccounts.first().first, true) + return + } + multiMethod(authStatusList) } - private fun prepareMultiSignIn(allowAutoLoginAccounts: List) { + private fun prepareMultiSignIn(authorizedAccounts: ArraySet>) { lifecycleScope.launch { notifyCancelBtn(true) container?.removeAllViews() @@ -120,9 +117,10 @@ class AssistedSignInFragment( chooseView.findViewById(R.id.sign_multi_description).text = String.format(getString(R.string.credentials_assisted_choose_account_subtitle), clientAppLabel) val accountViews = chooseView.findViewById(R.id.sign_multi_account_container) - accounts.forEachIndexed { index, account -> - val accountView = - LayoutInflater.from(requireContext()).inflate(R.layout.assisted_signin_multi_layout, null) + val progress = chooseView.findViewById(R.id.sign_multi_progress) + authorizedAccounts.forEachIndexed { index, pair -> + val account = pair.first + val accountView = LayoutInflater.from(requireContext()).inflate(R.layout.assisted_signin_multi_layout, null) accountView.findViewById(R.id.account_email).text = account.name withContext(Dispatchers.IO) { PeopleManager.getDisplayName(requireContext(), account.name) @@ -131,15 +129,23 @@ class AssistedSignInFragment( PeopleManager.getOwnerAvatarBitmap(requireContext(), account.name, false) ?: PeopleManager.getOwnerAvatarBitmap(requireContext(), account.name, true) }.let { accountView.findViewById(R.id.account_photo).setImageBitmap(it) } - accountView.findViewById(R.id.account_description).text = - getString(R.string.credentials_assisted_signin_button_text_long) + if (pair.second != null) { + accountView.findViewById(R.id.account_description).text = + getString(R.string.credentials_assisted_signin_button_text_long) + accountView.setOnClickListener { + progress.visibility = View.VISIBLE + prepareSignInLoading(account, pair.second!!) + } + } else { + accountView.findViewById(R.id.account_description).apply { + text = getString(R.string.credentials_assisted_choose_account_error_tips) + setTextColor(Color.RED) + } + accountView.setOnClickListener(null) + } if (index == accounts.size - 1) { accountView.findViewById(R.id.multi_account_line).visibility = View.GONE } - accountView.setOnClickListener { - chooseView.findViewById(R.id.sign_multi_progress).visibility = View.VISIBLE - prepareSignInLoading(account, permitted = allowAutoLoginAccounts.any { it == account }) - } accountViews.addView(accountView) } container?.addView(chooseView) @@ -173,7 +179,7 @@ class AssistedSignInFragment( private fun prepareChooseLogin(account: Account, showConsent: Boolean = false, permitted: Boolean = false) { lifecycleScope.launch { - notifyCancelBtn(true) + notifyCancelBtn(visible = true, backToMulti = authStatusList.size > 1) container?.removeAllViews() val reloadView = LayoutInflater.from(requireContext()).inflate(R.layout.assisted_signin_back_consent_layout, null) @@ -235,9 +241,6 @@ class AssistedSignInFragment( super.onViewCreated(view, savedInstanceState) Log.d(TAG, "onViewCreated") cancelBtn = view.findViewById(R.id.cancel) - cancelBtn?.setOnClickListener { - dismiss() - } container = view.findViewById(R.id.google_sign_in_container) } @@ -250,9 +253,16 @@ class AssistedSignInFragment( super.onDismiss(dialog) } - private fun notifyCancelBtn(visible: Boolean) { + private fun notifyCancelBtn(visible: Boolean, backToMulti:Boolean = false) { cancelBtn?.visibility = if (visible) View.VISIBLE else View.GONE cancelBtn?.isClickable = visible + cancelBtn?.setOnClickListener { + if (backToMulti) { + prepareMultiSignIn(authStatusList) + return@setOnClickListener + } + dismiss() + } } private fun startLogin(account: Account, permitted: Boolean = false) { diff --git a/play-services-core/src/main/res/values-zh-rCN/strings.xml b/play-services-core/src/main/res/values-zh-rCN/strings.xml index 64e3a71af2..6dfbb071de 100644 --- a/play-services-core/src/main/res/values-zh-rCN/strings.xml +++ b/play-services-core/src/main/res/values-zh-rCN/strings.xml @@ -236,6 +236,7 @@ microG GmsCore 内置一套自由的 SafetyNet 实现,但是官方服务器要 您可以在您的 Google 账号中管理“使用 Google 账号登录”功能。 选择账号 以继续使用 %1$s + 账号异常 使用 Google 账号登录 允许 %1$s 访问%2$s <b><xliff:g example=\"F-Droid\">%1$s</xliff:g></b> 想以 <xliff:g example=\"F-Droid Inc.\">%3$s</xliff:g></b> 的 <b><xliff:g example=\"F-Droid\">%2$s</xliff:g> 访问你的账户。这可能授予访问你账户的权限。 diff --git a/play-services-core/src/main/res/values/strings.xml b/play-services-core/src/main/res/values/strings.xml index 062aa8d1e7..15c63cf1ea 100644 --- a/play-services-core/src/main/res/values/strings.xml +++ b/play-services-core/src/main/res/values/strings.xml @@ -304,6 +304,7 @@ Please set up a password, PIN, or pattern lock screen." You can manage Sign in with Google in your Google Accounts. Choose account to continue to %1$s + Account abnormality Sign in with Google You are using the microG Limited Services. Unlike the usual microG Services, this flavor only works with apps using microG libraries, not those on Google Play. This means that most applications will ignore these services.