From 24403ff054f5c3086d297cafb8e928f3ac7c2f5b Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Tue, 4 Apr 2017 15:09:58 -0600 Subject: [PATCH] Just round up the data partition size. Instead of trying to be clever by poking at underlying flash part sizes, rely on the fact that device storage printed on retail packaging is a power-of-two value. For a typical device with a 23GiB data partition, this will return a value of "32GB" which matches the retail packaging. Test: builds, boots Bug: 34827187 Change-Id: Ib4cf7f637dffc9238252e1fedcd86dc8b5cf656d --- .../android/os/storage/StorageManager.java | 46 ++----------------- 1 file changed, 3 insertions(+), 43 deletions(-) diff --git a/core/java/android/os/storage/StorageManager.java b/core/java/android/os/storage/StorageManager.java index e5d73e04bda6b..b5af766fd1598 100644 --- a/core/java/android/os/storage/StorageManager.java +++ b/core/java/android/os/storage/StorageManager.java @@ -59,19 +59,14 @@ import com.android.internal.annotations.VisibleForTesting; import com.android.internal.logging.MetricsLogger; import com.android.internal.os.AppFuseMount; import com.android.internal.os.FuseAppLoop; -import com.android.internal.os.FuseAppLoop.UnmountedException; import com.android.internal.os.FuseUnavailableMountException; import com.android.internal.os.RoSystemProperties; import com.android.internal.os.SomeArgs; import com.android.internal.util.Preconditions; -import java.io.BufferedReader; import java.io.File; import java.io.FileDescriptor; -import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; -import java.io.InputStreamReader; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.ref.WeakReference; @@ -84,7 +79,6 @@ import java.util.List; import java.util.Objects; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; -import libcore.io.IoUtils; /** * StorageManager is the interface to the systems storage service. The storage @@ -186,15 +180,6 @@ public class StorageManager { private static volatile IStorageManager sStorageManager = null; - // TODO: the location of the primary storage block varies from device to device, so we need to - // try the most likely candidates - a long-term solution would be a device-specific vold - // function that returns the calculated size. - private static final String[] INTERNAL_STORAGE_SIZE_PATHS = { - "/sys/block/mmcblk0/size", - "/sys/block/sda/size" - }; - private static final int INTERNAL_STORAGE_SECTOR_SIZE = 512; - private final Context mContext; private final ContentResolver mResolver; @@ -1011,38 +996,13 @@ public class StorageManager { /** {@hide} */ public static Pair getPrimaryStoragePathAndSize() { - for (String path : INTERNAL_STORAGE_SIZE_PATHS) { - final long numberBlocks = readLong(path); - if (numberBlocks > 0) { - return new Pair<>(path, - FileUtils.roundStorageSize(numberBlocks * INTERNAL_STORAGE_SECTOR_SIZE)); - } - } - return null; + return Pair.create(null, + FileUtils.roundStorageSize(Environment.getDataDirectory().getTotalSpace())); } - /** {@hide} */ public long getPrimaryStorageSize() { - final Pair pair = getPrimaryStoragePathAndSize(); - return pair == null ? 0 : pair.second.longValue(); - } - - private static long readLong(String path) { - try (final FileInputStream fis = new FileInputStream(path); - final BufferedReader reader = new BufferedReader(new InputStreamReader(fis));) { - return Long.parseLong(reader.readLine()); - } catch (FileNotFoundException e) { - // This is expected since we are trying to parse multiple paths. - Slog.i(TAG, "readLong(): Path doesn't exist: " + path + ": " + e); - return 0; - } catch (NumberFormatException e) { - Slog.e(TAG, "readLong(): Could not parse " + path + ": " + e); - return 0; - } catch (Exception e) { - Slog.e(TAG, "readLong(): Unknown exception while opening " + path + ": " + e); - return 0; - } + return FileUtils.roundStorageSize(Environment.getDataDirectory().getTotalSpace()); } /** @removed */