Merge "Follow storage API polishing." into oc-dev

This commit is contained in:
Jeff Sharkey
2017-04-17 22:26:35 +00:00
committed by Android (Google) Code Review
7 changed files with 32 additions and 11 deletions

View File

@@ -19,6 +19,7 @@ package com.android.settings.applications;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.content.Context; import android.content.Context;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.UserHandle; import android.os.UserHandle;
import android.util.Log; import android.util.Log;
@@ -27,6 +28,8 @@ import com.android.settings.utils.AsyncLoader;
import com.android.settingslib.applications.StorageStatsSource; import com.android.settingslib.applications.StorageStatsSource;
import com.android.settingslib.applications.StorageStatsSource.AppStorageStats; import com.android.settingslib.applications.StorageStatsSource.AppStorageStats;
import java.io.IOException;
/** /**
* Fetches the storage stats using the StorageStatsManager for a given package and user tuple. * Fetches the storage stats using the StorageStatsManager for a given package and user tuple.
*/ */
@@ -49,7 +52,7 @@ public class FetchPackageStorageAsyncLoader extends AsyncLoader<AppStorageStats>
AppStorageStats result = null; AppStorageStats result = null;
try { try {
result = mSource.getStatsForPackage(mInfo.volumeUuid, mInfo.packageName, mUser); result = mSource.getStatsForPackage(mInfo.volumeUuid, mInfo.packageName, mUser);
} catch (IllegalStateException e) { } catch (NameNotFoundException | IOException e) {
Log.w(TAG, "Package may have been removed during query, failing gracefully", e); Log.w(TAG, "Package may have been removed during query, failing gracefully", e);
} }
return result; return result;

View File

@@ -23,15 +23,20 @@ import android.os.UserHandle;
import android.provider.DocumentsContract; import android.provider.DocumentsContract;
import android.support.annotation.WorkerThread; import android.support.annotation.WorkerThread;
import android.text.format.Formatter; import android.text.format.Formatter;
import android.util.Log;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.Utils; import com.android.settings.Utils;
import com.android.settingslib.applications.StorageStatsSource; import com.android.settingslib.applications.StorageStatsSource;
import java.io.IOException;
/** /**
* MusicViewHolderController controls an Audio/Music file view in the ManageApplications view. * MusicViewHolderController controls an Audio/Music file view in the ManageApplications view.
*/ */
public class MusicViewHolderController implements FileViewHolderController { public class MusicViewHolderController implements FileViewHolderController {
private static final String TAG = "MusicViewHolderController";
private static final String AUTHORITY_MEDIA = "com.android.providers.media.documents"; private static final String AUTHORITY_MEDIA = "com.android.providers.media.documents";
private Context mContext; private Context mContext;
@@ -51,7 +56,12 @@ public class MusicViewHolderController implements FileViewHolderController {
@Override @Override
@WorkerThread @WorkerThread
public void queryStats() { public void queryStats() {
mMusicSize = mSource.getExternalStorageStats(mVolumeUuid, mUser).audioBytes; try {
mMusicSize = mSource.getExternalStorageStats(mVolumeUuid, mUser).audioBytes;
} catch (IOException e) {
mMusicSize = 0;
Log.w(TAG, e);
}
} }
@Override @Override

View File

@@ -23,6 +23,7 @@ import static android.content.pm.ApplicationInfo.CATEGORY_VIDEO;
import android.content.Context; import android.content.Context;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.UserInfo; import android.content.pm.UserInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.UserHandle; import android.os.UserHandle;
import android.util.Log; import android.util.Log;
import android.util.SparseArray; import android.util.SparseArray;
@@ -32,6 +33,7 @@ import com.android.settings.applications.UserManagerWrapper;
import com.android.settings.utils.AsyncLoader; import com.android.settings.utils.AsyncLoader;
import com.android.settingslib.applications.StorageStatsSource; import com.android.settingslib.applications.StorageStatsSource;
import java.io.IOException;
import java.util.List; import java.util.List;
/** /**
@@ -83,7 +85,7 @@ public class StorageAsyncLoader
StorageStatsSource.AppStorageStats stats; StorageStatsSource.AppStorageStats stats;
try { try {
stats = mStatsManager.getStatsForPackage(mUuid, app.packageName, myUser); stats = mStatsManager.getStatsForPackage(mUuid, app.packageName, myUser);
} catch (IllegalStateException e) { } catch (NameNotFoundException | IOException e) {
// This may happen if the package was removed during our calculation. // This may happen if the package was removed during our calculation.
Log.w("App unexpectedly not found", e); Log.w("App unexpectedly not found", e);
continue; continue;
@@ -122,7 +124,11 @@ public class StorageAsyncLoader
} }
Log.d(TAG, "Loading external stats"); Log.d(TAG, "Loading external stats");
result.externalStats = mStatsManager.getExternalStorageStats(mUuid, UserHandle.of(userId)); try {
result.externalStats = mStatsManager.getExternalStorageStats(mUuid, UserHandle.of(userId));
} catch (IOException e) {
Log.w(TAG, e);
}
Log.d(TAG, "Obtaining result completed"); Log.d(TAG, "Obtaining result completed");
return result; return result;
} }

View File

@@ -40,6 +40,8 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import java.io.IOException;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class FetchPackageStorageAsyncLoaderTest { public class FetchPackageStorageAsyncLoaderTest {
@@ -56,7 +58,7 @@ public class FetchPackageStorageAsyncLoaderTest {
} }
@Test @Test
public void worksForValidPackageNameAndUid() { public void worksForValidPackageNameAndUid() throws Exception {
AppStorageStats stats = mock(AppStorageStats.class); AppStorageStats stats = mock(AppStorageStats.class);
when(stats.getCodeBytes()).thenReturn(1L); when(stats.getCodeBytes()).thenReturn(1L);
when(stats.getDataBytes()).thenReturn(2L); when(stats.getDataBytes()).thenReturn(2L);
@@ -72,9 +74,9 @@ public class FetchPackageStorageAsyncLoaderTest {
} }
@Test @Test
public void installerExceptionHandledCleanly() { public void installerExceptionHandledCleanly() throws Exception {
when(mSource.getStatsForPackage(anyString(), anyString(), any(UserHandle.class))). when(mSource.getStatsForPackage(anyString(), anyString(), any(UserHandle.class))).
thenThrow(new IllegalStateException("intentional failure")); thenThrow(new IOException("intentional failure"));
ApplicationInfo info = new ApplicationInfo(); ApplicationInfo info = new ApplicationInfo();
info.packageName = PACKAGE_NAME; info.packageName = PACKAGE_NAME;
FetchPackageStorageAsyncLoader task = new FetchPackageStorageAsyncLoader( FetchPackageStorageAsyncLoader task = new FetchPackageStorageAsyncLoader(

View File

@@ -81,7 +81,7 @@ public class MusicViewHolderControllerTest {
} }
@Test @Test
public void storageShouldRepresentStorageStatsQuery() { public void storageShouldRepresentStorageStatsQuery() throws Exception {
when(mSource.getExternalStorageStats(any(String.class), any(UserHandle.class))).thenReturn( when(mSource.getExternalStorageStats(any(String.class), any(UserHandle.class))).thenReturn(
new StorageStatsSource.ExternalStorageStats(1, 1, 0, 0)); new StorageStatsSource.ExternalStorageStats(1, 1, 0, 0));

View File

@@ -70,7 +70,7 @@ public class StorageSettingsTest {
} }
@Test @Test
public void updateSummary_shouldDisplayUsedPercentAndFreeSpace() { public void updateSummary_shouldDisplayUsedPercentAndFreeSpace() throws Exception {
final SummaryLoader loader = mock(SummaryLoader.class); final SummaryLoader loader = mock(SummaryLoader.class);
final SummaryLoader.SummaryProvider provider = final SummaryLoader.SummaryProvider provider =
StorageSettings.SUMMARY_PROVIDER_FACTORY.createSummaryProvider(mActivity, loader); StorageSettings.SUMMARY_PROVIDER_FACTORY.createSummaryProvider(mActivity, loader);

View File

@@ -194,8 +194,8 @@ public class StorageAsyncLoaderTest {
// Should not crash. // Should not crash.
} }
private ApplicationInfo addPackage( private ApplicationInfo addPackage(String packageName, long cacheSize, long codeSize,
String packageName, long cacheSize, long codeSize, long dataSize, int category) { long dataSize, int category) throws Exception {
StorageStatsSource.AppStorageStats storageStats = StorageStatsSource.AppStorageStats storageStats =
mock(StorageStatsSource.AppStorageStats.class); mock(StorageStatsSource.AppStorageStats.class);
when(storageStats.getCodeBytes()).thenReturn(codeSize); when(storageStats.getCodeBytes()).thenReturn(codeSize);