2015-03-31 22:44:41 +00:00
|
|
|
package org.thoughtcrime.securesms.mms;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-03-18 21:52:49 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.support.annotation.NonNull;
|
2017-10-12 00:12:46 +00:00
|
|
|
import android.util.Log;
|
2015-03-31 22:44:41 +00:00
|
|
|
|
|
|
|
import com.bumptech.glide.Glide;
|
|
|
|
import com.bumptech.glide.GlideBuilder;
|
2017-10-12 00:12:46 +00:00
|
|
|
import com.bumptech.glide.Registry;
|
|
|
|
import com.bumptech.glide.annotation.GlideModule;
|
2015-03-31 22:44:41 +00:00
|
|
|
import com.bumptech.glide.load.engine.cache.DiskCache;
|
|
|
|
import com.bumptech.glide.load.engine.cache.DiskCacheAdapter;
|
2016-10-17 02:05:07 +00:00
|
|
|
import com.bumptech.glide.load.model.GlideUrl;
|
2018-04-09 16:03:42 +00:00
|
|
|
import com.bumptech.glide.load.model.UnitModelLoader;
|
2018-03-18 21:52:49 +00:00
|
|
|
import com.bumptech.glide.load.resource.bitmap.Downsampler;
|
|
|
|
import com.bumptech.glide.load.resource.bitmap.StreamBitmapDecoder;
|
|
|
|
import com.bumptech.glide.load.resource.gif.ByteBufferGifDecoder;
|
|
|
|
import com.bumptech.glide.load.resource.gif.GifDrawable;
|
|
|
|
import com.bumptech.glide.load.resource.gif.StreamGifDecoder;
|
2017-10-12 00:12:46 +00:00
|
|
|
import com.bumptech.glide.module.AppGlideModule;
|
2015-03-31 22:44:41 +00:00
|
|
|
|
2017-10-16 20:11:42 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
|
2018-03-18 21:52:49 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.AttachmentSecret;
|
|
|
|
import org.thoughtcrime.securesms.crypto.AttachmentSecretProvider;
|
2019-01-15 08:41:05 +00:00
|
|
|
import org.thoughtcrime.securesms.giph.model.ChunkedImageUrl;
|
2019-04-17 14:21:30 +00:00
|
|
|
import org.thoughtcrime.securesms.glide.ChunkedImageUrlLoader;
|
2017-10-16 20:11:42 +00:00
|
|
|
import org.thoughtcrime.securesms.glide.ContactPhotoLoader;
|
2019-04-17 14:21:30 +00:00
|
|
|
import org.thoughtcrime.securesms.glide.OkHttpUrlLoader;
|
2018-03-18 21:52:49 +00:00
|
|
|
import org.thoughtcrime.securesms.glide.cache.EncryptedBitmapCacheDecoder;
|
2019-04-17 14:21:30 +00:00
|
|
|
import org.thoughtcrime.securesms.glide.cache.EncryptedBitmapResourceEncoder;
|
2018-03-18 21:52:49 +00:00
|
|
|
import org.thoughtcrime.securesms.glide.cache.EncryptedCacheEncoder;
|
|
|
|
import org.thoughtcrime.securesms.glide.cache.EncryptedGifCacheDecoder;
|
|
|
|
import org.thoughtcrime.securesms.glide.cache.EncryptedGifDrawableResourceEncoder;
|
2015-07-25 00:07:33 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.AttachmentStreamUriLoader.AttachmentModel;
|
2015-03-31 22:44:41 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri;
|
2019-04-17 14:21:30 +00:00
|
|
|
import org.thoughtcrime.securesms.stickers.StickerRemoteUri;
|
|
|
|
import org.thoughtcrime.securesms.stickers.StickerRemoteUriLoader;
|
2015-03-31 22:44:41 +00:00
|
|
|
|
2018-03-18 21:52:49 +00:00
|
|
|
import java.io.File;
|
2015-03-31 22:44:41 +00:00
|
|
|
import java.io.InputStream;
|
|
|
|
|
2017-10-12 00:12:46 +00:00
|
|
|
@GlideModule
|
|
|
|
public class SignalGlideModule extends AppGlideModule {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isManifestParsingEnabled() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-31 22:44:41 +00:00
|
|
|
@Override
|
|
|
|
public void applyOptions(Context context, GlideBuilder builder) {
|
2018-09-28 03:01:01 +00:00
|
|
|
builder.setLogLevel(Log.ERROR);
|
2016-10-17 02:05:07 +00:00
|
|
|
// builder.setDiskCache(new NoopDiskCacheFactory());
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-18 21:52:49 +00:00
|
|
|
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
|
|
|
|
AttachmentSecret attachmentSecret = AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret();
|
|
|
|
byte[] secret = attachmentSecret.getModernKey();
|
|
|
|
|
2018-04-09 16:03:42 +00:00
|
|
|
registry.prepend(File.class, File.class, UnitModelLoader.Factory.getInstance());
|
2018-03-18 21:52:49 +00:00
|
|
|
registry.prepend(InputStream.class, new EncryptedCacheEncoder(secret, glide.getArrayPool()));
|
|
|
|
registry.prepend(File.class, Bitmap.class, new EncryptedBitmapCacheDecoder(secret, new StreamBitmapDecoder(new Downsampler(registry.getImageHeaderParsers(), context.getResources().getDisplayMetrics(), glide.getBitmapPool(), glide.getArrayPool()), glide.getArrayPool())));
|
|
|
|
registry.prepend(File.class, GifDrawable.class, new EncryptedGifCacheDecoder(secret, new StreamGifDecoder(registry.getImageHeaderParsers(), new ByteBufferGifDecoder(context, registry.getImageHeaderParsers(), glide.getBitmapPool(), glide.getArrayPool()), glide.getArrayPool())));
|
|
|
|
|
|
|
|
registry.prepend(Bitmap.class, new EncryptedBitmapResourceEncoder(secret));
|
|
|
|
registry.prepend(GifDrawable.class, new EncryptedGifDrawableResourceEncoder(secret));
|
|
|
|
|
2017-10-16 20:11:42 +00:00
|
|
|
registry.append(ContactPhoto.class, InputStream.class, new ContactPhotoLoader.Factory(context));
|
2017-10-12 00:12:46 +00:00
|
|
|
registry.append(DecryptableUri.class, InputStream.class, new DecryptableStreamUriLoader.Factory(context));
|
|
|
|
registry.append(AttachmentModel.class, InputStream.class, new AttachmentStreamUriLoader.Factory());
|
2019-01-15 08:41:05 +00:00
|
|
|
registry.append(ChunkedImageUrl.class, InputStream.class, new ChunkedImageUrlLoader.Factory());
|
2019-04-17 14:21:30 +00:00
|
|
|
registry.append(StickerRemoteUri.class, InputStream.class, new StickerRemoteUriLoader.Factory(context));
|
2017-10-12 00:12:46 +00:00
|
|
|
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static class NoopDiskCacheFactory implements DiskCache.Factory {
|
|
|
|
@Override
|
|
|
|
public DiskCache build() {
|
|
|
|
return new DiskCacheAdapter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|