Consistently format byte sizes.

This commit is contained in:
Greyson Parrelli
2025-03-12 15:24:15 -04:00
parent d2006853c7
commit 22d908385b
16 changed files with 31 additions and 124 deletions

View File

@@ -1,58 +0,0 @@
package org.thoughtcrime.securesms.util;
import androidx.annotation.NonNull;
import java.text.DecimalFormat;
/**
* Used for the pretty formatting of bytes for user display.
*/
public enum MemoryUnitFormat {
BYTES(" B"),
KILO_BYTES(" kB"),
MEGA_BYTES(" MB"),
GIGA_BYTES(" GB"),
TERA_BYTES(" TB");
private static final DecimalFormat ONE_DP = new DecimalFormat("#,##0.0");
private static final DecimalFormat OPTIONAL_ONE_DP = new DecimalFormat("#,##0.#");
private final String unitString;
MemoryUnitFormat(String unitString) {
this.unitString = unitString;
}
public double fromBytes(long bytes) {
return bytes / Math.pow(1000, ordinal());
}
/**
* Creates a string suitable to present to the user from the specified {@param bytes}.
* It will pick a suitable unit of measure to display depending on the size of the bytes.
* It will not select a unit of measure lower than the specified {@param minimumUnit}.
*
* @param forceOneDp If true, will include 1 decimal place, even if 0. If false, will only show 1 dp when it's non-zero.
*/
public static String formatBytes(long bytes, @NonNull MemoryUnitFormat minimumUnit, boolean forceOneDp) {
if (bytes <= 0) bytes = 0;
int ordinal = bytes != 0 ? (int) (Math.log10(bytes) / 3) : 0;
if (ordinal >= MemoryUnitFormat.values().length) {
ordinal = MemoryUnitFormat.values().length - 1;
}
MemoryUnitFormat unit = MemoryUnitFormat.values()[ordinal];
if (unit.ordinal() < minimumUnit.ordinal()) {
unit = minimumUnit;
}
return (forceOneDp ? ONE_DP : OPTIONAL_ONE_DP).format(unit.fromBytes(bytes)) + unit.unitString;
}
public static String formatBytes(long bytes) {
return formatBytes(bytes, BYTES, false);
}
}

View File

@@ -187,18 +187,10 @@ public class Util {
return spanned;
}
public static @NonNull String toIsoString(byte[] bytes) {
return new String(bytes, StandardCharsets.ISO_8859_1);
}
public static byte[] toIsoBytes(String isoString) {
return isoString.getBytes(StandardCharsets.ISO_8859_1);
}
public static byte[] toUtf8Bytes(String utf8String) {
return utf8String.getBytes(StandardCharsets.UTF_8);
}
public static void wait(Object lock, long timeout) {
try {
lock.wait(timeout);
@@ -371,10 +363,6 @@ public class Util {
}
}
public static <T> T getRandomElement(T[] elements) {
return elements[new SecureRandom().nextInt(elements.length)];
}
public static <T> T getRandomElement(List<T> elements) {
return elements.get(new SecureRandom().nextInt(elements.size()));
}
@@ -448,38 +436,10 @@ public class Util {
return (int)value;
}
public static boolean isEquals(@Nullable Long first, long second) {
return first != null && first == second;
}
public static String getPrettyFileSize(long sizeBytes) {
return MemoryUnitFormat.formatBytes(sizeBytes);
}
public static void copyToClipboard(@NonNull Context context, @NonNull CharSequence text) {
ServiceUtil.getClipboardManager(context).setPrimaryClip(ClipData.newPlainText(COPY_LABEL, text));
}
@SafeVarargs
public static <T> List<T> concatenatedList(Collection <T>... items) {
final List<T> concat = new ArrayList<>(Stream.of(items).reduce(0, (sum, list) -> sum + list.size()));
for (Collection<T> list : items) {
concat.addAll(list);
}
return concat;
}
public static boolean isLong(String value) {
try {
Long.parseLong(value);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static int parseInt(String integer, int defaultValue) {
try {
return Integer.parseInt(integer);