Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 898e67df authored by Moez Bhatti's avatar Moez Bhatti
Browse files

Respect max MMS dimensions from sms manager config

parent cfd5bb72
Loading
Loading
Loading
Loading
+86 −0
Original line number Diff line number Diff line
@@ -26,12 +26,44 @@ import android.net.Uri
import androidx.exifinterface.media.ExifInterface
import javax.inject.Inject

class ImageRepostoryImpl @Inject constructor(private val context: Context) : ImageRepository {
class ImageRepositoryImpl @Inject constructor(private val context: Context) : ImageRepository {

    override fun loadImage(uri: Uri): Bitmap? {
        val exif = context.contentResolver.openInputStream(uri)?.let(::ExifInterface)
        val bitmap = BitmapFactory.decodeStream(context.contentResolver.openInputStream(uri))
        val orientation = exif?.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
    override fun loadImage(uri: Uri, width: Int, height: Int): Bitmap? {
        val orientation = context.contentResolver.openInputStream(uri)?.use(::ExifInterface)
                ?.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
        val rotated = orientation == ExifInterface.ORIENTATION_ROTATE_90
                || orientation == ExifInterface.ORIENTATION_ROTATE_270

        // Determine the dimensions
        val dimensionsOptions = BitmapFactory.Options().apply { inJustDecodeBounds = true }
        BitmapFactory.decodeStream(context.contentResolver.openInputStream(uri), null, dimensionsOptions)
        val srcWidth = if (rotated) dimensionsOptions.outHeight else dimensionsOptions.outWidth
        val srcHeight = if (rotated) dimensionsOptions.outWidth else dimensionsOptions.outHeight

        // If we get the dimensions and they don't exceed the max size, we don't need to scale
        val inputStream = context.contentResolver.openInputStream(uri)
        val bitmap = if ((width == 0 || srcWidth < width) && (height == 0 || srcHeight < height)) {
            BitmapFactory.decodeStream(inputStream)
        } else {
            val widthScaleFactor = width.toDouble() / srcWidth
            val heightScaleFactor = height.toDouble() / srcHeight
            val options = when {
                widthScaleFactor > heightScaleFactor -> BitmapFactory.Options().apply {
                    inScaled = true
                    inSampleSize = 4
                    inDensity = srcHeight
                    inTargetDensity = height * inSampleSize
                }

                else -> BitmapFactory.Options().apply {
                    inScaled = true
                    inSampleSize = 4
                    inDensity = srcWidth
                    inTargetDensity = width * inSampleSize
                }
            }
            BitmapFactory.decodeStream(inputStream, null, options) ?: return null
        }

        return when (orientation) {
            ExifInterface.ORIENTATION_ROTATE_90 -> rotateBitmap(bitmap, 90f)
+7 −1
Original line number Diff line number Diff line
@@ -303,6 +303,12 @@ class MessageRepositoryImpl @Inject constructor(
                parts += MMSPart("text", ContentType.TEXT_PLAIN, signedBody.toByteArray())
            }

            val smsManager = subId.takeIf { it != -1 }
                    ?.let(SmsManagerFactory::createSmsManager)
                    ?: SmsManager.getDefault()
            val width = smsManager.carrierConfigValues.getInt(SmsManager.MMS_CONFIG_MAX_IMAGE_WIDTH)
            val height = smsManager.carrierConfigValues.getInt(SmsManager.MMS_CONFIG_MAX_IMAGE_HEIGHT)

            // Add the GIFs as attachments
            parts += attachments
                    .mapNotNull { attachment -> attachment as? Attachment.Image }
@@ -317,7 +323,7 @@ class MessageRepositoryImpl @Inject constructor(
                    .mapNotNull { attachment -> attachment as? Attachment.Image }
                    .filter { attachment -> !attachment.isGif(context) }
                    .mapNotNull { attachment -> attachment.getUri() }
                    .mapNotNull { uri -> tryOrNull { imageRepository.loadImage(uri) } }
                    .mapNotNull { uri -> tryOrNull { imageRepository.loadImage(uri, width, height) } }
                    .also { totalImageBytes = it.sumBy { it.allocationByteCount } }
                    .map { bitmap ->
                        val byteRatio = bitmap.allocationByteCount / totalImageBytes.toFloat()
+1 −1
Original line number Diff line number Diff line
@@ -23,6 +23,6 @@ import android.net.Uri

interface ImageRepository {

    fun loadImage(uri: Uri): Bitmap?
    fun loadImage(uri: Uri, width: Int, height: Int): Bitmap?

}
+2 −2
Original line number Diff line number Diff line
@@ -70,7 +70,7 @@ import com.moez.QKSMS.repository.ContactRepositoryImpl
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.repository.ConversationRepositoryImpl
import com.moez.QKSMS.repository.ImageRepository
import com.moez.QKSMS.repository.ImageRepostoryImpl
import com.moez.QKSMS.repository.ImageRepositoryImpl
import com.moez.QKSMS.repository.MessageRepository
import com.moez.QKSMS.repository.MessageRepositoryImpl
import com.moez.QKSMS.repository.ScheduledMessageRepository
@@ -185,7 +185,7 @@ class AppModule(private var application: Application) {
    fun provideConversationRepository(repository: ConversationRepositoryImpl): ConversationRepository = repository

    @Provides
    fun provideImageRepository(repository: ImageRepostoryImpl): ImageRepository = repository
    fun provideImageRepository(repository: ImageRepositoryImpl): ImageRepository = repository

    @Provides
    fun provideMessageRepository(repository: MessageRepositoryImpl): MessageRepository = repository