From 4923d9c2a16e66e06e603e072b313bbeddfab36f Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 3 Dec 2023 21:17:26 +0100 Subject: [PATCH] Refactoring --- .../eu/faircode/email/FragmentCompose.java | 185 +++++++++--------- 1 file changed, 95 insertions(+), 90 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index cc686a3620..7375a41f6b 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -5198,102 +5198,107 @@ public class FragmentCompose extends FragmentBase { private static void resizeAttachment(Context context, EntityAttachment attachment, int resize) throws IOException { File file = attachment.getFile(context); - if (file.exists() /* upload cancelled */ && - ("image/jpg".equals(attachment.type) || - "image/jpeg".equals(attachment.type) || - "image/png".equals(attachment.type) || - "image/webp".equals(attachment.type))) { - ExifInterface exifSaved; - try { - exifSaved = new ExifInterface(file); - } catch (Throwable ex) { - Log.w(ex); - exifSaved = null; - } - - BitmapFactory.Options options = new BitmapFactory.Options(); - options.inJustDecodeBounds = true; - BitmapFactory.decodeFile(file.getAbsolutePath(), options); - - int factor = 1; - while (options.outWidth / factor > resize || - options.outHeight / factor > resize) - factor *= 2; - - Matrix rotation = ("image/jpeg".equals(attachment.type) ? ImageHelper.getImageRotation(file) : null); - Log.i("Image type=" + attachment.type + " rotation=" + rotation); - if (factor > 1 || rotation != null) { - options.inJustDecodeBounds = false; - options.inSampleSize = factor; - - Log.i("Image target size=" + resize + " factor=" + factor + " source=" + options.outWidth + "x" + options.outHeight); - Bitmap resized = BitmapFactory.decodeFile(file.getAbsolutePath(), options); - if (resized != null) { - Log.i("Image result size=" + resized.getWidth() + "x" + resized.getHeight() + " rotation=" + rotation); - - if (rotation != null) { - Bitmap rotated = Bitmap.createBitmap(resized, 0, 0, resized.getWidth(), resized.getHeight(), rotation, true); - resized.recycle(); - resized = rotated; - } + if (!file.exists()) // Upload cancelled; + return; - Bitmap.CompressFormat format; - if ("image/jpg".equals(attachment.type) || - "image/jpeg".equals(attachment.type)) - format = Bitmap.CompressFormat.JPEG; - else if ("image/png".equals(attachment.type)) - format = Bitmap.CompressFormat.PNG; - else if ("image/webp".equals(attachment.type)) - format = Bitmap.CompressFormat.WEBP; - else - throw new IllegalArgumentException("Invalid format type=" + attachment.type); + if (!"image/jpg".equals(attachment.type) && + !"image/jpeg".equals(attachment.type) && + !"image/png".equals(attachment.type) && + !"image/webp".equals(attachment.type)) { + Log.i("Skipping resize of type=" + attachment.type); + return; + } - File tmp = new File(file.getAbsolutePath() + ".tmp"); - try (OutputStream out = new BufferedOutputStream(new FileOutputStream(tmp))) { - if (!resized.compress(format, REDUCED_IMAGE_QUALITY, out)) - throw new IOException("compress"); - } catch (Throwable ex) { - Log.w(ex); - Helper.secureDelete(tmp); - } finally { - resized.recycle(); - } + ExifInterface exifSaved; + try { + exifSaved = new ExifInterface(file); + } catch (Throwable ex) { + Log.w(ex); + exifSaved = null; + } - if (tmp.exists() && tmp.length() > 0) { - Helper.secureDelete(file); - tmp.renameTo(file); - } + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; + BitmapFactory.decodeFile(file.getAbsolutePath(), options); + + int factor = 1; + while (options.outWidth / factor > resize || + options.outHeight / factor > resize) + factor *= 2; + + Matrix rotation = ("image/jpeg".equals(attachment.type) ? ImageHelper.getImageRotation(file) : null); + Log.i("Image type=" + attachment.type + " rotation=" + rotation); + if (factor > 1 || rotation != null) { + options.inJustDecodeBounds = false; + options.inSampleSize = factor; + + Log.i("Image target size=" + resize + " factor=" + factor + " source=" + options.outWidth + "x" + options.outHeight); + Bitmap resized = BitmapFactory.decodeFile(file.getAbsolutePath(), options); + if (resized != null) { + Log.i("Image result size=" + resized.getWidth() + "x" + resized.getHeight() + " rotation=" + rotation); + + if (rotation != null) { + Bitmap rotated = Bitmap.createBitmap(resized, 0, 0, resized.getWidth(), resized.getHeight(), rotation, true); + resized.recycle(); + resized = rotated; + } - DB db = DB.getInstance(context); - db.attachment().setDownloaded(attachment.id, file.length()); + Bitmap.CompressFormat format; + if ("image/jpg".equals(attachment.type) || + "image/jpeg".equals(attachment.type)) + format = Bitmap.CompressFormat.JPEG; + else if ("image/png".equals(attachment.type)) + format = Bitmap.CompressFormat.PNG; + else if ("image/webp".equals(attachment.type)) + format = Bitmap.CompressFormat.WEBP; + else + throw new IllegalArgumentException("Invalid format type=" + attachment.type); - if (exifSaved != null) - try { - ExifInterface exif = new ExifInterface(file); - - // Preserve time - if (exifSaved.hasAttribute(ExifInterface.TAG_DATETIME_ORIGINAL)) - exif.setAttribute(ExifInterface.TAG_DATETIME_ORIGINAL, - exifSaved.getAttribute(ExifInterface.TAG_DATETIME_ORIGINAL)); - if (exifSaved.hasAttribute(ExifInterface.TAG_GPS_DATESTAMP)) - exif.setAttribute(ExifInterface.TAG_GPS_DATESTAMP, - exifSaved.getAttribute(ExifInterface.TAG_GPS_DATESTAMP)); - - // Preserve location - double[] latlong = exifSaved.getLatLong(); - if (latlong != null) - exif.setLatLong(latlong[0], latlong[1]); - - // Preserve altitude - if (exifSaved.hasAttribute(ExifInterface.TAG_GPS_ALTITUDE) && - exifSaved.hasAttribute(ExifInterface.TAG_GPS_ALTITUDE_REF)) - exif.setAltitude(exifSaved.getAltitude(0)); - - exif.saveAttributes(); - } catch (Throwable ex) { - Log.w(ex); - } + File tmp = new File(file.getAbsolutePath() + ".tmp"); + try (OutputStream out = new BufferedOutputStream(new FileOutputStream(tmp))) { + if (!resized.compress(format, REDUCED_IMAGE_QUALITY, out)) + throw new IOException("compress"); + } catch (Throwable ex) { + Log.w(ex); + Helper.secureDelete(tmp); + } finally { + resized.recycle(); + } + + if (tmp.exists() && tmp.length() > 0) { + Helper.secureDelete(file); + tmp.renameTo(file); } + + DB db = DB.getInstance(context); + db.attachment().setDownloaded(attachment.id, file.length()); + + if (exifSaved != null) + try { + ExifInterface exif = new ExifInterface(file); + + // Preserve time + if (exifSaved.hasAttribute(ExifInterface.TAG_DATETIME_ORIGINAL)) + exif.setAttribute(ExifInterface.TAG_DATETIME_ORIGINAL, + exifSaved.getAttribute(ExifInterface.TAG_DATETIME_ORIGINAL)); + if (exifSaved.hasAttribute(ExifInterface.TAG_GPS_DATESTAMP)) + exif.setAttribute(ExifInterface.TAG_GPS_DATESTAMP, + exifSaved.getAttribute(ExifInterface.TAG_GPS_DATESTAMP)); + + // Preserve location + double[] latlong = exifSaved.getLatLong(); + if (latlong != null) + exif.setLatLong(latlong[0], latlong[1]); + + // Preserve altitude + if (exifSaved.hasAttribute(ExifInterface.TAG_GPS_ALTITUDE) && + exifSaved.hasAttribute(ExifInterface.TAG_GPS_ALTITUDE_REF)) + exif.setAltitude(exifSaved.getAltitude(0)); + + exif.saveAttributes(); + } catch (Throwable ex) { + Log.w(ex); + } } } }