Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.spyrosk.alchemy.symbols.activities
- import android.app.AlertDialog
- import android.content.Intent
- import android.os.Bundle
- import android.support.v4.content.ContextCompat
- import android.support.v7.app.AppCompatActivity
- 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.GoogleSignInClient
- import com.google.android.gms.auth.api.signin.GoogleSignInOptions
- import com.google.android.gms.common.api.ApiException
- import com.google.android.gms.games.AchievementsClient
- import com.google.android.gms.games.EventsClient
- import com.google.android.gms.games.Games
- import com.google.android.gms.games.PlayersClient
- import com.spyrosk.alchemy.symbols.R
- import com.spyrosk.alchemy.symbols.utils.savesDatabaseHelper
- import kotlinx.android.synthetic.main.activity_start.*
- class activityStart : AppCompatActivity() {
- private var mGoogleSignInClient: GoogleSignInClient? = null
- private var mAchievementsClient: AchievementsClient? = null
- var mSignedInAccount: GoogleSignInAccount? = null
- var mEventsClient: EventsClient? = null
- private var mPlayersClient: PlayersClient? = null
- private val RC_UNUSED = 5001
- private val RC_SIGN_IN = 9001
- val RC_ACHIEVEMENT_UI = 9003
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_start)
- mGoogleSignInClient = GoogleSignIn.getClient(this,
- GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
- .requestIdToken(getString(R.string.clientID))
- .requestEmail()
- .build());
- if(!isSignedIn()) {
- startActivityForResult(mGoogleSignInClient?.signInIntent, RC_SIGN_IN)
- } else {
- signInSilently()
- }
- buttonAchievements.setOnClickListener {
- mAchievementsClient?.achievementsIntent
- ?.addOnSuccessListener { intent -> startActivityForResult(intent, RC_UNUSED) }
- ?.addOnFailureListener { e -> handleException(e, getString(R.string.achievements_exception)) }
- }
- buttonPlay.setOnClickListener {
- val intent = Intent(this@activityStart, activityLevels::class.java)
- startActivity(intent)
- }
- buttonHelp.setOnClickListener {
- val intent = Intent(this@activityStart, activityHelp::class.java)
- ContextCompat.startActivity(this@activityStart, intent, null)
- }
- buttonAchievements.setOnClickListener {
- }
- }
- override fun onResume() {
- super.onResume()
- signInSilently()
- }
- fun startSignInIntent() {
- startActivityForResult(mGoogleSignInClient?.signInIntent, RC_SIGN_IN)
- }
- private fun signOut(){
- if (!isSignedIn()) {
- return
- }
- mGoogleSignInClient?.signOut()?.addOnCompleteListener(this
- ) { task ->
- val successful = task.isSuccessful
- onDisconnected()
- }
- }
- private fun isSignedIn(): Boolean {
- return GoogleSignIn.getLastSignedInAccount(this) != null
- }
- private fun onConnected(googleSignInAccount: GoogleSignInAccount) {
- mAchievementsClient = Games.getAchievementsClient(this, googleSignInAccount)
- ///textViewState.text = "CONNECTED"
- }
- private fun onDisconnected() {
- mAchievementsClient = null
- mPlayersClient = null
- /// textViewState.text = "DISCONECT"
- }
- private fun signInSilently() {
- mGoogleSignInClient?.silentSignIn()?.addOnCompleteListener(this
- ) { task ->
- if (task.isSuccessful) {
- /// textViewState.text = "CONNECTED"
- onConnected(task.result)
- } else {
- /// textViewState.text = "FAILED"
- onDisconnected()
- }
- }
- }
- private fun handleException(e: Exception, details: String) {
- var status = 0
- if (e is ApiException) {
- status = e.statusCode
- }
- val message = getString(R.string.status_exception_error, details, status, e)
- AlertDialog.Builder(this@activityStart)
- .setMessage(message)
- .setNeutralButton(android.R.string.ok, null)
- .show()
- }
- override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent) {
- super.onActivityResult(requestCode, resultCode, intent)
- if (requestCode == RC_SIGN_IN) {
- val task = GoogleSignIn.getSignedInAccountFromIntent(intent)
- try {
- val account = task.getResult(ApiException::class.java)
- onConnected(account)
- } catch (apiException: ApiException) {
- var message: String? = apiException.message
- if (message == null || message.isEmpty()) {
- message = getString(R.string.signin_other_error)
- }
- onDisconnected()
- AlertDialog.Builder(this)
- .setMessage(message)
- .setNeutralButton(android.R.string.ok, null)
- .show()
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement