Advertisement
Guest User

Untitled

a guest
Aug 20th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.71 KB | None | 0 0
  1. package com.chris.signintest;
  2. import com.chris.gametesting2.R;
  3.  
  4. import androidx.annotation.NonNull;
  5. import androidx.appcompat.app.AppCompatActivity;
  6.  
  7. import android.app.AlertDialog;
  8. import android.content.Intent;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.Toast;
  14.  
  15. import com.google.android.gms.auth.api.signin.GoogleSignIn;
  16. import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
  17. import com.google.android.gms.auth.api.signin.GoogleSignInClient;
  18. import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
  19. import com.google.android.gms.common.api.ApiException;
  20. import com.google.android.gms.games.Games;
  21. import com.google.android.gms.games.LeaderboardsClient;
  22. import com.google.android.gms.games.Player;
  23. import com.google.android.gms.games.PlayersClient;
  24. import com.google.android.gms.tasks.OnCompleteListener;
  25. import com.google.android.gms.tasks.OnFailureListener;
  26. import com.google.android.gms.tasks.OnSuccessListener;
  27. import com.google.android.gms.tasks.Task;
  28.  
  29. public class MainActivity extends AppCompatActivity {
  30.  
  31.     // Client used to sign in with Google APIs
  32.     private GoogleSignInClient mGoogleSignInClient;
  33.  
  34.     private LeaderboardsClient mLeaderboardsClient;
  35.     private PlayersClient mPlayersClient;
  36.     private Button signInButton;
  37.  
  38.     // request codes we use when invoking an external activity
  39.     private static final int RC_UNUSED = 5001;
  40.     private static final int RC_SIGN_IN = 9001;
  41.  
  42.     // tag for debug logging
  43.     private static final String TAG = "TanC";
  44.  
  45.     @Override
  46.     protected void onCreate(Bundle savedInstanceState) {
  47.         super.onCreate(savedInstanceState);
  48.         setContentView(R.layout.activity_main);
  49.  
  50.         signInButton = findViewById(R.id.button);
  51.  
  52.         // Create the client used to sign in to Google services.
  53.         mGoogleSignInClient = GoogleSignIn.getClient(this,
  54.                 new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).build());
  55.  
  56.  
  57.         signInButton.setOnClickListener(new View.OnClickListener() {
  58.             @Override
  59.             public void onClick(View view) {
  60.                 onSignInButtonClicked();
  61.             }
  62.         });
  63.  
  64.     }
  65.  
  66.     private boolean isSignedIn() {
  67.         return GoogleSignIn.getLastSignedInAccount(this) != null;
  68.     }
  69.  
  70.     private void signInSilently() {
  71.         Log.d(TAG, "signInSilently()");
  72.  
  73.         mGoogleSignInClient.silentSignIn().addOnCompleteListener(this,
  74.                 new OnCompleteListener<GoogleSignInAccount>() {
  75.                     @Override
  76.                     public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
  77.                         if (task.isSuccessful()) {
  78.                             Log.d(TAG, "signInSilently(): success");
  79.                             onConnected(task.getResult());
  80.                         } else {
  81.                             Log.d(TAG, "signInSilently(): failure", task.getException());
  82.                             onDisconnected();
  83.                         }
  84.                     }
  85.                 });
  86.     }
  87.  
  88.     private void startSignInIntent() {
  89.         startActivityForResult(mGoogleSignInClient.getSignInIntent(), RC_SIGN_IN);
  90.     }
  91.  
  92.     @Override
  93.     protected void onResume() {
  94.         super.onResume();
  95.         Log.d(TAG, "onResume()");
  96.  
  97.         // Since the state of the signed in user can change when the activity is not active
  98.         // it is recommended to try and sign in silently from when the app resumes.
  99.         signInSilently();
  100.     }
  101.  
  102.     private void signOut() {
  103.         Log.d(TAG, "signOut()");
  104.  
  105.         if (!isSignedIn()) {
  106.             Log.w(TAG, "signOut() called, but was not signed in!");
  107.             return;
  108.         }
  109.  
  110.         mGoogleSignInClient.signOut().addOnCompleteListener(this,
  111.                 new OnCompleteListener<Void>() {
  112.                     @Override
  113.                     public void onComplete(@NonNull Task<Void> task) {
  114.                         boolean successful = task.isSuccessful();
  115.                         Log.d(TAG, "signOut(): " + (successful ? "success" : "failed"));
  116.  
  117.                         onDisconnected();
  118.                     }
  119.                 });
  120.     }
  121.  
  122.     public void onShowLeaderboardsRequested() {
  123.         mLeaderboardsClient.getAllLeaderboardsIntent()
  124.                 .addOnSuccessListener(new OnSuccessListener<Intent>() {
  125.                     @Override
  126.                     public void onSuccess(Intent intent) {
  127.                         startActivityForResult(intent, RC_UNUSED);
  128.                     }
  129.                 })
  130.                 .addOnFailureListener(new OnFailureListener() {
  131.                     @Override
  132.                     public void onFailure(@NonNull Exception e) {
  133.  
  134.                     }
  135.                 });
  136.     }
  137.  
  138.     /**
  139.      * Start gameplay. This means updating some status variables and switching
  140.      * to the "gameplay" screen (the screen where the user types the score they want).
  141.      *
  142.      * @param hardMode whether to start gameplay in "hard mode".
  143.      */
  144.  
  145.     /*@Override
  146.     public void onEnteredScore(int requestedScore) {
  147.         // Compute final score (in easy mode, it's the requested score; in hard mode, it's half)
  148.         int finalScore = mHardMode ? requestedScore / 2 : requestedScore;
  149.  
  150.         mWinFragment.setScore(finalScore);
  151.         mWinFragment.setExplanation(mHardMode ? getString(R.string.hard_mode_explanation) :
  152.                 getString(R.string.easy_mode_explanation));
  153.  
  154.         // check for achievements
  155.         checkForAchievements(requestedScore, finalScore);
  156.  
  157.         // update leaderboards
  158.         updateLeaderboards(finalScore);
  159.  
  160.         // push those accomplishments to the cloud, if signed in
  161.         pushAccomplishments();
  162.  
  163.         // switch to the exciting "you won" screen
  164.         switchToFragment(mWinFragment);
  165.  
  166.         mEventsClient.increment(getString(R.string.event_number_chosen), 1);
  167.     }*/
  168.  
  169.     /**
  170.      * Update leaderboards with the user's score.
  171.      *
  172.      * //@param finalScore The score the user got.
  173.      */
  174.     /*private void updateLeaderboards(int finalScore) {
  175.         if (mHardMode && mOutbox.mHardModeScore < finalScore) {
  176.             mOutbox.mHardModeScore = finalScore;
  177.         } else if (!mHardMode && mOutbox.mEasyModeScore < finalScore) {
  178.             mOutbox.mEasyModeScore = finalScore;
  179.         }
  180.     }*/
  181.  
  182.  
  183.     @Override
  184.     protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  185.         super.onActivityResult(requestCode, resultCode, intent);
  186.         if (requestCode == RC_SIGN_IN) {
  187.             Task<GoogleSignInAccount> task =
  188.                     GoogleSignIn.getSignedInAccountFromIntent(intent);
  189.  
  190.             try {
  191.                 GoogleSignInAccount account = task.getResult(ApiException.class);
  192.                 onConnected(account);
  193.             } catch (ApiException apiException) {
  194.                 String message = apiException.getMessage();
  195.                 if (message == null || message.isEmpty()) {
  196.                     message = "kek";
  197.                 }
  198.  
  199.                 onDisconnected();
  200.  
  201.                 new AlertDialog.Builder(this)
  202.                         .setMessage(message)
  203.                         .setNeutralButton(android.R.string.ok, null)
  204.                         .show();
  205.             }
  206.         }
  207.     }
  208.  
  209.     private void onConnected(GoogleSignInAccount googleSignInAccount) {
  210.         Log.d(TAG, "onConnected(): connected to Google APIs");
  211.  
  212.  
  213.         mLeaderboardsClient = Games.getLeaderboardsClient(this, googleSignInAccount);
  214.  
  215.         mPlayersClient = Games.getPlayersClient(this, googleSignInAccount);
  216.  
  217.  
  218.         // Set the greeting appropriately on main menu
  219.         mPlayersClient.getCurrentPlayer()
  220.                 .addOnCompleteListener(new OnCompleteListener<Player>() {
  221.                     @Override
  222.                     public void onComplete(@NonNull Task<Player> task) {
  223.                         String displayName;
  224.                         if (task.isSuccessful()) {
  225.                             displayName = task.getResult().getDisplayName();
  226.                         } else {
  227.                             Exception e = task.getException();
  228.                             displayName = "???";
  229.                         }
  230.                     }
  231.                 });
  232.     }
  233.  
  234.     private void onDisconnected() {
  235.         Log.d(TAG, "onDisconnected()");
  236.  
  237.         mLeaderboardsClient = null;
  238.         mPlayersClient = null;
  239.  
  240.         // Show sign-in button on main menu
  241.  
  242.  
  243.         // Show sign-in button on win screen
  244.  
  245.  
  246.  
  247.     }
  248.  
  249.  
  250.     public void onSignInButtonClicked() {
  251.         startSignInIntent();
  252.     }
  253.  
  254.  
  255.     public void onSignOutButtonClicked() {
  256.         signOut();
  257.     }
  258.  
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement