From 63ba97f0af938e6fc4ed2e67ed7dc78dae5b2b28 Mon Sep 17 00:00:00 2001 From: Lucas Lima Date: Thu, 31 Dec 2020 20:13:27 -0300 Subject: [PATCH] Add try-catch to handle ApiException on Play Games --- .../dev/lucasnlm/external/PlayGamesManager.kt | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/proprietary/src/main/java/dev/lucasnlm/external/PlayGamesManager.kt b/proprietary/src/main/java/dev/lucasnlm/external/PlayGamesManager.kt index aaefefa7..6005f9ac 100644 --- a/proprietary/src/main/java/dev/lucasnlm/external/PlayGamesManager.kt +++ b/proprietary/src/main/java/dev/lucasnlm/external/PlayGamesManager.kt @@ -3,12 +3,14 @@ package dev.lucasnlm.external import android.app.Activity import android.content.Context import android.content.Intent +import android.util.Log import android.view.Gravity import android.widget.Toast import com.google.android.gms.auth.api.Auth import com.google.android.gms.auth.api.signin.GoogleSignIn import com.google.android.gms.auth.api.signin.GoogleSignInAccount import com.google.android.gms.auth.api.signin.GoogleSignInOptions +import com.google.android.gms.common.api.ApiException import com.google.android.gms.games.Games import com.google.android.gms.tasks.Tasks @@ -26,7 +28,13 @@ class PlayGamesManager( override fun playerId(): String? { return account?.let { - Tasks.await(Games.getPlayersClient(context, it).currentPlayerId) + try { + Tasks.await(Games.getPlayersClient(context, it).currentPlayerId) + } catch (e: ApiException) { + Log.e(TAG, "Fail to request current player id") + account = null + null + } } } @@ -43,14 +51,18 @@ class PlayGamesManager( override fun silentLogin(): Boolean { val signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).build() val lastAccount: GoogleSignInAccount? = GoogleSignIn.getLastSignedInAccount(context) - val client = GoogleSignIn.getClient(context, signInOptions) - account = lastAccount ?: Tasks.await(client.silentSignIn()) - return account != null + return try { + val client = GoogleSignIn.getClient(context, signInOptions) + account = lastAccount ?: Tasks.await(client.silentSignIn()) + account != null + } catch (e: ApiException) { + account = null + false + } } override fun getLoginIntent(): Intent? { - val signInClient = GoogleSignIn.getClient(context, GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN) - return signInClient.signInIntent + return GoogleSignIn.getClient(context, GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).signInIntent } override fun handleLoginResult(data: Intent?) { @@ -108,4 +120,8 @@ class PlayGamesManager( Games.getLeaderboardsClient(context, it).submitScore(leaderboard.value, value) } } + + companion object { + val TAG = PlayGamesManager::class.simpleName + } }