<commit_before>/*
   Copyright 2015 IBM Corp.
    Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
        http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package com.ibm.mobileclientaccess.clientsdk.android.auth.facebook;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginBehavior;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.ibm.mobilefirstplatform.clientsdk.android.logger.api.Logger;

import java.util.Arrays;
import java.util.List;

/**
 * Created by iklein on 8/5/15.
 */
public class MCADefaultFacebookAuthenticationHandler implements
        MCAFacebookAuthentication
{
    private Logger logger;
    private List<String> permissionNeeds = Arrays.asList("public_profile");
    private CallbackManager callbackmanager;

    private Context ctx;

    public MCADefaultFacebookAuthenticationHandler(Context ctx) {
        this.ctx = ctx;
        this.logger = Logger.getInstance(MCADefaultFacebookAuthenticationHandler.class.getSimpleName());
        callbackmanager = CallbackManager.Factory.create();
    }

    @Override
    public void handleAuthentication(Context context, String appId) {
        // Verify that the app Id defined in the .plist file is identical to the one requested by the IMF server.
        if(!(appId.equals(FacebookSdk.getApplicationId()))) {
            MCAFacebookAuthenticationManager.getInstance().onFacebookAuthenticationFailure(null);
            return;
        }

        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        // Use the Facebook SDK to login.
        if (accessToken != null && !accessToken.isExpired())
        {
            String token = accessToken.getToken();
            logger.debug("Token alerady available = " + token);
            MCAFacebookAuthenticationManager.getInstance().onFacebookAccessTokenReceived(token);
            return;
        }

        logger.debug("not loggedin, continue");

        if (context instanceof Activity) {
            // Set permissions
            LoginManager.getInstance().logInWithReadPermissions((Activity) context, permissionNeeds);

            LoginManager.getInstance().setLoginBehavior(LoginBehavior.NATIVE_WITH_FALLBACK);

            LoginManager.getInstance().registerCallback(callbackmanager,
                    new FacebookCallback<LoginResult>() {
                        @Override
                        public void onSuccess(LoginResult loginResult) {
                            logger.debug("LoginManager success loggedin");
                            String token = AccessToken.getCurrentAccessToken().getToken();
                            MCAFacebookAuthenticationManager.getInstance().onFacebookAccessTokenReceived(token);
                        }

                        @Override
                        public void onCancel() {
                            logger.debug("On cancel");
                            MCAFacebookAuthenticationManager.getInstance().onFacebookAuthenticationFailure(null);
                        }

                        @Override
                        public void onError(FacebookException error) {
                            logger.debug(error.toString());
                            MCAFacebookAuthenticationManager.getInstance().onFacebookAuthenticationFailure(null);
                        }
                    });
        }

        else{
            MCAFacebookAuthenticationManager.getInstance().onFacebookAuthenticationFailure(null);
        }

    }

    @Override
    public void onActivityResultCalled(int requestCode, int resultCode, Intent data) {
        logger.debug("FB, onActivityResultCalled called");
        callbackmanager.onActivityResult(requestCode, resultCode, data);
    }
}<commit_msg>Added better informed calls to submitAuthenticationFailure<commit_after>/*
   Copyright 2015 IBM Corp.
    Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
        http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package com.ibm.mobileclientaccess.clientsdk.android.auth.facebook;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginBehavior;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.ibm.mobilefirstplatform.clientsdk.android.logger.api.Logger;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;
import java.util.List;

/**
 * Created by iklein on 8/5/15.
 */
public class MCADefaultFacebookAuthenticationHandler implements
        MCAFacebookAuthentication
{
    private Logger logger;
    private List<String> permissionNeeds = Arrays.asList("public_profile");
    private CallbackManager callbackmanager;

    public static final String CANCEL_ERROR_CODE = "403";
    public static final String ERROR_ERROR_CODE = "500";

    private Context ctx;

    public MCADefaultFacebookAuthenticationHandler(Context ctx) {
        this.ctx = ctx;
        this.logger = Logger.getInstance(MCADefaultFacebookAuthenticationHandler.class.getSimpleName());
        callbackmanager = CallbackManager.Factory.create();
    }

    @Override
    public void handleAuthentication(Context context, String appId) {
        // Verify that the app Id defined in the .plist file is identical to the one requested by the IMF server.
        if(!(appId.equals(FacebookSdk.getApplicationId()))) {
            JSONObject obj = new JSONObject();
            try {
                obj.put("errorCode", ERROR_ERROR_CODE);
                obj.put("msg", "FBAuth - AppId is not the same");
            } catch (JSONException e) {
                throw new RuntimeException();
            }
            MCAFacebookAuthenticationManager.getInstance().onFacebookAuthenticationFailure(obj);
            return;
        }

        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        // Use the Facebook SDK to login.
        if (accessToken != null && !accessToken.isExpired())
        {
            String token = accessToken.getToken();
            logger.debug("Token alerady available = " + token);
            MCAFacebookAuthenticationManager.getInstance().onFacebookAccessTokenReceived(token);
            return;
        }

        logger.debug("not loggedin, continue");

        if (context instanceof Activity) {
            // Set permissions
            LoginManager.getInstance().logInWithReadPermissions((Activity) context, permissionNeeds);

            LoginManager.getInstance().setLoginBehavior(LoginBehavior.NATIVE_WITH_FALLBACK);

            LoginManager.getInstance().registerCallback(callbackmanager,
                    new FacebookCallback<LoginResult>() {
                        @Override
                        public void onSuccess(LoginResult loginResult) {
                            logger.debug("LoginManager success loggedin");
                            String token = AccessToken.getCurrentAccessToken().getToken();
                            MCAFacebookAuthenticationManager.getInstance().onFacebookAccessTokenReceived(token);
                        }

                        @Override
                        public void onCancel() {
                            logger.debug("On cancel");
                            JSONObject obj = new JSONObject();
                            try {
                                obj.put("errorCode", CANCEL_ERROR_CODE);
                                obj.put("msg", "LoginManager::onCancel called");
                            } catch (JSONException e) {
                                throw new RuntimeException();
                            }
                            MCAFacebookAuthenticationManager.getInstance().onFacebookAuthenticationFailure(obj);
                        }

                        @Override
                        public void onError(FacebookException error) {
                            logger.debug(error.toString());
                            JSONObject obj = new JSONObject();
                            try {
                                obj.put("errorCode", ERROR_ERROR_CODE);
                                obj.put("msg", error.toString());
                            } catch (JSONException e) {
                                throw new RuntimeException();
                            }
                            MCAFacebookAuthenticationManager.getInstance().onFacebookAuthenticationFailure(obj);
                        }
                    });
        }
        else{
            JSONObject obj = new JSONObject();
            try {
                obj.put("errorCode", ERROR_ERROR_CODE);
                obj.put("msg", "The context provided is not ActivityContext, cannot proceed");
            } catch (JSONException e) {
                throw new RuntimeException();
            }
            MCAFacebookAuthenticationManager.getInstance().onFacebookAuthenticationFailure(obj);
        }

    }

    @Override
    public void onActivityResultCalled(int requestCode, int resultCode, Intent data) {
        logger.debug("FB, onActivityResultCalled called");
        callbackmanager.onActivityResult(requestCode, resultCode, data);
    }
}
{'fraction_non_alphanumeric': 0.07847811576237246, 'fraction_numerical': 0.0028606846571946217, 'mean_word_length': 2.2280701754385963, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 46, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1095967', 'n_tokens_mistral': 2431, 'n_tokens_neox': 2317, 'n_words': 607}