2015-03-31 22:44:41 +00:00
|
|
|
package org.thoughtcrime.securesms.components;
|
|
|
|
|
2015-05-05 22:14:41 +00:00
|
|
|
import android.annotation.TargetApi;
|
|
|
|
import android.app.Activity;
|
2015-03-31 22:44:41 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
2015-05-05 22:14:41 +00:00
|
|
|
import android.os.Build.VERSION;
|
|
|
|
import android.os.Build.VERSION_CODES;
|
2015-03-31 22:44:41 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
2015-06-27 03:14:51 +00:00
|
|
|
import android.view.animation.AlphaAnimation;
|
|
|
|
import android.view.animation.Animation;
|
|
|
|
import android.view.animation.Animation.AnimationListener;
|
|
|
|
import android.widget.FrameLayout;
|
2015-03-31 22:44:41 +00:00
|
|
|
|
2015-06-08 18:07:46 +00:00
|
|
|
import com.bumptech.glide.DrawableTypeRequest;
|
2015-03-31 22:44:41 +00:00
|
|
|
import com.bumptech.glide.GenericRequestBuilder;
|
|
|
|
import com.bumptech.glide.Glide;
|
|
|
|
import com.bumptech.glide.request.RequestListener;
|
|
|
|
import com.bumptech.glide.request.target.Target;
|
2015-06-29 15:49:32 +00:00
|
|
|
import com.makeramen.roundedimageview.RoundedImageView;
|
2015-06-27 03:14:51 +00:00
|
|
|
import com.pnikosis.materialishprogress.ProgressWheel;
|
2015-03-31 22:44:41 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2015-06-27 03:14:51 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.PartProgressEvent;
|
2015-03-31 22:44:41 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri;
|
|
|
|
import org.thoughtcrime.securesms.mms.Slide;
|
|
|
|
import org.thoughtcrime.securesms.mms.SlideDeck;
|
|
|
|
import org.thoughtcrime.securesms.util.FutureTaskListener;
|
|
|
|
import org.thoughtcrime.securesms.util.ListenableFutureTask;
|
2015-05-18 15:38:48 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2015-03-31 22:44:41 +00:00
|
|
|
|
2015-06-27 03:14:51 +00:00
|
|
|
import de.greenrobot.event.EventBus;
|
2015-03-31 22:44:41 +00:00
|
|
|
import ws.com.google.android.mms.pdu.PduPart;
|
|
|
|
|
2015-06-27 03:14:51 +00:00
|
|
|
public class ThumbnailView extends FrameLayout {
|
|
|
|
private static final String TAG = ThumbnailView.class.getSimpleName();
|
|
|
|
|
|
|
|
private boolean showProgress = true;
|
|
|
|
private RoundedImageView image;
|
|
|
|
private ProgressWheel progress;
|
2015-05-18 15:38:48 +00:00
|
|
|
|
2015-03-31 22:44:41 +00:00
|
|
|
private ListenableFutureTask<SlideDeck> slideDeckFuture = null;
|
|
|
|
private SlideDeckListener slideDeckListener = null;
|
|
|
|
private ThumbnailClickListener thumbnailClickListener = null;
|
2015-05-18 15:38:48 +00:00
|
|
|
private String slideId = null;
|
|
|
|
private Slide slide = null;
|
2015-03-31 22:44:41 +00:00
|
|
|
|
|
|
|
public ThumbnailView(Context context) {
|
2015-06-27 03:14:51 +00:00
|
|
|
this(context, null);
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ThumbnailView(Context context, AttributeSet attrs) {
|
2015-06-27 03:14:51 +00:00
|
|
|
this(context, attrs, 0);
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ThumbnailView(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
super(context, attrs, defStyle);
|
2015-06-27 03:14:51 +00:00
|
|
|
inflate(context, R.layout.thumbnail_view, this);
|
|
|
|
image = (RoundedImageView) findViewById(R.id.thumbnail_image);
|
|
|
|
progress = (ProgressWheel) findViewById(R.id.progress_wheel);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override protected void onAttachedToWindow() {
|
|
|
|
super.onAttachedToWindow();
|
|
|
|
EventBus.getDefault().registerSticky(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override protected void onDetachedFromWindow() {
|
|
|
|
super.onDetachedFromWindow();
|
|
|
|
EventBus.getDefault().unregister(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public void onEventAsync(final PartProgressEvent event) {
|
|
|
|
if (this.slide != null && event.partId.equals(this.slide.getPart().getPartId())) {
|
|
|
|
Util.runOnMain(new Runnable() {
|
|
|
|
@Override public void run() {
|
|
|
|
progress.setInstantProgress(((float) event.progress) / event.total);
|
|
|
|
if (event.progress >= event.total) animateOutProgress();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 15:38:48 +00:00
|
|
|
public void setImageResource(@Nullable MasterSecret masterSecret,
|
|
|
|
long id, long timestamp,
|
|
|
|
@NonNull ListenableFutureTask<SlideDeck> slideDeckFuture)
|
2015-03-31 22:44:41 +00:00
|
|
|
{
|
|
|
|
if (this.slideDeckFuture != null && this.slideDeckListener != null) {
|
|
|
|
this.slideDeckFuture.removeListener(this.slideDeckListener);
|
|
|
|
}
|
|
|
|
|
2015-05-18 15:38:48 +00:00
|
|
|
String slideId = id + "::" + timestamp;
|
|
|
|
|
|
|
|
if (!slideId.equals(this.slideId)) {
|
2015-07-10 19:13:31 +00:00
|
|
|
progress.setVisibility(GONE);
|
2015-06-27 03:14:51 +00:00
|
|
|
image.setImageDrawable(null);
|
2015-05-18 15:38:48 +00:00
|
|
|
this.slide = null;
|
|
|
|
this.slideId = slideId;
|
|
|
|
}
|
|
|
|
|
2015-03-31 22:44:41 +00:00
|
|
|
this.slideDeckListener = new SlideDeckListener(masterSecret);
|
|
|
|
this.slideDeckFuture = slideDeckFuture;
|
|
|
|
this.slideDeckFuture.addListener(this.slideDeckListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setImageResource(@NonNull Slide slide, @Nullable MasterSecret masterSecret) {
|
2015-06-27 03:14:51 +00:00
|
|
|
if (Util.equals(slide, this.slide)) {
|
|
|
|
Log.w(TAG, "Not loading resource, slide was identical");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!isContextValid()) {
|
|
|
|
Log.w(TAG, "Not loading resource, context is invalid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.slide = slide;
|
|
|
|
if (slide.isInProgress() && showProgress) {
|
|
|
|
progress.spin();
|
|
|
|
progress.setVisibility(VISIBLE);
|
2015-05-05 22:14:41 +00:00
|
|
|
} else {
|
2015-06-27 03:14:51 +00:00
|
|
|
progress.setVisibility(GONE);
|
2015-05-05 22:14:41 +00:00
|
|
|
}
|
2015-06-27 03:14:51 +00:00
|
|
|
buildGlideRequest(slide, masterSecret).into(image);
|
|
|
|
setOnClickListener(new ThumbnailClickDispatcher(thumbnailClickListener, slide));
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setThumbnailClickListener(ThumbnailClickListener listener) {
|
|
|
|
this.thumbnailClickListener = listener;
|
|
|
|
}
|
|
|
|
|
2015-05-31 05:10:40 +00:00
|
|
|
public void clear() {
|
|
|
|
if (isContextValid()) Glide.clear(this);
|
|
|
|
}
|
|
|
|
|
2015-06-27 03:14:51 +00:00
|
|
|
public void setShowProgress(boolean showProgress) {
|
|
|
|
this.showProgress = showProgress;
|
|
|
|
if (progress.getVisibility() == View.VISIBLE && !showProgress) {
|
|
|
|
animateOutProgress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 22:14:41 +00:00
|
|
|
@TargetApi(VERSION_CODES.JELLY_BEAN_MR1)
|
|
|
|
private boolean isContextValid() {
|
|
|
|
return !(getContext() instanceof Activity) ||
|
|
|
|
VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN_MR1 ||
|
|
|
|
!((Activity)getContext()).isDestroyed();
|
|
|
|
}
|
|
|
|
|
2015-03-31 22:44:41 +00:00
|
|
|
private GenericRequestBuilder buildGlideRequest(@NonNull Slide slide,
|
|
|
|
@Nullable MasterSecret masterSecret)
|
|
|
|
{
|
|
|
|
final GenericRequestBuilder builder;
|
2015-06-27 03:14:51 +00:00
|
|
|
if (slide.getThumbnailUri() != null) {
|
2015-03-31 22:44:41 +00:00
|
|
|
builder = buildThumbnailGlideRequest(slide, masterSecret);
|
|
|
|
} else {
|
|
|
|
builder = buildPlaceholderGlideRequest(slide);
|
|
|
|
}
|
|
|
|
|
2015-06-27 03:14:51 +00:00
|
|
|
if (slide.isInProgress() && showProgress) {
|
|
|
|
return builder;
|
|
|
|
} else {
|
|
|
|
return builder.error(R.drawable.ic_missing_thumbnail_picture);
|
|
|
|
}
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private GenericRequestBuilder buildThumbnailGlideRequest(Slide slide, MasterSecret masterSecret) {
|
|
|
|
|
|
|
|
final GenericRequestBuilder builder;
|
2015-06-08 18:07:46 +00:00
|
|
|
if (slide.isDraft()) builder = buildDraftGlideRequest(slide, masterSecret);
|
|
|
|
else builder = buildPartGlideRequest(slide, masterSecret);
|
2015-03-31 22:44:41 +00:00
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
2015-06-08 18:07:46 +00:00
|
|
|
private GenericRequestBuilder buildDraftGlideRequest(Slide slide, MasterSecret masterSecret) {
|
|
|
|
final DrawableTypeRequest<?> request;
|
|
|
|
if (masterSecret == null) request = Glide.with(getContext()).load(slide.getThumbnailUri());
|
|
|
|
else request = Glide.with(getContext()).load(new DecryptableUri(masterSecret, slide.getThumbnailUri()));
|
2015-04-16 05:38:33 +00:00
|
|
|
|
2015-06-08 18:07:46 +00:00
|
|
|
return request.asBitmap()
|
|
|
|
.fitCenter()
|
|
|
|
.listener(new PduThumbnailSetListener(slide.getPart()));
|
2015-04-16 05:38:33 +00:00
|
|
|
}
|
|
|
|
|
2015-06-08 18:07:46 +00:00
|
|
|
private GenericRequestBuilder buildPartGlideRequest(Slide slide, MasterSecret masterSecret) {
|
2015-03-31 22:44:41 +00:00
|
|
|
if (masterSecret == null) {
|
|
|
|
throw new IllegalStateException("null MasterSecret when loading non-draft thumbnail");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Glide.with(getContext()).load(new DecryptableUri(masterSecret, slide.getThumbnailUri()))
|
2015-06-27 03:14:51 +00:00
|
|
|
.centerCrop();
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private GenericRequestBuilder buildPlaceholderGlideRequest(Slide slide) {
|
|
|
|
return Glide.with(getContext()).load(slide.getPlaceholderRes(getContext().getTheme()))
|
|
|
|
.fitCenter()
|
|
|
|
.crossFade();
|
|
|
|
}
|
|
|
|
|
2015-06-27 03:14:51 +00:00
|
|
|
private void animateOutProgress() {
|
|
|
|
AlphaAnimation animation = new AlphaAnimation(1f, 0f);
|
|
|
|
animation.setDuration(200);
|
|
|
|
animation.setAnimationListener(new AnimationListener() {
|
|
|
|
@Override public void onAnimationStart(Animation animation) { }
|
|
|
|
@Override public void onAnimationRepeat(Animation animation) { }
|
|
|
|
@Override public void onAnimationEnd(Animation animation) {
|
|
|
|
progress.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
progress.startAnimation(animation);
|
|
|
|
}
|
|
|
|
|
2015-03-31 22:44:41 +00:00
|
|
|
private class SlideDeckListener implements FutureTaskListener<SlideDeck> {
|
|
|
|
private final MasterSecret masterSecret;
|
|
|
|
|
|
|
|
public SlideDeckListener(MasterSecret masterSecret) {
|
|
|
|
this.masterSecret = masterSecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSuccess(final SlideDeck slideDeck) {
|
|
|
|
if (slideDeck == null) return;
|
|
|
|
|
|
|
|
final Slide slide = slideDeck.getThumbnailSlide(getContext());
|
|
|
|
if (slide != null) {
|
2015-06-27 03:14:51 +00:00
|
|
|
Util.runOnMain(new Runnable() {
|
2015-03-31 22:44:41 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
setImageResource(slide, masterSecret);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2015-06-27 03:14:51 +00:00
|
|
|
Util.runOnMain(new Runnable() {
|
2015-03-31 22:44:41 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-05-18 15:38:48 +00:00
|
|
|
Log.w(TAG, "Resolved slide was null!");
|
|
|
|
setVisibility(View.GONE);
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Throwable error) {
|
|
|
|
Log.w(TAG, error);
|
2015-06-27 03:14:51 +00:00
|
|
|
Util.runOnMain(new Runnable() {
|
2015-03-31 22:44:41 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-05-18 15:38:48 +00:00
|
|
|
Log.w(TAG, "onFailure!");
|
|
|
|
setVisibility(View.GONE);
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface ThumbnailClickListener {
|
|
|
|
void onClick(View v, Slide slide);
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ThumbnailClickDispatcher implements View.OnClickListener {
|
|
|
|
private ThumbnailClickListener listener;
|
|
|
|
private Slide slide;
|
|
|
|
|
|
|
|
public ThumbnailClickDispatcher(ThumbnailClickListener listener, Slide slide) {
|
|
|
|
this.listener = listener;
|
|
|
|
this.slide = slide;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
2015-04-23 18:33:34 +00:00
|
|
|
if (listener != null) {
|
|
|
|
listener.onClick(view, slide);
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "onClick, but no thumbnail click listener attached.");
|
|
|
|
}
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 18:07:46 +00:00
|
|
|
private static class PduThumbnailSetListener implements RequestListener<Object, Bitmap> {
|
2015-03-31 22:44:41 +00:00
|
|
|
private PduPart part;
|
|
|
|
|
|
|
|
public PduThumbnailSetListener(@NonNull PduPart part) {
|
|
|
|
this.part = part;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-06-08 18:07:46 +00:00
|
|
|
public boolean onException(Exception e, Object model, Target<Bitmap> target, boolean isFirstResource) {
|
2015-03-31 22:44:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-06-08 18:07:46 +00:00
|
|
|
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
|
2015-03-31 22:44:41 +00:00
|
|
|
part.setThumbnail(resource);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-06-29 15:49:32 +00:00
|
|
|
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|