packages/apps/Settings
修訂 | b22b85b593f63d38d29718ecd37e6a260011584e (tree) |
---|---|
時間 | 2017-06-23 11:08:48 |
作者 | Daniel Nishi <dhnishi@goog...> |
Commiter | Daniel Nishi |
Count cache in storage categories.
This makes the System size more consistent because cache is not
attributed to the system (when the cache is under quota).
Change-Id: I680e70daf5e98b9a205023a218dfd1dcc8ee8334
Fixes: 62623731
Test: Settings Unit Test
@@ -105,7 +105,17 @@ public class StorageAsyncLoader | ||
105 | 105 | continue; |
106 | 106 | } |
107 | 107 | |
108 | - long blamedSize = stats.getDataBytes() - stats.getCacheBytes(); | |
108 | + final long dataSize = stats.getDataBytes(); | |
109 | + final long cacheQuota = mStatsManager.getCacheQuotaBytes(mUuid, app.uid); | |
110 | + final long cacheBytes = stats.getCacheBytes(); | |
111 | + long blamedSize = dataSize; | |
112 | + // Technically, we could overages as freeable on the storage settings screen. | |
113 | + // If the app is using more cache than its quota, we would accidentally subtract the | |
114 | + // overage from the system size (because it shows up as unused) during our attribution. | |
115 | + // Thus, we cap the attribution at the quota size. | |
116 | + if (cacheQuota < cacheBytes) { | |
117 | + blamedSize = blamedSize - cacheBytes + cacheQuota; | |
118 | + } | |
109 | 119 | |
110 | 120 | // This isn't quite right because it slams the first user by user id with the whole code |
111 | 121 | // size, but this ensures that we count all apps seen once. |
@@ -29,6 +29,7 @@ import android.content.Context; | ||
29 | 29 | import android.content.pm.ApplicationInfo; |
30 | 30 | import android.content.pm.PackageManager.NameNotFoundException; |
31 | 31 | import android.content.pm.UserInfo; |
32 | +import android.net.TrafficStats; | |
32 | 33 | import android.os.UserHandle; |
33 | 34 | import android.support.test.filters.SmallTest; |
34 | 35 | import android.support.test.runner.AndroidJUnit4; |
@@ -55,6 +56,7 @@ public class StorageAsyncLoaderTest { | ||
55 | 56 | private static final int SECONDARY_USER_ID = 10; |
56 | 57 | private static final String PACKAGE_NAME_1 = "com.blah.test"; |
57 | 58 | private static final String PACKAGE_NAME_2 = "com.blah.test2"; |
59 | + private static final long DEFAULT_QUOTA = 64 * TrafficStats.MB_IN_BYTES; | |
58 | 60 | |
59 | 61 | @Mock |
60 | 62 | private StorageStatsSource mSource; |
@@ -81,6 +83,7 @@ public class StorageAsyncLoaderTest { | ||
81 | 83 | mUsers = new ArrayList<>(); |
82 | 84 | mUsers.add(info); |
83 | 85 | when(mUserManager.getUsers()).thenReturn(mUsers); |
86 | + when(mSource.getCacheQuotaBytes(anyString(), anyInt())).thenReturn(DEFAULT_QUOTA); | |
84 | 87 | } |
85 | 88 | |
86 | 89 | @Test |
@@ -120,13 +123,13 @@ public class StorageAsyncLoaderTest { | ||
120 | 123 | } |
121 | 124 | |
122 | 125 | @Test |
123 | - public void testCacheIsIgnored() throws Exception { | |
126 | + public void testCacheIsNotIgnored() throws Exception { | |
124 | 127 | addPackage(PACKAGE_NAME_1, 100, 1, 10, ApplicationInfo.CATEGORY_UNDEFINED); |
125 | 128 | |
126 | 129 | SparseArray<StorageAsyncLoader.AppsStorageResult> result = mLoader.loadInBackground(); |
127 | 130 | |
128 | 131 | assertThat(result.size()).isEqualTo(1); |
129 | - assertThat(result.get(PRIMARY_USER_ID).otherAppsSize).isEqualTo(11L); | |
132 | + assertThat(result.get(PRIMARY_USER_ID).otherAppsSize).isEqualTo(111L); | |
130 | 133 | } |
131 | 134 | |
132 | 135 | @Test |
@@ -155,7 +158,7 @@ public class StorageAsyncLoaderTest { | ||
155 | 158 | SparseArray<StorageAsyncLoader.AppsStorageResult> result = mLoader.loadInBackground(); |
156 | 159 | |
157 | 160 | assertThat(result.size()).isEqualTo(1); |
158 | - assertThat(result.get(PRIMARY_USER_ID).otherAppsSize).isEqualTo(11L); | |
161 | + assertThat(result.get(PRIMARY_USER_ID).otherAppsSize).isEqualTo(111L); | |
159 | 162 | } |
160 | 163 | |
161 | 164 | @Test |
@@ -207,12 +210,23 @@ public class StorageAsyncLoaderTest { | ||
207 | 210 | assertThat(result.get(SECONDARY_USER_ID).videoAppsSize).isEqualTo(10L); |
208 | 211 | } |
209 | 212 | |
213 | + @Test | |
214 | + public void testCacheOveragesAreCountedAsFree() throws Exception { | |
215 | + addPackage(PACKAGE_NAME_1, DEFAULT_QUOTA + 100, 1, 10, ApplicationInfo.CATEGORY_UNDEFINED); | |
216 | + | |
217 | + SparseArray<StorageAsyncLoader.AppsStorageResult> result = mLoader.loadInBackground(); | |
218 | + | |
219 | + assertThat(result.size()).isEqualTo(1); | |
220 | + assertThat(result.get(PRIMARY_USER_ID).otherAppsSize).isEqualTo(DEFAULT_QUOTA + 11); | |
221 | + } | |
222 | + | |
210 | 223 | private ApplicationInfo addPackage(String packageName, long cacheSize, long codeSize, |
211 | 224 | long dataSize, int category) throws Exception { |
212 | 225 | StorageStatsSource.AppStorageStats storageStats = |
213 | 226 | mock(StorageStatsSource.AppStorageStats.class); |
214 | 227 | when(storageStats.getCodeBytes()).thenReturn(codeSize); |
215 | - when(storageStats.getDataBytes()).thenReturn(dataSize); | |
228 | + when(storageStats.getDataBytes()).thenReturn(dataSize + cacheSize); | |
229 | + when(storageStats.getCacheBytes()).thenReturn(cacheSize); | |
216 | 230 | when(mSource.getStatsForPackage(anyString(), eq(packageName), any(UserHandle.class))) |
217 | 231 | .thenReturn(storageStats); |
218 | 232 |