<commit_before>package scal.io.liger.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.VideoView;

import com.fima.cardsui.objects.Card;

import java.io.File;
import java.io.IOException;

import scal.io.liger.JsonHelper;
import scal.io.liger.Utility;
import scal.io.liger.model.CardModel;
import scal.io.liger.model.ClipCardModel;
import scal.io.liger.Constants;
import scal.io.liger.R;


public class ClipCardView extends Card {

    private ClipCardModel mCardModel;
    private Context mContext;
    private static final String MEDIA_PATH_KEY = "value";

    MediaController mMediaController;

    public ClipCardView(Context context, CardModel cardModel) {
        mContext = context;
        mCardModel = (ClipCardModel) cardModel;
    }

    @Override
    public View getCardContent(Context context) {
        if (mCardModel == null) {
            return null;
        }

        View view = LayoutInflater.from(context).inflate(R.layout.card_clip, null);
        final VideoView vvCardVideo = ((VideoView) view.findViewById(R.id.vv_card_video));
        final ImageView ivCardPhoto = ((ImageView) view.findViewById(R.id.iv_card_photo));
        TextView tvHeader = ((TextView) view.findViewById(R.id.tv_header));
        TextView tvType = ((TextView) view.findViewById(R.id.tv_type));
        Button btnRecord = ((Button) view.findViewById(R.id.btn_record_media));
        final ToggleButton btnAudioPlay = ((ToggleButton) view.findViewById(R.id.tb_card_audio));

        tvHeader.setText(mCardModel.getHeader());
        tvType.setText(mCardModel.getClipType());

        final String clipMedium = mCardModel.getClipMedium();
        final String cardMediaId = mCardModel.getStoryPathReference().getId() + "::" + mCardModel.getId() + "::" + MEDIA_PATH_KEY;

        //set up media display
        File mediaFile = getValidFile(mCardModel.getValueByKey(MEDIA_PATH_KEY), mCardModel.getExampleMediaPath());

        if (mediaFile == null) {
            String clipType = mCardModel.getClipType();

            if (clipType.equals(Constants.CHARACTER)) {
                ivCardPhoto.setImageDrawable(mContext.getResources().getDrawable(R.drawable.cliptype_close));
            } else if (clipType.equals(Constants.ACTION)) {
                ivCardPhoto.setImageDrawable(mContext.getResources().getDrawable(R.drawable.cliptype_medium));
            } else if (clipType.equals(Constants.RESULT)){
                ivCardPhoto.setImageDrawable(mContext.getResources().getDrawable(R.drawable.cliptype_long));
            } else {
                //TODO handle invalid clip type
                ivCardPhoto.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_launcher));
            }

            ivCardPhoto.setVisibility(View.VISIBLE);
        } else if (mediaFile.exists() && !mediaFile.isDirectory()) {
            if (clipMedium.equals(Constants.VIDEO)) {
                mMediaController = new MediaController(mContext);
                mMediaController.setAnchorView(vvCardVideo);

                Uri video = Uri.parse(mediaFile.getPath());
                vvCardVideo.setVideoURI(video);
                vvCardVideo.seekTo(5);
                vvCardVideo.setMediaController(mMediaController);

                //set up image as preview
                Bitmap videoFrame = Utility.getFrameFromVideo(video.getPath());
                if(null != videoFrame) {
                    ivCardPhoto.setImageBitmap(videoFrame);
                }

                ivCardPhoto.setVisibility(View.VISIBLE);
                ivCardPhoto.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        vvCardVideo.setVisibility(View.VISIBLE);
                        ivCardPhoto.setVisibility(View.GONE);
                        vvCardVideo.start();
                    }
                });

                //revert back to image on video completion
                vvCardVideo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    public void onCompletion(MediaPlayer mp) {
                        vvCardVideo.setVisibility(View.GONE);
                        ivCardPhoto.setVisibility(View.VISIBLE);
                    }
                });
            } else if (clipMedium.equals(Constants.PHOTO)) {
                Uri uri = Uri.parse(mediaFile.getPath());
                ivCardPhoto.setImageURI(uri);

                ivCardPhoto.setVisibility(View.VISIBLE);
            } else if (clipMedium.equals(Constants.AUDIO)) {
                Uri myUri = Uri.parse(mediaFile.getPath());
                final MediaPlayer mediaPlayer = new MediaPlayer();
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

                //set background image
                String clipType = mCardModel.getClipType();
                int drawable = R.drawable.ic_launcher;

                if (clipType.equals(Constants.CHARACTER)) {
                    drawable = R.drawable.cliptype_close;
                } else if (clipType.equals(Constants.ACTION)) {
                    drawable = R.drawable.cliptype_medium;
                } else if (clipType.equals(Constants.RESULT)){
                    drawable = R.drawable.cliptype_long;
                }
                ivCardPhoto.setImageDrawable(mContext.getResources().getDrawable(drawable));
                ivCardPhoto.setVisibility(View.VISIBLE);

                //set up media player
                try {
                    mediaPlayer.setDataSource(mContext, myUri);
                    mediaPlayer.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                btnAudioPlay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            mediaPlayer.seekTo(5);
                            mediaPlayer.start();
                        } else {
                            mediaPlayer.pause();
                        }
                    }
                });

                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer arg0) {
                        btnAudioPlay.setChecked(false);
                    }
                });

                mediaPlayer.seekTo(5);
                btnAudioPlay.setVisibility(View.VISIBLE);
            } else {
                //TODO handle invalid-medium error
            }
        }

        //set correct listener for record button
        btnRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = null;
                int requestId = -1;
<commit_msg>Added play button to video ClipCard.<commit_after>package scal.io.liger.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.VideoView;

import com.fima.cardsui.objects.Card;

import java.io.File;
import java.io.IOException;

import scal.io.liger.JsonHelper;
import scal.io.liger.Utility;
import scal.io.liger.model.CardModel;
import scal.io.liger.model.ClipCardModel;
import scal.io.liger.Constants;
import scal.io.liger.R;


public class ClipCardView extends Card {

    private ClipCardModel mCardModel;
    private Context mContext;
    private static final String MEDIA_PATH_KEY = "value";

    MediaController mMediaController;

    public ClipCardView(Context context, CardModel cardModel) {
        mContext = context;
        mCardModel = (ClipCardModel) cardModel;
    }

    @Override
    public View getCardContent(Context context) {
        if (mCardModel == null) {
            return null;
        }

        View view = LayoutInflater.from(context).inflate(R.layout.card_clip, null);
        final VideoView vvCardVideo = ((VideoView) view.findViewById(R.id.vv_card_video));
        final ImageView ivCardPhoto = ((ImageView) view.findViewById(R.id.iv_card_photo));
        TextView tvHeader = ((TextView) view.findViewById(R.id.tv_header));
        TextView tvType = ((TextView) view.findViewById(R.id.tv_type));
        Button btnRecord = ((Button) view.findViewById(R.id.btn_record_media));
        final ToggleButton btnMediaPlay = ((ToggleButton) view.findViewById(R.id.tb_card_audio));

        tvHeader.setText(mCardModel.getHeader());
        tvType.setText(mCardModel.getClipType());

        final String clipMedium = mCardModel.getClipMedium();
        final String cardMediaId = mCardModel.getStoryPathReference().getId() + "::" + mCardModel.getId() + "::" + MEDIA_PATH_KEY;

        //set up media display
        File mediaFile = getValidFile(mCardModel.getValueByKey(MEDIA_PATH_KEY), mCardModel.getExampleMediaPath());

        if (mediaFile == null) {
            String clipType = mCardModel.getClipType();

            if (clipType.equals(Constants.CHARACTER)) {
                ivCardPhoto.setImageDrawable(mContext.getResources().getDrawable(R.drawable.cliptype_close));
            } else if (clipType.equals(Constants.ACTION)) {
                ivCardPhoto.setImageDrawable(mContext.getResources().getDrawable(R.drawable.cliptype_medium));
            } else if (clipType.equals(Constants.RESULT)){
                ivCardPhoto.setImageDrawable(mContext.getResources().getDrawable(R.drawable.cliptype_long));
            } else {
                //TODO handle invalid clip type
                ivCardPhoto.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_launcher));
            }

            ivCardPhoto.setVisibility(View.VISIBLE);
        } else if (mediaFile.exists() && !mediaFile.isDirectory()) {
            if (clipMedium.equals(Constants.VIDEO)) {
                mMediaController = new MediaController(mContext);
                mMediaController.setAnchorView(vvCardVideo);

                Uri video = Uri.parse(mediaFile.getPath());
                vvCardVideo.setVideoURI(video);
                vvCardVideo.seekTo(5);
                vvCardVideo.setMediaController(mMediaController);

                //set up image as preview
                Bitmap videoFrame = Utility.getFrameFromVideo(video.getPath());
                if(null != videoFrame) {
                    ivCardPhoto.setImageBitmap(videoFrame);
                }

                ivCardPhoto.setVisibility(View.VISIBLE);
                btnMediaPlay.setVisibility(View.VISIBLE);
                btnMediaPlay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        vvCardVideo.setVisibility(View.VISIBLE);
                        ivCardPhoto.setVisibility(View.GONE);
                        btnMediaPlay.setVisibility(View.GONE);
                        vvCardVideo.start();
                    }
                });

                //revert back to image on video completion
                vvCardVideo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    public void onCompletion(MediaPlayer mp) {
                        vvCardVideo.setVisibility(View.GONE);
                        ivCardPhoto.setVisibility(View.VISIBLE);
                        btnMediaPlay.setVisibility(View.VISIBLE);
                        btnMediaPlay.setChecked(false);
                    }
                });
            } else if (clipMedium.equals(Constants.PHOTO)) {
                Uri uri = Uri.parse(mediaFile.getPath());
                ivCardPhoto.setImageURI(uri);
                ivCardPhoto.setVisibility(View.VISIBLE);
            } else if (clipMedium.equals(Constants.AUDIO)) {
                Uri myUri = Uri.parse(mediaFile.getPath());
                final MediaPlayer mediaPlayer = new MediaPlayer();
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

                //set background image
                String clipType = mCardModel.getClipType();
                int drawable = R.drawable.ic_launcher;

                if (clipType.equals(Constants.CHARACTER)) {
                    drawable = R.drawable.cliptype_close;
                } else if (clipType.equals(Constants.ACTION)) {
                    drawable = R.drawable.cliptype_medium;
                } else if (clipType.equals(Constants.RESULT)){
                    drawable = R.drawable.cliptype_long;
                }
                ivCardPhoto.setImageDrawable(mContext.getResources().getDrawable(drawable));
                ivCardPhoto.setVisibility(View.VISIBLE);

                //set up media player
                try {
                    mediaPlayer.setDataSource(mContext, myUri);
                    mediaPlayer.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                btnMediaPlay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            mediaPlayer.seekTo(5);
                            mediaPlayer.start();
                        } else {
                            mediaPlayer.pause();
                        }
                    }
                });

                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer arg0) {
                        btnMediaPlay.setChecked(false);
                    }
                });

                mediaPlayer.seekTo(5);
                btnMediaPlay.setVisibility(View.VISIBLE);
            } else {
                //TODO handle invalid-medium error
            }
        }

        //set correct listener for record button
        btnRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = null;
                int requestId = -1;

{'fraction_non_alphanumeric': 0.09230567226890757, 'fraction_numerical': 0.0006565126050420169, 'mean_word_length': 1.9980318834875024, 'pattern_counts': {'":': 4, '<': 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': '672754', 'n_tokens_mistral': 4048, 'n_tokens_neox': 3744, 'n_words': 667}