<commit_before>package com.heavyplayer.audioplayerrecorder.util;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import com.heavyplayer.audioplayerrecorder.widget.AudioPlayerLayout;
import com.heavyplayer.audioplayerrecorder.widget.PlayPauseImageButton;
import com.heavyplayer.audioplayerrecorder.widget.interface_.OnDetachListener;

import java.io.IOException;

public class AudioPlayerHandler implements
		SafeMediaPlayer.OnStartListener,
		MediaPlayer.OnCompletionListener,
		MediaPlayer.OnBufferingUpdateListener,
		MediaPlayer.OnErrorListener {
	public static final String TAG = AudioPlayerHandler.class.getSimpleName();

	private final static long PROGRESS_UPDATE_INTERVAL_MS = 200;

	private AudioManager mAudioManager;
	private AudioFocusChangeListener mAudioFocusChangeListener;

	private Uri mFileUri;

	private boolean mShowBufferIfPossible;

	private Handler mHandler;
	private ProgressUpdater mProgressUpdater;

	private SafeMediaPlayer mMediaPlayer;
	private Integer mBufferingCurrentPosition;

	private AudioPlayerLayout mView;
	private PlayPauseImageButton mButton;
	private SeekBar mSeekBar;

	public AudioPlayerHandler(Context context, Uri fileUri, boolean showBufferIfPossible, Handler handler) {
		mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

		mFileUri = fileUri;

		mShowBufferIfPossible = showBufferIfPossible;

		mHandler = handler;
		mProgressUpdater = new ProgressUpdater();

		create();
	}

	protected void create() {
		mMediaPlayer = new SafeMediaPlayer();
		mMediaPlayer.setOnStartListener(this);
		mMediaPlayer.setOnCompletionListener(this);
		mMediaPlayer.setOnBufferingUpdateListener(this);
		mMediaPlayer.setOnErrorListener(this);

		mBufferingCurrentPosition = null;

		configureRegisteredViews();
	}

	public void destroy() {
		if(mMediaPlayer != null) {
			try {
				mMediaPlayer.setOnStartListener(null);
				mMediaPlayer.setOnCompletionListener(null);
				mMediaPlayer.setOnBufferingUpdateListener(null);
				mMediaPlayer.setOnErrorListener(null);
				mMediaPlayer.stop();
				mMediaPlayer.reset();
				mMediaPlayer.release();
				mMediaPlayer = null;
			}
			catch(Exception e) {
				Log.w(TAG, e);
			}
		}

		mBufferingCurrentPosition = null;

		abandonAudioFocus();
	}

	protected void start(boolean gainAudioFocus, boolean updateButton) {
		if(gainAudioFocus)
			gainAudioFocus();

		if(!mMediaPlayer.isPrepared()) {
			try {
				mMediaPlayer.setDataSource(mFileUri.toString());
				mMediaPlayer.prepare();
			} catch (IOException e) {
				Log.w(TAG, e);
			}
		}

		mMediaPlayer.start();

		if(updateButton)
			updateButton(true);
	}

	protected void pause(boolean abandonAudioFocus, boolean updateButton) {
		mMediaPlayer.pause();

		if(updateButton)
			updateButton(false);

		if(abandonAudioFocus)
			abandonAudioFocus();
	}

	protected void seekTo(int msec) {
		mMediaPlayer.seekTo(msec);
	}

	protected void updateButton(boolean isPlaying) {
		if(mButton != null)
			mButton.setIsPlaying(isPlaying);
	}

	@Override
	public void onStart(MediaPlayer mp) {
		if(mView != null)
			mView.setTimeDuration(mp.getDuration());

		if(mSeekBar != null) {
			if(mSeekBar.getMax() != mp.getDuration()) {
				mSeekBar.setMax(mp.getDuration());
				mSeekBar.setProgress(mp.getCurrentPosition());
			}
			else if(mSeekBar.getProgress() != mp.getCurrentPosition()) {
				mSeekBar.setProgress(mp.getCurrentPosition());
			}
		}

		// Update seek bar.
		startSeekBarUpdate();
	}

	public void startSeekBarUpdate() {
		// Update seek bar.
		mHandler.removeCallbacks(mProgressUpdater);
		mHandler.post(mProgressUpdater);
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		// Updates seek bar.
		if(mSeekBar != null)
			mSeekBar.setProgress(mp.getDuration());

		updateButton(false);

		abandonAudioFocus();<commit_msg>Updates seek bar max once the media player is prepared<commit_after>package com.heavyplayer.audioplayerrecorder.util;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import com.heavyplayer.audioplayerrecorder.widget.AudioPlayerLayout;
import com.heavyplayer.audioplayerrecorder.widget.PlayPauseImageButton;
import com.heavyplayer.audioplayerrecorder.widget.interface_.OnDetachListener;

import java.io.IOException;

public class AudioPlayerHandler implements
		MediaPlayer.OnPreparedListener,
		SafeMediaPlayer.OnStartListener,
		MediaPlayer.OnCompletionListener,
		MediaPlayer.OnBufferingUpdateListener,
		MediaPlayer.OnErrorListener {
	public static final String TAG = AudioPlayerHandler.class.getSimpleName();

	private final static long PROGRESS_UPDATE_INTERVAL_MS = 200;

	private AudioManager mAudioManager;
	private AudioFocusChangeListener mAudioFocusChangeListener;

	private Uri mFileUri;

	private boolean mShowBufferIfPossible;

	private Handler mHandler;
	private ProgressUpdater mProgressUpdater;

	private SafeMediaPlayer mMediaPlayer;
	private Integer mBufferingCurrentPosition;

	private AudioPlayerLayout mView;
	private PlayPauseImageButton mButton;
	private SeekBar mSeekBar;

	public AudioPlayerHandler(Context context, Uri fileUri, boolean showBufferIfPossible, Handler handler) {
		mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

		mFileUri = fileUri;

		mShowBufferIfPossible = showBufferIfPossible;

		mHandler = handler;
		mProgressUpdater = new ProgressUpdater();

		create();
	}

	protected void create() {
		mMediaPlayer = new SafeMediaPlayer();
		mMediaPlayer.setOnPreparedListener(this);
		mMediaPlayer.setOnStartListener(this);
		mMediaPlayer.setOnCompletionListener(this);
		mMediaPlayer.setOnBufferingUpdateListener(this);
		mMediaPlayer.setOnErrorListener(this);

		mBufferingCurrentPosition = null;

		configureRegisteredViews();
	}

	public void destroy() {
		if(mMediaPlayer != null) {
			try {
				mMediaPlayer.setOnPreparedListener(null);
				mMediaPlayer.setOnStartListener(null);
				mMediaPlayer.setOnCompletionListener(null);
				mMediaPlayer.setOnBufferingUpdateListener(null);
				mMediaPlayer.setOnErrorListener(null);
				mMediaPlayer.stop();
				mMediaPlayer.reset();
				mMediaPlayer.release();
				mMediaPlayer = null;
			}
			catch(Exception e) {
				Log.w(TAG, e);
			}
		}

		mBufferingCurrentPosition = null;

		abandonAudioFocus();
	}

	protected void start(boolean gainAudioFocus, boolean updateButton) {
		if(gainAudioFocus)
			gainAudioFocus();

		if(!mMediaPlayer.isPrepared()) {
			try {
				mMediaPlayer.setDataSource(mFileUri.toString());
				mMediaPlayer.prepare();
			} catch (IOException e) {
				Log.w(TAG, e);
			}
		}

		mMediaPlayer.start();

		if(updateButton)
			updateButton(true);
	}

	protected void pause(boolean abandonAudioFocus, boolean updateButton) {
		mMediaPlayer.pause();

		if(updateButton)
			updateButton(false);

		if(abandonAudioFocus)
			abandonAudioFocus();
	}

	protected void seekTo(int msec) {
		mMediaPlayer.seekTo(msec);
	}

	protected void updateButton(boolean isPlaying) {
		if(mButton != null)
			mButton.setIsPlaying(isPlaying);
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		if(mView != null)
			mView.setTimeDuration(mp.getDuration());

		if(mSeekBar != null) {
			if(mSeekBar.getMax() != mp.getDuration()) {
				mSeekBar.setMax(mp.getDuration());
				mSeekBar.setProgress(mp.getCurrentPosition());
			}
			else if(mSeekBar.getProgress() != mp.getCurrentPosition()) {
				mSeekBar.setProgress(mp.getCurrentPosition());
			}
		}
	}

	@Override
	public void onStart(MediaPlayer mp) {
		// Update seek bar.
		startSeekBarUpdate();
	}

	public void startSeekBarUpdate() {
		// Update seek bar.
		mHandler.removeCallbacks(mProgressUpdater);
		mHandler.post(mProgressUpdater);
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		// Updates seek bar.
		if(mSeekBar != null)
			mSeekBar.setProgress(mp.getDuration());

		updateButton(false);

		abandonAudioFocus();
{'fraction_non_alphanumeric': 0.09722392075333251, 'fraction_numerical': 0.0007337654396477926, 'mean_word_length': 5.242748091603054, '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': '23488', 'n_tokens_mistral': 2856, 'n_tokens_neox': 2516, 'n_words': 458}