2015-10-21 15:32:29 -07:00
|
|
|
package org.thoughtcrime.securesms.components;
|
|
|
|
|
2016-11-21 18:58:10 -08:00
|
|
|
import android.annotation.TargetApi;
|
2015-10-21 15:32:29 -07:00
|
|
|
import android.content.Context;
|
2016-11-21 18:58:10 -08:00
|
|
|
import android.content.res.ColorStateList;
|
2015-10-21 15:32:29 -07:00
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.PorterDuff;
|
2016-11-21 18:58:10 -08:00
|
|
|
import android.graphics.drawable.AnimatedVectorDrawable;
|
2015-10-21 15:32:29 -07:00
|
|
|
import android.os.Build;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.Log;
|
2017-01-13 03:13:28 +01:00
|
|
|
import android.view.MotionEvent;
|
2015-10-21 15:32:29 -07:00
|
|
|
import android.view.View;
|
2016-12-08 14:20:38 -08:00
|
|
|
import android.view.ViewGroup;
|
2015-10-21 15:32:29 -07:00
|
|
|
import android.widget.FrameLayout;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.SeekBar;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.pnikosis.materialishprogress.ProgressWheel;
|
|
|
|
|
2017-02-17 20:27:11 -08:00
|
|
|
import org.greenrobot.eventbus.EventBus;
|
|
|
|
import org.greenrobot.eventbus.Subscribe;
|
|
|
|
import org.greenrobot.eventbus.ThreadMode;
|
2015-10-21 15:32:29 -07:00
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
import org.thoughtcrime.securesms.audio.AudioSlidePlayer;
|
|
|
|
import org.thoughtcrime.securesms.database.AttachmentDatabase;
|
2015-11-02 14:32:02 -08:00
|
|
|
import org.thoughtcrime.securesms.events.PartProgressEvent;
|
2015-10-21 15:32:29 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.AudioSlide;
|
|
|
|
import org.thoughtcrime.securesms.mms.SlideClickListener;
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2015-10-23 16:23:00 -07:00
|
|
|
|
2015-10-21 15:32:29 -07:00
|
|
|
public class AudioView extends FrameLayout implements AudioSlidePlayer.Listener {
|
|
|
|
|
|
|
|
private static final String TAG = AudioView.class.getSimpleName();
|
|
|
|
|
|
|
|
private final @NonNull AnimatingToggle controlToggle;
|
2016-12-08 14:20:38 -08:00
|
|
|
private final @NonNull ViewGroup container;
|
2015-10-21 15:32:29 -07:00
|
|
|
private final @NonNull ImageView playButton;
|
|
|
|
private final @NonNull ImageView pauseButton;
|
|
|
|
private final @NonNull ImageView downloadButton;
|
|
|
|
private final @NonNull ProgressWheel downloadProgress;
|
|
|
|
private final @NonNull SeekBar seekBar;
|
|
|
|
private final @NonNull TextView timestamp;
|
|
|
|
|
|
|
|
private @Nullable SlideClickListener downloadListener;
|
|
|
|
private @Nullable AudioSlidePlayer audioSlidePlayer;
|
|
|
|
private int backwardsCounter;
|
|
|
|
|
|
|
|
public AudioView(Context context) {
|
|
|
|
this(context, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public AudioView(Context context, AttributeSet attrs) {
|
|
|
|
this(context, attrs, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public AudioView(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
|
inflate(context, R.layout.audio_view, this);
|
|
|
|
|
2016-12-08 14:20:38 -08:00
|
|
|
this.container = (ViewGroup) findViewById(R.id.audio_widget_container);
|
2015-10-21 15:32:29 -07:00
|
|
|
this.controlToggle = (AnimatingToggle) findViewById(R.id.control_toggle);
|
|
|
|
this.playButton = (ImageView) findViewById(R.id.play);
|
|
|
|
this.pauseButton = (ImageView) findViewById(R.id.pause);
|
|
|
|
this.downloadButton = (ImageView) findViewById(R.id.download);
|
|
|
|
this.downloadProgress = (ProgressWheel) findViewById(R.id.download_progress);
|
|
|
|
this.seekBar = (SeekBar) findViewById(R.id.seek);
|
|
|
|
this.timestamp = (TextView) findViewById(R.id.timestamp);
|
|
|
|
|
|
|
|
this.playButton.setOnClickListener(new PlayClickedListener());
|
|
|
|
this.pauseButton.setOnClickListener(new PauseClickedListener());
|
|
|
|
this.seekBar.setOnSeekBarChangeListener(new SeekBarModifiedListener());
|
|
|
|
|
2016-11-21 18:58:10 -08:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
this.playButton.setImageDrawable(context.getDrawable(R.drawable.play_icon));
|
|
|
|
this.pauseButton.setImageDrawable(context.getDrawable(R.drawable.pause_icon));
|
|
|
|
this.playButton.setBackground(context.getDrawable(R.drawable.ic_circle_fill_white_48dp));
|
|
|
|
this.pauseButton.setBackground(context.getDrawable(R.drawable.ic_circle_fill_white_48dp));
|
|
|
|
}
|
|
|
|
|
2015-10-21 15:32:29 -07:00
|
|
|
if (attrs != null) {
|
|
|
|
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AudioView, 0, 0);
|
2016-11-21 18:58:10 -08:00
|
|
|
setTint(typedArray.getColor(R.styleable.AudioView_foregroundTintColor, Color.WHITE),
|
|
|
|
typedArray.getColor(R.styleable.AudioView_backgroundTintColor, Color.WHITE));
|
2016-12-08 14:20:38 -08:00
|
|
|
container.setBackgroundColor(typedArray.getColor(R.styleable.AudioView_widgetBackground, Color.TRANSPARENT));
|
2015-10-21 15:32:29 -07:00
|
|
|
typedArray.recycle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-23 16:23:00 -07:00
|
|
|
@Override
|
|
|
|
protected void onAttachedToWindow() {
|
|
|
|
super.onAttachedToWindow();
|
2017-02-17 20:27:11 -08:00
|
|
|
if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);
|
2015-10-23 16:23:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDetachedFromWindow() {
|
|
|
|
super.onDetachedFromWindow();
|
|
|
|
EventBus.getDefault().unregister(this);
|
|
|
|
}
|
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
public void setAudio(final @NonNull AudioSlide audio,
|
2015-10-21 15:32:29 -07:00
|
|
|
final boolean showControls)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (showControls && audio.isPendingDownload()) {
|
|
|
|
controlToggle.displayQuick(downloadButton);
|
|
|
|
seekBar.setEnabled(false);
|
|
|
|
downloadButton.setOnClickListener(new DownloadClickedListener(audio));
|
|
|
|
if (downloadProgress.isSpinning()) downloadProgress.stopSpinning();
|
|
|
|
} else if (showControls && audio.getTransferState() == AttachmentDatabase.TRANSFER_PROGRESS_STARTED) {
|
|
|
|
controlToggle.displayQuick(downloadProgress);
|
|
|
|
seekBar.setEnabled(false);
|
|
|
|
downloadProgress.spin();
|
|
|
|
} else {
|
|
|
|
controlToggle.displayQuick(playButton);
|
|
|
|
seekBar.setEnabled(true);
|
|
|
|
if (downloadProgress.isSpinning()) downloadProgress.stopSpinning();
|
|
|
|
}
|
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
this.audioSlidePlayer = AudioSlidePlayer.createFor(getContext(), audio, this);
|
2015-10-21 15:32:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void cleanup() {
|
|
|
|
if (this.audioSlidePlayer != null && pauseButton.getVisibility() == View.VISIBLE) {
|
|
|
|
this.audioSlidePlayer.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setDownloadClickListener(@Nullable SlideClickListener listener) {
|
|
|
|
this.downloadListener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStart() {
|
2016-11-21 18:58:10 -08:00
|
|
|
if (this.pauseButton.getVisibility() != View.VISIBLE) {
|
|
|
|
togglePlayToPause();
|
|
|
|
}
|
2015-10-21 15:32:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStop() {
|
2016-11-21 18:58:10 -08:00
|
|
|
if (this.playButton.getVisibility() != View.VISIBLE) {
|
|
|
|
togglePauseToPlay();
|
|
|
|
}
|
2015-10-23 16:55:23 -07:00
|
|
|
|
|
|
|
if (seekBar.getProgress() + 5 >= seekBar.getMax()) {
|
|
|
|
backwardsCounter = 4;
|
|
|
|
onProgress(0.0, 0);
|
|
|
|
}
|
2015-10-21 15:32:29 -07:00
|
|
|
}
|
|
|
|
|
2017-01-13 03:13:28 +01:00
|
|
|
@Override
|
|
|
|
public void setFocusable(boolean focusable) {
|
|
|
|
super.setFocusable(focusable);
|
|
|
|
this.playButton.setFocusable(focusable);
|
|
|
|
this.pauseButton.setFocusable(focusable);
|
|
|
|
this.seekBar.setFocusable(focusable);
|
|
|
|
this.seekBar.setFocusableInTouchMode(focusable);
|
|
|
|
this.downloadButton.setFocusable(focusable);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setClickable(boolean clickable) {
|
|
|
|
super.setClickable(clickable);
|
|
|
|
this.playButton.setClickable(clickable);
|
|
|
|
this.pauseButton.setClickable(clickable);
|
|
|
|
this.seekBar.setClickable(clickable);
|
|
|
|
this.seekBar.setOnTouchListener(clickable ? null : new TouchIgnoringListener());
|
|
|
|
this.downloadButton.setClickable(clickable);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setEnabled(boolean enabled) {
|
|
|
|
super.setEnabled(enabled);
|
|
|
|
this.playButton.setEnabled(enabled);
|
|
|
|
this.pauseButton.setEnabled(enabled);
|
|
|
|
this.seekBar.setEnabled(enabled);
|
|
|
|
this.downloadButton.setEnabled(enabled);
|
|
|
|
}
|
|
|
|
|
2015-10-21 15:32:29 -07:00
|
|
|
@Override
|
|
|
|
public void onProgress(double progress, long millis) {
|
|
|
|
int seekProgress = (int)Math.floor(progress * this.seekBar.getMax());
|
|
|
|
|
|
|
|
if (seekProgress > seekBar.getProgress() || backwardsCounter > 3) {
|
|
|
|
backwardsCounter = 0;
|
|
|
|
this.seekBar.setProgress(seekProgress);
|
|
|
|
this.timestamp.setText(String.format("%02d:%02d",
|
|
|
|
TimeUnit.MILLISECONDS.toMinutes(millis),
|
|
|
|
TimeUnit.MILLISECONDS.toSeconds(millis)));
|
|
|
|
} else {
|
|
|
|
backwardsCounter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-21 18:58:10 -08:00
|
|
|
public void setTint(int foregroundTint, int backgroundTint) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
this.playButton.setBackgroundTintList(ColorStateList.valueOf(foregroundTint));
|
|
|
|
this.playButton.setImageTintList(ColorStateList.valueOf(backgroundTint));
|
|
|
|
this.pauseButton.setBackgroundTintList(ColorStateList.valueOf(foregroundTint));
|
|
|
|
this.pauseButton.setImageTintList(ColorStateList.valueOf(backgroundTint));
|
|
|
|
} else {
|
|
|
|
this.playButton.setColorFilter(foregroundTint, PorterDuff.Mode.SRC_IN);
|
|
|
|
this.pauseButton.setColorFilter(foregroundTint, PorterDuff.Mode.SRC_IN);
|
|
|
|
}
|
2015-10-21 15:32:29 -07:00
|
|
|
|
2016-11-21 18:58:10 -08:00
|
|
|
this.downloadButton.setColorFilter(foregroundTint, PorterDuff.Mode.SRC_IN);
|
|
|
|
this.downloadProgress.setBarColor(foregroundTint);
|
|
|
|
|
|
|
|
this.timestamp.setTextColor(foregroundTint);
|
|
|
|
this.seekBar.getProgressDrawable().setColorFilter(foregroundTint, PorterDuff.Mode.SRC_IN);
|
2015-10-21 15:32:29 -07:00
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
|
2016-11-21 18:58:10 -08:00
|
|
|
this.seekBar.getThumb().setColorFilter(foregroundTint, PorterDuff.Mode.SRC_IN);
|
2015-10-21 15:32:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private double getProgress() {
|
|
|
|
if (this.seekBar.getProgress() <= 0 || this.seekBar.getMax() <= 0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return (double)this.seekBar.getProgress() / (double)this.seekBar.getMax();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-21 18:58:10 -08:00
|
|
|
private void togglePlayToPause() {
|
|
|
|
controlToggle.displayQuick(pauseButton);
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
AnimatedVectorDrawable playToPauseDrawable = (AnimatedVectorDrawable)getContext().getDrawable(R.drawable.play_to_pause_animation);
|
|
|
|
pauseButton.setImageDrawable(playToPauseDrawable);
|
|
|
|
playToPauseDrawable.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void togglePauseToPlay() {
|
|
|
|
controlToggle.displayQuick(playButton);
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
AnimatedVectorDrawable pauseToPlayDrawable = (AnimatedVectorDrawable)getContext().getDrawable(R.drawable.pause_to_play_animation);
|
|
|
|
playButton.setImageDrawable(pauseToPlayDrawable);
|
|
|
|
pauseToPlayDrawable.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-21 15:32:29 -07:00
|
|
|
private class PlayClickedListener implements View.OnClickListener {
|
2016-11-21 18:58:10 -08:00
|
|
|
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
2015-10-21 15:32:29 -07:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
try {
|
|
|
|
Log.w(TAG, "playbutton onClick");
|
|
|
|
if (audioSlidePlayer != null) {
|
2016-11-21 18:58:10 -08:00
|
|
|
togglePlayToPause();
|
2015-10-21 15:32:29 -07:00
|
|
|
audioSlidePlayer.play(getProgress());
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class PauseClickedListener implements View.OnClickListener {
|
2016-11-21 18:58:10 -08:00
|
|
|
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
2015-10-21 15:32:29 -07:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Log.w(TAG, "pausebutton onClick");
|
|
|
|
if (audioSlidePlayer != null) {
|
2016-11-21 18:58:10 -08:00
|
|
|
togglePauseToPlay();
|
2015-10-21 15:32:29 -07:00
|
|
|
audioSlidePlayer.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class DownloadClickedListener implements View.OnClickListener {
|
|
|
|
private final @NonNull AudioSlide slide;
|
|
|
|
|
|
|
|
private DownloadClickedListener(@NonNull AudioSlide slide) {
|
|
|
|
this.slide = slide;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
if (downloadListener != null) downloadListener.onClick(v, slide);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class SeekBarModifiedListener implements SeekBar.OnSeekBarChangeListener {
|
|
|
|
@Override
|
|
|
|
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized void onStartTrackingTouch(SeekBar seekBar) {
|
|
|
|
if (audioSlidePlayer != null && pauseButton.getVisibility() == View.VISIBLE) {
|
|
|
|
audioSlidePlayer.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized void onStopTrackingTouch(SeekBar seekBar) {
|
|
|
|
try {
|
|
|
|
if (audioSlidePlayer != null && pauseButton.getVisibility() == View.VISIBLE) {
|
|
|
|
audioSlidePlayer.play(getProgress());
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 03:13:28 +01:00
|
|
|
private class TouchIgnoringListener implements OnTouchListener {
|
|
|
|
@Override
|
|
|
|
public boolean onTouch(View v, MotionEvent event) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 20:27:11 -08:00
|
|
|
@Subscribe(sticky = true, threadMode = ThreadMode.ASYNC)
|
2015-10-21 15:32:29 -07:00
|
|
|
public void onEventAsync(final PartProgressEvent event) {
|
|
|
|
if (audioSlidePlayer != null && event.attachment.equals(this.audioSlidePlayer.getAudioSlide().asAttachment())) {
|
|
|
|
Util.runOnMain(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
downloadProgress.setInstantProgress(((float) event.progress) / event.total);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|