session-android/src/org/thoughtcrime/securesms/mms/AttachmentStreamUriLoader.java

83 lines
2.5 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.mms;
import android.support.annotation.NonNull;
2017-10-11 17:12:46 -07:00
import android.support.annotation.Nullable;
2017-10-11 17:12:46 -07:00
import com.bumptech.glide.load.Key;
import com.bumptech.glide.load.Options;
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;
import org.thoughtcrime.securesms.mms.AttachmentStreamUriLoader.AttachmentModel;
import org.whispersystems.libsignal.util.guava.Optional;
import java.io.File;
import java.io.InputStream;
2017-10-11 17:12:46 -07:00
import java.security.MessageDigest;
2017-10-11 17:12:46 -07:00
public class AttachmentStreamUriLoader implements ModelLoader<AttachmentModel, InputStream> {
2017-10-11 17:12:46 -07:00
@Nullable
@Override
public LoadData<InputStream> buildLoadData(AttachmentModel attachmentModel, int width, int height, Options options) {
return new LoadData<>(attachmentModel, new AttachmentStreamLocalUriFetcher(attachmentModel.attachment, attachmentModel.plaintextLength, attachmentModel.key, attachmentModel.digest));
2017-10-11 17:12:46 -07:00
}
@Override
public boolean handles(AttachmentModel attachmentModel) {
return true;
}
static class Factory implements ModelLoaderFactory<AttachmentModel, InputStream> {
@Override
2017-10-11 17:12:46 -07:00
public ModelLoader<AttachmentModel, InputStream> build(MultiModelLoaderFactory multiFactory) {
return new AttachmentStreamUriLoader();
}
@Override
public void teardown() {
// Do nothing.
}
}
2017-10-11 17:12:46 -07:00
public static class AttachmentModel implements Key {
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;
}
2017-10-11 17:12:46 -07:00
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
messageDigest.update(attachment.toString().getBytes());
}
@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();
}
}
}