recover if attachImage canโ€™t generate scalled down version of image. fixes #3773

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/persistance/FileBackend.java        | 17 
src/main/java/eu/siacs/conversations/services/XmppConnectionService.java | 26 
src/main/java/eu/siacs/conversations/ui/util/Attachment.java             | 22 
3 files changed, 36 insertions(+), 29 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/persistance/FileBackend.java ๐Ÿ”—

@@ -704,7 +704,7 @@ public class FileBackend {
         return pos > 0 ? filename.substring(pos + 1) : null;
     }
 
-    private void copyImageToPrivateStorage(File file, Uri image, int sampleSize) throws FileCopyException {
+    private void copyImageToPrivateStorage(File file, Uri image, int sampleSize) throws FileCopyException, NotAnImageFileException {
         file.getParentFile().mkdirs();
         InputStream is = null;
         OutputStream os = null;
@@ -724,7 +724,7 @@ public class FileBackend {
             originalBitmap = BitmapFactory.decodeStream(is, null, options);
             is.close();
             if (originalBitmap == null) {
-                throw new FileCopyException(R.string.error_not_an_image_file);
+                throw new NotAnImageFileException();
             }
             Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE);
             int rotation = getRotation(image);
@@ -763,12 +763,12 @@ public class FileBackend {
         }
     }
 
-    public void copyImageToPrivateStorage(File file, Uri image) throws FileCopyException {
+    public void copyImageToPrivateStorage(File file, Uri image) throws FileCopyException, NotAnImageFileException {
         Log.d(Config.LOGTAG, "copy image (" + image.toString() + ") to private storage " + file.getAbsolutePath());
         copyImageToPrivateStorage(file, image, 0);
     }
 
-    public void copyImageToPrivateStorage(Message message, Uri image) throws FileCopyException {
+    public void copyImageToPrivateStorage(Message message, Uri image) throws FileCopyException, NotAnImageFileException {
         switch (Config.IMAGE_FORMAT) {
             case JPEG:
                 message.setRelativeFilePath(message.getUuid() + ".jpg");
@@ -1420,11 +1420,14 @@ public class FileBackend {
         }
     }
 
-    public class FileCopyException extends Exception {
-        private static final long serialVersionUID = -1010013599132881427L;
+    public static class NotAnImageFileException extends Exception {
+
+    }
+
+    public static class FileCopyException extends Exception {
         private int resId;
 
-        public FileCopyException(int resId) {
+        private FileCopyException(int resId) {
             this.resId = resId;
         }
 

src/main/java/eu/siacs/conversations/services/XmppConnectionService.java ๐Ÿ”—

@@ -567,19 +567,23 @@ public class XmppConnectionService extends Service {
         mFileAddingExecutor.execute(() -> {
             try {
                 getFileBackend().copyImageToPrivateStorage(message, uri);
-                if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
-                    final PgpEngine pgpEngine = getPgpEngine();
-                    if (pgpEngine != null) {
-                        pgpEngine.encrypt(message, callback);
-                    } else if (callback != null) {
-                        callback.error(R.string.unable_to_connect_to_keychain, null);
-                    }
-                } else {
-                    sendMessage(message);
-                    callback.success(message);
-                }
+            } catch (FileBackend.NotAnImageFileException e) {
+                attachFileToConversation(conversation, uri, mimeType, callback);
+                return;
             } catch (final FileBackend.FileCopyException e) {
                 callback.error(e.getResId(), message);
+                return;
+            }
+            if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
+                final PgpEngine pgpEngine = getPgpEngine();
+                if (pgpEngine != null) {
+                    pgpEngine.encrypt(message, callback);
+                } else if (callback != null) {
+                    callback.error(R.string.unable_to_connect_to_keychain, null);
+                }
+            } else {
+                sendMessage(message);
+                callback.success(message);
             }
         });
     }

src/main/java/eu/siacs/conversations/ui/util/Attachment.java ๐Ÿ”—

@@ -35,7 +35,6 @@ import android.content.Intent;
 import android.net.Uri;
 import android.os.Parcel;
 import android.os.Parcelable;
-import android.util.Log;
 
 import java.io.File;
 import java.util.ArrayList;
@@ -43,7 +42,6 @@ import java.util.Collections;
 import java.util.List;
 import java.util.UUID;
 
-import eu.siacs.conversations.Config;
 import eu.siacs.conversations.utils.Compatibility;
 import eu.siacs.conversations.utils.MimeUtils;
 
@@ -113,7 +111,7 @@ public class Attachment implements Parcelable {
     }
 
     public static boolean canBeSendInband(final List<Attachment> attachments) {
-        for(Attachment attachment : attachments) {
+        for (Attachment attachment : attachments) {
             if (attachment.type != Type.LOCATION) {
                 return false;
             }
@@ -122,21 +120,21 @@ public class Attachment implements Parcelable {
     }
 
     public static List<Attachment> of(final Context context, Uri uri, Type type) {
-        final String mime = type == Type.LOCATION ?null :MimeUtils.guessMimeTypeFromUri(context, uri);
+        final String mime = type == Type.LOCATION ? null : MimeUtils.guessMimeTypeFromUri(context, uri);
         return Collections.singletonList(new Attachment(uri, type, mime));
     }
 
     public static List<Attachment> of(final Context context, List<Uri> uris) {
         List<Attachment> attachments = new ArrayList<>();
-        for(Uri uri : uris) {
+        for (Uri uri : uris) {
             final String mime = MimeUtils.guessMimeTypeFromUri(context, uri);
-            attachments.add(new Attachment(uri, mime != null && mime.startsWith("image/") ? Type.IMAGE : Type.FILE,mime));
+            attachments.add(new Attachment(uri, mime != null && isImage(mime) ? Type.IMAGE : Type.FILE, mime));
         }
         return attachments;
     }
 
     public static Attachment of(UUID uuid, final File file, String mime) {
-        return new Attachment(uuid, Uri.fromFile(file),mime != null && (mime.startsWith("image/") || mime.startsWith("video/")) ? Type.IMAGE : Type.FILE, mime);
+        return new Attachment(uuid, Uri.fromFile(file), mime != null && (isImage(mime) || mime.startsWith("video/")) ? Type.IMAGE : Type.FILE, mime);
     }
 
     public static List<Attachment> extractAttachments(final Context context, final Intent intent, Type type) {
@@ -151,9 +149,7 @@ public class Attachment implements Parcelable {
             if (clipData != null) {
                 for (int i = 0; i < clipData.getItemCount(); ++i) {
                     final Uri uri = clipData.getItemAt(i).getUri();
-                    Log.d(Config.LOGTAG,"uri="+uri+" contentType="+contentType);
                     final String mime = MimeUtils.guessMimeTypeFromUriAndMime(context, uri, contentType);
-                    Log.d(Config.LOGTAG,"mime="+mime);
                     uris.add(new Attachment(uri, type, mime));
                 }
             }
@@ -165,12 +161,12 @@ public class Attachment implements Parcelable {
     }
 
     public boolean renderThumbnail() {
-        return type == Type.IMAGE  || (type == Type.FILE && mime != null && renderFileThumbnail(mime));
+        return type == Type.IMAGE || (type == Type.FILE && mime != null && renderFileThumbnail(mime));
     }
 
     private static boolean renderFileThumbnail(final String mime) {
         return mime.startsWith("video/")
-                || mime.startsWith("image/")
+                || isImage(mime)
                 || (Compatibility.runsTwentyOne() && "application/pdf".equals(mime));
     }
 
@@ -181,4 +177,8 @@ public class Attachment implements Parcelable {
     public UUID getUuid() {
         return uuid;
     }
+
+    private static boolean isImage(final String mime) {
+        return mime.startsWith("image/") && !mime.equals("image/svg+xml");
+    }
 }