Search in sources :

Example 1 with CallbackManager

use of com.facebook.CallbackManager in project Android-SDK by Backendless.

the class LoginWithFacebookSDKActivity method configureFacebookSDKLogin.

private CallbackManager configureFacebookSDKLogin() {
    loginFacebookButton.setReadPermissions("email");
    // If using in a fragment
    // loginFacebookButton.setFragment(this);
    CallbackManager callbackManager = CallbackManager.Factory.create();
    // Callback registration
    loginFacebookButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(LoginResult loginResult) {
            isLoggedInFacebook = true;
            fbAccessToken = loginResult.getAccessToken().getToken();
            String msg = "ApplicationId: " + loginResult.getAccessToken().getApplicationId() + "\n" + "UserId: " + loginResult.getAccessToken().getUserId() + "\n" + "Token: " + loginResult.getAccessToken().getToken() + "\n" + "LastRefresh: " + loginResult.getAccessToken().getLastRefresh().toString() + "\n" + "Expires: " + loginResult.getAccessToken().getExpires().toString();
            socialAccountInfo.setTextColor(getColor(android.R.color.black));
            socialAccountInfo.setText(msg);
            if (!isLoggedInBackendless)
                loginToBackendless();
            else
                loginFacebookButton.setVisibility(View.INVISIBLE);
            fbLogoutBackendlessButton.setVisibility(View.VISIBLE);
        }

        @Override
        public void onCancel() {
            // App code
            Log.i("LoginProcess", "loginFacebookButton::onCancel");
        }

        @Override
        public void onError(FacebookException exception) {
            fbAccessToken = null;
            String msg = exception.getMessage() + "\nCause:\n" + (exception.getCause() != null ? exception.getCause().getMessage() : "none");
            socialAccountInfo.setTextColor(getColor(android.R.color.holo_red_dark));
            socialAccountInfo.setText(msg);
            isLoggedInFacebook = false;
        }
    });
    return callbackManager;
}
Also used : FacebookException(com.facebook.FacebookException) LoginResult(com.facebook.login.LoginResult) CallbackManager(com.facebook.CallbackManager)

Example 2 with CallbackManager

use of com.facebook.CallbackManager in project CodenameOne by codenameone.

the class FacebookImpl method login.

private void login(final LoginCallback cb) {
    if (loginLock) {
        return;
    }
    loginLock = true;
    LoginManager login = LoginManager.getInstance();
    final CallbackManager mCallbackManager = CallbackManager.Factory.create();
    if (AndroidNativeUtil.getActivity() == null) {
        throw new RuntimeException("Cannot login to facebook when running in the background.");
    }
    final CodenameOneActivity activity = (CodenameOneActivity) AndroidNativeUtil.getActivity();
    activity.setIntentResultListener(new IntentResultListener() {

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            mCallbackManager.onActivityResult(requestCode, resultCode, data);
            activity.restoreIntentResultListener();
        }
    });
    login.registerCallback(mCallbackManager, new FBCallback(cb));
    login.logInWithReadPermissions(activity, permissions);
}
Also used : LoginManager(com.facebook.login.LoginManager) CodenameOneActivity(com.codename1.impl.android.CodenameOneActivity) IntentResultListener(com.codename1.impl.android.IntentResultListener) Intent(android.content.Intent) CallbackManager(com.facebook.CallbackManager)

Example 3 with CallbackManager

use of com.facebook.CallbackManager in project CodenameOne by codenameone.

the class FacebookImpl method inviteFriends.

@Override
public void inviteFriends(String appLinkUrl, String previewImageUrl, final Callback cb) {
    if (AndroidNativeUtil.getActivity() == null) {
        throw new RuntimeException("Cannot invite friends while running in the background.");
    }
    if (AppInviteDialog.canShow()) {
        AppInviteContent content = new AppInviteContent.Builder().setApplinkUrl(appLinkUrl).setPreviewImageUrl(previewImageUrl).build();
        final CodenameOneActivity activity = (CodenameOneActivity) AndroidNativeUtil.getActivity();
        if (cb == null) {
            AppInviteDialog.show(activity, content);
        } else {
            AppInviteDialog appInviteDialog = new AppInviteDialog(activity);
            final CallbackManager mCallbackManager = CallbackManager.Factory.create();
            activity.setIntentResultListener(new IntentResultListener() {

                @Override
                public void onActivityResult(int requestCode, int resultCode, Intent data) {
                    mCallbackManager.onActivityResult(requestCode, resultCode, data);
                    activity.restoreIntentResultListener();
                }
            });
            appInviteDialog.registerCallback(mCallbackManager, new FacebookCallback<AppInviteDialog.Result>() {

                @Override
                public void onSuccess(AppInviteDialog.Result result) {
                    Display.getInstance().callSerially(new Runnable() {

                        @Override
                        public void run() {
                            cb.onSucess(null);
                        }
                    });
                }

                @Override
                public void onCancel() {
                    Display.getInstance().callSerially(new Runnable() {

                        @Override
                        public void run() {
                            cb.onError(null, null, -1, "User Cancelled");
                        }
                    });
                }

                @Override
                public void onError(final FacebookException e) {
                    Display.getInstance().callSerially(new Runnable() {

                        @Override
                        public void run() {
                            cb.onError(null, e, 0, e.getMessage());
                        }
                    });
                }
            });
            appInviteDialog.show(content);
        }
    }
}
Also used : CodenameOneActivity(com.codename1.impl.android.CodenameOneActivity) Intent(android.content.Intent) CallbackManager(com.facebook.CallbackManager) AppInviteDialog(com.facebook.share.widget.AppInviteDialog) IntentResultListener(com.codename1.impl.android.IntentResultListener) FacebookException(com.facebook.FacebookException) AppInviteContent(com.facebook.share.model.AppInviteContent)

Example 4 with CallbackManager

use of com.facebook.CallbackManager in project CodenameOne by codenameone.

the class FacebookImpl method askPublishPermissions.

public void askPublishPermissions(final LoginCallback cb) {
    if (AndroidNativeUtil.getActivity() == null) {
        throw new RuntimeException("Cannot ask for publish permissions when running in the background.");
    }
    if (loginLock) {
        return;
    }
    loginLock = true;
    LoginManager login = LoginManager.getInstance();
    final CallbackManager mCallbackManager = CallbackManager.Factory.create();
    final CodenameOneActivity activity = (CodenameOneActivity) AndroidNativeUtil.getActivity();
    activity.setIntentResultListener(new IntentResultListener() {

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            mCallbackManager.onActivityResult(requestCode, resultCode, data);
            activity.restoreIntentResultListener();
        }
    });
    login.registerCallback(mCallbackManager, new FBCallback(cb));
    login.logInWithPublishPermissions(activity, PUBLISH_PERMISSIONS);
}
Also used : LoginManager(com.facebook.login.LoginManager) CodenameOneActivity(com.codename1.impl.android.CodenameOneActivity) IntentResultListener(com.codename1.impl.android.IntentResultListener) Intent(android.content.Intent) CallbackManager(com.facebook.CallbackManager)

Aggregations

CallbackManager (com.facebook.CallbackManager)4 Intent (android.content.Intent)3 CodenameOneActivity (com.codename1.impl.android.CodenameOneActivity)3 IntentResultListener (com.codename1.impl.android.IntentResultListener)3 FacebookException (com.facebook.FacebookException)2 LoginManager (com.facebook.login.LoginManager)2 LoginResult (com.facebook.login.LoginResult)1 AppInviteContent (com.facebook.share.model.AppInviteContent)1 AppInviteDialog (com.facebook.share.widget.AppInviteDialog)1