Add more sane multi-profile app attribution.

Due to issues w.r.t. attribution across multiple users, we originally
duplicated the previous implementation which zeroed out the sizes. This,
however, caused system bloat due to the change in how we calculate the
system size.

By attributing apps which do not exist in the primary profile to the
first user that shows up with it installed, we can avoid accidentally
attributing it to the system size.

Bug: 62623731
Test: Settings unittest & Settings robotest
Change-Id: I9514c9ecef62ea6270723f62e6bf27c69b75f180
This commit is contained in:
Daniel Nishi
2017-06-15 17:19:31 -07:00
parent 327b9119d7
commit b088010d12
5 changed files with 60 additions and 25 deletions

View File

@@ -27,6 +27,7 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.support.test.filters.SmallTest;
@@ -74,7 +75,8 @@ public class StorageAsyncLoaderTest {
MockitoAnnotations.initMocks(this);
mInfo = new ArrayList<>();
mLoader = new StorageAsyncLoader(mContext, mUserManager, "id", mSource, mPackageManager);
when(mPackageManager.getInstalledApplicationsAsUser(anyInt(), anyInt())).thenReturn(mInfo);
when(mPackageManager.getInstalledApplicationsAsUser(eq(PRIMARY_USER_ID), anyInt()))
.thenReturn(mInfo);
UserInfo info = new UserInfo();
mUsers = new ArrayList<>();
mUsers.add(info);
@@ -174,13 +176,37 @@ public class StorageAsyncLoaderTest {
info.category = ApplicationInfo.CATEGORY_UNDEFINED;
mInfo.add(info);
when(mSource.getStatsForPackage(anyString(), anyString(), any(UserHandle.class)))
.thenThrow(new IllegalStateException());
.thenThrow(new NameNotFoundException());
SparseArray<StorageAsyncLoader.AppsStorageResult> result = mLoader.loadInBackground();
// Should not crash.
}
@Test
public void testPackageIsNotDoubleCounted() throws Exception {
UserInfo info = new UserInfo();
info.id = SECONDARY_USER_ID;
mUsers.add(info);
when(mSource.getExternalStorageStats(anyString(), eq(UserHandle.SYSTEM)))
.thenReturn(new StorageStatsSource.ExternalStorageStats(9, 2, 3, 4, 0));
when(mSource.getExternalStorageStats(anyString(), eq(new UserHandle(SECONDARY_USER_ID))))
.thenReturn(new StorageStatsSource.ExternalStorageStats(10, 3, 3, 4, 0));
addPackage(PACKAGE_NAME_1, 0, 1, 10, ApplicationInfo.CATEGORY_VIDEO);
ArrayList<ApplicationInfo> secondaryUserApps = new ArrayList<>();
ApplicationInfo appInfo = new ApplicationInfo();
appInfo.packageName = PACKAGE_NAME_1;
appInfo.category = ApplicationInfo.CATEGORY_VIDEO;
secondaryUserApps.add(appInfo);
SparseArray<StorageAsyncLoader.AppsStorageResult> result = mLoader.loadInBackground();
assertThat(result.size()).isEqualTo(2);
assertThat(result.get(PRIMARY_USER_ID).videoAppsSize).isEqualTo(11L);
// No code size for the second user.
assertThat(result.get(SECONDARY_USER_ID).videoAppsSize).isEqualTo(10L);
}
private ApplicationInfo addPackage(String packageName, long cacheSize, long codeSize,
long dataSize, int category) throws Exception {
StorageStatsSource.AppStorageStats storageStats =
@@ -196,4 +222,5 @@ public class StorageAsyncLoaderTest {
mInfo.add(info);
return info;
}
}