blob: e8faa7c298f211526c025d219294c7247ee12582 (
plain)
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
|
package de.danoeh.antennapod.dialog;
import android.content.Context;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import com.afollestad.materialdialogs.MaterialDialog;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.StorageUtils;
public class ChooseDataFolderDialog {
public static abstract class RunnableWithString implements Runnable {
public RunnableWithString() {
super();
}
public abstract void run(final String arg);
@Override public void run() {
throw new IllegalArgumentException("Expect one String parameter.");
}
}
private ChooseDataFolderDialog() {}
public static void showDialog(final Context context, RunnableWithString handlerFunc) {
File dataFolder = UserPreferences.getDataFolder(null);
if (dataFolder == null) {
new MaterialDialog.Builder(context)
.title(R.string.error_label)
.content(R.string.external_storage_error_msg)
.neutralText(android.R.string.ok)
.show();
return;
}
String dataFolderPath = dataFolder.getAbsolutePath();
int selectedIndex = -1;
int index = 0;
File[] mediaDirs = ContextCompat.getExternalFilesDirs(context, null);
final List<String> folders = new ArrayList<>(mediaDirs.length);
final List<CharSequence> choices = new ArrayList<>(mediaDirs.length);
for (File dir : mediaDirs) {
if(dir == null || !dir.exists() || !dir.canRead() || !dir.canWrite()) {
continue;
}
String path = dir.getAbsolutePath();
folders.add(path);
if(dataFolderPath.equals(path)) {
selectedIndex = index;
}
int prefixIndex = path.indexOf("Android");
String choice = (prefixIndex > 0) ? path.substring(0, prefixIndex) : path;
long bytes = StorageUtils.getFreeSpaceAvailable(path);
String item = String.format(
"<small>%1$s [%2$s]</small>", choice, Converter.byteToString(bytes));
choices.add(fromHtmlVersioned(item));
index++;
}
if (choices.isEmpty()) {
new MaterialDialog.Builder(context)
.title(R.string.error_label)
.content(R.string.external_storage_error_msg)
.neutralText(android.R.string.ok)
.show();
return;
}
MaterialDialog dialog = new MaterialDialog.Builder(context)
.title(R.string.choose_data_directory)
.content(R.string.choose_data_directory_message)
.items(choices)
.itemsCallbackSingleChoice(selectedIndex, (dialog1, itemView, which, text) -> {
String folder = folders.get(which);
handlerFunc.run(folder);
return true;
})
.negativeText(R.string.cancel_label)
.cancelable(true)
.build();
dialog.show();
}
@SuppressWarnings("deprecation")
private static CharSequence fromHtmlVersioned(final String html) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
return Html.fromHtml(html);
}
return Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
}
}
|