<commit_before>package com.appspot.shiloh_ranch.fragments.sermons.audio;

import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import com.appspot.shiloh_ranch.R;
import com.appspot.shiloh_ranch.api.model.Sermon;

import java.io.IOException;

public class SermonService extends Service implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener {
    public static final String ACTION_PLAY = "com.appspot.shiloh_ranch.sermons.action.PLAY";
    public static final String ACTION_STOP = "com.appspot.shiloh_ranch.sermons.action.STOP";
    public static final String ACTION_CHANGE = "com.appspot.shiloh_ranch.sermons.action.CHANGE";
    public static final String ACTION_PAUSE = "com.appspot.shiloh_ranch.sermons.action.PAUSE";
    public static final String ACTION_PREPARED = "com.appspot.shiloh_ranch.sermons.action.PREPARED";

    private Sermon mCurrentSermon;
    private BroadcastReceiver mReceiver;
    private boolean mPlaybackReady;
    private MediaPlayer mMediaPlayer;

    // TODO: Handle audio focus & using wifi locks & and other fancy stuff

    public static Intent createChangeIntent(String entityKey, String audioLink, String title, String subtitle) {
        Intent intent = new Intent(SermonService.ACTION_CHANGE);
        intent.putExtra("KEY", entityKey);
        intent.putExtra("LINK", audioLink);
        intent.putExtra("TITLE", title);
        intent.putExtra("SUBTITLE", subtitle);
        return intent;
    }

    public static Intent createPlayIntent() {
        return new Intent(SermonService.ACTION_PLAY);
    }

    public static Intent createPauseIntent() {
        return new Intent(SermonService.ACTION_PAUSE);
    }

    private void handleIntent(Intent intent) {
        if (intent == null || intent.getAction() == null)
            return;
        String action = intent.getAction();
        if (action.equalsIgnoreCase(ACTION_PLAY)) {
            if (!mPlaybackReady) {
                Toast.makeText(this, getString(R.string.sermon_buffering), Toast.LENGTH_SHORT).show();
                return;
            }
            mMediaPlayer.start();
            updateNotification();
        } else if (action.equalsIgnoreCase(ACTION_PAUSE)) {
            mMediaPlayer.pause();
            updateNotification();
        } else if (action.equalsIgnoreCase(ACTION_CHANGE)) {
            mCurrentSermon = new Sermon();
            String key = intent.getStringExtra("KEY");
            if (key.equals(mCurrentSermon.getEntityKey())) {
                return;
            }
            mCurrentSermon.setEntityKey(key);
            mCurrentSermon.setTitle(intent.getStringExtra("TITLE"));
            mCurrentSermon.setAudioLink(intent.getStringExtra("LINK"));
            mCurrentSermon.setDate(intent.getStringExtra("SUBTITLE"));
            mPlaybackReady = false;
            if (mMediaPlayer == null) {
                mMediaPlayer = new MediaPlayer();
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mMediaPlayer.setOnPreparedListener(this);
                mMediaPlayer.setOnErrorListener(this);
                mMediaPlayer.setOnCompletionListener(this);
                mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
            } else {
                mMediaPlayer.reset();
            }
            try {
                mMediaPlayer.setDataSource(mCurrentSermon.getAudioLink());
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("SRCC", "Error loading sermon", e);
                Toast.makeText(this, getString(R.string.sermon_load_error), Toast.LENGTH_SHORT).show();
            }
            mMediaPlayer.prepareAsync();
            updateNotification();
        } else if (action.equalsIgnoreCase(ACTION_STOP)) {
            if (mMediaPlayer != null) {
                mMediaPlayer.stop();<commit_msg>Fixed HTML char codes being displayed in notification<commit_after>package com.appspot.shiloh_ranch.fragments.sermons.audio;

import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
import android.text.Html;
import android.util.Log;
import android.widget.Toast;

import com.appspot.shiloh_ranch.R;
import com.appspot.shiloh_ranch.api.model.Sermon;

import java.io.IOException;

public class SermonService extends Service implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener {
    public static final String ACTION_PLAY = "com.appspot.shiloh_ranch.sermons.action.PLAY";
    public static final String ACTION_STOP = "com.appspot.shiloh_ranch.sermons.action.STOP";
    public static final String ACTION_CHANGE = "com.appspot.shiloh_ranch.sermons.action.CHANGE";
    public static final String ACTION_PAUSE = "com.appspot.shiloh_ranch.sermons.action.PAUSE";
    public static final String ACTION_PREPARED = "com.appspot.shiloh_ranch.sermons.action.PREPARED";

    private Sermon mCurrentSermon;
    private BroadcastReceiver mReceiver;
    private boolean mPlaybackReady;
    private MediaPlayer mMediaPlayer;

    // TODO: Handle audio focus & using wifi locks & and other fancy stuff

    public static Intent createChangeIntent(String entityKey, String audioLink, String title, String subtitle) {
        Intent intent = new Intent(SermonService.ACTION_CHANGE);
        intent.putExtra("KEY", entityKey);
        intent.putExtra("LINK", audioLink);
        intent.putExtra("TITLE", title);
        intent.putExtra("SUBTITLE", subtitle);
        return intent;
    }

    public static Intent createPlayIntent() {
        return new Intent(SermonService.ACTION_PLAY);
    }

    public static Intent createPauseIntent() {
        return new Intent(SermonService.ACTION_PAUSE);
    }

    private void handleIntent(Intent intent) {
        if (intent == null || intent.getAction() == null)
            return;
        String action = intent.getAction();
        if (action.equalsIgnoreCase(ACTION_PLAY)) {
            if (!mPlaybackReady) {
                Toast.makeText(this, getString(R.string.sermon_buffering), Toast.LENGTH_SHORT).show();
                return;
            }
            mMediaPlayer.start();
            updateNotification();
        } else if (action.equalsIgnoreCase(ACTION_PAUSE)) {
            mMediaPlayer.pause();
            updateNotification();
        } else if (action.equalsIgnoreCase(ACTION_CHANGE)) {
            mCurrentSermon = new Sermon();
            String key = intent.getStringExtra("KEY");
            if (key.equals(mCurrentSermon.getEntityKey())) {
                return;
            }
            mCurrentSermon.setEntityKey(key);
            mCurrentSermon.setTitle(Html.fromHtml(intent.getStringExtra("TITLE")).toString());
            mCurrentSermon.setAudioLink(intent.getStringExtra("LINK"));
            mCurrentSermon.setDate(intent.getStringExtra("SUBTITLE"));
            mPlaybackReady = false;
            if (mMediaPlayer == null) {
                mMediaPlayer = new MediaPlayer();
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mMediaPlayer.setOnPreparedListener(this);
                mMediaPlayer.setOnErrorListener(this);
                mMediaPlayer.setOnCompletionListener(this);
                mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
            } else {
                mMediaPlayer.reset();
            }
            try {
                mMediaPlayer.setDataSource(mCurrentSermon.getAudioLink());
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("SRCC", "Error loading sermon", e);
                Toast.makeText(this, getString(R.string.sermon_load_error), Toast.LENGTH_SHORT).show();
            }
            mMediaPlayer.prepareAsync();
            updateNotification();
        } else if (action.equalsIgnoreCase(ACTION_STOP)) {
            if (mMediaPlayer != null) {
                mMediaPlayer.stop();
{'fraction_non_alphanumeric': 0.09603554340396446, 'fraction_numerical': 0.000227842333105491, 'mean_word_length': 2.8948535936113577, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2375765', 'n_tokens_mistral': 2430, 'n_tokens_neox': 2385, 'n_words': 448}