Loading core/java/android/net/TrafficStats.java +11 −5 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; /** Loading core/java/android/os/storage/StorageManager.java +4 −6 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; Loading Loading @@ -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 Loading core/java/android/provider/DocumentsContract.java +2 −2 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; Loading Loading @@ -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 = Loading core/java/android/util/DataUnit.java 0 → 100644 +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(); } } core/tests/coretests/src/android/util/DataUnitTest.java 0 → 100644 +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
core/java/android/net/TrafficStats.java +11 −5 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; /** Loading
core/java/android/os/storage/StorageManager.java +4 −6 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; Loading Loading @@ -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 Loading
core/java/android/provider/DocumentsContract.java +2 −2 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; Loading Loading @@ -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 = Loading
core/java/android/util/DataUnit.java 0 → 100644 +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(); } }
core/tests/coretests/src/android/util/DataUnitTest.java 0 → 100644 +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)); } }