Add try-catch to handle ApiException on Play Games
This commit is contained in:
parent
7a2e7cd963
commit
63ba97f0af
1 changed files with 22 additions and 6 deletions
|
@ -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 {
|
||||
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)
|
||||
return try {
|
||||
val client = GoogleSignIn.getClient(context, signInOptions)
|
||||
account = lastAccount ?: Tasks.await(client.silentSignIn())
|
||||
return account != null
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue