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

Commit 6fb6774c authored by Jeff Sharkey's avatar Jeff Sharkey Committed by Android (Google) Code Review
Browse files

Merge "Add DataUnit to clarify SI-vs-IEC units."

parents 6557fb49 9f2dc052
Loading
Loading
Loading
Loading
+11 −5
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.content.Context;
import android.media.MediaPlayer;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.DataUnit;

import com.android.server.NetworkManagementSocketTagger;

@@ -56,15 +57,20 @@ public class TrafficStats {
     */
    public final static int UNSUPPORTED = -1;

    /** @hide */
    /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
    @Deprecated
    public static final long KB_IN_BYTES = 1024;
    /** @hide */
    /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
    @Deprecated
    public static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
    /** @hide */
    /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
    @Deprecated
    public static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
    /** @hide */
    /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
    @Deprecated
    public static final long TB_IN_BYTES = GB_IN_BYTES * 1024;
    /** @hide */
    /** @hide @deprecated use {@link DataUnit} instead to clarify SI-vs-IEC */
    @Deprecated
    public static final long PB_IN_BYTES = TB_IN_BYTES * 1024;

    /**
+4 −6
Original line number Diff line number Diff line
@@ -16,9 +16,6 @@

package android.os.storage;

import static android.net.TrafficStats.GB_IN_BYTES;
import static android.net.TrafficStats.MB_IN_BYTES;

import android.annotation.BytesLong;
import android.annotation.IntDef;
import android.annotation.NonNull;
@@ -59,6 +56,7 @@ import android.system.ErrnoException;
import android.system.Os;
import android.system.OsConstants;
import android.text.TextUtils;
import android.util.DataUnit;
import android.util.Log;
import android.util.Pair;
import android.util.Slog;
@@ -1197,12 +1195,12 @@ public class StorageManager {
    }

    private static final int DEFAULT_THRESHOLD_PERCENTAGE = 5;
    private static final long DEFAULT_THRESHOLD_MAX_BYTES = 500 * MB_IN_BYTES;
    private static final long DEFAULT_THRESHOLD_MAX_BYTES = DataUnit.MEBIBYTES.toBytes(500);

    private static final int DEFAULT_CACHE_PERCENTAGE = 10;
    private static final long DEFAULT_CACHE_MAX_BYTES = 5 * GB_IN_BYTES;
    private static final long DEFAULT_CACHE_MAX_BYTES = DataUnit.GIBIBYTES.toBytes(5);

    private static final long DEFAULT_FULL_THRESHOLD_BYTES = MB_IN_BYTES;
    private static final long DEFAULT_FULL_THRESHOLD_BYTES = DataUnit.MEBIBYTES.toBytes(1);

    /**
     * Return the number of available bytes until the given path is considered
+2 −2
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package android.provider;

import static android.net.TrafficStats.KB_IN_BYTES;
import static android.system.OsConstants.SEEK_SET;

import static com.android.internal.util.Preconditions.checkArgument;
@@ -51,6 +50,7 @@ import android.os.RemoteException;
import android.os.storage.StorageVolume;
import android.system.ErrnoException;
import android.system.Os;
import android.util.DataUnit;
import android.util.Log;

import libcore.io.IoUtils;
@@ -173,7 +173,7 @@ public final class DocumentsContract {
    /**
     * Buffer is large enough to rewind past any EXIF headers.
     */
    private static final int THUMBNAIL_BUFFER_SIZE = (int) (128 * KB_IN_BYTES);
    private static final int THUMBNAIL_BUFFER_SIZE = (int) DataUnit.KIBIBYTES.toBytes(128);

    /** {@hide} */
    public static final String EXTERNAL_STORAGE_PROVIDER_AUTHORITY =
+41 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.util;

import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit;

/**
 * Constants for common byte-related units. Note that both SI and IEC units are
 * supported, and you'll need to pick the correct one for your use-case.
 * <p>
 * This design is mirrored after {@link TimeUnit} and {@link ChronoUnit}.
 *
 * @hide
 */
public enum DataUnit {
    KILOBYTES { @Override public long toBytes(long v) { return v * 1_000; } },
    MEGABYTES { @Override public long toBytes(long v) { return v * 1_000_000; } },
    GIGABYTES { @Override public long toBytes(long v) { return v * 1_000_000_000; } },
    KIBIBYTES { @Override public long toBytes(long v) { return v * 1_024; } },
    MEBIBYTES { @Override public long toBytes(long v) { return v * 1_048_576; } },
    GIBIBYTES { @Override public long toBytes(long v) { return v * 1_073_741_824; } };

    public long toBytes(long v) {
        throw new AbstractMethodError();
    }
}
+36 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.util;

import android.support.test.filters.SmallTest;

import junit.framework.TestCase;

@SmallTest
public class DataUnitTest extends TestCase {
    public void testSi() throws Exception {
        assertEquals(12_000L, DataUnit.KILOBYTES.toBytes(12));
        assertEquals(12_000_000L, DataUnit.MEGABYTES.toBytes(12));
        assertEquals(12_000_000_000L, DataUnit.GIGABYTES.toBytes(12));
    }

    public void testIec() throws Exception {
        assertEquals(12_288L, DataUnit.KIBIBYTES.toBytes(12));
        assertEquals(12_582_912L, DataUnit.MEBIBYTES.toBytes(12));
        assertEquals(12_884_901_888L, DataUnit.GIBIBYTES.toBytes(12));
    }
}
Loading