content: package alessandro.firebaseandroid.view;
  
  import android.content.Intent;
  import android.support.annotation.NonNull;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  
  import com.google.android.gms.auth.api.Auth;
  import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
  import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
  import com.google.android.gms.auth.api.signin.GoogleSignInResult;
  import com.google.android.gms.common.ConnectionResult;
  import com.google.android.gms.common.SignInButton;
  import com.google.android.gms.common.api.GoogleApiClient;
  import com.google.android.gms.tasks.OnCompleteListener;
  import com.google.android.gms.tasks.Task;
  import com.google.firebase.auth.AuthCredential;
  import com.google.firebase.auth.AuthResult;
  import com.google.firebase.auth.FirebaseAuth;
  import com.google.firebase.auth.GoogleAuthProvider;
  
  import alessandro.firebaseandroid.MainActivity;
  import alessandro.firebaseandroid.R;
  import alessandro.firebaseandroid.util.Util;
  
  public class LoginActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {
  
      //Constants
      private static final String TAG = LoginActivity.class.getSimpleName();
      private static final int RC_SIGN_IN = 9001;
  
      //UI
      private SignInButton mSignInButton;
  
      //Firebase and GoogleApiClient
      private GoogleApiClient mGoogleApiClient;
      private FirebaseAuth mFirebaseAuth;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_login);
  
          if (!Util.verificaConexao(this)){
              Util.initToast(this,"Você não tem conexão com internet");
              finish();
          }
  
          mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
          mSignInButton.setOnClickListener(this);
  
          GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                  .requestIdToken(getString(R.string.default_web_client_id))
                  .requestEmail()
                  .build();
          mGoogleApiClient = new GoogleApiClient.Builder(this)
                  .enableAutoManage(this,this)
                  .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                  .build();
  
          // Initialize FirebaseAuth
          mFirebaseAuth = FirebaseAuth.getInstance();
  
      }
  
      @Override
      public 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()) {
                  GoogleSignInAccount account = result.getSignInAccount();
                  firebaseAuthWithGoogle(account);
              } else {
                  Log.e(TAG, "Google Sign In failed.");
              }
          }
      }
  
      @Override
      public void onClick(View v) {
          switch (v.getId()) {
              case R.id.sign_in_button:
                  signIn();
                  break;
              default:
                  return;
          }
      }
  
      @Override
      public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
          Log.d(TAG, "onConnectionFailed:" + connectionResult);
          Util.initToast(this,"Google Play Services error.");
      }
  
      private void signIn() {
          Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
          startActivityForResult(signInIntent, RC_SIGN_IN);
      }
  
      private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
          Log.d(TAG, "firebaseAuthWithGooogle:" + acct.getId());
          AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
          mFirebaseAuth.signInWithCredential(credential)
                  .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                      @Override
                      public void onComplete(@NonNull Task<AuthResult> task) {
                          Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
                          if (!task.isSuccessful()) {
                              Log.w(TAG, "signInWithCredential", task.getException());
                              Util.initToast(LoginActivity.this,"Authentication failed");
                          } else {
                              startActivity(new Intent(LoginActivity.this, MainActivity.class));
                              finish();
                          }
                      }
                  });
      }
  
  }
  