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;
|
|
|
|
|
|
|
|
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
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public LoadData<InputStream> buildLoadData(AttachmentModel attachmentModel, int width, int height, Options options) {
|
|
|
|
return new LoadData<>(attachmentModel, new AttachmentStreamLocalUriFetcher(attachmentModel.attachment, attachmentModel.key));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean handles(AttachmentModel attachmentModel) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static class Factory implements ModelLoaderFactory<AttachmentModel, InputStream> {
|
2015-07-24 17:07:33 -07:00
|
|
|
|
|
|
|
@Override
|
2017-10-11 17:12:46 -07:00
|
|
|
public ModelLoader<AttachmentModel, InputStream> build(MultiModelLoaderFactory multiFactory) {
|
|
|
|
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 {
|
2015-11-27 14:45:07 -08:00
|
|
|
public @NonNull File attachment;
|
|
|
|
public @NonNull byte[] key;
|
2015-07-24 17:07:33 -07:00
|
|
|
|
2015-11-27 14:45:07 -08:00
|
|
|
public AttachmentModel(@NonNull File attachment, @NonNull byte[] key) {
|
2015-07-24 17:07:33 -07:00
|
|
|
this.attachment = attachment;
|
|
|
|
this.key = key;
|
|
|
|
}
|
2015-11-27 14:45:07 -08:00
|
|
|
|
2017-10-11 17:12:46 -07:00
|
|
|
@Override
|
|
|
|
public void updateDiskCacheKey(MessageDigest messageDigest) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|