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:
Moxie Marlinspike
2013-04-26 11:23:43 -07:00
parent dd0aecc811
commit 7c47ea5cec
29 changed files with 747 additions and 288 deletions

View File

@@ -0,0 +1,84 @@
package org.thoughtcrime.securesms.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class BitmapUtil {
private static final int MAX_COMPRESSION_QUALITY = 95;
private static final int MIN_COMPRESSION_QUALITY = 50;
private static final int MAX_COMPRESSION_ATTEMPTS = 4;
public static byte[] createScaledBytes(Context context, Uri uri, int maxWidth,
int maxHeight, int maxSize)
throws IOException
{
InputStream measure = context.getContentResolver().openInputStream(uri);
InputStream data = context.getContentResolver().openInputStream(uri);
Bitmap bitmap = createScaledBitmap(measure, data, maxWidth, maxHeight);
int quality = MAX_COMPRESSION_QUALITY;
int attempts = 0;
ByteArrayOutputStream baos;
do {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
quality = Math.max((quality * maxSize) / baos.size(), MIN_COMPRESSION_QUALITY);
} while (baos.size() > maxSize && attempts++ < MAX_COMPRESSION_ATTEMPTS);
bitmap.recycle();
if (baos.size() <= maxSize) return baos.toByteArray();
else throw new IOException("Unable to scale image below: " + baos.size());
}
public static Bitmap createScaledBitmap(InputStream measure, InputStream data,
int maxWidth, int maxHeight)
{
BitmapFactory.Options options = getImageDimensions(measure);
int imageWidth = options.outWidth;
int imageHeight = options.outHeight;
int scaler = 1;
while ((imageWidth / scaler > maxWidth) && (imageHeight / scaler > maxHeight))
scaler *= 2;
if (scaler > 1)
scaler /= 2;
options.inSampleSize = scaler;
options.inJustDecodeBounds = false;
Bitmap roughThumbnail = BitmapFactory.decodeStream(data, null, options);
if (imageWidth > maxWidth || imageHeight > maxHeight) {
Log.w("BitmapUtil", "Scaling to max width and height: " + maxWidth + "," + maxHeight);
Bitmap scaledThumbnail = Bitmap.createScaledBitmap(roughThumbnail, maxWidth, maxHeight, true);
roughThumbnail.recycle();
return scaledThumbnail;
} else {
return roughThumbnail;
}
}
private static BitmapFactory.Options getImageDimensions(InputStream inputStream) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);
return options;
}
}

View File

@@ -1,20 +1,30 @@
package org.thoughtcrime.securesms.util;
import java.lang.ref.WeakReference;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class ListenableFutureTask<V> extends FutureTask<V> {
// private WeakReference<FutureTaskListener<V>> listener;
private FutureTaskListener<V> listener;
public ListenableFutureTask(Callable<V> callable, FutureTaskListener<V> listener) {
super(callable);
this.listener = listener;
// if (listener == null) {
// this.listener = null;
// } else {
// this.listener = new WeakReference<FutureTaskListener<V>>(listener);
// }
}
public synchronized void setListener(FutureTaskListener<V> listener) {
// if (listener != null) this.listener = new WeakReference<FutureTaskListener<V>>(listener);
// else this.listener = null;
this.listener = listener;
if (this.isDone()) {
callback();
}
@@ -27,12 +37,16 @@ public class ListenableFutureTask<V> extends FutureTask<V> {
private void callback() {
if (this.listener != null) {
try {
this.listener.onSuccess(get());
} catch (ExecutionException ee) {
this.listener.onFailure(ee);
} catch (InterruptedException e) {
throw new AssertionError(e);
FutureTaskListener<V> nestedListener = this.listener;
// FutureTaskListener<V> nestedListener = this.listener.get();
if (nestedListener != null) {
try {
nestedListener.onSuccess(get());
} catch (ExecutionException ee) {
nestedListener.onFailure(ee);
} catch (InterruptedException e) {
throw new AssertionError(e);
}
}
}
}

View File

@@ -22,17 +22,20 @@ import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.util.Log;
import android.widget.EditText;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import ws.com.google.android.mms.pdu.CharacterSets;
import ws.com.google.android.mms.pdu.EncodedStringValue;
public class Util {
@@ -129,6 +132,25 @@ public class Util {
return spanned;
}
public static String toIsoString(byte[] bytes) {
try {
return new String(bytes, CharacterSets.MIMENAME_ISO_8859_1);
} catch (UnsupportedEncodingException e) {
// Impossible to reach here!
Log.e("MmsDatabase", "ISO_8859_1 must be supported!", e);
return "";
}
}
public static byte[] toIsoBytes(String isoString) {
try {
return isoString.getBytes(CharacterSets.MIMENAME_ISO_8859_1);
} catch (UnsupportedEncodingException e) {
Log.w("Util", "ISO_8859_1 must be supported!", e);
return new byte[0];
}
}
public static void showAlertDialog(Context context, String title, String message) {
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(title);