<commit_before>package bob.sun.mpod.service;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicBoolean;

import bob.sun.mpod.controller.PlayingListener;
import bob.sun.mpod.model.MediaLibrary;
import bob.sun.mpod.model.SongBean;
import bob.sun.mpod.utils.NotificationUtil;
import bob.sun.mpod.utils.PreferenceUtil;

/**
 * Created by sunkuan on 15/4/29.
 */
public class PlayerService extends Service implements MediaPlayer.OnCompletionListener, AudioManager.OnAudioFocusChangeListener {
    private MediaPlayer mediaPlayer;
    private final ServiceBinder binder = new ServiceBinder();
    private ArrayList<SongBean> playlist;
    private int index;
    private PlayingListener playingListener;
    private AudioManager audioManager;
//    private Thread progressThread;
    private ProgressRunnable runnable;
    public static final int CMD_PLAY = 1;
    public static final int CMD_PAUSE = 2;
    public static final int CMD_RESUME = 3;
    public static final int CMD_STOP = 4;
    public static final int CMD_NEXT = 5;
    public static final int CMD_PREVIOUS = 6;
    public static final int CMD_VOLUMN_UP = 7;
    public static final int CMD_VOLUMN_DOWN = 8;

    @Override
    public void onCreate(){
        super.onCreate();
        if(mediaPlayer != null){
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnCompletionListener(this);
//        progressThread = new Thread(new ProgressRunnable());
        runnable = new ProgressRunnable();
//        new Thread(runnable).start();
        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

    }
    @Override
    public int onStartCommand(Intent intent, int flags,int startId){
        if (intent == null){
            return START_REDELIVER_INTENT;
        }
        switch (intent.getIntExtra("CMD",-1)){
            case CMD_PLAY:
                String fileName = intent.getStringExtra("DATA");
                index = intent.getIntExtra("INDEX",0);
                runnable.stop();
                if(mediaPlayer.isPlaying()){
                    mediaPlayer.stop();
                }
                mediaPlayer.reset();
                try {
                    mediaPlayer.setDataSource(fileName);
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                    if (playingListener!=null)
                        playingListener.onSongChanged(playlist.get(index));
                    runnable.start();
                    new Thread(runnable).start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                NotificationUtil.getStaticInstance(getApplicationContext()).sendPlayNotification(playlist.get(index));<commit_msg>cleanup<commit_after>package bob.sun.mpod.service;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicBoolean;

import bob.sun.mpod.controller.PlayingListener;
import bob.sun.mpod.model.MediaLibrary;
import bob.sun.mpod.model.SongBean;
import bob.sun.mpod.utils.NotificationUtil;
import bob.sun.mpod.utils.PreferenceUtil;

/**
 * Created by sunkuan on 15/4/29.
 */
public class PlayerService extends Service implements MediaPlayer.OnCompletionListener, AudioManager.OnAudioFocusChangeListener {
    private MediaPlayer mediaPlayer;
    private final ServiceBinder binder = new ServiceBinder();
    private ArrayList<SongBean> playlist;
    private int index;
    private PlayingListener playingListener;
    private AudioManager audioManager;
    private ProgressRunnable runnable;
    public static final int CMD_PLAY = 1;
    public static final int CMD_PAUSE = 2;
    public static final int CMD_RESUME = 3;
    public static final int CMD_STOP = 4;
    public static final int CMD_NEXT = 5;
    public static final int CMD_PREVIOUS = 6;
    public static final int CMD_VOLUMN_UP = 7;
    public static final int CMD_VOLUMN_DOWN = 8;

    @Override
    public void onCreate(){
        super.onCreate();
        if(mediaPlayer != null){
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnCompletionListener(this);
        runnable = new ProgressRunnable();
        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

    }
    @Override
    public int onStartCommand(Intent intent, int flags,int startId){
        if (intent == null){
            return START_REDELIVER_INTENT;
        }
        switch (intent.getIntExtra("CMD",-1)){
            case CMD_PLAY:
                String fileName = intent.getStringExtra("DATA");
                index = intent.getIntExtra("INDEX",0);
                runnable.stop();
                if(mediaPlayer.isPlaying()){
                    mediaPlayer.stop();
                }
                mediaPlayer.reset();
                try {
                    mediaPlayer.setDataSource(fileName);
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                    if (playingListener!=null)
                        playingListener.onSongChanged(playlist.get(index));
                    runnable.start();
                    new Thread(runnable).start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                NotificationUtil.getStaticInstance(getApplicationContext()).sendPlayNotification(playlist.get(index));
{'fraction_non_alphanumeric': 0.08942457231726283, 'fraction_numerical': 0.004665629860031105, 'mean_word_length': 2.5767519466073416, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3747033', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1635, 'n_words': 384}