2015-03-31 22:44:41 +00:00
|
|
|
package org.thoughtcrime.securesms.components;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2015-07-15 20:42:59 +00:00
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.graphics.Color;
|
2015-10-16 20:59:40 +00:00
|
|
|
import android.net.Uri;
|
2015-03-31 22:44:41 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
2015-06-27 03:14:51 +00:00
|
|
|
import android.widget.FrameLayout;
|
2015-07-15 20:42:59 +00:00
|
|
|
import android.widget.ImageView;
|
2015-03-31 22:44:41 +00:00
|
|
|
|
2017-10-12 00:12:46 +00:00
|
|
|
import com.bumptech.glide.RequestBuilder;
|
2016-10-17 02:05:07 +00:00
|
|
|
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
2017-10-12 00:12:46 +00:00
|
|
|
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
|
|
|
|
import com.bumptech.glide.request.RequestOptions;
|
2015-03-31 22:44:41 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.database.AttachmentDatabase;
|
2015-03-31 22:44:41 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri;
|
2017-10-16 20:11:42 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideRequests;
|
2015-03-31 22:44:41 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.Slide;
|
2015-10-21 22:32:29 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.SlideClickListener;
|
2015-05-18 15:38:48 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2015-07-30 22:02:20 +00:00
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2015-03-31 22:44:41 +00:00
|
|
|
|
2017-10-12 00:12:46 +00:00
|
|
|
import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;
|
|
|
|
|
2015-06-27 03:14:51 +00:00
|
|
|
public class ThumbnailView extends FrameLayout {
|
2015-10-21 22:32:29 +00:00
|
|
|
|
2015-06-27 03:14:51 +00:00
|
|
|
private static final String TAG = ThumbnailView.class.getSimpleName();
|
|
|
|
|
2015-09-24 23:46:57 +00:00
|
|
|
private ImageView image;
|
2016-12-11 21:37:27 +00:00
|
|
|
private ImageView playOverlay;
|
2015-09-24 23:46:57 +00:00
|
|
|
private int backgroundColorHint;
|
|
|
|
private int radius;
|
|
|
|
private OnClickListener parentClickListener;
|
2015-05-18 15:38:48 +00:00
|
|
|
|
2015-10-21 22:32:29 +00:00
|
|
|
private Optional<TransferControlView> transferControls = Optional.absent();
|
|
|
|
private SlideClickListener thumbnailClickListener = null;
|
|
|
|
private SlideClickListener downloadClickListener = 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
|
|
|
}
|
|
|
|
|
2015-08-24 22:24:31 +00:00
|
|
|
public ThumbnailView(final Context context, AttributeSet attrs, int defStyle) {
|
2015-03-31 22:44:41 +00:00
|
|
|
super(context, attrs, defStyle);
|
2015-10-21 22:32:29 +00:00
|
|
|
|
2015-06-27 03:14:51 +00:00
|
|
|
inflate(context, R.layout.thumbnail_view, this);
|
2015-10-21 22:32:29 +00:00
|
|
|
|
2016-12-11 21:37:27 +00:00
|
|
|
this.radius = getResources().getDimensionPixelSize(R.dimen.message_bubble_corner_radius);
|
2018-01-25 03:17:44 +00:00
|
|
|
this.image = findViewById(R.id.thumbnail_image);
|
|
|
|
this.playOverlay = findViewById(R.id.play_overlay);
|
2015-09-24 23:46:57 +00:00
|
|
|
super.setOnClickListener(new ThumbnailClickDispatcher());
|
2015-09-09 18:55:06 +00:00
|
|
|
|
2015-07-15 20:42:59 +00:00
|
|
|
if (attrs != null) {
|
|
|
|
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ThumbnailView, 0, 0);
|
2017-04-22 23:29:26 +00:00
|
|
|
backgroundColorHint = typedArray.getColor(R.styleable.ThumbnailView_backgroundColorHint, Color.BLACK);
|
2015-07-15 20:42:59 +00:00
|
|
|
typedArray.recycle();
|
|
|
|
}
|
2015-06-27 03:14:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
@Override
|
|
|
|
public void setOnClickListener(OnClickListener l) {
|
2015-09-24 23:46:57 +00:00
|
|
|
parentClickListener = l;
|
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
@Override
|
|
|
|
public void setFocusable(boolean focusable) {
|
2015-09-24 23:46:57 +00:00
|
|
|
super.setFocusable(focusable);
|
|
|
|
if (transferControls.isPresent()) transferControls.get().setFocusable(focusable);
|
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
@Override
|
|
|
|
public void setClickable(boolean clickable) {
|
2015-09-24 22:55:17 +00:00
|
|
|
super.setClickable(clickable);
|
2015-09-24 23:46:57 +00:00
|
|
|
if (transferControls.isPresent()) transferControls.get().setClickable(clickable);
|
2015-09-24 22:55:17 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 01:08:44 +00:00
|
|
|
private TransferControlView getTransferControls() {
|
2015-09-24 23:46:57 +00:00
|
|
|
if (!transferControls.isPresent()) {
|
2018-01-25 03:17:44 +00:00
|
|
|
transferControls = Optional.of(ViewUtil.inflateStub(this, R.id.transfer_controls_stub));
|
2015-09-24 23:46:57 +00:00
|
|
|
}
|
|
|
|
return transferControls.get();
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
|
|
|
|
2015-07-15 20:42:59 +00:00
|
|
|
public void setBackgroundColorHint(int color) {
|
|
|
|
this.backgroundColorHint = color;
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public void setImageResource(@NonNull GlideRequests glideRequests, @NonNull Slide slide,
|
|
|
|
boolean showControls, boolean isPreview)
|
2017-10-16 20:11:42 +00:00
|
|
|
{
|
2015-11-02 21:55:16 +00:00
|
|
|
if (showControls) {
|
|
|
|
getTransferControls().setSlide(slide);
|
|
|
|
getTransferControls().setDownloadClickListener(new DownloadClickDispatcher());
|
|
|
|
} else if (transferControls.isPresent()) {
|
|
|
|
getTransferControls().setVisibility(View.GONE);
|
|
|
|
}
|
2015-10-21 22:32:29 +00:00
|
|
|
|
2017-04-20 04:23:57 +00:00
|
|
|
if (slide.getThumbnailUri() != null && slide.hasPlayOverlay() &&
|
|
|
|
(slide.getTransferState() == AttachmentDatabase.TRANSFER_PROGRESS_DONE || isPreview))
|
|
|
|
{
|
2016-12-11 21:37:27 +00:00
|
|
|
this.playOverlay.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
this.playOverlay.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
2015-06-27 03:14:51 +00:00
|
|
|
if (Util.equals(slide, this.slide)) {
|
2015-10-13 01:25:05 +00:00
|
|
|
Log.w(TAG, "Not re-loading slide " + slide.asAttachment().getDataUri());
|
2015-06-27 03:14:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-09-10 04:05:21 +00:00
|
|
|
|
2017-04-22 23:29:26 +00:00
|
|
|
if (this.slide != null && this.slide.getFastPreflightId() != null &&
|
|
|
|
this.slide.getFastPreflightId().equals(slide.getFastPreflightId()))
|
|
|
|
{
|
|
|
|
Log.w(TAG, "Not re-loading slide for fast preflight: " + slide.getFastPreflightId());
|
|
|
|
this.slide = slide;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
Log.w(TAG, "loading part with id " + slide.asAttachment().getDataUri()
|
2017-04-22 23:29:26 +00:00
|
|
|
+ ", progress " + slide.getTransferState() + ", fast preflight id: " +
|
|
|
|
slide.asAttachment().getFastPreflightId());
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
this.slide = slide;
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
if (slide.getThumbnailUri() != null) buildThumbnailGlideRequest(glideRequests, slide).into(image);
|
2017-10-16 20:11:42 +00:00
|
|
|
else if (slide.hasPlaceholder()) buildPlaceholderGlideRequest(glideRequests, slide).into(image);
|
|
|
|
else glideRequests.clear(image);
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public void setImageResource(@NonNull GlideRequests glideRequests, @NonNull Uri uri) {
|
2015-10-16 20:59:40 +00:00
|
|
|
if (transferControls.isPresent()) getTransferControls().setVisibility(View.GONE);
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
glideRequests.load(new DecryptableUri(uri))
|
2018-03-18 21:52:49 +00:00
|
|
|
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
|
2017-10-16 20:11:42 +00:00
|
|
|
.transform(new RoundedCorners(radius))
|
|
|
|
.transition(withCrossFade())
|
2017-11-01 22:56:44 +00:00
|
|
|
.centerCrop()
|
2017-10-16 20:11:42 +00:00
|
|
|
.into(image);
|
2015-10-16 20:59:40 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 22:32:29 +00:00
|
|
|
public void setThumbnailClickListener(SlideClickListener listener) {
|
2015-03-31 22:44:41 +00:00
|
|
|
this.thumbnailClickListener = listener;
|
|
|
|
}
|
|
|
|
|
2015-10-21 22:32:29 +00:00
|
|
|
public void setDownloadClickListener(SlideClickListener listener) {
|
2015-08-24 22:24:31 +00:00
|
|
|
this.downloadClickListener = listener;
|
|
|
|
}
|
|
|
|
|
2017-10-16 20:11:42 +00:00
|
|
|
public void clear(GlideRequests glideRequests) {
|
|
|
|
glideRequests.clear(image);
|
|
|
|
|
|
|
|
if (transferControls.isPresent()) {
|
|
|
|
getTransferControls().clear();
|
|
|
|
}
|
2015-10-21 22:32:19 +00:00
|
|
|
|
|
|
|
slide = null;
|
2015-05-31 05:10:40 +00:00
|
|
|
}
|
|
|
|
|
2015-09-05 00:33:22 +00:00
|
|
|
public void showProgressSpinner() {
|
2015-09-08 01:08:44 +00:00
|
|
|
getTransferControls().showProgressSpinner();
|
2015-09-05 00:33:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
private RequestBuilder buildThumbnailGlideRequest(@NonNull GlideRequests glideRequests, @NonNull Slide slide) {
|
|
|
|
RequestBuilder builder = glideRequests.load(new DecryptableUri(slide.getThumbnailUri()))
|
2018-03-18 21:52:49 +00:00
|
|
|
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
|
2017-10-16 20:11:42 +00:00
|
|
|
.transform(new RoundedCorners(radius))
|
2017-11-01 22:56:44 +00:00
|
|
|
.centerCrop()
|
2017-10-16 20:11:42 +00:00
|
|
|
.transition(withCrossFade());
|
2015-09-10 04:05:21 +00:00
|
|
|
|
|
|
|
if (slide.isInProgress()) return builder;
|
2017-10-12 00:12:46 +00:00
|
|
|
else return builder.apply(RequestOptions.errorOf(R.drawable.ic_missing_thumbnail_picture));
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
|
|
|
|
2017-10-16 20:11:42 +00:00
|
|
|
private RequestBuilder buildPlaceholderGlideRequest(@NonNull GlideRequests glideRequests, @NonNull Slide slide) {
|
|
|
|
return glideRequests.asBitmap()
|
|
|
|
.load(slide.getPlaceholderRes(getContext().getTheme()))
|
|
|
|
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
|
|
|
.fitCenter();
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 18:55:06 +00:00
|
|
|
private class ThumbnailClickDispatcher implements View.OnClickListener {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
2015-10-13 01:25:05 +00:00
|
|
|
if (thumbnailClickListener != null &&
|
|
|
|
slide != null &&
|
|
|
|
slide.asAttachment().getDataUri() != null &&
|
|
|
|
slide.getTransferState() == AttachmentDatabase.TRANSFER_PROGRESS_DONE)
|
2015-09-09 18:55:06 +00:00
|
|
|
{
|
|
|
|
thumbnailClickListener.onClick(view, slide);
|
2015-09-24 23:46:57 +00:00
|
|
|
} else if (parentClickListener != null) {
|
|
|
|
parentClickListener.onClick(view);
|
2015-09-09 18:55:06 +00:00
|
|
|
}
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
2015-09-09 18:55:06 +00:00
|
|
|
}
|
2015-03-31 22:44:41 +00:00
|
|
|
|
2015-09-09 18:55:06 +00:00
|
|
|
private class DownloadClickDispatcher implements View.OnClickListener {
|
2015-03-31 22:44:41 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
2015-09-09 18:55:06 +00:00
|
|
|
if (downloadClickListener != null && slide != null) {
|
|
|
|
downloadClickListener.onClick(view, slide);
|
2015-04-23 18:33:34 +00:00
|
|
|
}
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|