Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.chris.signintest;
- import com.chris.gametesting2.R;
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import android.app.AlertDialog;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- 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.Games;
- import com.google.android.gms.games.LeaderboardsClient;
- import com.google.android.gms.games.Player;
- import com.google.android.gms.games.PlayersClient;
- import com.google.android.gms.tasks.OnCompleteListener;
- import com.google.android.gms.tasks.OnFailureListener;
- import com.google.android.gms.tasks.OnSuccessListener;
- import com.google.android.gms.tasks.Task;
- public class MainActivity extends AppCompatActivity {
- // Client used to sign in with Google APIs
- private GoogleSignInClient mGoogleSignInClient;
- private LeaderboardsClient mLeaderboardsClient;
- private PlayersClient mPlayersClient;
- private Button signInButton;
- // request codes we use when invoking an external activity
- private static final int RC_UNUSED = 5001;
- private static final int RC_SIGN_IN = 9001;
- // tag for debug logging
- private static final String TAG = "TanC";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- signInButton = findViewById(R.id.button);
- // Create the client used to sign in to Google services.
- mGoogleSignInClient = GoogleSignIn.getClient(this,
- new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).build());
- signInButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- onSignInButtonClicked();
- }
- });
- }
- private boolean isSignedIn() {
- return GoogleSignIn.getLastSignedInAccount(this) != null;
- }
- private void signInSilently() {
- Log.d(TAG, "signInSilently()");
- mGoogleSignInClient.silentSignIn().addOnCompleteListener(this,
- new OnCompleteListener<GoogleSignInAccount>() {
- @Override
- public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
- if (task.isSuccessful()) {
- Log.d(TAG, "signInSilently(): success");
- onConnected(task.getResult());
- } else {
- Log.d(TAG, "signInSilently(): failure", task.getException());
- onDisconnected();
- }
- }
- });
- }
- private void startSignInIntent() {
- startActivityForResult(mGoogleSignInClient.getSignInIntent(), RC_SIGN_IN);
- }
- @Override
- protected void onResume() {
- super.onResume();
- Log.d(TAG, "onResume()");
- // Since the state of the signed in user can change when the activity is not active
- // it is recommended to try and sign in silently from when the app resumes.
- signInSilently();
- }
- private void signOut() {
- Log.d(TAG, "signOut()");
- if (!isSignedIn()) {
- Log.w(TAG, "signOut() called, but was not signed in!");
- return;
- }
- mGoogleSignInClient.signOut().addOnCompleteListener(this,
- new OnCompleteListener<Void>() {
- @Override
- public void onComplete(@NonNull Task<Void> task) {
- boolean successful = task.isSuccessful();
- Log.d(TAG, "signOut(): " + (successful ? "success" : "failed"));
- onDisconnected();
- }
- });
- }
- public void onShowLeaderboardsRequested() {
- mLeaderboardsClient.getAllLeaderboardsIntent()
- .addOnSuccessListener(new OnSuccessListener<Intent>() {
- @Override
- public void onSuccess(Intent intent) {
- startActivityForResult(intent, RC_UNUSED);
- }
- })
- .addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(@NonNull Exception e) {
- }
- });
- }
- /**
- * Start gameplay. This means updating some status variables and switching
- * to the "gameplay" screen (the screen where the user types the score they want).
- *
- * @param hardMode whether to start gameplay in "hard mode".
- */
- /*@Override
- public void onEnteredScore(int requestedScore) {
- // Compute final score (in easy mode, it's the requested score; in hard mode, it's half)
- int finalScore = mHardMode ? requestedScore / 2 : requestedScore;
- mWinFragment.setScore(finalScore);
- mWinFragment.setExplanation(mHardMode ? getString(R.string.hard_mode_explanation) :
- getString(R.string.easy_mode_explanation));
- // check for achievements
- checkForAchievements(requestedScore, finalScore);
- // update leaderboards
- updateLeaderboards(finalScore);
- // push those accomplishments to the cloud, if signed in
- pushAccomplishments();
- // switch to the exciting "you won" screen
- switchToFragment(mWinFragment);
- mEventsClient.increment(getString(R.string.event_number_chosen), 1);
- }*/
- /**
- * Update leaderboards with the user's score.
- *
- * //@param finalScore The score the user got.
- */
- /*private void updateLeaderboards(int finalScore) {
- if (mHardMode && mOutbox.mHardModeScore < finalScore) {
- mOutbox.mHardModeScore = finalScore;
- } else if (!mHardMode && mOutbox.mEasyModeScore < finalScore) {
- mOutbox.mEasyModeScore = finalScore;
- }
- }*/
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
- super.onActivityResult(requestCode, resultCode, intent);
- if (requestCode == RC_SIGN_IN) {
- Task<GoogleSignInAccount> task =
- GoogleSignIn.getSignedInAccountFromIntent(intent);
- try {
- GoogleSignInAccount account = task.getResult(ApiException.class);
- onConnected(account);
- } catch (ApiException apiException) {
- String message = apiException.getMessage();
- if (message == null || message.isEmpty()) {
- message = "kek";
- }
- onDisconnected();
- new AlertDialog.Builder(this)
- .setMessage(message)
- .setNeutralButton(android.R.string.ok, null)
- .show();
- }
- }
- }
- private void onConnected(GoogleSignInAccount googleSignInAccount) {
- Log.d(TAG, "onConnected(): connected to Google APIs");
- mLeaderboardsClient = Games.getLeaderboardsClient(this, googleSignInAccount);
- mPlayersClient = Games.getPlayersClient(this, googleSignInAccount);
- // Set the greeting appropriately on main menu
- mPlayersClient.getCurrentPlayer()
- .addOnCompleteListener(new OnCompleteListener<Player>() {
- @Override
- public void onComplete(@NonNull Task<Player> task) {
- String displayName;
- if (task.isSuccessful()) {
- displayName = task.getResult().getDisplayName();
- } else {
- Exception e = task.getException();
- displayName = "???";
- }
- }
- });
- }
- private void onDisconnected() {
- Log.d(TAG, "onDisconnected()");
- mLeaderboardsClient = null;
- mPlayersClient = null;
- // Show sign-in button on main menu
- // Show sign-in button on win screen
- }
- public void onSignInButtonClicked() {
- startSignInIntent();
- }
- public void onSignOutButtonClicked() {
- signOut();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement