Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. package com.spyrosk.alchemy.symbols.activities
  2.  
  3. import android.app.AlertDialog
  4. import android.content.Intent
  5. import android.os.Bundle
  6. import android.support.v4.content.ContextCompat
  7. import android.support.v7.app.AppCompatActivity
  8. import com.google.android.gms.auth.api.signin.GoogleSignIn
  9. import com.google.android.gms.auth.api.signin.GoogleSignInAccount
  10. import com.google.android.gms.auth.api.signin.GoogleSignInClient
  11. import com.google.android.gms.auth.api.signin.GoogleSignInOptions
  12. import com.google.android.gms.common.api.ApiException
  13. import com.google.android.gms.games.AchievementsClient
  14. import com.google.android.gms.games.EventsClient
  15. import com.google.android.gms.games.Games
  16. import com.google.android.gms.games.PlayersClient
  17. import com.spyrosk.alchemy.symbols.R
  18. import com.spyrosk.alchemy.symbols.utils.savesDatabaseHelper
  19. import kotlinx.android.synthetic.main.activity_start.*
  20.  
  21. class activityStart : AppCompatActivity() {
  22.  
  23.  
  24. private var mGoogleSignInClient: GoogleSignInClient? = null
  25. private var mAchievementsClient: AchievementsClient? = null
  26. var mSignedInAccount: GoogleSignInAccount? = null
  27. var mEventsClient: EventsClient? = null
  28. private var mPlayersClient: PlayersClient? = null
  29.  
  30. private val RC_UNUSED = 5001
  31. private val RC_SIGN_IN = 9001
  32. val RC_ACHIEVEMENT_UI = 9003
  33.  
  34. override fun onCreate(savedInstanceState: Bundle?) {
  35. super.onCreate(savedInstanceState)
  36. setContentView(R.layout.activity_start)
  37.  
  38. mGoogleSignInClient = GoogleSignIn.getClient(this,
  39. GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
  40. .requestIdToken(getString(R.string.clientID))
  41. .requestEmail()
  42. .build());
  43.  
  44. if(!isSignedIn()) {
  45. startActivityForResult(mGoogleSignInClient?.signInIntent, RC_SIGN_IN)
  46. } else {
  47. signInSilently()
  48. }
  49.  
  50. buttonAchievements.setOnClickListener {
  51. mAchievementsClient?.achievementsIntent
  52. ?.addOnSuccessListener { intent -> startActivityForResult(intent, RC_UNUSED) }
  53. ?.addOnFailureListener { e -> handleException(e, getString(R.string.achievements_exception)) }
  54. }
  55.  
  56. buttonPlay.setOnClickListener {
  57. val intent = Intent(this@activityStart, activityLevels::class.java)
  58. startActivity(intent)
  59. }
  60.  
  61. buttonHelp.setOnClickListener {
  62. val intent = Intent(this@activityStart, activityHelp::class.java)
  63. ContextCompat.startActivity(this@activityStart, intent, null)
  64. }
  65.  
  66. buttonAchievements.setOnClickListener {
  67.  
  68. }
  69. }
  70.  
  71. override fun onResume() {
  72. super.onResume()
  73. signInSilently()
  74. }
  75.  
  76. fun startSignInIntent() {
  77. startActivityForResult(mGoogleSignInClient?.signInIntent, RC_SIGN_IN)
  78. }
  79.  
  80. private fun signOut(){
  81. if (!isSignedIn()) {
  82. return
  83. }
  84. mGoogleSignInClient?.signOut()?.addOnCompleteListener(this
  85. ) { task ->
  86. val successful = task.isSuccessful
  87. onDisconnected()
  88. }
  89. }
  90.  
  91. private fun isSignedIn(): Boolean {
  92. return GoogleSignIn.getLastSignedInAccount(this) != null
  93. }
  94.  
  95. private fun onConnected(googleSignInAccount: GoogleSignInAccount) {
  96. mAchievementsClient = Games.getAchievementsClient(this, googleSignInAccount)
  97. ///textViewState.text = "CONNECTED"
  98. }
  99.  
  100. private fun onDisconnected() {
  101. mAchievementsClient = null
  102. mPlayersClient = null
  103. /// textViewState.text = "DISCONECT"
  104. }
  105.  
  106. private fun signInSilently() {
  107. mGoogleSignInClient?.silentSignIn()?.addOnCompleteListener(this
  108. ) { task ->
  109. if (task.isSuccessful) {
  110. /// textViewState.text = "CONNECTED"
  111. onConnected(task.result)
  112. } else {
  113. /// textViewState.text = "FAILED"
  114. onDisconnected()
  115. }
  116. }
  117. }
  118.  
  119. private fun handleException(e: Exception, details: String) {
  120. var status = 0
  121.  
  122. if (e is ApiException) {
  123. status = e.statusCode
  124. }
  125.  
  126. val message = getString(R.string.status_exception_error, details, status, e)
  127. AlertDialog.Builder(this@activityStart)
  128. .setMessage(message)
  129. .setNeutralButton(android.R.string.ok, null)
  130. .show()
  131. }
  132.  
  133. override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent) {
  134. super.onActivityResult(requestCode, resultCode, intent)
  135. if (requestCode == RC_SIGN_IN) {
  136. val task = GoogleSignIn.getSignedInAccountFromIntent(intent)
  137.  
  138. try {
  139. val account = task.getResult(ApiException::class.java)
  140. onConnected(account)
  141. } catch (apiException: ApiException) {
  142. var message: String? = apiException.message
  143. if (message == null || message.isEmpty()) {
  144. message = getString(R.string.signin_other_error)
  145. }
  146.  
  147. onDisconnected()
  148.  
  149. AlertDialog.Builder(this)
  150. .setMessage(message)
  151. .setNeutralButton(android.R.string.ok, null)
  152. .show()
  153. }
  154.  
  155. }
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement