From 5d91a94252f861f25b854a86b03444ba64366617 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Sat, 25 Aug 2018 10:33:14 -0700 Subject: [PATCH] Fix shortcut images being too large. We were hitting the transaction limit size. This change scales down shortcut icons to be at most 300x300, which comes out to ~360kb, which should be safely under the limit of 1mb. Fixes #8139 --- .../securesms/ConversationActivity.java | 6 ++++- .../securesms/util/BitmapUtil.java | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/org/thoughtcrime/securesms/ConversationActivity.java b/src/org/thoughtcrime/securesms/ConversationActivity.java index 1fcc1baa77..339fb76942 100644 --- a/src/org/thoughtcrime/securesms/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/ConversationActivity.java @@ -26,6 +26,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.res.Configuration; import android.content.res.TypedArray; +import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.PorterDuff.Mode; @@ -156,6 +157,7 @@ import org.thoughtcrime.securesms.sms.MessageSender; import org.thoughtcrime.securesms.sms.OutgoingEncryptedMessage; import org.thoughtcrime.securesms.sms.OutgoingEndSessionMessage; import org.thoughtcrime.securesms.sms.OutgoingTextMessage; +import org.thoughtcrime.securesms.util.BitmapUtil; import org.thoughtcrime.securesms.util.CharacterCalculator.CharacterState; import org.thoughtcrime.securesms.util.CommunicationActions; import org.thoughtcrime.securesms.util.Dialogs; @@ -796,7 +798,9 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity if (recipient.getContactPhoto() != null) { try { - icon = IconCompat.createWithAdaptiveBitmap(BitmapFactory.decodeStream(recipient.getContactPhoto().openInputStream(context))); + Bitmap bitmap = BitmapFactory.decodeStream(recipient.getContactPhoto().openInputStream(context)); + bitmap = BitmapUtil.createScaledBitmap(bitmap, 300, 300); + icon = IconCompat.createWithAdaptiveBitmap(bitmap); } catch (IOException e) { Log.w(TAG, "Failed to decode contact photo during shortcut creation. Falling back to generic icon.", e); } diff --git a/src/org/thoughtcrime/securesms/util/BitmapUtil.java b/src/org/thoughtcrime/securesms/util/BitmapUtil.java index 99f272adb1..5f0b45012f 100644 --- a/src/org/thoughtcrime/securesms/util/BitmapUtil.java +++ b/src/org/thoughtcrime/securesms/util/BitmapUtil.java @@ -130,6 +130,31 @@ public class BitmapUtil { } } + @WorkerThread + public static Bitmap createScaledBitmap(Bitmap bitmap, int maxWidth, int maxHeight) { + if (bitmap.getWidth() <= maxWidth && bitmap.getHeight() <= maxHeight) { + return bitmap; + } + + if (maxWidth <= 0 || maxHeight <= 0) { + return bitmap; + } + + int newWidth = maxWidth; + int newHeight = maxHeight; + + float widthRatio = bitmap.getWidth() / (float) maxWidth; + float heightRatio = bitmap.getHeight() / (float) maxHeight; + + if (widthRatio > heightRatio) { + newHeight = (int) (bitmap.getHeight() / widthRatio); + } else { + newWidth = (int) (bitmap.getWidth() / heightRatio); + } + + return Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true); + } + private static BitmapFactory.Options getImageDimensions(InputStream inputStream) throws BitmapDecodingException {