2015-09-08 01:08:44 +00:00
|
|
|
package org.thoughtcrime.securesms.components;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2015-09-18 00:53:09 +00:00
|
|
|
import android.graphics.PorterDuff.Mode;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.Build.VERSION;
|
|
|
|
import android.os.Build.VERSION_CODES;
|
2015-09-08 01:08:44 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2015-09-18 00:53:09 +00:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2015-09-10 04:05:21 +00:00
|
|
|
import android.support.v4.view.animation.FastOutSlowInInterpolator;
|
2015-09-08 01:08:44 +00:00
|
|
|
import android.util.AttributeSet;
|
2015-09-10 04:05:21 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.FrameLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2018-11-09 07:33:37 +00:00
|
|
|
import com.annimon.stream.Stream;
|
2015-09-10 04:05:21 +00:00
|
|
|
import com.nineoldandroids.animation.Animator;
|
|
|
|
import com.nineoldandroids.animation.ValueAnimator;
|
2015-09-08 01:08:44 +00:00
|
|
|
import com.pnikosis.materialishprogress.ProgressWheel;
|
|
|
|
|
2017-02-18 04:27:11 +00:00
|
|
|
import org.greenrobot.eventbus.EventBus;
|
|
|
|
import org.greenrobot.eventbus.Subscribe;
|
|
|
|
import org.greenrobot.eventbus.ThreadMode;
|
2015-09-08 01:08:44 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2018-11-09 07:33:37 +00:00
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.database.AttachmentDatabase;
|
2015-11-02 22:32:02 +00:00
|
|
|
import org.thoughtcrime.securesms.events.PartProgressEvent;
|
2015-09-08 01:08:44 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.Slide;
|
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
|
|
|
|
2018-11-09 07:33:37 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2015-09-10 04:05:21 +00:00
|
|
|
public class TransferControlView extends FrameLayout {
|
|
|
|
private static final int TRANSITION_MS = 300;
|
|
|
|
|
2018-11-09 07:33:37 +00:00
|
|
|
@Nullable private List<Slide> slides;
|
|
|
|
@Nullable private View current;
|
2015-09-10 04:05:21 +00:00
|
|
|
|
|
|
|
private final ProgressWheel progressWheel;
|
2018-11-09 07:33:37 +00:00
|
|
|
private final View downloadDetails;
|
|
|
|
private final TextView downloadDetailsText;
|
2015-09-10 04:05:21 +00:00
|
|
|
private final int contractedWidth;
|
|
|
|
private final int expandedWidth;
|
2015-09-08 01:08:44 +00:00
|
|
|
|
2018-11-09 07:33:37 +00:00
|
|
|
private final Map<Attachment, Float> downloadProgress;
|
|
|
|
|
2015-09-08 01:08:44 +00:00
|
|
|
public TransferControlView(Context context) {
|
|
|
|
this(context, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public TransferControlView(Context context, AttributeSet attrs) {
|
|
|
|
this(context, attrs, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public TransferControlView(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
|
inflate(context, R.layout.transfer_controls_view, this);
|
2015-09-18 00:53:09 +00:00
|
|
|
|
|
|
|
final Drawable background = ContextCompat.getDrawable(context, R.drawable.transfer_controls_background);
|
2015-11-14 01:46:35 +00:00
|
|
|
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
|
2015-09-18 00:53:09 +00:00
|
|
|
background.setColorFilter(0x66ffffff, Mode.MULTIPLY);
|
|
|
|
}
|
2015-09-24 23:46:57 +00:00
|
|
|
setLongClickable(false);
|
2015-09-18 00:53:09 +00:00
|
|
|
ViewUtil.setBackground(this, background);
|
2015-09-10 04:05:21 +00:00
|
|
|
setVisibility(GONE);
|
2015-09-18 00:53:09 +00:00
|
|
|
|
2018-11-09 07:33:37 +00:00
|
|
|
this.downloadProgress = new HashMap<>();
|
|
|
|
this.progressWheel = ViewUtil.findById(this, R.id.progress_wheel);
|
|
|
|
this.downloadDetails = ViewUtil.findById(this, R.id.download_details);
|
|
|
|
this.downloadDetailsText = ViewUtil.findById(this, R.id.download_details_text);
|
|
|
|
this.contractedWidth = getResources().getDimensionPixelSize(R.dimen.transfer_controls_contracted_width);
|
|
|
|
this.expandedWidth = getResources().getDimensionPixelSize(R.dimen.transfer_controls_expanded_width);
|
2015-09-08 01:08:44 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:46:57 +00:00
|
|
|
@Override
|
|
|
|
public void setFocusable(boolean focusable) {
|
|
|
|
super.setFocusable(focusable);
|
|
|
|
downloadDetails.setFocusable(focusable);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setClickable(boolean clickable) {
|
|
|
|
super.setClickable(clickable);
|
|
|
|
downloadDetails.setClickable(clickable);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onAttachedToWindow() {
|
2015-09-08 01:08:44 +00:00
|
|
|
super.onAttachedToWindow();
|
2017-02-18 04:27:11 +00:00
|
|
|
if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);
|
2015-09-08 01:08:44 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:46:57 +00:00
|
|
|
@Override
|
|
|
|
protected void onDetachedFromWindow() {
|
2015-09-08 01:08:44 +00:00
|
|
|
super.onDetachedFromWindow();
|
|
|
|
EventBus.getDefault().unregister(this);
|
|
|
|
}
|
|
|
|
|
2018-11-09 07:33:37 +00:00
|
|
|
public void setSlide(final @NonNull Slide slides) {
|
|
|
|
setSlides(Collections.singletonList(slides));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSlides(final @NonNull List<Slide> slides) {
|
|
|
|
if (slides.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("Must provide at least one slide.");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.slides = slides;
|
|
|
|
|
|
|
|
if (!isUpdateToExistingSet(slides)) {
|
|
|
|
downloadProgress.clear();
|
|
|
|
Stream.of(slides).forEach(s -> downloadProgress.put(s.asAttachment(), 0f));
|
2018-11-20 17:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (Slide slide : slides) {
|
|
|
|
if (slide.asAttachment().getTransferState() == AttachmentDatabase.TRANSFER_PROGRESS_DONE) {
|
|
|
|
downloadProgress.put(slide.asAttachment(), 1f);
|
2018-11-09 07:33:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (getTransferState(slides)) {
|
|
|
|
case AttachmentDatabase.TRANSFER_PROGRESS_STARTED:
|
|
|
|
showProgressSpinner(calculateProgress(downloadProgress));
|
|
|
|
break;
|
|
|
|
case AttachmentDatabase.TRANSFER_PROGRESS_PENDING:
|
|
|
|
case AttachmentDatabase.TRANSFER_PROGRESS_FAILED:
|
|
|
|
downloadDetailsText.setText(getDownloadText(this.slides));
|
|
|
|
display(downloadDetails);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
display(null);
|
|
|
|
break;
|
2015-09-08 01:08:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void showProgressSpinner() {
|
2018-11-09 07:33:37 +00:00
|
|
|
showProgressSpinner(calculateProgress(downloadProgress));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void showProgressSpinner(float progress) {
|
|
|
|
if (progress == 0) {
|
|
|
|
progressWheel.spin();
|
|
|
|
} else {
|
|
|
|
progressWheel.setInstantProgress(progress);
|
|
|
|
}
|
|
|
|
|
2015-09-08 01:08:44 +00:00
|
|
|
display(progressWheel);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setDownloadClickListener(final @Nullable OnClickListener listener) {
|
2015-09-10 04:05:21 +00:00
|
|
|
downloadDetails.setOnClickListener(listener);
|
2015-09-08 01:08:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void clear() {
|
2015-09-10 04:05:21 +00:00
|
|
|
clearAnimation();
|
|
|
|
setVisibility(GONE);
|
|
|
|
if (current != null) {
|
|
|
|
current.clearAnimation();
|
|
|
|
current.setVisibility(GONE);
|
|
|
|
}
|
|
|
|
current = null;
|
2018-11-09 07:33:37 +00:00
|
|
|
slides = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setShowDownloadText(boolean showDownloadText) {
|
|
|
|
downloadDetailsText.setVisibility(showDownloadText ? VISIBLE : GONE);
|
2019-01-15 08:41:05 +00:00
|
|
|
forceLayout();
|
2018-11-09 07:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isUpdateToExistingSet(@NonNull List<Slide> slides) {
|
|
|
|
if (slides.size() != downloadProgress.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Slide slide : slides) {
|
|
|
|
if (!downloadProgress.containsKey(slide.asAttachment())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getTransferState(@NonNull List<Slide> slides) {
|
|
|
|
int transferState = AttachmentDatabase.TRANSFER_PROGRESS_DONE;
|
|
|
|
for (Slide slide : slides) {
|
|
|
|
if (slide.getTransferState() == AttachmentDatabase.TRANSFER_PROGRESS_PENDING && transferState == AttachmentDatabase.TRANSFER_PROGRESS_DONE) {
|
|
|
|
transferState = slide.getTransferState();
|
|
|
|
} else {
|
|
|
|
transferState = Math.max(transferState, slide.getTransferState());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return transferState;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getDownloadText(@NonNull List<Slide> slides) {
|
|
|
|
if (slides.size() == 1) {
|
|
|
|
return slides.get(0).getContentDescription();
|
|
|
|
} else {
|
|
|
|
int downloadCount = Stream.of(slides).reduce(0, (count, slide) -> slide.getTransferState() != AttachmentDatabase.TRANSFER_PROGRESS_DONE ? count + 1 : count);
|
|
|
|
return getContext().getResources().getQuantityString(R.plurals.TransferControlView_n_items, downloadCount, downloadCount);
|
|
|
|
}
|
2015-09-10 04:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void display(@Nullable final View view) {
|
2018-11-09 07:33:37 +00:00
|
|
|
final int sourceWidth = (current == downloadDetails && downloadDetailsText.getVisibility() == VISIBLE) ? expandedWidth : contractedWidth;
|
|
|
|
final int targetWidth = (view == downloadDetails && downloadDetailsText.getVisibility() == VISIBLE) ? expandedWidth : contractedWidth;
|
2015-09-10 04:05:21 +00:00
|
|
|
|
|
|
|
if (current == view || current == null) {
|
|
|
|
ViewGroup.LayoutParams layoutParams = getLayoutParams();
|
|
|
|
layoutParams.width = targetWidth;
|
|
|
|
setLayoutParams(layoutParams);
|
|
|
|
} else {
|
2015-09-18 00:53:09 +00:00
|
|
|
ViewUtil.fadeOut(current, TRANSITION_MS);
|
2015-09-10 04:05:21 +00:00
|
|
|
Animator anim = getWidthAnimator(sourceWidth, targetWidth);
|
|
|
|
anim.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (view == null) {
|
2015-09-18 00:53:09 +00:00
|
|
|
ViewUtil.fadeOut(this, TRANSITION_MS);
|
2015-09-10 04:05:21 +00:00
|
|
|
} else {
|
2015-09-18 00:53:09 +00:00
|
|
|
ViewUtil.fadeIn(this, TRANSITION_MS);
|
|
|
|
ViewUtil.fadeIn(view, TRANSITION_MS);
|
2015-09-10 04:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
current = view;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Animator getWidthAnimator(final int from, final int to) {
|
|
|
|
final ValueAnimator anim = ValueAnimator.ofInt(from, to);
|
2018-11-09 07:33:37 +00:00
|
|
|
anim.addUpdateListener(animation -> {
|
|
|
|
final int val = (Integer)animation.getAnimatedValue();
|
|
|
|
final ViewGroup.LayoutParams layoutParams = getLayoutParams();
|
|
|
|
layoutParams.width = val;
|
|
|
|
setLayoutParams(layoutParams);
|
2015-09-10 04:05:21 +00:00
|
|
|
});
|
|
|
|
anim.setInterpolator(new FastOutSlowInInterpolator());
|
|
|
|
anim.setDuration(TRANSITION_MS);
|
|
|
|
return anim;
|
2015-09-08 01:08:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 07:33:37 +00:00
|
|
|
private float calculateProgress(@NonNull Map<Attachment, Float> downloadProgress) {
|
|
|
|
float totalProgress = 0;
|
|
|
|
for (float progress : downloadProgress.values()) {
|
|
|
|
totalProgress += progress / downloadProgress.size();
|
|
|
|
}
|
|
|
|
return totalProgress;
|
|
|
|
}
|
|
|
|
|
2019-04-10 16:13:10 +00:00
|
|
|
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
|
2015-09-08 01:08:44 +00:00
|
|
|
public void onEventAsync(final PartProgressEvent event) {
|
2018-11-09 07:33:37 +00:00
|
|
|
if (downloadProgress.containsKey(event.attachment)) {
|
2019-04-10 16:13:10 +00:00
|
|
|
downloadProgress.put(event.attachment, ((float) event.progress) / event.total);
|
|
|
|
progressWheel.setInstantProgress(calculateProgress(downloadProgress));
|
2015-09-08 01:08:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|