Support for tiling image view and large image viewing
Fixes #5949 Fixes #5574 Fixes #4380 // FREEBIEpull/1/head
parent
477589b092
commit
d2be49af42
@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<merge xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
tools:context="org.thoughtcrime.securesms.components.ZoomingImageView">
|
||||||
|
|
||||||
|
<ImageView android:id="@+id/image_view"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:visibility="visible"/>
|
||||||
|
|
||||||
|
<com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
|
||||||
|
android:id="@+id/subsampling_image_view"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:visibility="gone"/>
|
||||||
|
|
||||||
|
</merge>
|
@ -1,48 +1,131 @@
|
|||||||
package org.thoughtcrime.securesms.components;
|
package org.thoughtcrime.securesms.components;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Canvas;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.util.Pair;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.FrameLayout;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
|
|
||||||
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.Glide;
|
||||||
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
||||||
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
|
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
|
||||||
import com.bumptech.glide.request.target.BitmapImageViewTarget;
|
import com.bumptech.glide.request.RequestListener;
|
||||||
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
|
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
|
||||||
|
import com.bumptech.glide.request.target.Target;
|
||||||
|
import com.davemorrissey.labs.subscaleview.ImageSource;
|
||||||
|
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
|
||||||
|
|
||||||
|
import org.thoughtcrime.securesms.R;
|
||||||
|
import org.thoughtcrime.securesms.components.subsampling.AttachmentBitmapDecoder;
|
||||||
|
import org.thoughtcrime.securesms.components.subsampling.AttachmentRegionDecoder;
|
||||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||||
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri;
|
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri;
|
||||||
|
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;
|
||||||
|
|
||||||
import uk.co.senab.photoview.PhotoViewAttacher;
|
import uk.co.senab.photoview.PhotoViewAttacher;
|
||||||
|
|
||||||
public class ZoomingImageView extends ImageView {
|
public class ZoomingImageView extends FrameLayout {
|
||||||
private PhotoViewAttacher attacher = new PhotoViewAttacher(this);
|
|
||||||
|
private static final String TAG = ZoomingImageView.class.getName();
|
||||||
|
|
||||||
|
private final ImageView imageView;
|
||||||
|
private final PhotoViewAttacher imageViewAttacher;
|
||||||
|
private final SubsamplingScaleImageView subsamplingImageView;
|
||||||
|
|
||||||
public ZoomingImageView(Context context) {
|
public ZoomingImageView(Context context) {
|
||||||
super(context);
|
this(context, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ZoomingImageView(Context context, AttributeSet attrs) {
|
public ZoomingImageView(Context context, AttributeSet attrs) {
|
||||||
super(context, attrs);
|
this(context, attrs, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ZoomingImageView(Context context, AttributeSet attrs, int defStyleAttr) {
|
public ZoomingImageView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||||
super(context, attrs, defStyleAttr);
|
super(context, attrs, defStyleAttr);
|
||||||
|
|
||||||
|
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) {
|
||||||
|
Log.w(TAG, "Dimensions: " + dimensions.first + ", " + dimensions.second);
|
||||||
|
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setImageUri(MasterSecret masterSecret, Uri uri) {
|
private void setImageViewUri(MasterSecret masterSecret, Uri uri) {
|
||||||
|
subsamplingImageView.setVisibility(View.GONE);
|
||||||
|
imageView.setVisibility(View.VISIBLE);
|
||||||
|
|
||||||
Glide.with(getContext())
|
Glide.with(getContext())
|
||||||
.load(new DecryptableUri(masterSecret, uri))
|
.load(new DecryptableUri(masterSecret, uri))
|
||||||
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
||||||
.dontTransform()
|
.dontTransform()
|
||||||
.dontAnimate()
|
.dontAnimate()
|
||||||
.into(new GlideDrawableImageViewTarget(this) {
|
.into(new GlideDrawableImageViewTarget(imageView) {
|
||||||
@Override protected void setResource(GlideDrawable resource) {
|
@Override protected void setResource(GlideDrawable resource) {
|
||||||
super.setResource(resource);
|
super.setResource(resource);
|
||||||
attacher.update();
|
imageViewAttacher.update();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,52 @@
|
|||||||
|
package org.thoughtcrime.securesms.components.subsampling;
|
||||||
|
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
|
import android.graphics.Rect;
|
||||||
|
import android.net.Uri;
|
||||||
|
|
||||||
|
import com.davemorrissey.labs.subscaleview.decoder.ImageDecoder;
|
||||||
|
import com.davemorrissey.labs.subscaleview.decoder.SkiaImageDecoder;
|
||||||
|
|
||||||
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||||
|
import org.thoughtcrime.securesms.mms.PartAuthority;
|
||||||
|
import org.thoughtcrime.securesms.service.KeyCachingService;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
public class AttachmentBitmapDecoder implements ImageDecoder{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Bitmap decode(Context context, Uri uri) throws Exception {
|
||||||
|
if (!PartAuthority.isLocalUri(uri)) {
|
||||||
|
return new SkiaImageDecoder().decode(context, uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
MasterSecret masterSecret = KeyCachingService.getMasterSecret(context);
|
||||||
|
|
||||||
|
if (masterSecret == null) {
|
||||||
|
throw new IllegalStateException("Can't decode without secret");
|
||||||
|
}
|
||||||
|
|
||||||
|
InputStream inputStream = PartAuthority.getAttachmentStream(context, masterSecret, uri);
|
||||||
|
|
||||||
|
try {
|
||||||
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||||
|
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
|
||||||
|
|
||||||
|
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
|
||||||
|
|
||||||
|
if (bitmap == null) {
|
||||||
|
throw new RuntimeException("Skia image region decoder returned null bitmap - image format may not be supported");
|
||||||
|
}
|
||||||
|
|
||||||
|
return bitmap;
|
||||||
|
} finally {
|
||||||
|
if (inputStream != null) inputStream.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,95 @@
|
|||||||
|
package org.thoughtcrime.securesms.components.subsampling;
|
||||||
|
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
|
import android.graphics.BitmapRegionDecoder;
|
||||||
|
import android.graphics.Point;
|
||||||
|
import android.graphics.Rect;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.support.annotation.RequiresApi;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.davemorrissey.labs.subscaleview.decoder.ImageRegionDecoder;
|
||||||
|
import com.davemorrissey.labs.subscaleview.decoder.SkiaImageRegionDecoder;
|
||||||
|
|
||||||
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||||
|
import org.thoughtcrime.securesms.mms.PartAuthority;
|
||||||
|
import org.thoughtcrime.securesms.service.KeyCachingService;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
public class AttachmentRegionDecoder implements ImageRegionDecoder {
|
||||||
|
|
||||||
|
private static final String TAG = AttachmentRegionDecoder.class.getName();
|
||||||
|
|
||||||
|
private SkiaImageRegionDecoder passthrough;
|
||||||
|
|
||||||
|
private BitmapRegionDecoder bitmapRegionDecoder;
|
||||||
|
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.GINGERBREAD_MR1)
|
||||||
|
@Override
|
||||||
|
public Point init(Context context, Uri uri) throws Exception {
|
||||||
|
Log.w(TAG, "Init!");
|
||||||
|
if (!PartAuthority.isLocalUri(uri)) {
|
||||||
|
passthrough = new SkiaImageRegionDecoder();
|
||||||
|
return passthrough.init(context, uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
MasterSecret masterSecret = KeyCachingService.getMasterSecret(context);
|
||||||
|
|
||||||
|
if (masterSecret == null) {
|
||||||
|
throw new IllegalStateException("No master secret available...");
|
||||||
|
}
|
||||||
|
|
||||||
|
InputStream inputStream = PartAuthority.getAttachmentStream(context, masterSecret, uri);
|
||||||
|
|
||||||
|
this.bitmapRegionDecoder = BitmapRegionDecoder.newInstance(inputStream, false);
|
||||||
|
inputStream.close();
|
||||||
|
|
||||||
|
return new Point(bitmapRegionDecoder.getWidth(), bitmapRegionDecoder.getHeight());
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.GINGERBREAD_MR1)
|
||||||
|
@Override
|
||||||
|
public Bitmap decodeRegion(Rect rect, int sampleSize) {
|
||||||
|
Log.w(TAG, "Decode region: " + rect);
|
||||||
|
|
||||||
|
if (passthrough != null) {
|
||||||
|
return passthrough.decodeRegion(rect, sampleSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized(this) {
|
||||||
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||||
|
options.inSampleSize = sampleSize;
|
||||||
|
options.inPreferredConfig = Bitmap.Config.RGB_565;
|
||||||
|
|
||||||
|
Bitmap bitmap = bitmapRegionDecoder.decodeRegion(rect, options);
|
||||||
|
|
||||||
|
if (bitmap == null) {
|
||||||
|
throw new RuntimeException("Skia image decoder returned null bitmap - image format may not be supported");
|
||||||
|
}
|
||||||
|
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.GINGERBREAD_MR1)
|
||||||
|
public boolean isReady() {
|
||||||
|
Log.w(TAG, "isReady");
|
||||||
|
return (passthrough != null && passthrough.isReady()) ||
|
||||||
|
(bitmapRegionDecoder != null && !bitmapRegionDecoder.isRecycled());
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.GINGERBREAD_MR1)
|
||||||
|
public void recycle() {
|
||||||
|
if (passthrough != null) {
|
||||||
|
passthrough.recycle();
|
||||||
|
passthrough = null;
|
||||||
|
} else {
|
||||||
|
bitmapRegionDecoder.recycle();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue