Merge "Revert "Use the StorageStatsManager in FileCollector."" into oc-dev
am: 864d53d6fc
Change-Id: I21cf2b0a245cfeaf0c788b3c869b441776ec5d0c
This commit is contained in:
@@ -73,6 +73,7 @@ public class DiskStatsLoggingService extends JobService {
|
||||
final int userId = UserHandle.myUserId();
|
||||
UserEnvironment environment = new UserEnvironment(userId);
|
||||
LogRunnable task = new LogRunnable();
|
||||
task.setRootDirectory(environment.getExternalStorageDirectory());
|
||||
task.setDownloadsDirectory(
|
||||
environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
|
||||
task.setSystemSize(FileCollector.getSystemSize(this));
|
||||
@@ -126,10 +127,14 @@ public class DiskStatsLoggingService extends JobService {
|
||||
private JobParameters mParams;
|
||||
private AppCollector mCollector;
|
||||
private File mOutputFile;
|
||||
private File mRootDirectory;
|
||||
private File mDownloadsDirectory;
|
||||
private Context mContext;
|
||||
private long mSystemSize;
|
||||
|
||||
public void setRootDirectory(File file) {
|
||||
mRootDirectory = file;
|
||||
}
|
||||
|
||||
public void setDownloadsDirectory(File file) {
|
||||
mDownloadsDirectory = file;
|
||||
}
|
||||
@@ -146,25 +151,14 @@ public class DiskStatsLoggingService extends JobService {
|
||||
mSystemSize = size;
|
||||
}
|
||||
|
||||
public void setContext(Context context) {
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
public void setJobService(JobService jobService, JobParameters params) {
|
||||
mJobService = jobService;
|
||||
mParams = params;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
FileCollector.MeasurementResult mainCategories;
|
||||
try {
|
||||
mainCategories = FileCollector.getMeasurementResult(mContext);
|
||||
} catch (IllegalStateException e) {
|
||||
// This can occur if installd has an issue.
|
||||
Log.e(TAG, "Error while measuring storage", e);
|
||||
finishJob(true);
|
||||
return;
|
||||
}
|
||||
FileCollector.MeasurementResult mainCategories =
|
||||
FileCollector.getMeasurementResult(mRootDirectory);
|
||||
FileCollector.MeasurementResult downloads =
|
||||
FileCollector.getMeasurementResult(mDownloadsDirectory);
|
||||
|
||||
@@ -174,10 +168,12 @@ public class DiskStatsLoggingService extends JobService {
|
||||
needsReschedule = false;
|
||||
logToFile(mainCategories, downloads, stats, mSystemSize);
|
||||
} else {
|
||||
Log.w(TAG, "Timed out while fetching package stats.");
|
||||
Log.w("TAG", "Timed out while fetching package stats.");
|
||||
}
|
||||
|
||||
finishJob(needsReschedule);
|
||||
if (mJobService != null) {
|
||||
mJobService.jobFinished(mParams, needsReschedule);
|
||||
}
|
||||
}
|
||||
|
||||
private void logToFile(MeasurementResult mainCategories, MeasurementResult downloads,
|
||||
@@ -191,11 +187,5 @@ public class DiskStatsLoggingService extends JobService {
|
||||
Log.e(TAG, "Exception while writing opportunistic disk file cache.", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void finishJob(boolean needsReschedule) {
|
||||
if (mJobService != null) {
|
||||
mJobService.jobFinished(mParams, needsReschedule);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,11 +17,8 @@
|
||||
package com.android.server.storage;
|
||||
|
||||
import android.annotation.IntDef;
|
||||
import android.app.usage.ExternalStorageStats;
|
||||
import android.app.usage.StorageStatsManager;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.UserHandle;
|
||||
import android.os.storage.StorageManager;
|
||||
import android.os.storage.VolumeInfo;
|
||||
import android.util.ArrayMap;
|
||||
@@ -156,30 +153,9 @@ public class FileCollector {
|
||||
new MeasurementResult());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the file categorization result for the primary internal storage UUID.
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public static MeasurementResult getMeasurementResult(Context context) {
|
||||
MeasurementResult result = new MeasurementResult();
|
||||
StorageStatsManager ssm =
|
||||
(StorageStatsManager) context.getSystemService(Context.STORAGE_STATS_SERVICE);
|
||||
ExternalStorageStats stats =
|
||||
ssm.queryExternalStatsForUser(
|
||||
StorageManager.UUID_PRIVATE_INTERNAL, UserHandle.of(context.getUserId()));
|
||||
result.imagesSize = stats.getImageBytes();
|
||||
result.videosSize = stats.getVideoBytes();
|
||||
result.audioSize = stats.getAudioBytes();
|
||||
result.miscSize =
|
||||
stats.getTotalBytes() - result.imagesSize - result.videosSize - result.audioSize;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size of a system for a given context. This is done by finding the difference
|
||||
* between the shared data and the total primary storage size.
|
||||
*
|
||||
* @param context Context to use to get storage information.
|
||||
*/
|
||||
public static long getSystemSize(Context context) {
|
||||
|
||||
@@ -20,18 +20,15 @@ import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.anyLong;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.isNull;
|
||||
import static org.mockito.Mockito.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.job.JobService;
|
||||
import android.app.usage.ExternalStorageStats;
|
||||
import android.app.usage.StorageStatsManager;
|
||||
import android.app.job.JobParameters;
|
||||
import android.content.pm.PackageStats;
|
||||
import android.os.UserHandle;
|
||||
import android.test.AndroidTestCase;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.server.storage.DiskStatsLoggingService.LogRunnable;
|
||||
|
||||
@@ -55,10 +52,8 @@ import java.util.ArrayList;
|
||||
public class DiskStatsLoggingServiceTest extends AndroidTestCase {
|
||||
@Rule public TemporaryFolder mTemporaryFolder;
|
||||
@Rule public TemporaryFolder mDownloads;
|
||||
@Rule public TemporaryFolder mRootDirectory;
|
||||
@Mock private AppCollector mCollector;
|
||||
@Mock private JobService mJobService;
|
||||
@Mock private StorageStatsManager mSsm;
|
||||
private ExternalStorageStats mStorageStats;
|
||||
private File mInputFile;
|
||||
|
||||
|
||||
@@ -71,10 +66,8 @@ public class DiskStatsLoggingServiceTest extends AndroidTestCase {
|
||||
mInputFile = mTemporaryFolder.newFile();
|
||||
mDownloads = new TemporaryFolder();
|
||||
mDownloads.create();
|
||||
mStorageStats = new ExternalStorageStats();
|
||||
when(mSsm.queryExternalStatsForUser(isNull(String.class), any(UserHandle.class)))
|
||||
.thenReturn(mStorageStats);
|
||||
when(mJobService.getSystemService(anyString())).thenReturn(mSsm);
|
||||
mRootDirectory = new TemporaryFolder();
|
||||
mRootDirectory.create();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -82,9 +75,9 @@ public class DiskStatsLoggingServiceTest extends AndroidTestCase {
|
||||
LogRunnable task = new LogRunnable();
|
||||
task.setAppCollector(mCollector);
|
||||
task.setDownloadsDirectory(mDownloads.getRoot());
|
||||
task.setRootDirectory(mRootDirectory.getRoot());
|
||||
task.setLogOutputFile(mInputFile);
|
||||
task.setSystemSize(0L);
|
||||
task.setContext(mJobService);
|
||||
task.run();
|
||||
|
||||
JSONObject json = getJsonOutput();
|
||||
@@ -106,10 +99,10 @@ public class DiskStatsLoggingServiceTest extends AndroidTestCase {
|
||||
public void testPopulatedLogTask() throws Exception {
|
||||
// Write data to directories.
|
||||
writeDataToFile(mDownloads.newFile(), "lol");
|
||||
mStorageStats.audioBytes = 6L;
|
||||
mStorageStats.imageBytes = 4L;
|
||||
mStorageStats.videoBytes = 5L;
|
||||
mStorageStats.totalBytes = 22L;
|
||||
writeDataToFile(mRootDirectory.newFile("test.jpg"), "1234");
|
||||
writeDataToFile(mRootDirectory.newFile("test.mp4"), "12345");
|
||||
writeDataToFile(mRootDirectory.newFile("test.mp3"), "123456");
|
||||
writeDataToFile(mRootDirectory.newFile("test.whatever"), "1234567");
|
||||
|
||||
// Write apps.
|
||||
ArrayList<PackageStats> apps = new ArrayList<>();
|
||||
@@ -117,16 +110,15 @@ public class DiskStatsLoggingServiceTest extends AndroidTestCase {
|
||||
testApp.dataSize = 5L;
|
||||
testApp.cacheSize = 55L;
|
||||
testApp.codeSize = 10L;
|
||||
testApp.userHandle = UserHandle.USER_SYSTEM;
|
||||
apps.add(testApp);
|
||||
when(mCollector.getPackageStats(anyLong())).thenReturn(apps);
|
||||
when(mCollector.getPackageStats(anyInt())).thenReturn(apps);
|
||||
|
||||
LogRunnable task = new LogRunnable();
|
||||
task.setAppCollector(mCollector);
|
||||
task.setDownloadsDirectory(mDownloads.getRoot());
|
||||
task.setRootDirectory(mRootDirectory.getRoot());
|
||||
task.setLogOutputFile(mInputFile);
|
||||
task.setSystemSize(10L);
|
||||
task.setContext(mJobService);
|
||||
task.run();
|
||||
|
||||
JSONObject json = getJsonOutput();
|
||||
@@ -151,9 +143,9 @@ public class DiskStatsLoggingServiceTest extends AndroidTestCase {
|
||||
LogRunnable task = new LogRunnable();
|
||||
task.setAppCollector(mCollector);
|
||||
task.setDownloadsDirectory(mDownloads.getRoot());
|
||||
task.setRootDirectory(mRootDirectory.getRoot());
|
||||
task.setLogOutputFile(mInputFile);
|
||||
task.setSystemSize(10L);
|
||||
task.setContext(mJobService);
|
||||
task.run();
|
||||
|
||||
// No exception should be thrown.
|
||||
|
||||
Reference in New Issue
Block a user