Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Games: Unable to generate a game account for the first time #2656

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.accounts.Account
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.util.Log
import androidx.core.content.contentValuesOf
import androidx.core.net.toUri
import com.android.volley.*
Expand Down Expand Up @@ -153,7 +154,7 @@ fun JSONObject.toPlayer() = PlayerEntity(
null
)

suspend fun registerForGames(context: Context, account: Account, queue: RequestQueue = singleInstanceOf { Volley.newRequestQueue(context.applicationContext) }) {
suspend fun registerForGames(context: Context, account: Account, queue: RequestQueue = singleInstanceOf { Volley.newRequestQueue(context.applicationContext) }): JSONObject {
val authManager = AuthManager(context, account.name, Constants.GMS_PACKAGE_NAME, "oauth2:${Scopes.GAMES_FIRSTPARTY}")
authManager.setOauth2Foreground("1")
val authToken = withContext(Dispatchers.IO) { authManager.requestAuthWithBackgroundResolution(false).auth }
Expand All @@ -173,7 +174,7 @@ suspend fun registerForGames(context: Context, account: Account, queue: RequestQ
}
)
}
suspendCoroutine<JSONObject> { continuation ->
return suspendCoroutine { continuation ->
queue.add(
object : JsonObjectRequest(
Method.PUT,
Expand Down Expand Up @@ -219,39 +220,47 @@ suspend fun performGamesSignIn(
var authResponse = withContext(Dispatchers.IO) { authManager.requestAuthWithBackgroundResolution(true) }
if (authResponse.auth == null) return false
if (authResponse.issueAdvice != "stored" || GamesConfigurationService.getPlayer(context, account) == null) {
suspend fun fetchSelfPlayer() = suspendCoroutine<JSONObject> { continuation ->
queue.add(
object : JsonObjectRequest(
"https://www.googleapis.com/games/v1/players/me",
{ continuation.resume(it) },
{ continuation.resumeWithException(it) }) {
override fun getHeaders(): MutableMap<String, String> {
return mutableMapOf(
"Authorization" to "OAuth ${authResponse.auth}"
)
suspend fun fetchSelfPlayer(register: Boolean = false): JSONObject {
if (register) {
registerForGames(context, account, queue)
}
return suspendCoroutine { continuation ->
queue.add(
object : JsonObjectRequest(
"https://www.googleapis.com/games/v1/players/me",
{ continuation.resume(it) },
{ continuation.resumeWithException(it) }) {
override fun getHeaders(): MutableMap<String, String> {
return mutableMapOf(
"Authorization" to "OAuth ${authResponse.auth}"
)
}
}
}
)
)
}
}

val result = try {
fetchSelfPlayer()
} catch (e: Exception) {
if (e is VolleyError) {
val statusCode = e.networkResponse?.statusCode
when (statusCode) {
404 -> {
registerForGames(context, account, queue)
fetchSelfPlayer()
}
403 -> {
val gameAuthManager = AuthManager(context, account.name, GAMES_PACKAGE_NAME, authManager.service)
gameAuthManager.isPermitted = authManager.isPermitted
authResponse = withContext(Dispatchers.IO) { gameAuthManager.requestAuth(true) }
if (authResponse.auth == null) return false
fetchSelfPlayer()
try {
val statusCode = e.networkResponse?.statusCode
when (statusCode) {
404 -> {
fetchSelfPlayer(true)
}
403 -> {
val gameAuthManager = AuthManager(context, account.name, GAMES_PACKAGE_NAME, authManager.service)
gameAuthManager.isPermitted = authManager.isPermitted
authResponse = withContext(Dispatchers.IO) { gameAuthManager.requestAuth(true) }
if (authResponse.auth == null) return false
fetchSelfPlayer(true)
}
else -> throw e
}
else -> throw e
} catch (e: Exception) {
throw e
}
} else {
throw e
Expand Down