Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 36bd3b86 authored by Roozbeh Pournader's avatar Roozbeh Pournader
Browse files

Switch to standard SI prefixes for kilobyte etc

Previously, powers of 1024 were used in formatting human-readable
strings for file sizes, in contrast with international and industry
standards. With this change, we switch to powers of 1000, as
standardized in SI and others.

Bug: 27672362
Change-Id: I34ae16bb68a28e60b09a7c17ac3b1d5ab9ddbeea
parent 9a5bd8bb
Loading
Loading
Loading
Loading
+20 −14
Original line number Original line Diff line number Diff line
@@ -65,10 +65,16 @@ public final class Formatter {
    /**
    /**
     * Formats a content size to be in the form of bytes, kilobytes, megabytes, etc.
     * Formats a content size to be in the form of bytes, kilobytes, megabytes, etc.
     *
     *
     * If the context has a right-to-left locale, the returned string is wrapped in bidi formatting
     * <p>As of O, the prefixes are used in their standard meanings in the SI system, so kB = 1000
     * characters to make sure it's displayed correctly if inserted inside a right-to-left string.
     * bytes, MB = 1,000,000 bytes, etc.</p>
     * (This is useful in cases where the unit strings, like "MB", are left-to-right, but the
     *
     * locale is right-to-left.)
     * <p class="note">In {@link android.os.Build.VERSION_CODES#N} and earlier, powers of 1024 are
     * used instead, with KB = 1024 bytes, MB = 1,048,576 bytes, etc.</p>
     *
     * <p>If the context has a right-to-left locale, the returned string is wrapped in bidi
     * formatting characters to make sure it's displayed correctly if inserted inside a
     * right-to-left string. (This is useful in cases where the unit strings, like "MB", are
     * left-to-right, but the locale is right-to-left.)</p>
     *
     *
     * @param context Context to use to load the localized units
     * @param context Context to use to load the localized units
     * @param sizeBytes size value to be formatted, in bytes
     * @param sizeBytes size value to be formatted, in bytes
@@ -104,28 +110,28 @@ public final class Formatter {
        long mult = 1;
        long mult = 1;
        if (result > 900) {
        if (result > 900) {
            suffix = com.android.internal.R.string.kilobyteShort;
            suffix = com.android.internal.R.string.kilobyteShort;
            mult = TrafficStats.KB_IN_BYTES;
            mult = 1000;
            result = result / 1024;
            result = result / 1000;
        }
        }
        if (result > 900) {
        if (result > 900) {
            suffix = com.android.internal.R.string.megabyteShort;
            suffix = com.android.internal.R.string.megabyteShort;
            mult = TrafficStats.MB_IN_BYTES;
            mult *= 1000;
            result = result / 1024;
            result = result / 1000;
        }
        }
        if (result > 900) {
        if (result > 900) {
            suffix = com.android.internal.R.string.gigabyteShort;
            suffix = com.android.internal.R.string.gigabyteShort;
            mult = TrafficStats.GB_IN_BYTES;
            mult *= 1000;
            result = result / 1024;
            result = result / 1000;
        }
        }
        if (result > 900) {
        if (result > 900) {
            suffix = com.android.internal.R.string.terabyteShort;
            suffix = com.android.internal.R.string.terabyteShort;
            mult = TrafficStats.TB_IN_BYTES;
            mult *= 1000;
            result = result / 1024;
            result = result / 1000;
        }
        }
        if (result > 900) {
        if (result > 900) {
            suffix = com.android.internal.R.string.petabyteShort;
            suffix = com.android.internal.R.string.petabyteShort;
            mult = TrafficStats.PB_IN_BYTES;
            mult *= 1000;
            result = result / 1024;
            result = result / 1000;
        }
        }
        // Note we calculate the rounded long by ourselves, but still let String.format()
        // Note we calculate the rounded long by ourselves, but still let String.format()
        // compute the rounded value. String.format("%f", 0.1) might not return "0.1" due to
        // compute the rounded value. String.format("%f", 0.1) might not return "0.1" due to
+6 −4
Original line number Original line Diff line number Diff line
@@ -20,8 +20,10 @@
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <!-- Suffix added to a number to signify size in bytes. -->
    <!-- Suffix added to a number to signify size in bytes. -->
    <string name="byteShort">B</string>
    <string name="byteShort">B</string>
    <!-- Suffix added to a number to signify size in kilobytes. -->
    <!-- Suffix added to a number to signify size in kilobytes (1000 bytes).
    <string name="kilobyteShort">KB</string>
        If you retain the Latin script for the localization, please use the lowercase
        'k', as it signifies 1000 bytes as opposed to 1024 bytes. -->
    <string name="kilobyteShort">kB</string>
    <!-- Suffix added to a number to signify size in megabytes. -->
    <!-- Suffix added to a number to signify size in megabytes. -->
    <string name="megabyteShort">MB</string>
    <string name="megabyteShort">MB</string>
    <!-- Suffix added to a number to signify size in gigabytes. -->
    <!-- Suffix added to a number to signify size in gigabytes. -->
@@ -30,11 +32,11 @@
    <string name="terabyteShort">TB</string>
    <string name="terabyteShort">TB</string>
    <!-- Suffix added to a number to signify size in petabytes. -->
    <!-- Suffix added to a number to signify size in petabytes. -->
    <string name="petabyteShort">PB</string>
    <string name="petabyteShort">PB</string>
    <!-- Format string used to add a suffix like "KB" or "MB" to a number
    <!-- Format string used to add a suffix like "kB" or "MB" to a number
         to display a size in kilobytes, megabytes, or other size units.
         to display a size in kilobytes, megabytes, or other size units.
         Some languages (like French) will want to add a space between
         Some languages (like French) will want to add a space between
         the placeholders. -->
         the placeholders. -->
    <string name="fileSizeSuffix"><xliff:g id="number" example="123">%1$s</xliff:g> <xliff:g id="unit" example="KB">%2$s</xliff:g></string>
    <string name="fileSizeSuffix"><xliff:g id="number" example="123">%1$s</xliff:g> <xliff:g id="unit" example="MB">%2$s</xliff:g></string>


    <!-- [CHAR_LIMIT=10] Suffix added to signify duration in days -->
    <!-- [CHAR_LIMIT=10] Suffix added to signify duration in days -->
    <string name="durationDays"><xliff:g id="days">%1$d</xliff:g> days</string>
    <string name="durationDays"><xliff:g id="days">%1$d</xliff:g> days</string>