Guest User

Untitled

a guest
May 26th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. private boolean isSignedIn() {
  2. return GoogleSignIn.getLastSignedInAccount(this) != null;
  3. }
  4.  
  5. private void signInSilently() {
  6. GoogleSignInClient signInClient = GoogleSignIn.getClient(this,
  7. GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
  8. signInClient.silentSignIn().addOnCompleteListener(this,
  9. new OnCompleteListener<GoogleSignInAccount>() {
  10. @Override
  11. public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
  12. if (task.isSuccessful()) {
  13. // The signed in account is stored in the task's result.
  14. GoogleSignInAccount signedInAccount = task.getResult();
  15. } else {
  16. // Player will need to sign-in explicitly using via UI
  17. }
  18. }
  19. });
  20. }
  21.  
  22. @Override
  23. protected void onResume() {
  24. super.onResume();
  25. signInSilently();
  26. }
  27.  
  28. private void startSignInIntent() {
  29. GoogleSignInClient signInClient = GoogleSignIn.getClient(this,
  30. GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
  31. Intent intent = signInClient.getSignInIntent();
  32. startActivityForResult(intent, RC_SIGN_IN);
  33. }
  34.  
  35. @Override
  36. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  37. super.onActivityResult(requestCode, resultCode, data);
  38. if (requestCode == RC_SIGN_IN) {
  39. GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
  40. if (result.isSuccess()) {
  41. // The signed in account is stored in the result.
  42. GoogleSignInAccount signedInAccount = result.getSignInAccount();
  43. } else {
  44. String message = result.getStatus().getStatusMessage();
  45. if (message == null || message.isEmpty()) {
  46. message = getString(R.string.signin_other_error);
  47. }
  48. new AlertDialog.Builder(this).setMessage(message)
  49. .setNeutralButton(android.R.string.ok, null).show();
  50. }
  51. }
  52. }
  53.  
  54. @Override
  55. protected void onCreate(Bundle savedInstanceState) {
  56. super.onCreate(savedInstanceState);
  57. setContentView(R.layout.activity_sign_in);
  58. findViewById(R.id.sign_in_button).setOnClickListener(this);
  59. findViewById(R.id.sign_out_button).setOnClickListener(this);
  60. }
  61.  
  62. @Override
  63. public void onClick(View view) {
  64. if (view.getId() == R.id.sign_in_button) {
  65. // start the asynchronous sign in flow
  66. startSignInIntent();
  67. } else if (view.getId() == R.id.sign_out_button) {
  68. // sign out.
  69. signOut();
  70. // show sign-in button, hide the sign-out button
  71. findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
  72. findViewById(R.id.sign_out_button).setVisibility(View.GONE);
  73. }
  74. }
  75.  
  76. private void signOut() {
  77. GoogleSignInClient signInClient = GoogleSignIn.getClient(this,
  78. GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
  79. signInClient.signOut().addOnCompleteListener(this,
  80. new OnCompleteListener<Void>() {
  81. @Override
  82. public void onComplete(@NonNull Task<Void> task) {
  83. // at this point, the user is signed out.
  84. }
  85. });
  86. }
Add Comment
Please, Sign In to add comment