2015-06-04 20:21:20 +00:00
|
|
|
package org.thoughtcrime.securesms.components;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2016-12-14 18:21:14 +00:00
|
|
|
import android.graphics.Canvas;
|
2015-06-04 20:21:20 +00:00
|
|
|
import android.net.Uri;
|
2016-12-14 18:21:14 +00:00
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.support.annotation.Nullable;
|
2015-06-04 20:21:20 +00:00
|
|
|
import android.util.AttributeSet;
|
2016-12-14 18:21:14 +00:00
|
|
|
import android.util.Log;
|
|
|
|
import android.util.Pair;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.FrameLayout;
|
2015-06-04 20:21:20 +00:00
|
|
|
import android.widget.ImageView;
|
|
|
|
|
|
|
|
import com.bumptech.glide.Glide;
|
2016-10-17 02:05:07 +00:00
|
|
|
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
2015-07-15 20:42:59 +00:00
|
|
|
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
|
2016-12-14 18:21:14 +00:00
|
|
|
import com.bumptech.glide.request.RequestListener;
|
2015-07-15 20:42:59 +00:00
|
|
|
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
|
2016-12-14 18:21:14 +00:00
|
|
|
import com.bumptech.glide.request.target.Target;
|
|
|
|
import com.davemorrissey.labs.subscaleview.ImageSource;
|
|
|
|
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
|
2015-06-04 20:21:20 +00:00
|
|
|
|
2016-12-14 18:21:14 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
import org.thoughtcrime.securesms.components.subsampling.AttachmentBitmapDecoder;
|
|
|
|
import org.thoughtcrime.securesms.components.subsampling.AttachmentRegionDecoder;
|
2015-06-04 20:21:20 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri;
|
2016-12-14 18:21:14 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.PartAuthority;
|
|
|
|
import org.thoughtcrime.securesms.util.BitmapDecodingException;
|
|
|
|
import org.thoughtcrime.securesms.util.BitmapUtil;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2015-06-04 20:21:20 +00:00
|
|
|
|
|
|
|
import uk.co.senab.photoview.PhotoViewAttacher;
|
|
|
|
|
2016-12-14 18:21:14 +00:00
|
|
|
public class ZoomingImageView extends FrameLayout {
|
|
|
|
|
|
|
|
private static final String TAG = ZoomingImageView.class.getName();
|
|
|
|
|
|
|
|
private final ImageView imageView;
|
|
|
|
private final PhotoViewAttacher imageViewAttacher;
|
|
|
|
private final SubsamplingScaleImageView subsamplingImageView;
|
2015-06-04 20:21:20 +00:00
|
|
|
|
|
|
|
public ZoomingImageView(Context context) {
|
2016-12-14 18:21:14 +00:00
|
|
|
this(context, null);
|
2015-06-04 20:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ZoomingImageView(Context context, AttributeSet attrs) {
|
2016-12-14 18:21:14 +00:00
|
|
|
this(context, attrs, 0);
|
2015-06-04 20:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ZoomingImageView(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
super(context, attrs, defStyleAttr);
|
2016-12-14 18:21:14 +00:00
|
|
|
|
|
|
|
inflate(context, R.layout.zooming_image_view, this);
|
|
|
|
|
|
|
|
this.imageView = (ImageView) findViewById(R.id.image_view);
|
|
|
|
this.subsamplingImageView = (SubsamplingScaleImageView) findViewById(R.id.subsampling_image_view);
|
|
|
|
this.imageViewAttacher = new PhotoViewAttacher(imageView);
|
|
|
|
|
|
|
|
this.subsamplingImageView.setBitmapDecoderClass(AttachmentBitmapDecoder.class);
|
|
|
|
this.subsamplingImageView.setRegionDecoderClass(AttachmentRegionDecoder.class);
|
|
|
|
this.subsamplingImageView.setOrientation(SubsamplingScaleImageView.ORIENTATION_USE_EXIF);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setImageUri(final MasterSecret masterSecret, final Uri uri, final String contentType) {
|
|
|
|
final Context context = getContext();
|
|
|
|
final int maxTextureSize = BitmapUtil.getMaxTextureSize();
|
|
|
|
|
|
|
|
Log.w(TAG, "Max texture size: " + maxTextureSize);
|
|
|
|
|
|
|
|
new AsyncTask<Void, Void, Pair<Integer, Integer>>() {
|
|
|
|
@Override
|
|
|
|
protected @Nullable Pair<Integer, Integer> doInBackground(Void... params) {
|
|
|
|
if (contentType.equals("image/gif")) return null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
InputStream inputStream = PartAuthority.getAttachmentStream(context, masterSecret, uri);
|
|
|
|
return BitmapUtil.getDimensions(inputStream);
|
|
|
|
} catch (IOException | BitmapDecodingException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void onPostExecute(@Nullable Pair<Integer, Integer> dimensions) {
|
2017-01-07 15:39:19 +00:00
|
|
|
Log.w(TAG, "Dimensions: " + (dimensions == null ? "(null)" : dimensions.first + ", " + dimensions.second));
|
2016-12-14 18:21:14 +00:00
|
|
|
|
|
|
|
if (dimensions == null || (dimensions.first <= maxTextureSize && dimensions.second <= maxTextureSize)) {
|
|
|
|
Log.w(TAG, "Loading in standard image view...");
|
|
|
|
setImageViewUri(masterSecret, uri);
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Loading in subsampling image view...");
|
|
|
|
setSubsamplingImageViewUri(uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.execute();
|
2015-06-04 20:21:20 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 18:21:14 +00:00
|
|
|
private void setImageViewUri(MasterSecret masterSecret, Uri uri) {
|
|
|
|
subsamplingImageView.setVisibility(View.GONE);
|
|
|
|
imageView.setVisibility(View.VISIBLE);
|
|
|
|
|
2015-06-04 20:21:20 +00:00
|
|
|
Glide.with(getContext())
|
|
|
|
.load(new DecryptableUri(masterSecret, uri))
|
2016-10-17 02:05:07 +00:00
|
|
|
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
2015-06-04 20:21:20 +00:00
|
|
|
.dontTransform()
|
|
|
|
.dontAnimate()
|
2016-12-14 18:21:14 +00:00
|
|
|
.into(new GlideDrawableImageViewTarget(imageView) {
|
2015-07-15 20:42:59 +00:00
|
|
|
@Override protected void setResource(GlideDrawable resource) {
|
2015-06-04 20:21:20 +00:00
|
|
|
super.setResource(resource);
|
2016-12-14 18:21:14 +00:00
|
|
|
imageViewAttacher.update();
|
2015-06-04 20:21:20 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-12-14 18:21:14 +00:00
|
|
|
|
|
|
|
private void setSubsamplingImageViewUri(Uri uri) {
|
|
|
|
subsamplingImageView.setVisibility(View.VISIBLE);
|
|
|
|
imageView.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
subsamplingImageView.setImage(ImageSource.uri(uri));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void cleanup() {
|
|
|
|
imageView.setImageDrawable(null);
|
|
|
|
subsamplingImageView.recycle();
|
|
|
|
}
|
2015-06-04 20:21:20 +00:00
|
|
|
}
|