Skip to content

Commit

Permalink
Added the automatic login function for Games
Browse files Browse the repository at this point in the history
  • Loading branch information
DaVinci9196 committed Jul 8, 2024
1 parent 991da7b commit cdf80c9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class AssistedSignInFragment(
lifecycleScope.launch {
val allowAutoLoginAccounts = mutableListOf<Account>()
accounts.forEach { account ->
val authStatus = checkAppAuthStatus(requireContext(), clientPackageName, options, account)
val authStatus = checkAccountAuthStatus(requireContext(), clientPackageName, options.scopes, account)
if (authStatus) {
allowAutoLoginAccounts.add(account)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Scope>?, 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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -71,23 +75,45 @@ class GamesConnectServiceImpl(val context: Context, override val lifecycle: Life
}
}
lifecycleScope.launchWhenStarted {
try {
val account = request?.previousStepResolutionResult?.resultData?.getParcelableExtra<Account>(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<Account>(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 =
Expand Down

0 comments on commit cdf80c9

Please sign in to comment.