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
This commit is contained in:
Greyson Parrelli 2018-08-25 10:33:14 -07:00
parent 25db207e24
commit 5d91a94252
2 changed files with 30 additions and 1 deletions

View File

@ -26,6 +26,7 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.PorterDuff.Mode; 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.OutgoingEncryptedMessage;
import org.thoughtcrime.securesms.sms.OutgoingEndSessionMessage; import org.thoughtcrime.securesms.sms.OutgoingEndSessionMessage;
import org.thoughtcrime.securesms.sms.OutgoingTextMessage; import org.thoughtcrime.securesms.sms.OutgoingTextMessage;
import org.thoughtcrime.securesms.util.BitmapUtil;
import org.thoughtcrime.securesms.util.CharacterCalculator.CharacterState; import org.thoughtcrime.securesms.util.CharacterCalculator.CharacterState;
import org.thoughtcrime.securesms.util.CommunicationActions; import org.thoughtcrime.securesms.util.CommunicationActions;
import org.thoughtcrime.securesms.util.Dialogs; import org.thoughtcrime.securesms.util.Dialogs;
@ -796,7 +798,9 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
if (recipient.getContactPhoto() != null) { if (recipient.getContactPhoto() != null) {
try { 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) { } catch (IOException e) {
Log.w(TAG, "Failed to decode contact photo during shortcut creation. Falling back to generic icon.", e); Log.w(TAG, "Failed to decode contact photo during shortcut creation. Falling back to generic icon.", e);
} }

View File

@ -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) private static BitmapFactory.Options getImageDimensions(InputStream inputStream)
throws BitmapDecodingException throws BitmapDecodingException
{ {