From 5d9bc0c75ed8f5b8dea7279ff3dd4a7c4fe3a3dc Mon Sep 17 00:00:00 2001 From: junyulai Date: Tue, 31 Mar 2020 14:56:01 +0800 Subject: [PATCH] [SP26.2] add a method in NetworkStats that removes empty entries In current design, entries with zeros are preserved after addition/subtraction. These entries are not very useful and lead to difficulty of verifying the result of addition/subtraction. However, change the behavior in the original NetworkStats is considered risky in current stage. Thus, this change provide a function that could remove these empty entries in tests. Test: atest FrameworksNetTests Bug: 152827872 Bug: 150644692 Change-Id: I40a76935d55712b8083ee1e17e137a8a4ef5e029 --- core/java/android/net/NetworkStats.java | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/core/java/android/net/NetworkStats.java b/core/java/android/net/NetworkStats.java index 9c1fb41ecc..b7fb280efd 100644 --- a/core/java/android/net/NetworkStats.java +++ b/core/java/android/net/NetworkStats.java @@ -1195,18 +1195,24 @@ public final class NetworkStats implements Parcelable { /** * Remove all rows that match one of specified UIDs. + * This mutates the original structure in place. * @hide */ public void removeUids(int[] uids) { - int nextOutputEntry = 0; - for (int i = 0; i < size; i++) { - if (!ArrayUtils.contains(uids, uid[i])) { - maybeCopyEntry(nextOutputEntry, i); - nextOutputEntry++; - } - } + filter(e -> !ArrayUtils.contains(uids, e.uid)); + } - size = nextOutputEntry; + /** + * Remove all rows that match one of specified UIDs. + * @return the result object. + * @hide + */ + @NonNull + public NetworkStats removeEmptyEntries() { + final NetworkStats ret = this.clone(); + ret.filter(e -> e.rxBytes != 0 || e.rxPackets != 0 || e.txBytes != 0 || e.txPackets != 0 + || e.operations != 0); + return ret; } /**