Warn about large attachment sizes

This commit is contained in:
str4d
2015-01-08 23:23:05 +00:00
parent a62c805961
commit e44b838db0
4 changed files with 54 additions and 17 deletions

View File

@@ -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 {

View File

@@ -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));
}
}

View File

@@ -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

View File

@@ -127,6 +127,8 @@
<string name="compose">Compose</string>
<string name="subject">Subject</string>
<string name="compose_email">Compose email</string>
<!-- Argument is a file size, e.g. "500 KB" -->
<string name="attachment_size_warning">You have more than %s of attachments. Email sending may be slow.</string>
<string name="add_one_recipient">Add at least one recipient.</string>
<string name="stop_composing_email">Stop composing email?</string>
<string name="all_changes_will_be_discarded">All changes will be discarded.</string>