-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
AttachmentDownloadDialogFragment.java
118 lines (89 loc) · 3.56 KB
/
AttachmentDownloadDialogFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.fsck.k9.fragment;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.fragment.app.DialogFragment;
import com.fsck.k9.controller.MessagingController;
import com.fsck.k9.controller.MessagingListener;
import com.fsck.k9.controller.SimpleMessagingListener;
public class AttachmentDownloadDialogFragment extends DialogFragment {
private static final String ARG_SIZE = "size";
private static final String ARG_MESSAGE = "message";
private MessagingListener messagingListener;
private MessagingController messagingController;
public static AttachmentDownloadDialogFragment newInstance(long size, String message) {
AttachmentDownloadDialogFragment fragment = new AttachmentDownloadDialogFragment();
Bundle args = new Bundle();
args.putLong(ARG_SIZE, size);
args.putString(ARG_MESSAGE, message);
fragment.setArguments(args);
return fragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle args = getArguments();
long size = args.getLong(ARG_SIZE);
String message = args.getString(ARG_MESSAGE);
final SizeUnit sizeUnit = SizeUnit.getAppropriateFor(size);
ProgressDialog dialog = new ProgressDialog(getActivity());
dialog.setMessage(message);
dialog.setMax(sizeUnit.valueInSizeUnit(size));
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(0);
dialog.setProgressNumberFormat("%1d/%2d " + sizeUnit.shortName);
dialog.show();
messagingListener = new SimpleMessagingListener() {
@Override
public void updateProgress(int progress) {
dialog.setProgress(sizeUnit.valueInSizeUnit(progress));
}
};
messagingController = MessagingController.getInstance(getActivity());
messagingController.addListener(messagingListener);
return dialog;
}
@Override
public void onDestroyView() {
messagingController.removeListener(messagingListener);
super.onDestroyView();
}
@Override
public void onCancel(DialogInterface dialog) {
Activity activity = getActivity();
if (activity != null && activity instanceof AttachmentDownloadCancelListener) {
AttachmentDownloadCancelListener listener = (AttachmentDownloadCancelListener) activity;
listener.onProgressCancel(this);
}
super.onCancel(dialog);
}
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);
}
}