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

Commit 09734df8 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Grumble, nobody likes kibibytes.

All the cool kids are using storage in increments of 1000 instead
of 1024, so find a balance somewhere between the two.  We still round
to nice values like 32GB, 64GB, etc, but we represent them using
kilobytes under the hood.

Test: runtest -x frameworks/base/core/tests/coretests/src/android/os/FileUtilsTest.java
Bug: 28327846
Change-Id: I573aea43790816291e2b5c784b344b51b4444c06
parent c7eefdbd
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -777,10 +777,15 @@ public class FileUtils {
     * "29.5GB" in UI.
     */
    public static long roundStorageSize(long size) {
        long res = 1;
        while (res < size) {
            res <<= 1;
        long val = 1;
        long pow = 1;
        while ((val * pow) < size) {
            val <<= 1;
            if (val > 512) {
                val = 1;
                pow *= 1000;
            }
        return res;
        }
        return val * pow;
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -1006,7 +1006,8 @@ public class StorageManager {
        for (String path : INTERNAL_STORAGE_SIZE_PATHS) {
            final long numberBlocks = readLong(path);
            if (numberBlocks > 0) {
                return new Pair<>(path, Long.valueOf(numberBlocks * INTERNAL_STORAGE_SECTOR_SIZE));
                return new Pair<>(path,
                        FileUtils.roundStorageSize(numberBlocks * INTERNAL_STORAGE_SECTOR_SIZE));
            }
        }
        return null;
+28 −21
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.os;

import static android.os.FileUtils.roundStorageSize;
import static android.text.format.DateUtils.DAY_IN_MILLIS;
import static android.text.format.DateUtils.HOUR_IN_MILLIS;
import static android.text.format.DateUtils.WEEK_IN_MILLIS;
@@ -25,10 +26,10 @@ import android.provider.DocumentsContract.Document;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.MediumTest;

import com.google.android.collect.Sets;

import libcore.io.IoUtils;

import com.google.android.collect.Sets;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
@@ -312,25 +313,31 @@ public class FileUtilsTest extends AndroidTestCase {
    }

    public void testRoundStorageSize() throws Exception {
        final long M128 = 134217728L;
        final long M256 = M128 * 2;
        final long M512 = M256 * 2;
        final long M1024 = M512 * 2;
        final long G16 = M1024 * 16;
        final long G32 = M1024 * 32;
        final long G64 = M1024 * 64;

        assertEquals(M128, FileUtils.roundStorageSize(M128));
        assertEquals(M256, FileUtils.roundStorageSize(M128 + 1));
        assertEquals(M256, FileUtils.roundStorageSize(M256 - 1));
        assertEquals(M256, FileUtils.roundStorageSize(M256));
        assertEquals(M512, FileUtils.roundStorageSize(M256 + 1));

        assertEquals(G16, FileUtils.roundStorageSize(G16));
        assertEquals(G32, FileUtils.roundStorageSize(G16 + 1));
        assertEquals(G32, FileUtils.roundStorageSize(G32 - 1));
        assertEquals(G32, FileUtils.roundStorageSize(G32));
        assertEquals(G64, FileUtils.roundStorageSize(G32 + 1));
        final long M128 = 128000000L;
        final long M256 = 256000000L;
        final long M512 = 512000000L;
        final long G1 = 1000000000L;
        final long G2 = 2000000000L;
        final long G16 = 16000000000L;
        final long G32 = 32000000000L;
        final long G64 = 64000000000L;

        assertEquals(M128, roundStorageSize(M128));
        assertEquals(M256, roundStorageSize(M128 + 1));
        assertEquals(M256, roundStorageSize(M256 - 1));
        assertEquals(M256, roundStorageSize(M256));
        assertEquals(M512, roundStorageSize(M256 + 1));
        assertEquals(M512, roundStorageSize(M512 - 1));
        assertEquals(M512, roundStorageSize(M512));
        assertEquals(G1, roundStorageSize(M512 + 1));
        assertEquals(G1, roundStorageSize(G1));
        assertEquals(G2, roundStorageSize(G1 + 1));

        assertEquals(G16, roundStorageSize(G16));
        assertEquals(G32, roundStorageSize(G16 + 1));
        assertEquals(G32, roundStorageSize(G32 - 1));
        assertEquals(G32, roundStorageSize(G32));
        assertEquals(G64, roundStorageSize(G32 + 1));
    }

    private static void assertNameEquals(String expected, File actual) {