2015-09-08 03:08:44 +02:00
|
|
|
package org.thoughtcrime.securesms.components;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2015-09-18 02:53:09 +02: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 03:08:44 +02:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2015-09-18 02:53:09 +02:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2015-09-10 06:05:21 +02:00
|
|
|
import android.support.v4.view.animation.FastOutSlowInInterpolator;
|
2015-09-08 03:08:44 +02:00
|
|
|
import android.util.AttributeSet;
|
2015-09-10 06:05:21 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.FrameLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.nineoldandroids.animation.Animator;
|
|
|
|
import com.nineoldandroids.animation.ValueAnimator;
|
|
|
|
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;
|
2015-09-08 03:08:44 +02:00
|
|
|
import com.pnikosis.materialishprogress.ProgressWheel;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
2015-10-13 03:25:05 +02:00
|
|
|
import org.thoughtcrime.securesms.database.AttachmentDatabase;
|
2015-11-02 23:32:02 +01:00
|
|
|
import org.thoughtcrime.securesms.events.PartProgressEvent;
|
2015-09-08 03:08:44 +02:00
|
|
|
import org.thoughtcrime.securesms.mms.Slide;
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
|
|
|
|
|
|
|
import de.greenrobot.event.EventBus;
|
|
|
|
|
2015-09-10 06:05:21 +02:00
|
|
|
public class TransferControlView extends FrameLayout {
|
|
|
|
private static final int TRANSITION_MS = 300;
|
|
|
|
|
|
|
|
@Nullable private Slide slide;
|
|
|
|
@Nullable private View current;
|
|
|
|
|
|
|
|
private final ProgressWheel progressWheel;
|
|
|
|
private final TextView downloadDetails;
|
|
|
|
private final int contractedWidth;
|
|
|
|
private final int expandedWidth;
|
2015-09-08 03:08:44 +02: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 02:53:09 +02:00
|
|
|
|
|
|
|
final Drawable background = ContextCompat.getDrawable(context, R.drawable.transfer_controls_background);
|
2015-11-14 02:46:35 +01:00
|
|
|
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
|
2015-09-18 02:53:09 +02:00
|
|
|
background.setColorFilter(0x66ffffff, Mode.MULTIPLY);
|
|
|
|
}
|
2015-09-25 01:46:57 +02:00
|
|
|
setLongClickable(false);
|
2015-09-18 02:53:09 +02:00
|
|
|
ViewUtil.setBackground(this, background);
|
2015-09-10 06:05:21 +02:00
|
|
|
setVisibility(GONE);
|
2015-09-18 02:53:09 +02:00
|
|
|
|
2015-09-10 06:05:21 +02:00
|
|
|
this.progressWheel = ViewUtil.findById(this, R.id.progress_wheel);
|
|
|
|
this.downloadDetails = ViewUtil.findById(this, R.id.download_details);
|
|
|
|
this.contractedWidth = getResources().getDimensionPixelSize(R.dimen.transfer_controls_contracted_width);
|
|
|
|
this.expandedWidth = getResources().getDimensionPixelSize(R.dimen.transfer_controls_expanded_width);
|
2015-09-08 03:08:44 +02:00
|
|
|
}
|
|
|
|
|
2015-09-25 01:46:57 +02: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 03:08:44 +02:00
|
|
|
super.onAttachedToWindow();
|
|
|
|
if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().registerSticky(this);
|
|
|
|
}
|
|
|
|
|
2015-09-25 01:46:57 +02:00
|
|
|
@Override
|
|
|
|
protected void onDetachedFromWindow() {
|
2015-09-08 03:08:44 +02:00
|
|
|
super.onDetachedFromWindow();
|
|
|
|
EventBus.getDefault().unregister(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSlide(final @NonNull Slide slide) {
|
|
|
|
this.slide = slide;
|
2015-10-13 03:25:05 +02:00
|
|
|
if (slide.getTransferState() == AttachmentDatabase.TRANSFER_PROGRESS_STARTED) {
|
2015-09-08 03:08:44 +02:00
|
|
|
showProgressSpinner();
|
|
|
|
} else if (slide.isPendingDownload()) {
|
2015-09-10 06:05:21 +02:00
|
|
|
downloadDetails.setText(slide.getContentDescription());
|
|
|
|
display(downloadDetails);
|
2015-09-08 03:08:44 +02:00
|
|
|
} else {
|
2015-09-10 06:05:21 +02:00
|
|
|
display(null);
|
2015-09-08 03:08:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void showProgressSpinner() {
|
|
|
|
progressWheel.spin();
|
|
|
|
display(progressWheel);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setDownloadClickListener(final @Nullable OnClickListener listener) {
|
2015-09-10 06:05:21 +02:00
|
|
|
downloadDetails.setOnClickListener(listener);
|
2015-09-08 03:08:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void clear() {
|
2015-09-10 06:05:21 +02:00
|
|
|
clearAnimation();
|
|
|
|
setVisibility(GONE);
|
|
|
|
if (current != null) {
|
|
|
|
current.clearAnimation();
|
|
|
|
current.setVisibility(GONE);
|
|
|
|
}
|
|
|
|
current = null;
|
|
|
|
slide = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void display(@Nullable final View view) {
|
|
|
|
final int sourceWidth = current == downloadDetails ? expandedWidth : contractedWidth;
|
|
|
|
final int targetWidth = view == downloadDetails ? expandedWidth : contractedWidth;
|
|
|
|
|
|
|
|
if (current == view || current == null) {
|
|
|
|
ViewGroup.LayoutParams layoutParams = getLayoutParams();
|
|
|
|
layoutParams.width = targetWidth;
|
|
|
|
setLayoutParams(layoutParams);
|
|
|
|
} else {
|
2015-09-18 02:53:09 +02:00
|
|
|
ViewUtil.fadeOut(current, TRANSITION_MS);
|
2015-09-10 06:05:21 +02:00
|
|
|
Animator anim = getWidthAnimator(sourceWidth, targetWidth);
|
|
|
|
anim.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (view == null) {
|
2015-09-18 02:53:09 +02:00
|
|
|
ViewUtil.fadeOut(this, TRANSITION_MS);
|
2015-09-10 06:05:21 +02:00
|
|
|
} else {
|
2015-09-18 02:53:09 +02:00
|
|
|
ViewUtil.fadeIn(this, TRANSITION_MS);
|
|
|
|
ViewUtil.fadeIn(view, TRANSITION_MS);
|
2015-09-10 06:05:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
current = view;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Animator getWidthAnimator(final int from, final int to) {
|
|
|
|
final ValueAnimator anim = ValueAnimator.ofInt(from, to);
|
|
|
|
anim.addUpdateListener(new AnimatorUpdateListener() {
|
2015-09-25 01:46:57 +02:00
|
|
|
@Override
|
|
|
|
public void onAnimationUpdate(ValueAnimator animation) {
|
2015-09-10 06:05:21 +02:00
|
|
|
final int val = (Integer)animation.getAnimatedValue();
|
|
|
|
final ViewGroup.LayoutParams layoutParams = getLayoutParams();
|
|
|
|
layoutParams.width = val;
|
|
|
|
setLayoutParams(layoutParams);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
anim.setInterpolator(new FastOutSlowInInterpolator());
|
|
|
|
anim.setDuration(TRANSITION_MS);
|
|
|
|
return anim;
|
2015-09-08 03:08:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public void onEventAsync(final PartProgressEvent event) {
|
2015-10-13 03:25:05 +02:00
|
|
|
if (this.slide != null && event.attachment.equals(this.slide.asAttachment())) {
|
2015-09-08 03:08:44 +02:00
|
|
|
Util.runOnMain(new Runnable() {
|
2015-09-25 01:46:57 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-09-08 03:08:44 +02:00
|
|
|
progressWheel.setInstantProgress(((float)event.progress) / event.total);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|