mirror of
https://github.com/AntennaPod/AntennaPod.git
synced 2024-12-25 08:21:59 +01:00
Extract methods in ChooseDataFolderDialog
This makes the main `showDialog()` method a bit shorter by extracting behavior into shorter methods.
This commit is contained in:
parent
135df61692
commit
31adff0dcc
@ -9,6 +9,7 @@ import com.afollestad.materialdialogs.MaterialDialog;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import de.danoeh.antennapod.R;
|
||||
@ -32,38 +33,11 @@ public class 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()) {
|
||||
HashMap<String, List<String>> options = getStorageOptions(context);
|
||||
final List<String> entries = options.get("entries");
|
||||
final List<String> folders = options.get("folders");
|
||||
|
||||
if (dataFolder == null || entries.isEmpty()) {
|
||||
new MaterialDialog.Builder(context)
|
||||
.title(R.string.error_label)
|
||||
.content(R.string.external_storage_error_msg)
|
||||
@ -71,10 +45,12 @@ public class ChooseDataFolderDialog {
|
||||
.show();
|
||||
return;
|
||||
}
|
||||
|
||||
int selectedIndex = folders.indexOf(dataFolder.getAbsolutePath());
|
||||
MaterialDialog dialog = new MaterialDialog.Builder(context)
|
||||
.title(R.string.choose_data_directory)
|
||||
.content(R.string.choose_data_directory_message)
|
||||
.items(choices)
|
||||
.items(entries)
|
||||
.itemsCallbackSingleChoice(selectedIndex, (dialog1, itemView, which, text) -> {
|
||||
String folder = folders.get(which);
|
||||
handlerFunc.run(folder);
|
||||
@ -86,6 +62,44 @@ public class ChooseDataFolderDialog {
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private static HashMap<String, List<String>> getStorageOptions(Context context) {
|
||||
File[] mediaDirs = ContextCompat.getExternalFilesDirs(context, null);
|
||||
final List<String> folders = new ArrayList<>(mediaDirs.length);
|
||||
final List<String> entries = new ArrayList<>(mediaDirs.length);
|
||||
for (File dir : mediaDirs) {
|
||||
if (isNotWritable(dir)) continue;
|
||||
|
||||
String path = dir.getAbsolutePath();
|
||||
String location = getStorageLocation(path);
|
||||
String availableSpace = getAvailableSpace(path);
|
||||
folders.add(path);
|
||||
entries.add(storageEntry(location, availableSpace));
|
||||
}
|
||||
return new HashMap<String, List<String>>() {{
|
||||
put("folders", folders);
|
||||
put("entries", entries);
|
||||
}};
|
||||
}
|
||||
|
||||
private static String storageEntry(String location, String availableSpace) {
|
||||
String html = String.format("<small>%1$s [%2$s]</small>", location, availableSpace);
|
||||
return fromHtmlVersioned(html).toString();
|
||||
}
|
||||
|
||||
private static String getAvailableSpace(String path) {
|
||||
long spaceAvailable = StorageUtils.getFreeSpaceAvailable(path);
|
||||
return Converter.byteToString(spaceAvailable);
|
||||
}
|
||||
|
||||
private static String getStorageLocation(String path) {
|
||||
int prefixIndex = path.indexOf("Android");
|
||||
return (prefixIndex > 0) ? path.substring(0, prefixIndex) : path;
|
||||
}
|
||||
|
||||
private static boolean isNotWritable(File dir) {
|
||||
return dir == null || !dir.exists() || !dir.canRead() || !dir.canWrite();
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private static CharSequence fromHtmlVersioned(final String html) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
||||
|
Loading…
Reference in New Issue
Block a user