diff options
author | Danial Klimkin <dklimkin@github.com> | 2017-10-15 09:31:11 +0200 |
---|---|---|
committer | Danial Klimkin <dklimkin@github.com> | 2017-10-15 09:31:11 +0200 |
commit | 6c8f9bc80ee32efeb06798676a97dfc5bc375544 (patch) | |
tree | f1a99dff5ebbf8a1e8014f36fb5f3b23c942c332 /app/src/main/java/de/danoeh/antennapod/dialog | |
parent | 26cdef137825689d929f5a4fb55c09efa352823d (diff) | |
download | AntennaPod-6c8f9bc80ee32efeb06798676a97dfc5bc375544.zip |
Reworking data dir dialog
Diffstat (limited to 'app/src/main/java/de/danoeh/antennapod/dialog')
-rw-r--r-- | app/src/main/java/de/danoeh/antennapod/dialog/ChooseDataFolderDialog.java | 88 |
1 files changed, 88 insertions, 0 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 new file mode 100644 index 000000000..339d9a8c2 --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/dialog/ChooseDataFolderDialog.java @@ -0,0 +1,88 @@ +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 java.util.function.Consumer; + +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 { + + private ChooseDataFolderDialog() {} + + public static void showDialog(final Context context, Consumer<String> 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.accept(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, Html.FROM_HTML_MODE_LEGACY); + } + return Html.fromHtml(html); + } + +}
\ No newline at end of file |