Update storage usage to be consistent in settings

Storage usage is calculated differently accross multiple settings pages.
Update the logic to be consistent throughout.

Bug: 70475659
Test: Manual
Change-Id: Id01f39ead76fac505daa20998b0b326b1947a6a4
This commit is contained in:
Akshay Joshi
2018-01-16 14:23:19 -08:00
parent c261912301
commit 787bca627f
2 changed files with 31 additions and 11 deletions

View File

@@ -160,8 +160,11 @@ public class StorageSettings extends SettingsPreferenceFragment implements Index
mInternalCategory.addPreference(mInternalSummary); mInternalCategory.addPreference(mInternalSummary);
int privateCount = 0; int privateCount = 0;
long privateUsedBytes = 0;
long privateTotalBytes = 0; final StorageManagerVolumeProvider smvp = new StorageManagerVolumeProvider(mStorageManager);
final PrivateStorageInfo info = PrivateStorageInfo.getPrivateStorageInfo(smvp);
final long privateTotalBytes = info.totalBytes;
final long privateUsedBytes = info.totalBytes - info.freeBytes;
final List<VolumeInfo> volumes = mStorageManager.getVolumes(); final List<VolumeInfo> volumes = mStorageManager.getVolumes();
Collections.sort(volumes, VolumeInfo.getDescriptionComparator()); Collections.sort(volumes, VolumeInfo.getDescriptionComparator());
@@ -173,11 +176,6 @@ public class StorageSettings extends SettingsPreferenceFragment implements Index
final int color = COLOR_PRIVATE[privateCount++ % COLOR_PRIVATE.length]; final int color = COLOR_PRIVATE[privateCount++ % COLOR_PRIVATE.length];
mInternalCategory.addPreference( mInternalCategory.addPreference(
new StorageVolumePreference(context, vol, color, volumeTotalBytes)); new StorageVolumePreference(context, vol, color, volumeTotalBytes));
if (vol.isMountedReadable()) {
final File path = vol.getPath();
privateUsedBytes += (volumeTotalBytes - path.getFreeSpace());
privateTotalBytes += volumeTotalBytes;
}
} else if (vol.getType() == VolumeInfo.TYPE_PUBLIC) { } else if (vol.getType() == VolumeInfo.TYPE_PUBLIC) {
mExternalCategory.addPreference( mExternalCategory.addPreference(
new StorageVolumePreference(context, vol, COLOR_PUBLIC, 0)); new StorageVolumePreference(context, vol, COLOR_PUBLIC, 0));

View File

@@ -16,6 +16,7 @@
package com.android.settings.deviceinfo; package com.android.settings.deviceinfo;
import android.app.usage.StorageStatsManager;
import android.content.Context; import android.content.Context;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
import android.graphics.Color; import android.graphics.Color;
@@ -25,6 +26,7 @@ import android.os.storage.VolumeInfo;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceViewHolder; import android.support.v7.preference.PreferenceViewHolder;
import android.text.format.Formatter; import android.text.format.Formatter;
import android.util.Log;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.widget.ImageView; import android.widget.ImageView;
@@ -34,6 +36,7 @@ import com.android.settings.R;
import com.android.settings.deviceinfo.StorageSettings.UnmountTask; import com.android.settings.deviceinfo.StorageSettings.UnmountTask;
import com.android.settingslib.Utils; import com.android.settingslib.Utils;
import java.io.IOException;
import java.io.File; import java.io.File;
/** /**
@@ -41,6 +44,8 @@ import java.io.File;
* quick actions like unmounting. * quick actions like unmounting.
*/ */
public class StorageVolumePreference extends Preference { public class StorageVolumePreference extends Preference {
private static final String TAG = StorageVolumePreference.class.getSimpleName();
private final StorageManager mStorageManager; private final StorageManager mStorageManager;
private final VolumeInfo mVolume; private final VolumeInfo mVolume;
@@ -70,11 +75,28 @@ public class StorageVolumePreference extends Preference {
if (volume.isMountedReadable()) { if (volume.isMountedReadable()) {
// TODO: move statfs() to background thread // TODO: move statfs() to background thread
final File path = volume.getPath(); final File path = volume.getPath();
if (totalBytes <= 0) {
totalBytes = path.getTotalSpace(); long freeBytes = 0;
long usedBytes = 0;
if (volume.getType() == VolumeInfo.TYPE_PRIVATE) {
final StorageStatsManager stats =
context.getSystemService(StorageStatsManager.class);
try {
totalBytes = stats.getTotalBytes(volume.getFsUuid());
freeBytes = stats.getFreeBytes(volume.getFsUuid());
usedBytes = totalBytes - freeBytes;
} catch (IOException e) {
Log.w(TAG, e);
}
} else {
// StorageStatsManager can only query private volumes.
// Default to previous storage calculation for public volumes.
if (totalBytes <= 0) {
totalBytes = path.getTotalSpace();
}
freeBytes = path.getFreeSpace();
usedBytes = totalBytes - freeBytes;
} }
final long freeBytes = path.getFreeSpace();
final long usedBytes = totalBytes - freeBytes;
final String used = Formatter.formatFileSize(context, usedBytes); final String used = Formatter.formatFileSize(context, usedBytes);
final String total = Formatter.formatFileSize(context, totalBytes); final String total = Formatter.formatFileSize(context, totalBytes);