diff --git a/play-services-core/src/main/kotlin/org/microg/gms/games/GamesService.kt b/play-services-core/src/main/kotlin/org/microg/gms/games/GamesService.kt index cd30a1675b..e29c2c0ee4 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/games/GamesService.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/games/GamesService.kt @@ -1,4 +1,4 @@ -/* +/* * SPDX-FileCopyrightText: 2023 microG Project Team * SPDX-License-Identifier: Apache-2.0 */ @@ -115,8 +115,13 @@ class GamesService : BaseService(TAG, GmsService.GAMES) { return@launchWhenStarted sendSignInRequired(account) } - if (!performGamesSignIn(this@GamesService, packageName, account, scopes = scopes)) { - Log.d(TAG, "performGamesSignIn fail, sign in required") + try { + if (!performGamesSignIn(this@GamesService, packageName, account, scopes = scopes)) { + Log.d(TAG, "performGamesSignIn fail, sign in required") + return@launchWhenStarted sendSignInRequired(account) + } + } catch (e: Exception) { + Log.w(TAG, "performGamesSignIn exception, sign in required", e) return@launchWhenStarted sendSignInRequired(account) } @@ -793,4 +798,4 @@ class GamesServiceImpl(val context: Context, override val lifecycle: Lifecycle, override fun onTransact(code: Int, data: Parcel, reply: Parcel?, flags: Int): Boolean = warnOnTransactionIssues(code, reply, flags, TAG) { super.onTransact(code, data, reply, flags) } -} \ No newline at end of file +} diff --git a/play-services-core/src/main/kotlin/org/microg/gms/games/GamesSignInActivity.kt b/play-services-core/src/main/kotlin/org/microg/gms/games/GamesSignInActivity.kt index 2e616d2d10..e116c6afc8 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/games/GamesSignInActivity.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/games/GamesSignInActivity.kt @@ -1,4 +1,4 @@ -/* +/* * SPDX-FileCopyrightText: 2023 microG Project Team * SPDX-License-Identifier: Apache-2.0 */ @@ -78,7 +78,12 @@ class GamesSignInActivity : AppCompatActivity() { ?.getParcelable("googleSignInAccount")?.account if (account != null) { lifecycleScope.launchWhenStarted { - signIn(account) + try { + signIn(account) + } catch (e: Exception) { + Log.w(TAG, "signIn failed", e) + finish() + } } return } @@ -86,4 +91,4 @@ class GamesSignInActivity : AppCompatActivity() { finish() } } -} \ No newline at end of file +} diff --git a/play-services-core/src/main/kotlin/org/microg/gms/games/extensions.kt b/play-services-core/src/main/kotlin/org/microg/gms/games/extensions.kt index 215328957c..510126ae45 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/games/extensions.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/games/extensions.kt @@ -1,4 +1,4 @@ -/* +/* * SPDX-FileCopyrightText: 2023 microG Project Team * SPDX-License-Identifier: Apache-2.0 */ @@ -51,6 +51,7 @@ import org.microg.gms.utils.singleInstanceOf import kotlin.coroutines.resume import kotlin.coroutines.resumeWithException import kotlin.coroutines.suspendCoroutine +import kotlinx.coroutines.suspendCancellableCoroutine const val SERVICE_GAMES_LITE = "oauth2:https://www.googleapis.com/auth/games_lite" @@ -308,13 +309,21 @@ suspend fun performGamesSignIn( fetchSelfPlayer(context, authResponse.auth, queue) } catch (e : Exception){ requestGameToken(context, account, scopes, authManager.isPermitted)?.let { - fetchSelfPlayer(context, it, queue) + try { + fetchSelfPlayer(context, it, queue) + } catch (e: Exception) { + return false + } } ?: return false } } 403 -> { requestGameToken(context, account, scopes, authManager.isPermitted)?.let { - fetchSelfPlayer(context, it, queue) + try { + fetchSelfPlayer(context, it, queue) + } catch (e: Exception) { + return false + } } ?: return false } else -> throw e @@ -336,19 +345,19 @@ suspend fun fetchSelfPlayer( context: Context, authToken: String, queue: RequestQueue = singleInstanceOf { Volley.newRequestQueue(context.applicationContext) } -) = suspendCoroutine { continuation -> - queue.add( - object : JsonObjectRequest( - "https://www.googleapis.com/games/v1/players/me", - { continuation.resume(it) }, - { continuation.resumeWithException(it) }) { - override fun getHeaders(): MutableMap { - return mutableMapOf( - "Authorization" to "OAuth $authToken" - ) - } +) = suspendCancellableCoroutine { continuation -> + val request = object : JsonObjectRequest( + "https://www.googleapis.com/games/v1/players/me", + { if (continuation.isActive) continuation.resume(it) }, + { if (continuation.isActive) continuation.resumeWithException(it) }) { + override fun getHeaders(): MutableMap { + return mutableMapOf( + "Authorization" to "OAuth $authToken" + ) } - ) + } + continuation.invokeOnCancellation { request.cancel() } + queue.add(request) } suspend fun requestGameToken(