<commit_before>package fr.tvbarthel.simplesoundcloud.library.player;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.v7.app.ActionBarActivity;

import java.lang.ref.WeakReference;
import java.util.List;

import fr.tvbarthel.simplesoundcloud.library.R;
import fr.tvbarthel.simplesoundcloud.library.models.SoundCloudPlaylist;
import fr.tvbarthel.simplesoundcloud.library.models.SoundCloudTrack;

/**
 * Encapsulate network and player features to work with sound cloud.
 */
public final class SimpleSoundCloudPlayer {

    /**
     * Instance, singleton pattern.
     */
    private static SimpleSoundCloudPlayer sInstance;

    /**
     * WeakReference on the application context.
     */
    private WeakReference<Context> mApplicationContext;

    /**
     * Sound cloud client id used to have access to the API.
     */
    private String mClientKey;

    /**
     * Internal listener used to catch service callbacks
     */
    private SimpleSoundCloudListener mInternalListener;

    /**
     * Manage the playlist used by the player.
     */
    private PlayerPlaylist mPlayerPlaylist;

    /**
     * Manage the notification.
     */
    private NotificationManager mNotificationManager;

    /**
     * Used to know if the player is paused.
     */
    private boolean mIsPaused;

    /**
     * Used to know if the current client instance has been closed.
     */
    private boolean mIsClosed;


    /**
     * Private default constructor.
     */
    private SimpleSoundCloudPlayer() {

    }

    /**
     * Singleton pattern.
     *
     * @param applicationContext context used to initiate
     *                           {@link fr.tvbarthel.simplesoundcloud.library.offline.Offliner}
     * @param clientId           SoundCloud api client key.
     */
    private SimpleSoundCloudPlayer(Context applicationContext, String clientId) {

        mClientKey = clientId;
        mIsClosed = false;
        mIsPaused = false;

        mApplicationContext = new WeakReference<>(applicationContext);

        mPlayerPlaylist = PlayerPlaylist.getInstance();
        mNotificationManager = NotificationManager.getInstance(getContext());

        initInternalListener(applicationContext);
    }

    /**
     * Simple Sound cloud client initialized with a client id.
     *
     * @param context  context used to instantiate internal components, no hard reference will be kept.
     * @param clientId sound cloud client id.
     * @return instance of {@link fr.tvbarthel.simplesoundcloud.library.player.SimpleSoundCloudPlayer}
     */
    private static SimpleSoundCloudPlayer getInstance(Context context, String clientId) {
        if (clientId == null) {
            throw new IllegalArgumentException("Sound cloud client id can't be null.");
        }
        if (sInstance == null || sInstance.mIsClosed) {
            sInstance = new SimpleSoundCloudPlayer(context.getApplicationContext(), clientId);
        } else {
            sInstance.mClientKey = clientId;
        }
        return sInstance;
    }

    /**
     * Release resources associated with this client.
     */
    public void close() {
        if (mIsClosed) {
            return;
        }
        mIsClosed = true;

        PlaybackService.unregisterListener(getContext(), mInternalListener);
        mInternalListener = null;

        mApplicationContext.clear();
        mApplicationContext = null;
<commit_msg>[API] allow to directly add tracks to the player<commit_after>package fr.tvbarthel.simplesoundcloud.library.player;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.v7.app.ActionBarActivity;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import fr.tvbarthel.simplesoundcloud.library.R;
import fr.tvbarthel.simplesoundcloud.library.models.SoundCloudPlaylist;
import fr.tvbarthel.simplesoundcloud.library.models.SoundCloudTrack;
import rx.functions.Action1;

/**
 * Encapsulate network and player features to work with sound cloud.
 */
public final class SimpleSoundCloudPlayer implements Action1<ArrayList<SoundCloudTrack>> {

    /**
     * Instance, singleton pattern.
     */
    private static SimpleSoundCloudPlayer sInstance;

    /**
     * WeakReference on the application context.
     */
    private WeakReference<Context> mApplicationContext;

    /**
     * Sound cloud client id used to have access to the API.
     */
    private String mClientKey;

    /**
     * Internal listener used to catch service callbacks
     */
    private SimpleSoundCloudListener mInternalListener;

    /**
     * Manage the playlist used by the player.
     */
    private PlayerPlaylist mPlayerPlaylist;

    /**
     * Manage the notification.
     */
    private NotificationManager mNotificationManager;

    /**
     * Used to know if the player is paused.
     */
    private boolean mIsPaused;

    /**
     * Used to know if the current client instance has been closed.
     */
    private boolean mIsClosed;


    /**
     * Private default constructor.
     */
    private SimpleSoundCloudPlayer() {

    }

    /**
     * Singleton pattern.
     *
     * @param applicationContext context used to initiate
     *                           {@link fr.tvbarthel.simplesoundcloud.library.offline.Offliner}
     * @param clientId           SoundCloud api client key.
     */
    private SimpleSoundCloudPlayer(Context applicationContext, String clientId) {

        mClientKey = clientId;
        mIsClosed = false;
        mIsPaused = false;

        mApplicationContext = new WeakReference<>(applicationContext);

        mPlayerPlaylist = PlayerPlaylist.getInstance();
        mNotificationManager = NotificationManager.getInstance(getContext());

        initInternalListener(applicationContext);
    }

    /**
     * Simple Sound cloud client initialized with a client id.
     *
     * @param context  context used to instantiate internal components, no hard reference will be kept.
     * @param clientId sound cloud client id.
     * @return instance of {@link fr.tvbarthel.simplesoundcloud.library.player.SimpleSoundCloudPlayer}
     */
    private static SimpleSoundCloudPlayer getInstance(Context context, String clientId) {
        if (clientId == null) {
            throw new IllegalArgumentException("Sound cloud client id can't be null.");
        }
        if (sInstance == null || sInstance.mIsClosed) {
            sInstance = new SimpleSoundCloudPlayer(context.getApplicationContext(), clientId);
        } else {
            sInstance.mClientKey = clientId;
        }
        return sInstance;
    }

    @Override
    public void call(ArrayList<SoundCloudTrack> soundCloudTracks) {
        addTracks(soundCloudTracks);
    }

    /**
     * Release resources associated with this client.
     */
    public void close() {
        if (mIsClosed) {
            return;
        }
        mIsClosed = true;

        PlaybackService.unregisterListener(getContext(), mInternalListener);
        mInternalListener = null;

        mApplicationContext.clear();
        mApplicationContext = null;

{'fraction_non_alphanumeric': 0.08181944251837471, 'fraction_numerical': 0.000554708084870337, 'mean_word_length': 2.702258726899384, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3638790', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1822, 'n_words': 502}