2015-09-08 01:08:44 +00:00
|
|
|
package org.thoughtcrime.securesms.components;
|
|
|
|
|
2019-04-17 14:21:30 +00:00
|
|
|
import android.animation.LayoutTransition;
|
2015-09-08 01:08:44 +00:00
|
|
|
import android.content.Context;
|
|
|
|
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-08 01:08:44 +00:00
|
|
|
import android.util.AttributeSet;
|
2015-09-10 04:05:21 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.FrameLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2018-11-09 07:33:37 +00:00
|
|
|
import com.annimon.stream.Stream;
|
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;
|
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;
|
|
|
|
|
2019-09-09 05:04:29 +00:00
|
|
|
import network.loki.messenger.R;
|
|
|
|
|
2015-09-10 04:05:21 +00:00
|
|
|
public class TransferControlView extends FrameLayout {
|
|
|
|
|
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-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
|
|
|
|
2015-09-24 23:46:57 +00:00
|
|
|
setLongClickable(false);
|
2019-04-17 14:21:30 +00:00
|
|
|
ViewUtil.setBackground(this, ContextCompat.getDrawable(context, R.drawable.transfer_controls_background));
|
2015-09-10 04:05:21 +00:00
|
|
|
setVisibility(GONE);
|
2019-04-17 14:21:30 +00:00
|
|
|
setLayoutTransition(new LayoutTransition());
|
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);
|
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) {
|
2019-04-17 14:21:30 +00:00
|
|
|
if (current != null) {
|
|
|
|
current.setVisibility(GONE);
|
2015-09-10 04:05:21 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 14:21:30 +00:00
|
|
|
if (view != null) {
|
|
|
|
view.setVisibility(VISIBLE);
|
2015-09-10 04:05:21 +00:00
|
|
|
} else {
|
2019-04-17 14:21:30 +00:00
|
|
|
setVisibility(GONE);
|
2015-09-10 04:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
current = view;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|