Refactoring in memory storage stats
Removed hard coded NUM_MEDIA_DIRS_TRACKED - 1 in loops and used actual constants instead to make it less brittle if we add more categories. Change-Id: I51ce9e7faca1ee55cf369eedf9422628b9501e13
This commit is contained in:
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package com.android.settings.deviceinfo;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import android.os.Environment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -36,10 +38,12 @@ class Constants {
|
||||
final String[] mDirPaths;
|
||||
final String mKey;
|
||||
final String mPreferenceName;
|
||||
MediaDirectory(String pref, String debugInfo, String... paths) {
|
||||
mDirPaths = paths;
|
||||
mKey = debugInfo;
|
||||
final int mColor; // Required when mPreferenceName is not null
|
||||
MediaDirectory(String pref, String debugInfo, int color, String... paths) {
|
||||
mPreferenceName = pref;
|
||||
mKey = debugInfo;
|
||||
mColor = color;
|
||||
mDirPaths = paths;
|
||||
}
|
||||
}
|
||||
static final ArrayList<MediaDirectory> mMediaDirs = new ArrayList<MediaDirectory>();
|
||||
@@ -48,15 +52,18 @@ class Constants {
|
||||
mMediaDirs.add(MEDIA_INDEX,
|
||||
new MediaDirectory(null,
|
||||
"/sdcard",
|
||||
0,
|
||||
Environment.getExternalStorageDirectory().getAbsolutePath()));
|
||||
mMediaDirs.add(DOWNLOADS_INDEX,
|
||||
new MediaDirectory("memory_internal_downloads",
|
||||
"/sdcard/download",
|
||||
R.color.memory_downloads,
|
||||
Environment.getExternalStoragePublicDirectory(
|
||||
Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()));
|
||||
mMediaDirs.add(PIC_VIDEO_INDEX,
|
||||
new MediaDirectory("memory_internal_dcim",
|
||||
"/sdcard/pic_video",
|
||||
R.color.memory_video,
|
||||
Environment.getExternalStoragePublicDirectory(
|
||||
Environment.DIRECTORY_DCIM).getAbsolutePath(),
|
||||
Environment.getExternalStoragePublicDirectory(
|
||||
@@ -66,6 +73,7 @@ class Constants {
|
||||
mMediaDirs.add(MUSIC_INDEX,
|
||||
new MediaDirectory("memory_internal_music",
|
||||
"/sdcard/audio",
|
||||
R.color.memory_audio,
|
||||
Environment.getExternalStoragePublicDirectory(
|
||||
Environment.DIRECTORY_MUSIC).getAbsolutePath(),
|
||||
Environment.getExternalStoragePublicDirectory(
|
||||
@@ -79,17 +87,18 @@ class Constants {
|
||||
mMediaDirs.add(MEDIA_APPS_DATA_INDEX,
|
||||
new MediaDirectory(null,
|
||||
"/sdcard/Android",
|
||||
0,
|
||||
Environment.getExternalStorageAndroidDataDir().getAbsolutePath()));
|
||||
mMediaDirs.add(MEDIA_MISC_INDEX,
|
||||
new MediaDirectory("memory_internal_media_misc",
|
||||
"misc on /sdcard",
|
||||
"not relevant"));
|
||||
R.color.memory_misc,
|
||||
new String[] {})); // No associated directory to add to exclusion list
|
||||
// prepare a lit of strings representing dirpaths that should be skipped while looking
|
||||
// for 'other' files
|
||||
for (int j = 0; j < Constants.NUM_MEDIA_DIRS_TRACKED - 1; j++) {
|
||||
for (int j = 0; j < Constants.NUM_MEDIA_DIRS_TRACKED; j++) {
|
||||
String[] dirs = Constants.mMediaDirs.get(j).mDirPaths;
|
||||
int len = dirs.length;
|
||||
if (len > 0) {
|
||||
for (int k = 0; k < len; k++) {
|
||||
ExclusionTargetsForMiscFiles.add(dirs[k]);
|
||||
}
|
||||
@@ -98,5 +107,4 @@ class Constants {
|
||||
ExclusionTargetsForMiscFiles.add(
|
||||
Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -18,6 +18,7 @@ package com.android.settings.deviceinfo;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsPreferenceFragment;
|
||||
import com.android.settings.deviceinfo.Constants.MediaDirectory;
|
||||
import com.android.settings.deviceinfo.MemoryMeasurement.MeasurementReceiver;
|
||||
|
||||
import android.app.ActivityManager;
|
||||
@@ -191,25 +192,13 @@ public class Memory extends SettingsPreferenceFragment implements OnCancelListen
|
||||
// space used by individual major directories on /sdcard
|
||||
for (int i = 0; i < Constants.NUM_MEDIA_DIRS_TRACKED; i++) {
|
||||
// nothing to be displayed for certain entries in Constants.mMediaDirs
|
||||
if (Constants.mMediaDirs.get(i).mPreferenceName == null) {
|
||||
final MediaDirectory mediaDirectory = Constants.mMediaDirs.get(i);
|
||||
final String preferenceName = mediaDirectory.mPreferenceName;
|
||||
if (preferenceName == null) {
|
||||
continue;
|
||||
}
|
||||
mMediaPreferences[i] = findPreference(Constants.mMediaDirs.get(i).mPreferenceName);
|
||||
int color = 0;
|
||||
switch (i) {
|
||||
case Constants.DOWNLOADS_INDEX:
|
||||
color = mRes.getColor(R.color.memory_downloads);
|
||||
break;
|
||||
case Constants.PIC_VIDEO_INDEX:
|
||||
color = mRes.getColor(R.color.memory_video);
|
||||
break;
|
||||
case Constants.MUSIC_INDEX:
|
||||
color = mRes.getColor(R.color.memory_audio);
|
||||
break;
|
||||
case Constants.MEDIA_MISC_INDEX:
|
||||
color = mRes.getColor(R.color.memory_misc);
|
||||
break;
|
||||
}
|
||||
mMediaPreferences[i] = findPreference(preferenceName);
|
||||
final int color = mRes.getColor(mediaDirectory.mColor);
|
||||
mMediaPreferences[i].setIcon(createRectShape(buttonHeight, buttonWidth, color));
|
||||
}
|
||||
mInternalUsageChart = (UsageBarPreference) findPreference(MEMORY_INTERNAL_CHART);
|
||||
@@ -443,9 +432,14 @@ public class Memory extends SettingsPreferenceFragment implements OnCancelListen
|
||||
private void updateUiExact(long totalSize, long availSize, long appsSize, long[] mediaSizes) {
|
||||
// There are other things that can take up storage, but we didn't measure it.
|
||||
// add that unaccounted-for-usage to Apps Usage
|
||||
long appsPlusRemaining = totalSize - availSize - mediaSizes[Constants.DOWNLOADS_INDEX] -
|
||||
mediaSizes[Constants.PIC_VIDEO_INDEX] - mediaSizes[Constants.MUSIC_INDEX] -
|
||||
mediaSizes[Constants.MEDIA_MISC_INDEX];
|
||||
long appsPlusRemaining = totalSize - availSize;
|
||||
for (int i = 0; i < Constants.NUM_MEDIA_DIRS_TRACKED; i++) {
|
||||
if (Constants.mMediaDirs.get(i).mPreferenceName == null) {
|
||||
continue;
|
||||
}
|
||||
appsPlusRemaining -= mediaSizes[i];
|
||||
}
|
||||
|
||||
mInternalSize.setSummary(formatSize(totalSize));
|
||||
mInternalAvail.setSummary(formatSize(availSize));
|
||||
mInternalAppsUsage.setSummary(formatSize(appsPlusRemaining));
|
||||
@@ -459,22 +453,8 @@ public class Memory extends SettingsPreferenceFragment implements OnCancelListen
|
||||
}
|
||||
this.mMediaPreferences[i].setSummary(formatSize(mediaSizes[i]));
|
||||
// don't add entry to color chart for media usage and for zero-sized dirs
|
||||
if (i != Constants.MEDIA_INDEX && mediaSizes[i] > 0) {
|
||||
int color = 0;
|
||||
switch (i) {
|
||||
case Constants.DOWNLOADS_INDEX:
|
||||
color = mRes.getColor(R.color.memory_downloads);
|
||||
break;
|
||||
case Constants.PIC_VIDEO_INDEX:
|
||||
color = mRes.getColor(R.color.memory_video);
|
||||
break;
|
||||
case Constants.MUSIC_INDEX:
|
||||
color = mRes.getColor(R.color.memory_audio);
|
||||
break;
|
||||
case Constants.MEDIA_MISC_INDEX:
|
||||
color = mRes.getColor(R.color.memory_misc);
|
||||
break;
|
||||
}
|
||||
if (mediaSizes[i] > 0) {
|
||||
final int color = mRes.getColor(Constants.mMediaDirs.get(i).mColor);
|
||||
mInternalUsageChart.addEntry(mediaSizes[i] / (float) totalSize, color);
|
||||
}
|
||||
}
|
||||
@@ -520,8 +500,7 @@ public class Memory extends SettingsPreferenceFragment implements OnCancelListen
|
||||
|
||||
} catch (IllegalArgumentException e) {
|
||||
// this can occur if the SD card is removed, but we haven't
|
||||
// received the
|
||||
// ACTION_MEDIA_REMOVED Intent yet.
|
||||
// received the ACTION_MEDIA_REMOVED Intent yet.
|
||||
status = Environment.MEDIA_REMOVED;
|
||||
}
|
||||
} else {
|
||||
|
@@ -374,11 +374,11 @@ public class MemoryMeasurement {
|
||||
return;
|
||||
}
|
||||
// measure sizes for all except "media_misc" - which is computed
|
||||
for (int i = 0; i < Constants.NUM_MEDIA_DIRS_TRACKED - 1; i++) {
|
||||
for (int i = 0; i < Constants.NUM_MEDIA_DIRS_TRACKED; i++) {
|
||||
if (i == Constants.MEDIA_MISC_INDEX) continue;
|
||||
mMediaSizes[i] = 0;
|
||||
String[] dirs = Constants.mMediaDirs.get(i).mDirPaths;
|
||||
int len = dirs.length;
|
||||
if (len > 0) {
|
||||
for (int k = 0; k < len; k++) {
|
||||
long dirSize = getSize(imcs, dirs[k]);
|
||||
mMediaSizes[i] += dirSize;
|
||||
@@ -387,11 +387,11 @@ public class MemoryMeasurement {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// compute the size of "misc"
|
||||
mMediaSizes[Constants.MEDIA_MISC_INDEX] = mMediaSizes[Constants.MEDIA_INDEX];
|
||||
for (int i = 1; i < Constants.NUM_MEDIA_DIRS_TRACKED - 1; i++) {
|
||||
for (int i = 0; i < Constants.NUM_MEDIA_DIRS_TRACKED; i++) {
|
||||
if (i == Constants.MEDIA_INDEX || i == Constants.MEDIA_MISC_INDEX) continue;
|
||||
mMediaSizes[Constants.MEDIA_MISC_INDEX] -= mMediaSizes[i];
|
||||
}
|
||||
if (LOGV) {
|
||||
@@ -421,6 +421,7 @@ public class MemoryMeasurement {
|
||||
// Sending of the message back to the MeasurementReceiver is
|
||||
// completed in the PackageObserver
|
||||
}
|
||||
|
||||
private void measureSizesOfMisc(IMediaContainerService imcs) {
|
||||
File top = Environment.getExternalStorageDirectory();
|
||||
mFileInfoForMisc = new ArrayList<FileInfo>();
|
||||
@@ -442,6 +443,7 @@ public class MemoryMeasurement {
|
||||
long dirSize = getSize(imcs, path);
|
||||
mFileInfoForMisc.add(new FileInfo(path, dirSize, counter++));
|
||||
} else {
|
||||
// Non file, non directory
|
||||
}
|
||||
}
|
||||
// sort the list of FileInfo objects collected above in descending order of their sizes
|
||||
|
Reference in New Issue
Block a user