Be more explicit with mastersecret passing

fork-5.53.8
Moxie Marlinspike 2017-11-27 11:13:38 -08:00
rodzic d5cb804f90
commit ed508a8def
3 zmienionych plików z 51 dodań i 23 usunięć

Wyświetl plik

@ -1,5 +1,6 @@
package org.thoughtcrime.securesms.components;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
@ -15,6 +16,7 @@ import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.Target;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.davemorrissey.labs.subscaleview.decoder.DecoderFactory;
import com.github.chrisbanes.photoview.PhotoView;
import org.thoughtcrime.securesms.R;
@ -55,11 +57,10 @@ public class ZoomingImageView extends FrameLayout {
this.photoView = findViewById(R.id.image_view);
this.subsamplingImageView = findViewById(R.id.subsampling_image_view);
this.subsamplingImageView.setBitmapDecoderClass(AttachmentBitmapDecoder.class);
this.subsamplingImageView.setRegionDecoderClass(AttachmentRegionDecoder.class);
this.subsamplingImageView.setOrientation(SubsamplingScaleImageView.ORIENTATION_USE_EXIF);
}
@SuppressLint("StaticFieldLeak")
public void setImageUri(@NonNull MasterSecret masterSecret, @NonNull GlideRequests glideRequests,
@NonNull Uri uri, @NonNull String contentType)
{
@ -90,7 +91,7 @@ public class ZoomingImageView extends FrameLayout {
setImageViewUri(masterSecret, glideRequests, uri);
} else {
Log.w(TAG, "Loading in subsampling image view...");
setSubsamplingImageViewUri(uri);
setSubsamplingImageViewUri(masterSecret, uri);
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
@ -107,7 +108,10 @@ public class ZoomingImageView extends FrameLayout {
.into(photoView);
}
private void setSubsamplingImageViewUri(@NonNull Uri uri) {
private void setSubsamplingImageViewUri(@NonNull MasterSecret masterSecret, @NonNull Uri uri) {
subsamplingImageView.setBitmapDecoderFactory(new AttachmentBitmapDecoderFactory(masterSecret));
subsamplingImageView.setRegionDecoderFactory(new AttachmentRegionDecoderFactory(masterSecret));
subsamplingImageView.setVisibility(View.VISIBLE);
photoView.setVisibility(View.GONE);
@ -118,4 +122,33 @@ public class ZoomingImageView extends FrameLayout {
photoView.setImageDrawable(null);
subsamplingImageView.recycle();
}
private static class AttachmentBitmapDecoderFactory implements DecoderFactory<AttachmentBitmapDecoder> {
private final MasterSecret masterSecret;
private AttachmentBitmapDecoderFactory(MasterSecret masterSecret) {
this.masterSecret = masterSecret;
}
@Override
public AttachmentBitmapDecoder make() throws IllegalAccessException, InstantiationException {
return new AttachmentBitmapDecoder(masterSecret);
}
}
private static class AttachmentRegionDecoderFactory implements DecoderFactory<AttachmentRegionDecoder> {
private final MasterSecret masterSecret;
private AttachmentRegionDecoderFactory(@NonNull MasterSecret masterSecret) {
this.masterSecret = masterSecret;
}
@Override
public AttachmentRegionDecoder make() throws IllegalAccessException, InstantiationException {
return new AttachmentRegionDecoder(masterSecret);
}
}
}

Wyświetl plik

@ -6,6 +6,7 @@ import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.net.Uri;
import android.support.annotation.NonNull;
import com.davemorrissey.labs.subscaleview.decoder.ImageDecoder;
import com.davemorrissey.labs.subscaleview.decoder.SkiaImageDecoder;
@ -18,18 +19,18 @@ import java.io.InputStream;
public class AttachmentBitmapDecoder implements ImageDecoder{
private final MasterSecret masterSecret;
public AttachmentBitmapDecoder(@NonNull MasterSecret masterSecret) {
this.masterSecret = masterSecret;
}
@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 {

Wyświetl plik

@ -8,8 +8,7 @@ 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.support.annotation.NonNull;
import android.util.Log;
import com.davemorrissey.labs.subscaleview.decoder.ImageRegionDecoder;
@ -17,7 +16,6 @@ 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;
@ -25,11 +23,16 @@ public class AttachmentRegionDecoder implements ImageRegionDecoder {
private static final String TAG = AttachmentRegionDecoder.class.getName();
private final MasterSecret masterSecret;
private SkiaImageRegionDecoder passthrough;
private BitmapRegionDecoder bitmapRegionDecoder;
@RequiresApi(api = Build.VERSION_CODES.GINGERBREAD_MR1)
public AttachmentRegionDecoder(@NonNull MasterSecret masterSecret) {
this.masterSecret = masterSecret;
}
@Override
public Point init(Context context, Uri uri) throws Exception {
Log.w(TAG, "Init!");
@ -38,12 +41,6 @@ public class AttachmentRegionDecoder implements ImageRegionDecoder {
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);
@ -52,7 +49,6 @@ public class AttachmentRegionDecoder implements ImageRegionDecoder {
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);
@ -76,14 +72,12 @@ public class AttachmentRegionDecoder implements ImageRegionDecoder {
}
}
@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();