Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private boolean isSignedIn() {
- return GoogleSignIn.getLastSignedInAccount(this) != null;
- }
- private void signInSilently() {
- GoogleSignInClient signInClient = GoogleSignIn.getClient(this,
- GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
- signInClient.silentSignIn().addOnCompleteListener(this,
- new OnCompleteListener<GoogleSignInAccount>() {
- @Override
- public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
- if (task.isSuccessful()) {
- // The signed in account is stored in the task's result.
- GoogleSignInAccount signedInAccount = task.getResult();
- } else {
- // Player will need to sign-in explicitly using via UI
- }
- }
- });
- }
- @Override
- protected void onResume() {
- super.onResume();
- signInSilently();
- }
- private void startSignInIntent() {
- GoogleSignInClient signInClient = GoogleSignIn.getClient(this,
- GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
- Intent intent = signInClient.getSignInIntent();
- startActivityForResult(intent, RC_SIGN_IN);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == RC_SIGN_IN) {
- GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
- if (result.isSuccess()) {
- // The signed in account is stored in the result.
- GoogleSignInAccount signedInAccount = result.getSignInAccount();
- } else {
- String message = result.getStatus().getStatusMessage();
- if (message == null || message.isEmpty()) {
- message = getString(R.string.signin_other_error);
- }
- new AlertDialog.Builder(this).setMessage(message)
- .setNeutralButton(android.R.string.ok, null).show();
- }
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_sign_in);
- findViewById(R.id.sign_in_button).setOnClickListener(this);
- findViewById(R.id.sign_out_button).setOnClickListener(this);
- }
- @Override
- public void onClick(View view) {
- if (view.getId() == R.id.sign_in_button) {
- // start the asynchronous sign in flow
- startSignInIntent();
- } else if (view.getId() == R.id.sign_out_button) {
- // sign out.
- signOut();
- // show sign-in button, hide the sign-out button
- findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
- findViewById(R.id.sign_out_button).setVisibility(View.GONE);
- }
- }
- private void signOut() {
- GoogleSignInClient signInClient = GoogleSignIn.getClient(this,
- GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
- signInClient.signOut().addOnCompleteListener(this,
- new OnCompleteListener<Void>() {
- @Override
- public void onComplete(@NonNull Task<Void> task) {
- // at this point, the user is signed out.
- }
- });
- }
Add Comment
Please, Sign In to add comment