From b793866ed407933b17cc4fb2dc43ec2d6df3722b Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Thu, 31 Dec 2015 14:17:12 +0100 Subject: [PATCH 1/2] Show free space for current choice --- .../preferences/PreferenceController.java | 2 +- .../danoeh/antennapod/core/util/StorageUtils.java | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java index 94f5d822e..c5b5f237b 100644 --- a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java +++ b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java @@ -711,7 +711,7 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc } else { choices[i] = path; } - long bytes = StorageUtils.getFreeSpaceAvailable(); + long bytes = StorageUtils.getFreeSpaceAvailable(path); String freeSpace = String.format(context.getString(R.string.free_space_label), Converter.byteToString(bytes)); choices[i] = Html.fromHtml("" + choices[i] diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/StorageUtils.java b/core/src/main/java/de/danoeh/antennapod/core/util/StorageUtils.java index 248f2bf32..d64148715 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/StorageUtils.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/StorageUtils.java @@ -51,8 +51,19 @@ public class StorageUtils { * Get the number of free bytes that are available on the external storage. */ public static long getFreeSpaceAvailable() { - StatFs stat = new StatFs(UserPreferences.getDataFolder( - null).getAbsolutePath()); + File dataFolder = UserPreferences.getDataFolder(null); + if (dataFolder != null) { + return getFreeSpaceAvailable(dataFolder.getAbsolutePath()); + } else { + return 0; + } + } + + /** + * Get the number of free bytes that are available on the external storage. + */ + public static long getFreeSpaceAvailable(String path) { + StatFs stat = new StatFs(path); long availableBlocks; long blockSize; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { From 18830647141d7e1c252a549ff33c7294294eed56 Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Thu, 31 Dec 2015 14:17:33 +0100 Subject: [PATCH 2/2] Show error dialog when external dirs are not available --- .../preferences/PreferenceController.java | 44 ++++++++++++++----- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java index c5b5f237b..a872039f9 100644 --- a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java +++ b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java @@ -695,34 +695,56 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc private void showChooseDataFolderDialog() { Context context = ui.getActivity(); - String dataFolder = UserPreferences.getDataFolder(null).getAbsolutePath(); + File dataFolder = UserPreferences.getDataFolder(null); + if(dataFolder == null) { + new MaterialDialog.Builder(ui.getActivity()) + .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; File[] mediaDirs = ContextCompat.getExternalFilesDirs(context, null); - String[] folders = new String[mediaDirs.length]; - CharSequence[] choices = new CharSequence[mediaDirs.length]; + List folders = new ArrayList<>(mediaDirs.length); + List choices = new ArrayList<>(mediaDirs.length); for(int i=0; i < mediaDirs.length; i++) { - String path = folders[i] = mediaDirs[i].getAbsolutePath(); - if(dataFolder.equals(path)) { + if(mediaDirs[i] == null) { + continue; + } + String path = mediaDirs[i].getAbsolutePath(); + folders.add(path); + if(dataFolderPath.equals(path)) { selectedIndex = i; } int index = path.indexOf("Android"); + String choice; if(index >= 0) { - choices[i] = path.substring(0, index); + choice = path.substring(0, index); } else { - choices[i] = path; + choice = path; } long bytes = StorageUtils.getFreeSpaceAvailable(path); String freeSpace = String.format(context.getString(R.string.free_space_label), Converter.byteToString(bytes)); - choices[i] = Html.fromHtml("" + choices[i] - + " [" + freeSpace + "]" + ""); + choices.add(Html.fromHtml("" + choice + + " [" + freeSpace + "]" + "")); + } + if(choices.size() == 0) { + new MaterialDialog.Builder(ui.getActivity()) + .title(R.string.error_label) + .content(R.string.external_storage_error_msg) + .neutralText(android.R.string.ok) + .show(); + return; } MaterialDialog dialog = new MaterialDialog.Builder(ui.getActivity()) .title(R.string.choose_data_directory) .content(R.string.choose_data_directory_message) - .items(choices) + .items(choices.toArray(new CharSequence[choices.size()])) .itemsCallbackSingleChoice(selectedIndex, (dialog1, itemView, which, text) -> { - String folder = folders[which]; + String folder = folders.get(which); Log.d(TAG, "data folder: " + folder); UserPreferences.setDataFolder(folder); setDataFolderText();