From f6dc26689791d43a3ee1e54b4544810e75d9655e Mon Sep 17 00:00:00 2001 From: Kamil Rajtar Date: Mon, 7 May 2018 10:59:58 +0200 Subject: [PATCH 1/2] Attachment download dialog added memory units and changed size from int to long --- .../AttachmentDownloadDialogFragment.java | 35 ++++++++++++++++--- .../ui/messageview/MessageViewFragment.java | 2 +- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java b/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java index 7d2abe721..4a1d4691b 100644 --- a/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java +++ b/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java @@ -23,27 +23,40 @@ public class AttachmentDownloadDialogFragment extends DialogFragment { private MessagingController messagingController; - public static AttachmentDownloadDialogFragment newInstance(int size, String message) { + public static AttachmentDownloadDialogFragment newInstance(long size, String message) { AttachmentDownloadDialogFragment fragment = new AttachmentDownloadDialogFragment(); Bundle args = new Bundle(); - args.putInt(ARG_SIZE, size); + args.putLong(ARG_SIZE, size); args.putString(ARG_MESSAGE, message); fragment.setArguments(args); return fragment; } + + private SizeUnit getAppropriateSizeUnit(long size) { + for (SizeUnit sizeUnit : SizeUnit.values()) { + if (size < 1024 * 10 * sizeUnit.size) { + return sizeUnit; + } + } + return SizeUnit.B; + } + @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Bundle args = getArguments(); - int size = args.getInt(ARG_SIZE); + long size = args.getLong(ARG_SIZE); String message = args.getString(ARG_MESSAGE); + final SizeUnit sizeUnit = getAppropriateSizeUnit(size); + + messagingListener = new SimpleMessagingListener() { @Override public void updateProgress(int progress) { - dialog.setProgress(progress); + dialog.setProgress((int) (progress / sizeUnit.size)); } }; @@ -52,14 +65,26 @@ public class AttachmentDownloadDialogFragment extends DialogFragment { dialog = new ProgressDialog(getActivity()); dialog.setMessage(message); - dialog.setMax(size); + dialog.setMax((int) (size / sizeUnit.size)); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setProgress(0); + dialog.setProgressNumberFormat("%1d/%2d " + sizeUnit.name()); dialog.show(); return dialog; } + private enum SizeUnit { + B(1), KB(1024L), MB(1024L * 1024L), GB(1024L * 1024L * 1024L), TB(1024L * 1024L * 1024L * 1024L), PB( + 1024L * 1024L * 1024L * 1024L * 1024L); + + public final long size; + + SizeUnit(long size) { + this.size = size; + } + } + @Override public void onDestroyView() { messagingController.removeListener(messagingListener); diff --git a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java index 53771af98..6e468793e 100644 --- a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java +++ b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java @@ -544,7 +544,7 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF } case R.id.dialog_attachment_progress: { String message = getString(R.string.dialog_attachment_progress_title); - int size = (int) currentAttachmentViewInfo.size; + long size = currentAttachmentViewInfo.size; fragment = AttachmentDownloadDialogFragment.newInstance(size, message); break; } From c0e95bfc1b8d83f08e074a21c58722365700490f Mon Sep 17 00:00:00 2001 From: cketti Date: Mon, 7 May 2018 18:02:26 +0200 Subject: [PATCH 2/2] Refactor AttachmentDownloadDialogFragment --- .../AttachmentDownloadDialogFragment.java | 62 +++++++++++-------- .../ui/messageview/MessageViewFragment.java | 2 +- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java b/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java index 4a1d4691b..95e45f831 100644 --- a/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java +++ b/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java @@ -34,29 +34,18 @@ public class AttachmentDownloadDialogFragment extends DialogFragment { return fragment; } - - private SizeUnit getAppropriateSizeUnit(long size) { - for (SizeUnit sizeUnit : SizeUnit.values()) { - if (size < 1024 * 10 * sizeUnit.size) { - return sizeUnit; - } - } - return SizeUnit.B; - } - @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Bundle args = getArguments(); long size = args.getLong(ARG_SIZE); String message = args.getString(ARG_MESSAGE); - final SizeUnit sizeUnit = getAppropriateSizeUnit(size); - + final SizeUnit sizeUnit = SizeUnit.getAppropriateFor(size); messagingListener = new SimpleMessagingListener() { @Override public void updateProgress(int progress) { - dialog.setProgress((int) (progress / sizeUnit.size)); + dialog.setProgress(sizeUnit.valueInSizeUnit(progress)); } }; @@ -65,26 +54,15 @@ public class AttachmentDownloadDialogFragment extends DialogFragment { dialog = new ProgressDialog(getActivity()); dialog.setMessage(message); - dialog.setMax((int) (size / sizeUnit.size)); + dialog.setMax(sizeUnit.valueInSizeUnit(size)); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setProgress(0); - dialog.setProgressNumberFormat("%1d/%2d " + sizeUnit.name()); + dialog.setProgressNumberFormat("%1d/%2d " + sizeUnit.shortName); dialog.show(); return dialog; } - private enum SizeUnit { - B(1), KB(1024L), MB(1024L * 1024L), GB(1024L * 1024L * 1024L), TB(1024L * 1024L * 1024L * 1024L), PB( - 1024L * 1024L * 1024L * 1024L * 1024L); - - public final long size; - - SizeUnit(long size) { - this.size = size; - } - } - @Override public void onDestroyView() { messagingController.removeListener(messagingListener); @@ -103,6 +81,38 @@ public class AttachmentDownloadDialogFragment extends DialogFragment { } + private enum SizeUnit { + BYTE("B", 1L), + KIBIBYTE("KiB", 1024L), + MEBIBYTE("MiB", 1024L * 1024L), + GIBIBYTE("GiB", 1024L * 1024L * 1024L), + TEBIBYTE("TiB", 1024L * 1024L * 1024L * 1024L), + PEBIBYTE("PiB", 1024L * 1024L * 1024L * 1024L * 1024L); + + public final String shortName; + public final long size; + + + static SizeUnit getAppropriateFor(long value) { + for (SizeUnit sizeUnit : values()) { + if (value < 1024L * 10L * sizeUnit.size) { + return sizeUnit; + } + } + return SizeUnit.BYTE; + } + + + SizeUnit(String shortName, long size) { + this.shortName = shortName; + this.size = size; + } + + int valueInSizeUnit(long value) { + return (int) (value / size); + } + } + public interface AttachmentDownloadCancelListener { void onProgressCancel(AttachmentDownloadDialogFragment fragment); } diff --git a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java index 6e468793e..56db0deba 100644 --- a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java +++ b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java @@ -544,7 +544,7 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF } case R.id.dialog_attachment_progress: { String message = getString(R.string.dialog_attachment_progress_title); - long size = currentAttachmentViewInfo.size; + long size = currentAttachmentViewInfo.size; fragment = AttachmentDownloadDialogFragment.newInstance(size, message); break; }