From e44b838db0a9e6990b940a8fdc139804ef6ddf7e Mon Sep 17 00:00:00 2001 From: str4d Date: Thu, 8 Jan 2015 23:23:05 +0000 Subject: [PATCH] Warn about large attachment sizes --- .../i2p/bote/android/NewEmailFragment.java | 32 +++++++++++++++++++ .../i2p/bote/android/util/BoteHelper.java | 19 +++++++++++ .../bote/android/util/ContentAttachment.java | 18 +---------- app/src/main/res/values/strings.xml | 2 ++ 4 files changed, 54 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/i2p/bote/android/NewEmailFragment.java b/app/src/main/java/i2p/bote/android/NewEmailFragment.java index f9d8d82..55b80dd 100644 --- a/app/src/main/java/i2p/bote/android/NewEmailFragment.java +++ b/app/src/main/java/i2p/bote/android/NewEmailFragment.java @@ -98,6 +98,8 @@ public class NewEmailFragment extends Fragment { public static final String QUOTE_MSG_TYPE = "type"; + private static final long MAX_RECOMMENDED_ATTACHMENT_SIZE = 1048576; + private static final int REQUEST_FILE = 1; private String mSenderKey; @@ -112,6 +114,8 @@ public class NewEmailFragment extends Fragment { EditText mSubject; EditText mContent; LinearLayout mAttachments; + private long mTotalAttachmentSize; + private View mAttachmentSizeWarning; boolean mMoreVisible; boolean mDirty; @@ -455,17 +459,45 @@ public class NewEmailFragment extends Fragment { v.findViewById(R.id.attachment_action).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { + updateAttachmentSizeCount(attachment.getSize(), false); attachment.clean(); mAttachments.removeView(v); } }); mAttachments.addView(v); + updateAttachmentSizeCount(attachment.getSize(), true); } catch (FileNotFoundException e) { e.printStackTrace(); Log.e(Constants.ANDROID_LOG_TAG, "File not found: " + uri); } } + private void updateAttachmentSizeCount(long size, boolean increase) { + if (increase) { + mTotalAttachmentSize += size; + if (mTotalAttachmentSize > MAX_RECOMMENDED_ATTACHMENT_SIZE && + mAttachmentSizeWarning == null) { + mAttachmentSizeWarning = getActivity().getLayoutInflater().inflate( + R.layout.listitem_attachment_warning, mAttachments, false); + TextView warning = (TextView) mAttachmentSizeWarning.findViewById( + R.id.attachment_warning_text); + warning.setText( + getString(R.string.attachment_size_warning, + BoteHelper.getHumanReadableSize( + getActivity(), MAX_RECOMMENDED_ATTACHMENT_SIZE)) + ); + mAttachments.addView(mAttachmentSizeWarning, 0); + } + } else { + mTotalAttachmentSize -= size; + if (mTotalAttachmentSize <= MAX_RECOMMENDED_ATTACHMENT_SIZE && + mAttachmentSizeWarning != null) { + mAttachments.removeView(mAttachmentSizeWarning); + mAttachmentSizeWarning = null; + } + } + } + private boolean sendEmail() { Email email = new Email(I2PBote.getInstance().getConfiguration().getIncludeSentTime()); try { diff --git a/app/src/main/java/i2p/bote/android/util/BoteHelper.java b/app/src/main/java/i2p/bote/android/util/BoteHelper.java index e08b920..fd99ffd 100644 --- a/app/src/main/java/i2p/bote/android/util/BoteHelper.java +++ b/app/src/main/java/i2p/bote/android/util/BoteHelper.java @@ -28,9 +28,11 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.security.GeneralSecurityException; +import java.text.NumberFormat; import java.util.Collection; import java.util.Iterator; import java.util.List; +import java.util.Locale; import javax.mail.Address; import javax.mail.MessagingException; @@ -503,4 +505,21 @@ public class BoteHelper extends GeneralHelper { Log.e(Constants.ANDROID_LOG_TAG, "Exception copying streams", e); } } + + public static String getHumanReadableSize(Context context, long size) { + int unit = (63-Long.numberOfLeadingZeros(size)) / 10; // 0 if totalBytes<1K, 1 if 1K<=totalBytes<1M, etc. + double value = (double)size / (1<<(10*unit)); + int formatStr; + switch (unit) { + case 0: formatStr = R.string.n_bytes; break; + case 1: formatStr = R.string.n_kilobytes; break; + default: formatStr = R.string.n_megabytes; + } + NumberFormat formatter = NumberFormat.getInstance(Locale.getDefault()); + if (value < 100) + formatter.setMaximumFractionDigits(1); + else + formatter.setMaximumFractionDigits(0); + return context.getString(formatStr, formatter.format(value)); + } } diff --git a/app/src/main/java/i2p/bote/android/util/ContentAttachment.java b/app/src/main/java/i2p/bote/android/util/ContentAttachment.java index 0fceaf9..8ef2435 100644 --- a/app/src/main/java/i2p/bote/android/util/ContentAttachment.java +++ b/app/src/main/java/i2p/bote/android/util/ContentAttachment.java @@ -10,8 +10,6 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.text.NumberFormat; -import java.util.Locale; import javax.activation.DataHandler; import javax.activation.DataSource; @@ -19,7 +17,6 @@ import javax.mail.MessagingException; import javax.mail.Part; import i2p.bote.Util; -import i2p.bote.android.R; import i2p.bote.email.Attachment; public class ContentAttachment implements Attachment { @@ -89,20 +86,7 @@ public class ContentAttachment implements Attachment { } public String getHumanReadableSize() { - int unit = (63-Long.numberOfLeadingZeros(mSize)) / 10; // 0 if totalBytes<1K, 1 if 1K<=totalBytes<1M, etc. - double value = (double)mSize / (1<<(10*unit)); - int formatStr; - switch (unit) { - case 0: formatStr = R.string.n_bytes; break; - case 1: formatStr = R.string.n_kilobytes; break; - default: formatStr = R.string.n_megabytes; - } - NumberFormat formatter = NumberFormat.getInstance(Locale.getDefault()); - if (value < 100) - formatter.setMaximumFractionDigits(1); - else - formatter.setMaximumFractionDigits(0); - return mCtx.getString(formatStr, formatter.format(value)); + return BoteHelper.getHumanReadableSize(mCtx, mSize); } @Override diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3cc3ce4..57cd272 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -127,6 +127,8 @@ Compose Subject Compose email + + You have more than %s of attachments. Email sending may be slow. Add at least one recipient. Stop composing email? All changes will be discarded.