Allow selecting external media dirs as data directory (#7400)

In addition to /sdcard/Android/data/de.danoeh.antennapod,
this now enables /sdcard/Android/media/de.danoeh.antennapod.
The folder is readable by other applications that have the
"read storage" permission. At the same time, AntennaPod
does not need the permission to write the folder.
This commit is contained in:
ByteHamster 2024-09-14 11:44:59 +02:00 committed by GitHub
parent f35e92c112
commit 2a1b537d13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 11 deletions

View File

@ -47,14 +47,14 @@ public class DataFolderAdapter extends RecyclerView.Adapter<DataFolderAdapter.Vi
String freeSpace = Formatter.formatShortFileSize(context, storagePath.getAvailableSpace()); String freeSpace = Formatter.formatShortFileSize(context, storagePath.getAvailableSpace());
String totalSpace = Formatter.formatShortFileSize(context, storagePath.getTotalSpace()); String totalSpace = Formatter.formatShortFileSize(context, storagePath.getTotalSpace());
holder.path.setText(storagePath.getShortPath()); holder.path.setText(storagePath.getPath());
holder.size.setText(String.format(freeSpaceString, freeSpace, totalSpace)); holder.size.setText(String.format(freeSpaceString, freeSpace, totalSpace));
holder.progressBar.setProgress(storagePath.getUsagePercentage()); holder.progressBar.setProgress(storagePath.getUsagePercentage());
View.OnClickListener selectListener = v -> selectionHandler.accept(storagePath.getFullPath()); View.OnClickListener selectListener = v -> selectionHandler.accept(storagePath.getPath());
holder.root.setOnClickListener(selectListener); holder.root.setOnClickListener(selectListener);
holder.radioButton.setOnClickListener(selectListener); holder.radioButton.setOnClickListener(selectListener);
if (storagePath.getFullPath().equals(currentPath)) { if (storagePath.getPath().equals(currentPath)) {
holder.radioButton.toggle(); holder.radioButton.toggle();
} }
} }
@ -73,8 +73,10 @@ public class DataFolderAdapter extends RecyclerView.Adapter<DataFolderAdapter.Vi
} }
private List<StoragePath> getStorageEntries(Context context) { private List<StoragePath> getStorageEntries(Context context) {
File[] mediaDirs = context.getExternalFilesDirs(null); final List<File> mediaDirs = new ArrayList<>();
final List<StoragePath> entries = new ArrayList<>(mediaDirs.length); mediaDirs.addAll(List.of(context.getExternalFilesDirs(null)));
mediaDirs.addAll(List.of(context.getExternalMediaDirs()));
final List<StoragePath> entries = new ArrayList<>(mediaDirs.size());
for (File dir : mediaDirs) { for (File dir : mediaDirs) {
if (!isWritable(dir)) { if (!isWritable(dir)) {
continue; continue;
@ -115,12 +117,7 @@ public class DataFolderAdapter extends RecyclerView.Adapter<DataFolderAdapter.Vi
this.path = path; this.path = path;
} }
String getShortPath() { String getPath() {
int prefixIndex = path.indexOf("Android");
return (prefixIndex > 0) ? path.substring(0, prefixIndex) : path;
}
String getFullPath() {
return this.path; return this.path;
} }