mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-21 09:20:19 +01:00
Improve organization of glide packages.
Generic image processing classes were scattered alongside Signal-specific Glide code across multiple packages: `org.signal.glide`, `org.thoughtcrime.securesms.glide` and `org.thoughtcrime.securesms.mms`. This change provides a clearer separation of concerns: - `org.signal.glide` contains generic image loading components - `org.thoughtcrime.securesms.glide` contains Signal-specific Glide integrations - Feature-specific loaders are moved to their respective domain packages (e.g. `.badges`, `.contacts`)
This commit is contained in:
committed by
Jeffrey Starke
parent
cc43add7af
commit
47508495ed
@@ -1,35 +0,0 @@
|
||||
package org.thoughtcrime.securesms.mms;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.os.Build.VERSION;
|
||||
import android.os.Build.VERSION_CODES;
|
||||
import android.provider.ContactsContract;
|
||||
|
||||
import com.bumptech.glide.load.data.StreamLocalUriFetcher;
|
||||
|
||||
import org.signal.core.util.logging.Log;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
||||
class ContactPhotoLocalUriFetcher extends StreamLocalUriFetcher {
|
||||
|
||||
private static final String TAG = Log.tag(ContactPhotoLocalUriFetcher.class);
|
||||
|
||||
ContactPhotoLocalUriFetcher(Context context, Uri uri) {
|
||||
super(context.getContentResolver(), uri);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InputStream loadResource(Uri uri, ContentResolver contentResolver)
|
||||
throws FileNotFoundException
|
||||
{
|
||||
if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) {
|
||||
return ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, uri, true);
|
||||
} else {
|
||||
return ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, uri);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
package org.thoughtcrime.securesms.mms;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.Uri;
|
||||
import android.util.Pair;
|
||||
|
||||
import com.bumptech.glide.load.data.StreamLocalUriFetcher;
|
||||
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.attachments.AttachmentId;
|
||||
import org.thoughtcrime.securesms.providers.BlobProvider;
|
||||
import org.thoughtcrime.securesms.util.BitmapDecodingException;
|
||||
import org.thoughtcrime.securesms.util.BitmapUtil;
|
||||
import org.thoughtcrime.securesms.util.MediaUtil;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
class DecryptableStreamLocalUriFetcher extends StreamLocalUriFetcher {
|
||||
|
||||
private static final String TAG = Log.tag(DecryptableStreamLocalUriFetcher.class);
|
||||
|
||||
private static final long TOTAL_PIXEL_SIZE_LIMIT = 200_000_000L; // 200 megapixels
|
||||
|
||||
private final Context context;
|
||||
|
||||
DecryptableStreamLocalUriFetcher(Context context, Uri uri) {
|
||||
super(context.getContentResolver(), uri);
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InputStream loadResource(Uri uri, ContentResolver contentResolver) throws FileNotFoundException {
|
||||
if (MediaUtil.hasVideoThumbnail(context, uri)) {
|
||||
Bitmap thumbnail = MediaUtil.getVideoThumbnail(context, uri, 1000);
|
||||
|
||||
if (thumbnail != null) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, baos);
|
||||
ByteArrayInputStream thumbnailStream = new ByteArrayInputStream(baos.toByteArray());
|
||||
thumbnail.recycle();
|
||||
return thumbnailStream;
|
||||
}
|
||||
if (PartAuthority.isAttachmentUri(uri) && MediaUtil.isVideoType(PartAuthority.getAttachmentContentType(context, uri))) {
|
||||
try {
|
||||
AttachmentId attachmentId = PartAuthority.requireAttachmentId(uri);
|
||||
Uri thumbnailUri = PartAuthority.getAttachmentThumbnailUri(attachmentId);
|
||||
InputStream thumbStream = PartAuthority.getAttachmentThumbnailStream(context, thumbnailUri);
|
||||
if (thumbStream != null) {
|
||||
return thumbStream;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.i(TAG, "Failed to fetch thumbnail", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (PartAuthority.isBlobUri(uri) && BlobProvider.isSingleUseMemoryBlob(uri)) {
|
||||
return PartAuthority.getAttachmentThumbnailStream(context, uri);
|
||||
} else if (isSafeSize(PartAuthority.getAttachmentThumbnailStream(context, uri))) {
|
||||
return PartAuthority.getAttachmentThumbnailStream(context, uri);
|
||||
} else {
|
||||
throw new IOException("File dimensions are too large!");
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
Log.w(TAG, ioe);
|
||||
throw new FileNotFoundException("PartAuthority couldn't load Uri resource.");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isSafeSize(InputStream stream) {
|
||||
try {
|
||||
Pair<Integer, Integer> dimensions = BitmapUtil.getDimensions(stream);
|
||||
long totalPixels = (long) dimensions.first * dimensions.second;
|
||||
return totalPixels < TOTAL_PIXEL_SIZE_LIMIT;
|
||||
} catch (BitmapDecodingException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import android.content.Context
|
||||
import com.bumptech.glide.Priority
|
||||
import com.bumptech.glide.load.DataSource
|
||||
import com.bumptech.glide.load.data.DataFetcher
|
||||
import org.signal.glide.common.io.InputStreamFactory
|
||||
|
||||
/**
|
||||
* A Glide [DataFetcher] that retrieves an [InputStreamFactory] for a [DecryptableUri].
|
||||
|
||||
@@ -11,6 +11,7 @@ import com.bumptech.glide.load.model.ModelLoader
|
||||
import com.bumptech.glide.load.model.ModelLoaderFactory
|
||||
import com.bumptech.glide.load.model.MultiModelLoaderFactory
|
||||
import com.bumptech.glide.signature.ObjectKey
|
||||
import org.signal.glide.common.io.InputStreamFactory
|
||||
|
||||
/**
|
||||
* A Glide [ModelLoader] that handles conversion from [DecryptableUri] to [InputStreamFactory].
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
* Copyright 2025 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.thoughtcrime.securesms.mms
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool
|
||||
import com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream
|
||||
import org.signal.core.util.logging.Log
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.InputStream
|
||||
|
||||
interface InputStreamFactory {
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun build(context: Context, uri: Uri): InputStreamFactory = UriInputStreamFactory(context, uri)
|
||||
|
||||
@JvmStatic
|
||||
fun build(file: File): InputStreamFactory = FileInputStreamFactory(file)
|
||||
}
|
||||
|
||||
fun create(): InputStream
|
||||
fun createRecyclable(byteArrayPool: ArrayPool): InputStream = RecyclableBufferedInputStream(create(), byteArrayPool)
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory that creates a new [InputStream] for the given [Uri] each time [create] is called.
|
||||
*/
|
||||
class UriInputStreamFactory(
|
||||
private val context: Context,
|
||||
private val uri: Uri
|
||||
) : InputStreamFactory {
|
||||
companion object {
|
||||
private val TAG = Log.tag(UriInputStreamFactory::class)
|
||||
}
|
||||
|
||||
override fun create(): InputStream {
|
||||
return try {
|
||||
DecryptableStreamLocalUriFetcher(context, uri).loadResource(uri, context.contentResolver)
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Error creating input stream for URI.", e)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory that creates a new [InputStream] for the given [File] each time [create] is called.
|
||||
*/
|
||||
class FileInputStreamFactory(
|
||||
private val file: File
|
||||
) : InputStreamFactory {
|
||||
companion object {
|
||||
private val TAG = Log.tag(FileInputStreamFactory::class)
|
||||
}
|
||||
|
||||
override fun create(): InputStream {
|
||||
return try {
|
||||
FileInputStream(file)
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Error creating input stream for File.", e)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
* Copyright 2025 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.thoughtcrime.securesms.mms
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import com.bumptech.glide.Glide
|
||||
import com.bumptech.glide.Registry
|
||||
import com.bumptech.glide.load.Options
|
||||
import com.bumptech.glide.load.ResourceDecoder
|
||||
import com.bumptech.glide.load.engine.Resource
|
||||
import org.thoughtcrime.securesms.glide.Downsampler
|
||||
|
||||
/**
|
||||
* A Glide [ResourceDecoder] that decodes [Bitmap]s from a [InputStreamFactory] instances.
|
||||
*/
|
||||
class InputStreamFactoryBitmapDecoder(
|
||||
private val downsampler: Downsampler
|
||||
) : ResourceDecoder<InputStreamFactory, Bitmap> {
|
||||
|
||||
constructor(
|
||||
context: Context,
|
||||
glide: Glide,
|
||||
registry: Registry
|
||||
) : this(
|
||||
downsampler = Downsampler(registry.imageHeaderParsers, context.resources.displayMetrics, glide.bitmapPool, glide.arrayPool)
|
||||
)
|
||||
|
||||
override fun handles(source: InputStreamFactory, options: Options): Boolean = true
|
||||
|
||||
override fun decode(source: InputStreamFactory, width: Int, height: Int, options: Options): Resource<Bitmap?>? {
|
||||
return downsampler.decode(source, width, height, options)
|
||||
}
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
/*
|
||||
* Copyright 2025 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.thoughtcrime.securesms.mms;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.Registry;
|
||||
import com.bumptech.glide.load.model.GlideUrl;
|
||||
import com.bumptech.glide.load.model.UnitModelLoader;
|
||||
import com.bumptech.glide.load.resource.bitmap.BitmapDrawableEncoder;
|
||||
import com.bumptech.glide.load.resource.gif.ByteBufferGifDecoder;
|
||||
import com.bumptech.glide.load.resource.gif.GifDrawable;
|
||||
import com.bumptech.glide.load.resource.gif.StreamGifDecoder;
|
||||
|
||||
import org.signal.glide.apng.decode.APNGDecoder;
|
||||
import org.thoughtcrime.securesms.badges.models.Badge;
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHash;
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHashModelLoader;
|
||||
import org.thoughtcrime.securesms.blurhash.BlurHashResourceDecoder;
|
||||
import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto;
|
||||
import org.thoughtcrime.securesms.crypto.AttachmentSecret;
|
||||
import org.thoughtcrime.securesms.crypto.AttachmentSecretProvider;
|
||||
import org.thoughtcrime.securesms.giph.model.ChunkedImageUrl;
|
||||
import org.thoughtcrime.securesms.glide.BadgeLoader;
|
||||
import org.thoughtcrime.securesms.glide.ChunkedImageUrlLoader;
|
||||
import org.thoughtcrime.securesms.glide.ContactPhotoLoader;
|
||||
import org.thoughtcrime.securesms.glide.GiftBadgeModel;
|
||||
import org.thoughtcrime.securesms.glide.OkHttpUrlLoader;
|
||||
import org.thoughtcrime.securesms.glide.cache.ApngFrameDrawableTranscoder;
|
||||
import org.thoughtcrime.securesms.glide.cache.ByteBufferApngDecoder;
|
||||
import org.thoughtcrime.securesms.glide.cache.EncryptedApngCacheEncoder;
|
||||
import org.thoughtcrime.securesms.glide.cache.EncryptedBitmapResourceEncoder;
|
||||
import org.thoughtcrime.securesms.glide.cache.EncryptedCacheDecoder;
|
||||
import org.thoughtcrime.securesms.glide.cache.EncryptedCacheEncoder;
|
||||
import org.thoughtcrime.securesms.glide.cache.EncryptedGifDrawableResourceEncoder;
|
||||
import org.thoughtcrime.securesms.glide.cache.StreamApngDecoder;
|
||||
import org.thoughtcrime.securesms.glide.cache.StreamBitmapDecoder;
|
||||
import org.thoughtcrime.securesms.glide.cache.StreamFactoryApngDecoder;
|
||||
import org.thoughtcrime.securesms.glide.cache.StreamFactoryGifDecoder;
|
||||
import org.thoughtcrime.securesms.glide.cache.WebpSanDecoder;
|
||||
import org.thoughtcrime.securesms.stickers.StickerRemoteUri;
|
||||
import org.thoughtcrime.securesms.stickers.StickerRemoteUriLoader;
|
||||
import org.thoughtcrime.securesms.stories.StoryTextPostModel;
|
||||
import org.thoughtcrime.securesms.util.ConversationShortcutPhoto;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* The core logic for {@link SignalGlideModule}. This is a separate class because it uses
|
||||
* dependencies defined in the main Gradle module.
|
||||
*/
|
||||
public class SignalGlideComponents implements RegisterGlideComponents {
|
||||
|
||||
@Override
|
||||
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
|
||||
AttachmentSecret attachmentSecret = AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret();
|
||||
byte[] secret = attachmentSecret.getModernKey();
|
||||
|
||||
registry.prepend(File.class, File.class, UnitModelLoader.Factory.getInstance());
|
||||
|
||||
registry.prepend(InputStream.class, Bitmap.class, new WebpSanDecoder());
|
||||
|
||||
registry.prepend(InputStream.class, new EncryptedCacheEncoder(secret, glide.getArrayPool()));
|
||||
|
||||
registry.prepend(File.class, Bitmap.class, new EncryptedCacheDecoder<>(secret, new StreamBitmapDecoder(context, glide, registry)));
|
||||
|
||||
StreamGifDecoder streamGifDecoder = new StreamGifDecoder(registry.getImageHeaderParsers(), new ByteBufferGifDecoder(context, registry.getImageHeaderParsers(), glide.getBitmapPool(), glide.getArrayPool()), glide.getArrayPool());
|
||||
StreamFactoryGifDecoder streamFactoryGifDecoder = new StreamFactoryGifDecoder(streamGifDecoder);
|
||||
registry.prepend(InputStream.class, GifDrawable.class, streamGifDecoder);
|
||||
registry.prepend(InputStreamFactory.class, GifDrawable.class, streamFactoryGifDecoder);
|
||||
registry.prepend(GifDrawable.class, new EncryptedGifDrawableResourceEncoder(secret));
|
||||
registry.prepend(File.class, GifDrawable.class, new EncryptedCacheDecoder<>(secret, streamGifDecoder));
|
||||
|
||||
EncryptedBitmapResourceEncoder encryptedBitmapResourceEncoder = new EncryptedBitmapResourceEncoder(secret);
|
||||
registry.prepend(Bitmap.class, new EncryptedBitmapResourceEncoder(secret));
|
||||
registry.prepend(BitmapDrawable.class, new BitmapDrawableEncoder(glide.getBitmapPool(), encryptedBitmapResourceEncoder));
|
||||
|
||||
ByteBufferApngDecoder byteBufferApngDecoder = new ByteBufferApngDecoder();
|
||||
StreamApngDecoder streamApngDecoder = new StreamApngDecoder(byteBufferApngDecoder);
|
||||
StreamFactoryApngDecoder streamFactoryApngDecoder = new StreamFactoryApngDecoder(byteBufferApngDecoder, glide, registry);
|
||||
|
||||
registry.prepend(InputStream.class, APNGDecoder.class, streamApngDecoder);
|
||||
registry.prepend(InputStreamFactory.class, APNGDecoder.class, streamFactoryApngDecoder);
|
||||
registry.prepend(ByteBuffer.class, APNGDecoder.class, byteBufferApngDecoder);
|
||||
registry.prepend(APNGDecoder.class, new EncryptedApngCacheEncoder(secret));
|
||||
registry.prepend(File.class, APNGDecoder.class, new EncryptedCacheDecoder<>(secret, streamApngDecoder));
|
||||
registry.register(APNGDecoder.class, Drawable.class, new ApngFrameDrawableTranscoder());
|
||||
|
||||
registry.prepend(BlurHash.class, Bitmap.class, new BlurHashResourceDecoder());
|
||||
registry.prepend(StoryTextPostModel.class, Bitmap.class, new StoryTextPostModel.Decoder());
|
||||
|
||||
registry.append(StoryTextPostModel.class, StoryTextPostModel.class, UnitModelLoader.Factory.getInstance());
|
||||
registry.append(ConversationShortcutPhoto.class, Bitmap.class, new ConversationShortcutPhoto.Loader.Factory(context));
|
||||
registry.append(ContactPhoto.class, InputStream.class, new ContactPhotoLoader.Factory(context));
|
||||
registry.append(DecryptableUri.class, InputStreamFactory.class, new DecryptableUriStreamLoader.Factory(context));
|
||||
registry.append(InputStreamFactory.class, Bitmap.class, new InputStreamFactoryBitmapDecoder(context, glide, registry));
|
||||
registry.append(ChunkedImageUrl.class, InputStream.class, new ChunkedImageUrlLoader.Factory());
|
||||
registry.append(StickerRemoteUri.class, InputStream.class, new StickerRemoteUriLoader.Factory());
|
||||
registry.append(BlurHash.class, BlurHash.class, new BlurHashModelLoader.Factory());
|
||||
registry.append(Badge.class, InputStream.class, BadgeLoader.createFactory());
|
||||
registry.append(GiftBadgeModel.class, InputStream.class, GiftBadgeModel.createFactory());
|
||||
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user