From 905f034c57310f1cfe2253a27dc38acff166dc24 Mon Sep 17 00:00:00 2001 From: Chenbo Feng Date: Thu, 25 Jan 2018 11:43:52 -0800 Subject: [PATCH] Return non-negetive value in getMobileStats method The current implementation of getMobileRxBytes and all the similiar method adds up the return values for multiple calls to getRxBytes so if all of them return UNSUPPORTED for any reason, getMobileRxBytes() would return a value such as -3. This behavior is not compliance with the cts TrafficStatsTest which always assume getMobileRxBytes to return a non-negetive value. The method now will check tha stats get from getRxBytes method and add them up only if the stats is valid. Bug: 72473294 Test: run cts -m CtsNetTestCases -t android.net.cts.TrafficStatsTest Change-Id: I656970ebc8f6506cf17c4353ad46c0178bb65cfd --- core/java/android/net/TrafficStats.java | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/core/java/android/net/TrafficStats.java b/core/java/android/net/TrafficStats.java index 196a3bc9c8..fa4624ef56 100644 --- a/core/java/android/net/TrafficStats.java +++ b/core/java/android/net/TrafficStats.java @@ -433,6 +433,10 @@ public class TrafficStats { } } + private static long addIfSupported(long stat) { + return (stat == UNSUPPORTED) ? 0 : stat; + } + /** * Return number of packets transmitted across mobile networks since device * boot. Counts packets across all mobile network interfaces, and always @@ -445,7 +449,7 @@ public class TrafficStats { public static long getMobileTxPackets() { long total = 0; for (String iface : getMobileIfaces()) { - total += getTxPackets(iface); + total += addIfSupported(getTxPackets(iface)); } return total; } @@ -462,7 +466,7 @@ public class TrafficStats { public static long getMobileRxPackets() { long total = 0; for (String iface : getMobileIfaces()) { - total += getRxPackets(iface); + total += addIfSupported(getRxPackets(iface)); } return total; } @@ -479,7 +483,7 @@ public class TrafficStats { public static long getMobileTxBytes() { long total = 0; for (String iface : getMobileIfaces()) { - total += getTxBytes(iface); + total += addIfSupported(getTxBytes(iface)); } return total; } @@ -496,7 +500,7 @@ public class TrafficStats { public static long getMobileRxBytes() { long total = 0; for (String iface : getMobileIfaces()) { - total += getRxBytes(iface); + total += addIfSupported(getRxBytes(iface)); } return total; } @@ -511,9 +515,7 @@ public class TrafficStats { } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } - if (stat != UNSUPPORTED) { - total += stat; - } + total += addIfSupported(stat); } return total; } @@ -528,9 +530,7 @@ public class TrafficStats { } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } - if (stat != UNSUPPORTED) { - total += stat; - } + total += addIfSupported(stat); } return total; }