<commit_before>/*
 * Copyright (C) 2012-2014 Soomla Inc.
 *
 *   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.soomla.profile;

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

import android.net.Uri;
import android.os.Environment;
import android.view.View;
import com.soomla.BusProvider;
import com.soomla.SoomlaApp;
import com.soomla.SoomlaMarketUtils;
import com.soomla.SoomlaUtils;
import com.soomla.profile.domain.IProvider;
import com.soomla.profile.domain.UserProfile;
import com.soomla.profile.events.UserRatingEvent;
import com.soomla.profile.events.ProfileInitializedEvent;
import com.soomla.profile.exceptions.ProviderNotFoundException;
import com.soomla.rewards.Reward;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;

/**
 * This is the main class for the SOOMLA User Profile module.  This class
 * should be initialized once, after <code>Soomla.initialize()</code> is invoked.
 * Use this class to perform authentication and social actions on behalf of
 * the user that will grant him \ her rewards in your game.
 */
@SuppressWarnings("UnusedDeclaration")
public class SoomlaProfile {

    public static final String VERSION = "1.1.7";

    /**
     * see {@link #initialize(Activity, boolean, Map)}
     */
    public boolean initialize() {
        return initialize(null, null);
    }

    /**
     * see {@link #initialize(Activity, boolean, Map)}
     */
    public boolean initialize(Activity activity) {
        return initialize(activity, null);
    }

    /**
     * see {@link #initialize(Activity, boolean, Map)}
     */
    public boolean initialize(Map<IProvider.Provider, ? extends Map<String, String>> providerParams) {
        return initialize(null, false, providerParams);
    }

    /**
     * see {@link #initialize(Activity, boolean, Map)}
     */
    public boolean initialize(Activity activity, Map<IProvider.Provider, ? extends Map<String, String>> providerParams) {
        return initialize(activity, false, providerParams);
    }

    public boolean initialize(boolean usingExternalProvider, Map<IProvider.Provider, ? extends Map<String, String>> providerParams) {
        return initialize(null, false, providerParams);
    }

    /**
     * Initializes the Profile module.  Call this method after <code>Soomla.initialize()</code>
     * @param activity The parent activity
     * @param usingExternalProvider If using an external SDK (like Unity FB SDK) pass true
     *                              here so we know not to complain about native providers
     *                              not found
     * @param providerParams provides custom values for specific social providers
     */
    public boolean initialize(Activity activity, boolean usingExternalProvider, Map<IProvider.Provider, ? extends Map<String, String>> providerParams) {
        if (mInitialized) {
            String err = "SoomlaStore is already initialized. You can't initialize it twice!";
            SoomlaUtils.LogError(TAG, err);
            return false;
        }

        mAuthController = new AuthController(usingExternalProvider, providerParams);
        mSocialController = new SocialController(usingExternalProvider, providerParams);

        mInitialized = true;

        BusProvider.getInstance().post(new ProfileInitializedEvent());

        if (activity == null) {
            SoomlaUtils.LogWarning(TAG,
                    "You want to use `autoLogin`, but have not provided the `activity`. Skipping auto login");
            return false;
        }
        mAuthController.settleAutoLogin(activity);
        mSocialController.settleAutoLogin(activity);

        return true;
    }

    /**
     * Login to the given provider
     *
     * @param activity The parent activity
     * @param provider The provider to use
     * @throws ProviderNotFoundException if the supplied provider is not
     *                                   supported by the framework
     */
    public void login(Activity activity, final IProvider.Provider provider) throws ProviderNotFoundException {
        login(activity, provider, "", null);
    }

    /**
     * Login to the given provider and grant the user a reward.
     *
     * @param activity The parent activity
     * @param provider The provider to use
     * @param reward   The reward to give the user for logging in.
     * @throws ProviderNotFoundException if the supplied provider is not
     *                                   supported by the framework
     */
    public void login(Activity activity, final IProvider.Provider provider, final Reward reward) throws ProviderNotFoundException {
        login(activity, provider, "", reward);
    }

    /**
     * Login to the given provider and grant the user a reward.
     *
     * @param activity The parent activity
     * @param provider The provider to use
     * @param payload  a String to receive when the function returns.
     * @param reward   The reward to give the user for logging in.
     * @throws ProviderNotFoundException if the supplied provider is not
     *                                   supported by the framework
     */
    public void login(Activity activity, final IProvider.Provider provider,
                      String payload, final Reward reward) throws ProviderNotFoundException {
        try {
            mAuthController.login(activity, provider, false, payload, reward);
        } catch (ProviderNotFoundException e) {
            mSocialController.login(activity, provider, false, payload, reward);
        }
    }

    /**
     * Checks if the user is logged-in to the given provider
     *
     * @param activity The parent activity
     * @param provider The provider to use
     * @return true if the user is logged-in with the authentication provider,
     * false otherwise
     * @throws ProviderNotFoundException if the supplied provider is not
     *                                   supported by the framework
     */
    public boolean isLoggedIn(Activity activity, final IProvider.Provider provider) throws ProviderNotFoundException {
        try {
            return mAuthController.isLoggedIn(activity, provider);
        } catch (ProviderNotFoundException e) {
            return mSocialController.isLoggedIn(activity, provider);
        }
    }

    /**
     * Logout of the given provider
     *
     * @param provider The provider to use
     * @throws ProviderNotFoundException if the supplied provider is not
     *                                   supported by the framework
     */
    public void logout(final IProvider.Provider provider) throws ProviderNotFoundException {
        try {
            mAuthController.logout(provider);
        } catch (ProviderNotFoundException e) {
            mSocialController.logout(provider);
        }
    }

    /**
     * Fetches the user's profile for the given provider from the local device storage
     *
     * @param provider The provider to use
     * @return The user profile
     */
    public UserProfile getStoredUserProfile(IProvider.Provider provider) {
        UserProfile userProfile = mAuthController.getStoredUserProfile(provider);
        if (userProfile != null)
            return userProfile;

        return mSocialController.getStoredUserProfile(provider);
    }

    /**
     * Shares the given status to the user's feed and grants the user a reward.
     *
     * @param provider The provider to use
     * @param status   The text to share
     * @param reward   The reward to give the user
     * @throws ProviderNotFoundException if the supplied provider is not
     *                                   supported by the framework
     */
    public void updateStatus(IProvider.Provider provider, String status, final Reward reward) throws ProviderNotFoundException {
        updateStatus(provider, status, "", reward);
    }

    /**<commit_msg>New #113 Add logoutFromAllProviders method to SoomlaProfile<commit_after>/*
 * Copyright (C) 2012-2014 Soomla Inc.
 *
 *   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.soomla.profile;

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

import android.net.Uri;
import android.os.Environment;
import android.view.View;
import com.soomla.BusProvider;
import com.soomla.SoomlaApp;
import com.soomla.SoomlaMarketUtils;
import com.soomla.SoomlaUtils;
import com.soomla.profile.domain.IProvider;
import com.soomla.profile.domain.UserProfile;
import com.soomla.profile.events.UserRatingEvent;
import com.soomla.profile.events.ProfileInitializedEvent;
import com.soomla.profile.exceptions.ProviderNotFoundException;
import com.soomla.rewards.Reward;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;

/**
 * This is the main class for the SOOMLA User Profile module.  This class
 * should be initialized once, after <code>Soomla.initialize()</code> is invoked.
 * Use this class to perform authentication and social actions on behalf of
 * the user that will grant him \ her rewards in your game.
 */
@SuppressWarnings("UnusedDeclaration")
public class SoomlaProfile {

    public static final String VERSION = "1.1.7";

    /**
     * see {@link #initialize(Activity, boolean, Map)}
     */
    public boolean initialize() {
        return initialize(null, null);
    }

    /**
     * see {@link #initialize(Activity, boolean, Map)}
     */
    public boolean initialize(Activity activity) {
        return initialize(activity, null);
    }

    /**
     * see {@link #initialize(Activity, boolean, Map)}
     */
    public boolean initialize(Map<IProvider.Provider, ? extends Map<String, String>> providerParams) {
        return initialize(null, false, providerParams);
    }

    /**
     * see {@link #initialize(Activity, boolean, Map)}
     */
    public boolean initialize(Activity activity, Map<IProvider.Provider, ? extends Map<String, String>> providerParams) {
        return initialize(activity, false, providerParams);
    }

    public boolean initialize(boolean usingExternalProvider, Map<IProvider.Provider, ? extends Map<String, String>> providerParams) {
        return initialize(null, false, providerParams);
    }

    /**
     * Initializes the Profile module.  Call this method after <code>Soomla.initialize()</code>
     * @param activity The parent activity
     * @param usingExternalProvider If using an external SDK (like Unity FB SDK) pass true
     *                              here so we know not to complain about native providers
     *                              not found
     * @param providerParams provides custom values for specific social providers
     */
    public boolean initialize(Activity activity, boolean usingExternalProvider, Map<IProvider.Provider, ? extends Map<String, String>> providerParams) {
        if (mInitialized) {
            String err = "SoomlaStore is already initialized. You can't initialize it twice!";
            SoomlaUtils.LogError(TAG, err);
            return false;
        }

        mAuthController = new AuthController(usingExternalProvider, providerParams);
        mSocialController = new SocialController(usingExternalProvider, providerParams);

        mInitialized = true;

        BusProvider.getInstance().post(new ProfileInitializedEvent());

        if (activity == null) {
            SoomlaUtils.LogWarning(TAG,
                    "You want to use `autoLogin`, but have not provided the `activity`. Skipping auto login");
            return false;
        }
        mAuthController.settleAutoLogin(activity);
        mSocialController.settleAutoLogin(activity);

        return true;
    }

    /**
     * Login to the given provider
     *
     * @param activity The parent activity
     * @param provider The provider to use
     * @throws ProviderNotFoundException if the supplied provider is not
     *                                   supported by the framework
     */
    public void login(Activity activity, final IProvider.Provider provider) throws ProviderNotFoundException {
        login(activity, provider, "", null);
    }

    /**
     * Login to the given provider and grant the user a reward.
     *
     * @param activity The parent activity
     * @param provider The provider to use
     * @param reward   The reward to give the user for logging in.
     * @throws ProviderNotFoundException if the supplied provider is not
     *                                   supported by the framework
     */
    public void login(Activity activity, final IProvider.Provider provider, final Reward reward) throws ProviderNotFoundException {
        login(activity, provider, "", reward);
    }

    /**
     * Login to the given provider and grant the user a reward.
     *
     * @param activity The parent activity
     * @param provider The provider to use
     * @param payload  a String to receive when the function returns.
     * @param reward   The reward to give the user for logging in.
     * @throws ProviderNotFoundException if the supplied provider is not
     *                                   supported by the framework
     */
    public void login(Activity activity, final IProvider.Provider provider,
                      String payload, final Reward reward) throws ProviderNotFoundException {
        try {
            mAuthController.login(activity, provider, false, payload, reward);
        } catch (ProviderNotFoundException e) {
            mSocialController.login(activity, provider, false, payload, reward);
        }
    }

    /**
     * Checks if the user is logged-in to the given provider
     *
     * @param activity The parent activity
     * @param provider The provider to use
     * @return true if the user is logged-in with the authentication provider,
     * false otherwise
     * @throws ProviderNotFoundException if the supplied provider is not
     *                                   supported by the framework
     */
    public boolean isLoggedIn(Activity activity, final IProvider.Provider provider) throws ProviderNotFoundException {
        try {
            return mAuthController.isLoggedIn(activity, provider);
        } catch (ProviderNotFoundException e) {
            return mSocialController.isLoggedIn(activity, provider);
        }
    }

    /**
     * Logout from the given provider
     *
     * @param provider The provider to use
     * @throws ProviderNotFoundException if the supplied provider is not
     *                                   supported by the framework
     */
    public void logout(final IProvider.Provider provider) throws ProviderNotFoundException {
        try {
            mAuthController.logout(provider);
        } catch (ProviderNotFoundException e) {
            mSocialController.logout(provider);
        }
    }

    /**
     * Logout from all available providers
     */
    public void logoutFromAllProviders() {
        for (IProvider.Provider provider : IProvider.Provider.values()) {
            try {
                SoomlaProfile.getInstance().logout(provider);
            } catch (ProviderNotFoundException e) {
                // Skip
            }
        }
    }

    /**
     * Fetches the user's profile for the given provider from the local device storage
     *
     * @param provider The provider to use
     * @return The user profile
     */
    public UserProfile getStoredUserProfile(IProvider.Provider provider) {
        UserProfile userProfile = mAuthController.getStoredUserProfile(provider);
        if (userProfile != null)
            return userProfile;

        return mSocialController.getStoredUserProfile(provider);
    }

    /**
     * Shares the given status to the user's feed and grants the user a reward.
     *
     * @param provider The provider to use
     * @param status   The text to share
     * @param reward   The reward to give the user
     * @throws ProviderNotFoundException if the supplied provider is not
     *                                   supported by the framework
     */
    public void updateStatus(IProvider.Provider provider, String status, final Reward reward) throws ProviderNotFoundException {
        updateStatus(provider, status, "", reward);
    }

    /**
{'fraction_non_alphanumeric': 0.07580915017596493, 'fraction_numerical': 0.001903882766976288, 'mean_word_length': 2.626359832635983, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6887936', 'n_tokens_mistral': 4278, 'n_tokens_neox': 4158, 'n_words': 1510}