mirror of
https://github.com/oxen-io/session-android.git
synced 2025-12-03 10:32:39 +00:00
Make MMS more asynchronous and consistent with new SMS types.
1) We now delay MMS notifications until a payload is received, or there's an error downloading the payload. This makes group messages more consistent. 2) All "text" parts of an MMS are combined into a second text record, which is stored in the MMS row directly rather than as a distinct part. This allows for immediate text loading, which means there's no chance a ConversationItem will resize. To do this, we need to include MMS in the big DB migration that's already staged for this application update. It's also an "application-level" migration, because we need the MasterSecret to do it. 3) On conversation display, all image-based parts now have their thumbnails loaded asynchronously. This allows for smooth-scrolling. The thumbnails are also scaled more accurately.
This commit is contained in:
@@ -54,21 +54,21 @@ public class AttachmentManager {
|
||||
public void setImage(Uri image) throws IOException {
|
||||
ImageSlide slide = new ImageSlide(context, image);
|
||||
slideDeck.addSlide(slide);
|
||||
thumbnail.setImageBitmap(slide.getThumbnail());
|
||||
thumbnail.setImageBitmap(slide.getThumbnail(345, 261));
|
||||
attachmentView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
public void setVideo(Uri video) throws IOException, MediaTooLargeException {
|
||||
VideoSlide slide = new VideoSlide(context, video);
|
||||
slideDeck.addSlide(slide);
|
||||
thumbnail.setImageBitmap(slide.getThumbnail());
|
||||
thumbnail.setImageBitmap(slide.getThumbnail(thumbnail.getWidth(), thumbnail.getHeight()));
|
||||
attachmentView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
public void setAudio(Uri audio)throws IOException, MediaTooLargeException {
|
||||
AudioSlide slide = new AudioSlide(context, audio);
|
||||
slideDeck.addSlide(slide);
|
||||
thumbnail.setImageBitmap(slide.getThumbnail());
|
||||
thumbnail.setImageBitmap(slide.getThumbnail(thumbnail.getWidth(), thumbnail.getHeight()));
|
||||
attachmentView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.net.Uri;
|
||||
import android.provider.MediaStore.Audio;
|
||||
import android.widget.ImageView;
|
||||
|
||||
public class AudioSlide extends Slide {
|
||||
|
||||
@@ -49,10 +50,10 @@ public class AudioSlide extends Slide {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap getThumbnail() {
|
||||
public Bitmap getThumbnail(int maxWidth, int maxHeight) {
|
||||
return BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_menu_add_sound);
|
||||
}
|
||||
|
||||
|
||||
public static PduPart constructPartFromUri(Context context, Uri uri) throws IOException, MediaTooLargeException {
|
||||
PduPart part = new PduPart();
|
||||
|
||||
|
||||
@@ -16,25 +16,33 @@
|
||||
*/
|
||||
package org.thoughtcrime.securesms.mms;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.TransitionDrawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
import org.thoughtcrime.securesms.database.MmsDatabase;
|
||||
import org.thoughtcrime.securesms.util.BitmapUtil;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
|
||||
import ws.com.google.android.mms.ContentType;
|
||||
import ws.com.google.android.mms.pdu.PduPart;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Bitmap.CompressFormat;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
|
||||
public class ImageSlide extends Slide {
|
||||
|
||||
@@ -56,67 +64,98 @@ public class ImageSlide extends Slide {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap getThumbnail() {
|
||||
if (thumbnailCache.containsKey(part.getDataUri())) {
|
||||
Log.w("ImageSlide", "Cached thumbnail...");
|
||||
Bitmap bitmap = thumbnailCache.get(part.getDataUri()).get();
|
||||
if (bitmap != null) return bitmap;
|
||||
else thumbnailCache.remove(part.getDataUri());
|
||||
}
|
||||
public Bitmap getThumbnail(int maxWidth, int maxHeight) {
|
||||
Bitmap thumbnail = getCachedThumbnail();
|
||||
|
||||
if (thumbnail != null)
|
||||
return thumbnail;
|
||||
|
||||
try {
|
||||
BitmapFactory.Options options = getImageDimensions(getPartDataInputStream());
|
||||
int imageWidth = options.outWidth;
|
||||
int imageHeight = options.outHeight;
|
||||
|
||||
int scaler = 1;
|
||||
while ((imageWidth / scaler > 480) || (imageHeight / scaler > 480))
|
||||
scaler *= 2;
|
||||
|
||||
options.inSampleSize = scaler;
|
||||
options.inJustDecodeBounds = false;
|
||||
|
||||
Bitmap thumbnail = BitmapFactory.decodeStream(getPartDataInputStream(), null, options);
|
||||
InputStream measureStream = getPartDataInputStream();
|
||||
InputStream dataStream = getPartDataInputStream();
|
||||
|
||||
thumbnail = BitmapUtil.createScaledBitmap(measureStream, dataStream, maxWidth, maxHeight);
|
||||
thumbnailCache.put(part.getDataUri(), new SoftReference<Bitmap>(thumbnail));
|
||||
|
||||
return thumbnail;
|
||||
} catch (FileNotFoundException fnfe) {
|
||||
Log.w("ImageSlide", fnfe);
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.w("ImageSlide", e);
|
||||
return BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_missing_thumbnail_picture);
|
||||
}
|
||||
}
|
||||
|
||||
private static BitmapFactory.Options getImageDimensions(InputStream inputStream) throws FileNotFoundException {
|
||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inJustDecodeBounds = true;
|
||||
BitmapFactory.decodeStream(inputStream, null, options);
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
private static BitmapFactory.Options getImageDimensions(Context context, Uri uri) throws FileNotFoundException {
|
||||
InputStream in = context.getContentResolver().openInputStream(uri);
|
||||
return getImageDimensions(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasImage() {
|
||||
public void setThumbnailOn(ImageView imageView) {
|
||||
Bitmap thumbnail = getCachedThumbnail();
|
||||
|
||||
if (thumbnail != null) {
|
||||
Log.w("ImageSlide", "Setting cached thumbnail...");
|
||||
setThumbnailOn(imageView, thumbnail, true);
|
||||
return;
|
||||
}
|
||||
|
||||
final ColorDrawable temporaryDrawable = new ColorDrawable(Color.TRANSPARENT);
|
||||
final WeakReference<ImageView> weakImageView = new WeakReference<ImageView>(imageView);
|
||||
final Handler handler = new Handler();
|
||||
final int maxWidth = imageView.getWidth();
|
||||
final int maxHeight = imageView.getHeight();
|
||||
|
||||
imageView.setImageDrawable(temporaryDrawable);
|
||||
|
||||
MmsDatabase.slideResolver.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final Bitmap bitmap = getThumbnail(maxWidth, maxHeight);
|
||||
final ImageView destination = weakImageView.get();
|
||||
if (destination != null && destination.getDrawable() == temporaryDrawable) {
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
setThumbnailOn(destination, bitmap, false);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setThumbnailOn(ImageView imageView, Bitmap thumbnail, boolean fromMemory) {
|
||||
if (fromMemory) {
|
||||
imageView.setImageBitmap(thumbnail);
|
||||
} else {
|
||||
BitmapDrawable result = new BitmapDrawable(context.getResources(), thumbnail);
|
||||
TransitionDrawable fadingResult = new TransitionDrawable(new Drawable[]{new ColorDrawable(Color.TRANSPARENT), result});
|
||||
imageView.setImageDrawable(fadingResult);
|
||||
fadingResult.startTransition(300);
|
||||
}
|
||||
}
|
||||
|
||||
private Bitmap getCachedThumbnail() {
|
||||
synchronized (thumbnailCache) {
|
||||
SoftReference<Bitmap> bitmapReference = thumbnailCache.get(part.getDataUri());
|
||||
Log.w("ImageSlide", "Got soft reference: " + bitmapReference);
|
||||
|
||||
if (bitmapReference != null) {
|
||||
Bitmap bitmap = bitmapReference.get();
|
||||
Log.w("ImageSlide", "Got cached bitmap: " + bitmap);
|
||||
if (bitmap != null) return bitmap;
|
||||
else thumbnailCache.remove(part.getDataUri());
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasImage() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static PduPart constructPartFromUri(Context context, Uri uri) throws IOException {
|
||||
PduPart part = new PduPart();
|
||||
|
||||
BitmapFactory.Options options = getImageDimensions(context, uri);
|
||||
long size = getMediaSize(context, uri);
|
||||
|
||||
if (options.outWidth > 640 || options.outHeight > 480 || size > (1024*1024)) {
|
||||
byte[] data = scaleImage(context, uri, options, size, 640, 480, 1024*1024);
|
||||
part.setData(data);
|
||||
Log.w("ImageSlide", "Setting actual part data...");
|
||||
}
|
||||
|
||||
Log.w("ImageSlide", "Setting part data URI..");
|
||||
byte[] data = BitmapUtil.createScaledBytes(context, uri, 640, 480, (300 * 1024) - 5000);
|
||||
|
||||
part.setData(data);
|
||||
part.setDataUri(uri);
|
||||
part.setContentType(ContentType.IMAGE_JPEG.getBytes());
|
||||
part.setContentId((System.currentTimeMillis()+"").getBytes());
|
||||
@@ -124,25 +163,4 @@ public class ImageSlide extends Slide {
|
||||
|
||||
return part;
|
||||
}
|
||||
|
||||
private static byte[] scaleImage(Context context, Uri uri, BitmapFactory.Options options, long size, int maxWidth, int maxHeight, int maxSize) throws FileNotFoundException {
|
||||
int scaler = 1;
|
||||
while ((options.outWidth / scaler > maxWidth) || (options.outHeight / scaler > maxHeight))
|
||||
scaler *= 2;
|
||||
|
||||
options.inSampleSize = scaler;
|
||||
options.inJustDecodeBounds = false;
|
||||
|
||||
Bitmap bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri), null, options);
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
int quality = 80;
|
||||
|
||||
do {
|
||||
bitmap.compress(CompressFormat.JPEG, quality, baos);
|
||||
if (baos.size() > maxSize)
|
||||
quality = quality * maxSize / baos.size();
|
||||
} while (baos.size() > maxSize);
|
||||
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
47
src/org/thoughtcrime/securesms/mms/PartParser.java
Normal file
47
src/org/thoughtcrime/securesms/mms/PartParser.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package org.thoughtcrime.securesms.mms;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import ws.com.google.android.mms.ContentType;
|
||||
import ws.com.google.android.mms.pdu.CharacterSets;
|
||||
import ws.com.google.android.mms.pdu.PduBody;
|
||||
|
||||
public class PartParser {
|
||||
public static String getMessageText(PduBody body) {
|
||||
String bodyText = null;
|
||||
|
||||
for (int i=0;i<body.getPartsNum();i++) {
|
||||
if (ContentType.isTextType(Util.toIsoString(body.getPart(i).getContentType()))) {
|
||||
String partText;
|
||||
|
||||
try {
|
||||
partText = new String(body.getPart(i).getData(),
|
||||
CharacterSets.getMimeName(body.getPart(i).getCharset()));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
Log.w("PartParser", e);
|
||||
partText = "Unsupported Encoding!";
|
||||
}
|
||||
|
||||
bodyText = (bodyText == null) ? partText : bodyText + " " + partText;
|
||||
}
|
||||
}
|
||||
|
||||
return bodyText;
|
||||
}
|
||||
|
||||
public static PduBody getNonTextParts(PduBody body) {
|
||||
PduBody stripped = new PduBody();
|
||||
|
||||
for (int i=0;i<body.getPartsNum();i++) {
|
||||
if (!ContentType.isTextType(Util.toIsoString(body.getPart(i).getContentType()))) {
|
||||
stripped.addPart(body.getPart(i));
|
||||
}
|
||||
}
|
||||
|
||||
return stripped;
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,8 @@ import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import ws.com.google.android.mms.pdu.PduPart;
|
||||
|
||||
public abstract class Slide {
|
||||
@@ -88,10 +90,14 @@ public abstract class Slide {
|
||||
return part.getDataUri();
|
||||
}
|
||||
|
||||
public Bitmap getThumbnail() {
|
||||
public Bitmap getThumbnail(int maxWidth, int maxHeight) {
|
||||
throw new AssertionError("getThumbnail() called on non-thumbnail producing slide!");
|
||||
}
|
||||
|
||||
|
||||
public void setThumbnailOn(ImageView imageView) {
|
||||
imageView.setImageBitmap(getThumbnail(imageView.getWidth(), imageView.getHeight()));
|
||||
}
|
||||
|
||||
public boolean hasImage() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ import android.content.Context;
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ import android.graphics.BitmapFactory;
|
||||
import android.net.Uri;
|
||||
import android.provider.MediaStore;
|
||||
import android.util.Log;
|
||||
import android.widget.ImageView;
|
||||
|
||||
public class VideoSlide extends Slide {
|
||||
|
||||
@@ -41,10 +42,10 @@ public class VideoSlide extends Slide {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap getThumbnail() {
|
||||
public Bitmap getThumbnail(int width, int height) {
|
||||
return BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher_video_player);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean hasImage() {
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user