<commit_before>package com.nestedworld.nestedworld.authenticator;

import com.nestedworld.nestedworld.utils.log.LogHelper;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
 * AccountManager abstraction
 * /!\ this implementation only allow one account per application (it's a personal choice) /!\
 */
public class UserManager {
    //private static field
    private final static String ACCOUNT_DETAIL_KEY = "com.nestedworld.account_detail";
    private final static String KEY_ACCOUNT_NAME = "name";
    private final static String TAG = UserManager.class.getSimpleName();

    //private properties
    private final Context mContext;
    private final AccountManager mAccountManager;
    private Account mAccount;

    /*
    ** Constructor
     */
    private UserManager(@NonNull final Context context) {
        mContext = context;
        mAccountManager = AccountManager.get(context);
        mAccount = getAccountByName(getLastAccountNameConnected());
    }

    /*
    ** singleton
     */
    public static UserManager get(@NonNull final Context context) {
        return (new UserManager(context));
    }

    /*
    ** public method
     */
    public Account getCurrentAccount() {
        return mAccount;
    }

    public boolean setCurrentUser(@NonNull final String name, @NonNull final String password, @NonNull final String authToken, @Nullable final Bundle bundle) {
        //check if account already exist
        Account account = getAccountByName(name);

        if (account == null) {
            //We have to create a new account
            account = new Account(name, Constant.ACCOUNT_TYPE);
            if (!mAccountManager.addAccountExplicitly(account, password, bundle)) {
                return false;
            }
            LogHelper.d(TAG, "Successfully create a new account");
        }

        mAccount = account;
        setAuthTokenTypeOnAccount(account, authToken);
        setAccountNameToPref(name);

        return true;
    }

    public String getCurrentUserData(@NonNull final String key) {
        if (mAccount == null) {
            return null;
        }
        return mAccountManager.getUserData(mAccount, key);
    }

    public Boolean deleteCurrentAccount() {
        return deleteAccount(mAccount);
    }

    public String getCurrentAuthToken() {
        return getAuthTokenFromAccount(mAccount);
    }

    public String getCurrentAccountName() {
        return mAccount.name;
    }


    /*
    ** Private method
     */
    private Boolean deleteAccount(@NonNull final Account account) {
        removeAccountNameFromPref();
        invalidateAuthTokenFromAccount(account);
        if (Build.VERSION.SDK_INT >= 22) {
            mAccountManager.removeAccountExplicitly(account);
        } else {
            mAccountManager.removeAccount(account, null, null);
        }
        return true;
    }

    private Account getAccountByName(@NonNull final String accountName) {
        Account[] accounts = mAccountManager.getAccountsByType(Constant.ACCOUNT_TYPE);
        if (accounts.length == 0) {
            LogHelper.d(TAG, "No registered account");
        }
        for (Account account : accounts) {
            if (account.name.equalsIgnoreCase(accountName)) {
                LogHelper.d(TAG, "Successfully found an account (last_registered = " + accountName + ")");
                return account;
            }<commit_msg>Improve UserManager annotation<commit_after>package com.nestedworld.nestedworld.authenticator;

import com.nestedworld.nestedworld.utils.log.LogHelper;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
 * AccountManager abstraction
 * /!\ this implementation only allow one account per application (it's a personal choice) /!\
 */
public class UserManager {
    //private static field
    private final static String ACCOUNT_DETAIL_KEY = "com.nestedworld.account_detail";
    private final static String KEY_ACCOUNT_NAME = "name";
    private final static String TAG = UserManager.class.getSimpleName();

    //private properties
    private final Context mContext;
    private final AccountManager mAccountManager;
    private Account mAccount;

    /*
    ** Constructor
     */
    private UserManager(@NonNull final Context context) {
        mContext = context;
        mAccountManager = AccountManager.get(context);
        mAccount = getAccountByName(getLastAccountNameConnected());
    }

    /*
    ** singleton
     */
    public static UserManager get(@NonNull final Context context) {
        return (new UserManager(context));
    }

    /*
    ** public method
     */
    @Nullable
    public Account getCurrentAccount() {
        return mAccount;
    }

    public boolean setCurrentUser(@NonNull final String name, @NonNull final String password, @NonNull final String authToken, @Nullable final Bundle bundle) {
        //check if account already exist
        Account account = getAccountByName(name);

        if (account == null) {
            //We have to create a new account
            account = new Account(name, Constant.ACCOUNT_TYPE);
            if (!mAccountManager.addAccountExplicitly(account, password, bundle)) {
                return false;
            }
            LogHelper.d(TAG, "Successfully create a new account");
        }

        mAccount = account;
        setAuthTokenTypeOnAccount(account, authToken);
        setAccountNameToPref(name);

        return true;
    }

    @Nullable
    public String getCurrentUserData(@NonNull final String key) {
        if (mAccount == null) {
            return null;
        }
        return mAccountManager.getUserData(mAccount, key);
    }

    public Boolean deleteCurrentAccount() {
        return deleteAccount(mAccount);
    }

    public String getCurrentAuthToken() {
        return getAuthTokenFromAccount(mAccount);
    }

    @Nullable
    public String getCurrentAccountName() {
        return mAccount.name;
    }


    /*
    ** Private method
     */
    private Boolean deleteAccount(@NonNull final Account account) {
        removeAccountNameFromPref();
        invalidateAuthTokenFromAccount(account);
        if (Build.VERSION.SDK_INT >= 22) {
            mAccountManager.removeAccountExplicitly(account);
        } else {
            mAccountManager.removeAccount(account, null, null);
        }
        return true;
    }

    @Nullable
    private Account getAccountByName(@NonNull final String accountName) {
        Account[] accounts = mAccountManager.getAccountsByType(Constant.ACCOUNT_TYPE);
        if (accounts.length == 0) {
            LogHelper.d(TAG, "No registered account");
        }
        for (Account account : accounts) {
            if (account.name.equalsIgnoreCase(accountName)) {
                LogHelper.d(TAG, "Successfully found an account (last_registered = " + accountName + ")");
                return account;
            }
{'fraction_non_alphanumeric': 0.08695652173913043, 'fraction_numerical': 0.0008177729317159602, 'mean_word_length': 2.7229832572298327, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '681735', 'n_tokens_mistral': 1873, 'n_tokens_neox': 1831, 'n_words': 486}