<commit_before>package org.hsbp.burnstation3;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Handler;
import android.os.PowerManager;
import android.widget.ArrayAdapter;
import java.util.ArrayList;

public class Player extends ArrayAdapter<Player.Item> implements Runnable,
        MediaPlayer.OnCompletionListener {

    protected MediaPlayer mp = null;
    protected Item currentItem = null;
    protected final PlayerUI ui;
    protected final Handler handler = new Handler();

    public Player(Context ctx) {
        super(ctx, android.R.layout.simple_list_item_1, new ArrayList<Player.Item>());
        ui = (PlayerUI)ctx;
    }

    public synchronized void play(final Item item, boolean forceReplace) {
        if (mp != null && item != currentItem && forceReplace) {
            currentItem.setPlaying(false);
            mp.release();
            mp = null;
        }
        if (mp == null) {
            currentItem = item;
            new Thread(new Runnable() {
                protected static final int TRIAL_COUNT_TRESHOLD = 50;

                public void run() {
                    synchronized (Player.this) {
                        final Context ctx = getContext();
                        int trialCount = 0;
                        final Track track = item.getTrack();
                        final Uri track_uri = track.getUri();
                        while (!track.isReadyToPlay()) {
                            try {
                                Thread.sleep(200);
                            } catch (InterruptedException ie) {}
                        }
                        while (true) {
                            try {
                                mp = MediaPlayer.create(ctx, track_uri);
								mp.setOnCompletionListener(Player.this);
								mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
								mp.setWakeMode(ctx.getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
								performPlay();
								return;
                            } catch (Exception e) {
								if (mp != null) {
									mp.release();
									mp = null;
								}
								try {
	                                Thread.sleep(200);
								} catch (InterruptedException ie) {}
                                trialCount++;
                                if (trialCount >= TRIAL_COUNT_TRESHOLD) {
                                    item.setPlaying(false);
                                    currentItem = null;
                                    return; // TODO notify user
                                }
                            }
                        }
                    }
                }
            }).start();
            item.setPlaying(true);
            ui.updateTotal(item.getTrack().getDuration());
        } else {<commit_msg>extracted methods setupMediaPlayer and waitForReadyTrack<commit_after>package org.hsbp.burnstation3;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Handler;
import android.os.PowerManager;
import android.widget.ArrayAdapter;
import java.util.ArrayList;

public class Player extends ArrayAdapter<Player.Item> implements Runnable,
        MediaPlayer.OnCompletionListener {

    protected MediaPlayer mp = null;
    protected Item currentItem = null;
    protected final PlayerUI ui;
    protected final Handler handler = new Handler();

    public Player(Context ctx) {
        super(ctx, android.R.layout.simple_list_item_1, new ArrayList<Player.Item>());
        ui = (PlayerUI)ctx;
    }

    public synchronized void play(final Item item, boolean forceReplace) {
        if (mp != null && item != currentItem && forceReplace) {
            currentItem.setPlaying(false);
            mp.release();
            mp = null;
        }
        if (mp == null) {
            currentItem = item;
            new Thread(new Runnable() {
                protected static final int TRIAL_COUNT_TRESHOLD = 50;

                public void run() {
                    synchronized (Player.this) {
                        int trialCount = 0;
                        final Track track = item.getTrack();
						waitForReadyTrack(track);
                        while (true) {
                            try {
								setupMediaPlayer(track);
								performPlay();
								return;
                            } catch (Exception e) {
								if (mp != null) {
									mp.release();
									mp = null;
								}
								try {
	                                Thread.sleep(200);
								} catch (InterruptedException ie) {}
                                trialCount++;
                                if (trialCount >= TRIAL_COUNT_TRESHOLD) {
                                    item.setPlaying(false);
                                    currentItem = null;
                                    return; // TODO notify user
                                }
                            }
                        }
                    }
                }

                protected void setupMediaPlayer(Track track) {
                    final Context ctx = getContext();
                    mp = MediaPlayer.create(ctx, track.getUri());
                    mp.setOnCompletionListener(Player.this);
                    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    mp.setWakeMode(ctx.getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
                }

                protected void waitForReadyTrack(Track track) {
                    while (!track.isReadyToPlay()) {
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException ie) {}
                    }
                }
            }).start();
            item.setPlaying(true);
            ui.updateTotal(item.getTrack().getDuration());
        } else {
{'fraction_non_alphanumeric': 0.08528428093645485, 'fraction_numerical': 0.0036789297658862876, 'mean_word_length': 1.3236208236208236, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2633359', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1355, 'n_words': 316}