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