import android.media.MediaPlayer;
import android.os.RemoteException;
import android.os.ServiceManager;
+import android.util.DataUnit;
import com.android.server.NetworkManagementSocketTagger;
*/
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;
/**
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;
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;
}
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
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;
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;
/**
* 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 =
--- /dev/null
+/*
+ * 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();
+ }
+}
--- /dev/null
+/*
+ * 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));
+ }
+}
import android.text.format.DateUtils;
import android.util.ArrayMap;
import android.util.AtomicFile;
+import android.util.DataUnit;
import android.util.Log;
import android.util.Pair;
import android.util.Slog;
pw.print(") total size: ");
pw.print(pair.second);
pw.print(" (");
- pw.print((float) pair.second / TrafficStats.GB_IN_BYTES);
- pw.println(" GB)");
+ pw.print(DataUnit.MEBIBYTES.toBytes(pair.second));
+ pw.println(" MiB)");
}
pw.println("Force adoptable: " + mForceAdoptable);
pw.println();
import android.os.storage.VolumeInfo;
import android.text.format.DateUtils;
import android.util.ArrayMap;
+import android.util.DataUnit;
import android.util.Slog;
import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
private static final int MSG_CHECK = 1;
- private static final long DEFAULT_LOG_DELTA_BYTES = 64 * TrafficStats.MB_IN_BYTES;
+ private static final long DEFAULT_LOG_DELTA_BYTES = DataUnit.MEBIBYTES.toBytes(64);
private static final long DEFAULT_CHECK_INTERVAL = DateUtils.MINUTE_IN_MILLIS;
// com.android.internal.R.string.low_internal_storage_view_text_no_boot
// hard codes 250MB in the message as the storage space required for the
// boot image.
- private static final long BOOT_IMAGE_STORAGE_REQUIREMENT = 250 * TrafficStats.MB_IN_BYTES;
+ private static final long BOOT_IMAGE_STORAGE_REQUIREMENT = DataUnit.MEBIBYTES.toBytes(250);
private NotificationManager mNotifManager;
import android.provider.Settings;
import android.text.format.DateUtils;
import android.util.ArrayMap;
+import android.util.DataUnit;
import android.util.Slog;
import android.util.SparseLongArray;
private static final String PROP_VERIFY_STORAGE = "fw.verify_storage";
private static final long DELAY_IN_MILLIS = 30 * DateUtils.SECOND_IN_MILLIS;
- private static final long DEFAULT_QUOTA = 64 * TrafficStats.MB_IN_BYTES;
+ private static final long DEFAULT_QUOTA = DataUnit.MEBIBYTES.toBytes(64);
public static class Lifecycle extends SystemService {
private StorageStatsService mService;