diff --git a/app/src/main/java/dev/aldi/sayuti/editor/manage/ManageLocalLibraryActivity.java b/app/src/main/java/dev/aldi/sayuti/editor/manage/ManageLocalLibraryActivity.java index 8f055dde09..0de6b6f1c3 100644 --- a/app/src/main/java/dev/aldi/sayuti/editor/manage/ManageLocalLibraryActivity.java +++ b/app/src/main/java/dev/aldi/sayuti/editor/manage/ManageLocalLibraryActivity.java @@ -210,7 +210,7 @@ public void onBindViewHolder(ViewHolder holder, final int position) { var binding = holder.binding; final File libraryFile = libraryFiles.get(position); - final String librarySize = FileUtil.formatFileSize(libraryFile.length()); + final String librarySize = FileUtil.formatFileSize(FileUtil.getFileSize(libraryFile)); binding.libraryName.setText(libraryFile.getName()); binding.librarySize.setText(librarySize); binding.libraryName.setSelected(true); @@ -316,7 +316,7 @@ public void onBindViewHolder(ViewHolder holder, final int position) { var binding = holder.binding; final File libraryFile = filteredLibraryFiles.get(position); - final String librarySize = FileUtil.formatFileSize(libraryFile.length()); + final String librarySize = FileUtil.formatFileSize(FileUtil.getFileSize(libraryFile)); binding.libraryName.setText(libraryFile.getName()); binding.librarySize.setText(librarySize); binding.libraryName.setSelected(true); diff --git a/app/src/main/java/pro/sketchware/utility/FileUtil.java b/app/src/main/java/pro/sketchware/utility/FileUtil.java index ec24b2b4ac..a754665cff 100644 --- a/app/src/main/java/pro/sketchware/utility/FileUtil.java +++ b/app/src/main/java/pro/sketchware/utility/FileUtil.java @@ -54,34 +54,56 @@ @SuppressWarnings("unused") public class FileUtil { + public static long getFileSize(File file) { + if (file == null || !file.exists()) { + return 0; + } + + if (!file.isDirectory()) { + return file.length(); + } + + List dirs = new LinkedList<>(); + dirs.add(file); + + long result = 0; + while (!dirs.isEmpty()) { + File dir = dirs.remove(0); + if (!dir.exists()) + continue; + File[] listFiles = dir.listFiles(); + if (listFiles == null || listFiles.length == 0) + continue; + for (File child : listFiles) { + if (child.isDirectory()) { + dirs.add(child); + } else { + result += child.length(); + } + } + } + + return result; + } + public static String formatFileSize(long size) { return formatFileSize(size, false); } public static String formatFileSize(long size, boolean removeZero) { - if (size < 1024) { - return String.format("%d B", size); - } else if (size < 1024 * 1024) { - float value = size / 1024.0f; - if (removeZero && (value - (int) value) * 10 == 0) { - return String.format("%d KB", (int) value); - } else { - return String.format("%.1f KB", value); - } - } else if (size < 1024 * 1024 * 1024) { - float value = size / 1024.0f / 1024.0f; - if (removeZero && (value - (int) value) * 10 == 0) { - return String.format("%d MB", (int) value); - } else { - return String.format("%.1f MB", value); - } + String[] units = {"B", "KiB", "MiB", "GiB"}; + float value = size; + int unitIndex = 0; + + while (value >= 1024 && unitIndex < units.length - 1) { + value /= 1024; + unitIndex++; + } + + if (removeZero && (value - (int) value) * 10 == 0) { + return String.format("%d %s", (int) value, units[unitIndex]); } else { - float value = size / 1024.0f / 1024.0f / 1024.0f; - if (removeZero && (value - (int) value) * 10 == 0) { - return String.format("%d GB", (int) value); - } else { - return String.format("%.1f GB", value); - } + return String.format("%.1f %s", value, units[unitIndex]); } } diff --git a/app/src/main/res/layout/manage_locallibraries.xml b/app/src/main/res/layout/manage_locallibraries.xml index 3aee17d684..e8e21b78c7 100644 --- a/app/src/main/res/layout/manage_locallibraries.xml +++ b/app/src/main/res/layout/manage_locallibraries.xml @@ -68,6 +68,7 @@ @@ -76,6 +77,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4dp" + android:gravity="center" android:text="@string/local_library_manager_no_libraries_body" android:textColor="?attr/colorOnSurfaceVariant" android:textSize="14sp" /> @@ -83,7 +85,7 @@