<commit_before>package app.ddf.danskdatahistoriskforening.item;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import app.ddf.danskdatahistoriskforening.App;
import app.ddf.danskdatahistoriskforening.R;
import app.ddf.danskdatahistoriskforening.dal.Item;

public class ItemDescriptionFragment extends Fragment implements ItemUpdater, View.OnClickListener, SeekBar.OnSeekBarChangeListener, View.OnFocusChangeListener {
    EditText itemDescription;

    ImageButton recButton;
    ImageButton playButton;
    ImageButton prevButton;
    ImageButton nextButton;

    TextView durText;
    TextView posText;
    TextView audioText;

    SeekBar seekBar;

    ArrayList<MediaPlayer> aps;

    Handler apHandler;

    private MediaPlayer currentAP;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_item_description, container, false);
        itemDescription = (EditText) layout.findViewById(R.id.itemDescription);
        itemDescription.setOnFocusChangeListener(this);

        Item item = ((ItemActivity) getActivity()).getItem();
        itemDescription.setText(item.getItemDescription());

        // AUDIO
        posText = (TextView) layout.findViewById(R.id.posText);
        durText = (TextView) layout.findViewById(R.id.durText);
        audioText = (TextView) layout.findViewById(R.id.audioText);

        recButton = (ImageButton) layout.findViewById(R.id.recButton);
        playButton = (ImageButton) layout.findViewById(R.id.playButton);
        nextButton = (ImageButton) layout.findViewById(R.id.nextButton);
        prevButton = (ImageButton) layout.findViewById(R.id.prevButton);

        seekBar = (SeekBar) layout.findViewById(R.id.seekBar);

        recButton.setOnClickListener(this);
        playButton.setOnClickListener(this);
        nextButton.setOnClickListener(this);
        prevButton.setOnClickListener(this);

        seekBar.setOnSeekBarChangeListener(this);

        apHandler = new Handler();
            resetAudioPlayer(); // sets mPlayer
        return layout;
    }

    @Override
    public void onResume() {
        super.onResume();
        resetAudioPlayer();
        updateItem(((ItemActivity) getActivity()).getItem());
    }

    @Override
    public void updateItem(Item item) {
        item.setItemDescription(itemDescription.getText().toString());
    }

    @Override
    public void onDetach() {
        super.onDetach();
        forcestopAudioPlayer();
        destroyAudioPlayer();
    }

    @Override
    public void onPause() {
        super.onPause();
        forcestopAudioPlayer();
        destroyAudioPlayer();
        updateItem(((ItemActivity) getActivity()).getItem());
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (fromUser) {
            seekTo(progress);
            setSB(getAPSCurrentPosition());
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onClick(View v) {
        if (v == recButton) {
            ((ItemActivity) getActivity()).startRecording();
        } else if(v == playButton){
            if (currentAP.isPlaying())
                pauseAP();
            else
                setStartAP();
        } else if(v == prevButton){
            if (currentAP.getCurrentPosition() > 0) {
                pauseAP();
                currentAP.seekTo(0);
                setSB(getAPSCurrentPosition());
            } else if (setMP(getPrevAP(), 0, false)) {
                pauseAP();
                setSB(getAPSCurrentPosition());
            }
        }else if(v == nextButton){
            if (setMP(getNextAP(), 0, currentAP.isPlaying()))
                setSB(getAPSCurrentPosition());
        }
    }
    
    private boolean getNextAP() {
        for (int i = 0; i < aps.size(); i++) {
            if (aps.get(i) == currentAP && i+1 < aps.size()) {
                return aps.get(i+1);
            }
        }
        return null;
    }

    private MediaPlayer getPrevAP() {<commit_msg>#84 the audio player works now there is a small rare bug that can happen when using the seekbar, but it is harmless.<commit_after>package app.ddf.danskdatahistoriskforening.item;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import app.ddf.danskdatahistoriskforening.App;
import app.ddf.danskdatahistoriskforening.R;
import app.ddf.danskdatahistoriskforening.dal.Item;

public class ItemDescriptionFragment extends Fragment implements ItemUpdater, View.OnClickListener, SeekBar.OnSeekBarChangeListener, View.OnFocusChangeListener {
    EditText itemDescription;

    ImageButton recButton;
    ImageButton playButton;
    ImageButton prevButton;
    ImageButton nextButton;

    TextView durText;
    TextView posText;
    TextView audioText;

    SeekBar seekBar;

    ArrayList<MediaPlayer> aps;

    Handler apHandler;

    private MediaPlayer currentAP;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_item_description, container, false);
        itemDescription = (EditText) layout.findViewById(R.id.itemDescription);
        itemDescription.setOnFocusChangeListener(this);

        Item item = ((ItemActivity) getActivity()).getItem();
        itemDescription.setText(item.getItemDescription());

        // AUDIO
        posText = (TextView) layout.findViewById(R.id.posText);
        durText = (TextView) layout.findViewById(R.id.durText);
        audioText = (TextView) layout.findViewById(R.id.audioText);

        recButton = (ImageButton) layout.findViewById(R.id.recButton);
        playButton = (ImageButton) layout.findViewById(R.id.playButton);
        nextButton = (ImageButton) layout.findViewById(R.id.nextButton);
        prevButton = (ImageButton) layout.findViewById(R.id.prevButton);

        seekBar = (SeekBar) layout.findViewById(R.id.seekBar);

        recButton.setOnClickListener(this);
        playButton.setOnClickListener(this);
        nextButton.setOnClickListener(this);
        prevButton.setOnClickListener(this);

        seekBar.setOnSeekBarChangeListener(this);

        apHandler = new Handler();
            resetAudioPlayer(); // sets mPlayer
        return layout;
    }

    @Override
    public void onResume() {
        super.onResume();
        resetAudioPlayer();
        updateItem(((ItemActivity) getActivity()).getItem());
    }

    @Override
    public void updateItem(Item item) {
        item.setItemDescription(itemDescription.getText().toString());
    }

    @Override
    public void onDetach() {
        super.onDetach();
        forcestopAudioPlayer();
        destroyAudioPlayer();
    }

    @Override
    public void onPause() {
        super.onPause();
        forcestopAudioPlayer();
        destroyAudioPlayer();
        updateItem(((ItemActivity) getActivity()).getItem());
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (fromUser) {
            seekTo(progress);
            setSB(getAPSCurrentPosition());
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onClick(View v) {
        if (v == recButton) {
            ((ItemActivity) getActivity()).startRecording();
        } else if(v == playButton){
            if (currentAP.isPlaying())
                pauseAP();
            else
                setStartAP();
        } else if(v == prevButton){
            if (currentAP.getCurrentPosition() > 0) {
                pauseAP();
                currentAP.seekTo(0);
                setSB(getAPSCurrentPosition());
            } else if (setMP(getPrevAP(), 0, false)) {
                pauseAP();
                setSB(getAPSCurrentPosition());
            }
        }else if(v == nextButton){
            if (setMP(getNextAP(), 0, currentAP.isPlaying()))
                setSB(getAPSCurrentPosition());
        }
    }
    
    private MediaPlayer getNextAP() {
        for (int i = 0; i < aps.size(); i++) {
            if (aps.get(i) == currentAP && i+1 < aps.size()) {
                return aps.get(i+1);
            }
        }
        return null;
    }

    private MediaPlayer getPrevAP() {
{'fraction_non_alphanumeric': 0.10635107636285746, 'fraction_numerical': 0.0019278140730427332, 'mean_word_length': 2.7232854864433813, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '2897180', 'n_tokens_mistral': 2689, 'n_tokens_neox': 2564, 'n_words': 524}