From 31adff0dcc1b019d87be901e2bcd191cbc752722 Mon Sep 17 00:00:00 2001 From: Anderson Mesquita Date: Thu, 25 Apr 2019 11:07:23 -0400 Subject: [PATCH] Extract methods in ChooseDataFolderDialog This makes the main `showDialog()` method a bit shorter by extracting behavior into shorter methods. --- .../dialog/ChooseDataFolderDialog.java | 80 +++++++++++-------- 1 file changed, 47 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/dialog/ChooseDataFolderDialog.java b/app/src/main/java/de/danoeh/antennapod/dialog/ChooseDataFolderDialog.java index e8faa7c29..d5d2398dd 100644 --- a/app/src/main/java/de/danoeh/antennapod/dialog/ChooseDataFolderDialog.java +++ b/app/src/main/java/de/danoeh/antennapod/dialog/ChooseDataFolderDialog.java @@ -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 folders = new ArrayList<>(mediaDirs.length); - final List 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( - "%1$s [%2$s]", choice, Converter.byteToString(bytes)); - choices.add(fromHtmlVersioned(item)); - index++; - } - if (choices.isEmpty()) { + HashMap> options = getStorageOptions(context); + final List entries = options.get("entries"); + final List 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> getStorageOptions(Context context) { + File[] mediaDirs = ContextCompat.getExternalFilesDirs(context, null); + final List folders = new ArrayList<>(mediaDirs.length); + final List 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>() {{ + put("folders", folders); + put("entries", entries); + }}; + } + + private static String storageEntry(String location, String availableSpace) { + String html = String.format("%1$s [%2$s]", 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) {