From cdf80c93ea2e1edaa733eadb5a33ad03d018c300 Mon Sep 17 00:00:00 2001 From: davinci9196 Date: Mon, 8 Jul 2024 15:34:06 +0800 Subject: [PATCH] Added the automatic login function for Games --- .../gms/auth/signin/AssistedSignInFragment.kt | 2 +- .../org/microg/gms/auth/signin/extensions.kt | 5 +- .../microg/gms/games/GamesConnectService.kt | 54 ++++++++++++++----- 3 files changed, 44 insertions(+), 17 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 deb274b41c..6b07f59304 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 @@ -83,7 +83,7 @@ class AssistedSignInFragment( lifecycleScope.launch { val allowAutoLoginAccounts = mutableListOf() accounts.forEach { account -> - val authStatus = checkAppAuthStatus(requireContext(), clientPackageName, options, account) + val authStatus = checkAccountAuthStatus(requireContext(), clientPackageName, options.scopes, account) if (authStatus) { allowAutoLoginAccounts.add(account) } diff --git a/play-services-core/src/main/kotlin/org/microg/gms/auth/signin/extensions.kt b/play-services-core/src/main/kotlin/org/microg/gms/auth/signin/extensions.kt index 8285cec090..9c0cdfe4a7 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/auth/signin/extensions.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/auth/signin/extensions.kt @@ -94,8 +94,9 @@ fun getServerAuthTokenManager(context: Context, packageName: String, options: Go return serverAuthTokenManager } -suspend fun checkAppAuthStatus(context: Context, packageName: String, options: GoogleSignInOptions?, account: Account): Boolean { - val authManager = getOAuthManager(context, packageName, options, account) +suspend fun checkAccountAuthStatus(context: Context, packageName: String, scopeList: List?, account: Account): Boolean { + val scopes = scopeList.orEmpty().sortedBy { it.scopeUri } + val authManager = AuthManager(context, account.name, packageName, "oauth2:${scopes.joinToString(" ")}") authManager.ignoreStoredPermission = true return withContext(Dispatchers.IO) { authManager.requestAuth(true) }.auth != null } diff --git a/play-services-core/src/main/kotlin/org/microg/gms/games/GamesConnectService.kt b/play-services-core/src/main/kotlin/org/microg/gms/games/GamesConnectService.kt index 34810ef768..7e005d2372 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/games/GamesConnectService.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/games/GamesConnectService.kt @@ -6,6 +6,7 @@ package org.microg.gms.games import android.accounts.Account +import android.accounts.AccountManager import android.app.PendingIntent import android.content.Context import android.content.Intent @@ -17,6 +18,7 @@ import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.lifecycleScope import com.google.android.gms.common.Scopes import com.google.android.gms.common.api.CommonStatusCodes +import com.google.android.gms.common.api.Scope import com.google.android.gms.common.api.Status import com.google.android.gms.common.internal.ConnectionInfo import com.google.android.gms.common.internal.GetServiceRequest @@ -26,8 +28,10 @@ import com.google.android.gms.games.internal.connect.GamesSignInResponse import com.google.android.gms.games.internal.connect.IGamesConnectCallbacks import com.google.android.gms.games.internal.connect.IGamesConnectService import org.microg.gms.BaseService +import org.microg.gms.auth.AuthConstants import org.microg.gms.auth.AuthManager import org.microg.gms.auth.AuthPrefs +import org.microg.gms.auth.signin.checkAccountAuthStatus import org.microg.gms.common.GmsService import org.microg.gms.common.PackageUtils import org.microg.gms.utils.warnOnTransactionIssues @@ -71,23 +75,45 @@ class GamesConnectServiceImpl(val context: Context, override val lifecycle: Life } } lifecycleScope.launchWhenStarted { - try { - val account = request?.previousStepResolutionResult?.resultData?.getParcelableExtra(EXTRA_ACCOUNT) + val status = autoSelectLogin(request) + if (status) { + Log.d(TAG, "signIn success") + callback?.onSignIn(Status.SUCCESS, GamesSignInResponse().apply { gameRunToken = UUID.randomUUID().toString() }) + } else { + sendSignInRequired() + } + } + } + + private suspend fun autoSelectLogin(request: GamesSignInRequest?): Boolean { + runCatching { + var account = request?.previousStepResolutionResult?.resultData?.getParcelableExtra(EXTRA_ACCOUNT) ?: GamesConfigurationService.getDefaultAccount(context, packageName) - ?: return@launchWhenStarted sendSignInRequired() - val authManager = AuthManager(context, account.name, packageName, "oauth2:${Scopes.GAMES_LITE}") - if (!authManager.isPermitted && !AuthPrefs.isTrustGooglePermitted(context)) return@launchWhenStarted sendSignInRequired() - val result = performGamesSignIn(context, packageName, account) - if (result) { - callback?.onSignIn(Status.SUCCESS, GamesSignInResponse().apply { gameRunToken = UUID.randomUUID().toString() }) - } else { - sendSignInRequired() - } - } catch (e: Exception) { - Log.w(TAG, e) - return@launchWhenStarted sendSignInRequired() + Log.d(TAG, "autoSelectLogin signInType: ${request?.signInType} account: $account") + val autoLogin = if (account == null && request?.signInType == 1) { + val accounts = AccountManager.get(context).getAccountsByType(AuthConstants.DEFAULT_ACCOUNT_TYPE) + account = accounts.filter { targetAccount -> + checkAccountAuthStatus(context, packageName, arrayListOf(Scope(Scopes.GAMES_LITE)), targetAccount) + }.getOrNull(0) + true + } else { + false + } + if (account == null) { + Log.d(TAG, "autoSelectLogin Accounts is Empty") + return false + } + val authManager = AuthManager(context, account.name, packageName, "oauth2:${Scopes.GAMES_LITE}") + if (!authManager.isPermitted && !AuthPrefs.isTrustGooglePermitted(context)) return false + val performGamesSignInStatus = performGamesSignIn(context, packageName, account) + if (performGamesSignInStatus && autoLogin) { + GamesConfigurationService.setDefaultAccount(context, packageName, account) } + return performGamesSignInStatus + }.onFailure { + Log.d(TAG, "autoSelectLogin fail", it) } + return false } override fun onTransact(code: Int, data: Parcel, reply: Parcel?, flags: Int): Boolean =