Compare commits

..

4 Commits

Author SHA1 Message Date
akwizgran
7345d5c22c Add WebP library for API 16. 2021-05-24 15:09:19 +01:00
akwizgran
b4a2725f03 Add workarounds for missing MIME type information on API 16. 2021-05-24 14:56:50 +01:00
akwizgran
a5972e26fe Use WebP for compressing images. 2021-05-21 12:43:50 +01:00
akwizgran
aabb0bfb4a Allow WebP images to be used as attachments and avatars. 2021-05-21 12:43:19 +01:00
16 changed files with 41 additions and 32 deletions

View File

@@ -42,7 +42,7 @@ configurations {
dependencies { dependencies {
implementation project(path: ':bramble-core', configuration: 'default') implementation project(path: ':bramble-core', configuration: 'default')
tor 'org.briarproject:tor-android:0.3.5.14' tor 'org.briarproject:tor-android:0.3.5.13@zip'
tor 'org.briarproject:obfs4proxy-android:0.0.12-dev-40245c4a@zip' tor 'org.briarproject:obfs4proxy-android:0.0.12-dev-40245c4a@zip'
annotationProcessor 'com.google.dagger:dagger-compiler:2.24' annotationProcessor 'com.google.dagger:dagger-compiler:2.24'

View File

@@ -119,7 +119,12 @@ public class AndroidUtils {
* Returns an array of supported content types for image attachments. * Returns an array of supported content types for image attachments.
*/ */
public static String[] getSupportedImageContentTypes() { public static String[] getSupportedImageContentTypes() {
return new String[] {"image/jpeg", "image/png", "image/gif"}; return new String[] {
"image/jpeg",
"image/png",
"image/gif",
"image/webp"
};
} }
@Nullable @Nullable

View File

@@ -76,7 +76,7 @@ dependencyVerification {
'org.bouncycastle:bcpkix-jdk15on:1.56:bcpkix-jdk15on-1.56.jar:7043dee4e9e7175e93e0b36f45b1ec1ecb893c5f755667e8b916eb8dd201c6ca', 'org.bouncycastle:bcpkix-jdk15on:1.56:bcpkix-jdk15on-1.56.jar:7043dee4e9e7175e93e0b36f45b1ec1ecb893c5f755667e8b916eb8dd201c6ca',
'org.bouncycastle:bcprov-jdk15on:1.56:bcprov-jdk15on-1.56.jar:963e1ee14f808ffb99897d848ddcdb28fa91ddda867eb18d303e82728f878349', 'org.bouncycastle:bcprov-jdk15on:1.56:bcprov-jdk15on-1.56.jar:963e1ee14f808ffb99897d848ddcdb28fa91ddda867eb18d303e82728f878349',
'org.briarproject:obfs4proxy-android:0.0.12-dev-40245c4a:obfs4proxy-android-0.0.12-dev-40245c4a.zip:8ab05a8f8391be2cb5ab2b665c281a06d9e3a756bd0f95a40a36ca927866ea82', 'org.briarproject:obfs4proxy-android:0.0.12-dev-40245c4a:obfs4proxy-android-0.0.12-dev-40245c4a.zip:8ab05a8f8391be2cb5ab2b665c281a06d9e3a756bd0f95a40a36ca927866ea82',
'org.briarproject:tor-android:0.3.5.14:tor-android-0.3.5.14.jar:92bdb9890de8f14e9e90d1e3aff49a7a4b9262f19904410ae0a3baf94ed6be33', 'org.briarproject:tor-android:0.3.5.13:tor-android-0.3.5.13.zip:e0978db136731dae07774b722970cdae1e462fb5adc82845dd80a7e2d87f356c',
'org.checkerframework:checker-compat-qual:2.5.3:checker-compat-qual-2.5.3.jar:d76b9afea61c7c082908023f0cbc1427fab9abd2df915c8b8a3e7a509bccbc6d', 'org.checkerframework:checker-compat-qual:2.5.3:checker-compat-qual-2.5.3.jar:d76b9afea61c7c082908023f0cbc1427fab9abd2df915c8b8a3e7a509bccbc6d',
'org.checkerframework:checker-qual:2.5.2:checker-qual-2.5.2.jar:64b02691c8b9d4e7700f8ee2e742dce7ea2c6e81e662b7522c9ee3bf568c040a', 'org.checkerframework:checker-qual:2.5.2:checker-qual-2.5.2.jar:64b02691c8b9d4e7700f8ee2e742dce7ea2c6e81e662b7522c9ee3bf568c040a',
'org.checkerframework:checker-qual:2.8.1:checker-qual-2.8.1.jar:9103499008bcecd4e948da29b17864abb64304e15706444ae209d17ebe0575df', 'org.checkerframework:checker-qual:2.8.1:checker-qual-2.8.1.jar:9103499008bcecd4e948da29b17864abb64304e15706444ae209d17ebe0575df',

View File

@@ -16,7 +16,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: '*.jar') implementation fileTree(dir: 'libs', include: '*.jar')
implementation 'net.java.dev.jna:jna:4.5.2' implementation 'net.java.dev.jna:jna:4.5.2'
implementation 'net.java.dev.jna:jna-platform:4.5.2' implementation 'net.java.dev.jna:jna-platform:4.5.2'
tor 'org.briarproject:tor:0.3.5.14' tor 'org.briarproject:tor:0.3.5.13-1@zip'
tor 'org.briarproject:obfs4proxy:0.0.12-dev-40245c4a@zip' tor 'org.briarproject:obfs4proxy:0.0.12-dev-40245c4a@zip'
annotationProcessor 'com.google.dagger:dagger-compiler:2.24' annotationProcessor 'com.google.dagger:dagger-compiler:2.24'

View File

@@ -24,7 +24,7 @@ dependencyVerification {
'org.apache.ant:ant:1.9.4:ant-1.9.4.jar:649ae0730251de07b8913f49286d46bba7b92d47c5f332610aa426c4f02161d8', 'org.apache.ant:ant:1.9.4:ant-1.9.4.jar:649ae0730251de07b8913f49286d46bba7b92d47c5f332610aa426c4f02161d8',
'org.beanshell:bsh:1.3.0:bsh-1.3.0.jar:9b04edc75d19db54f1b4e8b5355e9364384c6cf71eb0a1b9724c159d779879f8', 'org.beanshell:bsh:1.3.0:bsh-1.3.0.jar:9b04edc75d19db54f1b4e8b5355e9364384c6cf71eb0a1b9724c159d779879f8',
'org.briarproject:obfs4proxy:0.0.12-dev-40245c4a:obfs4proxy-0.0.12-dev-40245c4a.zip:172029e7058b3a83ac93ac4991a44bf76e16ce8d46f558f5836d57da3cb3a766', 'org.briarproject:obfs4proxy:0.0.12-dev-40245c4a:obfs4proxy-0.0.12-dev-40245c4a.zip:172029e7058b3a83ac93ac4991a44bf76e16ce8d46f558f5836d57da3cb3a766',
'org.briarproject:tor:0.3.5.14:tor-0.3.5.14.jar:a969ab3f4bde4a7fc5945c53935cf3bca50de44451a34ebeca8add93e2a9af60', 'org.briarproject:tor:0.3.5.13-1:tor-0.3.5.13-1.zip:ef35c16bf8dc1f4c75ed71d9f55e4514f383d124ec96b859aca647c990927c99',
'org.checkerframework:checker-compat-qual:2.5.3:checker-compat-qual-2.5.3.jar:d76b9afea61c7c082908023f0cbc1427fab9abd2df915c8b8a3e7a509bccbc6d', 'org.checkerframework:checker-compat-qual:2.5.3:checker-compat-qual-2.5.3.jar:d76b9afea61c7c082908023f0cbc1427fab9abd2df915c8b8a3e7a509bccbc6d',
'org.checkerframework:checker-qual:2.5.2:checker-qual-2.5.2.jar:64b02691c8b9d4e7700f8ee2e742dce7ea2c6e81e662b7522c9ee3bf568c040a', 'org.checkerframework:checker-qual:2.5.2:checker-qual-2.5.2.jar:64b02691c8b9d4e7700f8ee2e742dce7ea2c6e81e662b7522c9ee3bf568c040a',
'org.codehaus.mojo:animal-sniffer-annotations:1.17:animal-sniffer-annotations-1.17.jar:92654f493ecfec52082e76354f0ebf87648dc3d5cec2e3c3cdb947c016747a53', 'org.codehaus.mojo:animal-sniffer-annotations:1.17:animal-sniffer-annotations-1.17.jar:92654f493ecfec52082e76354f0ebf87648dc3d5cec2e3c3cdb947c016747a53',

View File

@@ -121,6 +121,7 @@ dependencies {
exclude group: 'com.android.support' exclude group: 'com.android.support'
exclude module: 'disklrucache' // when there's no disk cache, we can't accidentally use it exclude module: 'disklrucache' // when there's no disk cache, we can't accidentally use it
} }
implementation "com.github.zjupure:webpdecoder:2.0.$glideVersion"
annotationProcessor 'com.google.dagger:dagger-compiler:2.24' annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
annotationProcessor "com.github.bumptech.glide:compiler:$glideVersion" annotationProcessor "com.github.bumptech.glide:compiler:$glideVersion"

View File

@@ -102,12 +102,10 @@ class AttachmentCreationTask {
} }
InputStream is = contentResolver.openInputStream(uri); InputStream is = contentResolver.openInputStream(uri);
if (is == null) throw new IOException(); if (is == null) throw new IOException();
is = imageCompressor is = imageCompressor.compressImage(is, contentType);
.compressImage(is, contentType);
long timestamp = System.currentTimeMillis(); long timestamp = System.currentTimeMillis();
AttachmentHeader h = messagingManager AttachmentHeader h = messagingManager.addLocalAttachment(groupId,
.addLocalAttachment(groupId, timestamp, timestamp, ImageCompressor.MIME_TYPE, is);
ImageCompressor.MIME_TYPE, is);
tryToClose(is, LOG, WARNING); tryToClose(is, LOG, WARNING);
logDuration(LOG, "Storing attachment", start); logDuration(LOG, "Storing attachment", start);
return h; return h;

View File

@@ -11,7 +11,7 @@ public interface ImageCompressor {
/** /**
* The MIME type of compressed images * The MIME type of compressed images
*/ */
String MIME_TYPE = "image/jpeg"; String MIME_TYPE = "image/webp";
/** /**
* Load an image from {@code is}, compress it and return an InputStream * Load an image from {@code is}, compress it and return an InputStream

View File

@@ -12,7 +12,7 @@ import java.util.logging.Logger;
import javax.inject.Inject; import javax.inject.Inject;
import static android.graphics.Bitmap.CompressFormat.JPEG; import static android.graphics.Bitmap.CompressFormat.WEBP;
import static android.graphics.BitmapFactory.decodeStream; import static android.graphics.BitmapFactory.decodeStream;
import static java.util.logging.Level.INFO; import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
@@ -50,7 +50,7 @@ class ImageCompressorImpl implements ImageCompressor {
public InputStream compressImage(Bitmap bitmap) throws IOException { public InputStream compressImage(Bitmap bitmap) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int quality = 100; quality >= 0; quality -= 10) { for (int quality = 100; quality >= 0; quality -= 10) {
if (!bitmap.compress(JPEG, quality, out)) if (!bitmap.compress(WEBP, quality, out))
throw new IOException(); throw new IOException();
if (out.size() <= MAX_IMAGE_SIZE) { if (out.size() <= MAX_IMAGE_SIZE) {
if (LOG.isLoggable(INFO)) { if (LOG.isLoggable(INFO)) {

View File

@@ -3,6 +3,8 @@ package org.briarproject.briar.android.attachment.media;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
import com.bumptech.glide.integration.webp.WebpBitmapFactory;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.io.InputStream; import java.io.InputStream;
@@ -25,6 +27,10 @@ class ImageHelperImpl implements ImageHelper {
BitmapFactory.Options options = new BitmapFactory.Options(); BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options); BitmapFactory.decodeStream(is, null, options);
if (options.outWidth < 1 || options.outHeight < 1) {
// BitmapFactory doesn't fully support WebP on API < 17
WebpBitmapFactory.decodeStream(is, null, options);
}
String mimeType = options.outMimeType; String mimeType = options.outMimeType;
if (mimeType == null) mimeType = ""; if (mimeType == null) mimeType = "";
return new DecodeResult(options.outWidth, options.outHeight, return new DecodeResult(options.outWidth, options.outHeight,
@@ -34,6 +40,7 @@ class ImageHelperImpl implements ImageHelper {
@Nullable @Nullable
@Override @Override
public String getExtensionFromMimeType(String mimeType) { public String getExtensionFromMimeType(String mimeType) {
if ("image/webp".equals(mimeType)) return "webp";
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(mimeType); return mimeTypeMap.getExtensionFromMimeType(mimeType);
} }

View File

@@ -21,6 +21,7 @@ import static androidx.exifinterface.media.ExifInterface.TAG_ORIENTATION;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger; import static java.util.logging.Logger.getLogger;
import static org.briarproject.bramble.util.LogUtils.logException; import static org.briarproject.bramble.util.LogUtils.logException;
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
@NotNullByDefault @NotNullByDefault
class ImageSizeCalculatorImpl implements ImageSizeCalculator { class ImageSizeCalculatorImpl implements ImageSizeCalculator {
@@ -61,6 +62,10 @@ class ImageSizeCalculatorImpl implements ImageSizeCalculator {
logException(LOG, WARNING, e); logException(LOG, WARNING, e);
} }
} }
if (!size.hasError() && isNullOrEmpty(size.getMimeType())) {
LOG.info("Could not determine content type, using supplied type");
size = new Size(size.getWidth(), size.getHeight(), contentType);
}
return size; return size;
} }

View File

@@ -4,6 +4,8 @@ import android.graphics.Bitmap;
import android.view.View; import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import com.bumptech.glide.integration.webp.decoder.WebpDrawable;
import com.bumptech.glide.integration.webp.decoder.WebpDrawableTransformation;
import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.Transformation;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
@@ -80,7 +82,9 @@ class ImageViewHolder extends ViewHolder {
.load(a.getHeader()) .load(a.getHeader())
.diskCacheStrategy(NONE) .diskCacheStrategy(NONE)
.error(ERROR_RES) .error(ERROR_RES)
.transform(transformation) .optionalTransform(transformation)
.optionalTransform(WebpDrawable.class,
new WebpDrawableTransformation(transformation))
.transition(withCrossFade()) .transition(withCrossFade())
.into(imageView) .into(imageView)
.waitForLayout(); .waitForLayout();

View File

@@ -21,7 +21,6 @@ import org.briarproject.briar.android.view.ImagePreview.ImagePreviewListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.logging.Logger;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.UiThread; import androidx.annotation.UiThread;
@@ -38,8 +37,6 @@ import static android.widget.Toast.LENGTH_LONG;
import static androidx.core.content.ContextCompat.getColor; import static androidx.core.content.ContextCompat.getColor;
import static androidx.customview.view.AbsSavedState.EMPTY_STATE; import static androidx.customview.view.AbsSavedState.EMPTY_STATE;
import static androidx.lifecycle.Lifecycle.State.DESTROYED; import static androidx.lifecycle.Lifecycle.State.DESTROYED;
import static java.util.logging.Level.INFO;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.briar.android.util.UiUtils.resolveColorAttribute; import static org.briarproject.briar.android.util.UiUtils.resolveColorAttribute;
import static org.briarproject.briar.android.view.TextSendController.SendState.SENT; import static org.briarproject.briar.android.view.TextSendController.SendState.SENT;
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_ATTACHMENTS_PER_MESSAGE; import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_ATTACHMENTS_PER_MESSAGE;
@@ -49,9 +46,6 @@ import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_ATTACH
public class TextAttachmentController extends TextSendController public class TextAttachmentController extends TextSendController
implements ImagePreviewListener { implements ImagePreviewListener {
private static final Logger LOG =
getLogger(TextAttachmentController.class.getName());
private final ImagePreview imagePreview; private final ImagePreview imagePreview;
private final AttachmentListener attachmentListener; private final AttachmentListener attachmentListener;
private final CompositeSendButton sendButton; private final CompositeSendButton sendButton;
@@ -73,7 +67,6 @@ public class TextAttachmentController extends TextSendController
sendButton.setOnImageClickListener(view -> onImageButtonClicked()); sendButton.setOnImageClickListener(view -> onImageButtonClicked());
} }
@UiThread
@Override @Override
protected void updateViewState() { protected void updateViewState() {
super.updateViewState(); super.updateViewState();
@@ -167,13 +160,10 @@ public class TextAttachmentController extends TextSendController
* at most once per call to * at most once per call to
* {@link Activity#startActivityForResult(Intent, int)}. * {@link Activity#startActivityForResult(Intent, int)}.
*/ */
@UiThread
@SuppressWarnings("JavadocReference") @SuppressWarnings("JavadocReference")
public void onImageReceived(@Nullable Intent resultData) { public void onImageReceived(@Nullable Intent resultData) {
if (LOG.isLoggable(INFO)) LOG.info("onImageReceived: " + resultData);
if (resultData == null) return; if (resultData == null) return;
if (loadingUris) throw new AssertionError("still loading URIs"); if (loadingUris || !imageUris.isEmpty()) throw new AssertionError();
if (!imageUris.isEmpty()) throw new AssertionError("uris " + imageUris);
List<Uri> newUris = new ArrayList<>(); List<Uri> newUris = new ArrayList<>();
if (resultData.getData() != null) { if (resultData.getData() != null) {
newUris.add(resultData.getData()); newUris.add(resultData.getData());
@@ -187,7 +177,6 @@ public class TextAttachmentController extends TextSendController
} }
} }
@UiThread
private void onNewUris(boolean restart, List<Uri> newUris) { private void onNewUris(boolean restart, List<Uri> newUris) {
if (newUris.isEmpty()) return; if (newUris.isEmpty()) return;
if (loadingUris) throw new AssertionError(); if (loadingUris) throw new AssertionError();
@@ -208,9 +197,8 @@ public class TextAttachmentController extends TextSendController
@Override @Override
public void onChanged(@Nullable AttachmentResult attachmentResult) { public void onChanged(@Nullable AttachmentResult attachmentResult) {
if (attachmentResult == null) { if (attachmentResult == null) {
// The fresh LiveData was deliberately set to null // The fresh LiveData was deliberately set to null.
// when the AttachmentCreator reset its state. // This means that we can stop observing it.
// This means that we can simply stop observing it.
result.removeObserver(this); result.removeObserver(this);
} else { } else {
boolean noError = onNewAttachmentItemResults( boolean noError = onNewAttachmentItemResults(
@@ -282,7 +270,6 @@ public class TextAttachmentController extends TextSendController
onCancel(); onCancel();
} }
@UiThread
@Override @Override
public void onCancel() { public void onCancel() {
textInput.clearText(); textInput.clearText();

View File

@@ -6,6 +6,9 @@ import java.io.InputStream;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public interface TestAvatarCreator { public interface TestAvatarCreator {
String MIME_TYPE = "image/webp";
@Nullable @Nullable
InputStream getAvatarInputStream() throws IOException; InputStream getAvatarInputStream() throws IOException;
} }

View File

@@ -316,7 +316,7 @@ public class TestDataCreatorImpl implements TestDataCreator {
Message m; Message m;
try { try {
m = avatarMessageEncoder.encodeUpdateMessage(groupId, 0, m = avatarMessageEncoder.encodeUpdateMessage(groupId, 0,
"image/jpeg", is).getFirst(); TestAvatarCreator.MIME_TYPE, is).getFirst();
} catch (IOException e) { } catch (IOException e) {
throw new DbException(e); throw new DbException(e);
} }

View File

@@ -4,7 +4,6 @@ allprojects {
repositories { repositories {
jcenter() jcenter()
mavenLocal() mavenLocal()
mavenCentral()
google() google()
maven { url "https://jitpack.io" } maven { url "https://jitpack.io" }
} }