content: package com.example.yohan.facebookfirebasesdk;
  
  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 android.widget.Button;
  import android.widget.Toast;
  
  import com.facebook.AccessToken;
  import com.facebook.CallbackManager;
  import com.facebook.FacebookCallback;
  import com.facebook.FacebookException;
  import com.facebook.login.LoginManager;
  import com.facebook.login.LoginResult;
  import com.facebook.login.widget.LoginButton;
  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.FacebookAuthProvider;
  import com.google.firebase.auth.FirebaseAuth;
  import com.google.firebase.auth.FirebaseUser;
  
  import java.util.Arrays;
  
  public class MainActivity extends AppCompatActivity {
  
      private static final String TAG = "FaceLog";
      private CallbackManager mCallbackManager;
      private FirebaseAuth mAuth;
      private Button btnFacebook;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // Initialize Firebase Auth
          mAuth = FirebaseAuth.getInstance();
          btnFacebook = findViewById(R.id.btnFBLogin);
  
          mCallbackManager = CallbackManager.Factory.create();
          //LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
         // loginButton.setReadPermissions("email", "public_profile");
          //these are the permission when we create account from  firebase . also we can get more permission from fb
          btnFacebook.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  btnFacebook.setEnabled(false);
                  LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("email", "public_profile"));
                  LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
                      @Override
                      public void onSuccess(LoginResult loginResult) {
                          Log.d(TAG, "facebook:onSuccess:" + loginResult);
                          handleFacebookAccessToken(loginResult.getAccessToken());
  
                      }
  
                      @Override
                      public void onCancel() {
                          Log.d(TAG, "facebook:onCancel");
                          // ...
                      }
  
                      @Override
                      public void onError(FacebookException error) {
                          Log.d(TAG, "facebook:onError", error);
  
                          // ...
                      }
                  });
  
              }
          });
        /*  loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
              @Override
              public void onSuccess(LoginResult loginResult) {
                  Log.d(TAG, "facebook:onSuccess:" + loginResult);
                  handleFacebookAccessToken(loginResult.getAccessToken());
              }
  
              @Override
              public void onCancel() {
                  Log.d(TAG, "facebook:onCancel");
                  // ...
              }
  
              @Override
              public void onError(FacebookException error) {
                  Log.d(TAG, "facebook:onError", error);
                  // ...
              }
          });*/
  
      }
  
      @Override
      public void onStart() {
          super.onStart();
          // Check if user is signed in (non-null) and update UI accordingly.
          FirebaseUser currentUser = mAuth.getCurrentUser();
  
          if(currentUser != null){
              updateUI(currentUser);
          }
  
      }
  
      private void updateUI(FirebaseUser currentUser) {
  
          Toast.makeText(MainActivity.this,"You are Logged in", Toast.LENGTH_LONG).show();
          Intent i = new Intent(MainActivity.this,profile.class);
          startActivity(i);
          finish();
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
          // Pass the activity result back to the Facebook SDK
          mCallbackManager.onActivityResult(requestCode, resultCode, data);
      }
  
      private void handleFacebookAccessToken(AccessToken token) {
          Log.d(TAG, "handleFacebookAccessToken:" + token);
  
          AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
          //sign with credential which create from firebase auth
          mAuth.signInWithCredential(credential)
                  .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                      @Override
                      public void onComplete(@NonNull Task<AuthResult> task) {
                          if (task.isSuccessful()) {
                              // Sign in success, update UI with the signed-in user's information
                              Log.d(TAG, "signInWithCredential:success");
                              FirebaseUser user = mAuth.getCurrentUser();
                              btnFacebook.setEnabled(true);
                              updateUI(user);
                          } else {
                              // If sign in fails, display a message to the user.
                              Log.w(TAG, "signInWithCredential:failure", task.getException());
                              Toast.makeText(MainActivity.this, "Authentication failed.",
                                      Toast.LENGTH_SHORT).show();
                              btnFacebook.setEnabled(true);
                              updateUI(null);
                          }
  
                          // ...
                      }
                  });
      }
  }
  