0

I'm creating a library to implement a Facebook login without having to compile FacebookSDK directly in the project using the library, meaning, the library (i.e. FacebookLogingUtils) compiles the Facebook SDK and uses its methods to create simpler methods for a programmer to implement Facebook login. The programmer won't ever have to compile Facebook SDK since everything dealing with it is managed within the library.

The issue is the following one: I want to provide the user with a button which they can call in their layout file instead of FacebookLogginButton, so once the button is received by the library, it will be converted into a Facebook.LoginButton and can be used as such. How can I do this? How can I change a button I receive as a parameter into a different kind of button and return it?

Juan José Melero Gómez
  • 2,742
  • 2
  • 19
  • 36
  • 1
    when User clicks a button, in the Library you can do `FacebookLoginButton.performClick();` – Logic May 19 '15 at 11:39
  • Let me check it. However the interesting thing is that the aspect is the same as the Facebook.LoginButton. – Juan José Melero Gómez May 19 '15 at 11:51
  • Yes, I mean it. What I need is the button to be identical to the Facebook.LoginButton but not being a Facebook.LoginButton, since that would require the programmer to compile Facebook SDK in their project in order to make it work. – Juan José Melero Gómez May 19 '15 at 12:10

2 Answers2

0

Now FB gives provision in their 4.x SDK to do like this without using the FB Login Button. Please try following:

          @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    FacebookSdk.sdkInitialize(this.getApplicationContext());
                    callbackManager = CallbackManager.Factory.create();
                }

            protected void doFBLoginForProfile() {
                    LoginManager loginManager = LoginManager.getInstance();
                    loginManager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                        @Override
                        public void onSuccess(LoginResult loginResult) {
                            fetchUserInfo();
                        }

                        @Override
                        public void onCancel() {
                        }

                        @Override
                        public void onError(FacebookException error) {

                        }
                    });
                    loginManager.logInWithReadPermissions(this, getReadPermissions());
                }

                private ArrayList<String> getReadPermissions() {
                    ArrayList<String> fbPermissions = new ArrayList<String>();
                    fbPermissions.add("public_profile");
                    fbPermissions.add("email");
                    fbPermissions.add("user_location");
                    fbPermissions.add("user_friends");
                    return fbPermissions;
                }

                public void fetchUserInfo() {
                GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        Utils.displayLogs("FB", "response: " + object);
                        onFBUserInfoFetched(new FBProfile().parse(object));
                    }
                }).executeAsync();
            }

            public class FBProfile {
                public String id = "";
                public String email = "";
                public String firstName = "";
                public String lastName = "";
                public String location = "";

        public FBProfile parse(JSONObject object) {
            id = object.optString("id");
            email = object.optString("email");
            firstName = object.optString("first_name");
            lastName = object.optString("last_name");
            if (object.has("location")) {
                location = object.optJSONObject("location").optString("name");
            }
            return this;
        }
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
Kantesh
  • 333
  • 4
  • 13
0

You don't have use the LoginButton class to perform a login. You can just use the LoginManager, and trigger a login anytime by using .loginWith(). To see an example of it can be done, check this answer: https://stackoverflow.com/a/30230718/1395437

Community
  • 1
  • 1
Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71