Search in sources :

Example 1 with LoginCallback

use of com.codename1.social.LoginCallback 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)

Example 2 with LoginCallback

use of com.codename1.social.LoginCallback 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 LoginCallback

use of com.codename1.social.LoginCallback in project CodenameOne by codenameone.

the class GoogleLoginSample method doLogin.

void doLogin(Login lg, UserData data, boolean forceLogin) {
    if (!forceLogin) {
        if (lg.isUserLoggedIn()) {
            showContactsForm(data);
            return;
        }
        // if the user already logged in previously and we have a token
        String t = Preferences.get(tokenPrefix + "token", (String) null);
        if (t != null) {
            // we check the expiration of the token which we previously stored as System time
            long tokenExpires = Preferences.get(tokenPrefix + "tokenExpires", (long) -1);
            if (tokenExpires < 0 || tokenExpires > System.currentTimeMillis()) {
                // we are still logged in
                showContactsForm(data);
                return;
            }
        }
    }
    lg.setCallback(new LoginCallback() {

        @Override
        public void loginFailed(String errorMessage) {
            Dialog.show("Error Logging In", "There was an error logging in: " + errorMessage, "OK", null);
        }

        @Override
        public void loginSuccessful() {
            // when login is successful we fetch the full data
            data.fetchData(lg.getAccessToken().getToken(), () -> {
                // we store the values of result into local variables
                uniqueId = data.getId();
                fullName = data.getName();
                imageURL = data.getImage();
                // we then store the data into local cached storage so they will be around when we run the app next time
                Preferences.set("fullName", fullName);
                Preferences.set("uniqueId", uniqueId);
                Preferences.set("imageURL", imageURL);
                Preferences.set(tokenPrefix + "token", lg.getAccessToken().getToken());
                // token expiration is in seconds from the current time, we convert it to a System.currentTimeMillis value so we can
                // reference it in the future to check expiration
                Preferences.set(tokenPrefix + "tokenExpires", tokenExpirationInMillis(lg.getAccessToken()));
                showContactsForm(data);
            });
        }
    });
    lg.doLogin();
}
Also used : LoginCallback(com.codename1.social.LoginCallback)

Aggregations

Intent (android.content.Intent)2 CodenameOneActivity (com.codename1.impl.android.CodenameOneActivity)2 IntentResultListener (com.codename1.impl.android.IntentResultListener)2 CallbackManager (com.facebook.CallbackManager)2 LoginManager (com.facebook.login.LoginManager)2 LoginCallback (com.codename1.social.LoginCallback)1