diff options
author | Anderson Mesquita <andersonvom@gmail.com> | 2019-04-25 18:36:12 -0400 |
---|---|---|
committer | Anderson Mesquita <andersonvom@gmail.com> | 2019-05-19 13:25:27 -0400 |
commit | 683f7e46a27740634d6cde0e5f648e240b95e401 (patch) | |
tree | 99c2b4dbb87edbb0e37bc073a84e13ed4d8a110c /app/src/main/java/de/danoeh/antennapod/dialog | |
parent | 31adff0dcc1b019d87be901e2bcd191cbc752722 (diff) | |
download | AntennaPod-683f7e46a27740634d6cde0e5f648e240b95e401.zip |
Extract data folder dialog layout
This is in preparation to add a progress bar displaying the amount of
used/free space in the dialog (#3049). Since we'll need a custom view to
do it, this extracts the layout beforehand.
Diffstat (limited to 'app/src/main/java/de/danoeh/antennapod/dialog')
-rw-r--r-- | app/src/main/java/de/danoeh/antennapod/dialog/ChooseDataFolderDialog.java | 74 |
1 files changed, 5 insertions, 69 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 d5d2398dd..c185a5557 100644 --- a/app/src/main/java/de/danoeh/antennapod/dialog/ChooseDataFolderDialog.java +++ b/app/src/main/java/de/danoeh/antennapod/dialog/ChooseDataFolderDialog.java @@ -1,21 +1,11 @@ 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.HashMap; -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; +import de.danoeh.antennapod.adapter.DataFolderAdapter; public class ChooseDataFolderDialog { @@ -32,12 +22,9 @@ public class ChooseDataFolderDialog { private ChooseDataFolderDialog() {} public static void showDialog(final Context context, RunnableWithString handlerFunc) { - File dataFolder = UserPreferences.getDataFolder(null); - HashMap<String, List<String>> options = getStorageOptions(context); - final List<String> entries = options.get("entries"); - final List<String> folders = options.get("folders"); + DataFolderAdapter adapter = new DataFolderAdapter(context, handlerFunc); - if (dataFolder == null || entries.isEmpty()) { + if (adapter.getItemCount() == 0) { new MaterialDialog.Builder(context) .title(R.string.error_label) .content(R.string.external_storage_error_msg) @@ -46,66 +33,15 @@ public class ChooseDataFolderDialog { 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(entries) - .itemsCallbackSingleChoice(selectedIndex, (dialog1, itemView, which, text) -> { - String folder = folders.get(which); - handlerFunc.run(folder); - return true; - }) + .adapter(adapter, null) .negativeText(R.string.cancel_label) .cancelable(true) .build(); + adapter.setDialog(dialog); 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) { - return Html.fromHtml(html); - } - return Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY); - } - }
\ No newline at end of file |