2015-07-24 17:07:33 -07:00
|
|
|
package org.thoughtcrime.securesms.mms;
|
|
|
|
|
2015-11-27 14:45:07 -08:00
|
|
|
import android.support.annotation.NonNull;
|
2017-10-11 17:12:46 -07:00
|
|
|
import android.support.annotation.Nullable;
|
2015-07-24 17:07:33 -07:00
|
|
|
|
2017-10-11 17:12:46 -07:00
|
|
|
import com.bumptech.glide.load.Key;
|
|
|
|
import com.bumptech.glide.load.Options;
|
2015-07-24 17:07:33 -07:00
|
|
|
import com.bumptech.glide.load.model.ModelLoader;
|
|
|
|
import com.bumptech.glide.load.model.ModelLoaderFactory;
|
2017-10-11 17:12:46 -07:00
|
|
|
import com.bumptech.glide.load.model.MultiModelLoaderFactory;
|
2015-07-24 17:07:33 -07:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.mms.AttachmentStreamUriLoader.AttachmentModel;
|
2017-11-01 15:56:31 -07:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2015-07-24 17:07:33 -07:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.InputStream;
|
2017-10-11 17:12:46 -07:00
|
|
|
import java.security.MessageDigest;
|
2015-07-24 17:07:33 -07:00
|
|
|
|
2017-10-11 17:12:46 -07:00
|
|
|
public class AttachmentStreamUriLoader implements ModelLoader<AttachmentModel, InputStream> {
|
2015-07-24 17:07:33 -07:00
|
|
|
|
2017-10-11 17:12:46 -07:00
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public @Nullable LoadData<InputStream> buildLoadData(@NonNull AttachmentModel attachmentModel, int width, int height, @NonNull Options options) {
|
2017-11-01 15:56:31 -07:00
|
|
|
return new LoadData<>(attachmentModel, new AttachmentStreamLocalUriFetcher(attachmentModel.attachment, attachmentModel.plaintextLength, attachmentModel.key, attachmentModel.digest));
|
2017-10-11 17:12:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public boolean handles(@NonNull AttachmentModel attachmentModel) {
|
2017-10-11 17:12:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static class Factory implements ModelLoaderFactory<AttachmentModel, InputStream> {
|
2015-07-24 17:07:33 -07:00
|
|
|
|
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public @NonNull ModelLoader<AttachmentModel, InputStream> build(@NonNull MultiModelLoaderFactory multiFactory) {
|
2017-10-11 17:12:46 -07:00
|
|
|
return new AttachmentStreamUriLoader();
|
2015-07-24 17:07:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void teardown() {
|
|
|
|
// Do nothing.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 17:12:46 -07:00
|
|
|
public static class AttachmentModel implements Key {
|
2017-11-01 15:56:31 -07:00
|
|
|
public @NonNull File attachment;
|
|
|
|
public @NonNull byte[] key;
|
|
|
|
public @NonNull Optional<byte[]> digest;
|
|
|
|
public long plaintextLength;
|
|
|
|
|
|
|
|
public AttachmentModel(@NonNull File attachment, @NonNull byte[] key,
|
|
|
|
long plaintextLength, @NonNull Optional<byte[]> digest)
|
|
|
|
{
|
|
|
|
this.attachment = attachment;
|
|
|
|
this.key = key;
|
|
|
|
this.digest = digest;
|
|
|
|
this.plaintextLength = plaintextLength;
|
2015-07-24 17:07:33 -07:00
|
|
|
}
|
2015-11-27 14:45:07 -08:00
|
|
|
|
2017-10-11 17:12:46 -07:00
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
|
2017-10-11 17:12:46 -07:00
|
|
|
messageDigest.update(attachment.toString().getBytes());
|
|
|
|
}
|
|
|
|
|
2015-11-27 14:45:07 -08:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) return true;
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
|
|
|
|
AttachmentModel that = (AttachmentModel)o;
|
|
|
|
|
|
|
|
return attachment.equals(that.attachment);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return attachment.hashCode();
|
|
|
|
}
|
2015-07-24 17:07:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|